2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 2000-2004 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
63 #include <openssl/asn1.h>
64 #include <openssl/asn1t.h>
65 #include <openssl/objects.h>
67 static int asn1_i2d_ex_primitive(ASN1_VALUE
**pval
, unsigned char **out
,
70 static int asn1_set_seq_out(STACK_OF(ASN1_VALUE
) *sk
, unsigned char **out
,
71 int skcontlen
, const ASN1_ITEM
*item
,
72 int do_sort
, int iclass
);
73 static int asn1_template_ex_i2d(ASN1_VALUE
**pval
, unsigned char **out
,
74 const ASN1_TEMPLATE
*tt
,
76 static int asn1_item_flags_i2d(ASN1_VALUE
*val
, unsigned char **out
,
77 const ASN1_ITEM
*it
, int flags
);
79 /* Top level i2d equivalents: the 'ndef' variant instructs the encoder
80 * to use indefinite length constructed encoding, where appropriate
83 int ASN1_item_ndef_i2d(ASN1_VALUE
*val
, unsigned char **out
,
86 return asn1_item_flags_i2d(val
, out
, it
, ASN1_TFLG_NDEF
);
89 int ASN1_item_i2d(ASN1_VALUE
*val
, unsigned char **out
, const ASN1_ITEM
*it
)
91 return asn1_item_flags_i2d(val
, out
, it
, 0);
94 /* Encode an ASN1 item, this is use by the
95 * standard 'i2d' function. 'out' points to
96 * a buffer to output the data to.
98 * The new i2d has one additional feature. If the output
99 * buffer is NULL (i.e. *out == NULL) then a buffer is
100 * allocated and populated with the encoding.
103 static int asn1_item_flags_i2d(ASN1_VALUE
*val
, unsigned char **out
,
104 const ASN1_ITEM
*it
, int flags
)
108 unsigned char *p
, *buf
;
110 len
= ASN1_item_ex_i2d(&val
, NULL
, it
, -1, flags
);
113 buf
= OPENSSL_malloc(len
);
117 ASN1_item_ex_i2d(&val
, &p
, it
, -1, flags
);
122 return ASN1_item_ex_i2d(&val
, out
, it
, -1, flags
);
125 /* Encode an item, taking care of IMPLICIT tagging (if any).
126 * This function performs the normal item handling: it can be
127 * used in external types.
130 int ASN1_item_ex_i2d(ASN1_VALUE
**pval
, unsigned char **out
,
131 const ASN1_ITEM
*it
, int tag
, int aclass
)
133 const ASN1_TEMPLATE
*tt
= NULL
;
134 unsigned char *p
= NULL
;
135 int i
, seqcontlen
, seqlen
, ndef
= 1;
136 const ASN1_COMPAT_FUNCS
*cf
;
137 const ASN1_EXTERN_FUNCS
*ef
;
138 const ASN1_AUX
*aux
= it
->funcs
;
139 ASN1_aux_cb
*asn1_cb
= 0;
141 if ((it
->itype
!= ASN1_ITYPE_PRIMITIVE
) && !*pval
)
144 if (aux
&& aux
->asn1_cb
)
145 asn1_cb
= aux
->asn1_cb
;
150 case ASN1_ITYPE_PRIMITIVE
:
152 return asn1_template_ex_i2d(pval
, out
, it
->templates
,
154 return asn1_i2d_ex_primitive(pval
, out
, it
, tag
, aclass
);
157 case ASN1_ITYPE_MSTRING
:
158 return asn1_i2d_ex_primitive(pval
, out
, it
, -1, aclass
);
160 case ASN1_ITYPE_CHOICE
:
161 if (asn1_cb
&& !asn1_cb(ASN1_OP_I2D_PRE
, pval
, it
, NULL
))
163 i
= asn1_get_choice_selector(pval
, it
);
164 if ((i
>= 0) && (i
< it
->tcount
))
167 const ASN1_TEMPLATE
*chtt
;
168 chtt
= it
->templates
+ i
;
169 pchval
= asn1_get_field_ptr(pval
, chtt
);
170 return asn1_template_ex_i2d(pchval
, out
, chtt
,
173 /* Fixme: error condition if selector out of range */
174 if (asn1_cb
&& !asn1_cb(ASN1_OP_I2D_POST
, pval
, it
, NULL
))
178 case ASN1_ITYPE_EXTERN
:
179 /* If new style i2d it does all the work */
181 return ef
->asn1_ex_i2d(pval
, out
, it
, tag
, aclass
);
183 case ASN1_ITYPE_COMPAT
:
184 /* old style hackery... */
188 i
= cf
->asn1_i2d(*pval
, out
);
189 /* Fixup for IMPLICIT tag: note this messes up for tags > 30,
190 * but so did the old code. Tags > 30 are very rare anyway.
192 if (out
&& (tag
!= -1))
193 *p
= aclass
| tag
| (*p
& V_ASN1_CONSTRUCTED
);
196 case ASN1_ITYPE_NDEF_SEQUENCE
:
197 /* Use indefinite length constructed if requested */
198 if (aclass
& ASN1_TFLG_NDEF
) ndef
= 2;
201 case ASN1_ITYPE_SEQUENCE
:
202 i
= asn1_enc_restore(&seqcontlen
, out
, pval
, it
);
203 /* An error occurred */
206 /* We have a valid cached encoding... */
209 /* Otherwise carry on */
211 /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
214 tag
= V_ASN1_SEQUENCE
;
215 /* Retain any other flags in aclass */
216 aclass
= (aclass
& ~ASN1_TFLG_TAG_CLASS
)
219 if (asn1_cb
&& !asn1_cb(ASN1_OP_I2D_PRE
, pval
, it
, NULL
))
221 /* First work out sequence content length */
222 for (i
= 0, tt
= it
->templates
; i
< it
->tcount
; tt
++, i
++)
224 const ASN1_TEMPLATE
*seqtt
;
225 ASN1_VALUE
**pseqval
;
226 seqtt
= asn1_do_adb(pval
, tt
, 1);
229 pseqval
= asn1_get_field_ptr(pval
, seqtt
);
230 /* FIXME: check for errors in enhanced version */
231 seqcontlen
+= asn1_template_ex_i2d(pseqval
, NULL
, seqtt
,
235 seqlen
= ASN1_object_size(ndef
, seqcontlen
, tag
);
238 /* Output SEQUENCE header */
239 ASN1_put_object(out
, ndef
, seqcontlen
, tag
, aclass
);
240 for (i
= 0, tt
= it
->templates
; i
< it
->tcount
; tt
++, i
++)
242 const ASN1_TEMPLATE
*seqtt
;
243 ASN1_VALUE
**pseqval
;
244 seqtt
= asn1_do_adb(pval
, tt
, 1);
247 pseqval
= asn1_get_field_ptr(pval
, seqtt
);
248 /* FIXME: check for errors in enhanced version */
249 asn1_template_ex_i2d(pseqval
, out
, seqtt
, -1, aclass
);
253 if (asn1_cb
&& !asn1_cb(ASN1_OP_I2D_POST
, pval
, it
, NULL
))
264 int ASN1_template_i2d(ASN1_VALUE
**pval
, unsigned char **out
,
265 const ASN1_TEMPLATE
*tt
)
267 return asn1_template_ex_i2d(pval
, out
, tt
, -1, 0);
270 static int asn1_template_ex_i2d(ASN1_VALUE
**pval
, unsigned char **out
,
271 const ASN1_TEMPLATE
*tt
, int tag
, int iclass
)
273 int i
, ret
, flags
, ttag
, tclass
, ndef
;
275 /* Work out tag and class to use: tagging may come
276 * either from the template or the arguments, not both
277 * because this would create ambiguity. Additionally
278 * the iclass argument may contain some additional flags
279 * which should be noted and passed down to other levels.
281 if (flags
& ASN1_TFLG_TAG_MASK
)
283 /* Error if argument and template tagging */
285 /* FIXME: error code here */
287 /* Get tagging from template */
289 tclass
= flags
& ASN1_TFLG_TAG_CLASS
;
293 /* No template tagging, get from arguments */
295 tclass
= iclass
& ASN1_TFLG_TAG_CLASS
;
303 * Remove any class mask from iflag.
305 iclass
&= ~ASN1_TFLG_TAG_CLASS
;
307 /* At this point 'ttag' contains the outer tag to use,
308 * 'tclass' is the class and iclass is any flags passed
312 /* if template and arguments require ndef, use it */
313 if ((flags
& ASN1_TFLG_NDEF
) && (iclass
& ASN1_TFLG_NDEF
))
317 if (flags
& ASN1_TFLG_SK_MASK
)
319 /* SET OF, SEQUENCE OF */
320 STACK_OF(ASN1_VALUE
) *sk
= (STACK_OF(ASN1_VALUE
) *)*pval
;
321 int isset
, sktag
, skaclass
;
322 int skcontlen
, sklen
;
328 if (flags
& ASN1_TFLG_SET_OF
)
331 /* 2 means we reorder */
332 if (flags
& ASN1_TFLG_SEQUENCE_OF
)
337 /* Work out inner tag value: if EXPLICIT
338 * or no tagging use underlying type.
340 if ((ttag
!= -1) && !(flags
& ASN1_TFLG_EXPTAG
))
347 skaclass
= V_ASN1_UNIVERSAL
;
350 else sktag
= V_ASN1_SEQUENCE
;
353 /* Determine total length of items */
355 for (i
= 0; i
< sk_ASN1_VALUE_num(sk
); i
++)
357 skitem
= sk_ASN1_VALUE_value(sk
, i
);
358 skcontlen
+= ASN1_item_ex_i2d(&skitem
, NULL
,
359 ASN1_ITEM_ptr(tt
->item
),
362 sklen
= ASN1_object_size(ndef
, skcontlen
, sktag
);
363 /* If EXPLICIT need length of surrounding tag */
364 if (flags
& ASN1_TFLG_EXPTAG
)
365 ret
= ASN1_object_size(ndef
, sklen
, ttag
);
371 /* Now encode this lot... */
373 if (flags
& ASN1_TFLG_EXPTAG
)
374 ASN1_put_object(out
, ndef
, sklen
, ttag
, tclass
);
375 /* SET or SEQUENCE and IMPLICIT tag */
376 ASN1_put_object(out
, ndef
, skcontlen
, sktag
, skaclass
);
377 /* And the stuff itself */
378 asn1_set_seq_out(sk
, out
, skcontlen
, ASN1_ITEM_ptr(tt
->item
),
383 if (flags
& ASN1_TFLG_EXPTAG
)
390 if (flags
& ASN1_TFLG_EXPTAG
)
392 /* EXPLICIT tagging */
393 /* Find length of tagged item */
394 i
= ASN1_item_ex_i2d(pval
, NULL
, ASN1_ITEM_ptr(tt
->item
),
398 /* Find length of EXPLICIT tag */
399 ret
= ASN1_object_size(ndef
, i
, ttag
);
402 /* Output tag and item */
403 ASN1_put_object(out
, ndef
, i
, ttag
, tclass
);
404 ASN1_item_ex_i2d(pval
, out
, ASN1_ITEM_ptr(tt
->item
),
412 /* Either normal or IMPLICIT tagging: combine class and flags */
413 return ASN1_item_ex_i2d(pval
, out
, ASN1_ITEM_ptr(tt
->item
),
414 ttag
, tclass
| iclass
);
418 /* Temporary structure used to hold DER encoding of items for SET OF */
426 static int der_cmp(const void *a
, const void *b
)
428 const DER_ENC
*d1
= a
, *d2
= b
;
430 cmplen
= (d1
->length
< d2
->length
) ? d1
->length
: d2
->length
;
431 i
= memcmp(d1
->data
, d2
->data
, cmplen
);
434 return d1
->length
- d2
->length
;
437 /* Output the content octets of SET OF or SEQUENCE OF */
439 static int asn1_set_seq_out(STACK_OF(ASN1_VALUE
) *sk
, unsigned char **out
,
440 int skcontlen
, const ASN1_ITEM
*item
,
441 int do_sort
, int iclass
)
445 unsigned char *tmpdat
= NULL
, *p
= NULL
;
446 DER_ENC
*derlst
= NULL
, *tder
;
449 /* Don't need to sort less than 2 items */
450 if (sk_ASN1_VALUE_num(sk
) < 2)
454 derlst
= OPENSSL_malloc(sk_ASN1_VALUE_num(sk
)
456 tmpdat
= OPENSSL_malloc(skcontlen
);
457 if (!derlst
|| !tmpdat
)
461 /* If not sorting just output each item */
464 for (i
= 0; i
< sk_ASN1_VALUE_num(sk
); i
++)
466 skitem
= sk_ASN1_VALUE_value(sk
, i
);
467 ASN1_item_ex_i2d(&skitem
, out
, item
, -1, iclass
);
473 /* Doing sort: build up a list of each member's DER encoding */
474 for (i
= 0, tder
= derlst
; i
< sk_ASN1_VALUE_num(sk
); i
++, tder
++)
476 skitem
= sk_ASN1_VALUE_value(sk
, i
);
478 tder
->length
= ASN1_item_ex_i2d(&skitem
, &p
, item
, -1, iclass
);
479 tder
->field
= skitem
;
483 qsort(derlst
, sk_ASN1_VALUE_num(sk
), sizeof(*derlst
), der_cmp
);
484 /* Output sorted DER encoding */
486 for (i
= 0, tder
= derlst
; i
< sk_ASN1_VALUE_num(sk
); i
++, tder
++)
488 memcpy(p
, tder
->data
, tder
->length
);
492 /* If do_sort is 2 then reorder the STACK */
495 for (i
= 0, tder
= derlst
; i
< sk_ASN1_VALUE_num(sk
);
497 (void)sk_ASN1_VALUE_set(sk
, i
, tder
->field
);
499 OPENSSL_free(derlst
);
500 OPENSSL_free(tmpdat
);
504 static int asn1_i2d_ex_primitive(ASN1_VALUE
**pval
, unsigned char **out
,
505 const ASN1_ITEM
*it
, int tag
, int aclass
)
514 /* Get length of content octets and maybe find
515 * out the underlying type.
518 len
= asn1_ex_i2c(pval
, NULL
, &utype
, it
);
520 /* If SEQUENCE, SET or OTHER then header is
521 * included in pseudo content octets so don't
522 * include tag+length. We need to check here
523 * because the call to asn1_ex_i2c() could change
526 if ((utype
== V_ASN1_SEQUENCE
) || (utype
== V_ASN1_SET
) ||
527 (utype
== V_ASN1_OTHER
))
531 /* -1 means omit type */
536 /* -2 return is special meaning use ndef */
543 /* If not implicitly tagged get tag from underlying type */
544 if (tag
== -1) tag
= utype
;
546 /* Output tag+length followed by content octets */
550 ASN1_put_object(out
, ndef
, len
, tag
, aclass
);
551 asn1_ex_i2c(pval
, *out
, &utype
, it
);
559 return ASN1_object_size(ndef
, len
, tag
);
563 /* Produce content octets from a structure */
565 int asn1_ex_i2c(ASN1_VALUE
**pval
, unsigned char *cout
, int *putype
,
568 ASN1_BOOLEAN
*tbool
= NULL
;
572 const unsigned char *cont
;
575 const ASN1_PRIMITIVE_FUNCS
*pf
;
577 if (pf
&& pf
->prim_i2c
)
578 return pf
->prim_i2c(pval
, cout
, putype
, it
);
580 /* Should type be omitted? */
581 if ((it
->itype
!= ASN1_ITYPE_PRIMITIVE
)
582 || (it
->utype
!= V_ASN1_BOOLEAN
))
584 if (!*pval
) return -1;
587 if (it
->itype
== ASN1_ITYPE_MSTRING
)
589 /* If MSTRING type set the underlying type */
590 strtmp
= (ASN1_STRING
*)*pval
;
591 utype
= strtmp
->type
;
594 else if (it
->utype
== V_ASN1_ANY
)
596 /* If ANY set type and pointer to value */
598 typ
= (ASN1_TYPE
*)*pval
;
601 pval
= &typ
->value
.asn1_value
;
603 else utype
= *putype
;
608 otmp
= (ASN1_OBJECT
*)*pval
;
619 tbool
= (ASN1_BOOLEAN
*)pval
;
622 if (it
->utype
!= V_ASN1_ANY
)
624 /* Default handling if value == size field then omit */
625 if (*tbool
&& (it
->size
> 0))
627 if (!*tbool
&& !it
->size
)
630 c
= (unsigned char)*tbool
;
635 case V_ASN1_BIT_STRING
:
636 return i2c_ASN1_BIT_STRING((ASN1_BIT_STRING
*)*pval
,
637 cout
? &cout
: NULL
);
641 case V_ASN1_NEG_INTEGER
:
642 case V_ASN1_ENUMERATED
:
643 case V_ASN1_NEG_ENUMERATED
:
644 /* These are all have the same content format
647 return i2c_ASN1_INTEGER((ASN1_INTEGER
*)*pval
,
648 cout
? &cout
: NULL
);
651 case V_ASN1_OCTET_STRING
:
652 case V_ASN1_NUMERICSTRING
:
653 case V_ASN1_PRINTABLESTRING
:
654 case V_ASN1_T61STRING
:
655 case V_ASN1_VIDEOTEXSTRING
:
656 case V_ASN1_IA5STRING
:
658 case V_ASN1_GENERALIZEDTIME
:
659 case V_ASN1_GRAPHICSTRING
:
660 case V_ASN1_VISIBLESTRING
:
661 case V_ASN1_GENERALSTRING
:
662 case V_ASN1_UNIVERSALSTRING
:
663 case V_ASN1_BMPSTRING
:
664 case V_ASN1_UTF8STRING
:
665 case V_ASN1_SEQUENCE
:
668 /* All based on ASN1_STRING and handled the same */
669 strtmp
= (ASN1_STRING
*)*pval
;
670 /* Special handling for NDEF */
671 if ((it
->size
== ASN1_TFLG_NDEF
)
672 && (strtmp
->flags
& ASN1_STRING_FLAG_NDEF
))
679 /* Special return code */
683 len
= strtmp
->length
;
689 memcpy(cout
, cont
, len
);