1
0

decoder_lib.c 34 KB

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