decoder_lib.c 39 KB

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