Coverage Report

Created: 2026-02-22 06:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/providers/implementations/encode_decode/encode_key2text.c
Line
Count
Source
1
/*
2
 * Copyright 2020-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
10
/*
11
 * Low level APIs are deprecated for public use, but still ok for internal use.
12
 */
13
#include "internal/deprecated.h"
14
15
#include <openssl/core.h>
16
#include <openssl/core_dispatch.h>
17
#include <openssl/core_names.h>
18
#include <openssl/bn.h>
19
#include <openssl/err.h>
20
#include <openssl/safestack.h>
21
#include <openssl/proverr.h>
22
#include "crypto/dh.h" /* ossl_dh_get0_params() */
23
#include "crypto/dsa.h" /* ossl_dsa_get0_params() */
24
#include "crypto/ec.h" /* ossl_ec_key_get_libctx */
25
#include "crypto/ecx.h" /* ECX_KEY, etc... */
26
#include "crypto/ml_kem.h" /* ML_KEM_KEY, etc... */
27
#include "crypto/rsa.h" /* RSA_PSS_PARAMS_30, etc... */
28
#include "crypto/ml_dsa.h"
29
#include "crypto/slh_dsa.h"
30
#include "prov/bio.h"
31
#include "prov/implementations.h"
32
#include "internal/encoder.h"
33
#include "prov/endecoder_local.h"
34
#include "prov/ml_dsa_codecs.h"
35
#include "prov/ml_kem_codecs.h"
36
37
DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
38
39
/* ---------------------------------------------------------------------- */
40
41
#ifndef OPENSSL_NO_DH
42
static int dh_to_text(BIO *out, const void *key, int selection)
43
0
{
44
0
    const DH *dh = key;
45
0
    const char *type_label = NULL;
46
0
    const BIGNUM *priv_key = NULL, *pub_key = NULL;
47
0
    const FFC_PARAMS *params = NULL;
48
0
    const BIGNUM *p = NULL;
49
0
    long length;
50
51
0
    if (out == NULL || dh == NULL) {
52
0
        ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
53
0
        return 0;
54
0
    }
55
56
0
    if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
57
0
        type_label = "DH Private-Key";
58
0
    else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
59
0
        type_label = "DH Public-Key";
60
0
    else if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
61
0
        type_label = "DH Parameters";
62
63
0
    if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
64
0
        priv_key = DH_get0_priv_key(dh);
65
0
        if (priv_key == NULL) {
66
0
            ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PRIVATE_KEY);
67
0
            return 0;
68
0
        }
69
0
    }
70
0
    if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
71
0
        pub_key = DH_get0_pub_key(dh);
72
0
        if (pub_key == NULL) {
73
0
            ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY);
74
0
            return 0;
75
0
        }
76
0
    }
77
0
    if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
78
0
        params = ossl_dh_get0_params((DH *)dh);
79
0
        if (params == NULL) {
80
0
            ERR_raise(ERR_LIB_PROV, PROV_R_NOT_PARAMETERS);
81
0
            return 0;
82
0
        }
83
0
    }
84
85
0
    p = DH_get0_p(dh);
86
0
    if (p == NULL) {
87
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
88
0
        return 0;
89
0
    }
90
91
0
    if (BIO_printf(out, "%s: (%d bit)\n", type_label, BN_num_bits(p)) <= 0)
92
0
        return 0;
93
0
    if (priv_key != NULL
94
0
        && !ossl_bio_print_labeled_bignum(out, "private-key:", priv_key))
95
0
        return 0;
96
0
    if (pub_key != NULL
97
0
        && !ossl_bio_print_labeled_bignum(out, "public-key:", pub_key))
98
0
        return 0;
99
0
    if (params != NULL
100
0
        && !ossl_bio_print_ffc_params(out, params))
101
0
        return 0;
102
0
    length = DH_get_length(dh);
103
0
    if (length > 0
104
0
        && BIO_printf(out, "recommended-private-length: %ld bits\n",
105
0
               length)
106
0
            <= 0)
107
0
        return 0;
108
109
0
    return 1;
110
0
}
111
#endif
112
113
/* ---------------------------------------------------------------------- */
114
115
#ifndef OPENSSL_NO_DSA
116
static int dsa_to_text(BIO *out, const void *key, int selection)
117
0
{
118
0
    const DSA *dsa = key;
119
0
    const char *type_label = NULL;
120
0
    const BIGNUM *priv_key = NULL, *pub_key = NULL;
121
0
    const FFC_PARAMS *params = NULL;
122
0
    const BIGNUM *p = NULL;
123
124
0
    if (out == NULL || dsa == NULL) {
125
0
        ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
126
0
        return 0;
127
0
    }
128
129
0
    if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
130
0
        type_label = "Private-Key";
131
0
    else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
132
0
        type_label = "Public-Key";
133
0
    else if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
134
0
        type_label = "DSA-Parameters";
135
136
0
    if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
137
0
        priv_key = DSA_get0_priv_key(dsa);
138
0
        if (priv_key == NULL) {
139
0
            ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PRIVATE_KEY);
140
0
            return 0;
141
0
        }
