asn1_gen.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. /*
  2. * Copyright 2002-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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. ASN1err(ASN1_F_ASN1_GENERATE_V3, 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. ASN1err(ASN1_F_ASN1_CB, ASN1_R_UNKNOWN_TAG);
  222. ERR_add_error_data(2, "tag=", 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. ASN1err(ASN1_F_ASN1_CB, 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. ASN1err(ASN1_F_ASN1_CB, 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. ASN1err(ASN1_F_ASN1_CB, ASN1_R_UNKNOWN_FORMAT);
  271. return -1;
  272. }
  273. if (strncmp(vstart, "ASCII", 5) == 0)
  274. arg->format = ASN1_GEN_FORMAT_ASCII;
  275. else if (strncmp(vstart, "UTF8", 4) == 0)
  276. arg->format = ASN1_GEN_FORMAT_UTF8;
  277. else if (strncmp(vstart, "HEX", 3) == 0)
  278. arg->format = ASN1_GEN_FORMAT_HEX;
  279. else if (strncmp(vstart, "BITLIST", 7) == 0)
  280. arg->format = ASN1_GEN_FORMAT_BITLIST;
  281. else {
  282. ASN1err(ASN1_F_ASN1_CB, 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. char erch[2];
  292. long tag_num;
  293. char *eptr;
  294. if (!vstart)
  295. return 0;
  296. tag_num = strtoul(vstart, &eptr, 10);
  297. /* Check we haven't gone past max length: should be impossible */
  298. if (eptr && *eptr && (eptr > vstart + vlen))
  299. return 0;
  300. if (tag_num < 0) {
  301. ASN1err(ASN1_F_PARSE_TAGGING, ASN1_R_INVALID_NUMBER);
  302. return 0;
  303. }
  304. *ptag = tag_num;
  305. /* If we have non numeric characters, parse them */
  306. if (eptr)
  307. vlen -= eptr - vstart;
  308. else
  309. vlen = 0;
  310. if (vlen) {
  311. switch (*eptr) {
  312. case 'U':
  313. *pclass = V_ASN1_UNIVERSAL;
  314. break;
  315. case 'A':
  316. *pclass = V_ASN1_APPLICATION;
  317. break;
  318. case 'P':
  319. *pclass = V_ASN1_PRIVATE;
  320. break;
  321. case 'C':
  322. *pclass = V_ASN1_CONTEXT_SPECIFIC;
  323. break;
  324. default:
  325. erch[0] = *eptr;
  326. erch[1] = 0;
  327. ASN1err(ASN1_F_PARSE_TAGGING, ASN1_R_INVALID_MODIFIER);
  328. ERR_add_error_data(2, "Char=", erch);
  329. return 0;
  330. }
  331. } else
  332. *pclass = V_ASN1_CONTEXT_SPECIFIC;
  333. return 1;
  334. }
  335. /* Handle multiple types: SET and SEQUENCE */
  336. static ASN1_TYPE *asn1_multi(int utype, const char *section, X509V3_CTX *cnf,
  337. int depth, int *perr)
  338. {
  339. ASN1_TYPE *ret = NULL;
  340. STACK_OF(ASN1_TYPE) *sk = NULL;
  341. STACK_OF(CONF_VALUE) *sect = NULL;
  342. unsigned char *der = NULL;
  343. int derlen;
  344. int i;
  345. sk = sk_ASN1_TYPE_new_null();
  346. if (!sk)
  347. goto bad;
  348. if (section) {
  349. if (!cnf)
  350. goto bad;
  351. sect = X509V3_get_section(cnf, (char *)section);
  352. if (!sect)
  353. goto bad;
  354. for (i = 0; i < sk_CONF_VALUE_num(sect); i++) {
  355. ASN1_TYPE *typ =
  356. generate_v3(sk_CONF_VALUE_value(sect, i)->value, cnf,
  357. depth + 1, perr);
  358. if (!typ)
  359. goto bad;
  360. if (!sk_ASN1_TYPE_push(sk, typ))
  361. goto bad;
  362. }
  363. }
  364. /*
  365. * Now we has a STACK of the components, convert to the correct form
  366. */
  367. if (utype == V_ASN1_SET)
  368. derlen = i2d_ASN1_SET_ANY(sk, &der);
  369. else
  370. derlen = i2d_ASN1_SEQUENCE_ANY(sk, &der);
  371. if (derlen < 0)
  372. goto bad;
  373. if ((ret = ASN1_TYPE_new()) == NULL)
  374. goto bad;
  375. if ((ret->value.asn1_string = ASN1_STRING_type_new(utype)) == NULL)
  376. goto bad;
  377. ret->type = utype;
  378. ret->value.asn1_string->data = der;
  379. ret->value.asn1_string->length = derlen;
  380. der = NULL;
  381. bad:
  382. OPENSSL_free(der);
  383. sk_ASN1_TYPE_pop_free(sk, ASN1_TYPE_free);
  384. X509V3_section_free(cnf, sect);
  385. return ret;
  386. }
  387. static int append_exp(tag_exp_arg *arg, int exp_tag, int exp_class,
  388. int exp_constructed, int exp_pad, int imp_ok)
  389. {
  390. tag_exp_type *exp_tmp;
  391. /* Can only have IMPLICIT if permitted */
  392. if ((arg->imp_tag != -1) && !imp_ok) {
  393. ASN1err(ASN1_F_APPEND_EXP, ASN1_R_ILLEGAL_IMPLICIT_TAG);
  394. return 0;
  395. }
  396. if (arg->exp_count == ASN1_FLAG_EXP_MAX) {
  397. ASN1err(ASN1_F_APPEND_EXP, ASN1_R_DEPTH_EXCEEDED);
  398. return 0;
  399. }
  400. exp_tmp = &arg->exp_list[arg->exp_count++];
  401. /*
  402. * If IMPLICIT set tag to implicit value then reset implicit tag since it
  403. * has been used.
  404. */
  405. if (arg->imp_tag != -1) {
  406. exp_tmp->exp_tag = arg->imp_tag;
  407. exp_tmp->exp_class = arg->imp_class;
  408. arg->imp_tag = -1;
  409. arg->imp_class = -1;
  410. } else {
  411. exp_tmp->exp_tag = exp_tag;
  412. exp_tmp->exp_class = exp_class;
  413. }
  414. exp_tmp->exp_constructed = exp_constructed;
  415. exp_tmp->exp_pad = exp_pad;
  416. return 1;
  417. }
  418. static int asn1_str2tag(const char *tagstr, int len)
  419. {
  420. unsigned int i;
  421. static const struct tag_name_st *tntmp, tnst[] = {
  422. ASN1_GEN_STR("BOOL", V_ASN1_BOOLEAN),
  423. ASN1_GEN_STR("BOOLEAN", V_ASN1_BOOLEAN),
  424. ASN1_GEN_STR("NULL", V_ASN1_NULL),
  425. ASN1_GEN_STR("INT", V_ASN1_INTEGER),
  426. ASN1_GEN_STR("INTEGER", V_ASN1_INTEGER),
  427. ASN1_GEN_STR("ENUM", V_ASN1_ENUMERATED),
  428. ASN1_GEN_STR("ENUMERATED", V_ASN1_ENUMERATED),
  429. ASN1_GEN_STR("OID", V_ASN1_OBJECT),
  430. ASN1_GEN_STR("OBJECT", V_ASN1_OBJECT),
  431. ASN1_GEN_STR("UTCTIME", V_ASN1_UTCTIME),
  432. ASN1_GEN_STR("UTC", V_ASN1_UTCTIME),
  433. ASN1_GEN_STR("GENERALIZEDTIME", V_ASN1_GENERALIZEDTIME),
  434. ASN1_GEN_STR("GENTIME", V_ASN1_GENERALIZEDTIME),
  435. ASN1_GEN_STR("OCT", V_ASN1_OCTET_STRING),
  436. ASN1_GEN_STR("OCTETSTRING", V_ASN1_OCTET_STRING),
  437. ASN1_GEN_STR("BITSTR", V_ASN1_BIT_STRING),
  438. ASN1_GEN_STR("BITSTRING", V_ASN1_BIT_STRING),
  439. ASN1_GEN_STR("UNIVERSALSTRING", V_ASN1_UNIVERSALSTRING),
  440. ASN1_GEN_STR("UNIV", V_ASN1_UNIVERSALSTRING),
  441. ASN1_GEN_STR("IA5", V_ASN1_IA5STRING),
  442. ASN1_GEN_STR("IA5STRING", V_ASN1_IA5STRING),
  443. ASN1_GEN_STR("UTF8", V_ASN1_UTF8STRING),
  444. ASN1_GEN_STR("UTF8String", V_ASN1_UTF8STRING),
  445. ASN1_GEN_STR("BMP", V_ASN1_BMPSTRING),
  446. ASN1_GEN_STR("BMPSTRING", V_ASN1_BMPSTRING),
  447. ASN1_GEN_STR("VISIBLESTRING", V_ASN1_VISIBLESTRING),
  448. ASN1_GEN_STR("VISIBLE", V_ASN1_VISIBLESTRING),
  449. ASN1_GEN_STR("PRINTABLESTRING", V_ASN1_PRINTABLESTRING),
  450. ASN1_GEN_STR("PRINTABLE", V_ASN1_PRINTABLESTRING),
  451. ASN1_GEN_STR("T61", V_ASN1_T61STRING),
  452. ASN1_GEN_STR("T61STRING", V_ASN1_T61STRING),
  453. ASN1_GEN_STR("TELETEXSTRING", V_ASN1_T61STRING),
  454. ASN1_GEN_STR("GeneralString", V_ASN1_GENERALSTRING),
  455. ASN1_GEN_STR("GENSTR", V_ASN1_GENERALSTRING),
  456. ASN1_GEN_STR("NUMERIC", V_ASN1_NUMERICSTRING),
  457. ASN1_GEN_STR("NUMERICSTRING", V_ASN1_NUMERICSTRING),
  458. /* Special cases */
  459. ASN1_GEN_STR("SEQUENCE", V_ASN1_SEQUENCE),
  460. ASN1_GEN_STR("SEQ", V_ASN1_SEQUENCE),
  461. ASN1_GEN_STR("SET", V_ASN1_SET),
  462. /* type modifiers */
  463. /* Explicit tag */
  464. ASN1_GEN_STR("EXP", ASN1_GEN_FLAG_EXP),
  465. ASN1_GEN_STR("EXPLICIT", ASN1_GEN_FLAG_EXP),
  466. /* Implicit tag */
  467. ASN1_GEN_STR("IMP", ASN1_GEN_FLAG_IMP),
  468. ASN1_GEN_STR("IMPLICIT", ASN1_GEN_FLAG_IMP),
  469. /* OCTET STRING wrapper */
  470. ASN1_GEN_STR("OCTWRAP", ASN1_GEN_FLAG_OCTWRAP),
  471. /* SEQUENCE wrapper */
  472. ASN1_GEN_STR("SEQWRAP", ASN1_GEN_FLAG_SEQWRAP),
  473. /* SET wrapper */
  474. ASN1_GEN_STR("SETWRAP", ASN1_GEN_FLAG_SETWRAP),
  475. /* BIT STRING wrapper */
  476. ASN1_GEN_STR("BITWRAP", ASN1_GEN_FLAG_BITWRAP),
  477. ASN1_GEN_STR("FORM", ASN1_GEN_FLAG_FORMAT),
  478. ASN1_GEN_STR("FORMAT", ASN1_GEN_FLAG_FORMAT),
  479. };
  480. if (len == -1)
  481. len = strlen(tagstr);
  482. tntmp = tnst;
  483. for (i = 0; i < OSSL_NELEM(tnst); i++, tntmp++) {
  484. if ((len == tntmp->len) && (strncmp(tntmp->strnam, tagstr, len) == 0))
  485. return tntmp->tag;
  486. }
  487. return -1;
  488. }
  489. static ASN1_TYPE *asn1_str2type(const char *str, int format, int utype)
  490. {
  491. ASN1_TYPE *atmp = NULL;
  492. CONF_VALUE vtmp;
  493. unsigned char *rdata;
  494. long rdlen;
  495. int no_unused = 1;
  496. if ((atmp = ASN1_TYPE_new()) == NULL) {
  497. ASN1err(ASN1_F_ASN1_STR2TYPE, ERR_R_MALLOC_FAILURE);
  498. return NULL;
  499. }
  500. if (!str)
  501. str = "";
  502. switch (utype) {
  503. case V_ASN1_NULL:
  504. if (str && *str) {
  505. ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_NULL_VALUE);
  506. goto bad_form;
  507. }
  508. break;
  509. case V_ASN1_BOOLEAN:
  510. if (format != ASN1_GEN_FORMAT_ASCII) {
  511. ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_NOT_ASCII_FORMAT);
  512. goto bad_form;
  513. }
  514. vtmp.name = NULL;
  515. vtmp.section = NULL;
  516. vtmp.value = (char *)str;
  517. if (!X509V3_get_value_bool(&vtmp, &atmp->value.boolean)) {
  518. ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_BOOLEAN);
  519. goto bad_str;
  520. }
  521. break;
  522. case V_ASN1_INTEGER:
  523. case V_ASN1_ENUMERATED:
  524. if (format != ASN1_GEN_FORMAT_ASCII) {
  525. ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_INTEGER_NOT_ASCII_FORMAT);
  526. goto bad_form;
  527. }
  528. if ((atmp->value.integer
  529. = s2i_ASN1_INTEGER(NULL, str)) == NULL) {
  530. ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_INTEGER);
  531. goto bad_str;
  532. }
  533. break;
  534. case V_ASN1_OBJECT:
  535. if (format != ASN1_GEN_FORMAT_ASCII) {
  536. ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_OBJECT_NOT_ASCII_FORMAT);
  537. goto bad_form;
  538. }
  539. if ((atmp->value.object = OBJ_txt2obj(str, 0)) == NULL) {
  540. ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_OBJECT);
  541. goto bad_str;
  542. }
  543. break;
  544. case V_ASN1_UTCTIME:
  545. case V_ASN1_GENERALIZEDTIME:
  546. if (format != ASN1_GEN_FORMAT_ASCII) {
  547. ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_TIME_NOT_ASCII_FORMAT);
  548. goto bad_form;
  549. }
  550. if ((atmp->value.asn1_string = ASN1_STRING_new()) == NULL) {
  551. ASN1err(ASN1_F_ASN1_STR2TYPE, ERR_R_MALLOC_FAILURE);
  552. goto bad_str;
  553. }
  554. if (!ASN1_STRING_set(atmp->value.asn1_string, str, -1)) {
  555. ASN1err(ASN1_F_ASN1_STR2TYPE, ERR_R_MALLOC_FAILURE);
  556. goto bad_str;
  557. }
  558. atmp->value.asn1_string->type = utype;
  559. if (!ASN1_TIME_check(atmp->value.asn1_string)) {
  560. ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_TIME_VALUE);
  561. goto bad_str;
  562. }
  563. break;
  564. case V_ASN1_BMPSTRING:
  565. case V_ASN1_PRINTABLESTRING:
  566. case V_ASN1_IA5STRING:
  567. case V_ASN1_T61STRING:
  568. case V_ASN1_UTF8STRING:
  569. case V_ASN1_VISIBLESTRING:
  570. case V_ASN1_UNIVERSALSTRING:
  571. case V_ASN1_GENERALSTRING:
  572. case V_ASN1_NUMERICSTRING:
  573. if (format == ASN1_GEN_FORMAT_ASCII)
  574. format = MBSTRING_ASC;
  575. else if (format == ASN1_GEN_FORMAT_UTF8)
  576. format = MBSTRING_UTF8;
  577. else {
  578. ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_FORMAT);
  579. goto bad_form;
  580. }
  581. if (ASN1_mbstring_copy(&atmp->value.asn1_string, (unsigned char *)str,
  582. -1, format, ASN1_tag2bit(utype)) <= 0) {
  583. ASN1err(ASN1_F_ASN1_STR2TYPE, ERR_R_MALLOC_FAILURE);
  584. goto bad_str;
  585. }
  586. break;
  587. case V_ASN1_BIT_STRING:
  588. case V_ASN1_OCTET_STRING:
  589. if ((atmp->value.asn1_string = ASN1_STRING_new()) == NULL) {
  590. ASN1err(ASN1_F_ASN1_STR2TYPE, ERR_R_MALLOC_FAILURE);
  591. goto bad_form;
  592. }
  593. if (format == ASN1_GEN_FORMAT_HEX) {
  594. if ((rdata = OPENSSL_hexstr2buf(str, &rdlen)) == NULL) {
  595. ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_HEX);
  596. goto bad_str;
  597. }
  598. atmp->value.asn1_string->data = rdata;
  599. atmp->value.asn1_string->length = rdlen;
  600. atmp->value.asn1_string->type = utype;
  601. } else if (format == ASN1_GEN_FORMAT_ASCII)
  602. ASN1_STRING_set(atmp->value.asn1_string, str, -1);
  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. ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_LIST_ERROR);
  608. goto bad_str;
  609. }
  610. no_unused = 0;
  611. } else {
  612. ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_BITSTRING_FORMAT);
  613. goto bad_form;
  614. }
  615. if ((utype == V_ASN1_BIT_STRING) && no_unused) {
  616. atmp->value.asn1_string->flags
  617. &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  618. atmp->value.asn1_string->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  619. }
  620. break;
  621. default:
  622. ASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_UNSUPPORTED_TYPE);
  623. goto bad_str;
  624. }
  625. atmp->type = utype;
  626. return atmp;
  627. bad_str:
  628. ERR_add_error_data(2, "string=", str);
  629. bad_form:
  630. ASN1_TYPE_free(atmp);
  631. return NULL;
  632. }
  633. static int bitstr_cb(const char *elem, int len, void *bitstr)
  634. {
  635. long bitnum;
  636. char *eptr;
  637. if (!elem)
  638. return 0;
  639. bitnum = strtoul(elem, &eptr, 10);
  640. if (eptr && *eptr && (eptr != elem + len))
  641. return 0;
  642. if (bitnum < 0) {
  643. ASN1err(ASN1_F_BITSTR_CB, ASN1_R_INVALID_NUMBER);
  644. return 0;
  645. }
  646. if (!ASN1_BIT_STRING_set_bit(bitstr, bitnum, 1)) {
  647. ASN1err(ASN1_F_BITSTR_CB, ERR_R_MALLOC_FAILURE);
  648. return 0;
  649. }
  650. return 1;
  651. }
  652. static int mask_cb(const char *elem, int len, void *arg)
  653. {
  654. unsigned long *pmask = arg, tmpmask;
  655. int tag;
  656. if (elem == NULL)
  657. return 0;
  658. if ((len == 3) && (strncmp(elem, "DIR", 3) == 0)) {
  659. *pmask |= B_ASN1_DIRECTORYSTRING;
  660. return 1;
  661. }
  662. tag = asn1_str2tag(elem, len);
  663. if (!tag || (tag & ASN1_GEN_FLAG))
  664. return 0;
  665. tmpmask = ASN1_tag2bit(tag);
  666. if (!tmpmask)
  667. return 0;
  668. *pmask |= tmpmask;
  669. return 1;
  670. }
  671. int ASN1_str2mask(const char *str, unsigned long *pmask)
  672. {
  673. *pmask = 0;
  674. return CONF_parse_list(str, '|', 1, mask_cb, pmask);
  675. }