asn1_gen.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. /*
  2. * Copyright 2002-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include "internal/cryptlib.h"
  10. #include <openssl/asn1.h>
  11. #include <openssl/x509v3.h>
  12. #define ASN1_GEN_FLAG 0x10000
  13. #define ASN1_GEN_FLAG_IMP (ASN1_GEN_FLAG|1)
  14. #define ASN1_GEN_FLAG_EXP (ASN1_GEN_FLAG|2)
  15. #define ASN1_GEN_FLAG_TAG (ASN1_GEN_FLAG|3)
  16. #define ASN1_GEN_FLAG_BITWRAP (ASN1_GEN_FLAG|4)
  17. #define ASN1_GEN_FLAG_OCTWRAP (ASN1_GEN_FLAG|5)
  18. #define ASN1_GEN_FLAG_SEQWRAP (ASN1_GEN_FLAG|6)
  19. #define ASN1_GEN_FLAG_SETWRAP (ASN1_GEN_FLAG|7)
  20. #define ASN1_GEN_FLAG_FORMAT (ASN1_GEN_FLAG|8)
  21. #define ASN1_GEN_STR(str,val) {str, sizeof(str) - 1, val}
  22. #define ASN1_FLAG_EXP_MAX 20
  23. /* Maximum number of nested sequences */
  24. #define ASN1_GEN_SEQ_MAX_DEPTH 50
  25. /* Input formats */
  26. /* ASCII: default */
  27. #define ASN1_GEN_FORMAT_ASCII 1
  28. /* UTF8 */
  29. #define ASN1_GEN_FORMAT_UTF8 2
  30. /* Hex */
  31. #define ASN1_GEN_FORMAT_HEX 3
  32. /* List of bits */
  33. #define ASN1_GEN_FORMAT_BITLIST 4
  34. struct tag_name_st {
  35. const char *strnam;
  36. int len;
  37. int tag;
  38. };
  39. typedef struct {
  40. int exp_tag;
  41. int exp_class;
  42. int exp_constructed;
  43. int exp_pad;
  44. long exp_len;
  45. } tag_exp_type;
  46. typedef struct {
  47. int imp_tag;
  48. int imp_class;
  49. int utype;
  50. int format;
  51. const char *str;
  52. tag_exp_type exp_list[ASN1_FLAG_EXP_MAX];
  53. int exp_count;
  54. } tag_exp_arg;
  55. static ASN1_TYPE *generate_v3(const char *str, X509V3_CTX *cnf, int depth,
  56. int *perr);
  57. static int bitstr_cb(const char *elem, int len, void *bitstr);
  58. static int asn1_cb(const char *elem, int len, void *bitstr);
  59. static int append_exp(tag_exp_arg *arg, int exp_tag, int exp_class,
  60. int exp_constructed, int exp_pad, int imp_ok);
  61. static int parse_tagging(const char *vstart, int vlen, int *ptag,
  62. int *pclass);
  63. static ASN1_TYPE *asn1_multi(int utype, const char *section, X509V3_CTX *cnf,
  64. int depth, int *perr);
  65. static ASN1_TYPE *asn1_str2type(const char *str, int format, int utype);
  66. static int asn1_str2tag(const char *tagstr, int len);
  67. ASN1_TYPE *ASN1_generate_nconf(const char *str, CONF *nconf)
  68. {
  69. X509V3_CTX cnf;
  70. if (!nconf)
  71. return ASN1_generate_v3(str, NULL);
  72. X509V3_set_nconf(&cnf, nconf);
  73. return ASN1_generate_v3(str, &cnf);
  74. }
  75. ASN1_TYPE *ASN1_generate_v3(const char *str, X509V3_CTX *cnf)
  76. {
  77. int err = 0;
  78. ASN1_TYPE *ret = generate_v3(str, cnf, 0, &err);
  79. if (err)
  80. ERR_raise(ERR_LIB_ASN1, err);
  81. return ret;
  82. }
  83. static ASN1_TYPE *generate_v3(const char *str, X509V3_CTX *cnf, int depth,
  84. int *perr)
  85. {
  86. ASN1_TYPE *ret;
  87. tag_exp_arg asn1_tags;
  88. tag_exp_type *etmp;
  89. int i, len;
  90. unsigned char *orig_der = NULL, *new_der = NULL;
  91. const unsigned char *cpy_start;
  92. unsigned char *p;
  93. const unsigned char *cp;
  94. int cpy_len;
  95. long hdr_len = 0;
  96. int hdr_constructed = 0, hdr_tag, hdr_class;
  97. int r;
  98. asn1_tags.imp_tag = -1;
  99. asn1_tags.imp_class = -1;
  100. asn1_tags.format = ASN1_GEN_FORMAT_ASCII;
  101. asn1_tags.exp_count = 0;
  102. if (CONF_parse_list(str, ',', 1, asn1_cb, &asn1_tags) != 0) {
  103. *perr = ASN1_R_UNKNOWN_TAG;
  104. return NULL;
  105. }
  106. if ((asn1_tags.utype == V_ASN1_SEQUENCE)
  107. || (asn1_tags.utype == V_ASN1_SET)) {
  108. if (!cnf) {
  109. *perr = ASN1_R_SEQUENCE_OR_SET_NEEDS_CONFIG;
  110. return NULL;
  111. }
  112. if (depth >= ASN1_GEN_SEQ_MAX_DEPTH) {
  113. *perr = ASN1_R_ILLEGAL_NESTED_TAGGING;
  114. return NULL;
  115. }
  116. ret = asn1_multi(asn1_tags.utype, asn1_tags.str, cnf, depth, perr);
  117. } else
  118. ret = asn1_str2type(asn1_tags.str, asn1_tags.format, asn1_tags.utype);
  119. if (!ret)
  120. return NULL;
  121. /* If no tagging return base type */
  122. if ((asn1_tags.imp_tag == -1) && (asn1_tags.exp_count == 0))
  123. return ret;
  124. /* Generate the encoding */
  125. cpy_len = i2d_ASN1_TYPE(ret, &orig_der);
  126. ASN1_TYPE_free(ret);
  127. ret = NULL;
  128. /* Set point to start copying for modified encoding */
  129. cpy_start = orig_der;
  130. /* Do we need IMPLICIT tagging? */
  131. if (asn1_tags.imp_tag != -1) {
  132. /* If IMPLICIT we will replace the underlying tag */
  133. /* Skip existing tag+len */
  134. r = ASN1_get_object(&cpy_start, &hdr_len, &hdr_tag, &hdr_class,
  135. cpy_len);
  136. if (r & 0x80)
  137. goto err;
  138. /* Update copy length */
  139. cpy_len -= cpy_start - orig_der;
  140. /*
  141. * For IMPLICIT tagging the length should match the original length
  142. * and constructed flag should be consistent.
  143. */
  144. if (r & 0x1) {
  145. /* Indefinite length constructed */
  146. hdr_constructed = 2;
  147. hdr_len = 0;
  148. } else
  149. /* Just retain constructed flag */
  150. hdr_constructed = r & V_ASN1_CONSTRUCTED;
  151. /*
  152. * Work out new length with IMPLICIT tag: ignore constructed because
  153. * it will mess up if indefinite length
  154. */
  155. len = ASN1_object_size(0, hdr_len, asn1_tags.imp_tag);
  156. } else
  157. len = cpy_len;
  158. /* Work out length in any EXPLICIT, starting from end */
  159. for (i = 0, etmp = asn1_tags.exp_list + asn1_tags.exp_count - 1;
  160. i < asn1_tags.exp_count; i++, etmp--) {
  161. /* Content length: number of content octets + any padding */
  162. len += etmp->exp_pad;
  163. etmp->exp_len = len;
  164. /* Total object length: length including new header */
  165. len = ASN1_object_size(0, len, etmp->exp_tag);
  166. }
  167. /* Allocate buffer for new encoding */
  168. new_der = OPENSSL_malloc(len);
  169. if (new_der == NULL)
  170. goto err;
  171. /* Generate tagged encoding */
  172. p = new_der;
  173. /* Output explicit tags first */
  174. for (i = 0, etmp = asn1_tags.exp_list; i < asn1_tags.exp_count;
  175. i++, etmp++) {
  176. ASN1_put_object(&p, etmp->exp_constructed, etmp->exp_len,
  177. etmp->exp_tag, etmp->exp_class);
  178. if (etmp->exp_pad)
  179. *p++ = 0;
  180. }
  181. /* If IMPLICIT, output tag */
  182. if (asn1_tags.imp_tag != -1) {
  183. if (asn1_tags.imp_class == V_ASN1_UNIVERSAL
  184. && (asn1_tags.imp_tag == V_ASN1_SEQUENCE
  185. || asn1_tags.imp_tag == V_ASN1_SET))
  186. hdr_constructed = V_ASN1_CONSTRUCTED;
  187. ASN1_put_object(&p, hdr_constructed, hdr_len,
  188. asn1_tags.imp_tag, asn1_tags.imp_class);
  189. }
  190. /* Copy across original encoding */
  191. memcpy(p, cpy_start, cpy_len);
  192. cp = new_der;
  193. /* Obtain new ASN1_TYPE structure */
  194. ret = d2i_ASN1_TYPE(NULL, &cp, len);
  195. err:
  196. OPENSSL_free(orig_der);
  197. OPENSSL_free(new_der);
  198. return ret;
  199. }
  200. static int asn1_cb(const char *elem, int len, void *bitstr)
  201. {
  202. tag_exp_arg *arg = bitstr;
  203. int i;
  204. int utype;
  205. int vlen = 0;
  206. const char *p, *vstart = NULL;
  207. int tmp_tag, tmp_class;
  208. if (elem == NULL)
  209. return -1;
  210. for (i = 0, p = elem; i < len; p++, i++) {
  211. /* Look for the ':' in name value pairs */
  212. if (*p == ':') {
  213. vstart = p + 1;
  214. vlen = len - (vstart - elem);
  215. len = p - elem;
  216. break;
  217. }
  218. }
  219. utype = asn1_str2tag(elem, len);
  220. if (utype == -1) {
  221. ERR_raise_data(ERR_LIB_ASN1, ASN1_R_UNKNOWN_TAG, "tag=%s", elem);
  222. return -1;
  223. }
  224. /* If this is not a modifier mark end of string and exit */
  225. if (!(utype & ASN1_GEN_FLAG)) {
  226. arg->utype = utype;
  227. arg->str = vstart;
  228. /* If no value and not end of string, error */
  229. if (!vstart && elem[len]) {
  230. ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_VALUE);
  231. return -1;
  232. }
  233. return 0;
  234. }
  235. switch (utype) {
  236. case ASN1_GEN_FLAG_IMP:
  237. /* Check for illegal multiple IMPLICIT tagging */
  238. if (arg->imp_tag != -1) {
  239. ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_NESTED_TAGGING);
  240. return -1;
  241. }
  242. if (!parse_tagging(vstart, vlen, &arg->imp_tag, &arg->imp_class))
  243. return -1;
  244. break;
  245. case ASN1_GEN_FLAG_EXP:
  246. if (!parse_tagging(vstart, vlen, &tmp_tag, &tmp_class))
  247. return -1;
  248. if (!append_exp(arg, tmp_tag, tmp_class, 1, 0, 0))
  249. return -1;
  250. break;
  251. case ASN1_GEN_FLAG_SEQWRAP:
  252. if (!append_exp(arg, V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL, 1, 0, 1))
  253. return -1;
  254. break;
  255. case ASN1_GEN_FLAG_SETWRAP:
  256. if (!append_exp(arg, V_ASN1_SET, V_ASN1_UNIVERSAL, 1, 0, 1))
  257. return -1;
  258. break;
  259. case ASN1_GEN_FLAG_BITWRAP:
  260. if (!append_exp(arg, V_ASN1_BIT_STRING, V_ASN1_UNIVERSAL, 0, 1, 1))
  261. return -1;
  262. break;
  263. case ASN1_GEN_FLAG_OCTWRAP:
  264. if (!append_exp(arg, V_ASN1_OCTET_STRING, V_ASN1_UNIVERSAL, 0, 0, 1))
  265. return -1;
  266. break;
  267. case ASN1_GEN_FLAG_FORMAT:
  268. if (!vstart) {
  269. ERR_raise(ERR_LIB_ASN1, ASN1_R_UNKNOWN_FORMAT);
  270. return -1;
  271. }
  272. if (HAS_PREFIX(vstart, "ASCII"))
  273. arg->format = ASN1_GEN_FORMAT_ASCII;
  274. else if (HAS_PREFIX(vstart, "UTF8"))
  275. arg->format = ASN1_GEN_FORMAT_UTF8;
  276. else if (HAS_PREFIX(vstart, "HEX"))
  277. arg->format = ASN1_GEN_FORMAT_HEX;
  278. else if (HAS_PREFIX(vstart, "BITLIST"))
  279. arg->format = ASN1_GEN_FORMAT_BITLIST;
  280. else {
  281. ERR_raise(ERR_LIB_ASN1, ASN1_R_UNKNOWN_FORMAT);
  282. return -1;
  283. }
  284. break;
  285. }
  286. return 1;
  287. }
  288. static int parse_tagging(const char *vstart, int vlen, int *ptag, int *pclass)
  289. {
  290. long tag_num;
  291. char *eptr;
  292. if (!vstart)
  293. return 0;
  294. tag_num = strtoul(vstart, &eptr, 10);
  295. /* Check we haven't gone past max length: should be impossible */
  296. if (eptr && *eptr && (eptr > vstart + vlen))
  297. return 0;
  298. if (tag_num < 0) {
  299. ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_NUMBER);
  300. return 0;
  301. }
  302. *ptag = tag_num;
  303. /* If we have non numeric characters, parse them */
  304. if (eptr)
  305. vlen -= eptr - vstart;
  306. else
  307. vlen = 0;
  308. if (vlen) {
  309. switch (*eptr) {
  310. case 'U':
  311. *pclass = V_ASN1_UNIVERSAL;
  312. break;
  313. case 'A':
  314. *pclass = V_ASN1_APPLICATION;
  315. break;
  316. case 'P':
  317. *pclass = V_ASN1_PRIVATE;
  318. break;
  319. case 'C':
  320. *pclass = V_ASN1_CONTEXT_SPECIFIC;
  321. break;
  322. default:
  323. ERR_raise_data(ERR_LIB_ASN1, ASN1_R_INVALID_MODIFIER,
  324. "Char=%c", *eptr);
  325. return 0;
  326. }
  327. } else
  328. *pclass = V_ASN1_CONTEXT_SPECIFIC;
  329. return 1;
  330. }
  331. /* Handle multiple types: SET and SEQUENCE */
  332. static ASN1_TYPE *asn1_multi(int utype, const char *section, X509V3_CTX *cnf,
  333. int depth, int *perr)
  334. {
  335. ASN1_TYPE *ret = NULL;
  336. STACK_OF(ASN1_TYPE) *sk = NULL;
  337. STACK_OF(CONF_VALUE) *sect = NULL;
  338. unsigned char *der = NULL;
  339. int derlen;
  340. int i;
  341. sk = sk_ASN1_TYPE_new_null();
  342. if (!sk)
  343. goto bad;
  344. if (section) {
  345. if (!cnf)
  346. goto bad;
  347. sect = X509V3_get_section(cnf, (char *)section);
  348. if (!sect)
  349. goto bad;
  350. for (i = 0; i < sk_CONF_VALUE_num(sect); i++) {
  351. ASN1_TYPE *typ =
  352. generate_v3(sk_CONF_VALUE_value(sect, i)->value, cnf,
  353. depth + 1, perr);
  354. if (!typ)
  355. goto bad;
  356. if (!sk_ASN1_TYPE_push(sk, typ))
  357. goto bad;
  358. }
  359. }
  360. /*
  361. * Now we has a STACK of the components, convert to the correct form
  362. */
  363. if (utype == V_ASN1_SET)
  364. derlen = i2d_ASN1_SET_ANY(sk, &der);
  365. else
  366. derlen = i2d_ASN1_SEQUENCE_ANY(sk, &der);
  367. if (derlen < 0)
  368. goto bad;
  369. if ((ret = ASN1_TYPE_new()) == NULL)
  370. goto bad;
  371. if ((ret->value.asn1_string = ASN1_STRING_type_new(utype)) == NULL)
  372. goto bad;
  373. ret->type = utype;
  374. ret->value.asn1_string->data = der;
  375. ret->value.asn1_string->length = derlen;
  376. der = NULL;
  377. bad:
  378. OPENSSL_free(der);
  379. sk_ASN1_TYPE_pop_free(sk, ASN1_TYPE_free);
  380. X509V3_section_free(cnf, sect);
  381. return ret;
  382. }
  383. static int append_exp(tag_exp_arg *arg, int exp_tag, int exp_class,
  384. int exp_constructed, int exp_pad, int imp_ok)
  385. {
  386. tag_exp_type *exp_tmp;
  387. /* Can only have IMPLICIT if permitted */
  388. if ((arg->imp_tag != -1) && !imp_ok) {
  389. ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_IMPLICIT_TAG);
  390. return 0;
  391. }
  392. if (arg->exp_count == ASN1_FLAG_EXP_MAX) {
  393. ERR_raise(ERR_LIB_ASN1, ASN1_R_DEPTH_EXCEEDED);
  394. return 0;
  395. }
  396. exp_tmp = &arg->exp_list[arg->exp_count++];
  397. /*
  398. * If IMPLICIT set tag to implicit value then reset implicit tag since it
  399. * has been used.
  400. */
  401. if (arg->imp_tag != -1) {
  402. exp_tmp->exp_tag = arg->imp_tag;
  403. exp_tmp->exp_class = arg->imp_class;
  404. arg->imp_tag = -1;
  405. arg->imp_class = -1;
  406. } else {
  407. exp_tmp->exp_tag = exp_tag;
  408. exp_tmp->exp_class = exp_class;
  409. }
  410. exp_tmp->exp_constructed = exp_constructed;
  411. exp_tmp->exp_pad = exp_pad;
  412. return 1;
  413. }
  414. static int asn1_str2tag(const char *tagstr, int len)
  415. {
  416. unsigned int i;
  417. static const struct tag_name_st *tntmp, tnst[] = {
  418. ASN1_GEN_STR("BOOL", V_ASN1_BOOLEAN),
  419. ASN1_GEN_STR("BOOLEAN", V_ASN1_BOOLEAN),
  420. ASN1_GEN_STR("NULL", V_ASN1_NULL),
  421. ASN1_GEN_STR("INT", V_ASN1_INTEGER),
  422. ASN1_GEN_STR("INTEGER", V_ASN1_INTEGER),
  423. ASN1_GEN_STR("ENUM", V_ASN1_ENUMERATED),
  424. ASN1_GEN_STR("ENUMERATED", V_ASN1_ENUMERATED),
  425. ASN1_GEN_STR("OID", V_ASN1_OBJECT),
  426. ASN1_GEN_STR("OBJECT", V_ASN1_OBJECT),
  427. ASN1_GEN_STR("UTCTIME", V_ASN1_UTCTIME),
  428. ASN1_GEN_STR("UTC", V_ASN1_UTCTIME),
  429. ASN1_GEN_STR("GENERALIZEDTIME", V_ASN1_GENERALIZEDTIME),
  430. ASN1_GEN_STR("GENTIME", V_ASN1_GENERALIZEDTIME),
  431. ASN1_GEN_STR("OCT", V_ASN1_OCTET_STRING),
  432. ASN1_GEN_STR("OCTETSTRING", V_ASN1_OCTET_STRING),
  433. ASN1_GEN_STR("BITSTR", V_ASN1_BIT_STRING),
  434. ASN1_GEN_STR("BITSTRING", V_ASN1_BIT_STRING),
  435. ASN1_GEN_STR("UNIVERSALSTRING", V_ASN1_UNIVERSALSTRING),
  436. ASN1_GEN_STR("UNIV", V_ASN1_UNIVERSALSTRING),
  437. ASN1_GEN_STR("IA5", V_ASN1_IA5STRING),
  438. ASN1_GEN_STR("IA5STRING", V_ASN1_IA5STRING),
  439. ASN1_GEN_STR("UTF8", V_ASN1_UTF8STRING),
  440. ASN1_GEN_STR("UTF8String", V_ASN1_UTF8STRING),
  441. ASN1_GEN_STR("BMP", V_ASN1_BMPSTRING),
  442. ASN1_GEN_STR("BMPSTRING", V_ASN1_BMPSTRING),
  443. ASN1_GEN_STR("VISIBLESTRING", V_ASN1_VISIBLESTRING),
  444. ASN1_GEN_STR("VISIBLE", V_ASN1_VISIBLESTRING),
  445. ASN1_GEN_STR("PRINTABLESTRING", V_ASN1_PRINTABLESTRING),
  446. ASN1_GEN_STR("PRINTABLE", V_ASN1_PRINTABLESTRING),
  447. ASN1_GEN_STR("T61", V_ASN1_T61STRING),
  448. ASN1_GEN_STR("T61STRING", V_ASN1_T61STRING),
  449. ASN1_GEN_STR("TELETEXSTRING", V_ASN1_T61STRING),
  450. ASN1_GEN_STR("GeneralString", V_ASN1_GENERALSTRING),
  451. ASN1_GEN_STR("GENSTR", V_ASN1_GENERALSTRING),
  452. ASN1_GEN_STR("NUMERIC", V_ASN1_NUMERICSTRING),
  453. ASN1_GEN_STR("NUMERICSTRING", V_ASN1_NUMERICSTRING),
  454. /* Special cases */
  455. ASN1_GEN_STR("SEQUENCE", V_ASN1_SEQUENCE),
  456. ASN1_GEN_STR("SEQ", V_ASN1_SEQUENCE),
  457. ASN1_GEN_STR("SET", V_ASN1_SET),
  458. /* type modifiers */
  459. /* Explicit tag */
  460. ASN1_GEN_STR("EXP", ASN1_GEN_FLAG_EXP),
  461. ASN1_GEN_STR("EXPLICIT", ASN1_GEN_FLAG_EXP),
  462. /* Implicit tag */
  463. ASN1_GEN_STR("IMP", ASN1_GEN_FLAG_IMP),
  464. ASN1_GEN_STR("IMPLICIT", ASN1_GEN_FLAG_IMP),
  465. /* OCTET STRING wrapper */
  466. ASN1_GEN_STR("OCTWRAP", ASN1_GEN_FLAG_OCTWRAP),
  467. /* SEQUENCE wrapper */
  468. ASN1_GEN_STR("SEQWRAP", ASN1_GEN_FLAG_SEQWRAP),
  469. /* SET wrapper */
  470. ASN1_GEN_STR("SETWRAP", ASN1_GEN_FLAG_SETWRAP),
  471. /* BIT STRING wrapper */
  472. ASN1_GEN_STR("BITWRAP", ASN1_GEN_FLAG_BITWRAP),
  473. ASN1_GEN_STR("FORM", ASN1_GEN_FLAG_FORMAT),
  474. ASN1_GEN_STR("FORMAT", ASN1_GEN_FLAG_FORMAT),
  475. };
  476. if (len == -1)
  477. len = strlen(tagstr);
  478. tntmp = tnst;
  479. for (i = 0; i < OSSL_NELEM(tnst); i++, tntmp++) {
  480. if ((len == tntmp->len)
  481. && (OPENSSL_strncasecmp(tntmp->strnam, tagstr, len) == 0))
  482. return tntmp->tag;
  483. }
  484. return -1;
  485. }
  486. static ASN1_TYPE *asn1_str2type(const char *str, int format, int utype)
  487. {
  488. ASN1_TYPE *atmp = NULL;
  489. CONF_VALUE vtmp;
  490. unsigned char *rdata;
  491. long rdlen;
  492. int no_unused = 1;
  493. if ((atmp = ASN1_TYPE_new()) == NULL) {
  494. ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
  495. return NULL;
  496. }
  497. if (!str)
  498. str = "";
  499. switch (utype) {
  500. case V_ASN1_NULL:
  501. if (str && *str) {
  502. ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_NULL_VALUE);
  503. goto bad_form;
  504. }
  505. break;
  506. case V_ASN1_BOOLEAN:
  507. if (format != ASN1_GEN_FORMAT_ASCII) {
  508. ERR_raise(ERR_LIB_ASN1, ASN1_R_NOT_ASCII_FORMAT);
  509. goto bad_form;
  510. }
  511. vtmp.name = NULL;
  512. vtmp.section = NULL;
  513. vtmp.value = (char *)str;
  514. if (!X509V3_get_value_bool(&vtmp, &atmp->value.boolean)) {
  515. ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_BOOLEAN);
  516. goto bad_str;
  517. }
  518. break;
  519. case V_ASN1_INTEGER:
  520. case V_ASN1_ENUMERATED:
  521. if (format != ASN1_GEN_FORMAT_ASCII) {
  522. ERR_raise(ERR_LIB_ASN1, ASN1_R_INTEGER_NOT_ASCII_FORMAT);
  523. goto bad_form;
  524. }
  525. if ((atmp->value.integer
  526. = s2i_ASN1_INTEGER(NULL, str)) == NULL) {
  527. ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_INTEGER);
  528. goto bad_str;
  529. }
  530. break;
  531. case V_ASN1_OBJECT:
  532. if (format != ASN1_GEN_FORMAT_ASCII) {
  533. ERR_raise(ERR_LIB_ASN1, ASN1_R_OBJECT_NOT_ASCII_FORMAT);
  534. goto bad_form;
  535. }
  536. if ((atmp->value.object = OBJ_txt2obj(str, 0)) == NULL) {
  537. ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_OBJECT);
  538. goto bad_str;
  539. }
  540. break;
  541. case V_ASN1_UTCTIME:
  542. case V_ASN1_GENERALIZEDTIME:
  543. if (format != ASN1_GEN_FORMAT_ASCII) {
  544. ERR_raise(ERR_LIB_ASN1, ASN1_R_TIME_NOT_ASCII_FORMAT);
  545. goto bad_form;
  546. }
  547. if ((atmp->value.asn1_string = ASN1_STRING_new()) == NULL) {
  548. ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
  549. goto bad_str;
  550. }
  551. if (!ASN1_STRING_set(atmp->value.asn1_string, str, -1)) {
  552. ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
  553. goto bad_str;
  554. }
  555. atmp->value.asn1_string->type = utype;
  556. if (!ASN1_TIME_check(atmp->value.asn1_string)) {
  557. ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_TIME_VALUE);
  558. goto bad_str;
  559. }
  560. break;
  561. case V_ASN1_BMPSTRING:
  562. case V_ASN1_PRINTABLESTRING:
  563. case V_ASN1_IA5STRING:
  564. case V_ASN1_T61STRING:
  565. case V_ASN1_UTF8STRING:
  566. case V_ASN1_VISIBLESTRING:
  567. case V_ASN1_UNIVERSALSTRING:
  568. case V_ASN1_GENERALSTRING:
  569. case V_ASN1_NUMERICSTRING:
  570. if (format == ASN1_GEN_FORMAT_ASCII)
  571. format = MBSTRING_ASC;
  572. else if (format == ASN1_GEN_FORMAT_UTF8)
  573. format = MBSTRING_UTF8;
  574. else {
  575. ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_FORMAT);
  576. goto bad_form;
  577. }
  578. if (ASN1_mbstring_copy(&atmp->value.asn1_string, (unsigned char *)str,
  579. -1, format, ASN1_tag2bit(utype)) <= 0) {
  580. ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
  581. goto bad_str;
  582. }
  583. break;
  584. case V_ASN1_BIT_STRING:
  585. case V_ASN1_OCTET_STRING:
  586. if ((atmp->value.asn1_string = ASN1_STRING_new()) == NULL) {
  587. ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
  588. goto bad_form;
  589. }
  590. if (format == ASN1_GEN_FORMAT_HEX) {
  591. if ((rdata = OPENSSL_hexstr2buf(str, &rdlen)) == NULL) {
  592. ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_HEX);
  593. goto bad_str;
  594. }
  595. atmp->value.asn1_string->data = rdata;
  596. atmp->value.asn1_string->length = rdlen;
  597. atmp->value.asn1_string->type = utype;
  598. } else if (format == ASN1_GEN_FORMAT_ASCII) {
  599. if (!ASN1_STRING_set(atmp->value.asn1_string, str, -1)) {
  600. ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
  601. goto bad_str;
  602. }
  603. } else if ((format == ASN1_GEN_FORMAT_BITLIST)
  604. && (utype == V_ASN1_BIT_STRING)) {
  605. if (!CONF_parse_list
  606. (str, ',', 1, bitstr_cb, atmp->value.bit_string)) {
  607. ERR_raise(ERR_LIB_ASN1, ASN1_R_LIST_ERROR);
  608. goto bad_str;
  609. }
  610. no_unused = 0;
  611. } else {
  612. ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_BITSTRING_FORMAT);
  613. goto bad_form;
  614. }
  615. if ((utype == V_ASN1_BIT_STRING) && no_unused)
  616. ossl_asn1_string_set_bits_left(atmp->value.asn1_string, 0);
  617. break;
  618. default:
  619. ERR_raise(ERR_LIB_ASN1, ASN1_R_UNSUPPORTED_TYPE);
  620. goto bad_str;
  621. }
  622. atmp->type = utype;
  623. return atmp;
  624. bad_str:
  625. ERR_add_error_data(2, "string=", str);
  626. bad_form:
  627. ASN1_TYPE_free(atmp);
  628. return NULL;
  629. }
  630. static int bitstr_cb(const char *elem, int len, void *bitstr)
  631. {
  632. long bitnum;
  633. char *eptr;
  634. if (!elem)
  635. return 0;
  636. bitnum = strtoul(elem, &eptr, 10);
  637. if (eptr && *eptr && (eptr != elem + len))
  638. return 0;
  639. if (bitnum < 0) {
  640. ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_NUMBER);
  641. return 0;
  642. }
  643. if (!ASN1_BIT_STRING_set_bit(bitstr, bitnum, 1)) {
  644. ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
  645. return 0;
  646. }
  647. return 1;
  648. }
  649. static int mask_cb(const char *elem, int len, void *arg)
  650. {
  651. unsigned long *pmask = arg, tmpmask;
  652. int tag;
  653. if (elem == NULL)
  654. return 0;
  655. if (len == 3 && HAS_PREFIX(elem, "DIR")) {
  656. *pmask |= B_ASN1_DIRECTORYSTRING;
  657. return 1;
  658. }
  659. tag = asn1_str2tag(elem, len);
  660. if (!tag || (tag & ASN1_GEN_FLAG))
  661. return 0;
  662. tmpmask = ASN1_tag2bit(tag);
  663. if (!tmpmask)
  664. return 0;
  665. *pmask |= tmpmask;
  666. return 1;
  667. }
  668. int ASN1_str2mask(const char *str, unsigned long *pmask)
  669. {
  670. *pmask = 0;
  671. return CONF_parse_list(str, '|', 1, mask_cb, pmask);
  672. }