tasn_dec.c 36 KB

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