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

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