decoder_lib.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  1. /*
  2. * Copyright 2020-2023 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 <openssl/core_names.h>
  10. #include <openssl/bio.h>
  11. #include <openssl/params.h>
  12. #include <openssl/provider.h>
  13. #include <openssl/evperr.h>
  14. #include <openssl/ecerr.h>
  15. #include <openssl/pkcs12err.h>
  16. #include <openssl/x509err.h>
  17. #include <openssl/trace.h>
  18. #include "internal/bio.h"
  19. #include "internal/provider.h"
  20. #include "internal/namemap.h"
  21. #include "crypto/decoder.h"
  22. #include "encoder_local.h"
  23. #include "internal/e_os.h"
  24. struct decoder_process_data_st {
  25. OSSL_DECODER_CTX *ctx;
  26. /* Current BIO */
  27. BIO *bio;
  28. /* Index of the current decoder instance to be processed */
  29. size_t current_decoder_inst_index;
  30. /* For tracing, count recursion level */
  31. size_t recursion;
  32. /*-
  33. * Flags
  34. */
  35. unsigned int flag_next_level_called : 1;
  36. unsigned int flag_construct_called : 1;
  37. unsigned int flag_input_structure_checked : 1;
  38. };
  39. static int decoder_process(const OSSL_PARAM params[], void *arg);
  40. int OSSL_DECODER_from_bio(OSSL_DECODER_CTX *ctx, BIO *in)
  41. {
  42. struct decoder_process_data_st data;
  43. int ok = 0;
  44. BIO *new_bio = NULL;
  45. unsigned long lasterr;
  46. if (in == NULL) {
  47. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
  48. return 0;
  49. }
  50. if (OSSL_DECODER_CTX_get_num_decoders(ctx) == 0) {
  51. ERR_raise_data(ERR_LIB_OSSL_DECODER, OSSL_DECODER_R_DECODER_NOT_FOUND,
  52. "No decoders were found. For standard decoders you need "
  53. "at least one of the default or base providers "
  54. "available. Did you forget to load them?");
  55. return 0;
  56. }
  57. lasterr = ERR_peek_last_error();
  58. if (BIO_tell(in) < 0) {
  59. new_bio = BIO_new(BIO_f_readbuffer());
  60. if (new_bio == NULL)
  61. return 0;
  62. in = BIO_push(new_bio, in);
  63. }
  64. memset(&data, 0, sizeof(data));
  65. data.ctx = ctx;
  66. data.bio = in;
  67. /* Enable passphrase caching */
  68. (void)ossl_pw_enable_passphrase_caching(&ctx->pwdata);
  69. ok = decoder_process(NULL, &data);
  70. if (!data.flag_construct_called) {
  71. const char *spaces
  72. = ctx->start_input_type != NULL && ctx->input_structure != NULL
  73. ? " "
  74. : "";
  75. const char *input_type_label
  76. = ctx->start_input_type != NULL ? "Input type: " : "";
  77. const char *input_structure_label
  78. = ctx->input_structure != NULL ? "Input structure: " : "";
  79. const char *comma
  80. = ctx->start_input_type != NULL && ctx->input_structure != NULL
  81. ? ", "
  82. : "";
  83. const char *input_type
  84. = ctx->start_input_type != NULL ? ctx->start_input_type : "";
  85. const char *input_structure
  86. = ctx->input_structure != NULL ? ctx->input_structure : "";
  87. if (ERR_peek_last_error() == lasterr || ERR_peek_error() == 0)
  88. /* Prevent spurious decoding error but add at least something */
  89. ERR_raise_data(ERR_LIB_OSSL_DECODER, ERR_R_UNSUPPORTED,
  90. "No supported data to decode. %s%s%s%s%s%s",
  91. spaces, input_type_label, input_type, comma,
  92. input_structure_label, input_structure);
  93. ok = 0;
  94. }
  95. /* Clear any internally cached passphrase */
  96. (void)ossl_pw_clear_passphrase_cache(&ctx->pwdata);
  97. if (new_bio != NULL) {
  98. BIO_pop(new_bio);
  99. BIO_free(new_bio);
  100. }
  101. return ok;
  102. }
  103. #ifndef OPENSSL_NO_STDIO
  104. static BIO *bio_from_file(FILE *fp)
  105. {
  106. BIO *b;
  107. if ((b = BIO_new(BIO_s_file())) == NULL) {
  108. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_BIO_LIB);
  109. return NULL;
  110. }
  111. BIO_set_fp(b, fp, BIO_NOCLOSE);
  112. return b;
  113. }
  114. int OSSL_DECODER_from_fp(OSSL_DECODER_CTX *ctx, FILE *fp)
  115. {
  116. BIO *b = bio_from_file(fp);
  117. int ret = 0;
  118. if (b != NULL)
  119. ret = OSSL_DECODER_from_bio(ctx, b);
  120. BIO_free(b);
  121. return ret;
  122. }
  123. #endif
  124. int OSSL_DECODER_from_data(OSSL_DECODER_CTX *ctx, const unsigned char **pdata,
  125. size_t *pdata_len)
  126. {
  127. BIO *membio;
  128. int ret = 0;
  129. if (pdata == NULL || *pdata == NULL || pdata_len == NULL) {
  130. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
  131. return 0;
  132. }
  133. membio = BIO_new_mem_buf(*pdata, (int)*pdata_len);
  134. if (OSSL_DECODER_from_bio(ctx, membio)) {
  135. *pdata_len = (size_t)BIO_get_mem_data(membio, pdata);
  136. ret = 1;
  137. }
  138. BIO_free(membio);
  139. return ret;
  140. }
  141. int OSSL_DECODER_CTX_set_selection(OSSL_DECODER_CTX *ctx, int selection)
  142. {
  143. if (!ossl_assert(ctx != NULL)) {
  144. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
  145. return 0;
  146. }
  147. /*
  148. * 0 is a valid selection, and means that the caller leaves
  149. * it to code to discover what the selection is.
  150. */
  151. ctx->selection = selection;
  152. return 1;
  153. }
  154. int OSSL_DECODER_CTX_set_input_type(OSSL_DECODER_CTX *ctx,
  155. const char *input_type)
  156. {
  157. if (!ossl_assert(ctx != NULL)) {
  158. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
  159. return 0;
  160. }
  161. /*
  162. * NULL is a valid starting input type, and means that the caller leaves
  163. * it to code to discover what the starting input type is.
  164. */
  165. ctx->start_input_type = input_type;
  166. return 1;
  167. }
  168. int OSSL_DECODER_CTX_set_input_structure(OSSL_DECODER_CTX *ctx,
  169. const char *input_structure)
  170. {
  171. if (!ossl_assert(ctx != NULL)) {
  172. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
  173. return 0;
  174. }
  175. /*
  176. * NULL is a valid starting input structure, and means that the caller
  177. * leaves it to code to discover what the starting input structure is.
  178. */
  179. ctx->input_structure = input_structure;
  180. return 1;
  181. }
  182. OSSL_DECODER_INSTANCE *ossl_decoder_instance_new(OSSL_DECODER *decoder,
  183. void *decoderctx)
  184. {
  185. OSSL_DECODER_INSTANCE *decoder_inst = NULL;
  186. const OSSL_PROVIDER *prov;
  187. OSSL_LIB_CTX *libctx;
  188. const OSSL_PROPERTY_LIST *props;
  189. const OSSL_PROPERTY_DEFINITION *prop;
  190. if (!ossl_assert(decoder != NULL)) {
  191. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
  192. return 0;
  193. }
  194. if ((decoder_inst = OPENSSL_zalloc(sizeof(*decoder_inst))) == NULL)
  195. return 0;
  196. prov = OSSL_DECODER_get0_provider(decoder);
  197. libctx = ossl_provider_libctx(prov);
  198. props = ossl_decoder_parsed_properties(decoder);
  199. if (props == NULL) {
  200. ERR_raise_data(ERR_LIB_OSSL_DECODER, ERR_R_INVALID_PROPERTY_DEFINITION,
  201. "there are no property definitions with decoder %s",
  202. OSSL_DECODER_get0_name(decoder));
  203. goto err;
  204. }
  205. /* The "input" property is mandatory */
  206. prop = ossl_property_find_property(props, libctx, "input");
  207. decoder_inst->input_type = ossl_property_get_string_value(libctx, prop);
  208. decoder_inst->input_type_id = 0;
  209. if (decoder_inst->input_type == NULL) {
  210. ERR_raise_data(ERR_LIB_OSSL_DECODER, ERR_R_INVALID_PROPERTY_DEFINITION,
  211. "the mandatory 'input' property is missing "
  212. "for decoder %s (properties: %s)",
  213. OSSL_DECODER_get0_name(decoder),
  214. OSSL_DECODER_get0_properties(decoder));
  215. goto err;
  216. }
  217. /* The "structure" property is optional */
  218. prop = ossl_property_find_property(props, libctx, "structure");
  219. if (prop != NULL) {
  220. decoder_inst->input_structure
  221. = ossl_property_get_string_value(libctx, prop);
  222. }
  223. if (!OSSL_DECODER_up_ref(decoder)) {
  224. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_INTERNAL_ERROR);
  225. goto err;
  226. }
  227. decoder_inst->decoder = decoder;
  228. decoder_inst->decoderctx = decoderctx;
  229. return decoder_inst;
  230. err:
  231. ossl_decoder_instance_free(decoder_inst);
  232. return NULL;
  233. }
  234. void ossl_decoder_instance_free(OSSL_DECODER_INSTANCE *decoder_inst)
  235. {
  236. if (decoder_inst != NULL) {
  237. if (decoder_inst->decoder != NULL)
  238. decoder_inst->decoder->freectx(decoder_inst->decoderctx);
  239. decoder_inst->decoderctx = NULL;
  240. OSSL_DECODER_free(decoder_inst->decoder);
  241. decoder_inst->decoder = NULL;
  242. OPENSSL_free(decoder_inst);
  243. }
  244. }
  245. OSSL_DECODER_INSTANCE *ossl_decoder_instance_dup(const OSSL_DECODER_INSTANCE *src)
  246. {
  247. OSSL_DECODER_INSTANCE *dest;
  248. const OSSL_PROVIDER *prov;
  249. void *provctx;
  250. if ((dest = OPENSSL_zalloc(sizeof(*dest))) == NULL)
  251. return NULL;
  252. *dest = *src;
  253. if (!OSSL_DECODER_up_ref(dest->decoder)) {
  254. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_INTERNAL_ERROR);
  255. goto err;
  256. }
  257. prov = OSSL_DECODER_get0_provider(dest->decoder);
  258. provctx = OSSL_PROVIDER_get0_provider_ctx(prov);
  259. dest->decoderctx = dest->decoder->newctx(provctx);
  260. if (dest->decoderctx == NULL) {
  261. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_INTERNAL_ERROR);
  262. OSSL_DECODER_free(dest->decoder);
  263. goto err;
  264. }
  265. return dest;
  266. err:
  267. OPENSSL_free(dest);
  268. return NULL;
  269. }
  270. int ossl_decoder_ctx_add_decoder_inst(OSSL_DECODER_CTX *ctx,
  271. OSSL_DECODER_INSTANCE *di)
  272. {
  273. int ok;
  274. if (ctx->decoder_insts == NULL
  275. && (ctx->decoder_insts = sk_OSSL_DECODER_INSTANCE_new_null()) == NULL) {
  276. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_CRYPTO_LIB);
  277. return 0;
  278. }
  279. ok = (sk_OSSL_DECODER_INSTANCE_push(ctx->decoder_insts, di) > 0);
  280. if (ok) {
  281. OSSL_TRACE_BEGIN(DECODER)
  282. {
  283. BIO_printf(trc_out,
  284. "(ctx %p) Added decoder instance %p for decoder %p\n"
  285. " %s with %s\n",
  286. (void *)ctx, (void *)di, (void *)di->decoder,
  287. OSSL_DECODER_get0_name(di->decoder),
  288. OSSL_DECODER_get0_properties(di->decoder));
  289. }
  290. OSSL_TRACE_END(DECODER);
  291. }
  292. return ok;
  293. }
  294. int OSSL_DECODER_CTX_add_decoder(OSSL_DECODER_CTX *ctx, OSSL_DECODER *decoder)
  295. {
  296. OSSL_DECODER_INSTANCE *decoder_inst = NULL;
  297. const OSSL_PROVIDER *prov = NULL;
  298. void *decoderctx = NULL;
  299. void *provctx = NULL;
  300. if (!ossl_assert(ctx != NULL) || !ossl_assert(decoder != NULL)) {
  301. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
  302. return 0;
  303. }
  304. prov = OSSL_DECODER_get0_provider(decoder);
  305. provctx = OSSL_PROVIDER_get0_provider_ctx(prov);
  306. if ((decoderctx = decoder->newctx(provctx)) == NULL
  307. || (decoder_inst = ossl_decoder_instance_new(decoder, decoderctx)) == NULL)
  308. goto err;
  309. /* Avoid double free of decoderctx on further errors */
  310. decoderctx = NULL;
  311. if (!ossl_decoder_ctx_add_decoder_inst(ctx, decoder_inst))
  312. goto err;
  313. return 1;
  314. err:
  315. ossl_decoder_instance_free(decoder_inst);
  316. if (decoderctx != NULL)
  317. decoder->freectx(decoderctx);
  318. return 0;
  319. }
  320. struct collect_extra_decoder_data_st {
  321. OSSL_DECODER_CTX *ctx;
  322. const char *output_type;
  323. int output_type_id;
  324. /*
  325. * 0 to check that the decoder's input type is the same as the decoder name
  326. * 1 to check that the decoder's input type differs from the decoder name
  327. */
  328. enum { IS_SAME = 0,
  329. IS_DIFFERENT = 1 } type_check;
  330. size_t w_prev_start, w_prev_end; /* "previous" decoders */
  331. size_t w_new_start, w_new_end; /* "new" decoders */
  332. };
  333. DEFINE_STACK_OF(OSSL_DECODER)
  334. static void collect_all_decoders(OSSL_DECODER *decoder, void *arg)
  335. {
  336. STACK_OF(OSSL_DECODER) *skdecoders = arg;
  337. if (OSSL_DECODER_up_ref(decoder)
  338. && !sk_OSSL_DECODER_push(skdecoders, decoder))
  339. OSSL_DECODER_free(decoder);
  340. }
  341. static void collect_extra_decoder(OSSL_DECODER *decoder, void *arg)
  342. {
  343. struct collect_extra_decoder_data_st *data = arg;
  344. size_t j;
  345. const OSSL_PROVIDER *prov = OSSL_DECODER_get0_provider(decoder);
  346. void *provctx = OSSL_PROVIDER_get0_provider_ctx(prov);
  347. if (ossl_decoder_fast_is_a(decoder, data->output_type, &data->output_type_id)) {
  348. void *decoderctx = NULL;
  349. OSSL_DECODER_INSTANCE *di = NULL;
  350. OSSL_TRACE_BEGIN(DECODER)
  351. {
  352. BIO_printf(trc_out,
  353. "(ctx %p) [%d] Checking out decoder %p:\n"
  354. " %s with %s\n",
  355. (void *)data->ctx, data->type_check, (void *)decoder,
  356. OSSL_DECODER_get0_name(decoder),
  357. OSSL_DECODER_get0_properties(decoder));
  358. }
  359. OSSL_TRACE_END(DECODER);
  360. /*
  361. * Check that we don't already have this decoder in our stack,
  362. * starting with the previous windows but also looking at what
  363. * we have added in the current window.
  364. */
  365. for (j = data->w_prev_start; j < data->w_new_end; j++) {
  366. OSSL_DECODER_INSTANCE *check_inst = sk_OSSL_DECODER_INSTANCE_value(data->ctx->decoder_insts, j);
  367. if (decoder->base.algodef == check_inst->decoder->base.algodef) {
  368. /* We found it, so don't do anything more */
  369. OSSL_TRACE_BEGIN(DECODER)
  370. {
  371. BIO_printf(trc_out,
  372. " REJECTED: already exists in the chain\n");
  373. }
  374. OSSL_TRACE_END(DECODER);
  375. return;
  376. }
  377. }
  378. if ((decoderctx = decoder->newctx(provctx)) == NULL)
  379. return;
  380. if ((di = ossl_decoder_instance_new(decoder, decoderctx)) == NULL) {
  381. decoder->freectx(decoderctx);
  382. return;
  383. }
  384. switch (data->type_check) {
  385. case IS_SAME:
  386. /* If it differs, this is not a decoder to add for now. */
  387. if (!ossl_decoder_fast_is_a(decoder,
  388. OSSL_DECODER_INSTANCE_get_input_type(di),
  389. &di->input_type_id)) {
  390. ossl_decoder_instance_free(di);
  391. OSSL_TRACE_BEGIN(DECODER)
  392. {
  393. BIO_printf(trc_out,
  394. " REJECTED: input type doesn't match output type\n");
  395. }
  396. OSSL_TRACE_END(DECODER);
  397. return;
  398. }
  399. break;
  400. case IS_DIFFERENT:
  401. /* If it's the same, this is not a decoder to add for now. */
  402. if (ossl_decoder_fast_is_a(decoder,
  403. OSSL_DECODER_INSTANCE_get_input_type(di),
  404. &di->input_type_id)) {
  405. ossl_decoder_instance_free(di);
  406. OSSL_TRACE_BEGIN(DECODER)
  407. {
  408. BIO_printf(trc_out,
  409. " REJECTED: input type matches output type\n");
  410. }
  411. OSSL_TRACE_END(DECODER);
  412. return;
  413. }
  414. break;
  415. }
  416. /*
  417. * Apart from keeping w_new_end up to date, We don't care about
  418. * errors here. If it doesn't collect, then it doesn't...
  419. */
  420. if (!ossl_decoder_ctx_add_decoder_inst(data->ctx, di)) {
  421. ossl_decoder_instance_free(di);
  422. return;
  423. }
  424. data->w_new_end++;
  425. }
  426. }
  427. int OSSL_DECODER_CTX_add_extra(OSSL_DECODER_CTX *ctx,
  428. OSSL_LIB_CTX *libctx, const char *propq)
  429. {
  430. /*
  431. * This function goes through existing decoder methods in
  432. * |ctx->decoder_insts|, and tries to fetch new decoders that produce
  433. * what the existing ones want as input, and push those newly fetched
  434. * decoders on top of the same stack.
  435. * Then it does the same again, but looping over the newly fetched
  436. * decoders, until there are no more decoders to be fetched, or
  437. * when we have done this 10 times.
  438. *
  439. * we do this with sliding windows on the stack by keeping track of indexes
  440. * and of the end.
  441. *
  442. * +----------------+
  443. * | DER to RSA | <--- w_prev_start
  444. * +----------------+
  445. * | DER to DSA |
  446. * +----------------+
  447. * | DER to DH |
  448. * +----------------+
  449. * | PEM to DER | <--- w_prev_end, w_new_start
  450. * +----------------+
  451. * <--- w_new_end
  452. */
  453. struct collect_extra_decoder_data_st data;
  454. size_t depth = 0; /* Counts the number of iterations */
  455. size_t count; /* Calculates how many were added in each iteration */
  456. size_t numdecoders;
  457. STACK_OF(OSSL_DECODER) *skdecoders;
  458. if (!ossl_assert(ctx != NULL)) {
  459. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
  460. return 0;
  461. }
  462. /*
  463. * If there is no stack of OSSL_DECODER_INSTANCE, we have nothing
  464. * more to add. That's fine.
  465. */
  466. if (ctx->decoder_insts == NULL)
  467. return 1;
  468. OSSL_TRACE_BEGIN(DECODER)
  469. {
  470. BIO_printf(trc_out, "(ctx %p) Looking for extra decoders\n",
  471. (void *)ctx);
  472. }
  473. OSSL_TRACE_END(DECODER);
  474. skdecoders = sk_OSSL_DECODER_new_null();
  475. if (skdecoders == NULL) {
  476. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_CRYPTO_LIB);
  477. return 0;
  478. }
  479. OSSL_DECODER_do_all_provided(libctx, collect_all_decoders, skdecoders);
  480. numdecoders = sk_OSSL_DECODER_num(skdecoders);
  481. memset(&data, 0, sizeof(data));
  482. data.ctx = ctx;
  483. data.w_prev_start = 0;
  484. data.w_prev_end = sk_OSSL_DECODER_INSTANCE_num(ctx->decoder_insts);
  485. do {
  486. size_t i, j;
  487. data.w_new_start = data.w_new_end = data.w_prev_end;
  488. /*
  489. * Two iterations:
  490. * 0. All decoders that have the same name as their input type.
  491. * This allows for decoders that unwrap some data in a specific
  492. * encoding, and pass the result on with the same encoding.
  493. * 1. All decoders that a different name than their input type.
  494. */
  495. for (data.type_check = IS_SAME;
  496. data.type_check <= IS_DIFFERENT;
  497. data.type_check++) {
  498. for (i = data.w_prev_start; i < data.w_prev_end; i++) {
  499. OSSL_DECODER_INSTANCE *decoder_inst = sk_OSSL_DECODER_INSTANCE_value(ctx->decoder_insts, i);
  500. data.output_type
  501. = OSSL_DECODER_INSTANCE_get_input_type(decoder_inst);
  502. data.output_type_id = 0;
  503. for (j = 0; j < numdecoders; j++)
  504. collect_extra_decoder(sk_OSSL_DECODER_value(skdecoders, j),
  505. &data);
  506. }
  507. }
  508. /* How many were added in this iteration */
  509. count = data.w_new_end - data.w_new_start;
  510. /* Slide the "previous decoder" windows */
  511. data.w_prev_start = data.w_new_start;
  512. data.w_prev_end = data.w_new_end;
  513. depth++;
  514. } while (count != 0 && depth <= 10);
  515. sk_OSSL_DECODER_pop_free(skdecoders, OSSL_DECODER_free);
  516. return 1;
  517. }
  518. int OSSL_DECODER_CTX_get_num_decoders(OSSL_DECODER_CTX *ctx)
  519. {
  520. if (ctx == NULL || ctx->decoder_insts == NULL)
  521. return 0;
  522. return sk_OSSL_DECODER_INSTANCE_num(ctx->decoder_insts);
  523. }
  524. int OSSL_DECODER_CTX_set_construct(OSSL_DECODER_CTX *ctx,
  525. OSSL_DECODER_CONSTRUCT *construct)
  526. {
  527. if (!ossl_assert(ctx != NULL)) {
  528. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
  529. return 0;
  530. }
  531. ctx->construct = construct;
  532. return 1;
  533. }
  534. int OSSL_DECODER_CTX_set_construct_data(OSSL_DECODER_CTX *ctx,
  535. void *construct_data)
  536. {
  537. if (!ossl_assert(ctx != NULL)) {
  538. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
  539. return 0;
  540. }
  541. ctx->construct_data = construct_data;
  542. return 1;
  543. }
  544. int OSSL_DECODER_CTX_set_cleanup(OSSL_DECODER_CTX *ctx,
  545. OSSL_DECODER_CLEANUP *cleanup)
  546. {
  547. if (!ossl_assert(ctx != NULL)) {
  548. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
  549. return 0;
  550. }
  551. ctx->cleanup = cleanup;
  552. return 1;
  553. }
  554. OSSL_DECODER_CONSTRUCT *
  555. OSSL_DECODER_CTX_get_construct(OSSL_DECODER_CTX *ctx)
  556. {
  557. if (ctx == NULL)
  558. return NULL;
  559. return ctx->construct;
  560. }
  561. void *OSSL_DECODER_CTX_get_construct_data(OSSL_DECODER_CTX *ctx)
  562. {
  563. if (ctx == NULL)
  564. return NULL;
  565. return ctx->construct_data;
  566. }
  567. OSSL_DECODER_CLEANUP *
  568. OSSL_DECODER_CTX_get_cleanup(OSSL_DECODER_CTX *ctx)
  569. {
  570. if (ctx == NULL)
  571. return NULL;
  572. return ctx->cleanup;
  573. }
  574. int OSSL_DECODER_export(OSSL_DECODER_INSTANCE *decoder_inst,
  575. void *reference, size_t reference_sz,
  576. OSSL_CALLBACK *export_cb, void *export_cbarg)
  577. {
  578. OSSL_DECODER *decoder = NULL;
  579. void *decoderctx = NULL;
  580. if (!(ossl_assert(decoder_inst != NULL)
  581. && ossl_assert(reference != NULL)
  582. && ossl_assert(export_cb != NULL)
  583. && ossl_assert(export_cbarg != NULL))) {
  584. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
  585. return 0;
  586. }
  587. decoder = OSSL_DECODER_INSTANCE_get_decoder(decoder_inst);
  588. decoderctx = OSSL_DECODER_INSTANCE_get_decoder_ctx(decoder_inst);
  589. return decoder->export_object(decoderctx, reference, reference_sz,
  590. export_cb, export_cbarg);
  591. }
  592. OSSL_DECODER *
  593. OSSL_DECODER_INSTANCE_get_decoder(OSSL_DECODER_INSTANCE *decoder_inst)
  594. {
  595. if (decoder_inst == NULL)
  596. return NULL;
  597. return decoder_inst->decoder;
  598. }
  599. void *
  600. OSSL_DECODER_INSTANCE_get_decoder_ctx(OSSL_DECODER_INSTANCE *decoder_inst)
  601. {
  602. if (decoder_inst == NULL)
  603. return NULL;
  604. return decoder_inst->decoderctx;
  605. }
  606. const char *
  607. OSSL_DECODER_INSTANCE_get_input_type(OSSL_DECODER_INSTANCE *decoder_inst)
  608. {
  609. if (decoder_inst == NULL)
  610. return NULL;
  611. return decoder_inst->input_type;
  612. }
  613. const char *
  614. OSSL_DECODER_INSTANCE_get_input_structure(OSSL_DECODER_INSTANCE *decoder_inst,
  615. int *was_set)
  616. {
  617. if (decoder_inst == NULL)
  618. return NULL;
  619. *was_set = decoder_inst->flag_input_structure_was_set;
  620. return decoder_inst->input_structure;
  621. }
  622. static int decoder_process(const OSSL_PARAM params[], void *arg)
  623. {
  624. struct decoder_process_data_st *data = arg;
  625. OSSL_DECODER_CTX *ctx = data->ctx;
  626. OSSL_DECODER_INSTANCE *decoder_inst = NULL;
  627. OSSL_DECODER *decoder = NULL;
  628. OSSL_CORE_BIO *cbio = NULL;
  629. BIO *bio = data->bio;
  630. long loc;
  631. size_t i;
  632. int ok = 0;
  633. /* For recursions */
  634. struct decoder_process_data_st new_data;
  635. const char *data_type = NULL;
  636. const char *data_structure = NULL;
  637. /*
  638. * This is an indicator up the call stack that something was indeed
  639. * decoded, leading to a recursive call of this function.
  640. */
  641. data->flag_next_level_called = 1;
  642. memset(&new_data, 0, sizeof(new_data));
  643. new_data.ctx = data->ctx;
  644. new_data.recursion = data->recursion + 1;
  645. #define LEVEL_STR ">>>>>>>>>>>>>>>>"
  646. #define LEVEL (new_data.recursion < sizeof(LEVEL_STR) \
  647. ? &LEVEL_STR[sizeof(LEVEL_STR) - new_data.recursion - 1] \
  648. : LEVEL_STR "...")
  649. if (params == NULL) {
  650. /* First iteration, where we prepare for what is to come */
  651. OSSL_TRACE_BEGIN(DECODER)
  652. {
  653. BIO_printf(trc_out,
  654. "(ctx %p) starting to walk the decoder chain\n",
  655. (void *)new_data.ctx);
  656. }
  657. OSSL_TRACE_END(DECODER);
  658. data->current_decoder_inst_index = OSSL_DECODER_CTX_get_num_decoders(ctx);
  659. bio = data->bio;
  660. } else {
  661. const OSSL_PARAM *p;
  662. const char *trace_data_structure;
  663. decoder_inst = sk_OSSL_DECODER_INSTANCE_value(ctx->decoder_insts,
  664. data->current_decoder_inst_index);
  665. decoder = OSSL_DECODER_INSTANCE_get_decoder(decoder_inst);
  666. data->flag_construct_called = 0;
  667. if (ctx->construct != NULL) {
  668. int rv;
  669. OSSL_TRACE_BEGIN(DECODER)
  670. {
  671. BIO_printf(trc_out,
  672. "(ctx %p) %s Running constructor\n",
  673. (void *)new_data.ctx, LEVEL);
  674. }
  675. OSSL_TRACE_END(DECODER);
  676. rv = ctx->construct(decoder_inst, params, ctx->construct_data);
  677. OSSL_TRACE_BEGIN(DECODER)
  678. {
  679. BIO_printf(trc_out,
  680. "(ctx %p) %s Running constructor => %d\n",
  681. (void *)new_data.ctx, LEVEL, rv);
  682. }
  683. OSSL_TRACE_END(DECODER);
  684. ok = (rv > 0);
  685. if (ok) {
  686. data->flag_construct_called = 1;
  687. goto end;
  688. }
  689. }
  690. /* The constructor didn't return success */
  691. /*
  692. * so we try to use the object we got and feed it to any next
  693. * decoder that will take it. Object references are not
  694. * allowed for this.
  695. * If this data isn't present, decoding has failed.
  696. */
  697. p = OSSL_PARAM_locate_const(params, OSSL_OBJECT_PARAM_DATA);
  698. if (p == NULL || p->data_type != OSSL_PARAM_OCTET_STRING)
  699. goto end;
  700. new_data.bio = BIO_new_mem_buf(p->data, (int)p->data_size);
  701. if (new_data.bio == NULL)
  702. goto end;
  703. bio = new_data.bio;
  704. /* Get the data type if there is one */
  705. p = OSSL_PARAM_locate_const(params, OSSL_OBJECT_PARAM_DATA_TYPE);
  706. if (p != NULL && !OSSL_PARAM_get_utf8_string_ptr(p, &data_type))
  707. goto end;
  708. /* Get the data structure if there is one */
  709. p = OSSL_PARAM_locate_const(params, OSSL_OBJECT_PARAM_DATA_STRUCTURE);
  710. if (p != NULL && !OSSL_PARAM_get_utf8_string_ptr(p, &data_structure))
  711. goto end;
  712. /*
  713. * If the data structure is "type-specific" and the data type is
  714. * given, we drop the data structure. The reasoning is that the
  715. * data type is already enough to find the applicable next decoder,
  716. * so an additional "type-specific" data structure is extraneous.
  717. *
  718. * Furthermore, if the OSSL_DECODER caller asked for a type specific
  719. * structure under another name, such as "DH", we get a mismatch
  720. * if the data structure we just received is "type-specific".
  721. * There's only so much you can do without infusing this code with
  722. * too special knowledge.
  723. */
  724. trace_data_structure = data_structure;
  725. if (data_type != NULL && data_structure != NULL
  726. && OPENSSL_strcasecmp(data_structure, "type-specific") == 0)
  727. data_structure = NULL;
  728. OSSL_TRACE_BEGIN(DECODER)
  729. {
  730. BIO_printf(trc_out,
  731. "(ctx %p) %s incoming from previous decoder (%p):\n"
  732. " data type: %s, data structure: %s%s\n",
  733. (void *)new_data.ctx, LEVEL, (void *)decoder,
  734. data_type, trace_data_structure,
  735. (trace_data_structure == data_structure
  736. ? ""
  737. : " (dropped)"));
  738. }
  739. OSSL_TRACE_END(DECODER);
  740. }
  741. /*
  742. * If we have no more decoders to look through at this point,
  743. * we failed
  744. */
  745. if (data->current_decoder_inst_index == 0)
  746. goto end;
  747. if ((loc = BIO_tell(bio)) < 0) {
  748. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_BIO_LIB);
  749. goto end;
  750. }
  751. if ((cbio = ossl_core_bio_new_from_bio(bio)) == NULL) {
  752. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_BIO_LIB);
  753. goto end;
  754. }
  755. for (i = data->current_decoder_inst_index; i-- > 0;) {
  756. OSSL_DECODER_INSTANCE *new_decoder_inst = sk_OSSL_DECODER_INSTANCE_value(ctx->decoder_insts, i);
  757. OSSL_DECODER *new_decoder = OSSL_DECODER_INSTANCE_get_decoder(new_decoder_inst);
  758. void *new_decoderctx = OSSL_DECODER_INSTANCE_get_decoder_ctx(new_decoder_inst);
  759. const char *new_input_type = OSSL_DECODER_INSTANCE_get_input_type(new_decoder_inst);
  760. int n_i_s_was_set = 0; /* We don't care here */
  761. const char *new_input_structure = OSSL_DECODER_INSTANCE_get_input_structure(new_decoder_inst,
  762. &n_i_s_was_set);
  763. OSSL_TRACE_BEGIN(DECODER)
  764. {
  765. BIO_printf(trc_out,
  766. "(ctx %p) %s [%u] Considering decoder instance %p (decoder %p):\n"
  767. " %s with %s\n",
  768. (void *)new_data.ctx, LEVEL, (unsigned int)i,
  769. (void *)new_decoder_inst, (void *)new_decoder,
  770. OSSL_DECODER_get0_name(new_decoder),
  771. OSSL_DECODER_get0_properties(new_decoder));
  772. }
  773. OSSL_TRACE_END(DECODER);
  774. /*
  775. * If |decoder| is NULL, it means we've just started, and the caller
  776. * may have specified what it expects the initial input to be. If
  777. * that's the case, we do this extra check.
  778. */
  779. if (decoder == NULL && ctx->start_input_type != NULL
  780. && OPENSSL_strcasecmp(ctx->start_input_type, new_input_type) != 0) {
  781. OSSL_TRACE_BEGIN(DECODER)
  782. {
  783. BIO_printf(trc_out,
  784. "(ctx %p) %s [%u] the start input type '%s' doesn't match the input type of the considered decoder, skipping...\n",
  785. (void *)new_data.ctx, LEVEL, (unsigned int)i,
  786. ctx->start_input_type);
  787. }
  788. OSSL_TRACE_END(DECODER);
  789. continue;
  790. }
  791. /*
  792. * If we have a previous decoder, we check that the input type
  793. * of the next to be used matches the type of this previous one.
  794. * |new_input_type| holds the value of the "input-type" parameter
  795. * for the decoder we're currently considering.
  796. */
  797. if (decoder != NULL && !ossl_decoder_fast_is_a(decoder, new_input_type, &new_decoder_inst->input_type_id)) {
  798. OSSL_TRACE_BEGIN(DECODER)
  799. {
  800. BIO_printf(trc_out,
  801. "(ctx %p) %s [%u] the input type doesn't match the name of the previous decoder (%p), skipping...\n",
  802. (void *)new_data.ctx, LEVEL, (unsigned int)i,
  803. (void *)decoder);
  804. }
  805. OSSL_TRACE_END(DECODER);
  806. continue;
  807. }
  808. /*
  809. * If the previous decoder gave us a data type, we check to see
  810. * if that matches the decoder we're currently considering.
  811. */
  812. if (data_type != NULL && !OSSL_DECODER_is_a(new_decoder, data_type)) {
  813. OSSL_TRACE_BEGIN(DECODER)
  814. {
  815. BIO_printf(trc_out,
  816. "(ctx %p) %s [%u] the previous decoder's data type doesn't match the name of the considered decoder, skipping...\n",
  817. (void *)new_data.ctx, LEVEL, (unsigned int)i);
  818. }
  819. OSSL_TRACE_END(DECODER);
  820. continue;
  821. }
  822. /*
  823. * If the previous decoder gave us a data structure name, we check
  824. * to see that it matches the input data structure of the decoder
  825. * we're currently considering.
  826. */
  827. if (data_structure != NULL
  828. && (new_input_structure == NULL
  829. || OPENSSL_strcasecmp(data_structure,
  830. new_input_structure)
  831. != 0)) {
  832. OSSL_TRACE_BEGIN(DECODER)
  833. {
  834. BIO_printf(trc_out,
  835. "(ctx %p) %s [%u] the previous decoder's data structure doesn't match the input structure of the considered decoder, skipping...\n",
  836. (void *)new_data.ctx, LEVEL, (unsigned int)i);
  837. }
  838. OSSL_TRACE_END(DECODER);
  839. continue;
  840. }
  841. /*
  842. * If the decoder we're currently considering specifies a structure,
  843. * and this check hasn't already been done earlier in this chain of
  844. * decoder_process() calls, check that it matches the user provided
  845. * input structure, if one is given.
  846. */
  847. if (!data->flag_input_structure_checked
  848. && ctx->input_structure != NULL
  849. && new_input_structure != NULL) {
  850. data->flag_input_structure_checked = 1;
  851. if (OPENSSL_strcasecmp(new_input_structure,
  852. ctx->input_structure)
  853. != 0) {
  854. OSSL_TRACE_BEGIN(DECODER)
  855. {
  856. BIO_printf(trc_out,
  857. "(ctx %p) %s [%u] the previous decoder's data structure doesn't match the input structure given by the user, skipping...\n",
  858. (void *)new_data.ctx, LEVEL, (unsigned int)i);
  859. }
  860. OSSL_TRACE_END(DECODER);
  861. continue;
  862. }
  863. }
  864. /*
  865. * Checking the return value of BIO_reset() or BIO_seek() is unsafe.
  866. * Furthermore, BIO_reset() is unsafe to use if the source BIO happens
  867. * to be a BIO_s_mem(), because the earlier BIO_tell() gives us zero
  868. * no matter where we are in the underlying buffer we're reading from.
  869. *
  870. * So, we simply do a BIO_seek(), and use BIO_tell() that we're back
  871. * at the same position. This is a best effort attempt, but BIO_seek()
  872. * and BIO_tell() should come as a pair...
  873. */
  874. (void)BIO_seek(bio, loc);
  875. if (BIO_tell(bio) != loc)
  876. goto end;
  877. /* Recurse */
  878. OSSL_TRACE_BEGIN(DECODER)
  879. {
  880. BIO_printf(trc_out,
  881. "(ctx %p) %s [%u] Running decoder instance %p\n",
  882. (void *)new_data.ctx, LEVEL, (unsigned int)i,
  883. (void *)new_decoder_inst);
  884. }
  885. OSSL_TRACE_END(DECODER);
  886. /*
  887. * We only care about errors reported from decoder implementations
  888. * if it returns false (i.e. there was a fatal error).
  889. */
  890. ERR_set_mark();
  891. new_data.current_decoder_inst_index = i;
  892. new_data.flag_input_structure_checked
  893. = data->flag_input_structure_checked;
  894. ok = new_decoder->decode(new_decoderctx, cbio,
  895. new_data.ctx->selection,
  896. decoder_process, &new_data,
  897. ossl_pw_passphrase_callback_dec,
  898. &new_data.ctx->pwdata);
  899. OSSL_TRACE_BEGIN(DECODER)
  900. {
  901. BIO_printf(trc_out,
  902. "(ctx %p) %s [%u] Running decoder instance %p => %d"
  903. " (recursed further: %s, construct called: %s)\n",
  904. (void *)new_data.ctx, LEVEL, (unsigned int)i,
  905. (void *)new_decoder_inst, ok,
  906. new_data.flag_next_level_called ? "yes" : "no",
  907. new_data.flag_construct_called ? "yes" : "no");
  908. }
  909. OSSL_TRACE_END(DECODER);
  910. data->flag_construct_called = new_data.flag_construct_called;
  911. /* Break on error or if we tried to construct an object already */
  912. if (!ok || data->flag_construct_called) {
  913. ERR_clear_last_mark();
  914. break;
  915. }
  916. ERR_pop_to_mark();
  917. /*
  918. * Break if the decoder implementation that we called recursed, since
  919. * that indicates that it successfully decoded something.
  920. */
  921. if (new_data.flag_next_level_called)
  922. break;
  923. }
  924. end:
  925. ossl_core_bio_free(cbio);
  926. BIO_free(new_data.bio);
  927. return ok;
  928. }