Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

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