No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

asn1_gen.c 25 KiB

Fix build when using Visual Studio 2015 Update 1. Many of the compatibility issues are described at https://msdn.microsoft.com/en-us/library/mt612856.aspx. The macros that suppressed warnings on a per-function basis no longer work in Update 1, so replace them with #pragmas. Update 1 warns when |size_t| arguments to |printf| are casted, so stop doing that casting. Unfortunately, this requires an ugly hack to continue working in MSVC 2013 as MSVC 2013 doesn't support "%zu". Finally, Update 1 has new warnings, some of which need to be suppressed. --- Updated by davidben to give up on suppressing warnings in crypto/x509 and crypto/x509v3 as those directories aren't changed much from upstream. In each of these cases, upstream opted just blindly initialize the variable, so do the same. Also switch C4265 to level 4, per Microsoft's recommendation and work around a bug in limits.h that happens to get fixed by Google include order style. (limits.h is sensitive to whether corecrt.h, pulled in by stddef.h and some other headers, is included before it. The reason it affected just one file is we often put the file's header first, which means base.h is pulling in stddef.h. Relying on this is ugly, but it's no worse than what everything else is doing and this doesn't seem worth making something as tame as limits.h so messy to use.) Change-Id: I02d1f935356899f424d3525d03eca401bfa3e6cd Reviewed-on: https://boringssl-review.googlesource.com/7480 Reviewed-by: David Benjamin <davidben@google.com>
hace 8 años
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  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. #include "../x509v3/internal.h"
  65. /*
  66. * Although this file is in crypto/x509 for layering purposes, it emits
  67. * errors from the ASN.1 module for OpenSSL compatibility.
  68. */
  69. #define ASN1_GEN_FLAG 0x10000
  70. #define ASN1_GEN_FLAG_IMP (ASN1_GEN_FLAG|1)
  71. #define ASN1_GEN_FLAG_EXP (ASN1_GEN_FLAG|2)
  72. #define ASN1_GEN_FLAG_TAG (ASN1_GEN_FLAG|3)
  73. #define ASN1_GEN_FLAG_BITWRAP (ASN1_GEN_FLAG|4)
  74. #define ASN1_GEN_FLAG_OCTWRAP (ASN1_GEN_FLAG|5)
  75. #define ASN1_GEN_FLAG_SEQWRAP (ASN1_GEN_FLAG|6)
  76. #define ASN1_GEN_FLAG_SETWRAP (ASN1_GEN_FLAG|7)
  77. #define ASN1_GEN_FLAG_FORMAT (ASN1_GEN_FLAG|8)
  78. #define ASN1_GEN_STR(str,val) {str, sizeof(str) - 1, val}
  79. #define ASN1_FLAG_EXP_MAX 20
  80. /* Maximum number of nested sequences */
  81. #define ASN1_GEN_SEQ_MAX_DEPTH 50
  82. /* Input formats */
  83. /* ASCII: default */
  84. #define ASN1_GEN_FORMAT_ASCII 1
  85. /* UTF8 */
  86. #define ASN1_GEN_FORMAT_UTF8 2
  87. /* Hex */
  88. #define ASN1_GEN_FORMAT_HEX 3
  89. /* List of bits */
  90. #define ASN1_GEN_FORMAT_BITLIST 4
  91. struct tag_name_st {
  92. const char *strnam;
  93. int len;
  94. int tag;
  95. };
  96. typedef struct {
  97. int exp_tag;
  98. int exp_class;
  99. int exp_constructed;
  100. int exp_pad;
  101. long exp_len;
  102. } tag_exp_type;
  103. typedef struct {
  104. int imp_tag;
  105. int imp_class;
  106. int utype;
  107. int format;
  108. const char *str;
  109. tag_exp_type exp_list[ASN1_FLAG_EXP_MAX];
  110. int exp_count;
  111. } tag_exp_arg;
  112. static ASN1_TYPE *generate_v3(char *str, X509V3_CTX *cnf, int depth,
  113. int *perr);
  114. static int bitstr_cb(const char *elem, int len, void *bitstr);
  115. static int asn1_cb(const char *elem, int len, void *bitstr);
  116. static int append_exp(tag_exp_arg *arg, int exp_tag, int exp_class,
  117. int exp_constructed, int exp_pad, int imp_ok);
  118. static int parse_tagging(const char *vstart, int vlen, int *ptag,
  119. int *pclass);
  120. static ASN1_TYPE *asn1_multi(int utype, const char *section, X509V3_CTX *cnf,
  121. int depth, int *perr);
  122. static ASN1_TYPE *asn1_str2type(const char *str, int format, int utype);
  123. static int asn1_str2tag(const char *tagstr, int len);
  124. ASN1_TYPE *ASN1_generate_nconf(char *str, CONF *nconf)
  125. {
  126. X509V3_CTX cnf;
  127. if (!nconf)
  128. return ASN1_generate_v3(str, NULL);
  129. X509V3_set_nconf(&cnf, nconf);
  130. return ASN1_generate_v3(str, &cnf);
  131. }
  132. ASN1_TYPE *ASN1_generate_v3(char *str, X509V3_CTX *cnf)
  133. {
  134. int err = 0;
  135. ASN1_TYPE *ret = generate_v3(str, cnf, 0, &err);
  136. if (err)
  137. OPENSSL_PUT_ERROR(ASN1, err);
  138. return ret;
  139. }
  140. static ASN1_TYPE *generate_v3(char *str, X509V3_CTX *cnf, int depth,
  141. int *perr)
  142. {
  143. ASN1_TYPE *ret;
  144. tag_exp_arg asn1_tags;
  145. tag_exp_type *etmp;
  146. int i, len;
  147. unsigned char *orig_der = NULL, *new_der = NULL;
  148. const unsigned char *cpy_start;
  149. unsigned char *p;
  150. const unsigned char *cp;
  151. int cpy_len;
  152. long hdr_len = 0;
  153. int hdr_constructed = 0, hdr_tag, hdr_class;
  154. int r;
  155. asn1_tags.imp_tag = -1;
  156. asn1_tags.imp_class = -1;
  157. asn1_tags.format = ASN1_GEN_FORMAT_ASCII;
  158. asn1_tags.exp_count = 0;
  159. if (CONF_parse_list(str, ',', 1, asn1_cb, &asn1_tags) != 0) {
  160. *perr = ASN1_R_UNKNOWN_TAG;
  161. return NULL;
  162. }
  163. if ((asn1_tags.utype == V_ASN1_SEQUENCE)
  164. || (asn1_tags.utype == V_ASN1_SET)) {
  165. if (!cnf) {
  166. *perr = ASN1_R_SEQUENCE_OR_SET_NEEDS_CONFIG;
  167. return NULL;
  168. }
  169. if (depth >= ASN1_GEN_SEQ_MAX_DEPTH) {
  170. *perr = ASN1_R_ILLEGAL_NESTED_TAGGING;
  171. return NULL;
  172. }
  173. ret = asn1_multi(asn1_tags.utype, asn1_tags.str, cnf, depth, perr);
  174. } else
  175. ret = asn1_str2type(asn1_tags.str, asn1_tags.format, asn1_tags.utype);
  176. if (!ret)
  177. return NULL;
  178. /* If no tagging return base type */
  179. if ((asn1_tags.imp_tag == -1) && (asn1_tags.exp_count == 0))
  180. return ret;
  181. /* Generate the encoding */
  182. cpy_len = i2d_ASN1_TYPE(ret, &orig_der);
  183. ASN1_TYPE_free(ret);
  184. ret = NULL;
  185. /* Set point to start copying for modified encoding */
  186. cpy_start = orig_der;
  187. /* Do we need IMPLICIT tagging? */
  188. if (asn1_tags.imp_tag != -1) {
  189. /* If IMPLICIT we will replace the underlying tag */
  190. /* Skip existing tag+len */
  191. r = ASN1_get_object(&cpy_start, &hdr_len, &hdr_tag, &hdr_class,
  192. cpy_len);
  193. if (r & 0x80)
  194. goto err;
  195. /* Update copy length */
  196. cpy_len -= cpy_start - orig_der;
  197. /*
  198. * For IMPLICIT tagging the length should match the original length
  199. * and constructed flag should be consistent.
  200. */
  201. if (r & 0x1) {
  202. /* Indefinite length constructed */
  203. hdr_constructed = 2;
  204. hdr_len = 0;
  205. } else
  206. /* Just retain constructed flag */
  207. hdr_constructed = r & V_ASN1_CONSTRUCTED;
  208. /*
  209. * Work out new length with IMPLICIT tag: ignore constructed because
  210. * it will mess up if indefinite length
  211. */
  212. len = ASN1_object_size(0, hdr_len, asn1_tags.imp_tag);
  213. } else
  214. len = cpy_len;
  215. /* Work out length in any EXPLICIT, starting from end */
  216. for (i = 0, etmp = asn1_tags.exp_list + asn1_tags.exp_count - 1;
  217. i < asn1_tags.exp_count; i++, etmp--) {
  218. /* Content length: number of content octets + any padding */
  219. len += etmp->exp_pad;
  220. etmp->exp_len = len;
  221. /* Total object length: length including new header */
  222. len = ASN1_object_size(0, len, etmp->exp_tag);
  223. }
  224. /* Allocate buffer for new encoding */
  225. new_der = OPENSSL_malloc(len);
  226. if (!new_der)
  227. goto err;
  228. /* Generate tagged encoding */
  229. p = new_der;
  230. /* Output explicit tags first */
  231. for (i = 0, etmp = asn1_tags.exp_list; i < asn1_tags.exp_count;
  232. i++, etmp++) {
  233. ASN1_put_object(&p, etmp->exp_constructed, etmp->exp_len,
  234. etmp->exp_tag, etmp->exp_class);
  235. if (etmp->exp_pad)
  236. *p++ = 0;
  237. }
  238. /* If IMPLICIT, output tag */
  239. if (asn1_tags.imp_tag != -1) {
  240. if (asn1_tags.imp_class == V_ASN1_UNIVERSAL
  241. && (asn1_tags.imp_tag == V_ASN1_SEQUENCE
  242. || asn1_tags.imp_tag == V_ASN1_SET))
  243. hdr_constructed = V_ASN1_CONSTRUCTED;
  244. ASN1_put_object(&p, hdr_constructed, hdr_len,
  245. asn1_tags.imp_tag, asn1_tags.imp_class);
  246. }
  247. /* Copy across original encoding */
  248. OPENSSL_memcpy(p, cpy_start, cpy_len);
  249. cp = new_der;
  250. /* Obtain new ASN1_TYPE structure */
  251. ret = d2i_ASN1_TYPE(NULL, &cp, len);
  252. err:
  253. if (orig_der)
  254. OPENSSL_free(orig_der);
  255. if (new_der)
  256. OPENSSL_free(new_der);
  257. return ret;
  258. }
  259. static int asn1_cb(const char *elem, int len, void *bitstr)
  260. {
  261. tag_exp_arg *arg = bitstr;
  262. int i;
  263. int utype;
  264. int vlen = 0;
  265. const char *p, *vstart = NULL;
  266. int tmp_tag, tmp_class;
  267. if (elem == NULL)
  268. return -1;
  269. for (i = 0, p = elem; i < len; p++, i++) {
  270. /* Look for the ':' in name value pairs */
  271. if (*p == ':') {
  272. vstart = p + 1;
  273. vlen = len - (vstart - elem);
  274. len = p - elem;
  275. break;
  276. }
  277. }
  278. utype = asn1_str2tag(elem, len);
  279. if (utype == -1) {
  280. OPENSSL_PUT_ERROR(ASN1, ASN1_R_UNKNOWN_TAG);
  281. ERR_add_error_data(2, "tag=", elem);
  282. return -1;
  283. }
  284. /* If this is not a modifier mark end of string and exit */
  285. if (!(utype & ASN1_GEN_FLAG)) {
  286. arg->utype = utype;
  287. arg->str = vstart;
  288. /* If no value and not end of string, error */
  289. if (!vstart && elem[len]) {
  290. OPENSSL_PUT_ERROR(ASN1, ASN1_R_MISSING_VALUE);
  291. return -1;
  292. }
  293. return 0;
  294. }
  295. switch (utype) {
  296. case ASN1_GEN_FLAG_IMP:
  297. /* Check for illegal multiple IMPLICIT tagging */
  298. if (arg->imp_tag != -1) {
  299. OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_NESTED_TAGGING);
  300. return -1;
  301. }
  302. if (!parse_tagging(vstart, vlen, &arg->imp_tag, &arg->imp_class))
  303. return -1;
  304. break;
  305. case ASN1_GEN_FLAG_EXP:
  306. if (!parse_tagging(vstart, vlen, &tmp_tag, &tmp_class))
  307. return -1;
  308. if (!append_exp(arg, tmp_tag, tmp_class, 1, 0, 0))
  309. return -1;
  310. break;
  311. case ASN1_GEN_FLAG_SEQWRAP:
  312. if (!append_exp(arg, V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL, 1, 0, 1))
  313. return -1;
  314. break;
  315. case ASN1_GEN_FLAG_SETWRAP:
  316. if (!append_exp(arg, V_ASN1_SET, V_ASN1_UNIVERSAL, 1, 0, 1))
  317. return -1;
  318. break;
  319. case ASN1_GEN_FLAG_BITWRAP:
  320. if (!append_exp(arg, V_ASN1_BIT_STRING, V_ASN1_UNIVERSAL, 0, 1, 1))
  321. return -1;
  322. break;
  323. case ASN1_GEN_FLAG_OCTWRAP:
  324. if (!append_exp(arg, V_ASN1_OCTET_STRING, V_ASN1_UNIVERSAL, 0, 0, 1))
  325. return -1;
  326. break;
  327. case ASN1_GEN_FLAG_FORMAT:
  328. if (!vstart) {
  329. OPENSSL_PUT_ERROR(ASN1, ASN1_R_UNKNOWN_FORMAT);
  330. return -1;
  331. }
  332. if (!strncmp(vstart, "ASCII", 5))
  333. arg->format = ASN1_GEN_FORMAT_ASCII;
  334. else if (!strncmp(vstart, "UTF8", 4))
  335. arg->format = ASN1_GEN_FORMAT_UTF8;
  336. else if (!strncmp(vstart, "HEX", 3))
  337. arg->format = ASN1_GEN_FORMAT_HEX;
  338. else if (!strncmp(vstart, "BITLIST", 7))
  339. arg->format = ASN1_GEN_FORMAT_BITLIST;
  340. else {
  341. OPENSSL_PUT_ERROR(ASN1, ASN1_R_UNKNOWN_FORMAT);
  342. return -1;
  343. }
  344. break;
  345. }
  346. return 1;
  347. }
  348. static int parse_tagging(const char *vstart, int vlen, int *ptag, int *pclass)
  349. {
  350. char erch[2];
  351. long tag_num;
  352. char *eptr;
  353. if (!vstart)
  354. return 0;
  355. tag_num = strtoul(vstart, &eptr, 10);
  356. /* Check we haven't gone past max length: should be impossible */
  357. if (eptr && *eptr && (eptr > vstart + vlen))
  358. return 0;
  359. if (tag_num < 0) {
  360. OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_NUMBER);
  361. return 0;
  362. }
  363. *ptag = tag_num;
  364. /* If we have non numeric characters, parse them */
  365. if (eptr)
  366. vlen -= eptr - vstart;
  367. else
  368. vlen = 0;
  369. if (vlen) {
  370. switch (*eptr) {
  371. case 'U':
  372. *pclass = V_ASN1_UNIVERSAL;
  373. break;
  374. case 'A':
  375. *pclass = V_ASN1_APPLICATION;
  376. break;
  377. case 'P':
  378. *pclass = V_ASN1_PRIVATE;
  379. break;
  380. case 'C':
  381. *pclass = V_ASN1_CONTEXT_SPECIFIC;
  382. break;
  383. default:
  384. erch[0] = *eptr;
  385. erch[1] = 0;
  386. OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_MODIFIER);
  387. ERR_add_error_data(2, "Char=", erch);
  388. return 0;
  389. break;
  390. }
  391. } else
  392. *pclass = V_ASN1_CONTEXT_SPECIFIC;
  393. return 1;
  394. }
  395. /* Handle multiple types: SET and SEQUENCE */
  396. static ASN1_TYPE *asn1_multi(int utype, const char *section, X509V3_CTX *cnf,
  397. int depth, int *perr)
  398. {
  399. ASN1_TYPE *ret = NULL;
  400. STACK_OF(ASN1_TYPE) *sk = NULL;
  401. STACK_OF(CONF_VALUE) *sect = NULL;
  402. unsigned char *der = NULL;
  403. int derlen;
  404. size_t i;
  405. sk = sk_ASN1_TYPE_new_null();
  406. if (!sk)
  407. goto bad;
  408. if (section) {
  409. if (!cnf)
  410. goto bad;
  411. sect = X509V3_get_section(cnf, (char *)section);
  412. if (!sect)
  413. goto bad;
  414. for (i = 0; i < sk_CONF_VALUE_num(sect); i++) {
  415. ASN1_TYPE *typ =
  416. generate_v3(sk_CONF_VALUE_value(sect, i)->value, cnf,
  417. depth + 1, perr);
  418. if (!typ)
  419. goto bad;
  420. if (!sk_ASN1_TYPE_push(sk, typ))
  421. goto bad;
  422. }
  423. }
  424. /*
  425. * Now we has a STACK of the components, convert to the correct form
  426. */
  427. if (utype == V_ASN1_SET)
  428. derlen = i2d_ASN1_SET_ANY(sk, &der);
  429. else
  430. derlen = i2d_ASN1_SEQUENCE_ANY(sk, &der);
  431. if (derlen < 0)
  432. goto bad;
  433. if (!(ret = ASN1_TYPE_new()))
  434. goto bad;
  435. if (!(ret->value.asn1_string = ASN1_STRING_type_new(utype)))
  436. goto bad;
  437. ret->type = utype;
  438. ret->value.asn1_string->data = der;
  439. ret->value.asn1_string->length = derlen;
  440. der = NULL;
  441. bad:
  442. if (der)
  443. OPENSSL_free(der);
  444. if (sk)
  445. sk_ASN1_TYPE_pop_free(sk, ASN1_TYPE_free);
  446. if (sect)
  447. X509V3_section_free(cnf, sect);
  448. return ret;
  449. }
  450. static int append_exp(tag_exp_arg *arg, int exp_tag, int exp_class,
  451. int exp_constructed, int exp_pad, int imp_ok)
  452. {
  453. tag_exp_type *exp_tmp;
  454. /* Can only have IMPLICIT if permitted */
  455. if ((arg->imp_tag != -1) && !imp_ok) {
  456. OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_IMPLICIT_TAG);
  457. return 0;
  458. }
  459. if (arg->exp_count == ASN1_FLAG_EXP_MAX) {
  460. OPENSSL_PUT_ERROR(ASN1, ASN1_R_DEPTH_EXCEEDED);
  461. return 0;
  462. }
  463. exp_tmp = &arg->exp_list[arg->exp_count++];
  464. /*
  465. * If IMPLICIT set tag to implicit value then reset implicit tag since it
  466. * has been used.
  467. */
  468. if (arg->imp_tag != -1) {
  469. exp_tmp->exp_tag = arg->imp_tag;
  470. exp_tmp->exp_class = arg->imp_class;
  471. arg->imp_tag = -1;
  472. arg->imp_class = -1;
  473. } else {
  474. exp_tmp->exp_tag = exp_tag;
  475. exp_tmp->exp_class = exp_class;
  476. }
  477. exp_tmp->exp_constructed = exp_constructed;
  478. exp_tmp->exp_pad = exp_pad;
  479. return 1;
  480. }
  481. static int asn1_str2tag(const char *tagstr, int len)
  482. {
  483. unsigned int i;
  484. static const struct tag_name_st *tntmp, tnst[] = {
  485. ASN1_GEN_STR("BOOL", V_ASN1_BOOLEAN),
  486. ASN1_GEN_STR("BOOLEAN", V_ASN1_BOOLEAN),
  487. ASN1_GEN_STR("NULL", V_ASN1_NULL),
  488. ASN1_GEN_STR("INT", V_ASN1_INTEGER),
  489. ASN1_GEN_STR("INTEGER", V_ASN1_INTEGER),
  490. ASN1_GEN_STR("ENUM", V_ASN1_ENUMERATED),
  491. ASN1_GEN_STR("ENUMERATED", V_ASN1_ENUMERATED),
  492. ASN1_GEN_STR("OID", V_ASN1_OBJECT),
  493. ASN1_GEN_STR("OBJECT", V_ASN1_OBJECT),
  494. ASN1_GEN_STR("UTCTIME", V_ASN1_UTCTIME),
  495. ASN1_GEN_STR("UTC", V_ASN1_UTCTIME),
  496. ASN1_GEN_STR("GENERALIZEDTIME", V_ASN1_GENERALIZEDTIME),
  497. ASN1_GEN_STR("GENTIME", V_ASN1_GENERALIZEDTIME),
  498. ASN1_GEN_STR("OCT", V_ASN1_OCTET_STRING),
  499. ASN1_GEN_STR("OCTETSTRING", V_ASN1_OCTET_STRING),
  500. ASN1_GEN_STR("BITSTR", V_ASN1_BIT_STRING),
  501. ASN1_GEN_STR("BITSTRING", V_ASN1_BIT_STRING),
  502. ASN1_GEN_STR("UNIVERSALSTRING", V_ASN1_UNIVERSALSTRING),
  503. ASN1_GEN_STR("UNIV", V_ASN1_UNIVERSALSTRING),
  504. ASN1_GEN_STR("IA5", V_ASN1_IA5STRING),
  505. ASN1_GEN_STR("IA5STRING", V_ASN1_IA5STRING),
  506. ASN1_GEN_STR("UTF8", V_ASN1_UTF8STRING),
  507. ASN1_GEN_STR("UTF8String", V_ASN1_UTF8STRING),
  508. ASN1_GEN_STR("BMP", V_ASN1_BMPSTRING),
  509. ASN1_GEN_STR("BMPSTRING", V_ASN1_BMPSTRING),
  510. ASN1_GEN_STR("VISIBLESTRING", V_ASN1_VISIBLESTRING),
  511. ASN1_GEN_STR("VISIBLE", V_ASN1_VISIBLESTRING),
  512. ASN1_GEN_STR("PRINTABLESTRING", V_ASN1_PRINTABLESTRING),
  513. ASN1_GEN_STR("PRINTABLE", V_ASN1_PRINTABLESTRING),
  514. ASN1_GEN_STR("T61", V_ASN1_T61STRING),
  515. ASN1_GEN_STR("T61STRING", V_ASN1_T61STRING),
  516. ASN1_GEN_STR("TELETEXSTRING", V_ASN1_T61STRING),
  517. ASN1_GEN_STR("GeneralString", V_ASN1_GENERALSTRING),
  518. ASN1_GEN_STR("GENSTR", V_ASN1_GENERALSTRING),
  519. ASN1_GEN_STR("NUMERIC", V_ASN1_NUMERICSTRING),
  520. ASN1_GEN_STR("NUMERICSTRING", V_ASN1_NUMERICSTRING),
  521. /* Special cases */
  522. ASN1_GEN_STR("SEQUENCE", V_ASN1_SEQUENCE),
  523. ASN1_GEN_STR("SEQ", V_ASN1_SEQUENCE),
  524. ASN1_GEN_STR("SET", V_ASN1_SET),
  525. /* type modifiers */
  526. /* Explicit tag */
  527. ASN1_GEN_STR("EXP", ASN1_GEN_FLAG_EXP),
  528. ASN1_GEN_STR("EXPLICIT", ASN1_GEN_FLAG_EXP),
  529. /* Implicit tag */
  530. ASN1_GEN_STR("IMP", ASN1_GEN_FLAG_IMP),
  531. ASN1_GEN_STR("IMPLICIT", ASN1_GEN_FLAG_IMP),
  532. /* OCTET STRING wrapper */
  533. ASN1_GEN_STR("OCTWRAP", ASN1_GEN_FLAG_OCTWRAP),
  534. /* SEQUENCE wrapper */
  535. ASN1_GEN_STR("SEQWRAP", ASN1_GEN_FLAG_SEQWRAP),
  536. /* SET wrapper */
  537. ASN1_GEN_STR("SETWRAP", ASN1_GEN_FLAG_SETWRAP),
  538. /* BIT STRING wrapper */
  539. ASN1_GEN_STR("BITWRAP", ASN1_GEN_FLAG_BITWRAP),
  540. ASN1_GEN_STR("FORM", ASN1_GEN_FLAG_FORMAT),
  541. ASN1_GEN_STR("FORMAT", ASN1_GEN_FLAG_FORMAT),
  542. };
  543. if (len == -1)
  544. len = strlen(tagstr);
  545. tntmp = tnst;
  546. for (i = 0; i < sizeof(tnst) / sizeof(struct tag_name_st); i++, tntmp++) {
  547. if ((len == tntmp->len) && !strncmp(tntmp->strnam, tagstr, len))
  548. return tntmp->tag;
  549. }
  550. return -1;
  551. }
  552. static ASN1_TYPE *asn1_str2type(const char *str, int format, int utype)
  553. {
  554. ASN1_TYPE *atmp = NULL;
  555. CONF_VALUE vtmp;
  556. unsigned char *rdata;
  557. long rdlen;
  558. int no_unused = 1;
  559. if (!(atmp = ASN1_TYPE_new())) {
  560. OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
  561. return NULL;
  562. }
  563. if (!str)
  564. str = "";
  565. switch (utype) {
  566. case V_ASN1_NULL:
  567. if (str && *str) {
  568. OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_NULL_VALUE);
  569. goto bad_form;
  570. }
  571. break;
  572. case V_ASN1_BOOLEAN:
  573. if (format != ASN1_GEN_FORMAT_ASCII) {
  574. OPENSSL_PUT_ERROR(ASN1, ASN1_R_NOT_ASCII_FORMAT);
  575. goto bad_form;
  576. }
  577. vtmp.name = NULL;
  578. vtmp.section = NULL;
  579. vtmp.value = (char *)str;
  580. if (!X509V3_get_value_bool(&vtmp, &atmp->value.boolean)) {
  581. OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_BOOLEAN);
  582. goto bad_str;
  583. }
  584. break;
  585. case V_ASN1_INTEGER:
  586. case V_ASN1_ENUMERATED:
  587. if (format != ASN1_GEN_FORMAT_ASCII) {
  588. OPENSSL_PUT_ERROR(ASN1, ASN1_R_INTEGER_NOT_ASCII_FORMAT);
  589. goto bad_form;
  590. }
  591. if (!(atmp->value.integer = s2i_ASN1_INTEGER(NULL, (char *)str))) {
  592. OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_INTEGER);
  593. goto bad_str;
  594. }
  595. break;
  596. case V_ASN1_OBJECT:
  597. if (format != ASN1_GEN_FORMAT_ASCII) {
  598. OPENSSL_PUT_ERROR(ASN1, ASN1_R_OBJECT_NOT_ASCII_FORMAT);
  599. goto bad_form;
  600. }
  601. if (!(atmp->value.object = OBJ_txt2obj(str, 0))) {
  602. OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_OBJECT);
  603. goto bad_str;
  604. }
  605. break;
  606. case V_ASN1_UTCTIME:
  607. case V_ASN1_GENERALIZEDTIME:
  608. if (format != ASN1_GEN_FORMAT_ASCII) {
  609. OPENSSL_PUT_ERROR(ASN1, ASN1_R_TIME_NOT_ASCII_FORMAT);
  610. goto bad_form;
  611. }
  612. if (!(atmp->value.asn1_string = ASN1_STRING_new())) {
  613. OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
  614. goto bad_str;
  615. }
  616. if (!ASN1_STRING_set(atmp->value.asn1_string, str, -1)) {
  617. OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
  618. goto bad_str;
  619. }
  620. atmp->value.asn1_string->type = utype;
  621. if (!ASN1_TIME_check(atmp->value.asn1_string)) {
  622. OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_TIME_VALUE);
  623. goto bad_str;
  624. }
  625. break;
  626. case V_ASN1_BMPSTRING:
  627. case V_ASN1_PRINTABLESTRING:
  628. case V_ASN1_IA5STRING:
  629. case V_ASN1_T61STRING:
  630. case V_ASN1_UTF8STRING:
  631. case V_ASN1_VISIBLESTRING:
  632. case V_ASN1_UNIVERSALSTRING:
  633. case V_ASN1_GENERALSTRING:
  634. case V_ASN1_NUMERICSTRING:
  635. if (format == ASN1_GEN_FORMAT_ASCII)
  636. format = MBSTRING_ASC;
  637. else if (format == ASN1_GEN_FORMAT_UTF8)
  638. format = MBSTRING_UTF8;
  639. else {
  640. OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_FORMAT);
  641. goto bad_form;
  642. }
  643. if (ASN1_mbstring_copy(&atmp->value.asn1_string, (unsigned char *)str,
  644. -1, format, ASN1_tag2bit(utype)) <= 0) {
  645. OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
  646. goto bad_str;
  647. }
  648. break;
  649. case V_ASN1_BIT_STRING:
  650. case V_ASN1_OCTET_STRING:
  651. if (!(atmp->value.asn1_string = ASN1_STRING_new())) {
  652. OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
  653. goto bad_form;
  654. }
  655. if (format == ASN1_GEN_FORMAT_HEX) {
  656. if (!(rdata = x509v3_hex_to_bytes((char *)str, &rdlen))) {
  657. OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_HEX);
  658. goto bad_str;
  659. }
  660. atmp->value.asn1_string->data = rdata;
  661. atmp->value.asn1_string->length = rdlen;
  662. atmp->value.asn1_string->type = utype;
  663. } else if (format == ASN1_GEN_FORMAT_ASCII)
  664. ASN1_STRING_set(atmp->value.asn1_string, str, -1);
  665. else if ((format == ASN1_GEN_FORMAT_BITLIST)
  666. && (utype == V_ASN1_BIT_STRING)) {
  667. if (!CONF_parse_list
  668. (str, ',', 1, bitstr_cb, atmp->value.bit_string)) {
  669. OPENSSL_PUT_ERROR(ASN1, ASN1_R_LIST_ERROR);
  670. goto bad_str;
  671. }
  672. no_unused = 0;
  673. } else {
  674. OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_BITSTRING_FORMAT);
  675. goto bad_form;
  676. }
  677. if ((utype == V_ASN1_BIT_STRING) && no_unused) {
  678. atmp->value.asn1_string->flags
  679. &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  680. atmp->value.asn1_string->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  681. }
  682. break;
  683. default:
  684. OPENSSL_PUT_ERROR(ASN1, ASN1_R_UNSUPPORTED_TYPE);
  685. goto bad_str;
  686. break;
  687. }
  688. atmp->type = utype;
  689. return atmp;
  690. bad_str:
  691. ERR_add_error_data(2, "string=", str);
  692. bad_form:
  693. ASN1_TYPE_free(atmp);
  694. return NULL;
  695. }
  696. static int bitstr_cb(const char *elem, int len, void *bitstr)
  697. {
  698. long bitnum;
  699. char *eptr;
  700. if (!elem)
  701. return 0;
  702. bitnum = strtoul(elem, &eptr, 10);
  703. if (eptr && *eptr && (eptr != elem + len))
  704. return 0;
  705. if (bitnum < 0) {
  706. OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_NUMBER);
  707. return 0;
  708. }
  709. if (!ASN1_BIT_STRING_set_bit(bitstr, bitnum, 1)) {
  710. OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
  711. return 0;
  712. }
  713. return 1;
  714. }