142
0
    }
143
0
    if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
144
0
        pub_key = DSA_get0_pub_key(dsa);
145
0
        if (pub_key == NULL) {
146
0
            ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY);
147
0
            return 0;
148
0
        }
149
0
    }
150
0
    if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
151
0
        params = ossl_dsa_get0_params((DSA *)dsa);
152
0
        if (params == NULL) {
153
0
            ERR_raise(ERR_LIB_PROV, PROV_R_NOT_PARAMETERS);
154
0
            return 0;
155
0
        }
156
0
    }
157
158
0
    p = DSA_get0_p(dsa);
159
0
    if (p == NULL) {
160
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
161
0
        return 0;
162
0
    }
163
164
0
    if (BIO_printf(out, "%s: (%d bit)\n", type_label, BN_num_bits(p)) <= 0)
165
0
        return 0;
166
0
    if (priv_key != NULL
167
0
        && !ossl_bio_print_labeled_bignum(out, "priv:", priv_key))
168
0
        return 0;
169
0
    if (pub_key != NULL
170
0
        && !ossl_bio_print_labeled_bignum(out, "pub: ", pub_key))
171
0
        return 0;
172
0
    if (params != NULL
173
0
        && !ossl_bio_print_ffc_params(out, params))
174
0
        return 0;
175
176
0
    return 1;
177
0
}
178
#endif
179
180
/* ---------------------------------------------------------------------- */
181
182
#ifndef OPENSSL_NO_EC
183
static int ec_param_explicit_curve_to_text(BIO *out, const EC_GROUP *group,
184
    BN_CTX *ctx)
185
0
{
186
0
    const char *plabel = "Prime:";
187
0
    BIGNUM *p = NULL, *a = NULL, *b = NULL;
188
189
0
    p = BN_CTX_get(ctx);
190
0
    a = BN_CTX_get(ctx);
191
0
    b = BN_CTX_get(ctx);
192
0
    if (b == NULL
193
0
        || !EC_GROUP_get_curve(group, p, a, b, ctx))
194
0
        return 0;
195
196
0
    if (EC_GROUP_get_field_type(group) == NID_X9_62_characteristic_two_field) {
197
0
        int basis_type = EC_GROUP_get_basis_type(group);
198
199
        /* print the 'short name' of the base type OID */
200
0
        if (basis_type == NID_undef
201
0
            || BIO_printf(out, "Basis Type: %s\n", OBJ_nid2sn(basis_type)) <= 0)
202
0
            return 0;
203
0
        plabel = "Polynomial:";
204
0
    }
205
0
    return ossl_bio_print_labeled_bignum(out, plabel, p)
206
0
        && ossl_bio_print_labeled_bignum(out, "A:   ", a)
207
0
        && ossl_bio_print_labeled_bignum(out, "B:   ", b);
208
0
}
209
210
static int ec_param_explicit_gen_to_text(BIO *out, const EC_GROUP *group,
211
    BN_CTX *ctx)
212
0
{
213
0
    int ret;
214
0
    size_t buflen;
215
0
    point_conversion_form_t form;
216
0
    const EC_POINT *point = NULL;
217
0
    const char *glabel = NULL;
218
0
    unsigned char *buf = NULL;
219
220
0
    form = EC_GROUP_get_point_conversion_form(group);
221
0
    point = EC_GROUP_get0_generator(group);
222
223
0
    if (point == NULL)
224
0
        return 0;
225
226
0
    switch (form) {
227
0
    case POINT_CONVERSION_COMPRESSED:
228
0
        glabel = "Generator (compressed):";
229
0
        break;
230
0
    case POINT_CONVERSION_UNCOMPRESSED:
231
0
        glabel = "Generator (uncompressed):";
232
0
        break;
233
0
    case POINT_CONVERSION_HYBRID:
234
0
        glabel = "Generator (hybrid):";
235
0
        break;
236
0
    default:
237
0
        return 0;
238
0
    }
239
240
0
    buflen = EC_POINT_point2buf(group, point, form, &buf, ctx);
241
0
    if (buflen == 0)
242
0
        return 0;
243
244
0
    ret = ossl_bio_print_labeled_buf(out, glabel, buf, buflen);
245
0
    OPENSSL_clear_free(buf, buflen);
246
0
    return ret;
247
0
}
248
249
/* Print explicit parameters */
250
static int ec_param_explicit_to_text(BIO *out, const EC_GROUP *group,
251
    OSSL_LIB_CTX *libctx)
