tasn_dec.c 31 KB

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