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.
 
 
 
 
 
 

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