2
0

tasn_dec.c 37 KB

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