2 * Copyright 2000-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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(ASN1_VALUE **pval, unsigned char **out,
20 const ASN1_ITEM *it, int tag, int aclass);
21 static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *sk, unsigned char **out,
22 int skcontlen, const ASN1_ITEM *item,
23 int do_sort, int iclass);
24 static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
25 const ASN1_TEMPLATE *tt, int tag, int aclass);
26 static int asn1_item_flags_i2d(ASN1_VALUE *val, unsigned char **out,
27 const ASN1_ITEM *it, int flags);
28 static int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype,
32 * Top level i2d equivalents: the 'ndef' variant instructs the encoder to use
33 * indefinite length constructed encoding, where appropriate
36 int ASN1_item_ndef_i2d(ASN1_VALUE *val, unsigned char **out,
39 return asn1_item_flags_i2d(val, out, it, ASN1_TFLG_NDEF);
42 int ASN1_item_i2d(ASN1_VALUE *val, unsigned char **out, const ASN1_ITEM *it)
44 return asn1_item_flags_i2d(val, out, it, 0);
48 * Encode an ASN1 item, this is use by the standard 'i2d' function. 'out'
49 * points to a buffer to output the data to. The new i2d has one additional
50 * feature. If the output buffer is NULL (i.e. *out == NULL) then a buffer is
51 * allocated and populated with the encoding.
54 static int asn1_item_flags_i2d(ASN1_VALUE *val, unsigned char **out,
55 const ASN1_ITEM *it, int flags)
58 unsigned char *p, *buf;
61 len = ASN1_item_ex_i2d(&val, NULL, it, -1, flags);
64 if ((buf = OPENSSL_malloc(len)) == NULL) {
65 ASN1err(ASN1_F_ASN1_ITEM_FLAGS_I2D, ERR_R_MALLOC_FAILURE);
69 ASN1_item_ex_i2d(&val, &p, it, -1, flags);
74 return ASN1_item_ex_i2d(&val, out, it, -1, flags);
78 * Encode an item, taking care of IMPLICIT tagging (if any). This function
79 * performs the normal item handling: it can be used in external types.
82 int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
83 const ASN1_ITEM *it, int tag, int aclass)
85 const ASN1_TEMPLATE *tt = NULL;
86 int i, seqcontlen, seqlen, ndef = 1;
87 const ASN1_EXTERN_FUNCS *ef;
88 const ASN1_AUX *aux = it->funcs;
89 ASN1_aux_cb *asn1_cb = 0;
91 if ((it->itype != ASN1_ITYPE_PRIMITIVE) && !*pval)
94 if (aux && aux->asn1_cb)
95 asn1_cb = aux->asn1_cb;
99 case ASN1_ITYPE_PRIMITIVE:
101 return asn1_template_ex_i2d(pval, out, it->templates,
103 return asn1_i2d_ex_primitive(pval, out, it, tag, aclass);
105 case ASN1_ITYPE_MSTRING:
107 * It never makes sense for multi-strings to have implicit tagging, so
108 * if tag != -1, then this looks like an error in the template.
111 ASN1err(ASN1_F_ASN1_ITEM_EX_I2D, ASN1_R_BAD_TEMPLATE);
114 return asn1_i2d_ex_primitive(pval, out, it, -1, aclass);
116 case ASN1_ITYPE_CHOICE:
118 * It never makes sense for CHOICE types to have implicit tagging, so
119 * if tag != -1, then this looks like an error in the template.
122 ASN1err(ASN1_F_ASN1_ITEM_EX_I2D, ASN1_R_BAD_TEMPLATE);
125 if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it, NULL))
127 i = asn1_get_choice_selector(pval, it);
128 if ((i >= 0) && (i < it->tcount)) {
130 const ASN1_TEMPLATE *chtt;
131 chtt = it->templates + i;
132 pchval = asn1_get_field_ptr(pval, chtt);
133 return asn1_template_ex_i2d(pchval, out, chtt, -1, aclass);
135 /* Fixme: error condition if selector out of range */
136 if (asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it, NULL))
140 case ASN1_ITYPE_EXTERN:
141 /* If new style i2d it does all the work */
143 return ef->asn1_ex_i2d(pval, out, it, tag, aclass);
145 case ASN1_ITYPE_NDEF_SEQUENCE:
146 /* Use indefinite length constructed if requested */
147 if (aclass & ASN1_TFLG_NDEF)
151 case ASN1_ITYPE_SEQUENCE:
152 i = asn1_enc_restore(&seqcontlen, out, pval, it);
153 /* An error occurred */
156 /* We have a valid cached encoding... */
159 /* Otherwise carry on */
161 /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
163 tag = V_ASN1_SEQUENCE;
164 /* Retain any other flags in aclass */
165 aclass = (aclass & ~ASN1_TFLG_TAG_CLASS)
168 if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it, NULL))
170 /* First work out sequence content length */
171 for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
172 const ASN1_TEMPLATE *seqtt;
173 ASN1_VALUE **pseqval;
175 seqtt = asn1_do_adb(pval, tt, 1);
178 pseqval = asn1_get_field_ptr(pval, seqtt);
179 tmplen = asn1_template_ex_i2d(pseqval, NULL, seqtt, -1, aclass);
180 if (tmplen == -1 || (tmplen > INT_MAX - seqcontlen))
182 seqcontlen += tmplen;
185 seqlen = ASN1_object_size(ndef, seqcontlen, tag);
186 if (!out || seqlen == -1)
188 /* Output SEQUENCE header */
189 ASN1_put_object(out, ndef, seqcontlen, tag, aclass);
190 for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
191 const ASN1_TEMPLATE *seqtt;
192 ASN1_VALUE **pseqval;
193 seqtt = asn1_do_adb(pval, tt, 1);
196 pseqval = asn1_get_field_ptr(pval, seqtt);
197 /* FIXME: check for errors in enhanced version */
198 asn1_template_ex_i2d(pseqval, out, seqtt, -1, aclass);
202 if (asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it, NULL))
213 static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
214 const ASN1_TEMPLATE *tt, int tag, int iclass)
216 int i, ret, flags, ttag, tclass, ndef;
221 * If field is embedded then val needs fixing so it is a pointer to
222 * a pointer to a field.
224 if (flags & ASN1_TFLG_EMBED) {
225 tval = (ASN1_VALUE *)pval;
229 * Work out tag and class to use: tagging may come either from the
230 * template or the arguments, not both because this would create
231 * ambiguity. Additionally the iclass argument may contain some
232 * additional flags which should be noted and passed down to other
235 if (flags & ASN1_TFLG_TAG_MASK) {
236 /* Error if argument and template tagging */
238 /* FIXME: error code here */
240 /* Get tagging from template */
242 tclass = flags & ASN1_TFLG_TAG_CLASS;
243 } else if (tag != -1) {
244 /* No template tagging, get from arguments */
246 tclass = iclass & ASN1_TFLG_TAG_CLASS;
252 * Remove any class mask from iflag.
254 iclass &= ~ASN1_TFLG_TAG_CLASS;
257 * At this point 'ttag' contains the outer tag to use, 'tclass' is the
258 * class and iclass is any flags passed to this function.
261 /* if template and arguments require ndef, use it */
262 if ((flags & ASN1_TFLG_NDEF) && (iclass & ASN1_TFLG_NDEF))
267 if (flags & ASN1_TFLG_SK_MASK) {
268 /* SET OF, SEQUENCE OF */
269 STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval;
270 int isset, sktag, skaclass;
271 int skcontlen, sklen;
277 if (flags & ASN1_TFLG_SET_OF) {
279 /* 2 means we reorder */
280 if (flags & ASN1_TFLG_SEQUENCE_OF)
286 * Work out inner tag value: if EXPLICIT or no tagging use underlying
289 if ((ttag != -1) && !(flags & ASN1_TFLG_EXPTAG)) {
293 skaclass = V_ASN1_UNIVERSAL;
297 sktag = V_ASN1_SEQUENCE;
300 /* Determine total length of items */
302 for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
304 skitem = sk_ASN1_VALUE_value(sk, i);
305 tmplen = ASN1_item_ex_i2d(&skitem, NULL, ASN1_ITEM_ptr(tt->item),
307 if (tmplen == -1 || (skcontlen > INT_MAX - tmplen))
311 sklen = ASN1_object_size(ndef, skcontlen, sktag);
314 /* If EXPLICIT need length of surrounding tag */
315 if (flags & ASN1_TFLG_EXPTAG)
316 ret = ASN1_object_size(ndef, sklen, ttag);
320 if (!out || ret == -1)
323 /* Now encode this lot... */
325 if (flags & ASN1_TFLG_EXPTAG)
326 ASN1_put_object(out, ndef, sklen, ttag, tclass);
327 /* SET or SEQUENCE and IMPLICIT tag */
328 ASN1_put_object(out, ndef, skcontlen, sktag, skaclass);
329 /* And the stuff itself */
330 asn1_set_seq_out(sk, out, skcontlen, ASN1_ITEM_ptr(tt->item),
334 if (flags & ASN1_TFLG_EXPTAG)
341 if (flags & ASN1_TFLG_EXPTAG) {
342 /* EXPLICIT tagging */
343 /* Find length of tagged item */
344 i = ASN1_item_ex_i2d(pval, NULL, ASN1_ITEM_ptr(tt->item), -1, iclass);
347 /* Find length of EXPLICIT tag */
348 ret = ASN1_object_size(ndef, i, ttag);
349 if (out && ret != -1) {
350 /* Output tag and item */
351 ASN1_put_object(out, ndef, i, ttag, tclass);
352 ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item), -1, iclass);
359 /* Either normal or IMPLICIT tagging: combine class and flags */
360 return ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item),
361 ttag, tclass | iclass);
365 /* Temporary structure used to hold DER encoding of items for SET OF */
373 static int der_cmp(const void *a, const void *b)
375 const DER_ENC *d1 = a, *d2 = b;
377 cmplen = (d1->length < d2->length) ? d1->length : d2->length;
378 i = memcmp(d1->data, d2->data, cmplen);
381 return d1->length - d2->length;
384 /* Output the content octets of SET OF or SEQUENCE OF */
386 static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *sk, unsigned char **out,
387 int skcontlen, const ASN1_ITEM *item,
388 int do_sort, int iclass)
392 unsigned char *tmpdat = NULL, *p = NULL;
393 DER_ENC *derlst = NULL, *tder;
395 /* Don't need to sort less than 2 items */
396 if (sk_ASN1_VALUE_num(sk) < 2)
399 derlst = OPENSSL_malloc(sk_ASN1_VALUE_num(sk)
403 tmpdat = OPENSSL_malloc(skcontlen);
404 if (tmpdat == NULL) {
405 OPENSSL_free(derlst);
410 /* If not sorting just output each item */
412 for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
413 skitem = sk_ASN1_VALUE_value(sk, i);
414 ASN1_item_ex_i2d(&skitem, out, item, -1, iclass);
420 /* Doing sort: build up a list of each member's DER encoding */
421 for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++) {
422 skitem = sk_ASN1_VALUE_value(sk, i);
424 tder->length = ASN1_item_ex_i2d(&skitem, &p, item, -1, iclass);
425 tder->field = skitem;
429 qsort(derlst, sk_ASN1_VALUE_num(sk), sizeof(*derlst), der_cmp);
430 /* Output sorted DER encoding */
432 for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++) {
433 memcpy(p, tder->data, tder->length);
437 /* If do_sort is 2 then reorder the STACK */
439 for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++)
440 (void)sk_ASN1_VALUE_set(sk, i, tder->field);
442 OPENSSL_free(derlst);
443 OPENSSL_free(tmpdat);
447 static int asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out,
448 const ASN1_ITEM *it, int tag, int aclass)
458 * Get length of content octets and maybe find out the underlying type.
461 len = asn1_ex_i2c(pval, NULL, &utype, it);
464 * If SEQUENCE, SET or OTHER then header is included in pseudo content
465 * octets so don't include tag+length. We need to check here because the
466 * call to asn1_ex_i2c() could change utype.
468 if ((utype == V_ASN1_SEQUENCE) || (utype == V_ASN1_SET) ||
469 (utype == V_ASN1_OTHER))
474 /* -1 means omit type */
479 /* -2 return is special meaning use ndef */
485 /* If not implicitly tagged get tag from underlying type */
489 /* Output tag+length followed by content octets */
492 ASN1_put_object(out, ndef, len, tag, aclass);
493 asn1_ex_i2c(pval, *out, &utype, it);
501 return ASN1_object_size(ndef, len, tag);
505 /* Produce content octets from a structure */
507 static int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype,
510 ASN1_BOOLEAN *tbool = NULL;
514 const unsigned char *cont;
517 const ASN1_PRIMITIVE_FUNCS *pf;
519 if (pf && pf->prim_i2c)
520 return pf->prim_i2c(pval, cout, putype, it);
522 /* Should type be omitted? */
523 if ((it->itype != ASN1_ITYPE_PRIMITIVE)
524 || (it->utype != V_ASN1_BOOLEAN)) {
529 if (it->itype == ASN1_ITYPE_MSTRING) {
530 /* If MSTRING type set the underlying type */
531 strtmp = (ASN1_STRING *)*pval;
532 utype = strtmp->type;
534 } else if (it->utype == V_ASN1_ANY) {
535 /* If ANY set type and pointer to value */
537 typ = (ASN1_TYPE *)*pval;
540 pval = &typ->value.asn1_value;
546 otmp = (ASN1_OBJECT *)*pval;
549 if (cont == NULL || len == 0)
559 tbool = (ASN1_BOOLEAN *)pval;
562 if (it->utype != V_ASN1_ANY) {
564 * Default handling if value == size field then omit
566 if (*tbool && (it->size > 0))
568 if (!*tbool && !it->size)
571 c = (unsigned char)*tbool;
576 case V_ASN1_BIT_STRING:
577 return i2c_ASN1_BIT_STRING((ASN1_BIT_STRING *)*pval,
578 cout ? &cout : NULL);
581 case V_ASN1_ENUMERATED:
583 * These are all have the same content format as ASN1_INTEGER
585 return i2c_ASN1_INTEGER((ASN1_INTEGER *)*pval, cout ? &cout : NULL);
587 case V_ASN1_OCTET_STRING:
588 case V_ASN1_NUMERICSTRING:
589 case V_ASN1_PRINTABLESTRING:
590 case V_ASN1_T61STRING:
591 case V_ASN1_VIDEOTEXSTRING:
592 case V_ASN1_IA5STRING:
594 case V_ASN1_GENERALIZEDTIME:
595 case V_ASN1_GRAPHICSTRING:
596 case V_ASN1_VISIBLESTRING:
597 case V_ASN1_GENERALSTRING:
598 case V_ASN1_UNIVERSALSTRING:
599 case V_ASN1_BMPSTRING:
600 case V_ASN1_UTF8STRING:
601 case V_ASN1_SEQUENCE:
604 /* All based on ASN1_STRING and handled the same */
605 strtmp = (ASN1_STRING *)*pval;
606 /* Special handling for NDEF */
607 if ((it->size == ASN1_TFLG_NDEF)
608 && (strtmp->flags & ASN1_STRING_FLAG_NDEF)) {
613 /* Special return code */
617 len = strtmp->length;
623 memcpy(cout, cont, len);