Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

tasn_dec.c 32 KiB

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