3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
6 /* ====================================================================
7 * Copyright (c) 2000-2004 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
64 #include <openssl/asn1.h>
65 #include <openssl/asn1t.h>
66 #include <openssl/objects.h>
68 static int asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out,
69 const ASN1_ITEM *it, int tag, int aclass);
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, int tag, int aclass);
75 static int asn1_item_flags_i2d(ASN1_VALUE *val, unsigned char **out,
76 const ASN1_ITEM *it, int flags);
79 * Top level i2d equivalents: the 'ndef' variant instructs the encoder to use
80 * 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);
95 * Encode an ASN1 item, this is use by the standard 'i2d' function. 'out'
96 * points to a buffer to output the data to. The new i2d has one additional
97 * feature. If the output buffer is NULL (i.e. *out == NULL) then a buffer is
98 * allocated and populated with the encoding.
101 static int asn1_item_flags_i2d(ASN1_VALUE *val, unsigned char **out,
102 const ASN1_ITEM *it, int flags)
105 unsigned char *p, *buf;
107 len = ASN1_item_ex_i2d(&val, NULL, it, -1, flags);
110 buf = OPENSSL_malloc(len);
114 ASN1_item_ex_i2d(&val, &p, it, -1, flags);
119 return ASN1_item_ex_i2d(&val, out, it, -1, flags);
123 * Encode an item, taking care of IMPLICIT tagging (if any). This function
124 * performs the normal item handling: it can be used in external types.
127 int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
128 const ASN1_ITEM *it, int tag, int aclass)
130 const ASN1_TEMPLATE *tt = NULL;
131 unsigned char *p = NULL;
132 int i, seqcontlen, seqlen, ndef = 1;
133 const ASN1_COMPAT_FUNCS *cf;
134 const ASN1_EXTERN_FUNCS *ef;
135 const ASN1_AUX *aux = it->funcs;
136 ASN1_aux_cb *asn1_cb = 0;
138 if ((it->itype != ASN1_ITYPE_PRIMITIVE) && !*pval)
141 if (aux && aux->asn1_cb)
142 asn1_cb = aux->asn1_cb;
146 case ASN1_ITYPE_PRIMITIVE:
148 return asn1_template_ex_i2d(pval, out, it->templates,
150 return asn1_i2d_ex_primitive(pval, out, it, tag, aclass);
153 case ASN1_ITYPE_MSTRING:
154 return asn1_i2d_ex_primitive(pval, out, it, -1, aclass);
156 case ASN1_ITYPE_CHOICE:
157 if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it, NULL))
159 i = asn1_get_choice_selector(pval, it);
160 if ((i >= 0) && (i < it->tcount)) {
162 const ASN1_TEMPLATE *chtt;
163 chtt = it->templates + i;
164 pchval = asn1_get_field_ptr(pval, chtt);
165 return asn1_template_ex_i2d(pchval, out, chtt, -1, aclass);
167 /* Fixme: error condition if selector out of range */
168 if (asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it, NULL))
172 case ASN1_ITYPE_EXTERN:
173 /* If new style i2d it does all the work */
175 return ef->asn1_ex_i2d(pval, out, it, tag, aclass);
177 case ASN1_ITYPE_COMPAT:
178 /* old style hackery... */
182 i = cf->asn1_i2d(*pval, out);
184 * Fixup for IMPLICIT tag: note this messes up for tags > 30, but so
185 * did the old code. Tags > 30 are very rare anyway.
187 if (out && (tag != -1))
188 *p = aclass | tag | (*p & V_ASN1_CONSTRUCTED);
191 case ASN1_ITYPE_NDEF_SEQUENCE:
192 /* Use indefinite length constructed if requested */
193 if (aclass & ASN1_TFLG_NDEF)
197 case ASN1_ITYPE_SEQUENCE:
198 i = asn1_enc_restore(&seqcontlen, out, pval, it);
199 /* An error occurred */
202 /* We have a valid cached encoding... */
205 /* Otherwise carry on */
207 /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
209 tag = V_ASN1_SEQUENCE;
210 /* Retain any other flags in aclass */
211 aclass = (aclass & ~ASN1_TFLG_TAG_CLASS)
214 if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it, NULL))
216 /* First work out sequence content length */
217 for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
218 const ASN1_TEMPLATE *seqtt;
219 ASN1_VALUE **pseqval;
221 seqtt = asn1_do_adb(pval, tt, 1);
224 pseqval = asn1_get_field_ptr(pval, seqtt);
225 tmplen = asn1_template_ex_i2d(pseqval, NULL, seqtt, -1, aclass);
226 if (tmplen == -1 || (tmplen > INT_MAX - seqcontlen))
228 seqcontlen += tmplen;
231 seqlen = ASN1_object_size(ndef, seqcontlen, tag);
232 if (!out || seqlen == -1)
234 /* Output SEQUENCE header */
235 ASN1_put_object(out, ndef, seqcontlen, tag, aclass);
236 for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
237 const ASN1_TEMPLATE *seqtt;
238 ASN1_VALUE **pseqval;
239 seqtt = asn1_do_adb(pval, tt, 1);
242 pseqval = asn1_get_field_ptr(pval, seqtt);
243 /* FIXME: check for errors in enhanced version */
244 asn1_template_ex_i2d(pseqval, out, seqtt, -1, aclass);
248 if (asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it, NULL))
259 int ASN1_template_i2d(ASN1_VALUE **pval, unsigned char **out,
260 const ASN1_TEMPLATE *tt)
262 return asn1_template_ex_i2d(pval, out, tt, -1, 0);
265 static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
266 const ASN1_TEMPLATE *tt, int tag, int iclass)
268 int i, ret, flags, ttag, tclass, ndef;
271 * Work out tag and class to use: tagging may come either from the
272 * template or the arguments, not both because this would create
273 * ambiguity. Additionally the iclass argument may contain some
274 * additional flags which should be noted and passed down to other
277 if (flags & ASN1_TFLG_TAG_MASK) {
278 /* Error if argument and template tagging */
280 /* FIXME: error code here */
282 /* Get tagging from template */
284 tclass = flags & ASN1_TFLG_TAG_CLASS;
285 } else if (tag != -1) {
286 /* No template tagging, get from arguments */
288 tclass = iclass & ASN1_TFLG_TAG_CLASS;
294 * Remove any class mask from iflag.
296 iclass &= ~ASN1_TFLG_TAG_CLASS;
299 * At this point 'ttag' contains the outer tag to use, 'tclass' is the
300 * class and iclass is any flags passed to this function.
303 /* if template and arguments require ndef, use it */
304 if ((flags & ASN1_TFLG_NDEF) && (iclass & ASN1_TFLG_NDEF))
309 if (flags & ASN1_TFLG_SK_MASK) {
310 /* SET OF, SEQUENCE OF */
311 STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval;
312 int isset, sktag, skaclass;
313 int skcontlen, sklen;
319 if (flags & ASN1_TFLG_SET_OF) {
321 /* 2 means we reorder */
322 if (flags & ASN1_TFLG_SEQUENCE_OF)
328 * Work out inner tag value: if EXPLICIT or no tagging use underlying
331 if ((ttag != -1) && !(flags & ASN1_TFLG_EXPTAG)) {
335 skaclass = V_ASN1_UNIVERSAL;
339 sktag = V_ASN1_SEQUENCE;
342 /* Determine total length of items */
344 for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
346 skitem = sk_ASN1_VALUE_value(sk, i);
347 tmplen = ASN1_item_ex_i2d(&skitem, NULL, ASN1_ITEM_ptr(tt->item),
349 if (tmplen == -1 || (skcontlen > INT_MAX - tmplen))
353 sklen = ASN1_object_size(ndef, skcontlen, sktag);
356 /* If EXPLICIT need length of surrounding tag */
357 if (flags & ASN1_TFLG_EXPTAG)
358 ret = ASN1_object_size(ndef, sklen, ttag);
362 if (!out || ret == -1)
365 /* Now encode this lot... */
367 if (flags & ASN1_TFLG_EXPTAG)
368 ASN1_put_object(out, ndef, sklen, ttag, tclass);
369 /* SET or SEQUENCE and IMPLICIT tag */
370 ASN1_put_object(out, ndef, skcontlen, sktag, skaclass);
371 /* And the stuff itself */
372 asn1_set_seq_out(sk, out, skcontlen, ASN1_ITEM_ptr(tt->item),
376 if (flags & ASN1_TFLG_EXPTAG)
383 if (flags & ASN1_TFLG_EXPTAG) {
384 /* EXPLICIT tagging */
385 /* Find length of tagged item */
386 i = ASN1_item_ex_i2d(pval, NULL, ASN1_ITEM_ptr(tt->item), -1, iclass);
389 /* Find length of EXPLICIT tag */
390 ret = ASN1_object_size(ndef, i, ttag);
391 if (out && ret != -1) {
392 /* Output tag and item */
393 ASN1_put_object(out, ndef, i, ttag, tclass);
394 ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item), -1, iclass);
401 /* Either normal or IMPLICIT tagging: combine class and flags */
402 return ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item),
403 ttag, tclass | iclass);
407 /* Temporary structure used to hold DER encoding of items for SET OF */
415 static int der_cmp(const void *a, const void *b)
417 const DER_ENC *d1 = a, *d2 = b;
419 cmplen = (d1->length < d2->length) ? d1->length : d2->length;
420 i = memcmp(d1->data, d2->data, cmplen);
423 return d1->length - d2->length;
426 /* Output the content octets of SET OF or SEQUENCE OF */
428 static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *sk, unsigned char **out,
429 int skcontlen, const ASN1_ITEM *item,
430 int do_sort, int iclass)
434 unsigned char *tmpdat = NULL, *p = NULL;
435 DER_ENC *derlst = NULL, *tder;
437 /* Don't need to sort less than 2 items */
438 if (sk_ASN1_VALUE_num(sk) < 2)
441 derlst = OPENSSL_malloc(sk_ASN1_VALUE_num(sk)
445 tmpdat = OPENSSL_malloc(skcontlen);
447 OPENSSL_free(derlst);
452 /* If not sorting just output each item */
454 for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
455 skitem = sk_ASN1_VALUE_value(sk, i);
456 ASN1_item_ex_i2d(&skitem, out, item, -1, iclass);
462 /* Doing sort: build up a list of each member's DER encoding */
463 for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++) {
464 skitem = sk_ASN1_VALUE_value(sk, i);
466 tder->length = ASN1_item_ex_i2d(&skitem, &p, item, -1, iclass);
467 tder->field = skitem;
471 qsort(derlst, sk_ASN1_VALUE_num(sk), sizeof(*derlst), der_cmp);
472 /* Output sorted DER encoding */
474 for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++) {
475 memcpy(p, tder->data, tder->length);
479 /* If do_sort is 2 then reorder the STACK */
481 for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++)
482 (void)sk_ASN1_VALUE_set(sk, i, tder->field);
484 OPENSSL_free(derlst);
485 OPENSSL_free(tmpdat);
489 static int asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out,
490 const ASN1_ITEM *it, int tag, int aclass)
500 * Get length of content octets and maybe find out the underlying type.
503 len = asn1_ex_i2c(pval, NULL, &utype, it);
506 * If SEQUENCE, SET or OTHER then header is included in pseudo content
507 * octets so don't include tag+length. We need to check here because the
508 * call to asn1_ex_i2c() could change utype.
510 if ((utype == V_ASN1_SEQUENCE) || (utype == V_ASN1_SET) ||
511 (utype == V_ASN1_OTHER))
516 /* -1 means omit type */
521 /* -2 return is special meaning use ndef */
527 /* If not implicitly tagged get tag from underlying type */
531 /* Output tag+length followed by content octets */
534 ASN1_put_object(out, ndef, len, tag, aclass);
535 asn1_ex_i2c(pval, *out, &utype, it);
543 return ASN1_object_size(ndef, len, tag);
547 /* Produce content octets from a structure */
549 int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype,
552 ASN1_BOOLEAN *tbool = NULL;
556 const unsigned char *cont;
559 const ASN1_PRIMITIVE_FUNCS *pf;
561 if (pf && pf->prim_i2c)
562 return pf->prim_i2c(pval, cout, putype, it);
564 /* Should type be omitted? */
565 if ((it->itype != ASN1_ITYPE_PRIMITIVE)
566 || (it->utype != V_ASN1_BOOLEAN)) {
571 if (it->itype == ASN1_ITYPE_MSTRING) {
572 /* If MSTRING type set the underlying type */
573 strtmp = (ASN1_STRING *)*pval;
574 utype = strtmp->type;
576 } else if (it->utype == V_ASN1_ANY) {
577 /* If ANY set type and pointer to value */
579 typ = (ASN1_TYPE *)*pval;
582 pval = &typ->value.asn1_value;
588 otmp = (ASN1_OBJECT *)*pval;
599 tbool = (ASN1_BOOLEAN *)pval;
602 if (it->utype != V_ASN1_ANY) {
604 * Default handling if value == size field then omit
606 if (*tbool && (it->size > 0))
608 if (!*tbool && !it->size)
611 c = (unsigned char)*tbool;
616 case V_ASN1_BIT_STRING:
617 return i2c_ASN1_BIT_STRING((ASN1_BIT_STRING *)*pval,
618 cout ? &cout : NULL);
622 case V_ASN1_ENUMERATED:
624 * These are all have the same content format as ASN1_INTEGER
626 return i2c_ASN1_INTEGER((ASN1_INTEGER *)*pval, cout ? &cout : NULL);
629 case V_ASN1_OCTET_STRING:
630 case V_ASN1_NUMERICSTRING:
631 case V_ASN1_PRINTABLESTRING:
632 case V_ASN1_T61STRING:
633 case V_ASN1_VIDEOTEXSTRING:
634 case V_ASN1_IA5STRING:
636 case V_ASN1_GENERALIZEDTIME:
637 case V_ASN1_GRAPHICSTRING:
638 case V_ASN1_VISIBLESTRING:
639 case V_ASN1_GENERALSTRING:
640 case V_ASN1_UNIVERSALSTRING:
641 case V_ASN1_BMPSTRING:
642 case V_ASN1_UTF8STRING:
643 case V_ASN1_SEQUENCE:
646 /* All based on ASN1_STRING and handled the same */
647 strtmp = (ASN1_STRING *)*pval;
648 /* Special handling for NDEF */
649 if ((it->size == ASN1_TFLG_NDEF)
650 && (strtmp->flags & ASN1_STRING_FLAG_NDEF)) {
655 /* Special return code */
659 len = strtmp->length;
665 memcpy(cout, cont, len);