encoder_lib.c 22 KB

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