]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/asn1/asn1_gen.c
threads_pthread.c: change inline to ossl_inline
[thirdparty/openssl.git] / crypto / asn1 / asn1_gen.c
1 /*
2 * Copyright 2002-2023 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
10 #include <openssl/asn1.h>
11 #include <openssl/x509v3.h>
12 #include "internal/cryptlib.h"
13 #include "crypto/asn1.h"
14
15 #define ASN1_GEN_FLAG 0x10000
16 #define ASN1_GEN_FLAG_IMP (ASN1_GEN_FLAG|1)
17 #define ASN1_GEN_FLAG_EXP (ASN1_GEN_FLAG|2)
18 #define ASN1_GEN_FLAG_TAG (ASN1_GEN_FLAG|3)
19 #define ASN1_GEN_FLAG_BITWRAP (ASN1_GEN_FLAG|4)
20 #define ASN1_GEN_FLAG_OCTWRAP (ASN1_GEN_FLAG|5)
21 #define ASN1_GEN_FLAG_SEQWRAP (ASN1_GEN_FLAG|6)
22 #define ASN1_GEN_FLAG_SETWRAP (ASN1_GEN_FLAG|7)
23 #define ASN1_GEN_FLAG_FORMAT (ASN1_GEN_FLAG|8)
24
25 #define ASN1_GEN_STR(str,val) {str, sizeof(str) - 1, val}
26
27 #define ASN1_FLAG_EXP_MAX 20
28 /* Maximum number of nested sequences */
29 #define ASN1_GEN_SEQ_MAX_DEPTH 50
30
31 /* Input formats */
32
33 /* ASCII: default */
34 #define ASN1_GEN_FORMAT_ASCII 1
35 /* UTF8 */
36 #define ASN1_GEN_FORMAT_UTF8 2
37 /* Hex */
38 #define ASN1_GEN_FORMAT_HEX 3
39 /* List of bits */
40 #define ASN1_GEN_FORMAT_BITLIST 4
41
42 struct tag_name_st {
43 const char *strnam;
44 int len;
45 int tag;
46 };
47
48 typedef struct {
49 int exp_tag;
50 int exp_class;
51 int exp_constructed;
52 int exp_pad;
53 long exp_len;
54 } tag_exp_type;
55
56 typedef struct {
57 int imp_tag;
58 int imp_class;
59 int utype;
60 int format;
61 const char *str;
62 tag_exp_type exp_list[ASN1_FLAG_EXP_MAX];
63 int exp_count;
64 } tag_exp_arg;
65
66 static ASN1_TYPE *generate_v3(const char *str, X509V3_CTX *cnf, int depth,
67 int *perr);
68 static int bitstr_cb(const char *elem, int len, void *bitstr);
69 static int asn1_cb(const char *elem, int len, void *bitstr);
70 static int append_exp(tag_exp_arg *arg, int exp_tag, int exp_class,
71 int exp_constructed, int exp_pad, int imp_ok);
72 static int parse_tagging(const char *vstart, int vlen, int *ptag,
73 int *pclass);
74 static ASN1_TYPE *asn1_multi(int utype, const char *section, X509V3_CTX *cnf,
75 int depth, int *perr);
76 static ASN1_TYPE *asn1_str2type(const char *str, int format, int utype);
77 static int asn1_str2tag(const char *tagstr, int len);
78
79 ASN1_TYPE *ASN1_generate_nconf(const char *str, CONF *nconf)
80 {
81 X509V3_CTX cnf;
82
83 if (!nconf)
84 return ASN1_generate_v3(str, NULL);
85
86 X509V3_set_nconf(&cnf, nconf);
87 return ASN1_generate_v3(str, &cnf);
88 }
89
90 ASN1_TYPE *ASN1_generate_v3(const char *str, X509V3_CTX *cnf)
91 {
92 int err = 0;
93 ASN1_TYPE *ret = generate_v3(str, cnf, 0, &err);
94 if (err)
95 ERR_raise(ERR_LIB_ASN1, err);
96 return ret;
97 }
98
99 static ASN1_TYPE *generate_v3(const char *str, X509V3_CTX *cnf, int depth,
100 int *perr)
101 {
102 ASN1_TYPE *ret;
103 tag_exp_arg asn1_tags;
104 tag_exp_type *etmp;
105
106 int i, len;
107
108 unsigned char *orig_der = NULL, *new_der = NULL;
109 const unsigned char *cpy_start;
110 unsigned char *p;
111 const unsigned char *cp;
112 int cpy_len;
113 long hdr_len = 0;
114 int hdr_constructed = 0, hdr_tag, hdr_class;
115 int r;
116
117 asn1_tags.imp_tag = -1;
118 asn1_tags.imp_class = -1;
119 asn1_tags.format = ASN1_GEN_FORMAT_ASCII;
120 asn1_tags.exp_count = 0;
121 if (CONF_parse_list(str, ',', 1, asn1_cb, &asn1_tags) != 0) {
122 *perr = ASN1_R_UNKNOWN_TAG;
123 return NULL;
124 }
125
126 if ((asn1_tags.utype == V_ASN1_SEQUENCE)
127 || (asn1_tags.utype == V_ASN1_SET)) {
128 if (!cnf) {
129 *perr = ASN1_R_SEQUENCE_OR_SET_NEEDS_CONFIG;
130 return NULL;
131 }
132 if (depth >= ASN1_GEN_SEQ_MAX_DEPTH) {
133 *perr = ASN1_R_ILLEGAL_NESTED_TAGGING;
134 return NULL;
135 }
136 ret = asn1_multi(asn1_tags.utype, asn1_tags.str, cnf, depth, perr);
137 } else
138 ret = asn1_str2type(asn1_tags.str, asn1_tags.format, asn1_tags.utype);
139
140 if (!ret)
141 return NULL;
142
143 /* If no tagging return base type */
144 if ((asn1_tags.imp_tag == -1) && (asn1_tags.exp_count == 0))
145 return ret;
146
147 /* Generate the encoding */
148 cpy_len = i2d_ASN1_TYPE(ret, &orig_der);
149 ASN1_TYPE_free(ret);
150 ret = NULL;
151 /* Set point to start copying for modified encoding */
152 cpy_start = orig_der;
153
154 /* Do we need IMPLICIT tagging? */
155 if (asn1_tags.imp_tag != -1) {
156 /* If IMPLICIT we will replace the underlying tag */
157 /* Skip existing tag+len */
158 r = ASN1_get_object(&cpy_start, &hdr_len, &hdr_tag, &hdr_class,
159 cpy_len);
160 if (r & 0x80)
161 goto err;
162 /* Update copy length */
163 cpy_len -= cpy_start - orig_der;
164 /*
165 * For IMPLICIT tagging the length should match the original length
166 * and constructed flag should be consistent.
167 */
168 if (r & 0x1) {
169 /* Indefinite length constructed */
170 hdr_constructed = 2;
171 hdr_len = 0;
172 } else
173 /* Just retain constructed flag */
174 hdr_constructed = r & V_ASN1_CONSTRUCTED;
175 /*
176 * Work out new length with IMPLICIT tag: ignore constructed because
177 * it will mess up if indefinite length
178 */
179 len = ASN1_object_size(0, hdr_len, asn1_tags.imp_tag);
180 } else
181 len = cpy_len;
182
183 /* Work out length in any EXPLICIT, starting from end */
184
185 for (i = 0, etmp = asn1_tags.exp_list + asn1_tags.exp_count - 1;
186 i < asn1_tags.exp_count; i++, etmp--) {
187 /* Content length: number of content octets + any padding */
188 len += etmp->exp_pad;
189 etmp->exp_len = len;
190 /* Total object length: length including new header */
191 len = ASN1_object_size(0, len, etmp->exp_tag);
192 }
193
194 /* Allocate buffer for new encoding */
195
196 new_der = OPENSSL_malloc(len);
197 if (new_der == NULL)
198 goto err;
199
200 /* Generate tagged encoding */
201
202 p = new_der;
203
204 /* Output explicit tags first */
205
206 for (i = 0, etmp = asn1_tags.exp_list; i < asn1_tags.exp_count;
207 i++, etmp++) {
208 ASN1_put_object(&p, etmp->exp_constructed, etmp->exp_len,
209 etmp->exp_tag, etmp->exp_class);
210 if (etmp->exp_pad)
211 *p++ = 0;
212 }
213
214 /* If IMPLICIT, output tag */
215
216 if (asn1_tags.imp_tag != -1) {
217 if (asn1_tags.imp_class == V_ASN1_UNIVERSAL
218 && (asn1_tags.imp_tag == V_ASN1_SEQUENCE
219 || asn1_tags.imp_tag == V_ASN1_SET))
220 hdr_constructed = V_ASN1_CONSTRUCTED;
221 ASN1_put_object(&p, hdr_constructed, hdr_len,
222 asn1_tags.imp_tag, asn1_tags.imp_class);
223 }
224
225 /* Copy across original encoding */
226 memcpy(p, cpy_start, cpy_len);
227
228 cp = new_der;
229
230 /* Obtain new ASN1_TYPE structure */
231 ret = d2i_ASN1_TYPE(NULL, &cp, len);
232
233 err:
234 OPENSSL_free(orig_der);
235 OPENSSL_free(new_der);
236
237 return ret;
238
239 }
240
241 static int asn1_cb(const char *elem, int len, void *bitstr)
242 {
243 tag_exp_arg *arg = bitstr;
244 int i;
245 int utype;
246 int vlen = 0;
247 const char *p, *vstart = NULL;
248
249 int tmp_tag, tmp_class;
250
251 if (elem == NULL)
252 return -1;
253
254 for (i = 0, p = elem; i < len; p++, i++) {
255 /* Look for the ':' in name value pairs */
256 if (*p == ':') {
257 vstart = p + 1;
258 vlen = len - (vstart - elem);
259 len = p - elem;
260 break;
261 }
262 }
263
264 utype = asn1_str2tag(elem, len);
265
266 if (utype == -1) {
267 ERR_raise_data(ERR_LIB_ASN1, ASN1_R_UNKNOWN_TAG, "tag=%s", elem);
268 return -1;
269 }
270
271 /* If this is not a modifier mark end of string and exit */
272 if (!(utype & ASN1_GEN_FLAG)) {
273 arg->utype = utype;
274 arg->str = vstart;
275 /* If no value and not end of string, error */
276 if (!vstart && elem[len]) {
277 ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_VALUE);
278 return -1;
279 }
280 return 0;
281 }
282
283 switch (utype) {
284
285 case ASN1_GEN_FLAG_IMP:
286 /* Check for illegal multiple IMPLICIT tagging */
287 if (arg->imp_tag != -1) {
288 ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_NESTED_TAGGING);
289 return -1;
290 }
291 if (!parse_tagging(vstart, vlen, &arg->imp_tag, &arg->imp_class))
292 return -1;
293 break;
294
295 case ASN1_GEN_FLAG_EXP:
296
297 if (!parse_tagging(vstart, vlen, &tmp_tag, &tmp_class))
298 return -1;
299 if (!append_exp(arg, tmp_tag, tmp_class, 1, 0, 0))
300 return -1;
301 break;
302
303 case ASN1_GEN_FLAG_SEQWRAP:
304 if (!append_exp(arg, V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL, 1, 0, 1))
305 return -1;
306 break;
307
308 case ASN1_GEN_FLAG_SETWRAP:
309 if (!append_exp(arg, V_ASN1_SET, V_ASN1_UNIVERSAL, 1, 0, 1))
310 return -1;
311 break;
312
313 case ASN1_GEN_FLAG_BITWRAP:
314 if (!append_exp(arg, V_ASN1_BIT_STRING, V_ASN1_UNIVERSAL, 0, 1, 1))
315 return -1;
316 break;
317
318 case ASN1_GEN_FLAG_OCTWRAP:
319 if (!append_exp(arg, V_ASN1_OCTET_STRING, V_ASN1_UNIVERSAL, 0, 0, 1))
320 return -1;
321 break;
322
323 case ASN1_GEN_FLAG_FORMAT:
324 if (!vstart) {
325 ERR_raise(ERR_LIB_ASN1, ASN1_R_UNKNOWN_FORMAT);
326 return -1;
327 }
328 if (HAS_PREFIX(vstart, "ASCII"))
329 arg->format = ASN1_GEN_FORMAT_ASCII;
330 else if (HAS_PREFIX(vstart, "UTF8"))
331 arg->format = ASN1_GEN_FORMAT_UTF8;
332 else if (HAS_PREFIX(vstart, "HEX"))
333 arg->format = ASN1_GEN_FORMAT_HEX;
334 else if (HAS_PREFIX(vstart, "BITLIST"))
335 arg->format = ASN1_GEN_FORMAT_BITLIST;
336 else {
337 ERR_raise(ERR_LIB_ASN1, ASN1_R_UNKNOWN_FORMAT);
338 return -1;
339 }
340 break;
341
342 }
343
344 return 1;
345
346 }
347
348 static int parse_tagging(const char *vstart, int vlen, int *ptag, int *pclass)
349 {
350 long tag_num;
351 char *eptr;
352 if (!vstart)
353 return 0;
354 tag_num = strtoul(vstart, &eptr, 10);
355 /* Check we haven't gone past max length: should be impossible */
356 if (eptr && *eptr && (eptr > vstart + vlen))
357 return 0;
358 if (tag_num < 0) {
359 ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_NUMBER);
360 return 0;
361 }
362 *ptag = tag_num;
363 /* If we have non numeric characters, parse them */
364 if (eptr)
365 vlen -= eptr - vstart;
366 else
367 vlen = 0;
368 if (vlen) {
369 switch (*eptr) {
370
371 case 'U':
372 *pclass = V_ASN1_UNIVERSAL;
373 break;
374
375 case 'A':
376 *pclass = V_ASN1_APPLICATION;
377 break;
378
379 case 'P':
380 *pclass = V_ASN1_PRIVATE;
381 break;
382
383 case 'C':
384 *pclass = V_ASN1_CONTEXT_SPECIFIC;
385 break;
386
387 default:
388 ERR_raise_data(ERR_LIB_ASN1, ASN1_R_INVALID_MODIFIER,
389 "Char=%c", *eptr);
390 return 0;
391
392 }
393 } else
394 *pclass = V_ASN1_CONTEXT_SPECIFIC;
395
396 return 1;
397
398 }
399
400 /* Handle multiple types: SET and SEQUENCE */
401
402 static ASN1_TYPE *asn1_multi(int utype, const char *section, X509V3_CTX *cnf,
403 int depth, int *perr)
404 {
405 ASN1_TYPE *ret = NULL;
406 STACK_OF(ASN1_TYPE) *sk = NULL;
407 STACK_OF(CONF_VALUE) *sect = NULL;
408 unsigned char *der = NULL;
409 int derlen;
410 int i;
411 sk = sk_ASN1_TYPE_new_null();
412 if (!sk)
413 goto bad;
414 if (section) {
415 if (!cnf)
416 goto bad;
417 sect = X509V3_get_section(cnf, (char *)section);
418 if (!sect)
419 goto bad;
420 for (i = 0; i < sk_CONF_VALUE_num(sect); i++) {
421 ASN1_TYPE *typ =
422 generate_v3(sk_CONF_VALUE_value(sect, i)->value, cnf,
423 depth + 1, perr);
424 if (!typ)
425 goto bad;
426 if (!sk_ASN1_TYPE_push(sk, typ))
427 goto bad;
428 }
429 }
430
431 /*
432 * Now we has a STACK of the components, convert to the correct form
433 */
434
435 if (utype == V_ASN1_SET)
436 derlen = i2d_ASN1_SET_ANY(sk, &der);
437 else
438 derlen = i2d_ASN1_SEQUENCE_ANY(sk, &der);
439
440 if (derlen < 0)
441 goto bad;
442 if ((ret = ASN1_TYPE_new()) == NULL)
443 goto bad;
444 if ((ret->value.asn1_string = ASN1_STRING_type_new(utype)) == NULL)
445 goto bad;
446
447 ret->type = utype;
448 ret->value.asn1_string->data = der;
449 ret->value.asn1_string->length = derlen;
450
451 der = NULL;
452
453 bad:
454
455 OPENSSL_free(der);
456
457 sk_ASN1_TYPE_pop_free(sk, ASN1_TYPE_free);
458 X509V3_section_free(cnf, sect);
459
460 return ret;
461 }
462
463 static int append_exp(tag_exp_arg *arg, int exp_tag, int exp_class,
464 int exp_constructed, int exp_pad, int imp_ok)
465 {
466 tag_exp_type *exp_tmp;
467 /* Can only have IMPLICIT if permitted */
468 if ((arg->imp_tag != -1) && !imp_ok) {
469 ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_IMPLICIT_TAG);
470 return 0;
471 }
472
473 if (arg->exp_count == ASN1_FLAG_EXP_MAX) {
474 ERR_raise(ERR_LIB_ASN1, ASN1_R_DEPTH_EXCEEDED);
475 return 0;
476 }
477
478 exp_tmp = &arg->exp_list[arg->exp_count++];
479
480 /*
481 * If IMPLICIT set tag to implicit value then reset implicit tag since it
482 * has been used.
483 */
484 if (arg->imp_tag != -1) {
485 exp_tmp->exp_tag = arg->imp_tag;
486 exp_tmp->exp_class = arg->imp_class;
487 arg->imp_tag = -1;
488 arg->imp_class = -1;
489 } else {
490 exp_tmp->exp_tag = exp_tag;
491 exp_tmp->exp_class = exp_class;
492 }
493 exp_tmp->exp_constructed = exp_constructed;
494 exp_tmp->exp_pad = exp_pad;
495
496 return 1;
497 }
498
499 static int asn1_str2tag(const char *tagstr, int len)
500 {
501 unsigned int i;
502 static const struct tag_name_st *tntmp, tnst[] = {
503 ASN1_GEN_STR("BOOL", V_ASN1_BOOLEAN),
504 ASN1_GEN_STR("BOOLEAN", V_ASN1_BOOLEAN),
505 ASN1_GEN_STR("NULL", V_ASN1_NULL),
506 ASN1_GEN_STR("INT", V_ASN1_INTEGER),
507 ASN1_GEN_STR("INTEGER", V_ASN1_INTEGER),
508 ASN1_GEN_STR("ENUM", V_ASN1_ENUMERATED),
509 ASN1_GEN_STR("ENUMERATED", V_ASN1_ENUMERATED),
510 ASN1_GEN_STR("OID", V_ASN1_OBJECT),
511 ASN1_GEN_STR("OBJECT", V_ASN1_OBJECT),
512 ASN1_GEN_STR("UTCTIME", V_ASN1_UTCTIME),
513 ASN1_GEN_STR("UTC", V_ASN1_UTCTIME),
514 ASN1_GEN_STR("GENERALIZEDTIME", V_ASN1_GENERALIZEDTIME),
515 ASN1_GEN_STR("GENTIME", V_ASN1_GENERALIZEDTIME),
516 ASN1_GEN_STR("OCT", V_ASN1_OCTET_STRING),
517 ASN1_GEN_STR("OCTETSTRING", V_ASN1_OCTET_STRING),
518 ASN1_GEN_STR("BITSTR", V_ASN1_BIT_STRING),
519 ASN1_GEN_STR("BITSTRING", V_ASN1_BIT_STRING),
520 ASN1_GEN_STR("UNIVERSALSTRING", V_ASN1_UNIVERSALSTRING),
521 ASN1_GEN_STR("UNIV", V_ASN1_UNIVERSALSTRING),
522 ASN1_GEN_STR("IA5", V_ASN1_IA5STRING),
523 ASN1_GEN_STR("IA5STRING", V_ASN1_IA5STRING),
524 ASN1_GEN_STR("UTF8", V_ASN1_UTF8STRING),
525 ASN1_GEN_STR("UTF8String", V_ASN1_UTF8STRING),
526 ASN1_GEN_STR("BMP", V_ASN1_BMPSTRING),
527 ASN1_GEN_STR("BMPSTRING", V_ASN1_BMPSTRING),
528 ASN1_GEN_STR("VISIBLESTRING", V_ASN1_VISIBLESTRING),
529 ASN1_GEN_STR("VISIBLE", V_ASN1_VISIBLESTRING),
530 ASN1_GEN_STR("PRINTABLESTRING", V_ASN1_PRINTABLESTRING),
531 ASN1_GEN_STR("PRINTABLE", V_ASN1_PRINTABLESTRING),
532 ASN1_GEN_STR("T61", V_ASN1_T61STRING),
533 ASN1_GEN_STR("T61STRING", V_ASN1_T61STRING),
534 ASN1_GEN_STR("TELETEXSTRING", V_ASN1_T61STRING),
535 ASN1_GEN_STR("GeneralString", V_ASN1_GENERALSTRING),
536 ASN1_GEN_STR("GENSTR", V_ASN1_GENERALSTRING),
537 ASN1_GEN_STR("NUMERIC", V_ASN1_NUMERICSTRING),
538 ASN1_GEN_STR("NUMERICSTRING", V_ASN1_NUMERICSTRING),
539
540 /* Special cases */
541 ASN1_GEN_STR("SEQUENCE", V_ASN1_SEQUENCE),
542 ASN1_GEN_STR("SEQ", V_ASN1_SEQUENCE),
543 ASN1_GEN_STR("SET", V_ASN1_SET),
544 /* type modifiers */
545 /* Explicit tag */
546 ASN1_GEN_STR("EXP", ASN1_GEN_FLAG_EXP),
547 ASN1_GEN_STR("EXPLICIT", ASN1_GEN_FLAG_EXP),
548 /* Implicit tag */
549 ASN1_GEN_STR("IMP", ASN1_GEN_FLAG_IMP),
550 ASN1_GEN_STR("IMPLICIT", ASN1_GEN_FLAG_IMP),
551 /* OCTET STRING wrapper */
552 ASN1_GEN_STR("OCTWRAP", ASN1_GEN_FLAG_OCTWRAP),
553 /* SEQUENCE wrapper */
554 ASN1_GEN_STR("SEQWRAP", ASN1_GEN_FLAG_SEQWRAP),
555 /* SET wrapper */
556 ASN1_GEN_STR("SETWRAP", ASN1_GEN_FLAG_SETWRAP),
557 /* BIT STRING wrapper */
558 ASN1_GEN_STR("BITWRAP", ASN1_GEN_FLAG_BITWRAP),
559 ASN1_GEN_STR("FORM", ASN1_GEN_FLAG_FORMAT),
560 ASN1_GEN_STR("FORMAT", ASN1_GEN_FLAG_FORMAT),
561 };
562
563 if (len == -1)
564 len = strlen(tagstr);
565
566 tntmp = tnst;
567 for (i = 0; i < OSSL_NELEM(tnst); i++, tntmp++) {
568 if ((len == tntmp->len)
569 && (OPENSSL_strncasecmp(tntmp->strnam, tagstr, len) == 0))
570 return tntmp->tag;
571 }
572
573 return -1;
574 }
575
576 static ASN1_TYPE *asn1_str2type(const char *str, int format, int utype)
577 {
578 ASN1_TYPE *atmp = NULL;
579 CONF_VALUE vtmp;
580 unsigned char *rdata;
581 long rdlen;
582 int no_unused = 1;
583
584 if ((atmp = ASN1_TYPE_new()) == NULL) {
585 ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
586 return NULL;
587 }
588
589 if (!str)
590 str = "";
591
592 switch (utype) {
593
594 case V_ASN1_NULL:
595 if (str && *str) {
596 ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_NULL_VALUE);
597 goto bad_form;
598 }
599 break;
600
601 case V_ASN1_BOOLEAN:
602 if (format != ASN1_GEN_FORMAT_ASCII) {
603 ERR_raise(ERR_LIB_ASN1, ASN1_R_NOT_ASCII_FORMAT);
604 goto bad_form;
605 }
606 vtmp.name = NULL;
607 vtmp.section = NULL;
608 vtmp.value = (char *)str;
609 if (!X509V3_get_value_bool(&vtmp, &atmp->value.boolean)) {
610 ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_BOOLEAN);
611 goto bad_str;
612 }
613 break;
614
615 case V_ASN1_INTEGER:
616 case V_ASN1_ENUMERATED:
617 if (format != ASN1_GEN_FORMAT_ASCII) {
618 ERR_raise(ERR_LIB_ASN1, ASN1_R_INTEGER_NOT_ASCII_FORMAT);
619 goto bad_form;
620 }
621 if ((atmp->value.integer
622 = s2i_ASN1_INTEGER(NULL, str)) == NULL) {
623 ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_INTEGER);
624 goto bad_str;
625 }
626 break;
627
628 case V_ASN1_OBJECT:
629 if (format != ASN1_GEN_FORMAT_ASCII) {
630 ERR_raise(ERR_LIB_ASN1, ASN1_R_OBJECT_NOT_ASCII_FORMAT);
631 goto bad_form;
632 }
633 if ((atmp->value.object = OBJ_txt2obj(str, 0)) == NULL) {
634 ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_OBJECT);
635 goto bad_str;
636 }
637 break;
638
639 case V_ASN1_UTCTIME:
640 case V_ASN1_GENERALIZEDTIME:
641 if (format != ASN1_GEN_FORMAT_ASCII) {
642 ERR_raise(ERR_LIB_ASN1, ASN1_R_TIME_NOT_ASCII_FORMAT);
643 goto bad_form;
644 }
645 if ((atmp->value.asn1_string = ASN1_STRING_new()) == NULL) {
646 ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
647 goto bad_str;
648 }
649 if (!ASN1_STRING_set(atmp->value.asn1_string, str, -1)) {
650 ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
651 goto bad_str;
652 }
653 atmp->value.asn1_string->type = utype;
654 if (!ASN1_TIME_check(atmp->value.asn1_string)) {
655 ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_TIME_VALUE);
656 goto bad_str;
657 }
658
659 break;
660
661 case V_ASN1_BMPSTRING:
662 case V_ASN1_PRINTABLESTRING:
663 case V_ASN1_IA5STRING:
664 case V_ASN1_T61STRING:
665 case V_ASN1_UTF8STRING:
666 case V_ASN1_VISIBLESTRING:
667 case V_ASN1_UNIVERSALSTRING:
668 case V_ASN1_GENERALSTRING:
669 case V_ASN1_NUMERICSTRING:
670 if (format == ASN1_GEN_FORMAT_ASCII)
671 format = MBSTRING_ASC;
672 else if (format == ASN1_GEN_FORMAT_UTF8)
673 format = MBSTRING_UTF8;
674 else {
675 ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_FORMAT);
676 goto bad_form;
677 }
678
679 if (ASN1_mbstring_copy(&atmp->value.asn1_string, (unsigned char *)str,
680 -1, format, ASN1_tag2bit(utype)) <= 0) {
681 ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
682 goto bad_str;
683 }
684
685 break;
686
687 case V_ASN1_BIT_STRING:
688 case V_ASN1_OCTET_STRING:
689 if ((atmp->value.asn1_string = ASN1_STRING_new()) == NULL) {
690 ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
691 goto bad_form;
692 }
693
694 if (format == ASN1_GEN_FORMAT_HEX) {
695 if ((rdata = OPENSSL_hexstr2buf(str, &rdlen)) == NULL) {
696 ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_HEX);
697 goto bad_str;
698 }
699 atmp->value.asn1_string->data = rdata;
700 atmp->value.asn1_string->length = rdlen;
701 atmp->value.asn1_string->type = utype;
702 } else if (format == ASN1_GEN_FORMAT_ASCII) {
703 if (!ASN1_STRING_set(atmp->value.asn1_string, str, -1)) {
704 ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
705 goto bad_str;
706 }
707 } else if ((format == ASN1_GEN_FORMAT_BITLIST)
708 && (utype == V_ASN1_BIT_STRING)) {
709 if (!CONF_parse_list
710 (str, ',', 1, bitstr_cb, atmp->value.bit_string)) {
711 ERR_raise(ERR_LIB_ASN1, ASN1_R_LIST_ERROR);
712 goto bad_str;
713 }
714 no_unused = 0;
715
716 } else {
717 ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_BITSTRING_FORMAT);
718 goto bad_form;
719 }
720
721 if ((utype == V_ASN1_BIT_STRING) && no_unused)
722 ossl_asn1_string_set_bits_left(atmp->value.asn1_string, 0);
723
724 break;
725
726 default:
727 ERR_raise(ERR_LIB_ASN1, ASN1_R_UNSUPPORTED_TYPE);
728 goto bad_str;
729 }
730
731 atmp->type = utype;
732 return atmp;
733
734 bad_str:
735 ERR_add_error_data(2, "string=", str);
736 bad_form:
737
738 ASN1_TYPE_free(atmp);
739 return NULL;
740
741 }
742
743 static int bitstr_cb(const char *elem, int len, void *bitstr)
744 {
745 long bitnum;
746 char *eptr;
747 if (!elem)
748 return 0;
749 bitnum = strtoul(elem, &eptr, 10);
750 if (eptr && *eptr && (eptr != elem + len))
751 return 0;
752 if (bitnum < 0) {
753 ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_NUMBER);
754 return 0;
755 }
756 if (!ASN1_BIT_STRING_set_bit(bitstr, bitnum, 1)) {
757 ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
758 return 0;
759 }
760 return 1;
761 }
762
763 static int mask_cb(const char *elem, int len, void *arg)
764 {
765 unsigned long *pmask = arg, tmpmask;
766 int tag;
767 if (elem == NULL)
768 return 0;
769 if (len == 3 && HAS_PREFIX(elem, "DIR")) {
770 *pmask |= B_ASN1_DIRECTORYSTRING;
771 return 1;
772 }
773 tag = asn1_str2tag(elem, len);
774 if (!tag || (tag & ASN1_GEN_FLAG))
775 return 0;
776 tmpmask = ASN1_tag2bit(tag);
777 if (!tmpmask)
778 return 0;
779 *pmask |= tmpmask;
780 return 1;
781 }
782
783 int ASN1_str2mask(const char *str, unsigned long *pmask)
784 {
785 *pmask = 0;
786 return CONF_parse_list(str, '|', 1, mask_cb, pmask);
787 }