2 * Copyright 2019-2025 The OpenSSL Project Authors. All Rights Reserved.
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
12 #include <openssl/core_names.h>
13 #include <openssl/bio.h>
14 #include <openssl/encoder.h>
15 #include <openssl/buffer.h>
16 #include <openssl/params.h>
17 #include <openssl/provider.h>
18 #include <openssl/trace.h>
19 #include <crypto/bn.h>
20 #include "internal/bio.h"
21 #include "internal/ffc.h"
22 #include "internal/provider.h"
23 #include "internal/encoder.h"
24 #include "encoder_local.h"
26 /* Number of octets per line */
27 #define LABELED_BUF_PRINT_WIDTH 15
29 # ifdef SIXTY_FOUR_BIT_LONG
30 # define BN_FMTu "%lu"
31 # define BN_FMTx "%lx"
34 # ifdef SIXTY_FOUR_BIT
35 # define BN_FMTu "%llu"
36 # define BN_FMTx "%llx"
39 # ifdef THIRTY_TWO_BIT
44 struct encoder_process_data_st
{
45 OSSL_ENCODER_CTX
*ctx
;
50 /* Index of the current encoder instance to be processed */
51 int current_encoder_inst_index
;
53 /* Processing data passed down through recursion */
54 int level
; /* Recursion level */
55 OSSL_ENCODER_INSTANCE
*next_encoder_inst
;
56 int count_output_structure
;
58 /* Processing data passed up through recursion */
59 OSSL_ENCODER_INSTANCE
*prev_encoder_inst
;
60 unsigned char *running_output
;
61 size_t running_output_length
;
62 /* Data type = the name of the first succeeding encoder implementation */
63 const char *data_type
;
66 static int encoder_process(struct encoder_process_data_st
*data
);
68 int OSSL_ENCODER_to_bio(OSSL_ENCODER_CTX
*ctx
, BIO
*out
)
70 struct encoder_process_data_st data
;
72 memset(&data
, 0, sizeof(data
));
75 data
.current_encoder_inst_index
= OSSL_ENCODER_CTX_get_num_encoders(ctx
);
77 if (data
.current_encoder_inst_index
== 0) {
78 ERR_raise_data(ERR_LIB_OSSL_ENCODER
, OSSL_ENCODER_R_ENCODER_NOT_FOUND
,
79 "No encoders were found. For standard encoders you need "
80 "at least one of the default or base providers "
81 "available. Did you forget to load them?");
85 if (ctx
->cleanup
== NULL
|| ctx
->construct
== NULL
) {
86 ERR_raise(ERR_LIB_OSSL_ENCODER
, ERR_R_INIT_FAIL
);
90 return encoder_process(&data
) > 0;
93 #ifndef OPENSSL_NO_STDIO
94 static BIO
*bio_from_file(FILE *fp
)
98 if ((b
= BIO_new(BIO_s_file())) == NULL
) {
99 ERR_raise(ERR_LIB_OSSL_ENCODER
, ERR_R_BUF_LIB
);
102 BIO_set_fp(b
, fp
, BIO_NOCLOSE
);
106 int OSSL_ENCODER_to_fp(OSSL_ENCODER_CTX
*ctx
, FILE *fp
)
108 BIO
*b
= bio_from_file(fp
);
112 ret
= OSSL_ENCODER_to_bio(ctx
, b
);
119 int OSSL_ENCODER_to_data(OSSL_ENCODER_CTX
*ctx
, unsigned char **pdata
,
126 if (pdata_len
== NULL
) {
127 ERR_raise(ERR_LIB_OSSL_ENCODER
, ERR_R_PASSED_NULL_PARAMETER
);
131 out
= BIO_new(BIO_s_mem());
134 && OSSL_ENCODER_to_bio(ctx
, out
)
135 && BIO_get_mem_ptr(out
, &buf
) > 0) {
136 ret
= 1; /* Hope for the best. A too small buffer will clear this */
138 if (pdata
!= NULL
&& *pdata
!= NULL
) {
139 if (*pdata_len
< buf
->length
)
141 * It's tempting to do |*pdata_len = (size_t)buf->length|
142 * However, it's believed to be confusing more than helpful,
147 *pdata_len
-= buf
->length
;
149 /* The buffer with the right size is already allocated for us */
150 *pdata_len
= (size_t)buf
->length
;
155 if (*pdata
!= NULL
) {
156 memcpy(*pdata
, buf
->data
, buf
->length
);
157 *pdata
+= buf
->length
;
159 /* In this case, we steal the data from BIO_s_mem() */
160 *pdata
= (unsigned char *)buf
->data
;
170 int OSSL_ENCODER_CTX_set_selection(OSSL_ENCODER_CTX
*ctx
, int selection
)
172 if (!ossl_assert(ctx
!= NULL
)) {
173 ERR_raise(ERR_LIB_OSSL_ENCODER
, ERR_R_PASSED_NULL_PARAMETER
);
177 if (!ossl_assert(selection
!= 0)) {
178 ERR_raise(ERR_LIB_OSSL_ENCODER
, ERR_R_PASSED_INVALID_ARGUMENT
);
182 ctx
->selection
= selection
;
186 int OSSL_ENCODER_CTX_set_output_type(OSSL_ENCODER_CTX
*ctx
,
187 const char *output_type
)
189 if (!ossl_assert(ctx
!= NULL
) || !ossl_assert(output_type
!= NULL
)) {
190 ERR_raise(ERR_LIB_OSSL_ENCODER
, ERR_R_PASSED_NULL_PARAMETER
);
194 ctx
->output_type
= output_type
;
198 int OSSL_ENCODER_CTX_set_output_structure(OSSL_ENCODER_CTX
*ctx
,
199 const char *output_structure
)
201 if (!ossl_assert(ctx
!= NULL
) || !ossl_assert(output_structure
!= NULL
)) {
202 ERR_raise(ERR_LIB_OSSL_ENCODER
, ERR_R_PASSED_NULL_PARAMETER
);
206 ctx
->output_structure
= output_structure
;
210 static OSSL_ENCODER_INSTANCE
*ossl_encoder_instance_new(OSSL_ENCODER
*encoder
,
213 OSSL_ENCODER_INSTANCE
*encoder_inst
= NULL
;
214 const OSSL_PROVIDER
*prov
;
215 OSSL_LIB_CTX
*libctx
;
216 const OSSL_PROPERTY_LIST
*props
;
217 const OSSL_PROPERTY_DEFINITION
*prop
;
219 if (!ossl_assert(encoder
!= NULL
)) {
220 ERR_raise(ERR_LIB_OSSL_ENCODER
, ERR_R_PASSED_NULL_PARAMETER
);
224 if ((encoder_inst
= OPENSSL_zalloc(sizeof(*encoder_inst
))) == NULL
)
227 if (!OSSL_ENCODER_up_ref(encoder
)) {
228 ERR_raise(ERR_LIB_OSSL_ENCODER
, ERR_R_INTERNAL_ERROR
);
232 prov
= OSSL_ENCODER_get0_provider(encoder
);
233 libctx
= ossl_provider_libctx(prov
);
234 props
= ossl_encoder_parsed_properties(encoder
);
236 ERR_raise_data(ERR_LIB_OSSL_DECODER
, ERR_R_INVALID_PROPERTY_DEFINITION
,
237 "there are no property definitions with encoder %s",
238 OSSL_ENCODER_get0_name(encoder
));
242 /* The "output" property is mandatory */
243 prop
= ossl_property_find_property(props
, libctx
, "output");
244 encoder_inst
->output_type
= ossl_property_get_string_value(libctx
, prop
);
245 if (encoder_inst
->output_type
== NULL
) {
246 ERR_raise_data(ERR_LIB_OSSL_DECODER
, ERR_R_INVALID_PROPERTY_DEFINITION
,
247 "the mandatory 'output' property is missing "
248 "for encoder %s (properties: %s)",
249 OSSL_ENCODER_get0_name(encoder
),
250 OSSL_ENCODER_get0_properties(encoder
));
254 /* The "structure" property is optional */
255 prop
= ossl_property_find_property(props
, libctx
, "structure");
257 encoder_inst
->output_structure
258 = ossl_property_get_string_value(libctx
, prop
);
260 encoder_inst
->encoder
= encoder
;
261 encoder_inst
->encoderctx
= encoderctx
;
264 ossl_encoder_instance_free(encoder_inst
);
268 void ossl_encoder_instance_free(OSSL_ENCODER_INSTANCE
*encoder_inst
)
270 if (encoder_inst
!= NULL
) {
271 if (encoder_inst
->encoder
!= NULL
)
272 encoder_inst
->encoder
->freectx(encoder_inst
->encoderctx
);
273 encoder_inst
->encoderctx
= NULL
;
274 OSSL_ENCODER_free(encoder_inst
->encoder
);
275 encoder_inst
->encoder
= NULL
;
276 OPENSSL_free(encoder_inst
);
280 static int ossl_encoder_ctx_add_encoder_inst(OSSL_ENCODER_CTX
*ctx
,
281 OSSL_ENCODER_INSTANCE
*ei
)
285 if (ctx
->encoder_insts
== NULL
286 && (ctx
->encoder_insts
=
287 sk_OSSL_ENCODER_INSTANCE_new_null()) == NULL
) {
288 ERR_raise(ERR_LIB_OSSL_ENCODER
, ERR_R_CRYPTO_LIB
);
292 ok
= (sk_OSSL_ENCODER_INSTANCE_push(ctx
->encoder_insts
, ei
) > 0);
294 OSSL_TRACE_BEGIN(ENCODER
) {
296 "(ctx %p) Added encoder instance %p (encoder %p):\n"
298 (void *)ctx
, (void *)ei
, (void *)ei
->encoder
,
299 OSSL_ENCODER_get0_name(ei
->encoder
),
300 OSSL_ENCODER_get0_properties(ei
->encoder
));
301 } OSSL_TRACE_END(ENCODER
);
306 int OSSL_ENCODER_CTX_add_encoder(OSSL_ENCODER_CTX
*ctx
, OSSL_ENCODER
*encoder
)
308 OSSL_ENCODER_INSTANCE
*encoder_inst
= NULL
;
309 const OSSL_PROVIDER
*prov
= NULL
;
310 void *encoderctx
= NULL
;
311 void *provctx
= NULL
;
313 if (!ossl_assert(ctx
!= NULL
) || !ossl_assert(encoder
!= NULL
)) {
314 ERR_raise(ERR_LIB_OSSL_ENCODER
, ERR_R_PASSED_NULL_PARAMETER
);
318 prov
= OSSL_ENCODER_get0_provider(encoder
);
319 provctx
= OSSL_PROVIDER_get0_provider_ctx(prov
);
321 if ((encoderctx
= encoder
->newctx(provctx
)) == NULL
323 ossl_encoder_instance_new(encoder
, encoderctx
)) == NULL
)
325 /* Avoid double free of encoderctx on further errors */
328 if (!ossl_encoder_ctx_add_encoder_inst(ctx
, encoder_inst
))
333 ossl_encoder_instance_free(encoder_inst
);
334 if (encoderctx
!= NULL
)
335 encoder
->freectx(encoderctx
);
339 int OSSL_ENCODER_CTX_add_extra(OSSL_ENCODER_CTX
*ctx
,
340 OSSL_LIB_CTX
*libctx
, const char *propq
)
345 int OSSL_ENCODER_CTX_get_num_encoders(OSSL_ENCODER_CTX
*ctx
)
347 if (ctx
== NULL
|| ctx
->encoder_insts
== NULL
)
349 return sk_OSSL_ENCODER_INSTANCE_num(ctx
->encoder_insts
);
352 int OSSL_ENCODER_CTX_set_construct(OSSL_ENCODER_CTX
*ctx
,
353 OSSL_ENCODER_CONSTRUCT
*construct
)
355 if (!ossl_assert(ctx
!= NULL
)) {
356 ERR_raise(ERR_LIB_OSSL_ENCODER
, ERR_R_PASSED_NULL_PARAMETER
);
359 ctx
->construct
= construct
;
363 int OSSL_ENCODER_CTX_set_construct_data(OSSL_ENCODER_CTX
*ctx
,
364 void *construct_data
)
366 if (!ossl_assert(ctx
!= NULL
)) {
367 ERR_raise(ERR_LIB_OSSL_ENCODER
, ERR_R_PASSED_NULL_PARAMETER
);
370 ctx
->construct_data
= construct_data
;
374 int OSSL_ENCODER_CTX_set_cleanup(OSSL_ENCODER_CTX
*ctx
,
375 OSSL_ENCODER_CLEANUP
*cleanup
)
377 if (!ossl_assert(ctx
!= NULL
)) {
378 ERR_raise(ERR_LIB_OSSL_ENCODER
, ERR_R_PASSED_NULL_PARAMETER
);
381 ctx
->cleanup
= cleanup
;
386 OSSL_ENCODER_INSTANCE_get_encoder(OSSL_ENCODER_INSTANCE
*encoder_inst
)
388 if (encoder_inst
== NULL
)
390 return encoder_inst
->encoder
;
394 OSSL_ENCODER_INSTANCE_get_encoder_ctx(OSSL_ENCODER_INSTANCE
*encoder_inst
)
396 if (encoder_inst
== NULL
)
398 return encoder_inst
->encoderctx
;
402 OSSL_ENCODER_INSTANCE_get_output_type(OSSL_ENCODER_INSTANCE
*encoder_inst
)
404 if (encoder_inst
== NULL
)
406 return encoder_inst
->output_type
;
410 OSSL_ENCODER_INSTANCE_get_output_structure(OSSL_ENCODER_INSTANCE
*encoder_inst
)
412 if (encoder_inst
== NULL
)
414 return encoder_inst
->output_structure
;
417 static int encoder_process(struct encoder_process_data_st
*data
)
419 OSSL_ENCODER_INSTANCE
*current_encoder_inst
= NULL
;
420 OSSL_ENCODER
*current_encoder
= NULL
;
421 OSSL_ENCODER_CTX
*current_encoder_ctx
= NULL
;
422 BIO
*allocated_out
= NULL
;
423 const void *original_data
= NULL
;
424 OSSL_PARAM abstract
[10];
425 const OSSL_PARAM
*current_abstract
= NULL
;
427 int ok
= -1; /* -1 signifies that the lookup loop gave nothing */
430 if (data
->next_encoder_inst
== NULL
) {
431 /* First iteration, where we prepare for what is to come */
433 data
->count_output_structure
=
434 data
->ctx
->output_structure
== NULL
? -1 : 0;
438 for (i
= data
->current_encoder_inst_index
; i
-- > 0;) {
439 OSSL_ENCODER
*next_encoder
= NULL
;
440 const char *current_output_type
;
441 const char *current_output_structure
;
442 struct encoder_process_data_st new_data
;
446 OSSL_ENCODER_INSTANCE_get_encoder(data
->next_encoder_inst
);
448 current_encoder_inst
=
449 sk_OSSL_ENCODER_INSTANCE_value(data
->ctx
->encoder_insts
, i
);
451 OSSL_ENCODER_INSTANCE_get_encoder(current_encoder_inst
);
452 current_encoder_ctx
=
453 OSSL_ENCODER_INSTANCE_get_encoder_ctx(current_encoder_inst
);
454 current_output_type
=
455 OSSL_ENCODER_INSTANCE_get_output_type(current_encoder_inst
);
456 current_output_structure
=
457 OSSL_ENCODER_INSTANCE_get_output_structure(current_encoder_inst
);
458 memset(&new_data
, 0, sizeof(new_data
));
459 new_data
.ctx
= data
->ctx
;
460 new_data
.current_encoder_inst_index
= i
;
461 new_data
.next_encoder_inst
= current_encoder_inst
;
462 new_data
.count_output_structure
= data
->count_output_structure
;
463 new_data
.level
= data
->level
+ 1;
465 OSSL_TRACE_BEGIN(ENCODER
) {
467 "[%d] (ctx %p) Considering encoder instance %p (encoder %p)\n",
468 data
->level
, (void *)data
->ctx
,
469 (void *)current_encoder_inst
, (void *)current_encoder
);
470 } OSSL_TRACE_END(ENCODER
);
473 * If this is the top call, we check if the output type of the current
474 * encoder matches the desired output type.
475 * If this isn't the top call, i.e. this is deeper in the recursion,
476 * we instead check if the output type of the current encoder matches
477 * the name of the next encoder (the one found by the parent call).
480 if (data
->ctx
->output_type
!= NULL
481 && OPENSSL_strcasecmp(current_output_type
,
482 data
->ctx
->output_type
) != 0) {
483 OSSL_TRACE_BEGIN(ENCODER
) {
485 "[%d] Skipping because current encoder output type (%s) != desired output type (%s)\n",
487 current_output_type
, data
->ctx
->output_type
);
488 } OSSL_TRACE_END(ENCODER
);
492 if (!OSSL_ENCODER_is_a(next_encoder
, current_output_type
)) {
493 OSSL_TRACE_BEGIN(ENCODER
) {
495 "[%d] Skipping because current encoder output type (%s) != name of encoder %p\n",
497 current_output_type
, (void *)next_encoder
);
498 } OSSL_TRACE_END(ENCODER
);
504 * If the caller and the current encoder specify an output structure,
505 * Check if they match. If they do, count the match, otherwise skip
506 * the current encoder.
508 if (data
->ctx
->output_structure
!= NULL
509 && current_output_structure
!= NULL
) {
510 if (OPENSSL_strcasecmp(data
->ctx
->output_structure
,
511 current_output_structure
) != 0) {
512 OSSL_TRACE_BEGIN(ENCODER
) {
514 "[%d] Skipping because current encoder output structure (%s) != ctx output structure (%s)\n",
516 current_output_structure
,
517 data
->ctx
->output_structure
);
518 } OSSL_TRACE_END(ENCODER
);
522 data
->count_output_structure
++;
526 * Recurse to process the encoder implementations before the current
529 ok
= encoder_process(&new_data
);
531 data
->prev_encoder_inst
= new_data
.prev_encoder_inst
;
532 data
->running_output
= new_data
.running_output
;
533 data
->running_output_length
= new_data
.running_output_length
;
536 * ok == -1 means that the recursion call above gave no further
537 * encoders, and that the one we're currently at should
539 * ok == 0 means that something failed in the recursion call
540 * above, making the result unsuitable for a chain.
541 * In this case, we simply continue to try finding a
542 * suitable encoder at this recursion level.
543 * ok == 1 means that the recursion call was successful, and we
544 * try to use the result at this recursion level.
549 OSSL_TRACE_BEGIN(ENCODER
) {
551 "[%d] Skipping because recursion level %d failed\n",
552 data
->level
, new_data
.level
);
553 } OSSL_TRACE_END(ENCODER
);
557 * If |i < 0|, we didn't find any useful encoder in this recursion, so
558 * we do the rest of the process only if |i >= 0|.
563 OSSL_TRACE_BEGIN(ENCODER
) {
565 "[%d] (ctx %p) No suitable encoder found\n",
566 data
->level
, (void *)data
->ctx
);
567 } OSSL_TRACE_END(ENCODER
);
576 * We have reached the beginning of the encoder instance sequence,
577 * so we prepare the object to be encoded.
581 * |data->count_output_structure| is one of these values:
583 * -1 There is no desired output structure
584 * 0 There is a desired output structure, and it wasn't
585 * matched by any of the encoder instances that were
587 * >0 There is a desired output structure, and at least one
588 * of the encoder instances matched it
590 if (data
->count_output_structure
== 0)
594 data
->ctx
->construct(current_encoder_inst
,
595 data
->ctx
->construct_data
);
597 /* Also set the data type, using the encoder implementation name */
598 data
->data_type
= OSSL_ENCODER_get0_name(current_encoder
);
600 /* Assume that the constructor recorded an error */
601 if (original_data
!= NULL
)
607 if (!ossl_assert(data
->running_output
!= NULL
)) {
608 ERR_raise(ERR_LIB_OSSL_ENCODER
, ERR_R_INTERNAL_ERROR
);
615 * Create an object abstraction from the latest output, which
616 * was stolen from the previous round.
619 OSSL_PARAM
*abstract_p
= abstract
;
620 const char *prev_output_structure
=
621 OSSL_ENCODER_INSTANCE_get_output_structure(data
->prev_encoder_inst
);
624 OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE
,
625 (char *)data
->data_type
, 0);
626 if (prev_output_structure
!= NULL
)
628 OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_STRUCTURE
,
629 (char *)prev_output_structure
,
632 OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_DATA
,
633 data
->running_output
,
634 data
->running_output_length
);
635 *abstract_p
= OSSL_PARAM_construct_end();
636 current_abstract
= abstract
;
641 /* Calling the encoder implementation */
644 OSSL_CORE_BIO
*cbio
= NULL
;
645 BIO
*current_out
= NULL
;
648 * If we're at the last encoder instance to use, we're setting up
649 * final output. Otherwise, set up an intermediary memory output.
652 current_out
= data
->bio
;
653 else if ((current_out
= allocated_out
= BIO_new(BIO_s_mem()))
655 ok
= 0; /* Assume BIO_new() recorded an error */
658 ok
= (cbio
= ossl_core_bio_new_from_bio(current_out
)) != NULL
;
660 ok
= current_encoder
->encode(current_encoder_ctx
, cbio
,
661 original_data
, current_abstract
,
662 data
->ctx
->selection
,
663 ossl_pw_passphrase_callback_enc
,
665 OSSL_TRACE_BEGIN(ENCODER
) {
667 "[%d] (ctx %p) Running encoder instance %p => %d\n",
668 data
->level
, (void *)data
->ctx
,
669 (void *)current_encoder_inst
, ok
);
670 } OSSL_TRACE_END(ENCODER
);
673 ossl_core_bio_free(cbio
);
674 data
->prev_encoder_inst
= current_encoder_inst
;
678 /* Cleanup and collecting the result */
680 OPENSSL_free(data
->running_output
);
681 data
->running_output
= NULL
;
684 * Steal the output from the BIO_s_mem, if we did allocate one.
685 * That'll be the data for an object abstraction in the next round.
687 if (allocated_out
!= NULL
) {
690 BIO_get_mem_ptr(allocated_out
, &buf
);
691 data
->running_output
= (unsigned char *)buf
->data
;
692 data
->running_output_length
= buf
->length
;
693 memset(buf
, 0, sizeof(*buf
));
696 BIO_free(allocated_out
);
697 if (original_data
!= NULL
)
698 data
->ctx
->cleanup(data
->ctx
->construct_data
);
702 int ossl_bio_print_labeled_bignum(BIO
*out
, const char *label
, const BIGNUM
*bn
)
704 int ret
= 0, use_sep
= 0;
705 char *hex_str
= NULL
, *p
;
706 const char spaces
[] = " ";
707 const char *post_label_spc
= " ";
709 const char *neg
= "";
720 return BIO_printf(out
, "%s%s0\n", label
, post_label_spc
);
722 if (BN_num_bytes(bn
) <= BN_BYTES
) {
723 BN_ULONG
*words
= bn_get_words(bn
);
725 if (BN_is_negative(bn
))
728 return BIO_printf(out
, "%s%s%s" BN_FMTu
" (%s0x" BN_FMTx
")\n",
729 label
, post_label_spc
, neg
, words
[0], neg
, words
[0]);
732 hex_str
= BN_bn2hex(bn
);
741 if (BIO_printf(out
, "%s%s\n", label
, neg
) <= 0)
744 /* Keep track of how many bytes we have printed out so far */
747 if (BIO_printf(out
, "%s", spaces
) <= 0)
750 /* Add a leading 00 if the top bit is set */
752 if (BIO_printf(out
, "%02x", 0) <= 0)
758 /* Do a newline after every 15 hex bytes + add the space indent */
759 if ((bytes
% 15) == 0 && bytes
> 0) {
760 if (BIO_printf(out
, ":\n%s", spaces
) <= 0)
762 use_sep
= 0; /* The first byte on the next line doesn't have a : */
764 if (BIO_printf(out
, "%s%c%c", use_sep
? ":" : "",
765 tolower((unsigned char)p
[0]),
766 tolower((unsigned char)p
[1])) <= 0)
772 if (BIO_printf(out
, "\n") <= 0)
776 OPENSSL_free(hex_str
);
780 int ossl_bio_print_labeled_buf(BIO
*out
, const char *label
,
781 const unsigned char *buf
, size_t buflen
)
785 if (BIO_printf(out
, "%s\n", label
) <= 0)
788 for (i
= 0; i
< buflen
; i
++) {
789 if ((i
% LABELED_BUF_PRINT_WIDTH
) == 0) {
790 if (i
> 0 && BIO_printf(out
, "\n") <= 0)
792 if (BIO_printf(out
, " ") <= 0)
796 if (BIO_printf(out
, "%02x%s", buf
[i
],
797 (i
== buflen
- 1) ? "" : ":") <= 0)
800 if (BIO_printf(out
, "\n") <= 0)
806 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_DSA)
807 int ossl_bio_print_ffc_params(BIO
*out
, const FFC_PARAMS
*ffc
)
809 if (ffc
->nid
!= NID_undef
) {
810 #ifndef OPENSSL_NO_DH
811 const DH_NAMED_GROUP
*group
= ossl_ffc_uid_to_dh_named_group(ffc
->nid
);
812 const char *name
= ossl_ffc_named_group_get_name(group
);
816 if (BIO_printf(out
, "GROUP: %s\n", name
) <= 0)
820 /* How could this be? We should not have a nid in a no-dh build. */
825 if (!ossl_bio_print_labeled_bignum(out
, "P: ", ffc
->p
))
827 if (ffc
->q
!= NULL
) {
828 if (!ossl_bio_print_labeled_bignum(out
, "Q: ", ffc
->q
))
831 if (!ossl_bio_print_labeled_bignum(out
, "G: ", ffc
->g
))
833 if (ffc
->j
!= NULL
) {
834 if (!ossl_bio_print_labeled_bignum(out
, "J: ", ffc
->j
))
837 if (ffc
->seed
!= NULL
) {
838 if (!ossl_bio_print_labeled_buf(out
, "SEED:", ffc
->seed
, ffc
->seedlen
))
841 if (ffc
->gindex
!= -1) {
842 if (BIO_printf(out
, "gindex: %d\n", ffc
->gindex
) <= 0)
845 if (ffc
->pcounter
!= -1) {
846 if (BIO_printf(out
, "pcounter: %d\n", ffc
->pcounter
) <= 0)
850 if (BIO_printf(out
, "h: %d\n", ffc
->h
) <= 0)