252
0
{
253
0
    int ret = 0, tmp_nid;
254
0
    BN_CTX *ctx = NULL;
255
0
    const BIGNUM *order = NULL, *cofactor = NULL;
256
0
    const unsigned char *seed;
257
0
    size_t seed_len = 0;
258
259
0
    ctx = BN_CTX_new_ex(libctx);
260
0
    if (ctx == NULL)
261
0
        return 0;
262
0
    BN_CTX_start(ctx);
263
264
0
    tmp_nid = EC_GROUP_get_field_type(group);
265
0
    order = EC_GROUP_get0_order(group);
266
0
    if (order == NULL)
267
0
        goto err;
268
269
0
    seed = EC_GROUP_get0_seed(group);
270
0
    if (seed != NULL)
271
0
        seed_len = EC_GROUP_get_seed_len(group);
272
0
    cofactor = EC_GROUP_get0_cofactor(group);
273
274
    /* print the 'short name' of the field type */
275
0
    if (BIO_printf(out, "Field Type: %s\n", OBJ_nid2sn(tmp_nid)) <= 0
276
0
        || !ec_param_explicit_curve_to_text(out, group, ctx)
277
0
        || !ec_param_explicit_gen_to_text(out, group, ctx)
278
0
        || !ossl_bio_print_labeled_bignum(out, "Order: ", order)
279
0
        || (cofactor != NULL
280
0
            && !ossl_bio_print_labeled_bignum(out, "Cofactor: ", cofactor))
281
0
        || (seed != NULL
282
0
            && !ossl_bio_print_labeled_buf(out, "Seed:", seed, seed_len)))
283
0
        goto err;
284
0
    ret = 1;
285
0
err:
286
0
    BN_CTX_end(ctx);
287
0
    BN_CTX_free(ctx);
288
0
    return ret;
289
0
}
290
291
static int ec_param_to_text(BIO *out, const EC_GROUP *group,
292
    OSSL_LIB_CTX *libctx)
293
0
{
294
0
    if (EC_GROUP_get_asn1_flag(group) & OPENSSL_EC_NAMED_CURVE) {
295
0
        const char *curve_name;
296
0
        int curve_nid = EC_GROUP_get_curve_name(group);
297
298
        /* Explicit parameters */
299
0
        if (curve_nid == NID_undef)
300
0
            return 0;
301
302
0
        if (BIO_printf(out, "%s: %s\n", "ASN1 OID", OBJ_nid2sn(curve_nid)) <= 0)
303
0
            return 0;
304
305
0
        curve_name = EC_curve_nid2nist(curve_nid);
306
0
        return (curve_name == NULL
307
0
            || BIO_printf(out, "%s: %s\n", "NIST CURVE", curve_name) > 0);
308
0
    } else {
309
0
        return ec_param_explicit_to_text(out, group, libctx);
310
0
    }
311
0
}
312
313
static int ec_to_text(BIO *out, const void *key, int selection)
314
0
{
315
0
    const EC_KEY *ec = key;
316
0
    const char *type_label = NULL;
317
0
    unsigned char *priv = NULL, *pub = NULL;
318
0
    size_t priv_len = 0, pub_len = 0;
319
0
    const EC_GROUP *group;
320
0
    int ret = 0;
321
322
0
    if (out == NULL || ec == NULL) {
323
0
        ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
324
0
        return 0;
325
0
    }
326
327
0
    if ((group = EC_KEY_get0_group(ec)) == NULL) {
328
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
329
0
        return 0;
330
0
    }
331
332
0
    if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
333
0
        type_label = "Private-Key";
334
0
    else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
335
0
        type_label = "Public-Key";
336
0
    else if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
337
0
        if (EC_GROUP_get_curve_name(group) != NID_sm2)
338
0
            type_label = "EC-Parameters";
339
340
0
    if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
341
0
        const BIGNUM *priv_key = EC_KEY_get0_private_key(ec);
342
343
0
        if (priv_key == NULL) {
344
0
            ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PRIVATE_KEY);
345
0
            goto err;
346
0
        }
347
0
        priv_len = EC_KEY_priv2buf(ec, &priv);
348
0
        if (priv_len == 0)
349
0
            goto err;
350
0
    }
351
0
    if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
352
0
        const EC_POINT *pub_pt = EC_KEY_get0_public_key(ec);
353
354
0
        if (pub_pt == NULL) {
355
0
            ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY);
356
0
            goto err;
357
0
        }
358
359
0
        pub_len = EC_KEY_key2buf(ec, EC_KEY_get_conv_form(ec), &pub, NULL);
360
0
        if (pub_len == 0)
361
0
            goto err;
