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>
66 #include "internal/asn1_int.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 int i, seqcontlen, seqlen, ndef = 1;
132 const ASN1_EXTERN_FUNCS *ef;
133 const ASN1_AUX *aux = it->funcs;
134 ASN1_aux_cb *asn1_cb = 0;
136 if ((it->itype != ASN1_ITYPE_PRIMITIVE) && !*pval)
139 if (aux && aux->asn1_cb)
140 asn1_cb = aux->asn1_cb;
144 case ASN1_ITYPE_PRIMITIVE:
146 return asn1_template_ex_i2d(pval, out, it->templates,
148 return asn1_i2d_ex_primitive(pval, out, it, tag, aclass);
150 case ASN1_ITYPE_MSTRING:
151 return asn1_i2d_ex_primitive(pval, out, it, -1, aclass);
153 case ASN1_ITYPE_CHOICE:
154 if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it, NULL))
156 i = asn1_get_choice_selector(pval, it);
157 if ((i >= 0) && (i < it->tcount)) {
159 const ASN1_TEMPLATE *chtt;
160 chtt = it->templates + i;
161 pchval = asn1_get_field_ptr(pval, chtt);
162 return asn1_template_ex_i2d(pchval, out, chtt, -1, aclass);
164 /* Fixme: error condition if selector out of range */
165 if (asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it, NULL))
169 case ASN1_ITYPE_EXTERN:
170 /* If new style i2d it does all the work */
172 return ef->asn1_ex_i2d(pval, out, it, tag, aclass);
174 case ASN1_ITYPE_NDEF_SEQUENCE:
175 /* Use indefinite length constructed if requested */
176 if (aclass & ASN1_TFLG_NDEF)
180 case ASN1_ITYPE_SEQUENCE:
181 i = asn1_enc_restore(&seqcontlen, out, pval, it);
182 /* An error occurred */
185 /* We have a valid cached encoding... */
188 /* Otherwise carry on */
190 /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
192 tag = V_ASN1_SEQUENCE;
193 /* Retain any other flags in aclass */
194 aclass = (aclass & ~ASN1_TFLG_TAG_CLASS)
197 if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it, NULL))
199 /* First work out sequence content length */
200 for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
201 const ASN1_TEMPLATE *seqtt;
202 ASN1_VALUE **pseqval;
203 seqtt = asn1_do_adb(pval, tt, 1);
206 pseqval = asn1_get_field_ptr(pval, seqtt);
207 /* FIXME: check for errors in enhanced version */
208 seqcontlen += asn1_template_ex_i2d(pseqval, NULL, seqtt,
212 seqlen = ASN1_object_size(ndef, seqcontlen, tag);
215 /* Output SEQUENCE header */
216 ASN1_put_object(out, ndef, seqcontlen, tag, aclass);
217 for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
218 const ASN1_TEMPLATE *seqtt;
219 ASN1_VALUE **pseqval;
220 seqtt = asn1_do_adb(pval, tt, 1);
223 pseqval = asn1_get_field_ptr(pval, seqtt);
224 /* FIXME: check for errors in enhanced version */
225 asn1_template_ex_i2d(pseqval, out, seqtt, -1, aclass);
229 if (asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it, NULL))
240 int ASN1_template_i2d(ASN1_VALUE **pval, unsigned char **out,
241 const ASN1_TEMPLATE *tt)
243 return asn1_template_ex_i2d(pval, out, tt, -1, 0);
246 static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
247 const ASN1_TEMPLATE *tt, int tag, int iclass)
249 int i, ret, flags, ttag, tclass, ndef;
252 * Work out tag and class to use: tagging may come either from the
253 * template or the arguments, not both because this would create
254 * ambiguity. Additionally the iclass argument may contain some
255 * additional flags which should be noted and passed down to other
258 if (flags & ASN1_TFLG_TAG_MASK) {
259 /* Error if argument and template tagging */
261 /* FIXME: error code here */
263 /* Get tagging from template */
265 tclass = flags & ASN1_TFLG_TAG_CLASS;
266 } else if (tag != -1) {
267 /* No template tagging, get from arguments */
269 tclass = iclass & ASN1_TFLG_TAG_CLASS;
275 * Remove any class mask from iflag.
277 iclass &= ~ASN1_TFLG_TAG_CLASS;
280 * At this point 'ttag' contains the outer tag to use, 'tclass' is the
281 * class and iclass is any flags passed to this function.
284 /* if template and arguments require ndef, use it */
285 if ((flags & ASN1_TFLG_NDEF) && (iclass & ASN1_TFLG_NDEF))
290 if (flags & ASN1_TFLG_SK_MASK) {
291 /* SET OF, SEQUENCE OF */
292 STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval;
293 int isset, sktag, skaclass;
294 int skcontlen, sklen;
300 if (flags & ASN1_TFLG_SET_OF) {
302 /* 2 means we reorder */
303 if (flags & ASN1_TFLG_SEQUENCE_OF)
309 * Work out inner tag value: if EXPLICIT or no tagging use underlying
312 if ((ttag != -1) && !(flags & ASN1_TFLG_EXPTAG)) {
316 skaclass = V_ASN1_UNIVERSAL;
320 sktag = V_ASN1_SEQUENCE;
323 /* Determine total length of items */
325 for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
326 skitem = sk_ASN1_VALUE_value(sk, i);
327 skcontlen += ASN1_item_ex_i2d(&skitem, NULL,
328 ASN1_ITEM_ptr(tt->item),
331 sklen = ASN1_object_size(ndef, skcontlen, sktag);
332 /* If EXPLICIT need length of surrounding tag */
333 if (flags & ASN1_TFLG_EXPTAG)
334 ret = ASN1_object_size(ndef, sklen, ttag);
341 /* Now encode this lot... */
343 if (flags & ASN1_TFLG_EXPTAG)
344 ASN1_put_object(out, ndef, sklen, ttag, tclass);
345 /* SET or SEQUENCE and IMPLICIT tag */
346 ASN1_put_object(out, ndef, skcontlen, sktag, skaclass);
347 /* And the stuff itself */
348 asn1_set_seq_out(sk, out, skcontlen, ASN1_ITEM_ptr(tt->item),
352 if (flags & ASN1_TFLG_EXPTAG)
359 if (flags & ASN1_TFLG_EXPTAG) {
360 /* EXPLICIT tagging */
361 /* Find length of tagged item */
362 i = ASN1_item_ex_i2d(pval, NULL, ASN1_ITEM_ptr(tt->item), -1, iclass);
365 /* Find length of EXPLICIT tag */
366 ret = ASN1_object_size(ndef, i, ttag);
368 /* Output tag and item */
369 ASN1_put_object(out, ndef, i, ttag, tclass);
370 ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item), -1, iclass);
377 /* Either normal or IMPLICIT tagging: combine class and flags */
378 return ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item),
379 ttag, tclass | iclass);
383 /* Temporary structure used to hold DER encoding of items for SET OF */
391 static int der_cmp(const void *a, const void *b)
393 const DER_ENC *d1 = a, *d2 = b;
395 cmplen = (d1->length < d2->length) ? d1->length : d2->length;
396 i = memcmp(d1->data, d2->data, cmplen);
399 return d1->length - d2->length;
402 /* Output the content octets of SET OF or SEQUENCE OF */
404 static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *sk, unsigned char **out,
405 int skcontlen, const ASN1_ITEM *item,
406 int do_sort, int iclass)
410 unsigned char *tmpdat = NULL, *p = NULL;
411 DER_ENC *derlst = NULL, *tder;
413 /* Don't need to sort less than 2 items */
414 if (sk_ASN1_VALUE_num(sk) < 2)
417 derlst = OPENSSL_malloc(sk_ASN1_VALUE_num(sk)
421 tmpdat = OPENSSL_malloc(skcontlen);
423 OPENSSL_free(derlst);
428 /* If not sorting just output each item */
430 for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
431 skitem = sk_ASN1_VALUE_value(sk, i);
432 ASN1_item_ex_i2d(&skitem, out, item, -1, iclass);
438 /* Doing sort: build up a list of each member's DER encoding */
439 for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++) {
440 skitem = sk_ASN1_VALUE_value(sk, i);
442 tder->length = ASN1_item_ex_i2d(&skitem, &p, item, -1, iclass);
443 tder->field = skitem;
447 qsort(derlst, sk_ASN1_VALUE_num(sk), sizeof(*derlst), der_cmp);
448 /* Output sorted DER encoding */
450 for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++) {
451 memcpy(p, tder->data, tder->length);
455 /* If do_sort is 2 then reorder the STACK */
457 for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++)
458 (void)sk_ASN1_VALUE_set(sk, i, tder->field);
460 OPENSSL_free(derlst);
461 OPENSSL_free(tmpdat);
465 static int asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out,
466 const ASN1_ITEM *it, int tag, int aclass)
476 * Get length of content octets and maybe find out the underlying type.
479 len = asn1_ex_i2c(pval, NULL, &utype, it);
482 * If SEQUENCE, SET or OTHER then header is included in pseudo content
483 * octets so don't include tag+length. We need to check here because the
484 * call to asn1_ex_i2c() could change utype.
486 if ((utype == V_ASN1_SEQUENCE) || (utype == V_ASN1_SET) ||
487 (utype == V_ASN1_OTHER))
492 /* -1 means omit type */
497 /* -2 return is special meaning use ndef */
503 /* If not implicitly tagged get tag from underlying type */
507 /* Output tag+length followed by content octets */
510 ASN1_put_object(out, ndef, len, tag, aclass);
511 asn1_ex_i2c(pval, *out, &utype, it);
519 return ASN1_object_size(ndef, len, tag);
523 /* Produce content octets from a structure */
525 int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype,
528 ASN1_BOOLEAN *tbool = NULL;
532 const unsigned char *cont;
535 const ASN1_PRIMITIVE_FUNCS *pf;
537 if (pf && pf->prim_i2c)
538 return pf->prim_i2c(pval, cout, putype, it);
540 /* Should type be omitted? */
541 if ((it->itype != ASN1_ITYPE_PRIMITIVE)
542 || (it->utype != V_ASN1_BOOLEAN)) {
547 if (it->itype == ASN1_ITYPE_MSTRING) {
548 /* If MSTRING type set the underlying type */
549 strtmp = (ASN1_STRING *)*pval;
550 utype = strtmp->type;
552 } else if (it->utype == V_ASN1_ANY) {
553 /* If ANY set type and pointer to value */
555 typ = (ASN1_TYPE *)*pval;
558 pval = &typ->value.asn1_value;
564 otmp = (ASN1_OBJECT *)*pval;
575 tbool = (ASN1_BOOLEAN *)pval;
578 if (it->utype != V_ASN1_ANY) {
580 * Default handling if value == size field then omit
582 if (*tbool && (it->size > 0))
584 if (!*tbool && !it->size)
587 c = (unsigned char)*tbool;
592 case V_ASN1_BIT_STRING:
593 return i2c_ASN1_BIT_STRING((ASN1_BIT_STRING *)*pval,
594 cout ? &cout : NULL);
597 case V_ASN1_NEG_INTEGER:
598 case V_ASN1_ENUMERATED:
599 case V_ASN1_NEG_ENUMERATED:
601 * These are all have the same content format as ASN1_INTEGER
603 return i2c_ASN1_INTEGER((ASN1_INTEGER *)*pval, cout ? &cout : NULL);
605 case V_ASN1_OCTET_STRING:
606 case V_ASN1_NUMERICSTRING:
607 case V_ASN1_PRINTABLESTRING:
608 case V_ASN1_T61STRING:
609 case V_ASN1_VIDEOTEXSTRING:
610 case V_ASN1_IA5STRING:
612 case V_ASN1_GENERALIZEDTIME:
613 case V_ASN1_GRAPHICSTRING:
614 case V_ASN1_VISIBLESTRING:
615 case V_ASN1_GENERALSTRING:
616 case V_ASN1_UNIVERSALSTRING:
617 case V_ASN1_BMPSTRING:
618 case V_ASN1_UTF8STRING:
619 case V_ASN1_SEQUENCE:
622 /* All based on ASN1_STRING and handled the same */
623 strtmp = (ASN1_STRING *)*pval;
624 /* Special handling for NDEF */
625 if ((it->size == ASN1_TFLG_NDEF)
626 && (strtmp->flags & ASN1_STRING_FLAG_NDEF)) {
631 /* Special return code */
635 len = strtmp->length;
641 memcpy(cout, cont, len);