Coverage Report

Created: 2025-12-31 06:58

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