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).
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,
68 const ASN1_ITEM *it, int tag, int aclass);
69 static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *sk, unsigned char **out,
70 int skcontlen, const ASN1_ITEM *item,
71 int do_sort, int iclass);
72 static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
73 const ASN1_TEMPLATE *tt, int tag, int aclass);
74 static int asn1_item_flags_i2d(ASN1_VALUE *val, unsigned char **out,
75 const ASN1_ITEM *it, int flags);
78 * Top level i2d equivalents: the 'ndef' variant instructs the encoder to use
79 * indefinite length constructed encoding, where appropriate
82 int ASN1_item_ndef_i2d(ASN1_VALUE *val, unsigned char **out,
85 return asn1_item_flags_i2d(val, out, it, ASN1_TFLG_NDEF);
88 int ASN1_item_i2d(ASN1_VALUE *val, unsigned char **out, const ASN1_ITEM *it)
90 return asn1_item_flags_i2d(val, out, it, 0);
94 * Encode an ASN1 item, this is use by the standard 'i2d' function. 'out'
95 * points to a buffer to output the data to. The new i2d has one additional
96 * feature. If the output buffer is NULL (i.e. *out == NULL) then a buffer is
97 * allocated and populated with the encoding.
100 static int asn1_item_flags_i2d(ASN1_VALUE *val, unsigned char **out,
101 const ASN1_ITEM *it, int flags)
104 unsigned char *p, *buf;
106 len = ASN1_item_ex_i2d(&val, NULL, it, -1, flags);
109 buf = OPENSSL_malloc(len);
113 ASN1_item_ex_i2d(&val, &p, it, -1, flags);
118 return ASN1_item_ex_i2d(&val, out, it, -1, flags);
122 * Encode an item, taking care of IMPLICIT tagging (if any). This function
123 * performs the normal item handling: it can be used in external types.
126 int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
127 const ASN1_ITEM *it, int tag, int aclass)
129 const ASN1_TEMPLATE *tt = NULL;
130 unsigned char *p = NULL;
131 int i, seqcontlen, seqlen, ndef = 1;
132 const ASN1_COMPAT_FUNCS *cf;
133 const ASN1_EXTERN_FUNCS *ef;
134 const ASN1_AUX *aux = it->funcs;
135 ASN1_aux_cb *asn1_cb = 0;
137 if ((it->itype != ASN1_ITYPE_PRIMITIVE) && !*pval)
140 if (aux && aux->asn1_cb)
141 asn1_cb = aux->asn1_cb;
145 case ASN1_ITYPE_PRIMITIVE:
147 return asn1_template_ex_i2d(pval, out, it->templates,
149 return asn1_i2d_ex_primitive(pval, out, it, tag, aclass);
152 case ASN1_ITYPE_MSTRING:
153 return asn1_i2d_ex_primitive(pval, out, it, -1, aclass);
155 case ASN1_ITYPE_CHOICE:
156 if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it))
158 i = asn1_get_choice_selector(pval, it);
159 if ((i >= 0) && (i < it->tcount)) {
161 const ASN1_TEMPLATE *chtt;
162 chtt = it->templates + i;
163 pchval = asn1_get_field_ptr(pval, chtt);
164 return asn1_template_ex_i2d(pchval, out, chtt, -1, aclass);
166 /* Fixme: error condition if selector out of range */
167 if (asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it))
171 case ASN1_ITYPE_EXTERN:
172 /* If new style i2d it does all the work */
174 return ef->asn1_ex_i2d(pval, out, it, tag, aclass);
176 case ASN1_ITYPE_COMPAT:
177 /* old style hackery... */
181 i = cf->asn1_i2d(*pval, out);
183 * Fixup for IMPLICIT tag: note this messes up for tags > 30, but so
184 * did the old code. Tags > 30 are very rare anyway.
186 if (out && (tag != -1))
187 *p = aclass | tag | (*p & V_ASN1_CONSTRUCTED);
190 case ASN1_ITYPE_NDEF_SEQUENCE:
191 /* Use indefinite length constructed if requested */
192 if (aclass & ASN1_TFLG_NDEF)
196 case ASN1_ITYPE_SEQUENCE:
197 i = asn1_enc_restore(&seqcontlen, out, pval, it);
198 /* An error occurred */
201 /* We have a valid cached encoding... */
204 /* Otherwise carry on */
206 /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
208 tag = V_ASN1_SEQUENCE;
209 /* Retain any other flags in aclass */
210 aclass = (aclass & ~ASN1_TFLG_TAG_CLASS)
213 if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it))
215 /* First work out sequence content length */
216 for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
217 const ASN1_TEMPLATE *seqtt;
218 ASN1_VALUE **pseqval;
219 seqtt = asn1_do_adb(pval, tt, 1);
222 pseqval = asn1_get_field_ptr(pval, seqtt);
223 /* FIXME: check for errors in enhanced version */
224 seqcontlen += asn1_template_ex_i2d(pseqval, NULL, seqtt,
228 seqlen = ASN1_object_size(ndef, seqcontlen, tag);
231 /* Output SEQUENCE header */
232 ASN1_put_object(out, ndef, seqcontlen, tag, aclass);
233 for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
234 const ASN1_TEMPLATE *seqtt;
235 ASN1_VALUE **pseqval;
236 seqtt = asn1_do_adb(pval, tt, 1);
239 pseqval = asn1_get_field_ptr(pval, seqtt);
240 /* FIXME: check for errors in enhanced version */
241 asn1_template_ex_i2d(pseqval, out, seqtt, -1, aclass);
245 if (asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it))
256 int ASN1_template_i2d(ASN1_VALUE **pval, unsigned char **out,
257 const ASN1_TEMPLATE *tt)
259 return asn1_template_ex_i2d(pval, out, tt, -1, 0);
262 static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
263 const ASN1_TEMPLATE *tt, int tag, int iclass)
265 int i, ret, flags, ttag, tclass, ndef;
268 * Work out tag and class to use: tagging may come either from the
269 * template or the arguments, not both because this would create
270 * ambiguity. Additionally the iclass argument may contain some
271 * additional flags which should be noted and passed down to other
274 if (flags & ASN1_TFLG_TAG_MASK) {
275 /* Error if argument and template tagging */
277 /* FIXME: error code here */
279 /* Get tagging from template */
281 tclass = flags & ASN1_TFLG_TAG_CLASS;
282 } else if (tag != -1) {
283 /* No template tagging, get from arguments */
285 tclass = iclass & ASN1_TFLG_TAG_CLASS;
291 * Remove any class mask from iflag.
293 iclass &= ~ASN1_TFLG_TAG_CLASS;
296 * At this point 'ttag' contains the outer tag to use, 'tclass' is the
297 * class and iclass is any flags passed to this function.
300 /* if template and arguments require ndef, use it */
301 if ((flags & ASN1_TFLG_NDEF) && (iclass & ASN1_TFLG_NDEF))
306 if (flags & ASN1_TFLG_SK_MASK) {
307 /* SET OF, SEQUENCE OF */
308 STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval;
309 int isset, sktag, skaclass;
310 int skcontlen, sklen;
316 if (flags & ASN1_TFLG_SET_OF) {
318 /* 2 means we reorder */
319 if (flags & ASN1_TFLG_SEQUENCE_OF)
325 * Work out inner tag value: if EXPLICIT or no tagging use underlying
328 if ((ttag != -1) && !(flags & ASN1_TFLG_EXPTAG)) {
332 skaclass = V_ASN1_UNIVERSAL;
336 sktag = V_ASN1_SEQUENCE;
339 /* Determine total length of items */
341 for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
342 skitem = sk_ASN1_VALUE_value(sk, i);
343 skcontlen += ASN1_item_ex_i2d(&skitem, NULL,
344 ASN1_ITEM_ptr(tt->item),
347 sklen = ASN1_object_size(ndef, skcontlen, sktag);
348 /* If EXPLICIT need length of surrounding tag */
349 if (flags & ASN1_TFLG_EXPTAG)
350 ret = ASN1_object_size(ndef, sklen, ttag);
357 /* Now encode this lot... */
359 if (flags & ASN1_TFLG_EXPTAG)
360 ASN1_put_object(out, ndef, sklen, ttag, tclass);
361 /* SET or SEQUENCE and IMPLICIT tag */
362 ASN1_put_object(out, ndef, skcontlen, sktag, skaclass);
363 /* And the stuff itself */
364 asn1_set_seq_out(sk, out, skcontlen, ASN1_ITEM_ptr(tt->item),
368 if (flags & ASN1_TFLG_EXPTAG)
375 if (flags & ASN1_TFLG_EXPTAG) {
376 /* EXPLICIT tagging */
377 /* Find length of tagged item */
378 i = ASN1_item_ex_i2d(pval, NULL, ASN1_ITEM_ptr(tt->item), -1, iclass);
381 /* Find length of EXPLICIT tag */
382 ret = ASN1_object_size(ndef, i, ttag);
384 /* Output tag and item */
385 ASN1_put_object(out, ndef, i, ttag, tclass);
386 ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item), -1, iclass);
393 /* Either normal or IMPLICIT tagging: combine class and flags */
394 return ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item),
395 ttag, tclass | iclass);
399 /* Temporary structure used to hold DER encoding of items for SET OF */
407 static int der_cmp(const void *a, const void *b)
409 const DER_ENC *d1 = a, *d2 = b;
411 cmplen = (d1->length < d2->length) ? d1->length : d2->length;
412 i = memcmp(d1->data, d2->data, cmplen);
415 return d1->length - d2->length;
418 /* Output the content octets of SET OF or SEQUENCE OF */
420 static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *sk, unsigned char **out,
421 int skcontlen, const ASN1_ITEM *item,
422 int do_sort, int iclass)
426 unsigned char *tmpdat = NULL, *p = NULL;
427 DER_ENC *derlst = NULL, *tder;
429 /* Don't need to sort less than 2 items */
430 if (sk_ASN1_VALUE_num(sk) < 2)
433 derlst = OPENSSL_malloc(sk_ASN1_VALUE_num(sk)
437 tmpdat = OPENSSL_malloc(skcontlen);
439 OPENSSL_free(derlst);
444 /* If not sorting just output each item */
446 for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
447 skitem = sk_ASN1_VALUE_value(sk, i);
448 ASN1_item_ex_i2d(&skitem, out, item, -1, iclass);
454 /* Doing sort: build up a list of each member's DER encoding */
455 for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++) {
456 skitem = sk_ASN1_VALUE_value(sk, i);
458 tder->length = ASN1_item_ex_i2d(&skitem, &p, item, -1, iclass);
459 tder->field = skitem;
463 qsort(derlst, sk_ASN1_VALUE_num(sk), sizeof(*derlst), der_cmp);
464 /* Output sorted DER encoding */
466 for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++) {
467 memcpy(p, tder->data, tder->length);
471 /* If do_sort is 2 then reorder the STACK */
473 for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++)
474 (void)sk_ASN1_VALUE_set(sk, i, tder->field);
476 OPENSSL_free(derlst);
477 OPENSSL_free(tmpdat);
481 static int asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out,
482 const ASN1_ITEM *it, int tag, int aclass)
492 * Get length of content octets and maybe find out the underlying type.
495 len = asn1_ex_i2c(pval, NULL, &utype, it);
498 * If SEQUENCE, SET or OTHER then header is included in pseudo content
499 * octets so don't include tag+length. We need to check here because the
500 * call to asn1_ex_i2c() could change utype.
502 if ((utype == V_ASN1_SEQUENCE) || (utype == V_ASN1_SET) ||
503 (utype == V_ASN1_OTHER))
508 /* -1 means omit type */
513 /* -2 return is special meaning use ndef */
519 /* If not implicitly tagged get tag from underlying type */
523 /* Output tag+length followed by content octets */
526 ASN1_put_object(out, ndef, len, tag, aclass);
527 asn1_ex_i2c(pval, *out, &utype, it);
535 return ASN1_object_size(ndef, len, tag);
539 /* Produce content octets from a structure */
541 int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype,
544 ASN1_BOOLEAN *tbool = NULL;
548 unsigned char *cont, c;
550 const ASN1_PRIMITIVE_FUNCS *pf;
552 if (pf && pf->prim_i2c)
553 return pf->prim_i2c(pval, cout, putype, it);
555 /* Should type be omitted? */
556 if ((it->itype != ASN1_ITYPE_PRIMITIVE)
557 || (it->utype != V_ASN1_BOOLEAN)) {
562 if (it->itype == ASN1_ITYPE_MSTRING) {
563 /* If MSTRING type set the underlying type */
564 strtmp = (ASN1_STRING *)*pval;
565 utype = strtmp->type;
567 } else if (it->utype == V_ASN1_ANY) {
568 /* If ANY set type and pointer to value */
570 typ = (ASN1_TYPE *)*pval;
573 pval = &typ->value.asn1_value;
579 otmp = (ASN1_OBJECT *)*pval;
590 tbool = (ASN1_BOOLEAN *)pval;
593 if (it->utype != V_ASN1_ANY) {
595 * Default handling if value == size field then omit
597 if (*tbool && (it->size > 0))
599 if (!*tbool && !it->size)
602 c = (unsigned char)*tbool;
607 case V_ASN1_BIT_STRING:
608 return i2c_ASN1_BIT_STRING((ASN1_BIT_STRING *)*pval,
609 cout ? &cout : NULL);
613 case V_ASN1_NEG_INTEGER:
614 case V_ASN1_ENUMERATED:
615 case V_ASN1_NEG_ENUMERATED:
617 * These are all have the same content format as ASN1_INTEGER
619 return i2c_ASN1_INTEGER((ASN1_INTEGER *)*pval, cout ? &cout : NULL);
622 case V_ASN1_OCTET_STRING:
623 case V_ASN1_NUMERICSTRING:
624 case V_ASN1_PRINTABLESTRING:
625 case V_ASN1_T61STRING:
626 case V_ASN1_VIDEOTEXSTRING:
627 case V_ASN1_IA5STRING:
629 case V_ASN1_GENERALIZEDTIME:
630 case V_ASN1_GRAPHICSTRING:
631 case V_ASN1_VISIBLESTRING:
632 case V_ASN1_GENERALSTRING:
633 case V_ASN1_UNIVERSALSTRING:
634 case V_ASN1_BMPSTRING:
635 case V_ASN1_UTF8STRING:
636 case V_ASN1_SEQUENCE:
639 /* All based on ASN1_STRING and handled the same */
640 strtmp = (ASN1_STRING *)*pval;
641 /* Special handling for NDEF */
642 if ((it->size == ASN1_TFLG_NDEF)
643 && (strtmp->flags & ASN1_STRING_FLAG_NDEF)) {
648 /* Special return code */
652 len = strtmp->length;
658 memcpy(cout, cont, len);