tasn_dec.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318
  1. /* tasn_dec.c */
  2. /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
  3. * project 2000.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 2000-2005 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * licensing@OpenSSL.org.
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. *
  53. * This product includes cryptographic software written by Eric Young
  54. * (eay@cryptsoft.com). This product includes software written by Tim
  55. * Hudson (tjh@cryptsoft.com).
  56. *
  57. */
  58. #include <stddef.h>
  59. #include <string.h>
  60. #include <openssl/asn1.h>
  61. #include <openssl/asn1t.h>
  62. #include <openssl/objects.h>
  63. #include <openssl/buffer.h>
  64. #include <openssl/err.h>
  65. static int asn1_check_eoc(const unsigned char **in, long len);
  66. static int asn1_find_end(const unsigned char **in, long len, char inf);
  67. static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
  68. char inf, int tag, int aclass);
  69. static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen);
  70. static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
  71. char *inf, char *cst,
  72. const unsigned char **in, long len,
  73. int exptag, int expclass, char opt,
  74. ASN1_TLC *ctx);
  75. static int asn1_template_ex_d2i(ASN1_VALUE **pval,
  76. const unsigned char **in, long len,
  77. const ASN1_TEMPLATE *tt, char opt,
  78. ASN1_TLC *ctx);
  79. static int asn1_template_noexp_d2i(ASN1_VALUE **val,
  80. const unsigned char **in, long len,
  81. const ASN1_TEMPLATE *tt, char opt,
  82. ASN1_TLC *ctx);
  83. static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
  84. const unsigned char **in, long len,
  85. const ASN1_ITEM *it,
  86. int tag, int aclass, char opt, ASN1_TLC *ctx);
  87. /* Table to convert tags to bit values, used for MSTRING type */
  88. static unsigned long tag2bit[32] = {
  89. 0, 0, 0, B_ASN1_BIT_STRING, /* tags 0 - 3 */
  90. B_ASN1_OCTET_STRING, 0, 0, B_ASN1_UNKNOWN,/* tags 4- 7 */
  91. B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN,/* tags 8-11 */
  92. B_ASN1_UTF8STRING,B_ASN1_UNKNOWN,B_ASN1_UNKNOWN,B_ASN1_UNKNOWN,/* tags 12-15 */
  93. B_ASN1_SEQUENCE,0,B_ASN1_NUMERICSTRING,B_ASN1_PRINTABLESTRING, /* tags 16-19 */
  94. B_ASN1_T61STRING,B_ASN1_VIDEOTEXSTRING,B_ASN1_IA5STRING, /* tags 20-22 */
  95. B_ASN1_UTCTIME, B_ASN1_GENERALIZEDTIME, /* tags 23-24 */
  96. B_ASN1_GRAPHICSTRING,B_ASN1_ISO64STRING,B_ASN1_GENERALSTRING, /* tags 25-27 */
  97. B_ASN1_UNIVERSALSTRING,B_ASN1_UNKNOWN,B_ASN1_BMPSTRING,B_ASN1_UNKNOWN, /* tags 28-31 */
  98. };
  99. unsigned long ASN1_tag2bit(int tag)
  100. {
  101. if ((tag < 0) || (tag > 30)) return 0;
  102. return tag2bit[tag];
  103. }
  104. /* Macro to initialize and invalidate the cache */
  105. #define asn1_tlc_clear(c) if (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(&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(&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 *pchval, **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. ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
  172. ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE);
  173. goto err;
  174. }
  175. return asn1_template_ex_d2i(pval, in, len,
  176. it->templates, opt, ctx);
  177. }
  178. return asn1_d2i_ex_primitive(pval, in, len, it,
  179. tag, aclass, opt, ctx);
  180. break;
  181. case ASN1_ITYPE_MSTRING:
  182. p = *in;
  183. /* Just read in tag and class */
  184. ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
  185. &p, len, -1, 0, 1, ctx);
  186. if (!ret)
  187. {
  188. ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
  189. ERR_R_NESTED_ASN1_ERROR);
  190. goto err;
  191. }
  192. /* Must be UNIVERSAL class */
  193. if (oclass != V_ASN1_UNIVERSAL)
  194. {
  195. /* If OPTIONAL, assume this is OK */
  196. if (opt) return -1;
  197. ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
  198. ASN1_R_MSTRING_NOT_UNIVERSAL);
  199. goto err;
  200. }
  201. /* Check tag matches bit map */
  202. if (!(ASN1_tag2bit(otag) & it->utype))
  203. {
  204. /* If OPTIONAL, assume this is OK */
  205. if (opt)
  206. return -1;
  207. ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
  208. ASN1_R_MSTRING_WRONG_TAG);
  209. goto err;
  210. }
  211. return asn1_d2i_ex_primitive(pval, in, len,
  212. it, otag, 0, 0, ctx);
  213. case ASN1_ITYPE_EXTERN:
  214. /* Use new style d2i */
  215. ef = it->funcs;
  216. return ef->asn1_ex_d2i(pval, in, len,
  217. it, tag, aclass, opt, ctx);
  218. case ASN1_ITYPE_COMPAT:
  219. /* we must resort to old style evil hackery */
  220. cf = it->funcs;
  221. /* If OPTIONAL see if it is there */
  222. if (opt)
  223. {
  224. int exptag;
  225. p = *in;
  226. if (tag == -1)
  227. exptag = it->utype;
  228. else exptag = tag;
  229. /* Don't care about anything other than presence
  230. * of expected tag */
  231. ret = asn1_check_tlen(NULL, NULL, NULL, NULL, NULL,
  232. &p, len, exptag, aclass, 1, ctx);
  233. if (!ret)
  234. {
  235. ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
  236. ERR_R_NESTED_ASN1_ERROR);
  237. goto err;
  238. }
  239. if (ret == -1)
  240. return -1;
  241. }
  242. /* This is the old style evil hack IMPLICIT handling:
  243. * since the underlying code is expecting a tag and
  244. * class other than the one present we change the
  245. * buffer temporarily then change it back afterwards.
  246. * This doesn't and never did work for tags > 30.
  247. *
  248. * Yes this is *horrible* but it is only needed for
  249. * old style d2i which will hopefully not be around
  250. * for much longer.
  251. * FIXME: should copy the buffer then modify it so
  252. * the input buffer can be const: we should *always*
  253. * copy because the old style d2i might modify the
  254. * buffer.
  255. */
  256. if (tag != -1)
  257. {
  258. wp = *(unsigned char **)in;
  259. imphack = *wp;
  260. if (p == NULL)
  261. {
  262. ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
  263. ERR_R_NESTED_ASN1_ERROR);
  264. goto err;
  265. }
  266. *wp = (unsigned char)((*p & V_ASN1_CONSTRUCTED)
  267. | it->utype);
  268. }
  269. ptmpval = cf->asn1_d2i(pval, in, len);
  270. if (tag != -1)
  271. *wp = imphack;
  272. if (ptmpval)
  273. return 1;
  274. ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
  275. goto err;
  276. case ASN1_ITYPE_CHOICE:
  277. if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
  278. goto auxerr;
  279. /* Allocate structure */
  280. if (!*pval && !ASN1_item_ex_new(pval, it))
  281. {
  282. ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
  283. ERR_R_NESTED_ASN1_ERROR);
  284. goto err;
  285. }
  286. /* CHOICE type, try each possibility in turn */
  287. pchval = NULL;
  288. p = *in;
  289. for (i = 0, tt=it->templates; i < it->tcount; i++, tt++)
  290. {
  291. pchptr = asn1_get_field_ptr(pval, tt);
  292. /* We mark field as OPTIONAL so its absence
  293. * 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. ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
  305. ERR_R_NESTED_ASN1_ERROR);
  306. goto err;
  307. }
  308. /* Did we fall off the end without reading anything? */
  309. if (i == it->tcount)
  310. {
  311. /* If OPTIONAL, this is OK */
  312. if (opt)
  313. {
  314. /* Free and zero it */
  315. ASN1_item_ex_free(pval, it);
  316. return -1;
  317. }
  318. ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
  319. ASN1_R_NO_MATCHING_CHOICE_TYPE);
  320. goto err;
  321. }
  322. asn1_set_choice_selector(pval, i, it);
  323. *in = p;
  324. if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
  325. goto auxerr;
  326. return 1;
  327. case ASN1_ITYPE_NDEF_SEQUENCE:
  328. case ASN1_ITYPE_SEQUENCE:
  329. p = *in;
  330. tmplen = len;
  331. /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
  332. if (tag == -1)
  333. {
  334. tag = V_ASN1_SEQUENCE;
  335. aclass = V_ASN1_UNIVERSAL;
  336. }
  337. /* Get SEQUENCE length and update len, p */
  338. ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,
  339. &p, len, tag, aclass, opt, ctx);
  340. if (!ret)
  341. {
  342. ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
  343. ERR_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. ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
  358. ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
  359. goto err;
  360. }
  361. if (!*pval && !ASN1_item_ex_new(pval, it))
  362. {
  363. ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
  364. ERR_R_NESTED_ASN1_ERROR);
  365. goto err;
  366. }
  367. if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
  368. goto auxerr;
  369. /* Get each field entry */
  370. for (i = 0, tt = it->templates; i < it->tcount; i++, tt++)
  371. {
  372. const ASN1_TEMPLATE *seqtt;
  373. ASN1_VALUE **pseqval;
  374. seqtt = asn1_do_adb(pval, tt, 1);
  375. if (!seqtt)
  376. goto err;
  377. pseqval = asn1_get_field_ptr(pval, seqtt);
  378. /* Have we ran out of data? */
  379. if (!len)
  380. break;
  381. q = p;
  382. if (asn1_check_eoc(&p, len))
  383. {
  384. if (!seq_eoc)
  385. {
  386. ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
  387. ASN1_R_UNEXPECTED_EOC);
  388. goto err;
  389. }
  390. len -= p - q;
  391. seq_eoc = 0;
  392. q = p;
  393. break;
  394. }
  395. /* This determines the OPTIONAL flag value. The field
  396. * cannot be omitted if it is the last of a SEQUENCE
  397. * and there is still data to be read. This isn't
  398. * strictly necessary but it increases efficiency in
  399. * some cases.
  400. */
  401. if (i == (it->tcount - 1))
  402. isopt = 0;
  403. else isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);
  404. /* attempt to read in field, allowing each to be
  405. * OPTIONAL */
  406. ret = asn1_template_ex_d2i(pseqval, &p, len,
  407. seqtt, isopt, ctx);
  408. if (!ret)
  409. {
  410. errtt = seqtt;
  411. goto err;
  412. }
  413. else if (ret == -1)
  414. {
  415. /* OPTIONAL component absent.
  416. * Free and zero the field.
  417. */
  418. ASN1_template_free(pseqval, seqtt);
  419. continue;
  420. }
  421. /* Update length */
  422. len -= p - q;
  423. }
  424. /* Check for EOC if expecting one */
  425. if (seq_eoc && !asn1_check_eoc(&p, len))
  426. {
  427. ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_MISSING_EOC);
  428. goto err;
  429. }
  430. /* Check all data read */
  431. if (!seq_nolen && len)
  432. {
  433. ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
  434. ASN1_R_SEQUENCE_LENGTH_MISMATCH);
  435. goto err;
  436. }
  437. /* If we get here we've got no more data in the SEQUENCE,
  438. * however we may not have read all fields so check all
  439. * remaining are OPTIONAL and clear any that are.
  440. */
  441. for (; i < it->tcount; tt++, i++)
  442. {
  443. const ASN1_TEMPLATE *seqtt;
  444. seqtt = asn1_do_adb(pval, tt, 1);
  445. if (!seqtt)
  446. goto err;
  447. if (seqtt->flags & ASN1_TFLG_OPTIONAL)
  448. {
  449. ASN1_VALUE **pseqval;
  450. pseqval = asn1_get_field_ptr(pval, seqtt);
  451. ASN1_template_free(pseqval, seqtt);
  452. }
  453. else
  454. {
  455. errtt = seqtt;
  456. ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
  457. ASN1_R_FIELD_MISSING);
  458. goto err;
  459. }
  460. }
  461. /* Save encoding */
  462. if (!asn1_enc_save(pval, *in, p - *in, it))
  463. goto auxerr;
  464. *in = p;
  465. if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
  466. goto auxerr;
  467. return 1;
  468. default:
  469. return 0;
  470. }
  471. auxerr:
  472. ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_AUX_ERROR);
  473. err:
  474. ASN1_item_ex_free(pval, it);
  475. if (errtt)
  476. ERR_add_error_data(4, "Field=", errtt->field_name,
  477. ", Type=", it->sname);
  478. else
  479. ERR_add_error_data(2, "Type=", it->sname);
  480. return 0;
  481. }
  482. /* Templates are handled with two separate functions.
  483. * One handles any EXPLICIT tag and the other handles the rest.
  484. */
  485. static int asn1_template_ex_d2i(ASN1_VALUE **val,
  486. const unsigned char **in, long inlen,
  487. const ASN1_TEMPLATE *tt, char opt,
  488. ASN1_TLC *ctx)
  489. {
  490. int flags, aclass;
  491. int ret;
  492. long len;
  493. const unsigned char *p, *q;
  494. char exp_eoc;
  495. if (!val)
  496. return 0;
  497. flags = tt->flags;
  498. aclass = flags & ASN1_TFLG_TAG_CLASS;
  499. p = *in;
  500. /* Check if EXPLICIT tag expected */
  501. if (flags & ASN1_TFLG_EXPTAG)
  502. {
  503. char cst;
  504. /* Need to work out amount of data available to the inner
  505. * content and where it starts: so read in EXPLICIT header to
  506. * get the info.
  507. */
  508. ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst,
  509. &p, inlen, tt->tag, aclass, opt, ctx);
  510. q = p;
  511. if (!ret)
  512. {
  513. ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
  514. ERR_R_NESTED_ASN1_ERROR);
  515. return 0;
  516. }
  517. else if (ret == -1)
  518. return -1;
  519. if (!cst)
  520. {
  521. ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
  522. ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED);
  523. return 0;
  524. }
  525. /* We've found the field so it can't be OPTIONAL now */
  526. ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx);
  527. if (!ret)
  528. {
  529. ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
  530. ERR_R_NESTED_ASN1_ERROR);
  531. return 0;
  532. }
  533. /* We read the field in OK so update length */
  534. len -= p - q;
  535. if (exp_eoc)
  536. {
  537. /* If NDEF we must have an EOC here */
  538. if (!asn1_check_eoc(&p, len))
  539. {
  540. ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
  541. ASN1_R_MISSING_EOC);
  542. goto err;
  543. }
  544. }
  545. else
  546. {
  547. /* Otherwise we must hit the EXPLICIT tag end or its
  548. * an error */
  549. if (len)
  550. {
  551. ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
  552. ASN1_R_EXPLICIT_LENGTH_MISMATCH);
  553. goto err;
  554. }
  555. }
  556. }
  557. else
  558. return asn1_template_noexp_d2i(val, in, inlen,
  559. tt, opt, ctx);
  560. *in = p;
  561. return 1;
  562. err:
  563. ASN1_template_free(val, tt);
  564. *val = NULL;
  565. return 0;
  566. }
  567. static int asn1_template_noexp_d2i(ASN1_VALUE **val,
  568. const unsigned char **in, long len,
  569. const ASN1_TEMPLATE *tt, char opt,
  570. ASN1_TLC *ctx)
  571. {
  572. int flags, aclass;
  573. int ret;
  574. const unsigned char *p, *q;
  575. if (!val)
  576. return 0;
  577. flags = tt->flags;
  578. aclass = flags & ASN1_TFLG_TAG_CLASS;
  579. p = *in;
  580. q = p;
  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. ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
  606. ERR_R_NESTED_ASN1_ERROR);
  607. return 0;
  608. }
  609. else if (ret == -1)
  610. return -1;
  611. if (!*val)
  612. *val = (ASN1_VALUE *)sk_new_null();
  613. else
  614. {
  615. /* We've got a valid STACK: free up any items present */
  616. STACK *sktmp = (STACK *)*val;
  617. ASN1_VALUE *vtmp;
  618. while(sk_num(sktmp) > 0)
  619. {
  620. vtmp = (ASN1_VALUE *)sk_pop(sktmp);
  621. ASN1_item_ex_free(&vtmp,
  622. ASN1_ITEM_ptr(tt->item));
  623. }
  624. }
  625. if (!*val)
  626. {
  627. ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
  628. ERR_R_MALLOC_FAILURE);
  629. goto err;
  630. }
  631. /* Read as many items as we can */
  632. while(len > 0)
  633. {
  634. ASN1_VALUE *skfield;
  635. q = p;
  636. /* See if EOC found */
  637. if (asn1_check_eoc(&p, len))
  638. {
  639. if (!sk_eoc)
  640. {
  641. ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
  642. ASN1_R_UNEXPECTED_EOC);
  643. goto err;
  644. }
  645. len -= p - q;
  646. sk_eoc = 0;
  647. break;
  648. }
  649. skfield = NULL;
  650. if (!ASN1_item_ex_d2i(&skfield, &p, len,
  651. ASN1_ITEM_ptr(tt->item),
  652. -1, 0, 0, ctx))
  653. {
  654. ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
  655. ERR_R_NESTED_ASN1_ERROR);
  656. goto err;
  657. }
  658. len -= p - q;
  659. if (!sk_push((STACK *)*val, (char *)skfield))
  660. {
  661. ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
  662. ERR_R_MALLOC_FAILURE);
  663. goto err;
  664. }
  665. }
  666. if (sk_eoc)
  667. {
  668. ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ASN1_R_MISSING_EOC);
  669. goto err;
  670. }
  671. }
  672. else if (flags & ASN1_TFLG_IMPTAG)
  673. {
  674. /* IMPLICIT tagging */
  675. ret = ASN1_item_ex_d2i(val, &p, len,
  676. ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt, ctx);
  677. if (!ret)
  678. {
  679. ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
  680. ERR_R_NESTED_ASN1_ERROR);
  681. goto err;
  682. }
  683. else if (ret == -1)
  684. return -1;
  685. }
  686. else
  687. {
  688. /* Nothing special */
  689. ret = ASN1_item_ex_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item),
  690. -1, 0, opt, ctx);
  691. if (!ret)
  692. {
  693. ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
  694. ERR_R_NESTED_ASN1_ERROR);
  695. goto err;
  696. }
  697. else if (ret == -1)
  698. return -1;
  699. }
  700. *in = p;
  701. return 1;
  702. err:
  703. ASN1_template_free(val, tt);
  704. *val = NULL;
  705. return 0;
  706. }
  707. static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
  708. const unsigned char **in, long inlen,
  709. const ASN1_ITEM *it,
  710. int tag, int aclass, char opt, ASN1_TLC *ctx)
  711. {
  712. int ret = 0, utype;
  713. long plen;
  714. char cst, inf, free_cont = 0;
  715. const unsigned char *p;
  716. BUF_MEM buf;
  717. const unsigned char *cont = NULL;
  718. long len;
  719. if (!pval)
  720. {
  721. ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_NULL);
  722. return 0; /* Should never happen */
  723. }
  724. if (it->itype == ASN1_ITYPE_MSTRING)
  725. {
  726. utype = tag;
  727. tag = -1;
  728. }
  729. else
  730. utype = it->utype;
  731. if (utype == V_ASN1_ANY)
  732. {
  733. /* If type is ANY need to figure out type from tag */
  734. unsigned char oclass;
  735. if (tag >= 0)
  736. {
  737. ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
  738. ASN1_R_ILLEGAL_TAGGED_ANY);
  739. return 0;
  740. }
  741. if (opt)
  742. {
  743. ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
  744. ASN1_R_ILLEGAL_OPTIONAL_ANY);
  745. return 0;
  746. }
  747. p = *in;
  748. ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
  749. &p, inlen, -1, 0, 0, ctx);
  750. if (!ret)
  751. {
  752. ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
  753. ERR_R_NESTED_ASN1_ERROR);
  754. return 0;
  755. }
  756. if (oclass != V_ASN1_UNIVERSAL)
  757. utype = V_ASN1_OTHER;
  758. }
  759. if (tag == -1)
  760. {
  761. tag = utype;
  762. aclass = V_ASN1_UNIVERSAL;
  763. }
  764. p = *in;
  765. /* Check header */
  766. ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
  767. &p, inlen, tag, aclass, opt, ctx);
  768. if (!ret)
  769. {
  770. ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);
  771. return 0;
  772. }
  773. else if (ret == -1)
  774. return -1;
  775. /* SEQUENCE, SET and "OTHER" are left in encoded form */
  776. if ((utype == V_ASN1_SEQUENCE)
  777. || (utype == V_ASN1_SET) || (utype == V_ASN1_OTHER))
  778. {
  779. /* Clear context cache for type OTHER because the auto clear
  780. * when we have a exact match wont work
  781. */
  782. if (utype == V_ASN1_OTHER)
  783. {
  784. asn1_tlc_clear(ctx);
  785. }
  786. /* SEQUENCE and SET must be constructed */
  787. else if (!cst)
  788. {
  789. ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
  790. ASN1_R_TYPE_NOT_CONSTRUCTED);
  791. return 0;
  792. }
  793. cont = *in;
  794. /* If indefinite length constructed find the real end */
  795. if (inf)
  796. {
  797. if (!asn1_find_end(&p, plen, inf))
  798. goto err;
  799. len = p - cont;
  800. }
  801. else
  802. {
  803. len = p - cont + plen;
  804. p += plen;
  805. buf.data = NULL;
  806. }
  807. }
  808. else if (cst)
  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))
  820. goto err;
  821. len = buf.length;
  822. /* Append a final null to string */
  823. if (!BUF_MEM_grow_clean(&buf, len + 1))
  824. {
  825. ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
  826. ERR_R_MALLOC_FAILURE);
  827. return 0;
  828. }
  829. buf.data[len] = 0;
  830. cont = (const unsigned char *)buf.data;
  831. free_cont = 1;
  832. }
  833. else
  834. {
  835. cont = p;
  836. len = plen;
  837. p += plen;
  838. }
  839. /* We now have content length and type: translate into a structure */
  840. if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it))
  841. goto err;
  842. *in = p;
  843. ret = 1;
  844. err:
  845. if (free_cont && buf.data) OPENSSL_free(buf.data);
  846. return ret;
  847. }
  848. /* Translate ASN1 content octets into a structure */
  849. int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
  850. int utype, char *free_cont, const ASN1_ITEM *it)
  851. {
  852. ASN1_VALUE **opval = NULL;
  853. ASN1_STRING *stmp;
  854. ASN1_TYPE *typ = NULL;
  855. int ret = 0;
  856. const ASN1_PRIMITIVE_FUNCS *pf;
  857. ASN1_INTEGER **tint;
  858. pf = it->funcs;
  859. if (pf && pf->prim_c2i)
  860. return pf->prim_c2i(pval, cont, len, utype, free_cont, it);
  861. /* If ANY type clear type and set pointer to internal value */
  862. if (it->utype == V_ASN1_ANY)
  863. {
  864. if (!*pval)
  865. {
  866. typ = ASN1_TYPE_new();
  867. if (typ == NULL)
  868. goto err;
  869. *pval = (ASN1_VALUE *)typ;
  870. }
  871. else
  872. typ = (ASN1_TYPE *)*pval;
  873. if (utype != typ->type)
  874. ASN1_TYPE_set(typ, utype, NULL);
  875. opval = pval;
  876. pval = (ASN1_VALUE **)&typ->value.ptr;
  877. }
  878. switch(utype)
  879. {
  880. case V_ASN1_OBJECT:
  881. if (!c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len))
  882. goto err;
  883. break;
  884. case V_ASN1_NULL:
  885. if (len)
  886. {
  887. ASN1err(ASN1_F_ASN1_EX_C2I,
  888. ASN1_R_NULL_IS_WRONG_LENGTH);
  889. goto err;
  890. }
  891. *pval = (ASN1_VALUE *)1;
  892. break;
  893. case V_ASN1_BOOLEAN:
  894. if (len != 1)
  895. {
  896. ASN1err(ASN1_F_ASN1_EX_C2I,
  897. 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. /* All based on ASN1_STRING and handled the same */
  940. if (!*pval)
  941. {
  942. stmp = ASN1_STRING_type_new(utype);
  943. if (!stmp)
  944. {
  945. ASN1err(ASN1_F_ASN1_EX_C2I,
  946. ERR_R_MALLOC_FAILURE);
  947. goto err;
  948. }
  949. *pval = (ASN1_VALUE *)stmp;
  950. }
  951. else
  952. {
  953. stmp = (ASN1_STRING *)*pval;
  954. stmp->type = utype;
  955. }
  956. /* If we've already allocated a buffer use it */
  957. if (*free_cont)
  958. {
  959. if (stmp->data)
  960. OPENSSL_free(stmp->data);
  961. stmp->data = (unsigned char *)cont; /* UGLY CAST! RL */
  962. stmp->length = len;
  963. *free_cont = 0;
  964. }
  965. else
  966. {
  967. if (!ASN1_STRING_set(stmp, cont, len))
  968. {
  969. ASN1err(ASN1_F_ASN1_EX_C2I,
  970. ERR_R_MALLOC_FAILURE);
  971. ASN1_STRING_free(stmp);
  972. *pval = NULL;
  973. goto err;
  974. }
  975. }
  976. break;
  977. }
  978. /* If ASN1_ANY and NULL type fix up value */
  979. if (typ && (utype == V_ASN1_NULL))
  980. typ->value.ptr = NULL;
  981. ret = 1;
  982. err:
  983. if (!ret)
  984. {
  985. ASN1_TYPE_free(typ);
  986. if (opval)
  987. *opval = NULL;
  988. }
  989. return ret;
  990. }
  991. /* This function finds the end of an ASN1 structure when passed its maximum
  992. * length, whether it is indefinite length and a pointer to the content.
  993. * This is more efficient than calling asn1_collect because it does not
  994. * recurse on each indefinite length header.
  995. */
  996. static int asn1_find_end(const unsigned char **in, long len, char inf)
  997. {
  998. int expected_eoc;
  999. long plen;
  1000. const unsigned char *p = *in, *q;
  1001. /* If not indefinite length constructed just add length */
  1002. if (inf == 0)
  1003. {
  1004. *in += len;
  1005. return 1;
  1006. }
  1007. expected_eoc = 1;
  1008. /* Indefinite length constructed form. Find the end when enough EOCs
  1009. * are found. If more indefinite length constructed headers
  1010. * are encountered increment the expected eoc count otherwise just
  1011. * skip to the end of the data.
  1012. */
  1013. while (len > 0)
  1014. {
  1015. if(asn1_check_eoc(&p, len))
  1016. {
  1017. expected_eoc--;
  1018. if (expected_eoc == 0)
  1019. break;
  1020. len -= 2;
  1021. continue;
  1022. }
  1023. q = p;
  1024. /* Just read in a header: only care about the length */
  1025. if(!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len,
  1026. -1, 0, 0, NULL))
  1027. {
  1028. ASN1err(ASN1_F_ASN1_FIND_END, ERR_R_NESTED_ASN1_ERROR);
  1029. return 0;
  1030. }
  1031. if (inf)
  1032. expected_eoc++;
  1033. else
  1034. p += plen;
  1035. len -= p - q;
  1036. }
  1037. if (expected_eoc)
  1038. {
  1039. ASN1err(ASN1_F_ASN1_FIND_END, ASN1_R_MISSING_EOC);
  1040. return 0;
  1041. }
  1042. *in = p;
  1043. return 1;
  1044. }
  1045. /* This function collects the asn1 data from a constructred string
  1046. * type into a buffer. The values of 'in' and 'len' should refer
  1047. * to the contents of the constructed type and 'inf' should be set
  1048. * if it is indefinite length.
  1049. */
  1050. static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
  1051. char inf, int tag, int aclass)
  1052. {
  1053. const unsigned char *p, *q;
  1054. long plen;
  1055. char cst, ininf;
  1056. p = *in;
  1057. inf &= 1;
  1058. /* If no buffer and not indefinite length constructed just pass over
  1059. * the encoded data */
  1060. if (!buf && !inf)
  1061. {
  1062. *in += len;
  1063. return 1;
  1064. }
  1065. while(len > 0)
  1066. {
  1067. q = p;
  1068. /* Check for EOC */
  1069. if (asn1_check_eoc(&p, len))
  1070. {
  1071. /* EOC is illegal outside indefinite length
  1072. * constructed form */
  1073. if (!inf)
  1074. {
  1075. ASN1err(ASN1_F_ASN1_COLLECT,
  1076. ASN1_R_UNEXPECTED_EOC);
  1077. return 0;
  1078. }
  1079. inf = 0;
  1080. break;
  1081. }
  1082. if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
  1083. len, tag, aclass, 0, NULL))
  1084. {
  1085. ASN1err(ASN1_F_ASN1_COLLECT, ERR_R_NESTED_ASN1_ERROR);
  1086. return 0;
  1087. }
  1088. /* If indefinite length constructed update max length */
  1089. if (cst)
  1090. {
  1091. #ifdef OPENSSL_ALLOW_NESTED_ASN1_STRINGS
  1092. if (!asn1_collect(buf, &p, plen, ininf, tag, aclass))
  1093. return 0;
  1094. #else
  1095. ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_NESTED_ASN1_STRING);
  1096. return 0;
  1097. #endif
  1098. }
  1099. else if (plen && !collect_data(buf, &p, plen))
  1100. return 0;
  1101. len -= p - q;
  1102. }
  1103. if (inf)
  1104. {
  1105. ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_MISSING_EOC);
  1106. return 0;
  1107. }
  1108. *in = p;
  1109. return 1;
  1110. }
  1111. static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
  1112. {
  1113. int len;
  1114. if (buf)
  1115. {
  1116. len = buf->length;
  1117. if (!BUF_MEM_grow_clean(buf, len + plen))
  1118. {
  1119. ASN1err(ASN1_F_COLLECT_DATA, ERR_R_MALLOC_FAILURE);
  1120. return 0;
  1121. }
  1122. memcpy(buf->data + len, *p, plen);
  1123. }
  1124. *p += plen;
  1125. return 1;
  1126. }
  1127. /* Check for ASN1 EOC and swallow it if found */
  1128. static int asn1_check_eoc(const unsigned char **in, long len)
  1129. {
  1130. const unsigned char *p;
  1131. if (len < 2) return 0;
  1132. p = *in;
  1133. if (!p[0] && !p[1])
  1134. {
  1135. *in += 2;
  1136. return 1;
  1137. }
  1138. return 0;
  1139. }
  1140. /* Check an ASN1 tag and length: a bit like ASN1_get_object
  1141. * but it sets the length for indefinite length constructed
  1142. * form, we don't know the exact length but we can set an
  1143. * upper bound to the amount of data available minus the
  1144. * header length just read.
  1145. */
  1146. static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
  1147. char *inf, char *cst,
  1148. const unsigned char **in, long len,
  1149. int exptag, int expclass, char opt,
  1150. ASN1_TLC *ctx)
  1151. {
  1152. int i;
  1153. int ptag, pclass;
  1154. long plen;
  1155. const unsigned char *p, *q;
  1156. p = *in;
  1157. q = p;
  1158. if (ctx && ctx->valid)
  1159. {
  1160. i = ctx->ret;
  1161. plen = ctx->plen;
  1162. pclass = ctx->pclass;
  1163. ptag = ctx->ptag;
  1164. p += ctx->hdrlen;
  1165. }
  1166. else
  1167. {
  1168. i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
  1169. if (ctx)
  1170. {
  1171. ctx->ret = i;
  1172. ctx->plen = plen;
  1173. ctx->pclass = pclass;
  1174. ctx->ptag = ptag;
  1175. ctx->hdrlen = p - q;
  1176. ctx->valid = 1;
  1177. /* If definite length, and no error, length +
  1178. * header can't exceed total amount of data available.
  1179. */
  1180. if (!(i & 0x81) && ((plen + ctx->hdrlen) > len))
  1181. {
  1182. ASN1err(ASN1_F_ASN1_CHECK_TLEN,
  1183. ASN1_R_TOO_LONG);
  1184. asn1_tlc_clear(ctx);
  1185. return 0;
  1186. }
  1187. }
  1188. }
  1189. if (i & 0x80)
  1190. {
  1191. ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_BAD_OBJECT_HEADER);
  1192. asn1_tlc_clear(ctx);
  1193. return 0;
  1194. }
  1195. if (exptag >= 0)
  1196. {
  1197. if ((exptag != ptag) || (expclass != pclass))
  1198. {
  1199. /* If type is OPTIONAL, not an error:
  1200. * indicate missing type.
  1201. */
  1202. if (opt) return -1;
  1203. asn1_tlc_clear(ctx);
  1204. ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_WRONG_TAG);
  1205. return 0;
  1206. }
  1207. /* We have a tag and class match:
  1208. * assume we are going to do something with it */
  1209. asn1_tlc_clear(ctx);
  1210. }
  1211. if (i & 1)
  1212. plen = len - (p - q);
  1213. if (inf)
  1214. *inf = i & 1;
  1215. if (cst)
  1216. *cst = i & V_ASN1_CONSTRUCTED;
  1217. if (olen)
  1218. *olen = plen;
  1219. if (oclass)
  1220. *oclass = pclass;
  1221. if (otag)
  1222. *otag = ptag;
  1223. *in = p;
  1224. return 1;
  1225. }