tasn_dec.c 39 KB

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