encoder_lib.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  1. /*
  2. * Copyright 2019-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 <ctype.h>
  10. #include <openssl/core_names.h>
  11. #include <openssl/bio.h>
  12. #include <openssl/encoder.h>
  13. #include <openssl/buffer.h>
  14. #include <openssl/params.h>
  15. #include <openssl/provider.h>
  16. #include <openssl/trace.h>
  17. #include <crypto/bn.h>
  18. #include "internal/bio.h"
  19. #include "internal/ffc.h"
  20. #include "internal/provider.h"
  21. #include "internal/encoder.h"
  22. #include "encoder_local.h"
  23. /* Number of octets per line */
  24. #define LABELED_BUF_PRINT_WIDTH 15
  25. # ifdef SIXTY_FOUR_BIT_LONG
  26. # define BN_FMTu "%lu"
  27. # define BN_FMTx "%lx"
  28. # endif
  29. # ifdef SIXTY_FOUR_BIT
  30. # define BN_FMTu "%llu"
  31. # define BN_FMTx "%llx"
  32. # endif
  33. # ifdef THIRTY_TWO_BIT
  34. # define BN_FMTu "%u"
  35. # define BN_FMTx "%x"
  36. # endif
  37. struct encoder_process_data_st {
  38. OSSL_ENCODER_CTX *ctx;
  39. /* Current BIO */
  40. BIO *bio;
  41. /* Index of the current encoder instance to be processed */
  42. int current_encoder_inst_index;
  43. /* Processing data passed down through recursion */
  44. int level; /* Recursion level */
  45. OSSL_ENCODER_INSTANCE *next_encoder_inst;
  46. int count_output_structure;
  47. /* Processing data passed up through recursion */
  48. OSSL_ENCODER_INSTANCE *prev_encoder_inst;
  49. unsigned char *running_output;
  50. size_t running_output_length;
  51. /* Data type = the name of the first succeeding encoder implementation */
  52. const char *data_type;
  53. };
  54. static int encoder_process(struct encoder_process_data_st *data);
  55. int OSSL_ENCODER_to_bio(OSSL_ENCODER_CTX *ctx, BIO *out)
  56. {
  57. struct encoder_process_data_st data;
  58. memset(&data, 0, sizeof(data));
  59. data.ctx = ctx;
  60. data.bio = out;
  61. data.current_encoder_inst_index = OSSL_ENCODER_CTX_get_num_encoders(ctx);
  62. if (data.current_encoder_inst_index == 0) {
  63. ERR_raise_data(ERR_LIB_OSSL_ENCODER, OSSL_ENCODER_R_ENCODER_NOT_FOUND,
  64. "No encoders were found. For standard encoders you need "
  65. "at least one of the default or base providers "
  66. "available. Did you forget to load them?");
  67. return 0;
  68. }
  69. if (ctx->cleanup == NULL || ctx->construct == NULL) {
  70. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_INIT_FAIL);
  71. return 0;
  72. }
  73. return encoder_process(&data) > 0;
  74. }
  75. #ifndef OPENSSL_NO_STDIO
  76. static BIO *bio_from_file(FILE *fp)
  77. {
  78. BIO *b;
  79. if ((b = BIO_new(BIO_s_file())) == NULL) {
  80. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_BUF_LIB);
  81. return NULL;
  82. }
  83. BIO_set_fp(b, fp, BIO_NOCLOSE);
  84. return b;
  85. }
  86. int OSSL_ENCODER_to_fp(OSSL_ENCODER_CTX *ctx, FILE *fp)
  87. {
  88. BIO *b = bio_from_file(fp);
  89. int ret = 0;
  90. if (b != NULL)
  91. ret = OSSL_ENCODER_to_bio(ctx, b);
  92. BIO_free(b);
  93. return ret;
  94. }
  95. #endif
  96. int OSSL_ENCODER_to_data(OSSL_ENCODER_CTX *ctx, unsigned char **pdata,
  97. size_t *pdata_len)
  98. {
  99. BIO *out;
  100. BUF_MEM *buf = NULL;
  101. int ret = 0;
  102. if (pdata_len == NULL) {
  103. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
  104. return 0;
  105. }
  106. out = BIO_new(BIO_s_mem());
  107. if (out != NULL
  108. && OSSL_ENCODER_to_bio(ctx, out)
  109. && BIO_get_mem_ptr(out, &buf) > 0) {
  110. ret = 1; /* Hope for the best. A too small buffer will clear this */
  111. if (pdata != NULL && *pdata != NULL) {
  112. if (*pdata_len < buf->length)
  113. /*
  114. * It's tempting to do |*pdata_len = (size_t)buf->length|
  115. * However, it's believed to be confusing more than helpful,
  116. * so we don't.
  117. */
  118. ret = 0;
  119. else
  120. *pdata_len -= buf->length;
  121. } else {
  122. /* The buffer with the right size is already allocated for us */
  123. *pdata_len = (size_t)buf->length;
  124. }
  125. if (ret) {
  126. if (pdata != NULL) {
  127. if (*pdata != NULL) {
  128. memcpy(*pdata, buf->data, buf->length);
  129. *pdata += buf->length;
  130. } else {
  131. /* In this case, we steal the data from BIO_s_mem() */
  132. *pdata = (unsigned char *)buf->data;
  133. buf->data = NULL;
  134. }
  135. }
  136. }
  137. }
  138. BIO_free(out);
  139. return ret;
  140. }
  141. int OSSL_ENCODER_CTX_set_selection(OSSL_ENCODER_CTX *ctx, int selection)
  142. {
  143. if (!ossl_assert(ctx != NULL)) {
  144. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
  145. return 0;
  146. }
  147. if (!ossl_assert(selection != 0)) {
  148. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_INVALID_ARGUMENT);
  149. return 0;
  150. }
  151. ctx->selection = selection;
  152. return 1;
  153. }
  154. int OSSL_ENCODER_CTX_set_output_type(OSSL_ENCODER_CTX *ctx,
  155. const char *output_type)
  156. {
  157. if (!ossl_assert(ctx != NULL) || !ossl_assert(output_type != NULL)) {
  158. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
  159. return 0;
  160. }
  161. ctx->output_type = output_type;
  162. return 1;
  163. }
  164. int OSSL_ENCODER_CTX_set_output_structure(OSSL_ENCODER_CTX *ctx,
  165. const char *output_structure)
  166. {
  167. if (!ossl_assert(ctx != NULL) || !ossl_assert(output_structure != NULL)) {
  168. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
  169. return 0;
  170. }
  171. ctx->output_structure = output_structure;
  172. return 1;
  173. }
  174. static OSSL_ENCODER_INSTANCE *ossl_encoder_instance_new(OSSL_ENCODER *encoder,
  175. void *encoderctx)
  176. {
  177. OSSL_ENCODER_INSTANCE *encoder_inst = NULL;
  178. const OSSL_PROVIDER *prov;
  179. OSSL_LIB_CTX *libctx;
  180. const OSSL_PROPERTY_LIST *props;
  181. const OSSL_PROPERTY_DEFINITION *prop;
  182. if (!ossl_assert(encoder != NULL)) {
  183. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
  184. return 0;
  185. }
  186. if ((encoder_inst = OPENSSL_zalloc(sizeof(*encoder_inst))) == NULL)
  187. return 0;
  188. if (!OSSL_ENCODER_up_ref(encoder)) {
  189. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_INTERNAL_ERROR);
  190. goto err;
  191. }
  192. prov = OSSL_ENCODER_get0_provider(encoder);
  193. libctx = ossl_provider_libctx(prov);
  194. props = ossl_encoder_parsed_properties(encoder);
  195. if (props == NULL) {
  196. ERR_raise_data(ERR_LIB_OSSL_DECODER, ERR_R_INVALID_PROPERTY_DEFINITION,
  197. "there are no property definitions with encoder %s",
  198. OSSL_ENCODER_get0_name(encoder));
  199. goto err;
  200. }
  201. /* The "output" property is mandatory */
  202. prop = ossl_property_find_property(props, libctx, "output");
  203. encoder_inst->output_type = ossl_property_get_string_value(libctx, prop);
  204. if (encoder_inst->output_type == NULL) {
  205. ERR_raise_data(ERR_LIB_OSSL_DECODER, ERR_R_INVALID_PROPERTY_DEFINITION,
  206. "the mandatory 'output' property is missing "
  207. "for encoder %s (properties: %s)",
  208. OSSL_ENCODER_get0_name(encoder),
  209. OSSL_ENCODER_get0_properties(encoder));
  210. goto err;
  211. }
  212. /* The "structure" property is optional */
  213. prop = ossl_property_find_property(props, libctx, "structure");
  214. if (prop != NULL)
  215. encoder_inst->output_structure
  216. = ossl_property_get_string_value(libctx, prop);
  217. encoder_inst->encoder = encoder;
  218. encoder_inst->encoderctx = encoderctx;
  219. return encoder_inst;
  220. err:
  221. ossl_encoder_instance_free(encoder_inst);
  222. return NULL;
  223. }
  224. void ossl_encoder_instance_free(OSSL_ENCODER_INSTANCE *encoder_inst)
  225. {
  226. if (encoder_inst != NULL) {
  227. if (encoder_inst->encoder != NULL)
  228. encoder_inst->encoder->freectx(encoder_inst->encoderctx);
  229. encoder_inst->encoderctx = NULL;
  230. OSSL_ENCODER_free(encoder_inst->encoder);
  231. encoder_inst->encoder = NULL;
  232. OPENSSL_free(encoder_inst);
  233. }
  234. }
  235. static int ossl_encoder_ctx_add_encoder_inst(OSSL_ENCODER_CTX *ctx,
  236. OSSL_ENCODER_INSTANCE *ei)
  237. {
  238. int ok;
  239. if (ctx->encoder_insts == NULL
  240. && (ctx->encoder_insts =
  241. sk_OSSL_ENCODER_INSTANCE_new_null()) == NULL) {
  242. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_CRYPTO_LIB);
  243. return 0;
  244. }
  245. ok = (sk_OSSL_ENCODER_INSTANCE_push(ctx->encoder_insts, ei) > 0);
  246. if (ok) {
  247. OSSL_TRACE_BEGIN(ENCODER) {
  248. BIO_printf(trc_out,
  249. "(ctx %p) Added encoder instance %p (encoder %p):\n"
  250. " %s with %s\n",
  251. (void *)ctx, (void *)ei, (void *)ei->encoder,
  252. OSSL_ENCODER_get0_name(ei->encoder),
  253. OSSL_ENCODER_get0_properties(ei->encoder));
  254. } OSSL_TRACE_END(ENCODER);
  255. }
  256. return ok;
  257. }
  258. int OSSL_ENCODER_CTX_add_encoder(OSSL_ENCODER_CTX *ctx, OSSL_ENCODER *encoder)
  259. {
  260. OSSL_ENCODER_INSTANCE *encoder_inst = NULL;
  261. const OSSL_PROVIDER *prov = NULL;
  262. void *encoderctx = NULL;
  263. void *provctx = NULL;
  264. if (!ossl_assert(ctx != NULL) || !ossl_assert(encoder != NULL)) {
  265. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
  266. return 0;
  267. }
  268. prov = OSSL_ENCODER_get0_provider(encoder);
  269. provctx = OSSL_PROVIDER_get0_provider_ctx(prov);
  270. if ((encoderctx = encoder->newctx(provctx)) == NULL
  271. || (encoder_inst =
  272. ossl_encoder_instance_new(encoder, encoderctx)) == NULL)
  273. goto err;
  274. /* Avoid double free of encoderctx on further errors */
  275. encoderctx = NULL;
  276. if (!ossl_encoder_ctx_add_encoder_inst(ctx, encoder_inst))
  277. goto err;
  278. return 1;
  279. err:
  280. ossl_encoder_instance_free(encoder_inst);
  281. if (encoderctx != NULL)
  282. encoder->freectx(encoderctx);
  283. return 0;
  284. }
  285. int OSSL_ENCODER_CTX_add_extra(OSSL_ENCODER_CTX *ctx,
  286. OSSL_LIB_CTX *libctx, const char *propq)
  287. {
  288. return 1;
  289. }
  290. int OSSL_ENCODER_CTX_get_num_encoders(OSSL_ENCODER_CTX *ctx)
  291. {
  292. if (ctx == NULL || ctx->encoder_insts == NULL)
  293. return 0;
  294. return sk_OSSL_ENCODER_INSTANCE_num(ctx->encoder_insts);
  295. }
  296. int OSSL_ENCODER_CTX_set_construct(OSSL_ENCODER_CTX *ctx,
  297. OSSL_ENCODER_CONSTRUCT *construct)
  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->construct = construct;
  304. return 1;
  305. }
  306. int OSSL_ENCODER_CTX_set_construct_data(OSSL_ENCODER_CTX *ctx,
  307. void *construct_data)
  308. {
  309. if (!ossl_assert(ctx != NULL)) {
  310. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
  311. return 0;
  312. }
  313. ctx->construct_data = construct_data;
  314. return 1;
  315. }
  316. int OSSL_ENCODER_CTX_set_cleanup(OSSL_ENCODER_CTX *ctx,
  317. OSSL_ENCODER_CLEANUP *cleanup)
  318. {
  319. if (!ossl_assert(ctx != NULL)) {
  320. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
  321. return 0;
  322. }
  323. ctx->cleanup = cleanup;
  324. return 1;
  325. }
  326. OSSL_ENCODER *
  327. OSSL_ENCODER_INSTANCE_get_encoder(OSSL_ENCODER_INSTANCE *encoder_inst)
  328. {
  329. if (encoder_inst == NULL)
  330. return NULL;
  331. return encoder_inst->encoder;
  332. }
  333. void *
  334. OSSL_ENCODER_INSTANCE_get_encoder_ctx(OSSL_ENCODER_INSTANCE *encoder_inst)
  335. {
  336. if (encoder_inst == NULL)
  337. return NULL;
  338. return encoder_inst->encoderctx;
  339. }
  340. const char *
  341. OSSL_ENCODER_INSTANCE_get_output_type(OSSL_ENCODER_INSTANCE *encoder_inst)
  342. {
  343. if (encoder_inst == NULL)
  344. return NULL;
  345. return encoder_inst->output_type;
  346. }
  347. const char *
  348. OSSL_ENCODER_INSTANCE_get_output_structure(OSSL_ENCODER_INSTANCE *encoder_inst)
  349. {
  350. if (encoder_inst == NULL)
  351. return NULL;
  352. return encoder_inst->output_structure;
  353. }
  354. static int encoder_process(struct encoder_process_data_st *data)
  355. {
  356. OSSL_ENCODER_INSTANCE *current_encoder_inst = NULL;
  357. OSSL_ENCODER *current_encoder = NULL;
  358. OSSL_ENCODER_CTX *current_encoder_ctx = NULL;
  359. BIO *allocated_out = NULL;
  360. const void *original_data = NULL;
  361. OSSL_PARAM abstract[10];
  362. const OSSL_PARAM *current_abstract = NULL;
  363. int i;
  364. int ok = -1; /* -1 signifies that the lookup loop gave nothing */
  365. int top = 0;
  366. if (data->next_encoder_inst == NULL) {
  367. /* First iteration, where we prepare for what is to come */
  368. data->count_output_structure =
  369. data->ctx->output_structure == NULL ? -1 : 0;
  370. top = 1;
  371. }
  372. for (i = data->current_encoder_inst_index; i-- > 0;) {
  373. OSSL_ENCODER *next_encoder = NULL;
  374. const char *current_output_type;
  375. const char *current_output_structure;
  376. struct encoder_process_data_st new_data;
  377. if (!top)
  378. next_encoder =
  379. OSSL_ENCODER_INSTANCE_get_encoder(data->next_encoder_inst);
  380. current_encoder_inst =
  381. sk_OSSL_ENCODER_INSTANCE_value(data->ctx->encoder_insts, i);
  382. current_encoder =
  383. OSSL_ENCODER_INSTANCE_get_encoder(current_encoder_inst);
  384. current_encoder_ctx =
  385. OSSL_ENCODER_INSTANCE_get_encoder_ctx(current_encoder_inst);
  386. current_output_type =
  387. OSSL_ENCODER_INSTANCE_get_output_type(current_encoder_inst);
  388. current_output_structure =
  389. OSSL_ENCODER_INSTANCE_get_output_structure(current_encoder_inst);
  390. memset(&new_data, 0, sizeof(new_data));
  391. new_data.ctx = data->ctx;
  392. new_data.current_encoder_inst_index = i;
  393. new_data.next_encoder_inst = current_encoder_inst;
  394. new_data.count_output_structure = data->count_output_structure;
  395. new_data.level = data->level + 1;
  396. OSSL_TRACE_BEGIN(ENCODER) {
  397. BIO_printf(trc_out,
  398. "[%d] (ctx %p) Considering encoder instance %p (encoder %p)\n",
  399. data->level, (void *)data->ctx,
  400. (void *)current_encoder_inst, (void *)current_encoder);
  401. } OSSL_TRACE_END(ENCODER);
  402. /*
  403. * If this is the top call, we check if the output type of the current
  404. * encoder matches the desired output type.
  405. * If this isn't the top call, i.e. this is deeper in the recursion,
  406. * we instead check if the output type of the current encoder matches
  407. * the name of the next encoder (the one found by the parent call).
  408. */
  409. if (top) {
  410. if (data->ctx->output_type != NULL
  411. && OPENSSL_strcasecmp(current_output_type,
  412. data->ctx->output_type) != 0) {
  413. OSSL_TRACE_BEGIN(ENCODER) {
  414. BIO_printf(trc_out,
  415. "[%d] Skipping because current encoder output type (%s) != desired output type (%s)\n",
  416. data->level,
  417. current_output_type, data->ctx->output_type);
  418. } OSSL_TRACE_END(ENCODER);
  419. continue;
  420. }
  421. } else {
  422. if (!OSSL_ENCODER_is_a(next_encoder, current_output_type)) {
  423. OSSL_TRACE_BEGIN(ENCODER) {
  424. BIO_printf(trc_out,
  425. "[%d] Skipping because current encoder output type (%s) != name of encoder %p\n",
  426. data->level,
  427. current_output_type, (void *)next_encoder);
  428. } OSSL_TRACE_END(ENCODER);
  429. continue;
  430. }
  431. }
  432. /*
  433. * If the caller and the current encoder specify an output structure,
  434. * Check if they match. If they do, count the match, otherwise skip
  435. * the current encoder.
  436. */
  437. if (data->ctx->output_structure != NULL
  438. && current_output_structure != NULL) {
  439. if (OPENSSL_strcasecmp(data->ctx->output_structure,
  440. current_output_structure) != 0) {
  441. OSSL_TRACE_BEGIN(ENCODER) {
  442. BIO_printf(trc_out,
  443. "[%d] Skipping because current encoder output structure (%s) != ctx output structure (%s)\n",
  444. data->level,
  445. current_output_structure,
  446. data->ctx->output_structure);
  447. } OSSL_TRACE_END(ENCODER);
  448. continue;
  449. }
  450. data->count_output_structure++;
  451. }
  452. /*
  453. * Recurse to process the encoder implementations before the current
  454. * one.
  455. */
  456. ok = encoder_process(&new_data);
  457. data->prev_encoder_inst = new_data.prev_encoder_inst;
  458. data->running_output = new_data.running_output;
  459. data->running_output_length = new_data.running_output_length;
  460. /*
  461. * ok == -1 means that the recursion call above gave no further
  462. * encoders, and that the one we're currently at should
  463. * be tried.
  464. * ok == 0 means that something failed in the recursion call
  465. * above, making the result unsuitable for a chain.
  466. * In this case, we simply continue to try finding a
  467. * suitable encoder at this recursion level.
  468. * ok == 1 means that the recursion call was successful, and we
  469. * try to use the result at this recursion level.
  470. */
  471. if (ok != 0)
  472. break;
  473. OSSL_TRACE_BEGIN(ENCODER) {
  474. BIO_printf(trc_out,
  475. "[%d] Skipping because recursion level %d failed\n",
  476. data->level, new_data.level);
  477. } OSSL_TRACE_END(ENCODER);
  478. }
  479. /*
  480. * If |i < 0|, we didn't find any useful encoder in this recursion, so
  481. * we do the rest of the process only if |i >= 0|.
  482. */
  483. if (i < 0) {
  484. ok = -1;
  485. OSSL_TRACE_BEGIN(ENCODER) {
  486. BIO_printf(trc_out,
  487. "[%d] (ctx %p) No suitable encoder found\n",
  488. data->level, (void *)data->ctx);
  489. } OSSL_TRACE_END(ENCODER);
  490. } else {
  491. /* Preparations */
  492. switch (ok) {
  493. case 0:
  494. break;
  495. case -1:
  496. /*
  497. * We have reached the beginning of the encoder instance sequence,
  498. * so we prepare the object to be encoded.
  499. */
  500. /*
  501. * |data->count_output_structure| is one of these values:
  502. *
  503. * -1 There is no desired output structure
  504. * 0 There is a desired output structure, and it wasn't
  505. * matched by any of the encoder instances that were
  506. * considered
  507. * >0 There is a desired output structure, and at least one
  508. * of the encoder instances matched it
  509. */
  510. if (data->count_output_structure == 0)
  511. return 0;
  512. original_data =
  513. data->ctx->construct(current_encoder_inst,
  514. data->ctx->construct_data);
  515. /* Also set the data type, using the encoder implementation name */
  516. data->data_type = OSSL_ENCODER_get0_name(current_encoder);
  517. /* Assume that the constructor recorded an error */
  518. if (original_data != NULL)
  519. ok = 1;
  520. else
  521. ok = 0;
  522. break;
  523. case 1:
  524. if (!ossl_assert(data->running_output != NULL)) {
  525. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_INTERNAL_ERROR);
  526. ok = 0;
  527. break;
  528. }
  529. {
  530. /*
  531. * Create an object abstraction from the latest output, which
  532. * was stolen from the previous round.
  533. */
  534. OSSL_PARAM *abstract_p = abstract;
  535. const char *prev_output_structure =
  536. OSSL_ENCODER_INSTANCE_get_output_structure(data->prev_encoder_inst);
  537. *abstract_p++ =
  538. OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE,
  539. (char *)data->data_type, 0);
  540. if (prev_output_structure != NULL)
  541. *abstract_p++ =
  542. OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_STRUCTURE,
  543. (char *)prev_output_structure,
  544. 0);
  545. *abstract_p++ =
  546. OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_DATA,
  547. data->running_output,
  548. data->running_output_length);
  549. *abstract_p = OSSL_PARAM_construct_end();
  550. current_abstract = abstract;
  551. }
  552. break;
  553. }
  554. /* Calling the encoder implementation */
  555. if (ok) {
  556. OSSL_CORE_BIO *cbio = NULL;
  557. BIO *current_out = NULL;
  558. /*
  559. * If we're at the last encoder instance to use, we're setting up
  560. * final output. Otherwise, set up an intermediary memory output.
  561. */
  562. if (top)
  563. current_out = data->bio;
  564. else if ((current_out = allocated_out = BIO_new(BIO_s_mem()))
  565. == NULL)
  566. ok = 0; /* Assume BIO_new() recorded an error */
  567. if (ok)
  568. ok = (cbio = ossl_core_bio_new_from_bio(current_out)) != NULL;
  569. if (ok) {
  570. ok = current_encoder->encode(current_encoder_ctx, cbio,
  571. original_data, current_abstract,
  572. data->ctx->selection,
  573. ossl_pw_passphrase_callback_enc,
  574. &data->ctx->pwdata);
  575. OSSL_TRACE_BEGIN(ENCODER) {
  576. BIO_printf(trc_out,
  577. "[%d] (ctx %p) Running encoder instance %p => %d\n",
  578. data->level, (void *)data->ctx,
  579. (void *)current_encoder_inst, ok);
  580. } OSSL_TRACE_END(ENCODER);
  581. }
  582. ossl_core_bio_free(cbio);
  583. data->prev_encoder_inst = current_encoder_inst;
  584. }
  585. }
  586. /* Cleanup and collecting the result */
  587. OPENSSL_free(data->running_output);
  588. data->running_output = NULL;
  589. /*
  590. * Steal the output from the BIO_s_mem, if we did allocate one.
  591. * That'll be the data for an object abstraction in the next round.
  592. */
  593. if (allocated_out != NULL) {
  594. BUF_MEM *buf;
  595. BIO_get_mem_ptr(allocated_out, &buf);
  596. data->running_output = (unsigned char *)buf->data;
  597. data->running_output_length = buf->length;
  598. memset(buf, 0, sizeof(*buf));
  599. }
  600. BIO_free(allocated_out);
  601. if (original_data != NULL)
  602. data->ctx->cleanup(data->ctx->construct_data);
  603. return ok;
  604. }
  605. int ossl_bio_print_labeled_bignum(BIO *out, const char *label, const BIGNUM *bn)
  606. {
  607. int ret = 0, use_sep = 0;
  608. char *hex_str = NULL, *p;
  609. const char spaces[] = " ";
  610. const char *post_label_spc = " ";
  611. const char *neg = "";
  612. int bytes;
  613. if (bn == NULL)
  614. return 0;
  615. if (label == NULL) {
  616. label = "";
  617. post_label_spc = "";
  618. }
  619. if (BN_is_zero(bn))
  620. return BIO_printf(out, "%s%s0\n", label, post_label_spc);
  621. if (BN_num_bytes(bn) <= BN_BYTES) {
  622. BN_ULONG *words = bn_get_words(bn);
  623. if (BN_is_negative(bn))
  624. neg = "-";
  625. return BIO_printf(out, "%s%s%s" BN_FMTu " (%s0x" BN_FMTx ")\n",
  626. label, post_label_spc, neg, words[0], neg, words[0]);
  627. }
  628. hex_str = BN_bn2hex(bn);
  629. if (hex_str == NULL)
  630. return 0;
  631. p = hex_str;
  632. if (*p == '-') {
  633. ++p;
  634. neg = " (Negative)";
  635. }
  636. if (BIO_printf(out, "%s%s\n", label, neg) <= 0)
  637. goto err;
  638. /* Keep track of how many bytes we have printed out so far */
  639. bytes = 0;
  640. if (BIO_printf(out, "%s", spaces) <= 0)
  641. goto err;
  642. /* Add a leading 00 if the top bit is set */
  643. if (*p >= '8') {
  644. if (BIO_printf(out, "%02x", 0) <= 0)
  645. goto err;
  646. ++bytes;
  647. use_sep = 1;
  648. }
  649. while (*p != '\0') {
  650. /* Do a newline after every 15 hex bytes + add the space indent */
  651. if ((bytes % 15) == 0 && bytes > 0) {
  652. if (BIO_printf(out, ":\n%s", spaces) <= 0)
  653. goto err;
  654. use_sep = 0; /* The first byte on the next line doesn't have a : */
  655. }
  656. if (BIO_printf(out, "%s%c%c", use_sep ? ":" : "",
  657. tolower((unsigned char)p[0]),
  658. tolower((unsigned char)p[1])) <= 0)
  659. goto err;
  660. ++bytes;
  661. p += 2;
  662. use_sep = 1;
  663. }
  664. if (BIO_printf(out, "\n") <= 0)
  665. goto err;
  666. ret = 1;
  667. err:
  668. OPENSSL_free(hex_str);
  669. return ret;
  670. }
  671. int ossl_bio_print_labeled_buf(BIO *out, const char *label,
  672. const unsigned char *buf, size_t buflen)
  673. {
  674. size_t i;
  675. if (BIO_printf(out, "%s\n", label) <= 0)
  676. return 0;
  677. for (i = 0; i < buflen; i++) {
  678. if ((i % LABELED_BUF_PRINT_WIDTH) == 0) {
  679. if (i > 0 && BIO_printf(out, "\n") <= 0)
  680. return 0;
  681. if (BIO_printf(out, " ") <= 0)
  682. return 0;
  683. }
  684. if (BIO_printf(out, "%02x%s", buf[i],
  685. (i == buflen - 1) ? "" : ":") <= 0)
  686. return 0;
  687. }
  688. if (BIO_printf(out, "\n") <= 0)
  689. return 0;
  690. return 1;
  691. }
  692. #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_DSA)
  693. int ossl_bio_print_ffc_params(BIO *out, const FFC_PARAMS *ffc)
  694. {
  695. if (ffc->nid != NID_undef) {
  696. #ifndef OPENSSL_NO_DH
  697. const DH_NAMED_GROUP *group = ossl_ffc_uid_to_dh_named_group(ffc->nid);
  698. const char *name = ossl_ffc_named_group_get_name(group);
  699. if (name == NULL)
  700. goto err;
  701. if (BIO_printf(out, "GROUP: %s\n", name) <= 0)
  702. goto err;
  703. return 1;
  704. #else
  705. /* How could this be? We should not have a nid in a no-dh build. */
  706. goto err;
  707. #endif
  708. }
  709. if (!ossl_bio_print_labeled_bignum(out, "P: ", ffc->p))
  710. goto err;
  711. if (ffc->q != NULL) {
  712. if (!ossl_bio_print_labeled_bignum(out, "Q: ", ffc->q))
  713. goto err;
  714. }
  715. if (!ossl_bio_print_labeled_bignum(out, "G: ", ffc->g))
  716. goto err;
  717. if (ffc->j != NULL) {
  718. if (!ossl_bio_print_labeled_bignum(out, "J: ", ffc->j))
  719. goto err;
  720. }
  721. if (ffc->seed != NULL) {
  722. if (!ossl_bio_print_labeled_buf(out, "SEED:", ffc->seed, ffc->seedlen))
  723. goto err;
  724. }
  725. if (ffc->gindex != -1) {
  726. if (BIO_printf(out, "gindex: %d\n", ffc->gindex) <= 0)
  727. goto err;
  728. }
  729. if (ffc->pcounter != -1) {
  730. if (BIO_printf(out, "pcounter: %d\n", ffc->pcounter) <= 0)
  731. goto err;
  732. }
  733. if (ffc->h != 0) {
  734. if (BIO_printf(out, "h: %d\n", ffc->h) <= 0)
  735. goto err;
  736. }
  737. return 1;
  738. err:
  739. return 0;
  740. }
  741. #endif