asn1_gen.c 21 KB

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