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