]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/asn1/tasn_enc.c
Copyright consolidation 08/10
[thirdparty/openssl.git] / crypto / asn1 / tasn_enc.c
1 /*
2 * Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
10 #include <stddef.h>
11 #include <string.h>
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"
18
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,
29 const ASN1_ITEM *it);
30
31 /*
32 * Top level i2d equivalents: the 'ndef' variant instructs the encoder to use
33 * indefinite length constructed encoding, where appropriate
34 */
35
36 int ASN1_item_ndef_i2d(ASN1_VALUE *val, unsigned char **out,
37 const ASN1_ITEM *it)
38 {
39 return asn1_item_flags_i2d(val, out, it, ASN1_TFLG_NDEF);
40 }
41
42 int ASN1_item_i2d(ASN1_VALUE *val, unsigned char **out, const ASN1_ITEM *it)
43 {
44 return asn1_item_flags_i2d(val, out, it, 0);
45 }
46
47 /*
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.
52 */
53
54 static int asn1_item_flags_i2d(ASN1_VALUE *val, unsigned char **out,
55 const ASN1_ITEM *it, int flags)
56 {
57 if (out && !*out) {
58 unsigned char *p, *buf;
59 int len;
60 len = ASN1_item_ex_i2d(&val, NULL, it, -1, flags);
61 if (len <= 0)
62 return len;
63 buf = OPENSSL_malloc(len);
64 if (buf == NULL)
65 return -1;
66 p = buf;
67 ASN1_item_ex_i2d(&val, &p, it, -1, flags);
68 *out = buf;
69 return len;
70 }
71
72 return ASN1_item_ex_i2d(&val, out, it, -1, flags);
73 }
74
75 /*
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.
78 */
79
80 int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
81 const ASN1_ITEM *it, int tag, int aclass)
82 {
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;
88
89 if ((it->itype != ASN1_ITYPE_PRIMITIVE) && !*pval)
90 return 0;
91
92 if (aux && aux->asn1_cb)
93 asn1_cb = aux->asn1_cb;
94
95 switch (it->itype) {
96
97 case ASN1_ITYPE_PRIMITIVE:
98 if (it->templates)
99 return asn1_template_ex_i2d(pval, out, it->templates,
100 tag, aclass);
101 return asn1_i2d_ex_primitive(pval, out, it, tag, aclass);
102
103 case ASN1_ITYPE_MSTRING:
104 return asn1_i2d_ex_primitive(pval, out, it, -1, aclass);
105
106 case ASN1_ITYPE_CHOICE:
107 if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it, NULL))
108 return 0;
109 i = asn1_get_choice_selector(pval, it);
110 if ((i >= 0) && (i < it->tcount)) {
111 ASN1_VALUE **pchval;
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);
116 }
117 /* Fixme: error condition if selector out of range */
118 if (asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it, NULL))
119 return 0;
120 break;
121
122 case ASN1_ITYPE_EXTERN:
123 /* If new style i2d it does all the work */
124 ef = it->funcs;
125 return ef->asn1_ex_i2d(pval, out, it, tag, aclass);
126
127 case ASN1_ITYPE_NDEF_SEQUENCE:
128 /* Use indefinite length constructed if requested */
129 if (aclass & ASN1_TFLG_NDEF)
130 ndef = 2;
131 /* fall through */
132
133 case ASN1_ITYPE_SEQUENCE:
134 i = asn1_enc_restore(&seqcontlen, out, pval, it);
135 /* An error occurred */
136 if (i < 0)
137 return 0;
138 /* We have a valid cached encoding... */
139 if (i > 0)
140 return seqcontlen;
141 /* Otherwise carry on */
142 seqcontlen = 0;
143 /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
144 if (tag == -1) {
145 tag = V_ASN1_SEQUENCE;
146 /* Retain any other flags in aclass */
147 aclass = (aclass & ~ASN1_TFLG_TAG_CLASS)
148 | V_ASN1_UNIVERSAL;
149 }
150 if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it, NULL))
151 return 0;
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);
157 if (!seqtt)
158 return 0;
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,
162 -1, aclass);
163 }
164
165 seqlen = ASN1_object_size(ndef, seqcontlen, tag);
166 if (!out)
167 return seqlen;
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);
174 if (!seqtt)
175 return 0;
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);
179 }
180 if (ndef == 2)
181 ASN1_put_eoc(out);
182 if (asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it, NULL))
183 return 0;
184 return seqlen;
185
186 default:
187 return 0;
188
189 }
190 return 0;
191 }
192
193 static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
194 const ASN1_TEMPLATE *tt, int tag, int iclass)
195 {
196 int i, ret, flags, ttag, tclass, ndef;
197 ASN1_VALUE *tval;
198 flags = tt->flags;
199
200 /*
201 * If field is embedded then val needs fixing so it is a pointer to
202 * a pointer to a field.
203 */
204 if (flags & ASN1_TFLG_EMBED) {
205 tval = (ASN1_VALUE *)pval;
206 pval = &tval;
207 }
208 /*
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
213 * levels.
214 */
215 if (flags & ASN1_TFLG_TAG_MASK) {
216 /* Error if argument and template tagging */
217 if (tag != -1)
218 /* FIXME: error code here */
219 return -1;
220 /* Get tagging from template */
221 ttag = tt->tag;
222 tclass = flags & ASN1_TFLG_TAG_CLASS;
223 } else if (tag != -1) {
224 /* No template tagging, get from arguments */
225 ttag = tag;
226 tclass = iclass & ASN1_TFLG_TAG_CLASS;
227 } else {
228 ttag = -1;
229 tclass = 0;
230 }
231 /*
232 * Remove any class mask from iflag.
233 */
234 iclass &= ~ASN1_TFLG_TAG_CLASS;
235
236 /*
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.
239 */
240
241 /* if template and arguments require ndef, use it */
242 if ((flags & ASN1_TFLG_NDEF) && (iclass & ASN1_TFLG_NDEF))
243 ndef = 2;
244 else
245 ndef = 1;
246
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;
252 ASN1_VALUE *skitem;
253
254 if (!*pval)
255 return 0;
256
257 if (flags & ASN1_TFLG_SET_OF) {
258 isset = 1;
259 /* 2 means we reorder */
260 if (flags & ASN1_TFLG_SEQUENCE_OF)
261 isset = 2;
262 } else
263 isset = 0;
264
265 /*
266 * Work out inner tag value: if EXPLICIT or no tagging use underlying
267 * type.
268 */
269 if ((ttag != -1) && !(flags & ASN1_TFLG_EXPTAG)) {
270 sktag = ttag;
271 skaclass = tclass;
272 } else {
273 skaclass = V_ASN1_UNIVERSAL;
274 if (isset)
275 sktag = V_ASN1_SET;
276 else
277 sktag = V_ASN1_SEQUENCE;
278 }
279
280 /* Determine total length of items */
281 skcontlen = 0;
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),
286 -1, iclass);
287 }
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);
292 else
293 ret = sklen;
294
295 if (!out)
296 return ret;
297
298 /* Now encode this lot... */
299 /* EXPLICIT tag */
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),
306 isset, iclass);
307 if (ndef == 2) {
308 ASN1_put_eoc(out);
309 if (flags & ASN1_TFLG_EXPTAG)
310 ASN1_put_eoc(out);
311 }
312
313 return ret;
314 }
315
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);
320 if (!i)
321 return 0;
322 /* Find length of EXPLICIT tag */
323 ret = ASN1_object_size(ndef, i, ttag);
324 if (out) {
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);
328 if (ndef == 2)
329 ASN1_put_eoc(out);
330 }
331 return ret;
332 }
333
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);
337
338 }
339
340 /* Temporary structure used to hold DER encoding of items for SET OF */
341
342 typedef struct {
343 unsigned char *data;
344 int length;
345 ASN1_VALUE *field;
346 } DER_ENC;
347
348 static int der_cmp(const void *a, const void *b)
349 {
350 const DER_ENC *d1 = a, *d2 = b;
351 int cmplen, i;
352 cmplen = (d1->length < d2->length) ? d1->length : d2->length;
353 i = memcmp(d1->data, d2->data, cmplen);
354 if (i)
355 return i;
356 return d1->length - d2->length;
357 }
358
359 /* Output the content octets of SET OF or SEQUENCE OF */
360
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)
364 {
365 int i;
366 ASN1_VALUE *skitem;
367 unsigned char *tmpdat = NULL, *p = NULL;
368 DER_ENC *derlst = NULL, *tder;
369 if (do_sort) {
370 /* Don't need to sort less than 2 items */
371 if (sk_ASN1_VALUE_num(sk) < 2)
372 do_sort = 0;
373 else {
374 derlst = OPENSSL_malloc(sk_ASN1_VALUE_num(sk)
375 * sizeof(*derlst));
376 if (derlst == NULL)
377 return 0;
378 tmpdat = OPENSSL_malloc(skcontlen);
379 if (tmpdat == NULL) {
380 OPENSSL_free(derlst);
381 return 0;
382 }
383 }
384 }
385 /* If not sorting just output each item */
386 if (!do_sort) {
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);
390 }
391 return 1;
392 }
393 p = tmpdat;
394
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);
398 tder->data = p;
399 tder->length = ASN1_item_ex_i2d(&skitem, &p, item, -1, iclass);
400 tder->field = skitem;
401 }
402
403 /* Now sort them */
404 qsort(derlst, sk_ASN1_VALUE_num(sk), sizeof(*derlst), der_cmp);
405 /* Output sorted DER encoding */
406 p = *out;
407 for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++) {
408 memcpy(p, tder->data, tder->length);
409 p += tder->length;
410 }
411 *out = p;
412 /* If do_sort is 2 then reorder the STACK */
413 if (do_sort == 2) {
414 for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++)
415 (void)sk_ASN1_VALUE_set(sk, i, tder->field);
416 }
417 OPENSSL_free(derlst);
418 OPENSSL_free(tmpdat);
419 return 1;
420 }
421
422 static int asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out,
423 const ASN1_ITEM *it, int tag, int aclass)
424 {
425 int len;
426 int utype;
427 int usetag;
428 int ndef = 0;
429
430 utype = it->utype;
431
432 /*
433 * Get length of content octets and maybe find out the underlying type.
434 */
435
436 len = asn1_ex_i2c(pval, NULL, &utype, it);
437
438 /*
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.
442 */
443 if ((utype == V_ASN1_SEQUENCE) || (utype == V_ASN1_SET) ||
444 (utype == V_ASN1_OTHER))
445 usetag = 0;
446 else
447 usetag = 1;
448
449 /* -1 means omit type */
450
451 if (len == -1)
452 return 0;
453
454 /* -2 return is special meaning use ndef */
455 if (len == -2) {
456 ndef = 2;
457 len = 0;
458 }
459
460 /* If not implicitly tagged get tag from underlying type */
461 if (tag == -1)
462 tag = utype;
463
464 /* Output tag+length followed by content octets */
465 if (out) {
466 if (usetag)
467 ASN1_put_object(out, ndef, len, tag, aclass);
468 asn1_ex_i2c(pval, *out, &utype, it);
469 if (ndef)
470 ASN1_put_eoc(out);
471 else
472 *out += len;
473 }
474
475 if (usetag)
476 return ASN1_object_size(ndef, len, tag);
477 return len;
478 }
479
480 /* Produce content octets from a structure */
481
482 static int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype,
483 const ASN1_ITEM *it)
484 {
485 ASN1_BOOLEAN *tbool = NULL;
486 ASN1_STRING *strtmp;
487 ASN1_OBJECT *otmp;
488 int utype;
489 const unsigned char *cont;
490 unsigned char c;
491 int len;
492 const ASN1_PRIMITIVE_FUNCS *pf;
493 pf = it->funcs;
494 if (pf && pf->prim_i2c)
495 return pf->prim_i2c(pval, cout, putype, it);
496
497 /* Should type be omitted? */
498 if ((it->itype != ASN1_ITYPE_PRIMITIVE)
499 || (it->utype != V_ASN1_BOOLEAN)) {
500 if (!*pval)
501 return -1;
502 }
503
504 if (it->itype == ASN1_ITYPE_MSTRING) {
505 /* If MSTRING type set the underlying type */
506 strtmp = (ASN1_STRING *)*pval;
507 utype = strtmp->type;
508 *putype = utype;
509 } else if (it->utype == V_ASN1_ANY) {
510 /* If ANY set type and pointer to value */
511 ASN1_TYPE *typ;
512 typ = (ASN1_TYPE *)*pval;
513 utype = typ->type;
514 *putype = utype;
515 pval = &typ->value.asn1_value;
516 } else
517 utype = *putype;
518
519 switch (utype) {
520 case V_ASN1_OBJECT:
521 otmp = (ASN1_OBJECT *)*pval;
522 cont = otmp->data;
523 len = otmp->length;
524 break;
525
526 case V_ASN1_NULL:
527 cont = NULL;
528 len = 0;
529 break;
530
531 case V_ASN1_BOOLEAN:
532 tbool = (ASN1_BOOLEAN *)pval;
533 if (*tbool == -1)
534 return -1;
535 if (it->utype != V_ASN1_ANY) {
536 /*
537 * Default handling if value == size field then omit
538 */
539 if (*tbool && (it->size > 0))
540 return -1;
541 if (!*tbool && !it->size)
542 return -1;
543 }
544 c = (unsigned char)*tbool;
545 cont = &c;
546 len = 1;
547 break;
548
549 case V_ASN1_BIT_STRING:
550 return i2c_ASN1_BIT_STRING((ASN1_BIT_STRING *)*pval,
551 cout ? &cout : NULL);
552
553 case V_ASN1_INTEGER:
554 case V_ASN1_ENUMERATED:
555 /*
556 * These are all have the same content format as ASN1_INTEGER
557 */
558 return i2c_ASN1_INTEGER((ASN1_INTEGER *)*pval, cout ? &cout : NULL);
559
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:
566 case V_ASN1_UTCTIME:
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:
575 case V_ASN1_SET:
576 default:
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)) {
582 if (cout) {
583 strtmp->data = cout;
584 strtmp->length = 0;
585 }
586 /* Special return code */
587 return -2;
588 }
589 cont = strtmp->data;
590 len = strtmp->length;
591
592 break;
593
594 }
595 if (cout && len)
596 memcpy(cout, cont, len);
597 return len;
598 }