2 * Copyright 2000-2016 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 "internal/asn1_int.h"
17 #include "asn1_locl.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;
60 len = ASN1_item_ex_i2d(&val, NULL, it, -1, flags);
63 buf = OPENSSL_malloc(len);
67 ASN1_item_ex_i2d(&val, &p, it, -1, flags);
72 return ASN1_item_ex_i2d(&val, out, it, -1, flags);
76 * Encode an item, taking care of IMPLICIT tagging (if any). This function
77 * performs the normal item handling: it can be used in external types.
80 int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
81 const ASN1_ITEM *it, int tag, int aclass)
83 const ASN1_TEMPLATE *tt = NULL;
84 int i, seqcontlen, seqlen, ndef = 1;
85 const ASN1_EXTERN_FUNCS *ef;
86 const ASN1_AUX *aux = it->funcs;
87 ASN1_aux_cb *asn1_cb = 0;
89 if ((it->itype != ASN1_ITYPE_PRIMITIVE) && !*pval)
92 if (aux && aux->asn1_cb)
93 asn1_cb = aux->asn1_cb;
97 case ASN1_ITYPE_PRIMITIVE:
99 return asn1_template_ex_i2d(pval, out, it->templates,
101 return asn1_i2d_ex_primitive(pval, out, it, tag, aclass);
103 case ASN1_ITYPE_MSTRING:
104 return asn1_i2d_ex_primitive(pval, out, it, -1, aclass);
106 case ASN1_ITYPE_CHOICE:
107 if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it, NULL))
109 i = asn1_get_choice_selector(pval, it);
110 if ((i >= 0) && (i < it->tcount)) {
112 const ASN1_TEMPLATE *chtt;
113 chtt = it->templates + i;
114 pchval = asn1_get_field_ptr(pval, chtt);
115 return asn1_template_ex_i2d(pchval, out, chtt, -1, aclass);
117 /* Fixme: error condition if selector out of range */
118 if (asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it, NULL))
122 case ASN1_ITYPE_EXTERN:
123 /* If new style i2d it does all the work */
125 return ef->asn1_ex_i2d(pval, out, it, tag, aclass);
127 case ASN1_ITYPE_NDEF_SEQUENCE:
128 /* Use indefinite length constructed if requested */
129 if (aclass & ASN1_TFLG_NDEF)
133 case ASN1_ITYPE_SEQUENCE:
134 i = asn1_enc_restore(&seqcontlen, out, pval, it);
135 /* An error occurred */
138 /* We have a valid cached encoding... */
141 /* Otherwise carry on */
143 /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
145 tag = V_ASN1_SEQUENCE;
146 /* Retain any other flags in aclass */
147 aclass = (aclass & ~ASN1_TFLG_TAG_CLASS)
150 if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it, NULL))
152 /* First work out sequence content length */
153 for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
154 const ASN1_TEMPLATE *seqtt;
155 ASN1_VALUE **pseqval;
156 seqtt = asn1_do_adb(pval, tt, 1);
159 pseqval = asn1_get_field_ptr(pval, seqtt);
160 /* FIXME: check for errors in enhanced version */
161 seqcontlen += asn1_template_ex_i2d(pseqval, NULL, seqtt,
165 seqlen = ASN1_object_size(ndef, seqcontlen, tag);
168 /* Output SEQUENCE header */
169 ASN1_put_object(out, ndef, seqcontlen, tag, aclass);
170 for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
171 const ASN1_TEMPLATE *seqtt;
172 ASN1_VALUE **pseqval;
173 seqtt = asn1_do_adb(pval, tt, 1);
176 pseqval = asn1_get_field_ptr(pval, seqtt);
177 /* FIXME: check for errors in enhanced version */
178 asn1_template_ex_i2d(pseqval, out, seqtt, -1, aclass);
182 if (asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it, NULL))
193 static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
194 const ASN1_TEMPLATE *tt, int tag, int iclass)
196 int i, ret, flags, ttag, tclass, ndef;
201 * If field is embedded then val needs fixing so it is a pointer to
202 * a pointer to a field.
204 if (flags & ASN1_TFLG_EMBED) {
205 tval = (ASN1_VALUE *)pval;
209 * Work out tag and class to use: tagging may come either from the
210 * template or the arguments, not both because this would create
211 * ambiguity. Additionally the iclass argument may contain some
212 * additional flags which should be noted and passed down to other
215 if (flags & ASN1_TFLG_TAG_MASK) {
216 /* Error if argument and template tagging */
218 /* FIXME: error code here */
220 /* Get tagging from template */
222 tclass = flags & ASN1_TFLG_TAG_CLASS;
223 } else if (tag != -1) {
224 /* No template tagging, get from arguments */
226 tclass = iclass & ASN1_TFLG_TAG_CLASS;
232 * Remove any class mask from iflag.
234 iclass &= ~ASN1_TFLG_TAG_CLASS;
237 * At this point 'ttag' contains the outer tag to use, 'tclass' is the
238 * class and iclass is any flags passed to this function.
241 /* if template and arguments require ndef, use it */
242 if ((flags & ASN1_TFLG_NDEF) && (iclass & ASN1_TFLG_NDEF))
247 if (flags & ASN1_TFLG_SK_MASK) {
248 /* SET OF, SEQUENCE OF */
249 STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval;
250 int isset, sktag, skaclass;
251 int skcontlen, sklen;
257 if (flags & ASN1_TFLG_SET_OF) {
259 /* 2 means we reorder */
260 if (flags & ASN1_TFLG_SEQUENCE_OF)
266 * Work out inner tag value: if EXPLICIT or no tagging use underlying
269 if ((ttag != -1) && !(flags & ASN1_TFLG_EXPTAG)) {
273 skaclass = V_ASN1_UNIVERSAL;
277 sktag = V_ASN1_SEQUENCE;
280 /* Determine total length of items */
282 for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
283 skitem = sk_ASN1_VALUE_value(sk, i);
284 skcontlen += ASN1_item_ex_i2d(&skitem, NULL,
285 ASN1_ITEM_ptr(tt->item),
288 sklen = ASN1_object_size(ndef, skcontlen, sktag);
289 /* If EXPLICIT need length of surrounding tag */
290 if (flags & ASN1_TFLG_EXPTAG)
291 ret = ASN1_object_size(ndef, sklen, ttag);
298 /* Now encode this lot... */
300 if (flags & ASN1_TFLG_EXPTAG)
301 ASN1_put_object(out, ndef, sklen, ttag, tclass);
302 /* SET or SEQUENCE and IMPLICIT tag */
303 ASN1_put_object(out, ndef, skcontlen, sktag, skaclass);
304 /* And the stuff itself */
305 asn1_set_seq_out(sk, out, skcontlen, ASN1_ITEM_ptr(tt->item),
309 if (flags & ASN1_TFLG_EXPTAG)
316 if (flags & ASN1_TFLG_EXPTAG) {
317 /* EXPLICIT tagging */
318 /* Find length of tagged item */
319 i = ASN1_item_ex_i2d(pval, NULL, ASN1_ITEM_ptr(tt->item), -1, iclass);
322 /* Find length of EXPLICIT tag */
323 ret = ASN1_object_size(ndef, i, ttag);
325 /* Output tag and item */
326 ASN1_put_object(out, ndef, i, ttag, tclass);
327 ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item), -1, iclass);
334 /* Either normal or IMPLICIT tagging: combine class and flags */
335 return ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item),
336 ttag, tclass | iclass);
340 /* Temporary structure used to hold DER encoding of items for SET OF */
348 static int der_cmp(const void *a, const void *b)
350 const DER_ENC *d1 = a, *d2 = b;
352 cmplen = (d1->length < d2->length) ? d1->length : d2->length;
353 i = memcmp(d1->data, d2->data, cmplen);
356 return d1->length - d2->length;
359 /* Output the content octets of SET OF or SEQUENCE OF */
361 static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *sk, unsigned char **out,
362 int skcontlen, const ASN1_ITEM *item,
363 int do_sort, int iclass)
367 unsigned char *tmpdat = NULL, *p = NULL;
368 DER_ENC *derlst = NULL, *tder;
370 /* Don't need to sort less than 2 items */
371 if (sk_ASN1_VALUE_num(sk) < 2)
374 derlst = OPENSSL_malloc(sk_ASN1_VALUE_num(sk)
378 tmpdat = OPENSSL_malloc(skcontlen);
379 if (tmpdat == NULL) {
380 OPENSSL_free(derlst);
385 /* If not sorting just output each item */
387 for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
388 skitem = sk_ASN1_VALUE_value(sk, i);
389 ASN1_item_ex_i2d(&skitem, out, item, -1, iclass);
395 /* Doing sort: build up a list of each member's DER encoding */
396 for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++) {
397 skitem = sk_ASN1_VALUE_value(sk, i);
399 tder->length = ASN1_item_ex_i2d(&skitem, &p, item, -1, iclass);
400 tder->field = skitem;
404 qsort(derlst, sk_ASN1_VALUE_num(sk), sizeof(*derlst), der_cmp);
405 /* Output sorted DER encoding */
407 for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++) {
408 memcpy(p, tder->data, tder->length);
412 /* If do_sort is 2 then reorder the STACK */
414 for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++)
415 (void)sk_ASN1_VALUE_set(sk, i, tder->field);
417 OPENSSL_free(derlst);
418 OPENSSL_free(tmpdat);
422 static int asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out,
423 const ASN1_ITEM *it, int tag, int aclass)
433 * Get length of content octets and maybe find out the underlying type.
436 len = asn1_ex_i2c(pval, NULL, &utype, it);
439 * If SEQUENCE, SET or OTHER then header is included in pseudo content
440 * octets so don't include tag+length. We need to check here because the
441 * call to asn1_ex_i2c() could change utype.
443 if ((utype == V_ASN1_SEQUENCE) || (utype == V_ASN1_SET) ||
444 (utype == V_ASN1_OTHER))
449 /* -1 means omit type */
454 /* -2 return is special meaning use ndef */
460 /* If not implicitly tagged get tag from underlying type */
464 /* Output tag+length followed by content octets */
467 ASN1_put_object(out, ndef, len, tag, aclass);
468 asn1_ex_i2c(pval, *out, &utype, it);
476 return ASN1_object_size(ndef, len, tag);
480 /* Produce content octets from a structure */
482 static int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype,
485 ASN1_BOOLEAN *tbool = NULL;
489 const unsigned char *cont;
492 const ASN1_PRIMITIVE_FUNCS *pf;
494 if (pf && pf->prim_i2c)
495 return pf->prim_i2c(pval, cout, putype, it);
497 /* Should type be omitted? */
498 if ((it->itype != ASN1_ITYPE_PRIMITIVE)
499 || (it->utype != V_ASN1_BOOLEAN)) {
504 if (it->itype == ASN1_ITYPE_MSTRING) {
505 /* If MSTRING type set the underlying type */
506 strtmp = (ASN1_STRING *)*pval;
507 utype = strtmp->type;
509 } else if (it->utype == V_ASN1_ANY) {
510 /* If ANY set type and pointer to value */
512 typ = (ASN1_TYPE *)*pval;
515 pval = &typ->value.asn1_value;
521 otmp = (ASN1_OBJECT *)*pval;
532 tbool = (ASN1_BOOLEAN *)pval;
535 if (it->utype != V_ASN1_ANY) {
537 * Default handling if value == size field then omit
539 if (*tbool && (it->size > 0))
541 if (!*tbool && !it->size)
544 c = (unsigned char)*tbool;
549 case V_ASN1_BIT_STRING:
550 return i2c_ASN1_BIT_STRING((ASN1_BIT_STRING *)*pval,
551 cout ? &cout : NULL);
554 case V_ASN1_ENUMERATED:
556 * These are all have the same content format as ASN1_INTEGER
558 return i2c_ASN1_INTEGER((ASN1_INTEGER *)*pval, cout ? &cout : NULL);
560 case V_ASN1_OCTET_STRING:
561 case V_ASN1_NUMERICSTRING:
562 case V_ASN1_PRINTABLESTRING:
563 case V_ASN1_T61STRING:
564 case V_ASN1_VIDEOTEXSTRING:
565 case V_ASN1_IA5STRING:
567 case V_ASN1_GENERALIZEDTIME:
568 case V_ASN1_GRAPHICSTRING:
569 case V_ASN1_VISIBLESTRING:
570 case V_ASN1_GENERALSTRING:
571 case V_ASN1_UNIVERSALSTRING:
572 case V_ASN1_BMPSTRING:
573 case V_ASN1_UTF8STRING:
574 case V_ASN1_SEQUENCE:
577 /* All based on ASN1_STRING and handled the same */
578 strtmp = (ASN1_STRING *)*pval;
579 /* Special handling for NDEF */
580 if ((it->size == ASN1_TFLG_NDEF)
581 && (strtmp->flags & ASN1_STRING_FLAG_NDEF)) {
586 /* Special return code */
590 len = strtmp->length;
596 memcpy(cout, cont, len);