encoder_lib.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  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. return 0;
  166. if (!OSSL_ENCODER_up_ref(encoder)) {
  167. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_INTERNAL_ERROR);
  168. goto err;
  169. }
  170. prov = OSSL_ENCODER_get0_provider(encoder);
  171. libctx = ossl_provider_libctx(prov);
  172. props = ossl_encoder_parsed_properties(encoder);
  173. if (props == NULL) {
  174. ERR_raise_data(ERR_LIB_OSSL_DECODER, ERR_R_INVALID_PROPERTY_DEFINITION,
  175. "there are no property definitions with encoder %s",
  176. OSSL_ENCODER_get0_name(encoder));
  177. goto err;
  178. }
  179. /* The "output" property is mandatory */
  180. prop = ossl_property_find_property(props, libctx, "output");
  181. encoder_inst->output_type = ossl_property_get_string_value(libctx, prop);
  182. if (encoder_inst->output_type == NULL) {
  183. ERR_raise_data(ERR_LIB_OSSL_DECODER, ERR_R_INVALID_PROPERTY_DEFINITION,
  184. "the mandatory 'output' property is missing "
  185. "for encoder %s (properties: %s)",
  186. OSSL_ENCODER_get0_name(encoder),
  187. OSSL_ENCODER_get0_properties(encoder));
  188. goto err;
  189. }
  190. /* The "structure" property is optional */
  191. prop = ossl_property_find_property(props, libctx, "structure");
  192. if (prop != NULL)
  193. encoder_inst->output_structure
  194. = ossl_property_get_string_value(libctx, prop);
  195. encoder_inst->encoder = encoder;
  196. encoder_inst->encoderctx = encoderctx;
  197. return encoder_inst;
  198. err:
  199. ossl_encoder_instance_free(encoder_inst);
  200. return NULL;
  201. }
  202. void ossl_encoder_instance_free(OSSL_ENCODER_INSTANCE *encoder_inst)
  203. {
  204. if (encoder_inst != NULL) {
  205. if (encoder_inst->encoder != NULL)
  206. encoder_inst->encoder->freectx(encoder_inst->encoderctx);
  207. encoder_inst->encoderctx = NULL;
  208. OSSL_ENCODER_free(encoder_inst->encoder);
  209. encoder_inst->encoder = NULL;
  210. OPENSSL_free(encoder_inst);
  211. }
  212. }
  213. static int ossl_encoder_ctx_add_encoder_inst(OSSL_ENCODER_CTX *ctx,
  214. OSSL_ENCODER_INSTANCE *ei)
  215. {
  216. int ok;
  217. if (ctx->encoder_insts == NULL
  218. && (ctx->encoder_insts =
  219. sk_OSSL_ENCODER_INSTANCE_new_null()) == NULL) {
  220. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_CRYPTO_LIB);
  221. return 0;
  222. }
  223. ok = (sk_OSSL_ENCODER_INSTANCE_push(ctx->encoder_insts, ei) > 0);
  224. if (ok) {
  225. OSSL_TRACE_BEGIN(ENCODER) {
  226. BIO_printf(trc_out,
  227. "(ctx %p) Added encoder instance %p (encoder %p):\n"
  228. " %s with %s\n",
  229. (void *)ctx, (void *)ei, (void *)ei->encoder,
  230. OSSL_ENCODER_get0_name(ei->encoder),
  231. OSSL_ENCODER_get0_properties(ei->encoder));
  232. } OSSL_TRACE_END(ENCODER);
  233. }
  234. return ok;
  235. }
  236. int OSSL_ENCODER_CTX_add_encoder(OSSL_ENCODER_CTX *ctx, OSSL_ENCODER *encoder)
  237. {
  238. OSSL_ENCODER_INSTANCE *encoder_inst = NULL;
  239. const OSSL_PROVIDER *prov = NULL;
  240. void *encoderctx = NULL;
  241. void *provctx = NULL;
  242. if (!ossl_assert(ctx != NULL) || !ossl_assert(encoder != NULL)) {
  243. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
  244. return 0;
  245. }
  246. prov = OSSL_ENCODER_get0_provider(encoder);
  247. provctx = OSSL_PROVIDER_get0_provider_ctx(prov);
  248. if ((encoderctx = encoder->newctx(provctx)) == NULL
  249. || (encoder_inst =
  250. ossl_encoder_instance_new(encoder, encoderctx)) == NULL)
  251. goto err;
  252. /* Avoid double free of encoderctx on further errors */
  253. encoderctx = NULL;
  254. if (!ossl_encoder_ctx_add_encoder_inst(ctx, encoder_inst))
  255. goto err;
  256. return 1;
  257. err:
  258. ossl_encoder_instance_free(encoder_inst);
  259. if (encoderctx != NULL)
  260. encoder->freectx(encoderctx);
  261. return 0;
  262. }
  263. int OSSL_ENCODER_CTX_add_extra(OSSL_ENCODER_CTX *ctx,
  264. OSSL_LIB_CTX *libctx, const char *propq)
  265. {
  266. return 1;
  267. }
  268. int OSSL_ENCODER_CTX_get_num_encoders(OSSL_ENCODER_CTX *ctx)
  269. {
  270. if (ctx == NULL || ctx->encoder_insts == NULL)
  271. return 0;
  272. return sk_OSSL_ENCODER_INSTANCE_num(ctx->encoder_insts);
  273. }
  274. int OSSL_ENCODER_CTX_set_construct(OSSL_ENCODER_CTX *ctx,
  275. OSSL_ENCODER_CONSTRUCT *construct)
  276. {
  277. if (!ossl_assert(ctx != NULL)) {
  278. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
  279. return 0;
  280. }
  281. ctx->construct = construct;
  282. return 1;
  283. }
  284. int OSSL_ENCODER_CTX_set_construct_data(OSSL_ENCODER_CTX *ctx,
  285. void *construct_data)
  286. {
  287. if (!ossl_assert(ctx != NULL)) {
  288. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
  289. return 0;
  290. }
  291. ctx->construct_data = construct_data;
  292. return 1;
  293. }
  294. int OSSL_ENCODER_CTX_set_cleanup(OSSL_ENCODER_CTX *ctx,
  295. OSSL_ENCODER_CLEANUP *cleanup)
  296. {
  297. if (!ossl_assert(ctx != NULL)) {
  298. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
  299. return 0;
  300. }
  301. ctx->cleanup = cleanup;
  302. return 1;
  303. }
  304. OSSL_ENCODER *
  305. OSSL_ENCODER_INSTANCE_get_encoder(OSSL_ENCODER_INSTANCE *encoder_inst)
  306. {
  307. if (encoder_inst == NULL)
  308. return NULL;
  309. return encoder_inst->encoder;
  310. }
  311. void *
  312. OSSL_ENCODER_INSTANCE_get_encoder_ctx(OSSL_ENCODER_INSTANCE *encoder_inst)
  313. {
  314. if (encoder_inst == NULL)
  315. return NULL;
  316. return encoder_inst->encoderctx;
  317. }
  318. const char *
  319. OSSL_ENCODER_INSTANCE_get_output_type(OSSL_ENCODER_INSTANCE *encoder_inst)
  320. {
  321. if (encoder_inst == NULL)
  322. return NULL;
  323. return encoder_inst->output_type;
  324. }
  325. const char *
  326. OSSL_ENCODER_INSTANCE_get_output_structure(OSSL_ENCODER_INSTANCE *encoder_inst)
  327. {
  328. if (encoder_inst == NULL)
  329. return NULL;
  330. return encoder_inst->output_structure;
  331. }
  332. static int encoder_process(struct encoder_process_data_st *data)
  333. {
  334. OSSL_ENCODER_INSTANCE *current_encoder_inst = NULL;
  335. OSSL_ENCODER *current_encoder = NULL;
  336. OSSL_ENCODER_CTX *current_encoder_ctx = NULL;
  337. BIO *allocated_out = NULL;
  338. const void *original_data = NULL;
  339. OSSL_PARAM abstract[10];
  340. const OSSL_PARAM *current_abstract = NULL;
  341. int i;
  342. int ok = -1; /* -1 signifies that the lookup loop gave nothing */
  343. int top = 0;
  344. if (data->next_encoder_inst == NULL) {
  345. /* First iteration, where we prepare for what is to come */
  346. data->count_output_structure =
  347. data->ctx->output_structure == NULL ? -1 : 0;
  348. top = 1;
  349. }
  350. for (i = data->current_encoder_inst_index; i-- > 0;) {
  351. OSSL_ENCODER *next_encoder = NULL;
  352. const char *current_output_type;
  353. const char *current_output_structure;
  354. struct encoder_process_data_st new_data;
  355. if (!top)
  356. next_encoder =
  357. OSSL_ENCODER_INSTANCE_get_encoder(data->next_encoder_inst);
  358. current_encoder_inst =
  359. sk_OSSL_ENCODER_INSTANCE_value(data->ctx->encoder_insts, i);
  360. current_encoder =
  361. OSSL_ENCODER_INSTANCE_get_encoder(current_encoder_inst);
  362. current_encoder_ctx =
  363. OSSL_ENCODER_INSTANCE_get_encoder_ctx(current_encoder_inst);
  364. current_output_type =
  365. OSSL_ENCODER_INSTANCE_get_output_type(current_encoder_inst);
  366. current_output_structure =
  367. OSSL_ENCODER_INSTANCE_get_output_structure(current_encoder_inst);
  368. memset(&new_data, 0, sizeof(new_data));
  369. new_data.ctx = data->ctx;
  370. new_data.current_encoder_inst_index = i;
  371. new_data.next_encoder_inst = current_encoder_inst;
  372. new_data.count_output_structure = data->count_output_structure;
  373. new_data.level = data->level + 1;
  374. OSSL_TRACE_BEGIN(ENCODER) {
  375. BIO_printf(trc_out,
  376. "[%d] (ctx %p) Considering encoder instance %p (encoder %p)\n",
  377. data->level, (void *)data->ctx,
  378. (void *)current_encoder_inst, (void *)current_encoder);
  379. } OSSL_TRACE_END(ENCODER);
  380. /*
  381. * If this is the top call, we check if the output type of the current
  382. * encoder matches the desired output type.
  383. * If this isn't the top call, i.e. this is deeper in the recursion,
  384. * we instead check if the output type of the current encoder matches
  385. * the name of the next encoder (the one found by the parent call).
  386. */
  387. if (top) {
  388. if (data->ctx->output_type != NULL
  389. && OPENSSL_strcasecmp(current_output_type,
  390. data->ctx->output_type) != 0) {
  391. OSSL_TRACE_BEGIN(ENCODER) {
  392. BIO_printf(trc_out,
  393. "[%d] Skipping because current encoder output type (%s) != desired output type (%s)\n",
  394. data->level,
  395. current_output_type, data->ctx->output_type);
  396. } OSSL_TRACE_END(ENCODER);
  397. continue;
  398. }
  399. } else {
  400. if (!OSSL_ENCODER_is_a(next_encoder, current_output_type)) {
  401. OSSL_TRACE_BEGIN(ENCODER) {
  402. BIO_printf(trc_out,
  403. "[%d] Skipping because current encoder output type (%s) != name of encoder %p\n",
  404. data->level,
  405. current_output_type, (void *)next_encoder);
  406. } OSSL_TRACE_END(ENCODER);
  407. continue;
  408. }
  409. }
  410. /*
  411. * If the caller and the current encoder specify an output structure,
  412. * Check if they match. If they do, count the match, otherwise skip
  413. * the current encoder.
  414. */
  415. if (data->ctx->output_structure != NULL
  416. && current_output_structure != NULL) {
  417. if (OPENSSL_strcasecmp(data->ctx->output_structure,
  418. current_output_structure) != 0) {
  419. OSSL_TRACE_BEGIN(ENCODER) {
  420. BIO_printf(trc_out,
  421. "[%d] Skipping because current encoder output structure (%s) != ctx output structure (%s)\n",
  422. data->level,
  423. current_output_structure,
  424. data->ctx->output_structure);
  425. } OSSL_TRACE_END(ENCODER);
  426. continue;
  427. }
  428. data->count_output_structure++;
  429. }
  430. /*
  431. * Recurse to process the encoder implementations before the current
  432. * one.
  433. */
  434. ok = encoder_process(&new_data);
  435. data->prev_encoder_inst = new_data.prev_encoder_inst;
  436. data->running_output = new_data.running_output;
  437. data->running_output_length = new_data.running_output_length;
  438. /*
  439. * ok == -1 means that the recursion call above gave no further
  440. * encoders, and that the one we're currently at should
  441. * be tried.
  442. * ok == 0 means that something failed in the recursion call
  443. * above, making the result unsuitable for a chain.
  444. * In this case, we simply continue to try finding a
  445. * suitable encoder at this recursion level.
  446. * ok == 1 means that the recursion call was successful, and we
  447. * try to use the result at this recursion level.
  448. */
  449. if (ok != 0)
  450. break;
  451. OSSL_TRACE_BEGIN(ENCODER) {
  452. BIO_printf(trc_out,
  453. "[%d] Skipping because recursion level %d failed\n",
  454. data->level, new_data.level);
  455. } OSSL_TRACE_END(ENCODER);
  456. }
  457. /*
  458. * If |i < 0|, we didn't find any useful encoder in this recursion, so
  459. * we do the rest of the process only if |i >= 0|.
  460. */
  461. if (i < 0) {
  462. ok = -1;
  463. OSSL_TRACE_BEGIN(ENCODER) {
  464. BIO_printf(trc_out,
  465. "[%d] (ctx %p) No suitable encoder found\n",
  466. data->level, (void *)data->ctx);
  467. } OSSL_TRACE_END(ENCODER);
  468. } else {
  469. /* Preparations */
  470. switch (ok) {
  471. case 0:
  472. break;
  473. case -1:
  474. /*
  475. * We have reached the beginning of the encoder instance sequence,
  476. * so we prepare the object to be encoded.
  477. */
  478. /*
  479. * |data->count_output_structure| is one of these values:
  480. *
  481. * -1 There is no desired output structure
  482. * 0 There is a desired output structure, and it wasn't
  483. * matched by any of the encoder instances that were
  484. * considered
  485. * >0 There is a desired output structure, and at least one
  486. * of the encoder instances matched it
  487. */
  488. if (data->count_output_structure == 0)
  489. return 0;
  490. original_data =
  491. data->ctx->construct(current_encoder_inst,
  492. data->ctx->construct_data);
  493. /* Also set the data type, using the encoder implementation name */
  494. data->data_type = OSSL_ENCODER_get0_name(current_encoder);
  495. /* Assume that the constructor recorded an error */
  496. if (original_data != NULL)
  497. ok = 1;
  498. else
  499. ok = 0;
  500. break;
  501. case 1:
  502. if (!ossl_assert(data->running_output != NULL)) {
  503. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_INTERNAL_ERROR);
  504. ok = 0;
  505. break;
  506. }
  507. {
  508. /*
  509. * Create an object abstraction from the latest output, which
  510. * was stolen from the previous round.
  511. */
  512. OSSL_PARAM *abstract_p = abstract;
  513. const char *prev_output_structure =
  514. OSSL_ENCODER_INSTANCE_get_output_structure(data->prev_encoder_inst);
  515. *abstract_p++ =
  516. OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE,
  517. (char *)data->data_type, 0);
  518. if (prev_output_structure != NULL)
  519. *abstract_p++ =
  520. OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_STRUCTURE,
  521. (char *)prev_output_structure,
  522. 0);
  523. *abstract_p++ =
  524. OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_DATA,
  525. data->running_output,
  526. data->running_output_length);
  527. *abstract_p = OSSL_PARAM_construct_end();
  528. current_abstract = abstract;
  529. }
  530. break;
  531. }
  532. /* Calling the encoder implementation */
  533. if (ok) {
  534. OSSL_CORE_BIO *cbio = NULL;
  535. BIO *current_out = NULL;
  536. /*
  537. * If we're at the last encoder instance to use, we're setting up
  538. * final output. Otherwise, set up an intermediary memory output.
  539. */
  540. if (top)
  541. current_out = data->bio;
  542. else if ((current_out = allocated_out = BIO_new(BIO_s_mem()))
  543. == NULL)
  544. ok = 0; /* Assume BIO_new() recorded an error */
  545. if (ok)
  546. ok = (cbio = ossl_core_bio_new_from_bio(current_out)) != NULL;
  547. if (ok) {
  548. ok = current_encoder->encode(current_encoder_ctx, cbio,
  549. original_data, current_abstract,
  550. data->ctx->selection,
  551. ossl_pw_passphrase_callback_enc,
  552. &data->ctx->pwdata);
  553. OSSL_TRACE_BEGIN(ENCODER) {
  554. BIO_printf(trc_out,
  555. "[%d] (ctx %p) Running encoder instance %p => %d\n",
  556. data->level, (void *)data->ctx,
  557. (void *)current_encoder_inst, ok);
  558. } OSSL_TRACE_END(ENCODER);
  559. }
  560. ossl_core_bio_free(cbio);
  561. data->prev_encoder_inst = current_encoder_inst;
  562. }
  563. }
  564. /* Cleanup and collecting the result */
  565. OPENSSL_free(data->running_output);
  566. data->running_output = NULL;
  567. /*
  568. * Steal the output from the BIO_s_mem, if we did allocate one.
  569. * That'll be the data for an object abstraction in the next round.
  570. */
  571. if (allocated_out != NULL) {
  572. BUF_MEM *buf;
  573. BIO_get_mem_ptr(allocated_out, &buf);
  574. data->running_output = (unsigned char *)buf->data;
  575. data->running_output_length = buf->length;
  576. memset(buf, 0, sizeof(*buf));
  577. }
  578. BIO_free(allocated_out);
  579. if (original_data != NULL)
  580. data->ctx->cleanup(data->ctx->construct_data);
  581. return ok;
  582. }