You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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