2 * Copyright 2000-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
12 #include "internal/cryptlib.h"
13 #include <openssl/asn1.h>
14 #include <openssl/asn1t.h>
15 #include <openssl/objects.h>
16 #include "crypto/asn1.h"
17 #include "asn1_local.h"
19 static int asn1_i2d_ex_primitive(const ASN1_VALUE **pval, unsigned char **out,
20 const ASN1_ITEM *it, int tag, int aclass);
21 static int asn1_set_seq_out(STACK_OF(const_ASN1_VALUE) *sk,
23 int skcontlen, const ASN1_ITEM *item,
24 int do_sort, int iclass);
25 static int asn1_template_ex_i2d(const ASN1_VALUE **pval, unsigned char **out,
26 const ASN1_TEMPLATE *tt, int tag, int aclass);
27 static int asn1_item_flags_i2d(const ASN1_VALUE *val, unsigned char **out,
28 const ASN1_ITEM *it, int flags);
29 static int asn1_ex_i2c(const ASN1_VALUE **pval, unsigned char *cout, int *putype,
33 * Top level i2d equivalents: the 'ndef' variant instructs the encoder to use
34 * indefinite length constructed encoding, where appropriate
37 int ASN1_item_ndef_i2d(const ASN1_VALUE *val, unsigned char **out,
40 return asn1_item_flags_i2d(val, out, it, ASN1_TFLG_NDEF);
43 int ASN1_item_i2d(const ASN1_VALUE *val, unsigned char **out, const ASN1_ITEM *it)
45 return asn1_item_flags_i2d(val, out, it, 0);
49 * Encode an ASN1 item, this is use by the standard 'i2d' function. 'out'
50 * points to a buffer to output the data to. The new i2d has one additional
51 * feature. If the output buffer is NULL (i.e. *out == NULL) then a buffer is
52 * allocated and populated with the encoding.
55 static int asn1_item_flags_i2d(const ASN1_VALUE *val, unsigned char **out,
56 const ASN1_ITEM *it, int flags)
58 if (out != NULL && *out == NULL) {
59 unsigned char *p, *buf;
62 len = ASN1_item_ex_i2d(&val, NULL, it, -1, flags);
65 if ((buf = OPENSSL_malloc(len)) == NULL) {
66 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
70 ASN1_item_ex_i2d(&val, &p, it, -1, flags);
75 return ASN1_item_ex_i2d(&val, out, it, -1, flags);
79 * Encode an item, taking care of IMPLICIT tagging (if any). This function
80 * performs the normal item handling: it can be used in external types.
83 int ASN1_item_ex_i2d(const ASN1_VALUE **pval, unsigned char **out,
84 const ASN1_ITEM *it, int tag, int aclass)
86 const ASN1_TEMPLATE *tt = NULL;
87 int i, seqcontlen, seqlen, ndef = 1;
88 const ASN1_EXTERN_FUNCS *ef;
89 const ASN1_AUX *aux = it->funcs;
90 ASN1_aux_const_cb *asn1_cb = NULL;
92 if ((it->itype != ASN1_ITYPE_PRIMITIVE) && *pval == NULL)
96 asn1_cb = ((aux->flags & ASN1_AFLG_CONST_CB) != 0) ? aux->asn1_const_cb
97 : (ASN1_aux_const_cb *)aux->asn1_cb; /* backward compatibility */
102 case ASN1_ITYPE_PRIMITIVE:
104 return asn1_template_ex_i2d(pval, out, it->templates,
106 return asn1_i2d_ex_primitive(pval, out, it, tag, aclass);
108 case ASN1_ITYPE_MSTRING:
110 * It never makes sense for multi-strings to have implicit tagging, so
111 * if tag != -1, then this looks like an error in the template.
114 ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_TEMPLATE);
117 return asn1_i2d_ex_primitive(pval, out, it, -1, aclass);
119 case ASN1_ITYPE_CHOICE:
121 * It never makes sense for CHOICE types to have implicit tagging, so
122 * if tag != -1, then this looks like an error in the template.
125 ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_TEMPLATE);
128 if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it, NULL))
130 i = ossl_asn1_get_choice_selector_const(pval, it);
131 if ((i >= 0) && (i < it->tcount)) {
132 const ASN1_VALUE **pchval;
133 const ASN1_TEMPLATE *chtt;
134 chtt = it->templates + i;
135 pchval = ossl_asn1_get_const_field_ptr(pval, chtt);
136 return asn1_template_ex_i2d(pchval, out, chtt, -1, aclass);
138 /* Fixme: error condition if selector out of range */
139 if (asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it, NULL))
143 case ASN1_ITYPE_EXTERN:
144 /* If new style i2d it does all the work */
146 return ef->asn1_ex_i2d(pval, out, it, tag, aclass);
148 case ASN1_ITYPE_NDEF_SEQUENCE:
149 /* Use indefinite length constructed if requested */
150 if (aclass & ASN1_TFLG_NDEF)
154 case ASN1_ITYPE_SEQUENCE:
155 i = ossl_asn1_enc_restore(&seqcontlen, out, pval, it);
156 /* An error occurred */
159 /* We have a valid cached encoding... */
162 /* Otherwise carry on */
164 /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
166 tag = V_ASN1_SEQUENCE;
167 /* Retain any other flags in aclass */
168 aclass = (aclass & ~ASN1_TFLG_TAG_CLASS)
171 if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it, NULL))
173 /* First work out sequence content length */
174 for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
175 const ASN1_TEMPLATE *seqtt;
176 const ASN1_VALUE **pseqval;
178 seqtt = ossl_asn1_do_adb(*pval, tt, 1);
181 pseqval = ossl_asn1_get_const_field_ptr(pval, seqtt);
182 tmplen = asn1_template_ex_i2d(pseqval, NULL, seqtt, -1, aclass);
183 if (tmplen == -1 || (tmplen > INT_MAX - seqcontlen))
185 seqcontlen += tmplen;
188 seqlen = ASN1_object_size(ndef, seqcontlen, tag);
189 if (!out || seqlen == -1)
191 /* Output SEQUENCE header */
192 ASN1_put_object(out, ndef, seqcontlen, tag, aclass);
193 for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
194 const ASN1_TEMPLATE *seqtt;
195 const ASN1_VALUE **pseqval;
196 seqtt = ossl_asn1_do_adb(*pval, tt, 1);
199 pseqval = ossl_asn1_get_const_field_ptr(pval, seqtt);
200 /* FIXME: check for errors in enhanced version */
201 asn1_template_ex_i2d(pseqval, out, seqtt, -1, aclass);
205 if (asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it, NULL))
216 static int asn1_template_ex_i2d(const ASN1_VALUE **pval, unsigned char **out,
217 const ASN1_TEMPLATE *tt, int tag, int iclass)
219 const int flags = tt->flags;
220 int i, ret, ttag, tclass, ndef, len;
221 const ASN1_VALUE *tval;
224 * If field is embedded then val needs fixing so it is a pointer to
225 * a pointer to a field.
227 if (flags & ASN1_TFLG_EMBED) {
228 tval = (ASN1_VALUE *)pval;
232 * Work out tag and class to use: tagging may come either from the
233 * template or the arguments, not both because this would create
234 * ambiguity. Additionally the iclass argument may contain some
235 * additional flags which should be noted and passed down to other
238 if (flags & ASN1_TFLG_TAG_MASK) {
239 /* Error if argument and template tagging */
241 /* FIXME: error code here */
243 /* Get tagging from template */
245 tclass = flags & ASN1_TFLG_TAG_CLASS;
246 } else if (tag != -1) {
247 /* No template tagging, get from arguments */
249 tclass = iclass & ASN1_TFLG_TAG_CLASS;
255 * Remove any class mask from iflag.
257 iclass &= ~ASN1_TFLG_TAG_CLASS;
260 * At this point 'ttag' contains the outer tag to use, 'tclass' is the
261 * class and iclass is any flags passed to this function.
264 /* if template and arguments require ndef, use it */
265 if ((flags & ASN1_TFLG_NDEF) && (iclass & ASN1_TFLG_NDEF))
270 if (flags & ASN1_TFLG_SK_MASK) {
271 /* SET OF, SEQUENCE OF */
272 STACK_OF(const_ASN1_VALUE) *sk = (STACK_OF(const_ASN1_VALUE) *)*pval;
273 int isset, sktag, skaclass;
274 int skcontlen, sklen;
275 const ASN1_VALUE *skitem;
280 if (flags & ASN1_TFLG_SET_OF) {
282 /* 2 means we reorder */
283 if (flags & ASN1_TFLG_SEQUENCE_OF)
289 * Work out inner tag value: if EXPLICIT or no tagging use underlying
292 if ((ttag != -1) && !(flags & ASN1_TFLG_EXPTAG)) {
296 skaclass = V_ASN1_UNIVERSAL;
300 sktag = V_ASN1_SEQUENCE;
303 /* Determine total length of items */
305 for (i = 0; i < sk_const_ASN1_VALUE_num(sk); i++) {
306 skitem = sk_const_ASN1_VALUE_value(sk, i);
307 len = ASN1_item_ex_i2d(&skitem, NULL, ASN1_ITEM_ptr(tt->item),
309 if (len == -1 || (skcontlen > INT_MAX - len))
311 if (len == 0 && (tt->flags & ASN1_TFLG_OPTIONAL) == 0) {
312 ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_ZERO_CONTENT);
317 sklen = ASN1_object_size(ndef, skcontlen, sktag);
320 /* If EXPLICIT need length of surrounding tag */
321 if (flags & ASN1_TFLG_EXPTAG)
322 ret = ASN1_object_size(ndef, sklen, ttag);
326 if (!out || ret == -1)
329 /* Now encode this lot... */
331 if (flags & ASN1_TFLG_EXPTAG)
332 ASN1_put_object(out, ndef, sklen, ttag, tclass);
333 /* SET or SEQUENCE and IMPLICIT tag */
334 ASN1_put_object(out, ndef, skcontlen, sktag, skaclass);
335 /* And the stuff itself */
336 asn1_set_seq_out(sk, out, skcontlen, ASN1_ITEM_ptr(tt->item),
340 if (flags & ASN1_TFLG_EXPTAG)
347 if (flags & ASN1_TFLG_EXPTAG) {
348 /* EXPLICIT tagging */
349 /* Find length of tagged item */
350 i = ASN1_item_ex_i2d(pval, NULL, ASN1_ITEM_ptr(tt->item), -1, iclass);
352 if ((tt->flags & ASN1_TFLG_OPTIONAL) == 0) {
353 ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_ZERO_CONTENT);
358 /* Find length of EXPLICIT tag */
359 ret = ASN1_object_size(ndef, i, ttag);
360 if (out && ret != -1) {
361 /* Output tag and item */
362 ASN1_put_object(out, ndef, i, ttag, tclass);
363 ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item), -1, iclass);
370 /* Either normal or IMPLICIT tagging: combine class and flags */
371 len = ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item),
372 ttag, tclass | iclass);
373 if (len == 0 && (tt->flags & ASN1_TFLG_OPTIONAL) == 0) {
374 ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_ZERO_CONTENT);
380 /* Temporary structure used to hold DER encoding of items for SET OF */
385 const ASN1_VALUE *field;
388 static int der_cmp(const void *a, const void *b)
390 const DER_ENC *d1 = a, *d2 = b;
392 cmplen = (d1->length < d2->length) ? d1->length : d2->length;
393 i = memcmp(d1->data, d2->data, cmplen);
396 return d1->length - d2->length;
399 /* Output the content octets of SET OF or SEQUENCE OF */
401 static int asn1_set_seq_out(STACK_OF(const_ASN1_VALUE) *sk,
403 int skcontlen, const ASN1_ITEM *item,
404 int do_sort, int iclass)
407 const ASN1_VALUE *skitem;
408 unsigned char *tmpdat = NULL, *p = NULL;
409 DER_ENC *derlst = NULL, *tder;
412 /* Don't need to sort less than 2 items */
413 if (sk_const_ASN1_VALUE_num(sk) < 2)
416 derlst = OPENSSL_malloc(sk_const_ASN1_VALUE_num(sk)
418 if (derlst == NULL) {
419 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
422 tmpdat = OPENSSL_malloc(skcontlen);
423 if (tmpdat == NULL) {
424 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
429 /* If not sorting just output each item */
431 for (i = 0; i < sk_const_ASN1_VALUE_num(sk); i++) {
432 skitem = sk_const_ASN1_VALUE_value(sk, i);
433 ASN1_item_ex_i2d(&skitem, out, item, -1, iclass);
439 /* Doing sort: build up a list of each member's DER encoding */
440 for (i = 0, tder = derlst; i < sk_const_ASN1_VALUE_num(sk); i++, tder++) {
441 skitem = sk_const_ASN1_VALUE_value(sk, i);
443 tder->length = ASN1_item_ex_i2d(&skitem, &p, item, -1, iclass);
444 tder->field = skitem;
448 qsort(derlst, sk_const_ASN1_VALUE_num(sk), sizeof(*derlst), der_cmp);
449 /* Output sorted DER encoding */
451 for (i = 0, tder = derlst; i < sk_const_ASN1_VALUE_num(sk); i++, tder++) {
452 memcpy(p, tder->data, tder->length);
456 /* If do_sort is 2 then reorder the STACK */
458 for (i = 0, tder = derlst; i < sk_const_ASN1_VALUE_num(sk); i++, tder++)
459 (void)sk_const_ASN1_VALUE_set(sk, i, tder->field);
463 OPENSSL_free(derlst);
464 OPENSSL_free(tmpdat);
468 static int asn1_i2d_ex_primitive(const ASN1_VALUE **pval, unsigned char **out,
469 const ASN1_ITEM *it, int tag, int aclass)
479 * Get length of content octets and maybe find out the underlying type.
482 len = asn1_ex_i2c(pval, NULL, &utype, it);
485 * If SEQUENCE, SET or OTHER then header is included in pseudo content
486 * octets so don't include tag+length. We need to check here because the
487 * call to asn1_ex_i2c() could change utype.
489 if ((utype == V_ASN1_SEQUENCE) || (utype == V_ASN1_SET) ||
490 (utype == V_ASN1_OTHER))
495 /* -1 means omit type */
500 /* -2 return is special meaning use ndef */
506 /* If not implicitly tagged get tag from underlying type */
510 /* Output tag+length followed by content octets */
513 ASN1_put_object(out, ndef, len, tag, aclass);
514 asn1_ex_i2c(pval, *out, &utype, it);
522 return ASN1_object_size(ndef, len, tag);
526 /* Produce content octets from a structure */
528 static int asn1_ex_i2c(const ASN1_VALUE **pval, unsigned char *cout, int *putype,
531 ASN1_BOOLEAN *tbool = NULL;
535 const unsigned char *cont;
538 const ASN1_PRIMITIVE_FUNCS *pf;
540 if (pf && pf->prim_i2c)
541 return pf->prim_i2c(pval, cout, putype, it);
543 /* Should type be omitted? */
544 if ((it->itype != ASN1_ITYPE_PRIMITIVE)
545 || (it->utype != V_ASN1_BOOLEAN)) {
550 if (it->itype == ASN1_ITYPE_MSTRING) {
551 /* If MSTRING type set the underlying type */
552 strtmp = (ASN1_STRING *)*pval;
553 utype = strtmp->type;
555 } else if (it->utype == V_ASN1_ANY) {
556 /* If ANY set type and pointer to value */
558 typ = (ASN1_TYPE *)*pval;
561 pval = (const ASN1_VALUE **)&typ->value.asn1_value; /* actually is const */
567 otmp = (ASN1_OBJECT *)*pval;
570 if (cont == NULL || len == 0)
580 tbool = (ASN1_BOOLEAN *)pval;
583 if (it->utype != V_ASN1_ANY) {
585 * Default handling if value == size field then omit
587 if (*tbool && (it->size > 0))
589 if (!*tbool && !it->size)
592 c = (unsigned char)*tbool;
597 case V_ASN1_BIT_STRING:
598 return ossl_i2c_ASN1_BIT_STRING((ASN1_BIT_STRING *)*pval,
599 cout ? &cout : NULL);
602 case V_ASN1_ENUMERATED:
604 * These are all have the same content format as ASN1_INTEGER
606 return ossl_i2c_ASN1_INTEGER((ASN1_INTEGER *)*pval, cout ? &cout : NULL);
608 case V_ASN1_OCTET_STRING:
609 case V_ASN1_NUMERICSTRING:
610 case V_ASN1_PRINTABLESTRING:
611 case V_ASN1_T61STRING:
612 case V_ASN1_VIDEOTEXSTRING:
613 case V_ASN1_IA5STRING:
615 case V_ASN1_GENERALIZEDTIME:
616 case V_ASN1_GRAPHICSTRING:
617 case V_ASN1_VISIBLESTRING:
618 case V_ASN1_GENERALSTRING:
619 case V_ASN1_UNIVERSALSTRING:
620 case V_ASN1_BMPSTRING:
621 case V_ASN1_UTF8STRING:
622 case V_ASN1_SEQUENCE:
625 /* All based on ASN1_STRING and handled the same */
626 strtmp = (ASN1_STRING *)*pval;
627 /* Special handling for NDEF */
628 if ((it->size == ASN1_TFLG_NDEF)
629 && (strtmp->flags & ASN1_STRING_FLAG_NDEF)) {
634 /* Special return code */
638 len = strtmp->length;
644 memcpy(cout, cont, len);