362
0
    }
363
364
0
    if (type_label != NULL
365
0
        && BIO_printf(out, "%s: (%d bit field, %d bit security level)\n",
366
0
               type_label, EC_GROUP_get_degree(group),
367
0
               EC_GROUP_security_bits(group))
368
0
            <= 0)
369
0
        goto err;
370
0
    if (priv != NULL
371
0
        && !ossl_bio_print_labeled_buf(out, "priv:", priv, priv_len))
372
0
        goto err;
373
0
    if (pub != NULL
374
0
        && !ossl_bio_print_labeled_buf(out, "pub:", pub, pub_len))
375
0
        goto err;
376
0
    if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
377
0
        ret = ec_param_to_text(out, group, ossl_ec_key_get_libctx(ec));
378
0
err:
379
0
    OPENSSL_clear_free(priv, priv_len);
380
0
    OPENSSL_free(pub);
381
0
    return ret;
382
0
}
383
#endif
384
385
/* ---------------------------------------------------------------------- */
386
387
#ifndef OPENSSL_NO_ECX
388
static int ecx_to_text(BIO *out, const void *key, int selection)
389
0
{
390
0
    const ECX_KEY *ecx = key;
391
0
    const char *type_label = NULL;
392
393
0
    if (out == NULL || ecx == NULL) {
394
0
        ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
395
0
        return 0;
396
0
    }
397
398
0
    switch (ecx->type) {
399
0
    case ECX_KEY_TYPE_X25519:
400
0
        type_label = "X25519";
401
0
        break;
402
0
    case ECX_KEY_TYPE_X448:
403
0
        type_label = "X448";
404
0
        break;
405
0
    case ECX_KEY_TYPE_ED25519:
406
0
        type_label = "ED25519";
407
0
        break;
408
0
    case ECX_KEY_TYPE_ED448:
409
0
        type_label = "ED448";
410
0
        break;
411
0
    }
412
413
0
    if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
414
0
        if (ecx->privkey == NULL) {
415
0
            ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PRIVATE_KEY);
416
0
            return 0;
417
0
        }
418
419
0
        if (BIO_printf(out, "%s Private-Key:\n", type_label) <= 0)
420
0
            return 0;
421
0
        if (!ossl_bio_print_labeled_buf(out, "priv:", ecx->privkey, ecx->keylen))
422
0
            return 0;
423
0
    } else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
424
        /* ecx->pubkey is an array, not a pointer... */
425
0
        if (!ecx->haspubkey) {
426
0
            ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY);
427
0
            return 0;
428
0
        }
429
430
0
        if (BIO_printf(out, "%s Public-Key:\n", type_label) <= 0)
431
0
            return 0;
432
0
    }
433
434
0
    if (!ossl_bio_print_labeled_buf(out, "pub:", ecx->pubkey, ecx->keylen))
435
0
        return 0;
436
437
0
    return 1;
438
0
}
439
#endif
440
441
/* ---------------------------------------------------------------------- */
442
443
#ifndef OPENSSL_NO_ML_KEM
444
static int ml_kem_to_text(BIO *out, const void *vkey, int selection)
445
0
{
446
0
    return ossl_ml_kem_key_to_text(out, (const ML_KEM_KEY *)vkey, selection);
447
0
}
448
#endif
449
450
/* ---------------------------------------------------------------------- */
451
452
#ifndef OPENSSL_NO_SLH_DSA
453
static int slh_dsa_to_text(BIO *out, const void *key, int selection)
454
0
{
455
0
    return ossl_slh_dsa_key_to_text(out, (const SLH_DSA_KEY *)key, selection);
456
0
}
457
#endif /* OPENSSL_NO_SLH_DSA */
458
459
static int rsa_to_text(BIO *out, const void *key, int selection)
460
0
{
461
0
    const RSA *rsa = key;
462
0
    const char *type_label = "RSA key";
463
0
    const char *modulus_label = NULL;
464
0
    const char *exponent_label = NULL;
465
0
    const BIGNUM *rsa_d = NULL, *rsa_n = NULL, *rsa_e = NULL;
466
0
    STACK_OF(BIGNUM_const) *factors = NULL;
467
0
    STACK_OF(BIGNUM_const) *exps = NULL;
468
0
    STACK_OF(BIGNUM_const) *coeffs = NULL;
469
0
    int primes;
470
0
    const RSA_PSS_PARAMS_30 *pss_params = ossl_rsa_get0_pss_params_30((RSA *)rsa);
471
0
    int ret = 0;
472
473
0
    if (out == NULL || rsa == NULL) {
474
0
        ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
475
0
        goto err;
476
0
    }
477
478
0
    factors = sk_BIGNUM_const_new_null();
479
0
    exps = sk_BIGNUM_const_new_null();
480
0
    coeffs = sk_BIGNUM_const_new_null();
481
482
0
    if (factors == NULL || exps == NULL || coeffs == NULL) {
483
0
        ERR_raise(ERR_LIB_PROV, ERR_R_CRYPTO_LIB);
484
0
        goto err;
485
0
    }
486
487
0
    if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
488
0
        type_label = "Private-Key";
489
0
        modulus_label = "modulus:";
490
0
        exponent_label = "publicExponent:";
491
0
    } else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
