tasn_dec.c 37 KB

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