asn1_gen.c 20 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, *typ = 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. 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. typ = NULL;
  395. }
  396. }
  397. /* Now we has a STACK of the components, convert to the correct form */
  398. if (utype == V_ASN1_SET)
  399. is_set = 1;
  400. else
  401. is_set = 0;
  402. derlen = i2d_ASN1_SET_OF_ASN1_TYPE(sk, NULL, i2d_ASN1_TYPE, utype,
  403. V_ASN1_UNIVERSAL, is_set);
  404. der = OPENSSL_malloc(derlen);
  405. p = der;
  406. i2d_ASN1_SET_OF_ASN1_TYPE(sk, &p, i2d_ASN1_TYPE, utype,
  407. V_ASN1_UNIVERSAL, is_set);
  408. if (!(ret = ASN1_TYPE_new()))
  409. goto bad;
  410. if (!(ret->value.asn1_string = ASN1_STRING_type_new(utype)))
  411. goto bad;
  412. ret->type = utype;
  413. ret->value.asn1_string->data = der;
  414. ret->value.asn1_string->length = derlen;
  415. der = NULL;
  416. bad:
  417. if (der)
  418. OPENSSL_free(der);
  419. if (sk)
  420. sk_ASN1_TYPE_pop_free(sk, ASN1_TYPE_free);
  421. if (typ)
  422. ASN1_TYPE_free(typ);
  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, int exp_constructed, int exp_pad, int imp_ok)
  428. {
  429. tag_exp_type *exp_tmp;
  430. /* Can only have IMPLICIT if permitted */
  431. if ((arg->imp_tag != -1) && !imp_ok)
  432. {
  433. ASN1err(ASN1_F_APPEND_EXP, ASN1_R_ILLEGAL_IMPLICIT_TAG);
  434. return 0;
  435. }
  436. if (arg->exp_count == ASN1_FLAG_EXP_MAX)
  437. {
  438. ASN1err(ASN1_F_APPEND_EXP, ASN1_R_DEPTH_EXCEEDED);
  439. return 0;
  440. }
  441. exp_tmp = &arg->exp_list[arg->exp_count++];
  442. /* If IMPLICIT set tag to implicit value then
  443. * reset implicit tag since it has been used.
  444. */
  445. if (arg->imp_tag != -1)
  446. {
  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. }
  452. else
  453. {
  454. exp_tmp->exp_tag = exp_tag;
  455. exp_tmp->exp_class = exp_class;
  456. }
  457. exp_tmp->exp_constructed = exp_constructed;
  458. exp_tmp->exp_pad = exp_pad;
  459. return 1;
  460. }
  461. static int asn1_str2tag(const char *tagstr, int len)
  462. {
  463. unsigned int i;
  464. static struct tag_name_st *tntmp, tnst [] = {
  465. ASN1_GEN_STR("BOOL", V_ASN1_BOOLEAN),
  466. ASN1_GEN_STR("BOOLEAN", V_ASN1_BOOLEAN),
  467. ASN1_GEN_STR("NULL", V_ASN1_NULL),
  468. ASN1_GEN_STR("INT", V_ASN1_INTEGER),
  469. ASN1_GEN_STR("INTEGER", V_ASN1_INTEGER),
  470. ASN1_GEN_STR("ENUM", V_ASN1_ENUMERATED),
  471. ASN1_GEN_STR("ENUMERATED", V_ASN1_ENUMERATED),
  472. ASN1_GEN_STR("OID", V_ASN1_OBJECT),
  473. ASN1_GEN_STR("OBJECT", V_ASN1_OBJECT),
  474. ASN1_GEN_STR("UTCTIME", V_ASN1_UTCTIME),
  475. ASN1_GEN_STR("UTC", V_ASN1_UTCTIME),
  476. ASN1_GEN_STR("GENERALIZEDTIME", V_ASN1_GENERALIZEDTIME),
  477. ASN1_GEN_STR("GENTIME", V_ASN1_GENERALIZEDTIME),
  478. ASN1_GEN_STR("OCT", V_ASN1_OCTET_STRING),
  479. ASN1_GEN_STR("OCTETSTRING", V_ASN1_OCTET_STRING),
  480. ASN1_GEN_STR("BITSTR", V_ASN1_BIT_STRING),
  481. ASN1_GEN_STR("BITSTRING", V_ASN1_BIT_STRING),
  482. ASN1_GEN_STR("UNIVERSALSTRING", V_ASN1_UNIVERSALSTRING),
  483. ASN1_GEN_STR("UNIV", V_ASN1_UNIVERSALSTRING),
  484. ASN1_GEN_STR("IA5", V_ASN1_IA5STRING),
  485. ASN1_GEN_STR("IA5STRING", V_ASN1_IA5STRING),
  486. ASN1_GEN_STR("UTF8", V_ASN1_UTF8STRING),
  487. ASN1_GEN_STR("UTF8String", V_ASN1_UTF8STRING),
  488. ASN1_GEN_STR("BMP", V_ASN1_BMPSTRING),
  489. ASN1_GEN_STR("BMPSTRING", V_ASN1_BMPSTRING),
  490. ASN1_GEN_STR("VISIBLESTRING", V_ASN1_VISIBLESTRING),
  491. ASN1_GEN_STR("VISIBLE", V_ASN1_VISIBLESTRING),
  492. ASN1_GEN_STR("PRINTABLESTRING", V_ASN1_PRINTABLESTRING),
  493. ASN1_GEN_STR("PRINTABLE", V_ASN1_PRINTABLESTRING),
  494. ASN1_GEN_STR("T61", V_ASN1_T61STRING),
  495. ASN1_GEN_STR("T61STRING", V_ASN1_T61STRING),
  496. ASN1_GEN_STR("TELETEXSTRING", V_ASN1_T61STRING),
  497. ASN1_GEN_STR("GeneralString", V_ASN1_GENERALSTRING),
  498. ASN1_GEN_STR("GENSTR", V_ASN1_GENERALSTRING),
  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. {
  526. if ((len == tntmp->len) && !strncmp(tntmp->strnam, tagstr, len))
  527. return tntmp->tag;
  528. }
  529. return -1;
  530. }
  531. static ASN1_TYPE *asn1_str2type(const char *str, int format, int utype)
  532. {
  533. ASN1_TYPE *atmp = NULL;
  534. CONF_VALUE vtmp;
  535. unsigned char *rdata;
  536. long rdlen;
  537. int no_unused = 1;
  538. if (!(atmp = ASN1_TYPE_new()))
  539. {
  540. ASN1err(ASN1_F_ASN1_STR2TYPE, ERR_R_MALLOC_FAILURE);
  541. return NULL;
  542. }
  543. if (!str)
  544. str = "";
  545. switch(utype)
  546. {
  547. case V_ASN1_NULL:
  548. if (str && *str)
  549. {
  550. ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_NULL_VALUE);
  551. goto bad_form;
  552. }
  553. break;
  554. case V_ASN1_BOOLEAN:
  555. if (format != ASN1_GEN_FORMAT_ASCII)
  556. {
  557. ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_NOT_ASCII_FORMAT);
  558. goto bad_form;
  559. }
  560. vtmp.name = NULL;
  561. vtmp.section = NULL;
  562. vtmp.value = (char *)str;
  563. if (!X509V3_get_value_bool(&vtmp, &atmp->value.boolean))
  564. {
  565. ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_BOOLEAN);
  566. goto bad_str;
  567. }
  568. break;
  569. case V_ASN1_INTEGER:
  570. case V_ASN1_ENUMERATED:
  571. if (format != ASN1_GEN_FORMAT_ASCII)
  572. {
  573. ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_INTEGER_NOT_ASCII_FORMAT);
  574. goto bad_form;
  575. }
  576. if (!(atmp->value.integer = s2i_ASN1_INTEGER(NULL, (char *)str)))
  577. {
  578. ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_INTEGER);
  579. goto bad_str;
  580. }
  581. break;
  582. case V_ASN1_OBJECT:
  583. if (format != ASN1_GEN_FORMAT_ASCII)
  584. {
  585. ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_OBJECT_NOT_ASCII_FORMAT);
  586. goto bad_form;
  587. }
  588. if (!(atmp->value.object = OBJ_txt2obj(str, 0)))
  589. {
  590. ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_OBJECT);
  591. goto bad_str;
  592. }
  593. break;
  594. case V_ASN1_UTCTIME:
  595. case V_ASN1_GENERALIZEDTIME:
  596. if (format != ASN1_GEN_FORMAT_ASCII)
  597. {
  598. ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_TIME_NOT_ASCII_FORMAT);
  599. goto bad_form;
  600. }
  601. if (!(atmp->value.asn1_string = ASN1_STRING_new()))
  602. {
  603. ASN1err(ASN1_F_ASN1_STR2TYPE, ERR_R_MALLOC_FAILURE);
  604. goto bad_str;
  605. }
  606. if (!ASN1_STRING_set(atmp->value.asn1_string, str, -1))
  607. {
  608. ASN1err(ASN1_F_ASN1_STR2TYPE, ERR_R_MALLOC_FAILURE);
  609. goto bad_str;
  610. }
  611. atmp->value.asn1_string->type = utype;
  612. if (!ASN1_TIME_check(atmp->value.asn1_string))
  613. {
  614. ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_TIME_VALUE);
  615. goto bad_str;
  616. }
  617. break;
  618. case V_ASN1_BMPSTRING:
  619. case V_ASN1_PRINTABLESTRING:
  620. case V_ASN1_IA5STRING:
  621. case V_ASN1_T61STRING:
  622. case V_ASN1_UTF8STRING:
  623. case V_ASN1_VISIBLESTRING:
  624. case V_ASN1_UNIVERSALSTRING:
  625. case V_ASN1_GENERALSTRING:
  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. }