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.
 
 
 
 
 
 

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