asn1_gen.c 25 KB

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