tasn_dec.c 39 KB

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