2
0

asn1_gen.c 22 KB

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