encoder_lib.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. /*
  2. * Copyright 2019-2024 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/encoder.h>
  12. #include <openssl/buffer.h>
  13. #include <openssl/params.h>
  14. #include <openssl/provider.h>
  15. #include <openssl/trace.h>
  16. #include "internal/bio.h"
  17. #include "internal/provider.h"
  18. #include "encoder_local.h"
  19. struct encoder_process_data_st {
  20. OSSL_ENCODER_CTX *ctx;
  21. /* Current BIO */
  22. BIO *bio;
  23. /* Index of the current encoder instance to be processed */
  24. int current_encoder_inst_index;
  25. /* Processing data passed down through recursion */
  26. int level; /* Recursion level */
  27. OSSL_ENCODER_INSTANCE *next_encoder_inst;
  28. int count_output_structure;
  29. /* Processing data passed up through recursion */
  30. OSSL_ENCODER_INSTANCE *prev_encoder_inst;
  31. unsigned char *running_output;
  32. size_t running_output_length;
  33. /* Data type = the name of the first succeeding encoder implementation */
  34. const char *data_type;
  35. };
  36. static int encoder_process(struct encoder_process_data_st *data);
  37. int OSSL_ENCODER_to_bio(OSSL_ENCODER_CTX *ctx, BIO *out)
  38. {
  39. struct encoder_process_data_st data;
  40. memset(&data, 0, sizeof(data));
  41. data.ctx = ctx;
  42. data.bio = out;
  43. data.current_encoder_inst_index = OSSL_ENCODER_CTX_get_num_encoders(ctx);
  44. if (data.current_encoder_inst_index == 0) {
  45. ERR_raise_data(ERR_LIB_OSSL_ENCODER, OSSL_ENCODER_R_ENCODER_NOT_FOUND,
  46. "No encoders were found. For standard encoders you need "
  47. "at least one of the default or base providers "
  48. "available. Did you forget to load them?");
  49. return 0;
  50. }
  51. if (ctx->cleanup == NULL || ctx->construct == NULL) {
  52. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_INIT_FAIL);
  53. return 0;
  54. }
  55. return encoder_process(&data) > 0;
  56. }
  57. #ifndef OPENSSL_NO_STDIO
  58. static BIO *bio_from_file(FILE *fp)
  59. {
  60. BIO *b;
  61. if ((b = BIO_new(BIO_s_file())) == NULL) {
  62. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_BUF_LIB);
  63. return NULL;
  64. }
  65. BIO_set_fp(b, fp, BIO_NOCLOSE);
  66. return b;
  67. }
  68. int OSSL_ENCODER_to_fp(OSSL_ENCODER_CTX *ctx, FILE *fp)
  69. {
  70. BIO *b = bio_from_file(fp);
  71. int ret = 0;
  72. if (b != NULL)
  73. ret = OSSL_ENCODER_to_bio(ctx, b);
  74. BIO_free(b);
  75. return ret;
  76. }
  77. #endif
  78. int OSSL_ENCODER_to_data(OSSL_ENCODER_CTX *ctx, unsigned char **pdata,
  79. size_t *pdata_len)
  80. {
  81. BIO *out;
  82. BUF_MEM *buf = NULL;
  83. int ret = 0;
  84. if (pdata_len == NULL) {
  85. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
  86. return 0;
  87. }
  88. out = BIO_new(BIO_s_mem());
  89. if (out != NULL
  90. && OSSL_ENCODER_to_bio(ctx, out)
  91. && BIO_get_mem_ptr(out, &buf) > 0) {
  92. ret = 1; /* Hope for the best. A too small buffer will clear this */
  93. if (pdata != NULL && *pdata != NULL) {
  94. if (*pdata_len < buf->length)
  95. /*
  96. * It's tempting to do |*pdata_len = (size_t)buf->length|
  97. * However, it's believed to be confusing more than helpful,
  98. * so we don't.
  99. */
  100. ret = 0;
  101. else
  102. *pdata_len -= buf->length;
  103. } else {
  104. /* The buffer with the right size is already allocated for us */
  105. *pdata_len = (size_t)buf->length;
  106. }
  107. if (ret) {
  108. if (pdata != NULL) {
  109. if (*pdata != NULL) {
  110. memcpy(*pdata, buf->data, buf->length);
  111. *pdata += buf->length;
  112. } else {
  113. /* In this case, we steal the data from BIO_s_mem() */
  114. *pdata = (unsigned char *)buf->data;
  115. buf->data = NULL;
  116. }
  117. }
  118. }
  119. }
  120. BIO_free(out);
  121. return ret;
  122. }
  123. int OSSL_ENCODER_CTX_set_selection(OSSL_ENCODER_CTX *ctx, int selection)
  124. {
  125. if (!ossl_assert(ctx != NULL)) {
  126. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
  127. return 0;
  128. }
  129. if (!ossl_assert(selection != 0)) {
  130. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_INVALID_ARGUMENT);
  131. return 0;
  132. }
  133. ctx->selection = selection;
  134. return 1;
  135. }
  136. int OSSL_ENCODER_CTX_set_output_type(OSSL_ENCODER_CTX *ctx,
  137. const char *output_type)
  138. {
  139. if (!ossl_assert(ctx != NULL) || !ossl_assert(output_type != NULL)) {
  140. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
  141. return 0;
  142. }
  143. ctx->output_type = output_type;
  144. return 1;
  145. }
  146. int OSSL_ENCODER_CTX_set_output_structure(OSSL_ENCODER_CTX *ctx,
  147. const char *output_structure)
  148. {
  149. if (!ossl_assert(ctx != NULL) || !ossl_assert(output_structure != NULL)) {
  150. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
  151. return 0;
  152. }
  153. ctx->output_structure = output_structure;
  154. return 1;
  155. }
  156. static OSSL_ENCODER_INSTANCE *ossl_encoder_instance_new(OSSL_ENCODER *encoder,
  157. void *encoderctx)
  158. {
  159. OSSL_ENCODER_INSTANCE *encoder_inst = NULL;
  160. const OSSL_PROVIDER *prov;
  161. OSSL_LIB_CTX *libctx;
  162. const OSSL_PROPERTY_LIST *props;
  163. const OSSL_PROPERTY_DEFINITION *prop;
  164. if (!ossl_assert(encoder != NULL)) {
  165. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
  166. return 0;
  167. }
  168. if ((encoder_inst = OPENSSL_zalloc(sizeof(*encoder_inst))) == NULL)
  169. return 0;
  170. if (!OSSL_ENCODER_up_ref(encoder)) {
  171. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_INTERNAL_ERROR);
  172. goto err;
  173. }
  174. prov = OSSL_ENCODER_get0_provider(encoder);
  175. libctx = ossl_provider_libctx(prov);
  176. props = ossl_encoder_parsed_properties(encoder);
  177. if (props == NULL) {
  178. ERR_raise_data(ERR_LIB_OSSL_DECODER, ERR_R_INVALID_PROPERTY_DEFINITION,
  179. "there are no property definitions with encoder %s",
  180. OSSL_ENCODER_get0_name(encoder));
  181. goto err;
  182. }
  183. /* The "output" property is mandatory */
  184. prop = ossl_property_find_property(props, libctx, "output");
  185. encoder_inst->output_type = ossl_property_get_string_value(libctx, prop);
  186. if (encoder_inst->output_type == NULL) {
  187. ERR_raise_data(ERR_LIB_OSSL_DECODER, ERR_R_INVALID_PROPERTY_DEFINITION,
  188. "the mandatory 'output' property is missing "
  189. "for encoder %s (properties: %s)",
  190. OSSL_ENCODER_get0_name(encoder),
  191. OSSL_ENCODER_get0_properties(encoder));
  192. goto err;
  193. }
  194. /* The "structure" property is optional */
  195. prop = ossl_property_find_property(props, libctx, "structure");
  196. if (prop != NULL)
  197. encoder_inst->output_structure
  198. = ossl_property_get_string_value(libctx, prop);
  199. encoder_inst->encoder = encoder;
  200. encoder_inst->encoderctx = encoderctx;
  201. return encoder_inst;
  202. err:
  203. ossl_encoder_instance_free(encoder_inst);
  204. return NULL;
  205. }
  206. void ossl_encoder_instance_free(OSSL_ENCODER_INSTANCE *encoder_inst)
  207. {
  208. if (encoder_inst != NULL) {
  209. if (encoder_inst->encoder != NULL)
  210. encoder_inst->encoder->freectx(encoder_inst->encoderctx);
  211. encoder_inst->encoderctx = NULL;
  212. OSSL_ENCODER_free(encoder_inst->encoder);
  213. encoder_inst->encoder = NULL;
  214. OPENSSL_free(encoder_inst);
  215. }
  216. }
  217. static int ossl_encoder_ctx_add_encoder_inst(OSSL_ENCODER_CTX *ctx,
  218. OSSL_ENCODER_INSTANCE *ei)
  219. {
  220. int ok;
  221. if (ctx->encoder_insts == NULL
  222. && (ctx->encoder_insts =
  223. sk_OSSL_ENCODER_INSTANCE_new_null()) == NULL) {
  224. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_CRYPTO_LIB);
  225. return 0;
  226. }
  227. ok = (sk_OSSL_ENCODER_INSTANCE_push(ctx->encoder_insts, ei) > 0);
  228. if (ok) {
  229. OSSL_TRACE_BEGIN(ENCODER) {
  230. BIO_printf(trc_out,
  231. "(ctx %p) Added encoder instance %p (encoder %p):\n"
  232. " %s with %s\n",
  233. (void *)ctx, (void *)ei, (void *)ei->encoder,
  234. OSSL_ENCODER_get0_name(ei->encoder),
  235. OSSL_ENCODER_get0_properties(ei->encoder));
  236. } OSSL_TRACE_END(ENCODER);
  237. }
  238. return ok;
  239. }
  240. int OSSL_ENCODER_CTX_add_encoder(OSSL_ENCODER_CTX *ctx, OSSL_ENCODER *encoder)
  241. {
  242. OSSL_ENCODER_INSTANCE *encoder_inst = NULL;
  243. const OSSL_PROVIDER *prov = NULL;
  244. void *encoderctx = NULL;
  245. void *provctx = NULL;
  246. if (!ossl_assert(ctx != NULL) || !ossl_assert(encoder != NULL)) {
  247. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
  248. return 0;
  249. }
  250. prov = OSSL_ENCODER_get0_provider(encoder);
  251. provctx = OSSL_PROVIDER_get0_provider_ctx(prov);
  252. if ((encoderctx = encoder->newctx(provctx)) == NULL
  253. || (encoder_inst =
  254. ossl_encoder_instance_new(encoder, encoderctx)) == NULL)
  255. goto err;
  256. /* Avoid double free of encoderctx on further errors */
  257. encoderctx = NULL;
  258. if (!ossl_encoder_ctx_add_encoder_inst(ctx, encoder_inst))
  259. goto err;
  260. return 1;
  261. err:
  262. ossl_encoder_instance_free(encoder_inst);
  263. if (encoderctx != NULL)
  264. encoder->freectx(encoderctx);
  265. return 0;
  266. }
  267. int OSSL_ENCODER_CTX_add_extra(OSSL_ENCODER_CTX *ctx,
  268. OSSL_LIB_CTX *libctx, const char *propq)
  269. {
  270. return 1;
  271. }
  272. int OSSL_ENCODER_CTX_get_num_encoders(OSSL_ENCODER_CTX *ctx)
  273. {
  274. if (ctx == NULL || ctx->encoder_insts == NULL)
  275. return 0;
  276. return sk_OSSL_ENCODER_INSTANCE_num(ctx->encoder_insts);
  277. }
  278. int OSSL_ENCODER_CTX_set_construct(OSSL_ENCODER_CTX *ctx,
  279. OSSL_ENCODER_CONSTRUCT *construct)
  280. {
  281. if (!ossl_assert(ctx != NULL)) {
  282. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
  283. return 0;
  284. }
  285. ctx->construct = construct;
  286. return 1;
  287. }
  288. int OSSL_ENCODER_CTX_set_construct_data(OSSL_ENCODER_CTX *ctx,
  289. void *construct_data)
  290. {
  291. if (!ossl_assert(ctx != NULL)) {
  292. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
  293. return 0;
  294. }
  295. ctx->construct_data = construct_data;
  296. return 1;
  297. }
  298. int OSSL_ENCODER_CTX_set_cleanup(OSSL_ENCODER_CTX *ctx,
  299. OSSL_ENCODER_CLEANUP *cleanup)
  300. {
  301. if (!ossl_assert(ctx != NULL)) {
  302. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
  303. return 0;
  304. }
  305. ctx->cleanup = cleanup;
  306. return 1;
  307. }
  308. OSSL_ENCODER *
  309. OSSL_ENCODER_INSTANCE_get_encoder(OSSL_ENCODER_INSTANCE *encoder_inst)
  310. {
  311. if (encoder_inst == NULL)
  312. return NULL;
  313. return encoder_inst->encoder;
  314. }
  315. void *
  316. OSSL_ENCODER_INSTANCE_get_encoder_ctx(OSSL_ENCODER_INSTANCE *encoder_inst)
  317. {
  318. if (encoder_inst == NULL)
  319. return NULL;
  320. return encoder_inst->encoderctx;
  321. }
  322. const char *
  323. OSSL_ENCODER_INSTANCE_get_output_type(OSSL_ENCODER_INSTANCE *encoder_inst)
  324. {
  325. if (encoder_inst == NULL)
  326. return NULL;
  327. return encoder_inst->output_type;
  328. }
  329. const char *
  330. OSSL_ENCODER_INSTANCE_get_output_structure(OSSL_ENCODER_INSTANCE *encoder_inst)
  331. {
  332. if (encoder_inst == NULL)
  333. return NULL;
  334. return encoder_inst->output_structure;
  335. }
  336. static int encoder_process(struct encoder_process_data_st *data)
  337. {
  338. OSSL_ENCODER_INSTANCE *current_encoder_inst = NULL;
  339. OSSL_ENCODER *current_encoder = NULL;
  340. OSSL_ENCODER_CTX *current_encoder_ctx = NULL;
  341. BIO *allocated_out = NULL;
  342. const void *original_data = NULL;
  343. OSSL_PARAM abstract[10];
  344. const OSSL_PARAM *current_abstract = NULL;
  345. int i;
  346. int ok = -1; /* -1 signifies that the lookup loop gave nothing */
  347. int top = 0;
  348. if (data->next_encoder_inst == NULL) {
  349. /* First iteration, where we prepare for what is to come */
  350. data->count_output_structure =
  351. data->ctx->output_structure == NULL ? -1 : 0;
  352. top = 1;
  353. }
  354. for (i = data->current_encoder_inst_index; i-- > 0;) {
  355. OSSL_ENCODER *next_encoder = NULL;
  356. const char *current_output_type;
  357. const char *current_output_structure;
  358. struct encoder_process_data_st new_data;
  359. if (!top)
  360. next_encoder =
  361. OSSL_ENCODER_INSTANCE_get_encoder(data->next_encoder_inst);
  362. current_encoder_inst =
  363. sk_OSSL_ENCODER_INSTANCE_value(data->ctx->encoder_insts, i);
  364. current_encoder =
  365. OSSL_ENCODER_INSTANCE_get_encoder(current_encoder_inst);
  366. current_encoder_ctx =
  367. OSSL_ENCODER_INSTANCE_get_encoder_ctx(current_encoder_inst);
  368. current_output_type =
  369. OSSL_ENCODER_INSTANCE_get_output_type(current_encoder_inst);
  370. current_output_structure =
  371. OSSL_ENCODER_INSTANCE_get_output_structure(current_encoder_inst);
  372. memset(&new_data, 0, sizeof(new_data));
  373. new_data.ctx = data->ctx;
  374. new_data.current_encoder_inst_index = i;
  375. new_data.next_encoder_inst = current_encoder_inst;
  376. new_data.count_output_structure = data->count_output_structure;
  377. new_data.level = data->level + 1;
  378. OSSL_TRACE_BEGIN(ENCODER) {
  379. BIO_printf(trc_out,
  380. "[%d] (ctx %p) Considering encoder instance %p (encoder %p)\n",
  381. data->level, (void *)data->ctx,
  382. (void *)current_encoder_inst, (void *)current_encoder);
  383. } OSSL_TRACE_END(ENCODER);
  384. /*
  385. * If this is the top call, we check if the output type of the current
  386. * encoder matches the desired output type.
  387. * If this isn't the top call, i.e. this is deeper in the recursion,
  388. * we instead check if the output type of the current encoder matches
  389. * the name of the next encoder (the one found by the parent call).
  390. */
  391. if (top) {
  392. if (data->ctx->output_type != NULL
  393. && OPENSSL_strcasecmp(current_output_type,
  394. data->ctx->output_type) != 0) {
  395. OSSL_TRACE_BEGIN(ENCODER) {
  396. BIO_printf(trc_out,
  397. "[%d] Skipping because current encoder output type (%s) != desired output type (%s)\n",
  398. data->level,
  399. current_output_type, data->ctx->output_type);
  400. } OSSL_TRACE_END(ENCODER);
  401. continue;
  402. }
  403. } else {
  404. if (!OSSL_ENCODER_is_a(next_encoder, current_output_type)) {
  405. OSSL_TRACE_BEGIN(ENCODER) {
  406. BIO_printf(trc_out,
  407. "[%d] Skipping because current encoder output type (%s) != name of encoder %p\n",
  408. data->level,
  409. current_output_type, (void *)next_encoder);
  410. } OSSL_TRACE_END(ENCODER);
  411. continue;
  412. }
  413. }
  414. /*
  415. * If the caller and the current encoder specify an output structure,
  416. * Check if they match. If they do, count the match, otherwise skip
  417. * the current encoder.
  418. */
  419. if (data->ctx->output_structure != NULL
  420. && current_output_structure != NULL) {
  421. if (OPENSSL_strcasecmp(data->ctx->output_structure,
  422. current_output_structure) != 0) {
  423. OSSL_TRACE_BEGIN(ENCODER) {
  424. BIO_printf(trc_out,
  425. "[%d] Skipping because current encoder output structure (%s) != ctx output structure (%s)\n",
  426. data->level,
  427. current_output_structure,
  428. data->ctx->output_structure);
  429. } OSSL_TRACE_END(ENCODER);
  430. continue;
  431. }
  432. data->count_output_structure++;
  433. }
  434. /*
  435. * Recurse to process the encoder implementations before the current
  436. * one.
  437. */
  438. ok = encoder_process(&new_data);
  439. data->prev_encoder_inst = new_data.prev_encoder_inst;
  440. data->running_output = new_data.running_output;
  441. data->running_output_length = new_data.running_output_length;
  442. /*
  443. * ok == -1 means that the recursion call above gave no further
  444. * encoders, and that the one we're currently at should
  445. * be tried.
  446. * ok == 0 means that something failed in the recursion call
  447. * above, making the result unsuitable for a chain.
  448. * In this case, we simply continue to try finding a
  449. * suitable encoder at this recursion level.
  450. * ok == 1 means that the recursion call was successful, and we
  451. * try to use the result at this recursion level.
  452. */
  453. if (ok != 0)
  454. break;
  455. OSSL_TRACE_BEGIN(ENCODER) {
  456. BIO_printf(trc_out,
  457. "[%d] Skipping because recursion level %d failed\n",
  458. data->level, new_data.level);
  459. } OSSL_TRACE_END(ENCODER);
  460. }
  461. /*
  462. * If |i < 0|, we didn't find any useful encoder in this recursion, so
  463. * we do the rest of the process only if |i >= 0|.
  464. */
  465. if (i < 0) {
  466. ok = -1;
  467. OSSL_TRACE_BEGIN(ENCODER) {
  468. BIO_printf(trc_out,
  469. "[%d] (ctx %p) No suitable encoder found\n",
  470. data->level, (void *)data->ctx);
  471. } OSSL_TRACE_END(ENCODER);
  472. } else {
  473. /* Preparations */
  474. switch (ok) {
  475. case 0:
  476. break;
  477. case -1:
  478. /*
  479. * We have reached the beginning of the encoder instance sequence,
  480. * so we prepare the object to be encoded.
  481. */
  482. /*
  483. * |data->count_output_structure| is one of these values:
  484. *
  485. * -1 There is no desired output structure
  486. * 0 There is a desired output structure, and it wasn't
  487. * matched by any of the encoder instances that were
  488. * considered
  489. * >0 There is a desired output structure, and at least one
  490. * of the encoder instances matched it
  491. */
  492. if (data->count_output_structure == 0)
  493. return 0;
  494. original_data =
  495. data->ctx->construct(current_encoder_inst,
  496. data->ctx->construct_data);
  497. /* Also set the data type, using the encoder implementation name */
  498. data->data_type = OSSL_ENCODER_get0_name(current_encoder);
  499. /* Assume that the constructor recorded an error */
  500. if (original_data != NULL)
  501. ok = 1;
  502. else
  503. ok = 0;
  504. break;
  505. case 1:
  506. if (!ossl_assert(data->running_output != NULL)) {
  507. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_INTERNAL_ERROR);
  508. ok = 0;
  509. break;
  510. }
  511. {
  512. /*
  513. * Create an object abstraction from the latest output, which
  514. * was stolen from the previous round.
  515. */
  516. OSSL_PARAM *abstract_p = abstract;
  517. const char *prev_output_structure =
  518. OSSL_ENCODER_INSTANCE_get_output_structure(data->prev_encoder_inst);
  519. *abstract_p++ =
  520. OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE,
  521. (char *)data->data_type, 0);
  522. if (prev_output_structure != NULL)
  523. *abstract_p++ =
  524. OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_STRUCTURE,
  525. (char *)prev_output_structure,
  526. 0);
  527. *abstract_p++ =
  528. OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_DATA,
  529. data->running_output,
  530. data->running_output_length);
  531. *abstract_p = OSSL_PARAM_construct_end();
  532. current_abstract = abstract;
  533. }
  534. break;
  535. }
  536. /* Calling the encoder implementation */
  537. if (ok) {
  538. OSSL_CORE_BIO *cbio = NULL;
  539. BIO *current_out = NULL;
  540. /*
  541. * If we're at the last encoder instance to use, we're setting up
  542. * final output. Otherwise, set up an intermediary memory output.
  543. */
  544. if (top)
  545. current_out = data->bio;
  546. else if ((current_out = allocated_out = BIO_new(BIO_s_mem()))
  547. == NULL)
  548. ok = 0; /* Assume BIO_new() recorded an error */
  549. if (ok)
  550. ok = (cbio = ossl_core_bio_new_from_bio(current_out)) != NULL;
  551. if (ok) {
  552. ok = current_encoder->encode(current_encoder_ctx, cbio,
  553. original_data, current_abstract,
  554. data->ctx->selection,
  555. ossl_pw_passphrase_callback_enc,
  556. &data->ctx->pwdata);
  557. OSSL_TRACE_BEGIN(ENCODER) {
  558. BIO_printf(trc_out,
  559. "[%d] (ctx %p) Running encoder instance %p => %d\n",
  560. data->level, (void *)data->ctx,
  561. (void *)current_encoder_inst, ok);
  562. } OSSL_TRACE_END(ENCODER);
  563. }
  564. ossl_core_bio_free(cbio);
  565. data->prev_encoder_inst = current_encoder_inst;
  566. }
  567. }
  568. /* Cleanup and collecting the result */
  569. OPENSSL_free(data->running_output);
  570. data->running_output = NULL;
  571. /*
  572. * Steal the output from the BIO_s_mem, if we did allocate one.
  573. * That'll be the data for an object abstraction in the next round.
  574. */
  575. if (allocated_out != NULL) {
  576. BUF_MEM *buf;
  577. BIO_get_mem_ptr(allocated_out, &buf);
  578. data->running_output = (unsigned char *)buf->data;
  579. data->running_output_length = buf->length;
  580. memset(buf, 0, sizeof(*buf));
  581. }
  582. BIO_free(allocated_out);
  583. if (original_data != NULL)
  584. data->ctx->cleanup(data->ctx->construct_data);
  585. return ok;
  586. }