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