tasn_dec.c 39 KB

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