asn1_gen.c 24 KB

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