492
0
        type_label = "Public-Key";
493
0
        modulus_label = "Modulus:";
494
0
        exponent_label = "Exponent:";
495
0
    }
496
497
0
    RSA_get0_key(rsa, &rsa_n, &rsa_e, &rsa_d);
498
0
    ossl_rsa_get0_all_params((RSA *)rsa, factors, exps, coeffs);
499
0
    primes = sk_BIGNUM_const_num(factors);
500
501
0
    if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
502
0
        if (BIO_printf(out, "%s: (%d bit, %d primes)\n",
503
0
                type_label, BN_num_bits(rsa_n), primes)
504
0
            <= 0)
505
0
            goto err;
506
0
    } else {
507
0
        if (BIO_printf(out, "%s: (%d bit)\n",
508
0
                type_label, BN_num_bits(rsa_n))
509
0
            <= 0)
510
0
            goto err;
511
0
    }
512
513
0
    if (!ossl_bio_print_labeled_bignum(out, modulus_label, rsa_n))
514
0
        goto err;
515
0
    if (!ossl_bio_print_labeled_bignum(out, exponent_label, rsa_e))
516
0
        goto err;
517
0
    if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
518
0
        int i;
519
520
0
        if (!ossl_bio_print_labeled_bignum(out, "privateExponent:", rsa_d))
521
0
            goto err;
522
0
        if (!ossl_bio_print_labeled_bignum(out, "prime1:",
523
0
                sk_BIGNUM_const_value(factors, 0)))
524
0
            goto err;
525
0
        if (!ossl_bio_print_labeled_bignum(out, "prime2:",
526
0
                sk_BIGNUM_const_value(factors, 1)))
527
0
            goto err;
528
0
        if (!ossl_bio_print_labeled_bignum(out, "exponent1:",
529
0
                sk_BIGNUM_const_value(exps, 0)))
530
0
            goto err;
531
0
        if (!ossl_bio_print_labeled_bignum(out, "exponent2:",
532
0
                sk_BIGNUM_const_value(exps, 1)))
533
0
            goto err;
534
0
        if (!ossl_bio_print_labeled_bignum(out, "coefficient:",
535
0
                sk_BIGNUM_const_value(coeffs, 0)))
536
0
            goto err;
537
0
        for (i = 2; i < sk_BIGNUM_const_num(factors); i++) {
538
0
            if (BIO_printf(out, "prime%d:", i + 1) <= 0)
539
0
                goto err;
540
0
            if (!ossl_bio_print_labeled_bignum(out, NULL,
541
0
                    sk_BIGNUM_const_value(factors, i)))
542
0
                goto err;
543
0
            if (BIO_printf(out, "exponent%d:", i + 1) <= 0)
544
0
                goto err;
545
0
            if (!ossl_bio_print_labeled_bignum(out, NULL,
546
0
                    sk_BIGNUM_const_value(exps, i)))
547
0
                goto err;
548
0
            if (BIO_printf(out, "coefficient%d:", i + 1) <= 0)
549
0
                goto err;
550
0
            if (!ossl_bio_print_labeled_bignum(out, NULL,
551
0
                    sk_BIGNUM_const_value(coeffs, i - 1)))
552
0
                goto err;
553
0
        }
554
0
    }
