tasn_dec.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220
  1. /*
  2. * Copyright 2000-2021 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. /* All based on ASN1_STRING and handled the same */
  850. if (*pval == NULL) {
  851. stmp = ASN1_STRING_type_new(utype);
  852. if (stmp == NULL) {
  853. ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
  854. goto err;
  855. }
  856. *pval = (ASN1_VALUE *)stmp;
  857. } else {
  858. stmp = (ASN1_STRING *)*pval;
  859. stmp->type = utype;
  860. }
  861. /* If we've already allocated a buffer use it */
  862. if (*free_cont) {
  863. ASN1_STRING_set0(stmp, (unsigned char *)cont /* UGLY CAST! */, len);
  864. *free_cont = 0;
  865. } else {
  866. if (!ASN1_STRING_set(stmp, cont, len)) {
  867. ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
  868. ASN1_STRING_free(stmp);
  869. *pval = NULL;
  870. goto err;
  871. }
  872. }
  873. break;
  874. }
  875. /* If ASN1_ANY and NULL type fix up value */
  876. if (typ && (utype == V_ASN1_NULL))
  877. typ->value.ptr = NULL;
  878. ret = 1;
  879. err:
  880. if (!ret) {
  881. ASN1_TYPE_free(typ);
  882. if (opval)
  883. *opval = NULL;
  884. }
  885. return ret;
  886. }
  887. /*
  888. * This function finds the end of an ASN1 structure when passed its maximum
  889. * length, whether it is indefinite length and a pointer to the content. This
  890. * is more efficient than calling asn1_collect because it does not recurse on
  891. * each indefinite length header.
  892. */
  893. static int asn1_find_end(const unsigned char **in, long len, char inf)
  894. {
  895. uint32_t expected_eoc;
  896. long plen;
  897. const unsigned char *p = *in, *q;
  898. /* If not indefinite length constructed just add length */
  899. if (inf == 0) {
  900. *in += len;
  901. return 1;
  902. }
  903. expected_eoc = 1;
  904. /*
  905. * Indefinite length constructed form. Find the end when enough EOCs are
  906. * found. If more indefinite length constructed headers are encountered
  907. * increment the expected eoc count otherwise just skip to the end of the
  908. * data.
  909. */
  910. while (len > 0) {
  911. if (asn1_check_eoc(&p, len)) {
  912. expected_eoc--;
  913. if (expected_eoc == 0)
  914. break;
  915. len -= 2;
  916. continue;
  917. }
  918. q = p;
  919. /* Just read in a header: only care about the length */
  920. if (!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len,
  921. -1, 0, 0, NULL)) {
  922. ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
  923. return 0;
  924. }
  925. if (inf) {
  926. if (expected_eoc == UINT32_MAX) {
  927. ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
  928. return 0;
  929. }
  930. expected_eoc++;
  931. } else {
  932. p += plen;
  933. }
  934. len -= p - q;
  935. }
  936. if (expected_eoc) {
  937. ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
  938. return 0;
  939. }
  940. *in = p;
  941. return 1;
  942. }
  943. /*
  944. * This function collects the asn1 data from a constructed string type into
  945. * a buffer. The values of 'in' and 'len' should refer to the contents of the
  946. * constructed type and 'inf' should be set if it is indefinite length.
  947. */
  948. #ifndef ASN1_MAX_STRING_NEST
  949. /*
  950. * This determines how many levels of recursion are permitted in ASN1 string
  951. * types. If it is not limited stack overflows can occur. If set to zero no
  952. * recursion is allowed at all. Although zero should be adequate examples
  953. * exist that require a value of 1. So 5 should be more than enough.
  954. */
  955. # define ASN1_MAX_STRING_NEST 5
  956. #endif
  957. static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
  958. char inf, int tag, int aclass, int depth)
  959. {
  960. const unsigned char *p, *q;
  961. long plen;
  962. char cst, ininf;
  963. p = *in;
  964. inf &= 1;
  965. /*
  966. * If no buffer and not indefinite length constructed just pass over the
  967. * encoded data
  968. */
  969. if (!buf && !inf) {
  970. *in += len;
  971. return 1;
  972. }
  973. while (len > 0) {
  974. q = p;
  975. /* Check for EOC */
  976. if (asn1_check_eoc(&p, len)) {
  977. /*
  978. * EOC is illegal outside indefinite length constructed form
  979. */
  980. if (!inf) {
  981. ERR_raise(ERR_LIB_ASN1, ASN1_R_UNEXPECTED_EOC);
  982. return 0;
  983. }
  984. inf = 0;
  985. break;
  986. }
  987. if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
  988. len, tag, aclass, 0, NULL)) {
  989. ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
  990. return 0;
  991. }
  992. /* If indefinite length constructed update max length */
  993. if (cst) {
  994. if (depth >= ASN1_MAX_STRING_NEST) {
  995. ERR_raise(ERR_LIB_ASN1, ASN1_R_NESTED_ASN1_STRING);
  996. return 0;
  997. }
  998. if (!asn1_collect(buf, &p, plen, ininf, tag, aclass, depth + 1))
  999. return 0;
  1000. } else if (plen && !collect_data(buf, &p, plen))
  1001. return 0;
  1002. len -= p - q;
  1003. }
  1004. if (inf) {
  1005. ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
  1006. return 0;
  1007. }
  1008. *in = p;
  1009. return 1;
  1010. }
  1011. static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
  1012. {
  1013. int len;
  1014. if (buf) {
  1015. len = buf->length;
  1016. if (!BUF_MEM_grow_clean(buf, len + plen)) {
  1017. ERR_raise(ERR_LIB_ASN1, ERR_R_BUF_LIB);
  1018. return 0;
  1019. }
  1020. memcpy(buf->data + len, *p, plen);
  1021. }
  1022. *p += plen;
  1023. return 1;
  1024. }
  1025. /* Check for ASN1 EOC and swallow it if found */
  1026. static int asn1_check_eoc(const unsigned char **in, long len)
  1027. {
  1028. const unsigned char *p;
  1029. if (len < 2)
  1030. return 0;
  1031. p = *in;
  1032. if (p[0] == '\0' && p[1] == '\0') {
  1033. *in += 2;
  1034. return 1;
  1035. }
  1036. return 0;
  1037. }
  1038. /*
  1039. * Check an ASN1 tag and length: a bit like ASN1_get_object but it sets the
  1040. * length for indefinite length constructed form, we don't know the exact
  1041. * length but we can set an upper bound to the amount of data available minus
  1042. * the header length just read.
  1043. */
  1044. static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
  1045. char *inf, char *cst,
  1046. const unsigned char **in, long len,
  1047. int exptag, int expclass, char opt, ASN1_TLC *ctx)
  1048. {
  1049. int i;
  1050. int ptag, pclass;
  1051. long plen;
  1052. const unsigned char *p, *q;
  1053. p = *in;
  1054. q = p;
  1055. if (len <= 0) {
  1056. ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_SMALL);
  1057. goto err;
  1058. }
  1059. if (ctx != NULL && ctx->valid) {
  1060. i = ctx->ret;
  1061. plen = ctx->plen;
  1062. pclass = ctx->pclass;
  1063. ptag = ctx->ptag;
  1064. p += ctx->hdrlen;
  1065. } else {
  1066. i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
  1067. if (ctx != NULL) {
  1068. ctx->ret = i;
  1069. ctx->plen = plen;
  1070. ctx->pclass = pclass;
  1071. ctx->ptag = ptag;
  1072. ctx->hdrlen = p - q;
  1073. ctx->valid = 1;
  1074. /*
  1075. * If definite length, and no error, length + header can't exceed
  1076. * total amount of data available.
  1077. */
  1078. if ((i & 0x81) == 0 && (plen + ctx->hdrlen) > len) {
  1079. ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
  1080. goto err;
  1081. }
  1082. }
  1083. }
  1084. if ((i & 0x80) != 0) {
  1085. ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_OBJECT_HEADER);
  1086. goto err;
  1087. }
  1088. if (exptag >= 0) {
  1089. if (exptag != ptag || expclass != pclass) {
  1090. /*
  1091. * If type is OPTIONAL, not an error: indicate missing type.
  1092. */
  1093. if (opt != 0)
  1094. return -1;
  1095. ERR_raise(ERR_LIB_ASN1, ASN1_R_WRONG_TAG);
  1096. goto err;
  1097. }
  1098. /*
  1099. * We have a tag and class match: assume we are going to do something
  1100. * with it
  1101. */
  1102. asn1_tlc_clear(ctx);
  1103. }
  1104. if ((i & 1) != 0)
  1105. plen = len - (p - q);
  1106. if (inf != NULL)
  1107. *inf = i & 1;
  1108. if (cst != NULL)
  1109. *cst = i & V_ASN1_CONSTRUCTED;
  1110. if (olen != NULL)
  1111. *olen = plen;
  1112. if (oclass != NULL)
  1113. *oclass = pclass;
  1114. if (otag != NULL)
  1115. *otag = ptag;
  1116. *in = p;
  1117. return 1;
  1118. err:
  1119. asn1_tlc_clear(ctx);
  1120. return 0;
  1121. }