Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

874 Zeilen
22 KiB

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