2 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
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).
61 #include "internal/cryptlib.h"
62 #include <openssl/asn1.h>
63 #include <openssl/asn1t.h>
64 #include <openssl/objects.h>
65 #include "internal/asn1_int.h"
66 #include "asn1_locl.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);
77 static int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype,
81 * Top level i2d equivalents: the 'ndef' variant instructs the encoder to use
82 * indefinite length constructed encoding, where appropriate
85 int ASN1_item_ndef_i2d(ASN1_VALUE *val, unsigned char **out,
88 return asn1_item_flags_i2d(val, out, it, ASN1_TFLG_NDEF);
91 int ASN1_item_i2d(ASN1_VALUE *val, unsigned char **out, const ASN1_ITEM *it)
93 return asn1_item_flags_i2d(val, out, it, 0);
97 * Encode an ASN1 item, this is use by the standard 'i2d' function. 'out'
98 * points to a buffer to output the data to. The new i2d has one additional
99 * feature. If the output 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)
107 unsigned char *p, *buf;
109 len = ASN1_item_ex_i2d(&val, NULL, it, -1, flags);
112 buf = OPENSSL_malloc(len);
116 ASN1_item_ex_i2d(&val, &p, it, -1, flags);
121 return ASN1_item_ex_i2d(&val, out, it, -1, flags);
125 * Encode an item, taking care of IMPLICIT tagging (if any). This function
126 * performs the normal item handling: it can be used in external types.
129 int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
130 const ASN1_ITEM *it, int tag, int aclass)
132 const ASN1_TEMPLATE *tt = NULL;
133 int i, seqcontlen, seqlen, ndef = 1;
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);
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, NULL))
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, NULL))
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_NDEF_SEQUENCE:
177 /* Use indefinite length constructed if requested */
178 if (aclass & ASN1_TFLG_NDEF)
182 case ASN1_ITYPE_SEQUENCE:
183 i = asn1_enc_restore(&seqcontlen, out, pval, it);
184 /* An error occurred */
187 /* We have a valid cached encoding... */
190 /* Otherwise carry on */
192 /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
194 tag = V_ASN1_SEQUENCE;
195 /* Retain any other flags in aclass */
196 aclass = (aclass & ~ASN1_TFLG_TAG_CLASS)
199 if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it, NULL))
201 /* First work out sequence content length */
202 for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
203 const ASN1_TEMPLATE *seqtt;
204 ASN1_VALUE **pseqval;
205 seqtt = asn1_do_adb(pval, tt, 1);
208 pseqval = asn1_get_field_ptr(pval, seqtt);
209 /* FIXME: check for errors in enhanced version */
210 seqcontlen += asn1_template_ex_i2d(pseqval, NULL, seqtt,
214 seqlen = ASN1_object_size(ndef, seqcontlen, tag);
217 /* Output SEQUENCE header */
218 ASN1_put_object(out, ndef, seqcontlen, tag, aclass);
219 for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
220 const ASN1_TEMPLATE *seqtt;
221 ASN1_VALUE **pseqval;
222 seqtt = asn1_do_adb(pval, tt, 1);
225 pseqval = asn1_get_field_ptr(pval, seqtt);
226 /* FIXME: check for errors in enhanced version */
227 asn1_template_ex_i2d(pseqval, out, seqtt, -1, aclass);
231 if (asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it, NULL))
242 static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
243 const ASN1_TEMPLATE *tt, int tag, int iclass)
245 int i, ret, flags, ttag, tclass, ndef;
250 * If field is embedded then val needs fixing so it is a pointer to
251 * a pointer to a field.
253 if (flags & ASN1_TFLG_EMBED) {
254 tval = (ASN1_VALUE *)pval;
258 * Work out tag and class to use: tagging may come either from the
259 * template or the arguments, not both because this would create
260 * ambiguity. Additionally the iclass argument may contain some
261 * additional flags which should be noted and passed down to other
264 if (flags & ASN1_TFLG_TAG_MASK) {
265 /* Error if argument and template tagging */
267 /* FIXME: error code here */
269 /* Get tagging from template */
271 tclass = flags & ASN1_TFLG_TAG_CLASS;
272 } else if (tag != -1) {
273 /* No template tagging, get from arguments */
275 tclass = iclass & ASN1_TFLG_TAG_CLASS;
281 * Remove any class mask from iflag.
283 iclass &= ~ASN1_TFLG_TAG_CLASS;
286 * At this point 'ttag' contains the outer tag to use, 'tclass' is the
287 * class and iclass is any flags passed to this function.
290 /* if template and arguments require ndef, use it */
291 if ((flags & ASN1_TFLG_NDEF) && (iclass & ASN1_TFLG_NDEF))
296 if (flags & ASN1_TFLG_SK_MASK) {
297 /* SET OF, SEQUENCE OF */
298 STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval;
299 int isset, sktag, skaclass;
300 int skcontlen, sklen;
306 if (flags & ASN1_TFLG_SET_OF) {
308 /* 2 means we reorder */
309 if (flags & ASN1_TFLG_SEQUENCE_OF)
315 * Work out inner tag value: if EXPLICIT or no tagging use underlying
318 if ((ttag != -1) && !(flags & ASN1_TFLG_EXPTAG)) {
322 skaclass = V_ASN1_UNIVERSAL;
326 sktag = V_ASN1_SEQUENCE;
329 /* Determine total length of items */
331 for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
332 skitem = sk_ASN1_VALUE_value(sk, i);
333 skcontlen += ASN1_item_ex_i2d(&skitem, NULL,
334 ASN1_ITEM_ptr(tt->item),
337 sklen = ASN1_object_size(ndef, skcontlen, sktag);
338 /* If EXPLICIT need length of surrounding tag */
339 if (flags & ASN1_TFLG_EXPTAG)
340 ret = ASN1_object_size(ndef, sklen, ttag);
347 /* Now encode this lot... */
349 if (flags & ASN1_TFLG_EXPTAG)
350 ASN1_put_object(out, ndef, sklen, ttag, tclass);
351 /* SET or SEQUENCE and IMPLICIT tag */
352 ASN1_put_object(out, ndef, skcontlen, sktag, skaclass);
353 /* And the stuff itself */
354 asn1_set_seq_out(sk, out, skcontlen, ASN1_ITEM_ptr(tt->item),
358 if (flags & ASN1_TFLG_EXPTAG)
365 if (flags & ASN1_TFLG_EXPTAG) {
366 /* EXPLICIT tagging */
367 /* Find length of tagged item */
368 i = ASN1_item_ex_i2d(pval, NULL, ASN1_ITEM_ptr(tt->item), -1, iclass);
371 /* Find length of EXPLICIT tag */
372 ret = ASN1_object_size(ndef, i, ttag);
374 /* Output tag and item */
375 ASN1_put_object(out, ndef, i, ttag, tclass);
376 ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item), -1, iclass);
383 /* Either normal or IMPLICIT tagging: combine class and flags */
384 return ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item),
385 ttag, tclass | iclass);
389 /* Temporary structure used to hold DER encoding of items for SET OF */
397 static int der_cmp(const void *a, const void *b)
399 const DER_ENC *d1 = a, *d2 = b;
401 cmplen = (d1->length < d2->length) ? d1->length : d2->length;
402 i = memcmp(d1->data, d2->data, cmplen);
405 return d1->length - d2->length;
408 /* Output the content octets of SET OF or SEQUENCE OF */
410 static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *sk, unsigned char **out,
411 int skcontlen, const ASN1_ITEM *item,
412 int do_sort, int iclass)
416 unsigned char *tmpdat = NULL, *p = NULL;
417 DER_ENC *derlst = NULL, *tder;
419 /* Don't need to sort less than 2 items */
420 if (sk_ASN1_VALUE_num(sk) < 2)
423 derlst = OPENSSL_malloc(sk_ASN1_VALUE_num(sk)
427 tmpdat = OPENSSL_malloc(skcontlen);
428 if (tmpdat == NULL) {
429 OPENSSL_free(derlst);
434 /* If not sorting just output each item */
436 for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
437 skitem = sk_ASN1_VALUE_value(sk, i);
438 ASN1_item_ex_i2d(&skitem, out, item, -1, iclass);
444 /* Doing sort: build up a list of each member's DER encoding */
445 for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++) {
446 skitem = sk_ASN1_VALUE_value(sk, i);
448 tder->length = ASN1_item_ex_i2d(&skitem, &p, item, -1, iclass);
449 tder->field = skitem;
453 qsort(derlst, sk_ASN1_VALUE_num(sk), sizeof(*derlst), der_cmp);
454 /* Output sorted DER encoding */
456 for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++) {
457 memcpy(p, tder->data, tder->length);
461 /* If do_sort is 2 then reorder the STACK */
463 for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++)
464 (void)sk_ASN1_VALUE_set(sk, i, tder->field);
466 OPENSSL_free(derlst);
467 OPENSSL_free(tmpdat);
471 static int asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out,
472 const ASN1_ITEM *it, int tag, int aclass)
482 * Get length of content octets and maybe find out the underlying type.
485 len = asn1_ex_i2c(pval, NULL, &utype, it);
488 * If SEQUENCE, SET or OTHER then header is included in pseudo content
489 * octets so don't include tag+length. We need to check here because the
490 * call to asn1_ex_i2c() could change utype.
492 if ((utype == V_ASN1_SEQUENCE) || (utype == V_ASN1_SET) ||
493 (utype == V_ASN1_OTHER))
498 /* -1 means omit type */
503 /* -2 return is special meaning use ndef */
509 /* If not implicitly tagged get tag from underlying type */
513 /* Output tag+length followed by content octets */
516 ASN1_put_object(out, ndef, len, tag, aclass);
517 asn1_ex_i2c(pval, *out, &utype, it);
525 return ASN1_object_size(ndef, len, tag);
529 /* Produce content octets from a structure */
531 static int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype,
534 ASN1_BOOLEAN *tbool = NULL;
538 const unsigned char *cont;
541 const ASN1_PRIMITIVE_FUNCS *pf;
543 if (pf && pf->prim_i2c)
544 return pf->prim_i2c(pval, cout, putype, it);
546 /* Should type be omitted? */
547 if ((it->itype != ASN1_ITYPE_PRIMITIVE)
548 || (it->utype != V_ASN1_BOOLEAN)) {
553 if (it->itype == ASN1_ITYPE_MSTRING) {
554 /* If MSTRING type set the underlying type */
555 strtmp = (ASN1_STRING *)*pval;
556 utype = strtmp->type;
558 } else if (it->utype == V_ASN1_ANY) {
559 /* If ANY set type and pointer to value */
561 typ = (ASN1_TYPE *)*pval;
564 pval = &typ->value.asn1_value;
570 otmp = (ASN1_OBJECT *)*pval;
581 tbool = (ASN1_BOOLEAN *)pval;
584 if (it->utype != V_ASN1_ANY) {
586 * Default handling if value == size field then omit
588 if (*tbool && (it->size > 0))
590 if (!*tbool && !it->size)
593 c = (unsigned char)*tbool;
598 case V_ASN1_BIT_STRING:
599 return i2c_ASN1_BIT_STRING((ASN1_BIT_STRING *)*pval,
600 cout ? &cout : NULL);
603 case V_ASN1_NEG_INTEGER:
604 case V_ASN1_ENUMERATED:
605 case V_ASN1_NEG_ENUMERATED:
607 * These are all have the same content format as ASN1_INTEGER
609 return i2c_ASN1_INTEGER((ASN1_INTEGER *)*pval, cout ? &cout : NULL);
611 case V_ASN1_OCTET_STRING:
612 case V_ASN1_NUMERICSTRING:
613 case V_ASN1_PRINTABLESTRING:
614 case V_ASN1_T61STRING:
615 case V_ASN1_VIDEOTEXSTRING:
616 case V_ASN1_IA5STRING:
618 case V_ASN1_GENERALIZEDTIME:
619 case V_ASN1_GRAPHICSTRING:
620 case V_ASN1_VISIBLESTRING:
621 case V_ASN1_GENERALSTRING:
622 case V_ASN1_UNIVERSALSTRING:
623 case V_ASN1_BMPSTRING:
624 case V_ASN1_UTF8STRING:
625 case V_ASN1_SEQUENCE:
628 /* All based on ASN1_STRING and handled the same */
629 strtmp = (ASN1_STRING *)*pval;
630 /* Special handling for NDEF */
631 if ((it->size == ASN1_TFLG_NDEF)
632 && (strtmp->flags & ASN1_STRING_FLAG_NDEF)) {
637 /* Special return code */
641 len = strtmp->length;
647 memcpy(cout, cont, len);