Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

1222 linhas
39 KiB

  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/asn1.h>
  57. #include <string.h>
  58. #include <openssl/asn1t.h>
  59. #include <openssl/buf.h>
  60. #include <openssl/err.h>
  61. #include <openssl/mem.h>
  62. #include "../internal.h"
  63. static int asn1_check_eoc(const unsigned char **in, long len);
  64. static int asn1_find_end(const unsigned char **in, long len, char inf);
  65. static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
  66. char inf, int tag, int aclass, int depth);
  67. static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen);
  68. static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
  69. char *inf, char *cst,
  70. const unsigned char **in, long len,
  71. int exptag, int expclass, char opt, ASN1_TLC *ctx);
  72. static int asn1_template_ex_d2i(ASN1_VALUE **pval,
  73. const unsigned char **in, long len,
  74. const ASN1_TEMPLATE *tt, char opt,
  75. ASN1_TLC *ctx);
  76. static int asn1_template_noexp_d2i(ASN1_VALUE **val,
  77. const unsigned char **in, long len,
  78. const ASN1_TEMPLATE *tt, char opt,
  79. ASN1_TLC *ctx);
  80. static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
  81. const unsigned char **in, long len,
  82. const ASN1_ITEM *it,
  83. int tag, int aclass, char opt,
  84. ASN1_TLC *ctx);
  85. /* Table to convert tags to bit values, used for MSTRING type */
  86. static const unsigned long tag2bit[32] = {
  87. 0, 0, 0, B_ASN1_BIT_STRING, /* tags 0 - 3 */
  88. B_ASN1_OCTET_STRING, 0, 0, B_ASN1_UNKNOWN, /* tags 4- 7 */
  89. B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, /* tags
  90. * 8-11 */
  91. B_ASN1_UTF8STRING, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, /* tags
  92. * 12-15
  93. */
  94. B_ASN1_SEQUENCE, 0, B_ASN1_NUMERICSTRING, B_ASN1_PRINTABLESTRING, /* tags
  95. * 16-19
  96. */
  97. B_ASN1_T61STRING, B_ASN1_VIDEOTEXSTRING, B_ASN1_IA5STRING, /* tags 20-22 */
  98. B_ASN1_UTCTIME, B_ASN1_GENERALIZEDTIME, /* tags 23-24 */
  99. B_ASN1_GRAPHICSTRING, B_ASN1_ISO64STRING, B_ASN1_GENERALSTRING, /* tags
  100. * 25-27 */
  101. B_ASN1_UNIVERSALSTRING, B_ASN1_UNKNOWN, B_ASN1_BMPSTRING, B_ASN1_UNKNOWN, /* tags
  102. * 28-31
  103. */
  104. };
  105. unsigned long ASN1_tag2bit(int tag)
  106. {
  107. if ((tag < 0) || (tag > 30))
  108. return 0;
  109. return tag2bit[tag];
  110. }
  111. /* Macro to initialize and invalidate the cache */
  112. #define asn1_tlc_clear(c) if (c) (c)->valid = 0
  113. /* Version to avoid compiler warning about 'c' always non-NULL */
  114. #define asn1_tlc_clear_nc(c) (c)->valid = 0
  115. /*
  116. * Decode an ASN1 item, this currently behaves just like a standard 'd2i'
  117. * function. 'in' points to a buffer to read the data from, in future we
  118. * will have more advanced versions that can input data a piece at a time and
  119. * this will simply be a special case.
  120. */
  121. ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval,
  122. const unsigned char **in, long len,
  123. const ASN1_ITEM *it)
  124. {
  125. ASN1_TLC c;
  126. ASN1_VALUE *ptmpval = NULL;
  127. if (!pval)
  128. pval = &ptmpval;
  129. asn1_tlc_clear_nc(&c);
  130. if (ASN1_item_ex_d2i(pval, in, len, it, -1, 0, 0, &c) > 0)
  131. return *pval;
  132. return NULL;
  133. }
  134. int ASN1_template_d2i(ASN1_VALUE **pval,
  135. const unsigned char **in, long len,
  136. const ASN1_TEMPLATE *tt)
  137. {
  138. ASN1_TLC c;
  139. asn1_tlc_clear_nc(&c);
  140. return asn1_template_ex_d2i(pval, in, len, tt, 0, &c);
  141. }
  142. /*
  143. * Decode an item, taking care of IMPLICIT tagging, if any. If 'opt' set and
  144. * tag mismatch return -1 to handle OPTIONAL
  145. */
  146. int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
  147. const ASN1_ITEM *it,
  148. int tag, int aclass, char opt, ASN1_TLC *ctx)
  149. {
  150. const ASN1_TEMPLATE *tt, *errtt = NULL;
  151. const ASN1_COMPAT_FUNCS *cf;
  152. const ASN1_EXTERN_FUNCS *ef;
  153. const ASN1_AUX *aux = it->funcs;
  154. ASN1_aux_cb *asn1_cb;
  155. const unsigned char *p = NULL, *q;
  156. unsigned char *wp = NULL; /* BIG FAT WARNING! BREAKS CONST WHERE USED */
  157. unsigned char imphack = 0, oclass;
  158. char seq_eoc, seq_nolen, cst, isopt;
  159. long tmplen;
  160. int i;
  161. int otag;
  162. int ret = 0;
  163. ASN1_VALUE **pchptr, *ptmpval;
  164. int combine = aclass & ASN1_TFLG_COMBINE;
  165. if (!pval)
  166. return 0;
  167. if (aux && aux->asn1_cb)
  168. asn1_cb = aux->asn1_cb;
  169. else
  170. asn1_cb = 0;
  171. switch (it->itype) {
  172. case ASN1_ITYPE_PRIMITIVE:
  173. if (it->templates) {
  174. /*
  175. * tagging or OPTIONAL is currently illegal on an item template
  176. * because the flags can't get passed down. In practice this
  177. * isn't a problem: we include the relevant flags from the item
  178. * template in the template itself.
  179. */
  180. if ((tag != -1) || opt) {
  181. OPENSSL_PUT_ERROR(ASN1,
  182. ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE);
  183. goto err;
  184. }
  185. return asn1_template_ex_d2i(pval, in, len,
  186. it->templates, opt, ctx);
  187. }
  188. return asn1_d2i_ex_primitive(pval, in, len, it,
  189. tag, aclass, opt, ctx);
  190. break;
  191. case ASN1_ITYPE_MSTRING:
  192. p = *in;
  193. /* Just read in tag and class */
  194. ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
  195. &p, len, -1, 0, 1, ctx);
  196. if (!ret) {
  197. OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR);
  198. goto err;
  199. }
  200. /* Must be UNIVERSAL class */
  201. if (oclass != V_ASN1_UNIVERSAL) {
  202. /* If OPTIONAL, assume this is OK */
  203. if (opt)
  204. return -1;
  205. OPENSSL_PUT_ERROR(ASN1, ASN1_R_MSTRING_NOT_UNIVERSAL);
  206. goto err;
  207. }
  208. /* Check tag matches bit map */
  209. if (!(ASN1_tag2bit(otag) & it->utype)) {
  210. /* If OPTIONAL, assume this is OK */
  211. if (opt)
  212. return -1;
  213. OPENSSL_PUT_ERROR(ASN1, ASN1_R_MSTRING_WRONG_TAG);
  214. goto err;
  215. }
  216. return asn1_d2i_ex_primitive(pval, in, len, it, otag, 0, 0, ctx);
  217. case ASN1_ITYPE_EXTERN:
  218. /* Use new style d2i */
  219. ef = it->funcs;
  220. return ef->asn1_ex_d2i(pval, in, len, it, tag, aclass, opt, ctx);
  221. case ASN1_ITYPE_COMPAT:
  222. /* we must resort to old style evil hackery */
  223. cf = it->funcs;
  224. /* If OPTIONAL see if it is there */
  225. if (opt) {
  226. int exptag;
  227. p = *in;
  228. if (tag == -1)
  229. exptag = it->utype;
  230. else
  231. exptag = tag;
  232. /*
  233. * Don't care about anything other than presence of expected tag
  234. */
  235. ret = asn1_check_tlen(NULL, NULL, NULL, NULL, NULL,
  236. &p, len, exptag, aclass, 1, ctx);
  237. if (!ret) {
  238. OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR);
  239. goto err;
  240. }
  241. if (ret == -1)
  242. return -1;
  243. }
  244. /*
  245. * This is the old style evil hack IMPLICIT handling: since the
  246. * underlying code is expecting a tag and class other than the one
  247. * present we change the buffer temporarily then change it back
  248. * afterwards. This doesn't and never did work for tags > 30. Yes
  249. * this is *horrible* but it is only needed for old style d2i which
  250. * will hopefully not be around for much longer. FIXME: should copy
  251. * the buffer then modify it so the input buffer can be const: we
  252. * should *always* copy because the old style d2i might modify the
  253. * buffer.
  254. */
  255. if (tag != -1) {
  256. wp = *(unsigned char **)in;
  257. imphack = *wp;
  258. if (p == NULL) {
  259. OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR);
  260. goto err;
  261. }
  262. *wp = (unsigned char)((*p & V_ASN1_CONSTRUCTED)
  263. | it->utype);
  264. }
  265. ptmpval = cf->asn1_d2i(pval, in, len);
  266. if (tag != -1)
  267. *wp = imphack;
  268. if (ptmpval)
  269. return 1;
  270. OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR);
  271. goto err;
  272. case ASN1_ITYPE_CHOICE:
  273. if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
  274. goto auxerr;
  275. if (*pval) {
  276. /* Free up and zero CHOICE value if initialised */
  277. i = asn1_get_choice_selector(pval, it);
  278. if ((i >= 0) && (i < it->tcount)) {
  279. tt = it->templates + i;
  280. pchptr = asn1_get_field_ptr(pval, tt);
  281. ASN1_template_free(pchptr, tt);
  282. asn1_set_choice_selector(pval, -1, it);
  283. }
  284. } else if (!ASN1_item_ex_new(pval, it)) {
  285. OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR);
  286. goto err;
  287. }
  288. /* CHOICE type, try each possibility in turn */
  289. p = *in;
  290. for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
  291. pchptr = asn1_get_field_ptr(pval, tt);
  292. /*
  293. * We mark field as OPTIONAL so its absence can be recognised.
  294. */
  295. ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx);
  296. /* If field not present, try the next one */
  297. if (ret == -1)
  298. continue;
  299. /* If positive return, read OK, break loop */
  300. if (ret > 0)
  301. break;
  302. /* Otherwise must be an ASN1 parsing error */
  303. errtt = tt;
  304. OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR);
  305. goto err;
  306. }
  307. /* Did we fall off the end without reading anything? */
  308. if (i == it->tcount) {
  309. /* If OPTIONAL, this is OK */
  310. if (opt) {
  311. /* Free and zero it */
  312. ASN1_item_ex_free(pval, it);
  313. return -1;
  314. }
  315. OPENSSL_PUT_ERROR(ASN1, ASN1_R_NO_MATCHING_CHOICE_TYPE);
  316. goto err;
  317. }
  318. asn1_set_choice_selector(pval, i, it);
  319. if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
  320. goto auxerr;
  321. *in = p;
  322. return 1;
  323. case ASN1_ITYPE_NDEF_SEQUENCE:
  324. case ASN1_ITYPE_SEQUENCE:
  325. p = *in;
  326. tmplen = len;
  327. /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
  328. if (tag == -1) {
  329. tag = V_ASN1_SEQUENCE;
  330. aclass = V_ASN1_UNIVERSAL;
  331. }
  332. /* Get SEQUENCE length and update len, p */
  333. ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,
  334. &p, len, tag, aclass, opt, ctx);
  335. if (!ret) {
  336. OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR);
  337. goto err;
  338. } else if (ret == -1)
  339. return -1;
  340. if (aux && (aux->flags & ASN1_AFLG_BROKEN)) {
  341. len = tmplen - (p - *in);
  342. seq_nolen = 1;
  343. }
  344. /* If indefinite we don't do a length check */
  345. else
  346. seq_nolen = seq_eoc;
  347. if (!cst) {
  348. OPENSSL_PUT_ERROR(ASN1, ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
  349. goto err;
  350. }
  351. if (!*pval && !ASN1_item_ex_new(pval, it)) {
  352. OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR);
  353. goto err;
  354. }
  355. if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
  356. goto auxerr;
  357. /* Free up and zero any ADB found */
  358. for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
  359. if (tt->flags & ASN1_TFLG_ADB_MASK) {
  360. const ASN1_TEMPLATE *seqtt;
  361. ASN1_VALUE **pseqval;
  362. seqtt = asn1_do_adb(pval, tt, 0);
  363. if (seqtt == NULL)
  364. continue;
  365. pseqval = asn1_get_field_ptr(pval, seqtt);
  366. ASN1_template_free(pseqval, seqtt);
  367. }
  368. }
  369. /* Get each field entry */
  370. for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
  371. const ASN1_TEMPLATE *seqtt;
  372. ASN1_VALUE **pseqval;
  373. seqtt = asn1_do_adb(pval, tt, 1);
  374. if (seqtt == NULL)
  375. goto err;
  376. pseqval = asn1_get_field_ptr(pval, seqtt);
  377. /* Have we ran out of data? */
  378. if (!len)
  379. break;
  380. q = p;
  381. if (asn1_check_eoc(&p, len)) {
  382. if (!seq_eoc) {
  383. OPENSSL_PUT_ERROR(ASN1, ASN1_R_UNEXPECTED_EOC);
  384. goto err;
  385. }
  386. len -= p - q;
  387. seq_eoc = 0;
  388. q = p;
  389. break;
  390. }
  391. /*
  392. * This determines the OPTIONAL flag value. The field cannot be
  393. * omitted if it is the last of a SEQUENCE and there is still
  394. * data to be read. This isn't strictly necessary but it
  395. * increases efficiency in some cases.
  396. */
  397. if (i == (it->tcount - 1))
  398. isopt = 0;
  399. else
  400. isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);
  401. /*
  402. * attempt to read in field, allowing each to be OPTIONAL
  403. */
  404. ret = asn1_template_ex_d2i(pseqval, &p, len, seqtt, isopt, ctx);
  405. if (!ret) {
  406. errtt = seqtt;
  407. goto err;
  408. } else if (ret == -1) {
  409. /*
  410. * OPTIONAL component absent. Free and zero the field.
  411. */
  412. ASN1_template_free(pseqval, seqtt);
  413. continue;
  414. }
  415. /* Update length */
  416. len -= p - q;
  417. }
  418. /* Check for EOC if expecting one */
  419. if (seq_eoc && !asn1_check_eoc(&p, len)) {
  420. OPENSSL_PUT_ERROR(ASN1, ASN1_R_MISSING_EOC);
  421. goto err;
  422. }
  423. /* Check all data read */
  424. if (!seq_nolen && len) {
  425. OPENSSL_PUT_ERROR(ASN1, ASN1_R_SEQUENCE_LENGTH_MISMATCH);
  426. goto err;
  427. }
  428. /*
  429. * If we get here we've got no more data in the SEQUENCE, however we
  430. * may not have read all fields so check all remaining are OPTIONAL
  431. * and clear any that are.
  432. */
  433. for (; i < it->tcount; tt++, i++) {
  434. const ASN1_TEMPLATE *seqtt;
  435. seqtt = asn1_do_adb(pval, tt, 1);
  436. if (seqtt == NULL)
  437. goto err;
  438. if (seqtt->flags & ASN1_TFLG_OPTIONAL) {
  439. ASN1_VALUE **pseqval;
  440. pseqval = asn1_get_field_ptr(pval, seqtt);
  441. ASN1_template_free(pseqval, seqtt);
  442. } else {
  443. errtt = seqtt;
  444. OPENSSL_PUT_ERROR(ASN1, ASN1_R_FIELD_MISSING);
  445. goto err;
  446. }
  447. }
  448. /* Save encoding */
  449. if (!asn1_enc_save(pval, *in, p - *in, it))
  450. goto auxerr;
  451. if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
  452. goto auxerr;
  453. *in = p;
  454. return 1;
  455. default:
  456. return 0;
  457. }
  458. auxerr:
  459. OPENSSL_PUT_ERROR(ASN1, ASN1_R_AUX_ERROR);
  460. err:
  461. if (combine == 0)
  462. ASN1_item_ex_free(pval, it);
  463. if (errtt)
  464. ERR_add_error_data(4, "Field=", errtt->field_name,
  465. ", Type=", it->sname);
  466. else
  467. ERR_add_error_data(2, "Type=", it->sname);
  468. return 0;
  469. }
  470. /*
  471. * Templates are handled with two separate functions. One handles any
  472. * EXPLICIT tag and the other handles the rest.
  473. */
  474. static int asn1_template_ex_d2i(ASN1_VALUE **val,
  475. const unsigned char **in, long inlen,
  476. const ASN1_TEMPLATE *tt, char opt,
  477. ASN1_TLC *ctx)
  478. {
  479. int flags, aclass;
  480. int ret;
  481. long len;
  482. const unsigned char *p, *q;
  483. char exp_eoc;
  484. if (!val)
  485. return 0;
  486. flags = tt->flags;
  487. aclass = flags & ASN1_TFLG_TAG_CLASS;
  488. p = *in;
  489. /* Check if EXPLICIT tag expected */
  490. if (flags & ASN1_TFLG_EXPTAG) {
  491. char cst;
  492. /*
  493. * Need to work out amount of data available to the inner content and
  494. * where it starts: so read in EXPLICIT header to get the info.
  495. */
  496. ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst,
  497. &p, inlen, tt->tag, aclass, opt, ctx);
  498. q = p;
  499. if (!ret) {
  500. OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR);
  501. return 0;
  502. } else if (ret == -1)
  503. return -1;
  504. if (!cst) {
  505. OPENSSL_PUT_ERROR(ASN1, ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED);
  506. return 0;
  507. }
  508. /* We've found the field so it can't be OPTIONAL now */
  509. ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx);
  510. if (!ret) {
  511. OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR);
  512. return 0;
  513. }
  514. /* We read the field in OK so update length */
  515. len -= p - q;
  516. if (exp_eoc) {
  517. /* If NDEF we must have an EOC here */
  518. if (!asn1_check_eoc(&p, len)) {
  519. OPENSSL_PUT_ERROR(ASN1, ASN1_R_MISSING_EOC);
  520. goto err;
  521. }
  522. } else {
  523. /*
  524. * Otherwise we must hit the EXPLICIT tag end or its an error
  525. */
  526. if (len) {
  527. OPENSSL_PUT_ERROR(ASN1, ASN1_R_EXPLICIT_LENGTH_MISMATCH);
  528. goto err;
  529. }
  530. }
  531. } else
  532. return asn1_template_noexp_d2i(val, in, inlen, tt, opt, ctx);
  533. *in = p;
  534. return 1;
  535. err:
  536. ASN1_template_free(val, tt);
  537. return 0;
  538. }
  539. static int asn1_template_noexp_d2i(ASN1_VALUE **val,
  540. const unsigned char **in, long len,
  541. const ASN1_TEMPLATE *tt, char opt,
  542. ASN1_TLC *ctx)
  543. {
  544. int flags, aclass;
  545. int ret;
  546. const unsigned char *p;
  547. if (!val)
  548. return 0;
  549. flags = tt->flags;
  550. aclass = flags & ASN1_TFLG_TAG_CLASS;
  551. p = *in;
  552. if (flags & ASN1_TFLG_SK_MASK) {
  553. /* SET OF, SEQUENCE OF */
  554. int sktag, skaclass;
  555. char sk_eoc;
  556. /* First work out expected inner tag value */
  557. if (flags & ASN1_TFLG_IMPTAG) {
  558. sktag = tt->tag;
  559. skaclass = aclass;
  560. } else {
  561. skaclass = V_ASN1_UNIVERSAL;
  562. if (flags & ASN1_TFLG_SET_OF)
  563. sktag = V_ASN1_SET;
  564. else
  565. sktag = V_ASN1_SEQUENCE;
  566. }
  567. /* Get the tag */
  568. ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL,
  569. &p, len, sktag, skaclass, opt, ctx);
  570. if (!ret) {
  571. OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR);
  572. return 0;
  573. } else if (ret == -1)
  574. return -1;
  575. if (!*val)
  576. *val = (ASN1_VALUE *)sk_new_null();
  577. else {
  578. /*
  579. * We've got a valid STACK: free up any items present
  580. */
  581. STACK_OF(ASN1_VALUE) *sktmp = (STACK_OF(ASN1_VALUE) *)*val;
  582. ASN1_VALUE *vtmp;
  583. while (sk_ASN1_VALUE_num(sktmp) > 0) {
  584. vtmp = sk_ASN1_VALUE_pop(sktmp);
  585. ASN1_item_ex_free(&vtmp, ASN1_ITEM_ptr(tt->item));
  586. }
  587. }
  588. if (!*val) {
  589. OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
  590. goto err;
  591. }
  592. /* Read as many items as we can */
  593. while (len > 0) {
  594. ASN1_VALUE *skfield;
  595. const unsigned char *q = p;
  596. /* See if EOC found */
  597. if (asn1_check_eoc(&p, len)) {
  598. if (!sk_eoc) {
  599. OPENSSL_PUT_ERROR(ASN1, ASN1_R_UNEXPECTED_EOC);
  600. goto err;
  601. }
  602. len -= p - q;
  603. sk_eoc = 0;
  604. break;
  605. }
  606. skfield = NULL;
  607. if (!ASN1_item_ex_d2i(&skfield, &p, len,
  608. ASN1_ITEM_ptr(tt->item), -1, 0, 0, ctx)) {
  609. OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR);
  610. goto err;
  611. }
  612. len -= p - q;
  613. if (!sk_ASN1_VALUE_push((STACK_OF(ASN1_VALUE) *)*val, skfield)) {
  614. OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
  615. goto err;
  616. }
  617. }
  618. if (sk_eoc) {
  619. OPENSSL_PUT_ERROR(ASN1, ASN1_R_MISSING_EOC);
  620. goto err;
  621. }
  622. } else if (flags & ASN1_TFLG_IMPTAG) {
  623. /* IMPLICIT tagging */
  624. ret = ASN1_item_ex_d2i(val, &p, len,
  625. ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt,
  626. ctx);
  627. if (!ret) {
  628. OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR);
  629. goto err;
  630. } else if (ret == -1)
  631. return -1;
  632. } else {
  633. /* Nothing special */
  634. ret = ASN1_item_ex_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item),
  635. -1, tt->flags & ASN1_TFLG_COMBINE, opt, ctx);
  636. if (!ret) {
  637. OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR);
  638. goto err;
  639. } else if (ret == -1)
  640. return -1;
  641. }
  642. *in = p;
  643. return 1;
  644. err:
  645. ASN1_template_free(val, tt);
  646. return 0;
  647. }
  648. static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
  649. const unsigned char **in, long inlen,
  650. const ASN1_ITEM *it,
  651. int tag, int aclass, char opt, ASN1_TLC *ctx)
  652. {
  653. int ret = 0, utype;
  654. long plen;
  655. char cst, inf, free_cont = 0;
  656. const unsigned char *p;
  657. BUF_MEM buf = {0, NULL, 0 };
  658. const unsigned char *cont = NULL;
  659. long len;
  660. if (!pval) {
  661. OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_NULL);
  662. return 0; /* Should never happen */
  663. }
  664. if (it->itype == ASN1_ITYPE_MSTRING) {
  665. utype = tag;
  666. tag = -1;
  667. } else
  668. utype = it->utype;
  669. if (utype == V_ASN1_ANY) {
  670. /* If type is ANY need to figure out type from tag */
  671. unsigned char oclass;
  672. if (tag >= 0) {
  673. OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_TAGGED_ANY);
  674. return 0;
  675. }
  676. if (opt) {
  677. OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_OPTIONAL_ANY);
  678. return 0;
  679. }
  680. p = *in;
  681. ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
  682. &p, inlen, -1, 0, 0, ctx);
  683. if (!ret) {
  684. OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR);
  685. return 0;
  686. }
  687. if (oclass != V_ASN1_UNIVERSAL)
  688. utype = V_ASN1_OTHER;
  689. }
  690. if (tag == -1) {
  691. tag = utype;
  692. aclass = V_ASN1_UNIVERSAL;
  693. }
  694. p = *in;
  695. /* Check header */
  696. ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
  697. &p, inlen, tag, aclass, opt, ctx);
  698. if (!ret) {
  699. OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR);
  700. return 0;
  701. } else if (ret == -1)
  702. return -1;
  703. ret = 0;
  704. /* SEQUENCE, SET and "OTHER" are left in encoded form */
  705. if ((utype == V_ASN1_SEQUENCE)
  706. || (utype == V_ASN1_SET) || (utype == V_ASN1_OTHER)) {
  707. /*
  708. * Clear context cache for type OTHER because the auto clear when we
  709. * have a exact match wont work
  710. */
  711. if (utype == V_ASN1_OTHER) {
  712. asn1_tlc_clear(ctx);
  713. }
  714. /* SEQUENCE and SET must be constructed */
  715. else if (!cst) {
  716. OPENSSL_PUT_ERROR(ASN1, ASN1_R_TYPE_NOT_CONSTRUCTED);
  717. return 0;
  718. }
  719. cont = *in;
  720. /* If indefinite length constructed find the real end */
  721. if (inf) {
  722. if (!asn1_find_end(&p, plen, inf))
  723. goto err;
  724. len = p - cont;
  725. } else {
  726. len = p - cont + plen;
  727. p += plen;
  728. }
  729. } else if (cst) {
  730. if (utype == V_ASN1_NULL || utype == V_ASN1_BOOLEAN
  731. || utype == V_ASN1_OBJECT || utype == V_ASN1_INTEGER
  732. || utype == V_ASN1_ENUMERATED) {
  733. /* These types only have primitive encodings. */
  734. OPENSSL_PUT_ERROR(ASN1, ASN1_R_TYPE_NOT_PRIMITIVE);
  735. return 0;
  736. }
  737. /* Free any returned 'buf' content */
  738. free_cont = 1;
  739. /*
  740. * Should really check the internal tags are correct but some things
  741. * may get this wrong. The relevant specs say that constructed string
  742. * types should be OCTET STRINGs internally irrespective of the type.
  743. * So instead just check for UNIVERSAL class and ignore the tag.
  744. */
  745. if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL, 0)) {
  746. goto err;
  747. }
  748. len = buf.length;
  749. /* Append a final null to string */
  750. if (!BUF_MEM_grow_clean(&buf, len + 1)) {
  751. OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
  752. goto err;
  753. }
  754. buf.data[len] = 0;
  755. cont = (const unsigned char *)buf.data;
  756. } else {
  757. cont = p;
  758. len = plen;
  759. p += plen;
  760. }
  761. /* We now have content length and type: translate into a structure */
  762. /* asn1_ex_c2i may reuse allocated buffer, and so sets free_cont to 0 */
  763. if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it))
  764. goto err;
  765. *in = p;
  766. ret = 1;
  767. err:
  768. if (free_cont && buf.data)
  769. OPENSSL_free(buf.data);
  770. return ret;
  771. }
  772. /* Translate ASN1 content octets into a structure */
  773. int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
  774. int utype, char *free_cont, const ASN1_ITEM *it)
  775. {
  776. ASN1_VALUE **opval = NULL;
  777. ASN1_STRING *stmp;
  778. ASN1_TYPE *typ = NULL;
  779. int ret = 0;
  780. const ASN1_PRIMITIVE_FUNCS *pf;
  781. ASN1_INTEGER **tint;
  782. pf = it->funcs;
  783. if (pf && pf->prim_c2i)
  784. return pf->prim_c2i(pval, cont, len, utype, free_cont, it);
  785. /* If ANY type clear type and set pointer to internal value */
  786. if (it->utype == V_ASN1_ANY) {
  787. if (!*pval) {
  788. typ = ASN1_TYPE_new();
  789. if (typ == NULL)
  790. goto err;
  791. *pval = (ASN1_VALUE *)typ;
  792. } else
  793. typ = (ASN1_TYPE *)*pval;
  794. if (utype != typ->type)
  795. ASN1_TYPE_set(typ, utype, NULL);
  796. opval = pval;
  797. pval = &typ->value.asn1_value;
  798. }
  799. switch (utype) {
  800. case V_ASN1_OBJECT:
  801. if (!c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len))
  802. goto err;
  803. break;
  804. case V_ASN1_NULL:
  805. if (len) {
  806. OPENSSL_PUT_ERROR(ASN1, ASN1_R_NULL_IS_WRONG_LENGTH);
  807. goto err;
  808. }
  809. *pval = (ASN1_VALUE *)1;
  810. break;
  811. case V_ASN1_BOOLEAN:
  812. if (len != 1) {
  813. OPENSSL_PUT_ERROR(ASN1, ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
  814. goto err;
  815. } else {
  816. ASN1_BOOLEAN *tbool;
  817. tbool = (ASN1_BOOLEAN *)pval;
  818. *tbool = *cont;
  819. }
  820. break;
  821. case V_ASN1_BIT_STRING:
  822. if (!c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len))
  823. goto err;
  824. break;
  825. case V_ASN1_INTEGER:
  826. case V_ASN1_ENUMERATED:
  827. tint = (ASN1_INTEGER **)pval;
  828. if (!c2i_ASN1_INTEGER(tint, &cont, len))
  829. goto err;
  830. /* Fixup type to match the expected form */
  831. (*tint)->type = utype | ((*tint)->type & V_ASN1_NEG);
  832. break;
  833. case V_ASN1_OCTET_STRING:
  834. case V_ASN1_NUMERICSTRING:
  835. case V_ASN1_PRINTABLESTRING:
  836. case V_ASN1_T61STRING:
  837. case V_ASN1_VIDEOTEXSTRING:
  838. case V_ASN1_IA5STRING:
  839. case V_ASN1_UTCTIME:
  840. case V_ASN1_GENERALIZEDTIME:
  841. case V_ASN1_GRAPHICSTRING:
  842. case V_ASN1_VISIBLESTRING:
  843. case V_ASN1_GENERALSTRING:
  844. case V_ASN1_UNIVERSALSTRING:
  845. case V_ASN1_BMPSTRING:
  846. case V_ASN1_UTF8STRING:
  847. case V_ASN1_OTHER:
  848. case V_ASN1_SET:
  849. case V_ASN1_SEQUENCE:
  850. default:
  851. if (utype == V_ASN1_BMPSTRING && (len & 1)) {
  852. OPENSSL_PUT_ERROR(ASN1, ASN1_R_BMPSTRING_IS_WRONG_LENGTH);
  853. goto err;
  854. }
  855. if (utype == V_ASN1_UNIVERSALSTRING && (len & 3)) {
  856. OPENSSL_PUT_ERROR(ASN1, ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH);
  857. goto err;
  858. }
  859. /* All based on ASN1_STRING and handled the same */
  860. if (!*pval) {
  861. stmp = ASN1_STRING_type_new(utype);
  862. if (!stmp) {
  863. OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
  864. goto err;
  865. }
  866. *pval = (ASN1_VALUE *)stmp;
  867. } else {
  868. stmp = (ASN1_STRING *)*pval;
  869. stmp->type = utype;
  870. }
  871. /* If we've already allocated a buffer use it */
  872. if (*free_cont) {
  873. if (stmp->data)
  874. OPENSSL_free(stmp->data);
  875. stmp->data = (unsigned char *)cont; /* UGLY CAST! RL */
  876. stmp->length = len;
  877. *free_cont = 0;
  878. } else {
  879. if (!ASN1_STRING_set(stmp, cont, len)) {
  880. OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
  881. ASN1_STRING_free(stmp);
  882. *pval = NULL;
  883. goto err;
  884. }
  885. }
  886. break;
  887. }
  888. /* If ASN1_ANY and NULL type fix up value */
  889. if (typ && (utype == V_ASN1_NULL))
  890. typ->value.ptr = NULL;
  891. ret = 1;
  892. err:
  893. if (!ret) {
  894. ASN1_TYPE_free(typ);
  895. if (opval)
  896. *opval = NULL;
  897. }
  898. return ret;
  899. }
  900. /*
  901. * This function finds the end of an ASN1 structure when passed its maximum
  902. * length, whether it is indefinite length and a pointer to the content. This
  903. * is more efficient than calling asn1_collect because it does not recurse on
  904. * each indefinite length header.
  905. */
  906. static int asn1_find_end(const unsigned char **in, long len, char inf)
  907. {
  908. int expected_eoc;
  909. long plen;
  910. const unsigned char *p = *in, *q;
  911. /* If not indefinite length constructed just add length */
  912. if (inf == 0) {
  913. *in += len;
  914. return 1;
  915. }
  916. expected_eoc = 1;
  917. /*
  918. * Indefinite length constructed form. Find the end when enough EOCs are
  919. * found. If more indefinite length constructed headers are encountered
  920. * increment the expected eoc count otherwise just skip to the end of the
  921. * data.
  922. */
  923. while (len > 0) {
  924. if (asn1_check_eoc(&p, len)) {
  925. expected_eoc--;
  926. if (expected_eoc == 0)
  927. break;
  928. len -= 2;
  929. continue;
  930. }
  931. q = p;
  932. /* Just read in a header: only care about the length */
  933. if (!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len,
  934. -1, 0, 0, NULL)) {
  935. OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR);
  936. return 0;
  937. }
  938. if (inf)
  939. expected_eoc++;
  940. else
  941. p += plen;
  942. len -= p - q;
  943. }
  944. if (expected_eoc) {
  945. OPENSSL_PUT_ERROR(ASN1, ASN1_R_MISSING_EOC);
  946. return 0;
  947. }
  948. *in = p;
  949. return 1;
  950. }
  951. /*
  952. * This function collects the asn1 data from a constructred string type into
  953. * a buffer. The values of 'in' and 'len' should refer to the contents of the
  954. * constructed type and 'inf' should be set if it is indefinite length.
  955. */
  956. #ifndef ASN1_MAX_STRING_NEST
  957. /*
  958. * This determines how many levels of recursion are permitted in ASN1 string
  959. * types. If it is not limited stack overflows can occur. If set to zero no
  960. * recursion is allowed at all. Although zero should be adequate examples
  961. * exist that require a value of 1. So 5 should be more than enough.
  962. */
  963. # define ASN1_MAX_STRING_NEST 5
  964. #endif
  965. static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
  966. char inf, int tag, int aclass, int depth)
  967. {
  968. const unsigned char *p, *q;
  969. long plen;
  970. char cst, ininf;
  971. p = *in;
  972. inf &= 1;
  973. /*
  974. * If no buffer and not indefinite length constructed just pass over the
  975. * encoded data
  976. */
  977. if (!buf && !inf) {
  978. *in += len;
  979. return 1;
  980. }
  981. while (len > 0) {
  982. q = p;
  983. /* Check for EOC */
  984. if (asn1_check_eoc(&p, len)) {
  985. /*
  986. * EOC is illegal outside indefinite length constructed form
  987. */
  988. if (!inf) {
  989. OPENSSL_PUT_ERROR(ASN1, ASN1_R_UNEXPECTED_EOC);
  990. return 0;
  991. }
  992. inf = 0;
  993. break;
  994. }
  995. if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
  996. len, tag, aclass, 0, NULL)) {
  997. OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR);
  998. return 0;
  999. }
  1000. /* If indefinite length constructed update max length */
  1001. if (cst) {
  1002. if (depth >= ASN1_MAX_STRING_NEST) {
  1003. OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_STRING);
  1004. return 0;
  1005. }
  1006. if (!asn1_collect(buf, &p, plen, ininf, tag, aclass, depth + 1))
  1007. return 0;
  1008. } else if (plen && !collect_data(buf, &p, plen))
  1009. return 0;
  1010. len -= p - q;
  1011. }
  1012. if (inf) {
  1013. OPENSSL_PUT_ERROR(ASN1, ASN1_R_MISSING_EOC);
  1014. return 0;
  1015. }
  1016. *in = p;
  1017. return 1;
  1018. }
  1019. static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
  1020. {
  1021. int len;
  1022. if (buf) {
  1023. len = buf->length;
  1024. if (!BUF_MEM_grow_clean(buf, len + plen)) {
  1025. OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
  1026. return 0;
  1027. }
  1028. memcpy(buf->data + len, *p, plen);
  1029. }
  1030. *p += plen;
  1031. return 1;
  1032. }
  1033. /* Check for ASN1 EOC and swallow it if found */
  1034. static int asn1_check_eoc(const unsigned char **in, long len)
  1035. {
  1036. const unsigned char *p;
  1037. if (len < 2)
  1038. return 0;
  1039. p = *in;
  1040. if (!p[0] && !p[1]) {
  1041. *in += 2;
  1042. return 1;
  1043. }
  1044. return 0;
  1045. }
  1046. /*
  1047. * Check an ASN1 tag and length: a bit like ASN1_get_object but it sets the
  1048. * length for indefinite length constructed form, we don't know the exact
  1049. * length but we can set an upper bound to the amount of data available minus
  1050. * the header length just read.
  1051. */
  1052. static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
  1053. char *inf, char *cst,
  1054. const unsigned char **in, long len,
  1055. int exptag, int expclass, char opt, ASN1_TLC *ctx)
  1056. {
  1057. int i;
  1058. int ptag, pclass;
  1059. long plen;
  1060. const unsigned char *p, *q;
  1061. p = *in;
  1062. q = p;
  1063. if (ctx && ctx->valid) {
  1064. i = ctx->ret;
  1065. plen = ctx->plen;
  1066. pclass = ctx->pclass;
  1067. ptag = ctx->ptag;
  1068. p += ctx->hdrlen;
  1069. } else {
  1070. i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
  1071. if (ctx) {
  1072. ctx->ret = i;
  1073. ctx->plen = plen;
  1074. ctx->pclass = pclass;
  1075. ctx->ptag = ptag;
  1076. ctx->hdrlen = p - q;
  1077. ctx->valid = 1;
  1078. /*
  1079. * If definite length, and no error, length + header can't exceed
  1080. * total amount of data available.
  1081. */
  1082. if (!(i & 0x81) && ((plen + ctx->hdrlen) > len)) {
  1083. OPENSSL_PUT_ERROR(ASN1, ASN1_R_TOO_LONG);
  1084. asn1_tlc_clear(ctx);
  1085. return 0;
  1086. }
  1087. }
  1088. }
  1089. if (i & 0x80) {
  1090. OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_OBJECT_HEADER);
  1091. asn1_tlc_clear(ctx);
  1092. return 0;
  1093. }
  1094. if (exptag >= 0) {
  1095. if ((exptag != ptag) || (expclass != pclass)) {
  1096. /*
  1097. * If type is OPTIONAL, not an error: indicate missing type.
  1098. */
  1099. if (opt)
  1100. return -1;
  1101. asn1_tlc_clear(ctx);
  1102. OPENSSL_PUT_ERROR(ASN1, ASN1_R_WRONG_TAG);
  1103. return 0;
  1104. }
  1105. /*
  1106. * We have a tag and class match: assume we are going to do something
  1107. * with it
  1108. */
  1109. asn1_tlc_clear(ctx);
  1110. }
  1111. if (i & 1)
  1112. plen = len - (p - q);
  1113. if (inf)
  1114. *inf = i & 1;
  1115. if (cst)
  1116. *cst = i & V_ASN1_CONSTRUCTED;
  1117. if (olen)
  1118. *olen = plen;
  1119. if (oclass)
  1120. *oclass = pclass;
  1121. if (otag)
  1122. *otag = ptag;
  1123. *in = p;
  1124. return 1;
  1125. }