555
556
0
    if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0) {
557
0
        switch (RSA_test_flags(rsa, RSA_FLAG_TYPE_MASK)) {
558
0
        case RSA_FLAG_TYPE_RSA:
559
0
            if (!ossl_rsa_pss_params_30_is_unrestricted(pss_params)) {
560
0
                if (BIO_printf(out, "(INVALID PSS PARAMETERS)\n") <= 0)
561
0
                    goto err;
562
0
            }
563
0
            break;
564
0
        case RSA_FLAG_TYPE_RSASSAPSS:
565
0
            if (ossl_rsa_pss_params_30_is_unrestricted(pss_params)) {
566
0
                if (BIO_printf(out, "No PSS parameter restrictions\n") <= 0)
567
0
                    goto err;
568
0
            } else {
569
0
                int hashalg_nid = ossl_rsa_pss_params_30_hashalg(pss_params);
570
0
                int maskgenalg_nid = ossl_rsa_pss_params_30_maskgenalg(pss_params);
571
0
                int maskgenhashalg_nid = ossl_rsa_pss_params_30_maskgenhashalg(pss_params);
572
0
                int saltlen = ossl_rsa_pss_params_30_saltlen(pss_params);
573
0
                int trailerfield = ossl_rsa_pss_params_30_trailerfield(pss_params);
574
575
0
                if (BIO_printf(out, "PSS parameter restrictions:\n") <= 0)
576
0
                    goto err;
577
0
                if (BIO_printf(out, "  Hash Algorithm: %s%s\n",
578
0
                        ossl_rsa_oaeppss_nid2name(hashalg_nid),
579
0
                        (hashalg_nid == NID_sha1
580
0
                                ? " (default)"
581
0
                                : ""))
582
0
                    <= 0)
583
0
                    goto err;
584
0
                if (BIO_printf(out, "  Mask Algorithm: %s with %s%s\n",
585
0
                        ossl_rsa_mgf_nid2name(maskgenalg_nid),
586
0
                        ossl_rsa_oaeppss_nid2name(maskgenhashalg_nid),
587
0
                        (maskgenalg_nid == NID_mgf1
588
0
                                    && maskgenhashalg_nid == NID_sha1
589
0
                                ? " (default)"
590
0
                                : ""))
591
0
                    <= 0)
592
0
                    goto err;
593
0
                if (BIO_printf(out, "  Minimum Salt Length: %d%s\n",
594
0
                        saltlen,
595
0
                        (saltlen == 20 ? " (default)" : ""))
596
0
                    <= 0)
597
0
                    goto err;
598
0
                if (BIO_printf(out, "  Trailer Field: 0x%x%s\n",
599
0
                        trailerfield,
600
0
                        (trailerfield == 1 ? " (default)" : ""))
601
0
                    <= 0)
602
0
                    goto err;
603
0
            }
604
0
            break;
605
0
        }
606
0
    }
607
608
0
    ret = 1;
609
0
err:
610
0
    sk_BIGNUM_const_free(factors);
611
0
    sk_BIGNUM_const_free(exps);
612
0
    sk_BIGNUM_const_free(coeffs);
613
0
    return ret;
614
0
}
615
616
/* ---------------------------------------------------------------------- */
617
618
#ifndef OPENSSL_NO_ML_DSA
619
static int ml_dsa_to_text(BIO *out, const void *key, int selection)
620
0
{
621
0
    return ossl_ml_dsa_key_to_text(out, (const ML_DSA_KEY *)key, selection);
622
0
}
623
#endif /* OPENSSL_NO_ML_DSA */
624
/* ---------------------------------------------------------------------- */
625
626
static void *key2text_newctx(void *provctx)
627
0
{
628
0
    return provctx;
629
0
}
630
631
static void key2text_freectx(ossl_unused void *vctx)
632
0
{
633
0
}
634
635
static int key2text_encode(void *vctx, const void *key, int selection,
636
    OSSL_CORE_BIO *cout,
637
    int (*key2text)(BIO *out, const void *key,
638
        int selection),
639
    OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
640
0
{
641
0
    BIO *out = ossl_bio_new_from_core_bio(vctx, cout);
642
0
    int ret;
643
644
0
    if (out == NULL)
645
0
        return 0;
646
647
0
    ret = key2text(out, key, selection);
648
0
    BIO_free(out);
649
650
0
    return ret;
651
0
}
652
653
#define MAKE_TEXT_ENCODER(impl, type)                                 \
654
    static OSSL_FUNC_encoder_import_object_fn                         \
655
        impl##2text_import_object;                                    \
656
    static OSSL_FUNC_encoder_free_object_fn                           \
657
        impl##2text_free_object;                                      \
658
    static OSSL_FUNC_encoder_encode_fn impl##2text_encode;            \
659
                                                                      \
660
    static void *impl##2text_import_object(void *ctx, int selection,  \
661
        const OSSL_PARAM params[])                                    \
662
0
    {                                                                 \
663
0
        return ossl_prov_import_key(ossl_##impl##_keymgmt_functions,  \
664
0
            ctx, selection, params);                                  \
665
0
    }                                                                 \
