asn1_gen.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  1. /* asn1_gen.c */
  2. /* Written by Dr Stephen N Henson (steve@openssl.org) 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. if (!new_der)
  204. goto err;
  205. /* Generate tagged encoding */
  206. p = new_der;
  207. /* Output explicit tags first */
  208. for (i = 0, etmp = asn1_tags.exp_list; i < asn1_tags.exp_count; i++, etmp++)
  209. {
  210. ASN1_put_object(&p, etmp->exp_constructed, etmp->exp_len,
  211. etmp->exp_tag, etmp->exp_class);
  212. if (etmp->exp_pad)
  213. *p++ = 0;
  214. }
  215. /* If IMPLICIT, output tag */
  216. if (asn1_tags.imp_tag != -1)
  217. {
  218. if (asn1_tags.imp_class == V_ASN1_UNIVERSAL
  219. && (asn1_tags.imp_tag == V_ASN1_SEQUENCE
  220. || asn1_tags.imp_tag == V_ASN1_SET) )
  221. hdr_constructed = V_ASN1_CONSTRUCTED;
  222. ASN1_put_object(&p, hdr_constructed, hdr_len,
  223. asn1_tags.imp_tag, asn1_tags.imp_class);
  224. }
  225. /* Copy across original encoding */
  226. memcpy(p, cpy_start, cpy_len);
  227. cp = new_der;
  228. /* Obtain new ASN1_TYPE structure */
  229. ret = d2i_ASN1_TYPE(NULL, &cp, len);
  230. err:
  231. if (orig_der)
  232. OPENSSL_free(orig_der);
  233. if (new_der)
  234. OPENSSL_free(new_der);
  235. return ret;
  236. }
  237. static int asn1_cb(const char *elem, int len, void *bitstr)
  238. {
  239. tag_exp_arg *arg = bitstr;
  240. int i;
  241. int utype;
  242. int vlen = 0;
  243. const char *p, *vstart = NULL;
  244. int tmp_tag, tmp_class;
  245. for(i = 0, p = elem; i < len; p++, i++)
  246. {
  247. /* Look for the ':' in name value pairs */
  248. if (*p == ':')
  249. {
  250. vstart = p + 1;
  251. vlen = len - (vstart - elem);
  252. len = p - elem;
  253. break;
  254. }
  255. }
  256. utype = asn1_str2tag(elem, len);
  257. if (utype == -1)
  258. {
  259. ASN1err(ASN1_F_ASN1_CB, ASN1_R_UNKNOWN_TAG);
  260. ERR_add_error_data(2, "tag=", elem);
  261. return -1;
  262. }
  263. /* If this is not a modifier mark end of string and exit */
  264. if (!(utype & ASN1_GEN_FLAG))
  265. {
  266. arg->utype = utype;
  267. arg->str = vstart;
  268. /* If no value and not end of string, error */
  269. if (!vstart && elem[len])
  270. {
  271. ASN1err(ASN1_F_ASN1_CB, ASN1_R_MISSING_VALUE);
  272. return -1;
  273. }
  274. return 0;
  275. }
  276. switch(utype)
  277. {
  278. case ASN1_GEN_FLAG_IMP:
  279. /* Check for illegal multiple IMPLICIT tagging */
  280. if (arg->imp_tag != -1)
  281. {
  282. ASN1err(ASN1_F_ASN1_CB, ASN1_R_ILLEGAL_NESTED_TAGGING);
  283. return -1;
  284. }
  285. if (!parse_tagging(vstart, vlen, &arg->imp_tag, &arg->imp_class))
  286. return -1;
  287. break;
  288. case ASN1_GEN_FLAG_EXP:
  289. if (!parse_tagging(vstart, vlen, &tmp_tag, &tmp_class))
  290. return -1;
  291. if (!append_exp(arg, tmp_tag, tmp_class, 1, 0, 0))
  292. return -1;
  293. break;
  294. case ASN1_GEN_FLAG_SEQWRAP:
  295. if (!append_exp(arg, V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL, 1, 0, 1))
  296. return -1;
  297. break;
  298. case ASN1_GEN_FLAG_SETWRAP:
  299. if (!append_exp(arg, V_ASN1_SET, V_ASN1_UNIVERSAL, 1, 0, 1))
  300. return -1;
  301. break;
  302. case ASN1_GEN_FLAG_BITWRAP:
  303. if (!append_exp(arg, V_ASN1_BIT_STRING, V_ASN1_UNIVERSAL, 0, 1, 1))
  304. return -1;
  305. break;
  306. case ASN1_GEN_FLAG_OCTWRAP:
  307. if (!append_exp(arg, V_ASN1_OCTET_STRING, V_ASN1_UNIVERSAL, 0, 0, 1))
  308. return -1;
  309. break;
  310. case ASN1_GEN_FLAG_FORMAT:
  311. if (!strncmp(vstart, "ASCII", 5))
  312. arg->format = ASN1_GEN_FORMAT_ASCII;
  313. else if (!strncmp(vstart, "UTF8", 4))
  314. arg->format = ASN1_GEN_FORMAT_UTF8;
  315. else if (!strncmp(vstart, "HEX", 3))
  316. arg->format = ASN1_GEN_FORMAT_HEX;
  317. else if (!strncmp(vstart, "BITLIST", 3))
  318. arg->format = ASN1_GEN_FORMAT_BITLIST;
  319. else
  320. {
  321. ASN1err(ASN1_F_ASN1_CB, ASN1_R_UNKOWN_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. {
  341. ASN1err(ASN1_F_PARSE_TAGGING, ASN1_R_INVALID_NUMBER);
  342. return 0;
  343. }
  344. *ptag = tag_num;
  345. /* If we have non numeric characters, parse them */
  346. if (eptr)
  347. vlen -= eptr - vstart;
  348. else
  349. vlen = 0;
  350. if (vlen)
  351. {
  352. switch (*eptr)
  353. {
  354. case 'U':
  355. *pclass = V_ASN1_UNIVERSAL;
  356. break;
  357. case 'A':
  358. *pclass = V_ASN1_APPLICATION;
  359. break;
  360. case 'P':
  361. *pclass = V_ASN1_PRIVATE;
  362. break;
  363. case 'C':
  364. *pclass = V_ASN1_CONTEXT_SPECIFIC;
  365. break;
  366. default:
  367. erch[0] = *eptr;
  368. erch[1] = 0;
  369. ASN1err(ASN1_F_PARSE_TAGGING, ASN1_R_INVALID_MODIFIER);
  370. ERR_add_error_data(2, "Char=", erch);
  371. return 0;
  372. break;
  373. }
  374. }
  375. else
  376. *pclass = V_ASN1_CONTEXT_SPECIFIC;
  377. return 1;
  378. }
  379. /* Handle multiple types: SET and SEQUENCE */
  380. static ASN1_TYPE *asn1_multi(int utype, const char *section, X509V3_CTX *cnf)
  381. {
  382. ASN1_TYPE *ret = NULL;
  383. STACK_OF(ASN1_TYPE) *sk = NULL;
  384. STACK_OF(CONF_VALUE) *sect = NULL;
  385. unsigned char *der = NULL;
  386. int derlen;
  387. int i;
  388. sk = sk_ASN1_TYPE_new_null();
  389. if (!sk)
  390. goto bad;
  391. if (section)
  392. {
  393. if (!cnf)
  394. goto bad;
  395. sect = X509V3_get_section(cnf, (char *)section);
  396. if (!sect)
  397. goto bad;
  398. for (i = 0; i < sk_CONF_VALUE_num(sect); i++)
  399. {
  400. ASN1_TYPE *typ = ASN1_generate_v3(sk_CONF_VALUE_value(sect, i)->value, cnf);
  401. if (!typ)
  402. goto bad;
  403. if (!sk_ASN1_TYPE_push(sk, typ))
  404. goto bad;
  405. }
  406. }
  407. /* Now we has a STACK of the components, convert to the correct form */
  408. if (utype == V_ASN1_SET)
  409. derlen = i2d_ASN1_SET_ANY(sk, &der);
  410. else
  411. derlen = i2d_ASN1_SEQUENCE_ANY(sk, &der);
  412. if (derlen < 0)
  413. goto bad;
  414. if (!(ret = ASN1_TYPE_new()))
  415. goto bad;
  416. if (!(ret->value.asn1_string = ASN1_STRING_type_new(utype)))
  417. goto bad;
  418. ret->type = utype;
  419. ret->value.asn1_string->data = der;
  420. ret->value.asn1_string->length = derlen;
  421. der = NULL;
  422. bad:
  423. if (der)
  424. OPENSSL_free(der);
  425. if (sk)
  426. sk_ASN1_TYPE_pop_free(sk, ASN1_TYPE_free);
  427. if (sect)
  428. X509V3_section_free(cnf, sect);
  429. return ret;
  430. }
  431. static int append_exp(tag_exp_arg *arg, int exp_tag, int exp_class, int exp_constructed, int exp_pad, int imp_ok)
  432. {
  433. tag_exp_type *exp_tmp;
  434. /* Can only have IMPLICIT if permitted */
  435. if ((arg->imp_tag != -1) && !imp_ok)
  436. {
  437. ASN1err(ASN1_F_APPEND_EXP, ASN1_R_ILLEGAL_IMPLICIT_TAG);
  438. return 0;
  439. }
  440. if (arg->exp_count == ASN1_FLAG_EXP_MAX)
  441. {
  442. ASN1err(ASN1_F_APPEND_EXP, ASN1_R_DEPTH_EXCEEDED);
  443. return 0;
  444. }
  445. exp_tmp = &arg->exp_list[arg->exp_count++];
  446. /* If IMPLICIT set tag to implicit value then
  447. * reset implicit tag since it has been used.
  448. */
  449. if (arg->imp_tag != -1)
  450. {
  451. exp_tmp->exp_tag = arg->imp_tag;
  452. exp_tmp->exp_class = arg->imp_class;
  453. arg->imp_tag = -1;
  454. arg->imp_class = -1;
  455. }
  456. else
  457. {
  458. exp_tmp->exp_tag = exp_tag;
  459. exp_tmp->exp_class = exp_class;
  460. }
  461. exp_tmp->exp_constructed = exp_constructed;
  462. exp_tmp->exp_pad = exp_pad;
  463. return 1;
  464. }
  465. static int asn1_str2tag(const char *tagstr, int len)
  466. {
  467. unsigned int i;
  468. static const struct tag_name_st *tntmp, tnst [] = {
  469. ASN1_GEN_STR("BOOL", V_ASN1_BOOLEAN),
  470. ASN1_GEN_STR("BOOLEAN", V_ASN1_BOOLEAN),
  471. ASN1_GEN_STR("NULL", V_ASN1_NULL),
  472. ASN1_GEN_STR("INT", V_ASN1_INTEGER),
  473. ASN1_GEN_STR("INTEGER", V_ASN1_INTEGER),
  474. ASN1_GEN_STR("ENUM", V_ASN1_ENUMERATED),
  475. ASN1_GEN_STR("ENUMERATED", V_ASN1_ENUMERATED),
  476. ASN1_GEN_STR("OID", V_ASN1_OBJECT),
  477. ASN1_GEN_STR("OBJECT", V_ASN1_OBJECT),
  478. ASN1_GEN_STR("UTCTIME", V_ASN1_UTCTIME),
  479. ASN1_GEN_STR("UTC", V_ASN1_UTCTIME),
  480. ASN1_GEN_STR("GENERALIZEDTIME", V_ASN1_GENERALIZEDTIME),
  481. ASN1_GEN_STR("GENTIME", V_ASN1_GENERALIZEDTIME),
  482. ASN1_GEN_STR("OCT", V_ASN1_OCTET_STRING),
  483. ASN1_GEN_STR("OCTETSTRING", V_ASN1_OCTET_STRING),
  484. ASN1_GEN_STR("BITSTR", V_ASN1_BIT_STRING),
  485. ASN1_GEN_STR("BITSTRING", V_ASN1_BIT_STRING),
  486. ASN1_GEN_STR("UNIVERSALSTRING", V_ASN1_UNIVERSALSTRING),
  487. ASN1_GEN_STR("UNIV", V_ASN1_UNIVERSALSTRING),
  488. ASN1_GEN_STR("IA5", V_ASN1_IA5STRING),
  489. ASN1_GEN_STR("IA5STRING", V_ASN1_IA5STRING),
  490. ASN1_GEN_STR("UTF8", V_ASN1_UTF8STRING),
  491. ASN1_GEN_STR("UTF8String", V_ASN1_UTF8STRING),
  492. ASN1_GEN_STR("BMP", V_ASN1_BMPSTRING),
  493. ASN1_GEN_STR("BMPSTRING", V_ASN1_BMPSTRING),
  494. ASN1_GEN_STR("VISIBLESTRING", V_ASN1_VISIBLESTRING),
  495. ASN1_GEN_STR("VISIBLE", V_ASN1_VISIBLESTRING),
  496. ASN1_GEN_STR("PRINTABLESTRING", V_ASN1_PRINTABLESTRING),
  497. ASN1_GEN_STR("PRINTABLE", V_ASN1_PRINTABLESTRING),
  498. ASN1_GEN_STR("T61", V_ASN1_T61STRING),
  499. ASN1_GEN_STR("T61STRING", V_ASN1_T61STRING),
  500. ASN1_GEN_STR("TELETEXSTRING", V_ASN1_T61STRING),
  501. ASN1_GEN_STR("GeneralString", V_ASN1_GENERALSTRING),
  502. ASN1_GEN_STR("GENSTR", V_ASN1_GENERALSTRING),
  503. ASN1_GEN_STR("NUMERIC", V_ASN1_NUMERICSTRING),
  504. ASN1_GEN_STR("NUMERICSTRING", V_ASN1_NUMERICSTRING),
  505. /* Special cases */
  506. ASN1_GEN_STR("SEQUENCE", V_ASN1_SEQUENCE),
  507. ASN1_GEN_STR("SEQ", V_ASN1_SEQUENCE),
  508. ASN1_GEN_STR("SET", V_ASN1_SET),
  509. /* type modifiers */
  510. /* Explicit tag */
  511. ASN1_GEN_STR("EXP", ASN1_GEN_FLAG_EXP),
  512. ASN1_GEN_STR("EXPLICIT", ASN1_GEN_FLAG_EXP),
  513. /* Implicit tag */
  514. ASN1_GEN_STR("IMP", ASN1_GEN_FLAG_IMP),
  515. ASN1_GEN_STR("IMPLICIT", ASN1_GEN_FLAG_IMP),
  516. /* OCTET STRING wrapper */
  517. ASN1_GEN_STR("OCTWRAP", ASN1_GEN_FLAG_OCTWRAP),
  518. /* SEQUENCE wrapper */
  519. ASN1_GEN_STR("SEQWRAP", ASN1_GEN_FLAG_SEQWRAP),
  520. /* SET wrapper */
  521. ASN1_GEN_STR("SETWRAP", ASN1_GEN_FLAG_SETWRAP),
  522. /* BIT STRING wrapper */
  523. ASN1_GEN_STR("BITWRAP", ASN1_GEN_FLAG_BITWRAP),
  524. ASN1_GEN_STR("FORM", ASN1_GEN_FLAG_FORMAT),
  525. ASN1_GEN_STR("FORMAT", ASN1_GEN_FLAG_FORMAT),
  526. };
  527. if (len == -1)
  528. len = strlen(tagstr);
  529. tntmp = tnst;
  530. for (i = 0; i < sizeof(tnst) / sizeof(struct tag_name_st); i++, tntmp++)
  531. {
  532. if ((len == tntmp->len) && !strncmp(tntmp->strnam, tagstr, len))
  533. return tntmp->tag;
  534. }
  535. return -1;
  536. }
  537. static ASN1_TYPE *asn1_str2type(const char *str, int format, int utype)
  538. {
  539. ASN1_TYPE *atmp = NULL;
  540. CONF_VALUE vtmp;
  541. unsigned char *rdata;
  542. long rdlen;
  543. int no_unused = 1;
  544. if (!(atmp = ASN1_TYPE_new()))
  545. {
  546. ASN1err(ASN1_F_ASN1_STR2TYPE, ERR_R_MALLOC_FAILURE);
  547. return NULL;
  548. }
  549. if (!str)
  550. str = "";
  551. switch(utype)
  552. {
  553. case V_ASN1_NULL:
  554. if (str && *str)
  555. {
  556. ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_NULL_VALUE);
  557. goto bad_form;
  558. }
  559. break;
  560. case V_ASN1_BOOLEAN:
  561. if (format != ASN1_GEN_FORMAT_ASCII)
  562. {
  563. ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_NOT_ASCII_FORMAT);
  564. goto bad_form;
  565. }
  566. vtmp.name = NULL;
  567. vtmp.section = NULL;
  568. vtmp.value = (char *)str;
  569. if (!X509V3_get_value_bool(&vtmp, &atmp->value.boolean))
  570. {
  571. ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_BOOLEAN);
  572. goto bad_str;
  573. }
  574. break;
  575. case V_ASN1_INTEGER:
  576. case V_ASN1_ENUMERATED:
  577. if (format != ASN1_GEN_FORMAT_ASCII)
  578. {
  579. ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_INTEGER_NOT_ASCII_FORMAT);
  580. goto bad_form;
  581. }
  582. if (!(atmp->value.integer = s2i_ASN1_INTEGER(NULL, (char *)str)))
  583. {
  584. ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_INTEGER);
  585. goto bad_str;
  586. }
  587. break;
  588. case V_ASN1_OBJECT:
  589. if (format != ASN1_GEN_FORMAT_ASCII)
  590. {
  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. {
  596. ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_OBJECT);
  597. goto bad_str;
  598. }
  599. break;
  600. case V_ASN1_UTCTIME:
  601. case V_ASN1_GENERALIZEDTIME:
  602. if (format != ASN1_GEN_FORMAT_ASCII)
  603. {
  604. ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_TIME_NOT_ASCII_FORMAT);
  605. goto bad_form;
  606. }
  607. if (!(atmp->value.asn1_string = ASN1_STRING_new()))
  608. {
  609. ASN1err(ASN1_F_ASN1_STR2TYPE, ERR_R_MALLOC_FAILURE);
  610. goto bad_str;
  611. }
  612. if (!ASN1_STRING_set(atmp->value.asn1_string, str, -1))
  613. {
  614. ASN1err(ASN1_F_ASN1_STR2TYPE, ERR_R_MALLOC_FAILURE);
  615. goto bad_str;
  616. }
  617. atmp->value.asn1_string->type = utype;
  618. if (!ASN1_TIME_check(atmp->value.asn1_string))
  619. {
  620. ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_TIME_VALUE);
  621. goto bad_str;
  622. }
  623. break;
  624. case V_ASN1_BMPSTRING:
  625. case V_ASN1_PRINTABLESTRING:
  626. case V_ASN1_IA5STRING:
  627. case V_ASN1_T61STRING:
  628. case V_ASN1_UTF8STRING:
  629. case V_ASN1_VISIBLESTRING:
  630. case V_ASN1_UNIVERSALSTRING:
  631. case V_ASN1_GENERALSTRING:
  632. case V_ASN1_NUMERICSTRING:
  633. if (format == ASN1_GEN_FORMAT_ASCII)
  634. format = MBSTRING_ASC;
  635. else if (format == ASN1_GEN_FORMAT_UTF8)
  636. format = MBSTRING_UTF8;
  637. else
  638. {
  639. ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_FORMAT);
  640. goto bad_form;
  641. }
  642. if (ASN1_mbstring_copy(&atmp->value.asn1_string, (unsigned char *)str,
  643. -1, format, ASN1_tag2bit(utype)) <= 0)
  644. {
  645. ASN1err(ASN1_F_ASN1_STR2TYPE, ERR_R_MALLOC_FAILURE);
  646. goto bad_str;
  647. }
  648. break;
  649. case V_ASN1_BIT_STRING:
  650. case V_ASN1_OCTET_STRING:
  651. if (!(atmp->value.asn1_string = ASN1_STRING_new()))
  652. {
  653. ASN1err(ASN1_F_ASN1_STR2TYPE, ERR_R_MALLOC_FAILURE);
  654. goto bad_form;
  655. }
  656. if (format == ASN1_GEN_FORMAT_HEX)
  657. {
  658. if (!(rdata = string_to_hex((char *)str, &rdlen)))
  659. {
  660. ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_HEX);
  661. goto bad_str;
  662. }
  663. atmp->value.asn1_string->data = rdata;
  664. atmp->value.asn1_string->length = rdlen;
  665. atmp->value.asn1_string->type = utype;
  666. }
  667. else if (format == ASN1_GEN_FORMAT_ASCII)
  668. ASN1_STRING_set(atmp->value.asn1_string, str, -1);
  669. else if ((format == ASN1_GEN_FORMAT_BITLIST) && (utype == V_ASN1_BIT_STRING))
  670. {
  671. if (!CONF_parse_list(str, ',', 1, bitstr_cb, atmp->value.bit_string))
  672. {
  673. ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_LIST_ERROR);
  674. goto bad_str;
  675. }
  676. no_unused = 0;
  677. }
  678. else
  679. {
  680. ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_BITSTRING_FORMAT);
  681. goto bad_form;
  682. }
  683. if ((utype == V_ASN1_BIT_STRING) && no_unused)
  684. {
  685. atmp->value.asn1_string->flags
  686. &= ~(ASN1_STRING_FLAG_BITS_LEFT|0x07);
  687. atmp->value.asn1_string->flags
  688. |= ASN1_STRING_FLAG_BITS_LEFT;
  689. }
  690. break;
  691. default:
  692. ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_UNSUPPORTED_TYPE);
  693. goto bad_str;
  694. break;
  695. }
  696. atmp->type = utype;
  697. return atmp;
  698. bad_str:
  699. ERR_add_error_data(2, "string=", str);
  700. bad_form:
  701. ASN1_TYPE_free(atmp);
  702. return NULL;
  703. }
  704. static int bitstr_cb(const char *elem, int len, void *bitstr)
  705. {
  706. long bitnum;
  707. char *eptr;
  708. if (!elem)
  709. return 0;
  710. bitnum = strtoul(elem, &eptr, 10);
  711. if (eptr && *eptr && (eptr != elem + len))
  712. return 0;
  713. if (bitnum < 0)
  714. {
  715. ASN1err(ASN1_F_BITSTR_CB, ASN1_R_INVALID_NUMBER);
  716. return 0;
  717. }
  718. if (!ASN1_BIT_STRING_set_bit(bitstr, bitnum, 1))
  719. {
  720. ASN1err(ASN1_F_BITSTR_CB, ERR_R_MALLOC_FAILURE);
  721. return 0;
  722. }
  723. return 1;
  724. }