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