Unexecuted instantiation: encode_key2text.c:dh2text_import_object
Unexecuted instantiation: encode_key2text.c:dhx2text_import_object
Unexecuted instantiation: encode_key2text.c:dsa2text_import_object
Unexecuted instantiation: encode_key2text.c:ec2text_import_object
Unexecuted instantiation: encode_key2text.c:sm22text_import_object
Unexecuted instantiation: encode_key2text.c:ed255192text_import_object
Unexecuted instantiation: encode_key2text.c:ed4482text_import_object
Unexecuted instantiation: encode_key2text.c:x255192text_import_object
Unexecuted instantiation: encode_key2text.c:x4482text_import_object
Unexecuted instantiation: encode_key2text.c:ml_kem_5122text_import_object
Unexecuted instantiation: encode_key2text.c:ml_kem_7682text_import_object
Unexecuted instantiation: encode_key2text.c:ml_kem_10242text_import_object
Unexecuted instantiation: encode_key2text.c:rsa2text_import_object
Unexecuted instantiation: encode_key2text.c:rsapss2text_import_object
Unexecuted instantiation: encode_key2text.c:ml_dsa_442text_import_object
Unexecuted instantiation: encode_key2text.c:ml_dsa_652text_import_object
Unexecuted instantiation: encode_key2text.c:ml_dsa_872text_import_object
Unexecuted instantiation: encode_key2text.c:slh_dsa_sha2_128s2text_import_object
Unexecuted instantiation: encode_key2text.c:slh_dsa_sha2_128f2text_import_object
Unexecuted instantiation: encode_key2text.c:slh_dsa_sha2_192s2text_import_object
Unexecuted instantiation: encode_key2text.c:slh_dsa_sha2_192f2text_import_object
Unexecuted instantiation: encode_key2text.c:slh_dsa_sha2_256s2text_import_object
Unexecuted instantiation: encode_key2text.c:slh_dsa_sha2_256f2text_import_object
Unexecuted instantiation: encode_key2text.c:slh_dsa_shake_128s2text_import_object
Unexecuted instantiation: encode_key2text.c:slh_dsa_shake_128f2text_import_object
Unexecuted instantiation: encode_key2text.c:slh_dsa_shake_192s2text_import_object
Unexecuted instantiation: encode_key2text.c:slh_dsa_shake_192f2text_import_object
Unexecuted instantiation: encode_key2text.c:slh_dsa_shake_256s2text_import_object
Unexecuted instantiation: encode_key2text.c:slh_dsa_shake_256f2text_import_object
666
    static void impl##2text_free_object(void *key)                    \
667
0
    {                                                                 \
668
0
        ossl_prov_free_key(ossl_##impl##_keymgmt_functions, key);     \
669
0
    }                                                                 \
Unexecuted instantiation: encode_key2text.c:dh2text_free_object
Unexecuted instantiation: encode_key2text.c:dhx2text_free_object
Unexecuted instantiation: encode_key2text.c:dsa2text_free_object
Unexecuted instantiation: encode_key2text.c:ec2text_free_object
Unexecuted instantiation: encode_key2text.c:sm22text_free_object
Unexecuted instantiation: encode_key2text.c:ed255192text_free_object
Unexecuted instantiation: encode_key2text.c:ed4482text_free_object
Unexecuted instantiation: encode_key2text.c:x255192text_free_object
Unexecuted instantiation: encode_key2text.c:x4482text_free_object
Unexecuted instantiation: encode_key2text.c:ml_kem_5122text_free_object
Unexecuted instantiation: encode_key2text.c:ml_kem_7682text_free_object
Unexecuted instantiation: encode_key2text.c:ml_kem_10242text_free_object
Unexecuted instantiation: encode_key2text.c:rsa2text_free_object
Unexecuted instantiation: encode_key2text.c:rsapss2text_free_object
Unexecuted instantiation: encode_key2text.c:ml_dsa_442text_free_object
Unexecuted instantiation: encode_key2text.c:ml_dsa_652text_free_object
Unexecuted instantiation: encode_key2text.c:ml_dsa_872text_free_object
Unexecuted instantiation: encode_key2text.c:slh_dsa_sha2_128s2text_free_object
Unexecuted instantiation: encode_key2text.c:slh_dsa_sha2_128f2text_free_object
Unexecuted instantiation: encode_key2text.c:slh_dsa_sha2_192s2text_free_object
Unexecuted instantiation: encode_key2text.c:slh_dsa_sha2_192f2text_free_object
Unexecuted instantiation: encode_key2text.c:slh_dsa_sha2_256s2text_free_object
Unexecuted instantiation: encode_key2text.c:slh_dsa_sha2_256f2text_free_object
Unexecuted instantiation: encode_key2text.c:slh_dsa_shake_128s2text_free_object
Unexecuted instantiation: encode_key2text.c:slh_dsa_shake_128f2text_free_object
Unexecuted instantiation: encode_key2text.c:slh_dsa_shake_192s2text_free_object
Unexecuted instantiation: encode_key2text.c:slh_dsa_shake_192f2text_free_object
Unexecuted instantiation: encode_key2text.c:slh_dsa_shake_256s2text_free_object
Unexecuted instantiation: encode_key2text.c:slh_dsa_shake_256f2text_free_object
670
    static int impl##2text_encode(void *vctx, OSSL_CORE_BIO *cout,    \
671
        const void *key,                                              \
672
        const OSSL_PARAM key_abstract[],                              \
673
        int selection,                                                \
674
        OSSL_PASSPHRASE_CALLBACK *cb,                                 \
675
        void *cbarg)                                                  \
