選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

1341 行
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. #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_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_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_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_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_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_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_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_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_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_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_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_R_SEQUENCE_NOT_CONSTRUCTED);
  358. goto err;
  359. }
  360. if (!*pval && !ASN1_item_ex_new(pval, it))
  361. {
  362. OPENSSL_PUT_ERROR(ASN1, 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_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_R_MISSING_EOC);
  437. goto err;
  438. }
  439. /* Check all data read */
  440. if (!seq_nolen && len)
  441. {
  442. OPENSSL_PUT_ERROR(ASN1, 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_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_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_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_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_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_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_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_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, 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_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_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, ERR_R_MALLOC_FAILURE);
  660. goto err;
  661. }
  662. }
  663. if (sk_eoc)
  664. {
  665. OPENSSL_PUT_ERROR(ASN1, 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_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_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_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_R_ILLEGAL_TAGGED_ANY);
  733. return 0;
  734. }
  735. if (opt)
  736. {
  737. OPENSSL_PUT_ERROR(ASN1, 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_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_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_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_R_TYPE_NOT_PRIMITIVE);
  808. return 0;
  809. }
  810. buf.length = 0;
  811. buf.max = 0;
  812. buf.data = NULL;
  813. /* Should really check the internal tags are correct but
  814. * some things may get this wrong. The relevant specs
  815. * say that constructed string types should be OCTET STRINGs
  816. * internally irrespective of the type. So instead just check
  817. * for UNIVERSAL class and ignore the tag.
  818. */
  819. if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL, 0))
  820. {
  821. free_cont = 1;
  822. goto err;
  823. }
  824. len = buf.length;
  825. /* Append a final null to string */
  826. if (!BUF_MEM_grow_clean(&buf, len + 1))
  827. {
  828. OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
  829. return 0;
  830. }
  831. buf.data[len] = 0;
  832. cont = (const unsigned char *)buf.data;
  833. free_cont = 1;
  834. }
  835. else
  836. {
  837. cont = p;
  838. len = plen;
  839. p += plen;
  840. }
  841. /* We now have content length and type: translate into a structure */
  842. if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it))
  843. goto err;
  844. *in = p;
  845. ret = 1;
  846. err:
  847. if (free_cont && buf.data) OPENSSL_free(buf.data);
  848. return ret;
  849. }
  850. /* Translate ASN1 content octets into a structure */
  851. int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
  852. int utype, char *free_cont, const ASN1_ITEM *it)
  853. {
  854. ASN1_VALUE **opval = NULL;
  855. ASN1_STRING *stmp;
  856. ASN1_TYPE *typ = NULL;
  857. int ret = 0;
  858. const ASN1_PRIMITIVE_FUNCS *pf;
  859. ASN1_INTEGER **tint;
  860. pf = it->funcs;
  861. if (pf && pf->prim_c2i)
  862. return pf->prim_c2i(pval, cont, len, utype, free_cont, it);
  863. /* If ANY type clear type and set pointer to internal value */
  864. if (it->utype == V_ASN1_ANY)
  865. {
  866. if (!*pval)
  867. {
  868. typ = ASN1_TYPE_new();
  869. if (typ == NULL)
  870. goto err;
  871. *pval = (ASN1_VALUE *)typ;
  872. }
  873. else
  874. typ = (ASN1_TYPE *)*pval;
  875. if (utype != typ->type)
  876. ASN1_TYPE_set(typ, utype, NULL);
  877. opval = pval;
  878. pval = &typ->value.asn1_value;
  879. }
  880. switch(utype)
  881. {
  882. case V_ASN1_OBJECT:
  883. if (!c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len))
  884. goto err;
  885. break;
  886. case V_ASN1_NULL:
  887. if (len)
  888. {
  889. OPENSSL_PUT_ERROR(ASN1, ASN1_R_NULL_IS_WRONG_LENGTH);
  890. goto err;
  891. }
  892. *pval = (ASN1_VALUE *)1;
  893. break;
  894. case V_ASN1_BOOLEAN:
  895. if (len != 1)
  896. {
  897. OPENSSL_PUT_ERROR(ASN1, ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
  898. goto err;
  899. }
  900. else
  901. {
  902. ASN1_BOOLEAN *tbool;
  903. tbool = (ASN1_BOOLEAN *)pval;
  904. *tbool = *cont;
  905. }
  906. break;
  907. case V_ASN1_BIT_STRING:
  908. if (!c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len))
  909. goto err;
  910. break;
  911. case V_ASN1_INTEGER:
  912. case V_ASN1_NEG_INTEGER:
  913. case V_ASN1_ENUMERATED:
  914. case V_ASN1_NEG_ENUMERATED:
  915. tint = (ASN1_INTEGER **)pval;
  916. if (!c2i_ASN1_INTEGER(tint, &cont, len))
  917. goto err;
  918. /* Fixup type to match the expected form */
  919. (*tint)->type = utype | ((*tint)->type & V_ASN1_NEG);
  920. break;
  921. case V_ASN1_OCTET_STRING:
  922. case V_ASN1_NUMERICSTRING:
  923. case V_ASN1_PRINTABLESTRING:
  924. case V_ASN1_T61STRING:
  925. case V_ASN1_VIDEOTEXSTRING:
  926. case V_ASN1_IA5STRING:
  927. case V_ASN1_UTCTIME:
  928. case V_ASN1_GENERALIZEDTIME:
  929. case V_ASN1_GRAPHICSTRING:
  930. case V_ASN1_VISIBLESTRING:
  931. case V_ASN1_GENERALSTRING:
  932. case V_ASN1_UNIVERSALSTRING:
  933. case V_ASN1_BMPSTRING:
  934. case V_ASN1_UTF8STRING:
  935. case V_ASN1_OTHER:
  936. case V_ASN1_SET:
  937. case V_ASN1_SEQUENCE:
  938. default:
  939. if (utype == V_ASN1_BMPSTRING && (len & 1))
  940. {
  941. OPENSSL_PUT_ERROR(ASN1, ASN1_R_BMPSTRING_IS_WRONG_LENGTH);
  942. goto err;
  943. }
  944. if (utype == V_ASN1_UNIVERSALSTRING && (len & 3))
  945. {
  946. OPENSSL_PUT_ERROR(ASN1, ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH);
  947. goto err;
  948. }
  949. /* All based on ASN1_STRING and handled the same */
  950. if (!*pval)
  951. {
  952. stmp = ASN1_STRING_type_new(utype);
  953. if (!stmp)
  954. {
  955. OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
  956. goto err;
  957. }
  958. *pval = (ASN1_VALUE *)stmp;
  959. }
  960. else
  961. {
  962. stmp = (ASN1_STRING *)*pval;
  963. stmp->type = utype;
  964. }
  965. /* If we've already allocated a buffer use it */
  966. if (*free_cont)
  967. {
  968. if (stmp->data)
  969. OPENSSL_free(stmp->data);
  970. stmp->data = (unsigned char *)cont; /* UGLY CAST! RL */
  971. stmp->length = len;
  972. *free_cont = 0;
  973. }
  974. else
  975. {
  976. if (!ASN1_STRING_set(stmp, cont, len))
  977. {
  978. OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
  979. ASN1_STRING_free(stmp);
  980. *pval = NULL;
  981. goto err;
  982. }
  983. }
  984. break;
  985. }
  986. /* If ASN1_ANY and NULL type fix up value */
  987. if (typ && (utype == V_ASN1_NULL))
  988. typ->value.ptr = NULL;
  989. ret = 1;
  990. err:
  991. if (!ret)
  992. {
  993. ASN1_TYPE_free(typ);
  994. if (opval)
  995. *opval = NULL;
  996. }
  997. return ret;
  998. }
  999. /* This function finds the end of an ASN1 structure when passed its maximum
  1000. * length, whether it is indefinite length and a pointer to the content.
  1001. * This is more efficient than calling asn1_collect because it does not
  1002. * recurse on each indefinite length header.
  1003. */
  1004. static int asn1_find_end(const unsigned char **in, long len, char inf)
  1005. {
  1006. int expected_eoc;
  1007. long plen;
  1008. const unsigned char *p = *in, *q;
  1009. /* If not indefinite length constructed just add length */
  1010. if (inf == 0)
  1011. {
  1012. *in += len;
  1013. return 1;
  1014. }
  1015. expected_eoc = 1;
  1016. /* Indefinite length constructed form. Find the end when enough EOCs
  1017. * are found. If more indefinite length constructed headers
  1018. * are encountered increment the expected eoc count otherwise just
  1019. * skip to the end of the data.
  1020. */
  1021. while (len > 0)
  1022. {
  1023. if(asn1_check_eoc(&p, len))
  1024. {
  1025. expected_eoc--;
  1026. if (expected_eoc == 0)
  1027. break;
  1028. len -= 2;
  1029. continue;
  1030. }
  1031. q = p;
  1032. /* Just read in a header: only care about the length */
  1033. if(!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len,
  1034. -1, 0, 0, NULL))
  1035. {
  1036. OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR);
  1037. return 0;
  1038. }
  1039. if (inf)
  1040. expected_eoc++;
  1041. else
  1042. p += plen;
  1043. len -= p - q;
  1044. }
  1045. if (expected_eoc)
  1046. {
  1047. OPENSSL_PUT_ERROR(ASN1, ASN1_R_MISSING_EOC);
  1048. return 0;
  1049. }
  1050. *in = p;
  1051. return 1;
  1052. }
  1053. /* This function collects the asn1 data from a constructred string
  1054. * type into a buffer. The values of 'in' and 'len' should refer
  1055. * to the contents of the constructed type and 'inf' should be set
  1056. * if it is indefinite length.
  1057. */
  1058. #ifndef ASN1_MAX_STRING_NEST
  1059. /* This determines how many levels of recursion are permitted in ASN1
  1060. * string types. If it is not limited stack overflows can occur. If set
  1061. * to zero no recursion is allowed at all. Although zero should be adequate
  1062. * examples exist that require a value of 1. So 5 should be more than enough.
  1063. */
  1064. #define ASN1_MAX_STRING_NEST 5
  1065. #endif
  1066. static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
  1067. char inf, int tag, int aclass, int depth)
  1068. {
  1069. const unsigned char *p, *q;
  1070. long plen;
  1071. char cst, ininf;
  1072. p = *in;
  1073. inf &= 1;
  1074. /* If no buffer and not indefinite length constructed just pass over
  1075. * the encoded data */
  1076. if (!buf && !inf)
  1077. {
  1078. *in += len;
  1079. return 1;
  1080. }
  1081. while(len > 0)
  1082. {
  1083. q = p;
  1084. /* Check for EOC */
  1085. if (asn1_check_eoc(&p, len))
  1086. {
  1087. /* EOC is illegal outside indefinite length
  1088. * constructed form */
  1089. if (!inf)
  1090. {
  1091. OPENSSL_PUT_ERROR(ASN1, ASN1_R_UNEXPECTED_EOC);
  1092. return 0;
  1093. }
  1094. inf = 0;
  1095. break;
  1096. }
  1097. if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
  1098. len, tag, aclass, 0, NULL))
  1099. {
  1100. OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR);
  1101. return 0;
  1102. }
  1103. /* If indefinite length constructed update max length */
  1104. if (cst)
  1105. {
  1106. if (depth >= ASN1_MAX_STRING_NEST)
  1107. {
  1108. OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_STRING);
  1109. return 0;
  1110. }
  1111. if (!asn1_collect(buf, &p, plen, ininf, tag, aclass,
  1112. depth + 1))
  1113. return 0;
  1114. }
  1115. else if (plen && !collect_data(buf, &p, plen))
  1116. return 0;
  1117. len -= p - q;
  1118. }
  1119. if (inf)
  1120. {
  1121. OPENSSL_PUT_ERROR(ASN1, ASN1_R_MISSING_EOC);
  1122. return 0;
  1123. }
  1124. *in = p;
  1125. return 1;
  1126. }
  1127. static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
  1128. {
  1129. int len;
  1130. if (buf)
  1131. {
  1132. len = buf->length;
  1133. if (!BUF_MEM_grow_clean(buf, len + plen))
  1134. {
  1135. OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
  1136. return 0;
  1137. }
  1138. memcpy(buf->data + len, *p, plen);
  1139. }
  1140. *p += plen;
  1141. return 1;
  1142. }
  1143. /* Check for ASN1 EOC and swallow it if found */
  1144. static int asn1_check_eoc(const unsigned char **in, long len)
  1145. {
  1146. const unsigned char *p;
  1147. if (len < 2) return 0;
  1148. p = *in;
  1149. if (!p[0] && !p[1])
  1150. {
  1151. *in += 2;
  1152. return 1;
  1153. }
  1154. return 0;
  1155. }
  1156. /* Check an ASN1 tag and length: a bit like ASN1_get_object
  1157. * but it sets the length for indefinite length constructed
  1158. * form, we don't know the exact length but we can set an
  1159. * upper bound to the amount of data available minus the
  1160. * header length just read.
  1161. */
  1162. static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
  1163. char *inf, char *cst,
  1164. const unsigned char **in, long len,
  1165. int exptag, int expclass, char opt,
  1166. ASN1_TLC *ctx)
  1167. {
  1168. int i;
  1169. int ptag, pclass;
  1170. long plen;
  1171. const unsigned char *p, *q;
  1172. p = *in;
  1173. q = p;
  1174. if (ctx && ctx->valid)
  1175. {
  1176. i = ctx->ret;
  1177. plen = ctx->plen;
  1178. pclass = ctx->pclass;
  1179. ptag = ctx->ptag;
  1180. p += ctx->hdrlen;
  1181. }
  1182. else
  1183. {
  1184. i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
  1185. if (ctx)
  1186. {
  1187. ctx->ret = i;
  1188. ctx->plen = plen;
  1189. ctx->pclass = pclass;
  1190. ctx->ptag = ptag;
  1191. ctx->hdrlen = p - q;
  1192. ctx->valid = 1;
  1193. /* If definite length, and no error, length +
  1194. * header can't exceed total amount of data available.
  1195. */
  1196. if (!(i & 0x81) && ((plen + ctx->hdrlen) > len))
  1197. {
  1198. OPENSSL_PUT_ERROR(ASN1, ASN1_R_TOO_LONG);
  1199. asn1_tlc_clear(ctx);
  1200. return 0;
  1201. }
  1202. }
  1203. }
  1204. if (i & 0x80)
  1205. {
  1206. OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_OBJECT_HEADER);
  1207. asn1_tlc_clear(ctx);
  1208. return 0;
  1209. }
  1210. if (exptag >= 0)
  1211. {
  1212. if ((exptag != ptag) || (expclass != pclass))
  1213. {
  1214. /* If type is OPTIONAL, not an error:
  1215. * indicate missing type.
  1216. */
  1217. if (opt) return -1;
  1218. asn1_tlc_clear(ctx);
  1219. OPENSSL_PUT_ERROR(ASN1, ASN1_R_WRONG_TAG);
  1220. return 0;
  1221. }
  1222. /* We have a tag and class match:
  1223. * assume we are going to do something with it */
  1224. asn1_tlc_clear(ctx);
  1225. }
  1226. if (i & 1)
  1227. plen = len - (p - q);
  1228. if (inf)
  1229. *inf = i & 1;
  1230. if (cst)
  1231. *cst = i & V_ASN1_CONSTRUCTED;
  1232. if (olen)
  1233. *olen = plen;
  1234. if (oclass)
  1235. *oclass = pclass;
  1236. if (otag)
  1237. *otag = ptag;
  1238. *in = p;
  1239. return 1;
  1240. }