2
0

decoder_lib.c 35 KB

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