]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/asn1/tasn_enc.c
Stop raising ERR_R_MALLOC_FAILURE in most places
[thirdparty/openssl.git] / crypto / asn1 / tasn_enc.c
CommitLineData
0f113f3e 1/*
3c2bdd7d 2 * Copyright 2000-2021 The OpenSSL Project Authors. All Rights Reserved.
9d6b1ce6 3 *
365a2d99 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
2039c421
RS
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
9d6b1ce6
DSH
8 */
9
9d6b1ce6 10#include <stddef.h>
d8770f3e 11#include <string.h>
b39fc560 12#include "internal/cryptlib.h"
9d6b1ce6
DSH
13#include <openssl/asn1.h>
14#include <openssl/asn1t.h>
15#include <openssl/objects.h>
25f2138b 16#include "crypto/asn1.h"
706457b7 17#include "asn1_local.h"
9d6b1ce6 18
9fdcc21f 19static int asn1_i2d_ex_primitive(const ASN1_VALUE **pval, unsigned char **out,
0f113f3e 20 const ASN1_ITEM *it, int tag, int aclass);
9fdcc21f
DO
21static int asn1_set_seq_out(STACK_OF(const_ASN1_VALUE) *sk,
22 unsigned char **out,
0f113f3e
MC
23 int skcontlen, const ASN1_ITEM *item,
24 int do_sort, int iclass);
9fdcc21f 25static int asn1_template_ex_i2d(const ASN1_VALUE **pval, unsigned char **out,
0f113f3e 26 const ASN1_TEMPLATE *tt, int tag, int aclass);
9fdcc21f 27static int asn1_item_flags_i2d(const ASN1_VALUE *val, unsigned char **out,
0f113f3e 28 const ASN1_ITEM *it, int flags);
9fdcc21f 29static int asn1_ex_i2c(const ASN1_VALUE **pval, unsigned char *cout, int *putype,
23dc1706 30 const ASN1_ITEM *it);
230fd6b7 31
0f113f3e
MC
32/*
33 * Top level i2d equivalents: the 'ndef' variant instructs the encoder to use
34 * indefinite length constructed encoding, where appropriate
230fd6b7
DSH
35 */
36
9fdcc21f 37int ASN1_item_ndef_i2d(const ASN1_VALUE *val, unsigned char **out,
0f113f3e
MC
38 const ASN1_ITEM *it)
39{
40 return asn1_item_flags_i2d(val, out, it, ASN1_TFLG_NDEF);
41}
9d6b1ce6 42
9fdcc21f 43int ASN1_item_i2d(const ASN1_VALUE *val, unsigned char **out, const ASN1_ITEM *it)
0f113f3e
MC
44{
45 return asn1_item_flags_i2d(val, out, it, 0);
46}
230fd6b7 47
0f113f3e
MC
48/*
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
9d6b1ce6
DSH
52 * allocated and populated with the encoding.
53 */
54
9fdcc21f 55static int asn1_item_flags_i2d(const ASN1_VALUE *val, unsigned char **out,
0f113f3e
MC
56 const ASN1_ITEM *it, int flags)
57{
12a765a5 58 if (out != NULL && *out == NULL) {
0f113f3e
MC
59 unsigned char *p, *buf;
60 int len;
cdb10bae 61
0f113f3e
MC
62 len = ASN1_item_ex_i2d(&val, NULL, it, -1, flags);
63 if (len <= 0)
64 return len;
e077455e 65 if ((buf = OPENSSL_malloc(len)) == NULL)
0f113f3e
MC
66 return -1;
67 p = buf;
68 ASN1_item_ex_i2d(&val, &p, it, -1, flags);
69 *out = buf;
70 return len;
71 }
72
73 return ASN1_item_ex_i2d(&val, out, it, -1, flags);
74}
75
76/*
77 * Encode an item, taking care of IMPLICIT tagging (if any). This function
78 * performs the normal item handling: it can be used in external types.
9d6b1ce6
DSH
79 */
80
9fdcc21f 81int ASN1_item_ex_i2d(const ASN1_VALUE **pval, unsigned char **out,
0f113f3e
MC
82 const ASN1_ITEM *it, int tag, int aclass)
83{
84 const ASN1_TEMPLATE *tt = NULL;
0f113f3e 85 int i, seqcontlen, seqlen, ndef = 1;
0f113f3e
MC
86 const ASN1_EXTERN_FUNCS *ef;
87 const ASN1_AUX *aux = it->funcs;
9fdcc21f 88 ASN1_aux_const_cb *asn1_cb = NULL;
0f113f3e 89
12a765a5 90 if ((it->itype != ASN1_ITYPE_PRIMITIVE) && *pval == NULL)
0f113f3e
MC
91 return 0;
92
9fdcc21f
DO
93 if (aux != NULL) {
94 asn1_cb = ((aux->flags & ASN1_AFLG_CONST_CB) != 0) ? aux->asn1_const_cb
95 : (ASN1_aux_const_cb *)aux->asn1_cb; /* backward compatibility */
96 }
0f113f3e
MC
97
98 switch (it->itype) {
99
100 case ASN1_ITYPE_PRIMITIVE:
101 if (it->templates)
102 return asn1_template_ex_i2d(pval, out, it->templates,
103 tag, aclass);
104 return asn1_i2d_ex_primitive(pval, out, it, tag, aclass);
0f113f3e
MC
105
106 case ASN1_ITYPE_MSTRING:
3db2c9f3
MC
107 /*
108 * It never makes sense for multi-strings to have implicit tagging, so
109 * if tag != -1, then this looks like an error in the template.
110 */
111 if (tag != -1) {
112 ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_TEMPLATE);
113 return -1;
114 }
0f113f3e
MC
115 return asn1_i2d_ex_primitive(pval, out, it, -1, aclass);
116
117 case ASN1_ITYPE_CHOICE:
3db2c9f3
MC
118 /*
119 * It never makes sense for CHOICE types to have implicit tagging, so
120 * if tag != -1, then this looks like an error in the template.
121 */
122 if (tag != -1) {
123 ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_TEMPLATE);
124 return -1;
125 }
0f113f3e
MC
126 if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it, NULL))
127 return 0;
adf7e6d1 128 i = ossl_asn1_get_choice_selector_const(pval, it);
0f113f3e 129 if ((i >= 0) && (i < it->tcount)) {
9fdcc21f 130 const ASN1_VALUE **pchval;
0f113f3e
MC
131 const ASN1_TEMPLATE *chtt;
132 chtt = it->templates + i;
adf7e6d1 133 pchval = ossl_asn1_get_const_field_ptr(pval, chtt);
0f113f3e
MC
134 return asn1_template_ex_i2d(pchval, out, chtt, -1, aclass);
135 }
136 /* Fixme: error condition if selector out of range */
137 if (asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it, NULL))
138 return 0;
139 break;
140
141 case ASN1_ITYPE_EXTERN:
142 /* If new style i2d it does all the work */
143 ef = it->funcs;
144 return ef->asn1_ex_i2d(pval, out, it, tag, aclass);
145
0f113f3e
MC
146 case ASN1_ITYPE_NDEF_SEQUENCE:
147 /* Use indefinite length constructed if requested */
148 if (aclass & ASN1_TFLG_NDEF)
149 ndef = 2;
150 /* fall through */
151
152 case ASN1_ITYPE_SEQUENCE:
adf7e6d1 153 i = ossl_asn1_enc_restore(&seqcontlen, out, pval, it);
0f113f3e
MC
154 /* An error occurred */
155 if (i < 0)
156 return 0;
157 /* We have a valid cached encoding... */
158 if (i > 0)
159 return seqcontlen;
160 /* Otherwise carry on */
161 seqcontlen = 0;
162 /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
163 if (tag == -1) {
164 tag = V_ASN1_SEQUENCE;
165 /* Retain any other flags in aclass */
166 aclass = (aclass & ~ASN1_TFLG_TAG_CLASS)
167 | V_ASN1_UNIVERSAL;
168 }
169 if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it, NULL))
170 return 0;
171 /* First work out sequence content length */
172 for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
173 const ASN1_TEMPLATE *seqtt;
9fdcc21f 174 const ASN1_VALUE **pseqval;
56f9953c 175 int tmplen;
adf7e6d1 176 seqtt = ossl_asn1_do_adb(*pval, tt, 1);
0f113f3e
MC
177 if (!seqtt)
178 return 0;
adf7e6d1 179 pseqval = ossl_asn1_get_const_field_ptr(pval, seqtt);
56f9953c
DSH
180 tmplen = asn1_template_ex_i2d(pseqval, NULL, seqtt, -1, aclass);
181 if (tmplen == -1 || (tmplen > INT_MAX - seqcontlen))
182 return -1;
183 seqcontlen += tmplen;
0f113f3e
MC
184 }
185
186 seqlen = ASN1_object_size(ndef, seqcontlen, tag);
56f9953c 187 if (!out || seqlen == -1)
0f113f3e
MC
188 return seqlen;
189 /* Output SEQUENCE header */
190 ASN1_put_object(out, ndef, seqcontlen, tag, aclass);
191 for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
192 const ASN1_TEMPLATE *seqtt;
9fdcc21f 193 const ASN1_VALUE **pseqval;
adf7e6d1 194 seqtt = ossl_asn1_do_adb(*pval, tt, 1);
0f113f3e
MC
195 if (!seqtt)
196 return 0;
adf7e6d1 197 pseqval = ossl_asn1_get_const_field_ptr(pval, seqtt);
0f113f3e
MC
198 /* FIXME: check for errors in enhanced version */
199 asn1_template_ex_i2d(pseqval, out, seqtt, -1, aclass);
200 }
201 if (ndef == 2)
202 ASN1_put_eoc(out);
203 if (asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it, NULL))
204 return 0;
205 return seqlen;
206
207 default:
208 return 0;
209
210 }
211 return 0;
212}
9d6b1ce6 213
9fdcc21f 214static int asn1_template_ex_i2d(const ASN1_VALUE **pval, unsigned char **out,
0f113f3e
MC
215 const ASN1_TEMPLATE *tt, int tag, int iclass)
216{
89947af2 217 const int flags = tt->flags;
4cd47351 218 int i, ret, ttag, tclass, ndef, len;
9fdcc21f 219 const ASN1_VALUE *tval;
de17bd5d
DSH
220
221 /*
222 * If field is embedded then val needs fixing so it is a pointer to
223 * a pointer to a field.
224 */
225 if (flags & ASN1_TFLG_EMBED) {
226 tval = (ASN1_VALUE *)pval;
227 pval = &tval;
228 }
0f113f3e
MC
229 /*
230 * Work out tag and class to use: tagging may come either from the
231 * template or the arguments, not both because this would create
232 * ambiguity. Additionally the iclass argument may contain some
233 * additional flags which should be noted and passed down to other
234 * levels.
235 */
236 if (flags & ASN1_TFLG_TAG_MASK) {
237 /* Error if argument and template tagging */
238 if (tag != -1)
239 /* FIXME: error code here */
240 return -1;
241 /* Get tagging from template */
242 ttag = tt->tag;
243 tclass = flags & ASN1_TFLG_TAG_CLASS;
244 } else if (tag != -1) {
245 /* No template tagging, get from arguments */
246 ttag = tag;
247 tclass = iclass & ASN1_TFLG_TAG_CLASS;
248 } else {
249 ttag = -1;
250 tclass = 0;
251 }
252 /*
253 * Remove any class mask from iflag.
254 */
255 iclass &= ~ASN1_TFLG_TAG_CLASS;
256
257 /*
258 * At this point 'ttag' contains the outer tag to use, 'tclass' is the
259 * class and iclass is any flags passed to this function.
260 */
261
262 /* if template and arguments require ndef, use it */
263 if ((flags & ASN1_TFLG_NDEF) && (iclass & ASN1_TFLG_NDEF))
264 ndef = 2;
265 else
266 ndef = 1;
267
268 if (flags & ASN1_TFLG_SK_MASK) {
269 /* SET OF, SEQUENCE OF */
9fdcc21f 270 STACK_OF(const_ASN1_VALUE) *sk = (STACK_OF(const_ASN1_VALUE) *)*pval;
0f113f3e
MC
271 int isset, sktag, skaclass;
272 int skcontlen, sklen;
9fdcc21f 273 const ASN1_VALUE *skitem;
0f113f3e 274
12a765a5 275 if (*pval == NULL)
0f113f3e
MC
276 return 0;
277
278 if (flags & ASN1_TFLG_SET_OF) {
279 isset = 1;
280 /* 2 means we reorder */
281 if (flags & ASN1_TFLG_SEQUENCE_OF)
282 isset = 2;
283 } else
284 isset = 0;
285
286 /*
287 * Work out inner tag value: if EXPLICIT or no tagging use underlying
288 * type.
289 */
290 if ((ttag != -1) && !(flags & ASN1_TFLG_EXPTAG)) {
291 sktag = ttag;
292 skaclass = tclass;
293 } else {
294 skaclass = V_ASN1_UNIVERSAL;
295 if (isset)
296 sktag = V_ASN1_SET;
297 else
298 sktag = V_ASN1_SEQUENCE;
299 }
300
301 /* Determine total length of items */
302 skcontlen = 0;
9fdcc21f 303 for (i = 0; i < sk_const_ASN1_VALUE_num(sk); i++) {
9fdcc21f 304 skitem = sk_const_ASN1_VALUE_value(sk, i);
4cd47351
RL
305 len = ASN1_item_ex_i2d(&skitem, NULL, ASN1_ITEM_ptr(tt->item),
306 -1, iclass);
307 if (len == -1 || (skcontlen > INT_MAX - len))
308 return -1;
309 if (len == 0 && (tt->flags & ASN1_TFLG_OPTIONAL) == 0) {
310 ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_ZERO_CONTENT);
56f9953c 311 return -1;
4cd47351
RL
312 }
313 skcontlen += len;
0f113f3e
MC
314 }
315 sklen = ASN1_object_size(ndef, skcontlen, sktag);
56f9953c
DSH
316 if (sklen == -1)
317 return -1;
0f113f3e
MC
318 /* If EXPLICIT need length of surrounding tag */
319 if (flags & ASN1_TFLG_EXPTAG)
320 ret = ASN1_object_size(ndef, sklen, ttag);
321 else
322 ret = sklen;
323
56f9953c 324 if (!out || ret == -1)
0f113f3e
MC
325 return ret;
326
327 /* Now encode this lot... */
328 /* EXPLICIT tag */
329 if (flags & ASN1_TFLG_EXPTAG)
330 ASN1_put_object(out, ndef, sklen, ttag, tclass);
331 /* SET or SEQUENCE and IMPLICIT tag */
332 ASN1_put_object(out, ndef, skcontlen, sktag, skaclass);
333 /* And the stuff itself */
334 asn1_set_seq_out(sk, out, skcontlen, ASN1_ITEM_ptr(tt->item),
335 isset, iclass);
336 if (ndef == 2) {
337 ASN1_put_eoc(out);
338 if (flags & ASN1_TFLG_EXPTAG)
339 ASN1_put_eoc(out);
340 }
341
342 return ret;
343 }
344
345 if (flags & ASN1_TFLG_EXPTAG) {
346 /* EXPLICIT tagging */
347 /* Find length of tagged item */
348 i = ASN1_item_ex_i2d(pval, NULL, ASN1_ITEM_ptr(tt->item), -1, iclass);
a773e67b
P
349 if (i == 0) {
350 if ((tt->flags & ASN1_TFLG_OPTIONAL) == 0) {
351 ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_ZERO_CONTENT);
352 return -1;
353 }
0f113f3e 354 return 0;
4cd47351 355 }
0f113f3e
MC
356 /* Find length of EXPLICIT tag */
357 ret = ASN1_object_size(ndef, i, ttag);
56f9953c 358 if (out && ret != -1) {
0f113f3e
MC
359 /* Output tag and item */
360 ASN1_put_object(out, ndef, i, ttag, tclass);
361 ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item), -1, iclass);
362 if (ndef == 2)
363 ASN1_put_eoc(out);
364 }
365 return ret;
366 }
367
368 /* Either normal or IMPLICIT tagging: combine class and flags */
4cd47351
RL
369 len = ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item),
370 ttag, tclass | iclass);
371 if (len == 0 && (tt->flags & ASN1_TFLG_OPTIONAL) == 0) {
372 ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_ZERO_CONTENT);
373 return -1;
374 }
375 return len;
9d6b1ce6
DSH
376}
377
378/* Temporary structure used to hold DER encoding of items for SET OF */
379
0f113f3e
MC
380typedef struct {
381 unsigned char *data;
382 int length;
9fdcc21f 383 const ASN1_VALUE *field;
9d6b1ce6
DSH
384} DER_ENC;
385
386static int der_cmp(const void *a, const void *b)
0f113f3e
MC
387{
388 const DER_ENC *d1 = a, *d2 = b;
389 int cmplen, i;
390 cmplen = (d1->length < d2->length) ? d1->length : d2->length;
391 i = memcmp(d1->data, d2->data, cmplen);
392 if (i)
393 return i;
394 return d1->length - d2->length;
395}
9d6b1ce6
DSH
396
397/* Output the content octets of SET OF or SEQUENCE OF */
398
9fdcc21f
DO
399static int asn1_set_seq_out(STACK_OF(const_ASN1_VALUE) *sk,
400 unsigned char **out,
0f113f3e
MC
401 int skcontlen, const ASN1_ITEM *item,
402 int do_sort, int iclass)
403{
89947af2 404 int i, ret = 0;
9fdcc21f 405 const ASN1_VALUE *skitem;
0f113f3e
MC
406 unsigned char *tmpdat = NULL, *p = NULL;
407 DER_ENC *derlst = NULL, *tder;
89947af2 408
0f113f3e
MC
409 if (do_sort) {
410 /* Don't need to sort less than 2 items */
9fdcc21f 411 if (sk_const_ASN1_VALUE_num(sk) < 2)
0f113f3e
MC
412 do_sort = 0;
413 else {
9fdcc21f 414 derlst = OPENSSL_malloc(sk_const_ASN1_VALUE_num(sk)
0f113f3e 415 * sizeof(*derlst));
e077455e 416 if (derlst == NULL)
0f113f3e
MC
417 return 0;
418 tmpdat = OPENSSL_malloc(skcontlen);
e077455e 419 if (tmpdat == NULL)
89947af2 420 goto err;
0f113f3e
MC
421 }
422 }
423 /* If not sorting just output each item */
424 if (!do_sort) {
9fdcc21f
DO
425 for (i = 0; i < sk_const_ASN1_VALUE_num(sk); i++) {
426 skitem = sk_const_ASN1_VALUE_value(sk, i);
0f113f3e
MC
427 ASN1_item_ex_i2d(&skitem, out, item, -1, iclass);
428 }
429 return 1;
430 }
431 p = tmpdat;
432
433 /* Doing sort: build up a list of each member's DER encoding */
9fdcc21f
DO
434 for (i = 0, tder = derlst; i < sk_const_ASN1_VALUE_num(sk); i++, tder++) {
435 skitem = sk_const_ASN1_VALUE_value(sk, i);
0f113f3e
MC
436 tder->data = p;
437 tder->length = ASN1_item_ex_i2d(&skitem, &p, item, -1, iclass);
438 tder->field = skitem;
439 }
440
441 /* Now sort them */
9fdcc21f 442 qsort(derlst, sk_const_ASN1_VALUE_num(sk), sizeof(*derlst), der_cmp);
0f113f3e
MC
443 /* Output sorted DER encoding */
444 p = *out;
9fdcc21f 445 for (i = 0, tder = derlst; i < sk_const_ASN1_VALUE_num(sk); i++, tder++) {
0f113f3e
MC
446 memcpy(p, tder->data, tder->length);
447 p += tder->length;
448 }
449 *out = p;
450 /* If do_sort is 2 then reorder the STACK */
451 if (do_sort == 2) {
9fdcc21f
DO
452 for (i = 0, tder = derlst; i < sk_const_ASN1_VALUE_num(sk); i++, tder++)
453 (void)sk_const_ASN1_VALUE_set(sk, i, tder->field);
0f113f3e 454 }
89947af2
F
455 ret = 1;
456err:
0f113f3e
MC
457 OPENSSL_free(derlst);
458 OPENSSL_free(tmpdat);
89947af2 459 return ret;
0f113f3e 460}
9d6b1ce6 461
9fdcc21f 462static int asn1_i2d_ex_primitive(const ASN1_VALUE **pval, unsigned char **out,
0f113f3e
MC
463 const ASN1_ITEM *it, int tag, int aclass)
464{
465 int len;
466 int utype;
467 int usetag;
468 int ndef = 0;
469
470 utype = it->utype;
471
472 /*
473 * Get length of content octets and maybe find out the underlying type.
474 */
475
476 len = asn1_ex_i2c(pval, NULL, &utype, it);
477
478 /*
479 * If SEQUENCE, SET or OTHER then header is included in pseudo content
480 * octets so don't include tag+length. We need to check here because the
481 * call to asn1_ex_i2c() could change utype.
482 */
483 if ((utype == V_ASN1_SEQUENCE) || (utype == V_ASN1_SET) ||
484 (utype == V_ASN1_OTHER))
485 usetag = 0;
486 else
487 usetag = 1;
488
489 /* -1 means omit type */
490
491 if (len == -1)
492 return 0;
493
494 /* -2 return is special meaning use ndef */
495 if (len == -2) {
496 ndef = 2;
497 len = 0;
498 }
499
500 /* If not implicitly tagged get tag from underlying type */
501 if (tag == -1)
502 tag = utype;
503
504 /* Output tag+length followed by content octets */
505 if (out) {
506 if (usetag)
507 ASN1_put_object(out, ndef, len, tag, aclass);
508 asn1_ex_i2c(pval, *out, &utype, it);
509 if (ndef)
510 ASN1_put_eoc(out);
511 else
512 *out += len;
513 }
514
515 if (usetag)
516 return ASN1_object_size(ndef, len, tag);
517 return len;
518}
9d6b1ce6
DSH
519
520/* Produce content octets from a structure */
521
9fdcc21f 522static int asn1_ex_i2c(const ASN1_VALUE **pval, unsigned char *cout, int *putype,
23dc1706 523 const ASN1_ITEM *it)
0f113f3e
MC
524{
525 ASN1_BOOLEAN *tbool = NULL;
526 ASN1_STRING *strtmp;
527 ASN1_OBJECT *otmp;
528 int utype;
529 const unsigned char *cont;
530 unsigned char c;
531 int len;
532 const ASN1_PRIMITIVE_FUNCS *pf;
533 pf = it->funcs;
534 if (pf && pf->prim_i2c)
535 return pf->prim_i2c(pval, cout, putype, it);
536
537 /* Should type be omitted? */
538 if ((it->itype != ASN1_ITYPE_PRIMITIVE)
539 || (it->utype != V_ASN1_BOOLEAN)) {
12a765a5 540 if (*pval == NULL)
0f113f3e
MC
541 return -1;
542 }
543
544 if (it->itype == ASN1_ITYPE_MSTRING) {
545 /* If MSTRING type set the underlying type */
546 strtmp = (ASN1_STRING *)*pval;
547 utype = strtmp->type;
548 *putype = utype;
549 } else if (it->utype == V_ASN1_ANY) {
550 /* If ANY set type and pointer to value */
551 ASN1_TYPE *typ;
552 typ = (ASN1_TYPE *)*pval;
553 utype = typ->type;
554 *putype = utype;
9fdcc21f 555 pval = (const ASN1_VALUE **)&typ->value.asn1_value; /* actually is const */
0f113f3e
MC
556 } else
557 utype = *putype;
558
559 switch (utype) {
560 case V_ASN1_OBJECT:
561 otmp = (ASN1_OBJECT *)*pval;
562 cont = otmp->data;
563 len = otmp->length;
53c9818e
MC
564 if (cont == NULL || len == 0)
565 return -1;
0f113f3e
MC
566 break;
567
568 case V_ASN1_NULL:
569 cont = NULL;
570 len = 0;
571 break;
572
573 case V_ASN1_BOOLEAN:
574 tbool = (ASN1_BOOLEAN *)pval;
575 if (*tbool == -1)
576 return -1;
577 if (it->utype != V_ASN1_ANY) {
578 /*
579 * Default handling if value == size field then omit
580 */
581 if (*tbool && (it->size > 0))
582 return -1;
583 if (!*tbool && !it->size)
584 return -1;
585 }
586 c = (unsigned char)*tbool;
587 cont = &c;
588 len = 1;
589 break;
590
591 case V_ASN1_BIT_STRING:
adf7e6d1
SL
592 return ossl_i2c_ASN1_BIT_STRING((ASN1_BIT_STRING *)*pval,
593 cout ? &cout : NULL);
0f113f3e
MC
594
595 case V_ASN1_INTEGER:
0f113f3e 596 case V_ASN1_ENUMERATED:
0f113f3e
MC
597 /*
598 * These are all have the same content format as ASN1_INTEGER
599 */
adf7e6d1 600 return ossl_i2c_ASN1_INTEGER((ASN1_INTEGER *)*pval, cout ? &cout : NULL);
0f113f3e
MC
601
602 case V_ASN1_OCTET_STRING:
603 case V_ASN1_NUMERICSTRING:
604 case V_ASN1_PRINTABLESTRING:
605 case V_ASN1_T61STRING:
606 case V_ASN1_VIDEOTEXSTRING:
607 case V_ASN1_IA5STRING:
608 case V_ASN1_UTCTIME:
609 case V_ASN1_GENERALIZEDTIME:
610 case V_ASN1_GRAPHICSTRING:
611 case V_ASN1_VISIBLESTRING:
612 case V_ASN1_GENERALSTRING:
613 case V_ASN1_UNIVERSALSTRING:
614 case V_ASN1_BMPSTRING:
615 case V_ASN1_UTF8STRING:
616 case V_ASN1_SEQUENCE:
617 case V_ASN1_SET:
618 default:
619 /* All based on ASN1_STRING and handled the same */
620 strtmp = (ASN1_STRING *)*pval;
621 /* Special handling for NDEF */
622 if ((it->size == ASN1_TFLG_NDEF)
623 && (strtmp->flags & ASN1_STRING_FLAG_NDEF)) {
624 if (cout) {
625 strtmp->data = cout;
626 strtmp->length = 0;
627 }
628 /* Special return code */
629 return -2;
630 }
631 cont = strtmp->data;
632 len = strtmp->length;
633
634 break;
635
636 }
637 if (cout && len)
638 memcpy(cout, cont, len);
639 return len;
640}