1 /* $OpenBSD: tasn_enc.c,v 1.21 2016/12/30 16:04:34 jsing Exp $ */
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).
62 #include <openssl/asn1.h>
63 #include <openssl/asn1t.h>
64 #include <openssl/objects.h>
66 static int asn1_i2d_ex_primitive(ASN1_VALUE
**pval
, unsigned char **out
,
67 const ASN1_ITEM
*it
, int tag
, int aclass
);
68 static int asn1_set_seq_out(STACK_OF(ASN1_VALUE
) *sk
, unsigned char **out
,
69 int skcontlen
, const ASN1_ITEM
*item
, int do_sort
, int iclass
);
70 static int asn1_template_ex_i2d(ASN1_VALUE
**pval
, unsigned char **out
,
71 const ASN1_TEMPLATE
*tt
, int tag
, int aclass
);
72 static int asn1_item_flags_i2d(ASN1_VALUE
*val
, unsigned char **out
,
73 const ASN1_ITEM
*it
, int flags
);
74 static int asn1_ex_i2c(ASN1_VALUE
**pval
, unsigned char *cout
, int *putype
,
77 /* Top level i2d equivalents: the 'ndef' variant instructs the encoder
78 * to use indefinite length constructed encoding, where appropriate
82 ASN1_item_ndef_i2d(ASN1_VALUE
*val
, unsigned char **out
, const ASN1_ITEM
*it
)
84 return asn1_item_flags_i2d(val
, out
, it
, ASN1_TFLG_NDEF
);
88 ASN1_item_i2d(ASN1_VALUE
*val
, unsigned char **out
, const ASN1_ITEM
*it
)
90 return asn1_item_flags_i2d(val
, out
, it
, 0);
93 /* Encode an ASN1 item, this is use by the
94 * standard 'i2d' function. 'out' points to
95 * a buffer to output the data to.
97 * The new i2d has one additional feature. If the output
98 * buffer is NULL (i.e. *out == NULL) then a buffer is
99 * allocated and populated with the encoding.
103 asn1_item_flags_i2d(ASN1_VALUE
*val
, unsigned char **out
, const ASN1_ITEM
*it
,
107 unsigned char *p
, *buf
;
109 len
= ASN1_item_ex_i2d(&val
, NULL
, it
, -1, flags
);
116 ASN1_item_ex_i2d(&val
, &p
, it
, -1, flags
);
121 return ASN1_item_ex_i2d(&val
, out
, it
, -1, flags
);
124 /* Encode an item, taking care of IMPLICIT tagging (if any).
125 * This function performs the normal item handling: it can be
126 * used in external types.
130 ASN1_item_ex_i2d(ASN1_VALUE
**pval
, unsigned char **out
, const ASN1_ITEM
*it
,
133 const ASN1_TEMPLATE
*tt
= NULL
;
134 int i
, seqcontlen
, seqlen
, ndef
= 1;
135 const ASN1_EXTERN_FUNCS
*ef
;
136 const ASN1_AUX
*aux
= it
->funcs
;
137 ASN1_aux_cb
*asn1_cb
= NULL
;
139 if ((it
->itype
!= ASN1_ITYPE_PRIMITIVE
) && !*pval
)
142 if (aux
&& aux
->asn1_cb
)
143 asn1_cb
= aux
->asn1_cb
;
147 case ASN1_ITYPE_PRIMITIVE
:
149 return asn1_template_ex_i2d(pval
, out
, it
->templates
,
151 return asn1_i2d_ex_primitive(pval
, out
, it
, tag
, aclass
);
154 case ASN1_ITYPE_MSTRING
:
155 return asn1_i2d_ex_primitive(pval
, out
, it
, -1, aclass
);
157 case ASN1_ITYPE_CHOICE
:
158 if (asn1_cb
&& !asn1_cb(ASN1_OP_I2D_PRE
, pval
, it
, NULL
))
160 i
= asn1_get_choice_selector(pval
, it
);
161 if ((i
>= 0) && (i
< it
->tcount
)) {
163 const ASN1_TEMPLATE
*chtt
;
164 chtt
= it
->templates
+ i
;
165 pchval
= asn1_get_field_ptr(pval
, chtt
);
166 return asn1_template_ex_i2d(pchval
, out
, chtt
,
169 /* Fixme: error condition if selector out of range */
170 if (asn1_cb
&& !asn1_cb(ASN1_OP_I2D_POST
, pval
, it
, NULL
))
174 case ASN1_ITYPE_EXTERN
:
175 /* If new style i2d it does all the work */
177 return ef
->asn1_ex_i2d(pval
, out
, it
, tag
, aclass
);
179 case ASN1_ITYPE_NDEF_SEQUENCE
:
180 /* Use indefinite length constructed if requested */
181 if (aclass
& ASN1_TFLG_NDEF
)
185 case ASN1_ITYPE_SEQUENCE
:
186 i
= asn1_enc_restore(&seqcontlen
, out
, pval
, it
);
187 /* An error occurred */
190 /* We have a valid cached encoding... */
193 /* Otherwise carry on */
195 /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
197 tag
= V_ASN1_SEQUENCE
;
198 /* Retain any other flags in aclass */
199 aclass
= (aclass
& ~ASN1_TFLG_TAG_CLASS
) |
202 if (asn1_cb
&& !asn1_cb(ASN1_OP_I2D_PRE
, pval
, it
, NULL
))
204 /* First work out sequence content length */
205 for (i
= 0, tt
= it
->templates
; i
< it
->tcount
; tt
++, i
++) {
206 const ASN1_TEMPLATE
*seqtt
;
207 ASN1_VALUE
**pseqval
;
208 seqtt
= asn1_do_adb(pval
, tt
, 1);
211 pseqval
= asn1_get_field_ptr(pval
, seqtt
);
212 /* FIXME: check for errors in enhanced version */
213 seqcontlen
+= asn1_template_ex_i2d(pseqval
, NULL
, seqtt
,
217 seqlen
= ASN1_object_size(ndef
, seqcontlen
, tag
);
220 /* Output SEQUENCE header */
221 ASN1_put_object(out
, ndef
, seqcontlen
, tag
, aclass
);
222 for (i
= 0, tt
= it
->templates
; i
< it
->tcount
; tt
++, i
++) {
223 const ASN1_TEMPLATE
*seqtt
;
224 ASN1_VALUE
**pseqval
;
225 seqtt
= asn1_do_adb(pval
, tt
, 1);
228 pseqval
= asn1_get_field_ptr(pval
, seqtt
);
229 /* FIXME: check for errors in enhanced version */
230 asn1_template_ex_i2d(pseqval
, out
, seqtt
, -1, aclass
);
234 if (asn1_cb
&& !asn1_cb(ASN1_OP_I2D_POST
, pval
, it
, NULL
))
246 ASN1_template_i2d(ASN1_VALUE
**pval
, unsigned char **out
,
247 const ASN1_TEMPLATE
*tt
)
249 return asn1_template_ex_i2d(pval
, out
, tt
, -1, 0);
253 asn1_template_ex_i2d(ASN1_VALUE
**pval
, unsigned char **out
,
254 const ASN1_TEMPLATE
*tt
, int tag
, int iclass
)
256 int i
, ret
, flags
, ttag
, tclass
, ndef
;
258 /* Work out tag and class to use: tagging may come
259 * either from the template or the arguments, not both
260 * because this would create ambiguity. Additionally
261 * the iclass argument may contain some additional flags
262 * which should be noted and passed down to other levels.
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
;
285 /* At this point 'ttag' contains the outer tag to use,
286 * 'tclass' is the class and iclass is any flags passed
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
)
314 /* Work out inner tag value: if EXPLICIT
315 * or no tagging use underlying type.
317 if ((ttag
!= -1) && !(flags
& ASN1_TFLG_EXPTAG
)) {
321 skaclass
= V_ASN1_UNIVERSAL
;
325 sktag
= V_ASN1_SEQUENCE
;
328 /* Determine total length of items */
330 for (i
= 0; i
< sk_ASN1_VALUE_num(sk
); i
++) {
331 skitem
= sk_ASN1_VALUE_value(sk
, i
);
332 skcontlen
+= ASN1_item_ex_i2d(&skitem
, NULL
,
333 tt
->item
, -1, iclass
);
335 sklen
= ASN1_object_size(ndef
, skcontlen
, sktag
);
336 /* If EXPLICIT need length of surrounding tag */
337 if (flags
& ASN1_TFLG_EXPTAG
)
338 ret
= ASN1_object_size(ndef
, sklen
, ttag
);
345 /* Now encode this lot... */
347 if (flags
& ASN1_TFLG_EXPTAG
)
348 ASN1_put_object(out
, ndef
, sklen
, ttag
, tclass
);
349 /* SET or SEQUENCE and IMPLICIT tag */
350 ASN1_put_object(out
, ndef
, skcontlen
, sktag
, skaclass
);
351 /* And the stuff itself */
352 asn1_set_seq_out(sk
, out
, skcontlen
, tt
->item
,
356 if (flags
& ASN1_TFLG_EXPTAG
)
363 if (flags
& ASN1_TFLG_EXPTAG
) {
364 /* EXPLICIT tagging */
365 /* Find length of tagged item */
366 i
= ASN1_item_ex_i2d(pval
, NULL
, tt
->item
,
370 /* Find length of EXPLICIT tag */
371 ret
= ASN1_object_size(ndef
, i
, ttag
);
373 /* Output tag and item */
374 ASN1_put_object(out
, ndef
, i
, ttag
, tclass
);
375 ASN1_item_ex_i2d(pval
, out
, tt
->item
,
383 /* Either normal or IMPLICIT tagging: combine class and flags */
384 return ASN1_item_ex_i2d(pval
, out
, tt
->item
,
385 ttag
, tclass
| iclass
);
388 /* Temporary structure used to hold DER encoding of items for SET OF */
397 der_cmp(const void *a
, const void *b
)
399 const DER_ENC
*d1
= a
, *d2
= b
;
402 cmplen
= (d1
->length
< d2
->length
) ? d1
->length
: d2
->length
;
403 i
= memcmp(d1
->data
, d2
->data
, cmplen
);
406 return d1
->length
- d2
->length
;
409 /* Output the content octets of SET OF or SEQUENCE OF */
412 asn1_set_seq_out(STACK_OF(ASN1_VALUE
) *sk
, unsigned char **out
, int skcontlen
,
413 const ASN1_ITEM
*item
, int do_sort
, int iclass
)
417 unsigned char *tmpdat
= NULL
, *p
= NULL
;
418 DER_ENC
*derlst
= NULL
, *tder
;
421 /* Don't need to sort less than 2 items */
422 if (sk_ASN1_VALUE_num(sk
) < 2)
425 derlst
= reallocarray(NULL
, sk_ASN1_VALUE_num(sk
),
427 tmpdat
= malloc(skcontlen
);
428 if (!derlst
|| !tmpdat
) {
435 /* If not sorting just output each item */
437 for (i
= 0; i
< sk_ASN1_VALUE_num(sk
); i
++) {
438 skitem
= sk_ASN1_VALUE_value(sk
, i
);
439 ASN1_item_ex_i2d(&skitem
, out
, item
, -1, iclass
);
445 /* Doing sort: build up a list of each member's DER encoding */
446 for (i
= 0, tder
= derlst
; i
< sk_ASN1_VALUE_num(sk
); i
++, tder
++) {
447 skitem
= sk_ASN1_VALUE_value(sk
, i
);
449 tder
->length
= ASN1_item_ex_i2d(&skitem
, &p
, item
, -1, iclass
);
450 tder
->field
= skitem
;
454 qsort(derlst
, sk_ASN1_VALUE_num(sk
), sizeof(*derlst
), der_cmp
);
455 /* Output sorted DER encoding */
457 for (i
= 0, tder
= derlst
; i
< sk_ASN1_VALUE_num(sk
); i
++, tder
++) {
458 memcpy(p
, tder
->data
, tder
->length
);
462 /* If do_sort is 2 then reorder the STACK */
464 for (i
= 0, tder
= derlst
; i
< sk_ASN1_VALUE_num(sk
); i
++, tder
++)
465 (void)sk_ASN1_VALUE_set(sk
, i
, tder
->field
);
473 asn1_i2d_ex_primitive(ASN1_VALUE
**pval
, unsigned char **out
,
474 const ASN1_ITEM
*it
, int tag
, int aclass
)
483 /* Get length of content octets and maybe find
484 * out the underlying type.
487 len
= asn1_ex_i2c(pval
, NULL
, &utype
, it
);
489 /* If SEQUENCE, SET or OTHER then header is
490 * included in pseudo content octets so don't
491 * include tag+length. We need to check here
492 * because the call to asn1_ex_i2c() could change
495 if ((utype
== V_ASN1_SEQUENCE
) || (utype
== V_ASN1_SET
) ||
496 (utype
== V_ASN1_OTHER
))
501 /* -1 means omit type */
505 /* -2 return is special meaning use ndef */
511 /* If not implicitly tagged get tag from underlying type */
515 /* Output tag+length followed by content octets */
518 ASN1_put_object(out
, ndef
, len
, tag
, aclass
);
519 asn1_ex_i2c(pval
, *out
, &utype
, it
);
527 return ASN1_object_size(ndef
, len
, tag
);
531 /* Produce content octets from a structure */
534 asn1_ex_i2c(ASN1_VALUE
**pval
, unsigned char *cout
, int *putype
,
537 ASN1_BOOLEAN
*tbool
= NULL
;
541 const unsigned char *cont
;
544 const ASN1_PRIMITIVE_FUNCS
*pf
;
547 if (pf
&& pf
->prim_i2c
)
548 return pf
->prim_i2c(pval
, cout
, putype
, it
);
550 /* Should type be omitted? */
551 if ((it
->itype
!= ASN1_ITYPE_PRIMITIVE
) ||
552 (it
->utype
!= V_ASN1_BOOLEAN
)) {
557 if (it
->itype
== ASN1_ITYPE_MSTRING
) {
558 /* If MSTRING type set the underlying type */
559 strtmp
= (ASN1_STRING
*)*pval
;
560 utype
= strtmp
->type
;
562 } else if (it
->utype
== V_ASN1_ANY
) {
563 /* If ANY set type and pointer to value */
565 typ
= (ASN1_TYPE
*)*pval
;
568 pval
= &typ
->value
.asn1_value
;
574 otmp
= (ASN1_OBJECT
*)*pval
;
585 tbool
= (ASN1_BOOLEAN
*)pval
;
588 if (it
->utype
!= V_ASN1_ANY
) {
589 /* Default handling if value == size field then omit */
590 if (*tbool
&& (it
->size
> 0))
592 if (!*tbool
&& !it
->size
)
595 c
= (unsigned char)*tbool
;
600 case V_ASN1_BIT_STRING
:
601 return i2c_ASN1_BIT_STRING((ASN1_BIT_STRING
*)*pval
,
602 cout
? &cout
: NULL
);
606 case V_ASN1_ENUMERATED
:
607 /* These are all have the same content format
610 return i2c_ASN1_INTEGER((ASN1_INTEGER
*)*pval
,
611 cout
? &cout
: NULL
);
614 case V_ASN1_OCTET_STRING
:
615 case V_ASN1_NUMERICSTRING
:
616 case V_ASN1_PRINTABLESTRING
:
617 case V_ASN1_T61STRING
:
618 case V_ASN1_VIDEOTEXSTRING
:
619 case V_ASN1_IA5STRING
:
621 case V_ASN1_GENERALIZEDTIME
:
622 case V_ASN1_GRAPHICSTRING
:
623 case V_ASN1_VISIBLESTRING
:
624 case V_ASN1_GENERALSTRING
:
625 case V_ASN1_UNIVERSALSTRING
:
626 case V_ASN1_BMPSTRING
:
627 case V_ASN1_UTF8STRING
:
628 case V_ASN1_SEQUENCE
:
631 /* All based on ASN1_STRING and handled the same */
632 strtmp
= (ASN1_STRING
*)*pval
;
633 /* Special handling for NDEF */
634 if ((it
->size
== ASN1_TFLG_NDEF
) &&
635 (strtmp
->flags
& ASN1_STRING_FLAG_NDEF
)) {
640 /* Special return code */
644 len
= strtmp
->length
;
650 memcpy(cout
, cont
, len
);