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