676
0
    {                                                                 \
677
0
        /* We don't deal with abstract objects */                     \
678
0
        if (key_abstract != NULL) {                                   \
679
0
            ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);   \
680
0
            return 0;                                                 \
681
0
        }                                                             \
682
0
        return key2text_encode(vctx, key, selection, cout,            \
683
0
            type##_to_text, cb, cbarg);                               \
684
0
    }                                                                 \
685
    const OSSL_DISPATCH ossl_##impl##_to_text_encoder_functions[] = { \
686
        { OSSL_FUNC_ENCODER_NEWCTX,                                   \
687
            (void (*)(void))key2text_newctx },                        \
688
        { OSSL_FUNC_ENCODER_FREECTX,                                  \
689
            (void (*)(void))key2text_freectx },                       \
690
        { OSSL_FUNC_ENCODER_IMPORT_OBJECT,                            \
691
            (void (*)(void))impl##2text_import_object },              \
692
        { OSSL_FUNC_ENCODER_FREE_OBJECT,                              \
693
            (void (*)(void))impl##2text_free_object },                \
694
        { OSSL_FUNC_ENCODER_ENCODE,                                   \
695
            (void (*)(void))impl##2text_encode },                     \
696
        OSSL_DISPATCH_END                                             \
697
    }
698
699
#ifndef OPENSSL_NO_DH
700
0
MAKE_TEXT_ENCODER(dh, dh);
701
0
MAKE_TEXT_ENCODER(dhx, dh);
702
#endif
703
#ifndef OPENSSL_NO_DSA
704
0
MAKE_TEXT_ENCODER(dsa, dsa);
705
#endif
706
#ifndef OPENSSL_NO_EC
707
0
MAKE_TEXT_ENCODER(ec, ec);
708
#ifndef OPENSSL_NO_SM2
709
0
MAKE_TEXT_ENCODER(sm2, ec);
710
#endif
711
#ifndef OPENSSL_NO_ECX
712
0
MAKE_TEXT_ENCODER(ed25519, ecx);
713
0
MAKE_TEXT_ENCODER(ed448, ecx);
714
0
MAKE_TEXT_ENCODER(x25519, ecx);
715
0
MAKE_TEXT_ENCODER(x448, ecx);
716
#endif
717
#endif
718
#ifndef OPENSSL_NO_ML_KEM
719
0
MAKE_TEXT_ENCODER(ml_kem_512, ml_kem);
720
0
MAKE_TEXT_ENCODER(ml_kem_768, ml_kem);
721
0
MAKE_TEXT_ENCODER(ml_kem_1024, ml_kem);
722
#endif
723
0
MAKE_TEXT_ENCODER(rsa, rsa);
724
0
MAKE_TEXT_ENCODER(rsapss, rsa);
725
726
#ifndef OPENSSL_NO_ML_DSA
727
0
MAKE_TEXT_ENCODER(ml_dsa_44, ml_dsa);
728
0
MAKE_TEXT_ENCODER(ml_dsa_65, ml_dsa);
729
0
MAKE_TEXT_ENCODER(ml_dsa_87, ml_dsa);
730
#endif
731
732
#ifndef OPENSSL_NO_SLH_DSA
733
0
MAKE_TEXT_ENCODER(slh_dsa_sha2_128s, slh_dsa);
734
0
MAKE_TEXT_ENCODER(slh_dsa_sha2_128f, slh_dsa);
735
0
MAKE_TEXT_ENCODER(slh_dsa_sha2_192s, slh_dsa);
736
0
MAKE_TEXT_ENCODER(slh_dsa_sha2_192f, slh_dsa);
737
0
MAKE_TEXT_ENCODER(slh_dsa_sha2_256s, slh_dsa);
738
0
MAKE_TEXT_ENCODER(slh_dsa_sha2_256f, slh_dsa);
739
0
MAKE_TEXT_ENCODER(slh_dsa_shake_128s, slh_dsa);
740
0
MAKE_TEXT_ENCODER(slh_dsa_shake_128f, slh_dsa);
741
0
MAKE_TEXT_ENCODER(slh_dsa_shake_192s, slh_dsa);
742
0
MAKE_TEXT_ENCODER(slh_dsa_shake_192f, slh_dsa);
743
0
MAKE_TEXT_ENCODER(slh_dsa_shake_256s, slh_dsa);
744
MAKE_TEXT_ENCODER(slh_dsa_shake_256f, slh_dsa);
745
#endif