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.
 
 
 
 
 
 

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