You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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