tasn_dec.c 39 KB

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