decoder_lib.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  1. /*
  2. * Copyright 2020-2022 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. data->flag_construct_called = 1;
  647. ok = (rv > 0);
  648. if (ok)
  649. goto end;
  650. }
  651. /* The constructor didn't return success */
  652. /*
  653. * so we try to use the object we got and feed it to any next
  654. * decoder that will take it. Object references are not
  655. * allowed for this.
  656. * If this data isn't present, decoding has failed.
  657. */
  658. p = OSSL_PARAM_locate_const(params, OSSL_OBJECT_PARAM_DATA);
  659. if (p == NULL || p->data_type != OSSL_PARAM_OCTET_STRING)
  660. goto end;
  661. new_data.bio = BIO_new_mem_buf(p->data, (int)p->data_size);
  662. if (new_data.bio == NULL)
  663. goto end;
  664. bio = new_data.bio;
  665. /* Get the data type if there is one */
  666. p = OSSL_PARAM_locate_const(params, OSSL_OBJECT_PARAM_DATA_TYPE);
  667. if (p != NULL && !OSSL_PARAM_get_utf8_string_ptr(p, &data_type))
  668. goto end;
  669. /* Get the data structure if there is one */
  670. p = OSSL_PARAM_locate_const(params, OSSL_OBJECT_PARAM_DATA_STRUCTURE);
  671. if (p != NULL && !OSSL_PARAM_get_utf8_string_ptr(p, &data_structure))
  672. goto end;
  673. /*
  674. * If the data structure is "type-specific" and the data type is
  675. * given, we drop the data structure. The reasoning is that the
  676. * data type is already enough to find the applicable next decoder,
  677. * so an additional "type-specific" data structure is extraneous.
  678. *
  679. * Furthermore, if the OSSL_DECODER caller asked for a type specific
  680. * structure under another name, such as "DH", we get a mismatch
  681. * if the data structure we just received is "type-specific".
  682. * There's only so much you can do without infusing this code with
  683. * too special knowledge.
  684. */
  685. trace_data_structure = data_structure;
  686. if (data_type != NULL && data_structure != NULL
  687. && OPENSSL_strcasecmp(data_structure, "type-specific") == 0)
  688. data_structure = NULL;
  689. OSSL_TRACE_BEGIN(DECODER) {
  690. BIO_printf(trc_out,
  691. "(ctx %p) %s incoming from previous decoder (%p):\n"
  692. " data type: %s, data structure: %s%s\n",
  693. (void *)new_data.ctx, LEVEL, (void *)decoder,
  694. data_type, trace_data_structure,
  695. (trace_data_structure == data_structure
  696. ? "" : " (dropped)"));
  697. } OSSL_TRACE_END(DECODER);
  698. }
  699. /*
  700. * If we have no more decoders to look through at this point,
  701. * we failed
  702. */
  703. if (data->current_decoder_inst_index == 0)
  704. goto end;
  705. if ((loc = BIO_tell(bio)) < 0) {
  706. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_BIO_LIB);
  707. goto end;
  708. }
  709. if ((cbio = ossl_core_bio_new_from_bio(bio)) == NULL) {
  710. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_MALLOC_FAILURE);
  711. goto end;
  712. }
  713. for (i = data->current_decoder_inst_index; i-- > 0;) {
  714. OSSL_DECODER_INSTANCE *new_decoder_inst =
  715. sk_OSSL_DECODER_INSTANCE_value(ctx->decoder_insts, i);
  716. OSSL_DECODER *new_decoder =
  717. OSSL_DECODER_INSTANCE_get_decoder(new_decoder_inst);
  718. void *new_decoderctx =
  719. OSSL_DECODER_INSTANCE_get_decoder_ctx(new_decoder_inst);
  720. const char *new_input_type =
  721. OSSL_DECODER_INSTANCE_get_input_type(new_decoder_inst);
  722. int n_i_s_was_set = 0; /* We don't care here */
  723. const char *new_input_structure =
  724. OSSL_DECODER_INSTANCE_get_input_structure(new_decoder_inst,
  725. &n_i_s_was_set);
  726. OSSL_TRACE_BEGIN(DECODER) {
  727. BIO_printf(trc_out,
  728. "(ctx %p) %s [%u] Considering decoder instance %p (decoder %p):\n"
  729. " %s with %s\n",
  730. (void *)new_data.ctx, LEVEL, (unsigned int)i,
  731. (void *)new_decoder_inst, (void *)new_decoder,
  732. OSSL_DECODER_get0_name(new_decoder),
  733. OSSL_DECODER_get0_properties(new_decoder));
  734. } OSSL_TRACE_END(DECODER);
  735. /*
  736. * If |decoder| is NULL, it means we've just started, and the caller
  737. * may have specified what it expects the initial input to be. If
  738. * that's the case, we do this extra check.
  739. */
  740. if (decoder == NULL && ctx->start_input_type != NULL
  741. && OPENSSL_strcasecmp(ctx->start_input_type, new_input_type) != 0) {
  742. OSSL_TRACE_BEGIN(DECODER) {
  743. BIO_printf(trc_out,
  744. "(ctx %p) %s [%u] the start input type '%s' doesn't match the input type of the considered decoder, skipping...\n",
  745. (void *)new_data.ctx, LEVEL, (unsigned int)i,
  746. ctx->start_input_type);
  747. } OSSL_TRACE_END(DECODER);
  748. continue;
  749. }
  750. /*
  751. * If we have a previous decoder, we check that the input type
  752. * of the next to be used matches the type of this previous one.
  753. * |new_input_type| holds the value of the "input-type" parameter
  754. * for the decoder we're currently considering.
  755. */
  756. if (decoder != NULL && !ossl_decoder_fast_is_a(decoder, new_input_type,
  757. &new_decoder_inst->input_type_id)) {
  758. OSSL_TRACE_BEGIN(DECODER) {
  759. BIO_printf(trc_out,
  760. "(ctx %p) %s [%u] the input type doesn't match the name of the previous decoder (%p), skipping...\n",
  761. (void *)new_data.ctx, LEVEL, (unsigned int)i,
  762. (void *)decoder);
  763. } OSSL_TRACE_END(DECODER);
  764. continue;
  765. }
  766. /*
  767. * If the previous decoder gave us a data type, we check to see
  768. * if that matches the decoder we're currently considering.
  769. */
  770. if (data_type != NULL && !OSSL_DECODER_is_a(new_decoder, data_type)) {
  771. OSSL_TRACE_BEGIN(DECODER) {
  772. BIO_printf(trc_out,
  773. "(ctx %p) %s [%u] the previous decoder's data type doesn't match the name of the considered decoder, skipping...\n",
  774. (void *)new_data.ctx, LEVEL, (unsigned int)i);
  775. } OSSL_TRACE_END(DECODER);
  776. continue;
  777. }
  778. /*
  779. * If the previous decoder gave us a data structure name, we check
  780. * to see that it matches the input data structure of the decoder
  781. * we're currently considering.
  782. */
  783. if (data_structure != NULL
  784. && (new_input_structure == NULL
  785. || OPENSSL_strcasecmp(data_structure,
  786. new_input_structure) != 0)) {
  787. OSSL_TRACE_BEGIN(DECODER) {
  788. BIO_printf(trc_out,
  789. "(ctx %p) %s [%u] the previous decoder's data structure doesn't match the input structure of the considered decoder, skipping...\n",
  790. (void *)new_data.ctx, LEVEL, (unsigned int)i);
  791. } OSSL_TRACE_END(DECODER);
  792. continue;
  793. }
  794. /*
  795. * If the decoder we're currently considering specifies a structure,
  796. * and this check hasn't already been done earlier in this chain of
  797. * decoder_process() calls, check that it matches the user provided
  798. * input structure, if one is given.
  799. */
  800. if (!data->flag_input_structure_checked
  801. && ctx->input_structure != NULL
  802. && new_input_structure != NULL) {
  803. data->flag_input_structure_checked = 1;
  804. if (OPENSSL_strcasecmp(new_input_structure,
  805. ctx->input_structure) != 0) {
  806. OSSL_TRACE_BEGIN(DECODER) {
  807. BIO_printf(trc_out,
  808. "(ctx %p) %s [%u] the previous decoder's data structure doesn't match the input structure given by the user, skipping...\n",
  809. (void *)new_data.ctx, LEVEL, (unsigned int)i);
  810. } OSSL_TRACE_END(DECODER);
  811. continue;
  812. }
  813. }
  814. /*
  815. * Checking the return value of BIO_reset() or BIO_seek() is unsafe.
  816. * Furthermore, BIO_reset() is unsafe to use if the source BIO happens
  817. * to be a BIO_s_mem(), because the earlier BIO_tell() gives us zero
  818. * no matter where we are in the underlying buffer we're reading from.
  819. *
  820. * So, we simply do a BIO_seek(), and use BIO_tell() that we're back
  821. * at the same position. This is a best effort attempt, but BIO_seek()
  822. * and BIO_tell() should come as a pair...
  823. */
  824. (void)BIO_seek(bio, loc);
  825. if (BIO_tell(bio) != loc)
  826. goto end;
  827. /* Recurse */
  828. OSSL_TRACE_BEGIN(DECODER) {
  829. BIO_printf(trc_out,
  830. "(ctx %p) %s [%u] Running decoder instance %p\n",
  831. (void *)new_data.ctx, LEVEL, (unsigned int)i,
  832. (void *)new_decoder_inst);
  833. } OSSL_TRACE_END(DECODER);
  834. /*
  835. * We only care about errors reported from decoder implementations
  836. * if it returns false (i.e. there was a fatal error).
  837. */
  838. ERR_set_mark();
  839. new_data.current_decoder_inst_index = i;
  840. new_data.flag_input_structure_checked
  841. = data->flag_input_structure_checked;
  842. ok = new_decoder->decode(new_decoderctx, cbio,
  843. new_data.ctx->selection,
  844. decoder_process, &new_data,
  845. ossl_pw_passphrase_callback_dec,
  846. &new_data.ctx->pwdata);
  847. OSSL_TRACE_BEGIN(DECODER) {
  848. BIO_printf(trc_out,
  849. "(ctx %p) %s [%u] Running decoder instance %p => %d"
  850. " (recursed further: %s, construct called: %s)\n",
  851. (void *)new_data.ctx, LEVEL, (unsigned int)i,
  852. (void *)new_decoder_inst, ok,
  853. new_data.flag_next_level_called ? "yes" : "no",
  854. new_data.flag_construct_called ? "yes" : "no");
  855. } OSSL_TRACE_END(DECODER);
  856. data->flag_construct_called = new_data.flag_construct_called;
  857. /* Break on error or if we tried to construct an object already */
  858. if (!ok || data->flag_construct_called) {
  859. ERR_clear_last_mark();
  860. break;
  861. }
  862. ERR_pop_to_mark();
  863. /*
  864. * Break if the decoder implementation that we called recursed, since
  865. * that indicates that it successfully decoded something.
  866. */
  867. if (new_data.flag_next_level_called)
  868. break;
  869. }
  870. end:
  871. ossl_core_bio_free(cbio);
  872. BIO_free(new_data.bio);
  873. return ok;
  874. }