tasn_dec.c 39 KB

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