tasn_dec.c 36 KB

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