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