Coverage Report

Created: 2025-06-13 06:58

/src/openssl30/providers/implementations/encode_decode/encode_key2text.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2020-2023 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 <ctype.h>
16
17
#include <openssl/core.h>
18
#include <openssl/core_dispatch.h>
19
#include <openssl/core_names.h>
20
#include <openssl/bn.h>
21
#include <openssl/err.h>
22
#include <openssl/safestack.h>
23
#include <openssl/proverr.h>
24
#include "internal/ffc.h"
25
#include "crypto/bn.h"           /* bn_get_words() */
26
#include "crypto/dh.h"           /* ossl_dh_get0_params() */
27
#include "crypto/dsa.h"          /* ossl_dsa_get0_params() */
28
#include "crypto/ec.h"           /* ossl_ec_key_get_libctx */
29
#include "crypto/ecx.h"          /* ECX_KEY, etc... */
30
#include "crypto/rsa.h"          /* RSA_PSS_PARAMS_30, etc... */
31
#include "prov/bio.h"
32
#include "prov/implementations.h"
33
#include "endecoder_local.h"
34
35
DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
36
37
# ifdef SIXTY_FOUR_BIT_LONG
38
#  define BN_FMTu "%lu"
39
#  define BN_FMTx "%lx"
40
# endif
41
42
# ifdef SIXTY_FOUR_BIT
43
#  define BN_FMTu "%llu"
44
#  define BN_FMTx "%llx"
45
# endif
46
47
# ifdef THIRTY_TWO_BIT
48
#  define BN_FMTu "%u"
49
#  define BN_FMTx "%x"
50
# endif
51
52
static int print_labeled_bignum(BIO *out, const char *label, const BIGNUM *bn)
53
270k
{
54
270k
    int ret = 0, use_sep = 0;
55
270k
    char *hex_str = NULL, *p;
56
270k
    const char spaces[] = "    ";
57
270k
    const char *post_label_spc = " ";
58
59
270k
    const char *neg = "";
60
270k
    int bytes;
61
62
270k
    if (bn == NULL)
63
286
        return 0;
64
270k
    if (label == NULL) {
65
228k
        label = "";
66
228k
        post_label_spc = "";
67
228k
    }
68
69
270k
    if (BN_is_zero(bn))
70
48.7k
        return BIO_printf(out, "%s%s0\n", label, post_label_spc);
71
72
221k
    if (BN_num_bytes(bn) <= BN_BYTES) {
73
197k
        BN_ULONG *words = bn_get_words(bn);
74
75
197k
        if (BN_is_negative(bn))
76
281
            neg = "-";
77
78
197k
        return BIO_printf(out, "%s%s%s" BN_FMTu " (%s0x" BN_FMTx ")\n",
79
197k
                          label, post_label_spc, neg, words[0], neg, words[0]);
80
197k
    }
81
82
24.3k
    hex_str = BN_bn2hex(bn);
83
24.3k
    if (hex_str == NULL)
84
0
        return 0;
85
86
24.3k
    p = hex_str;
87
24.3k
    if (*p == '-') {
88
52
        ++p;
89
52
        neg = " (Negative)";
90
52
    }
91
24.3k
    if (BIO_printf(out, "%s%s\n", label, neg) <= 0)
92
0
        goto err;
93
94
    /* Keep track of how many bytes we have printed out so far */
95
24.3k
    bytes = 0;
96
97
24.3k
    if (BIO_printf(out, "%s", spaces) <= 0)
98
0
        goto err;
99
100
    /* Add a leading 00 if the top bit is set */
101
24.3k
    if (*p >= '8') {
102
13.8k
        if (BIO_printf(out, "%02x", 0) <= 0)
103
0
            goto err;
104
13.8k
        ++bytes;
105
13.8k
        use_sep = 1;
106
13.8k
    }
107
19.4M
    while (*p != '\0') {
108
        /* Do a newline after every 15 hex bytes + add the space indent */
109
19.4M
        if ((bytes % 15) == 0 && bytes > 0) {
110
1.28M
            if (BIO_printf(out, ":\n%s", spaces) <= 0)
111
0
                goto err;
112
1.28M
            use_sep = 0; /* The first byte on the next line doesnt have a : */
113
1.28M
        }
114
19.4M
        if (BIO_printf(out, "%s%c%c", use_sep ? ":" : "",
115
19.4M
                       tolower((unsigned char)p[0]),
116
19.4M
                       tolower((unsigned char)p[1])) <= 0)
117
0
            goto err;
118
19.4M
        ++bytes;
119
19.4M
        p += 2;
120
19.4M
        use_sep = 1;
121
19.4M
    }
122
24.3k
    if (BIO_printf(out, "\n") <= 0)
123
0
        goto err;
124
24.3k
    ret = 1;
125
24.3k
err:
126
24.3k
    OPENSSL_free(hex_str);
127
24.3k
    return ret;
128
24.3k
}
129
130
/* Number of octets per line */
131
257k
#define LABELED_BUF_PRINT_WIDTH    15
132
133
#if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_EC)
134
static int print_labeled_buf(BIO *out, const char *label,
135
                             const unsigned char *buf, size_t buflen)
136
6.51k
{
137
6.51k
    size_t i;
138
139
6.51k
    if (BIO_printf(out, "%s\n", label) <= 0)
140
0
        return 0;
141
142
263k
    for (i = 0; i < buflen; i++) {
143
257k
        if ((i % LABELED_BUF_PRINT_WIDTH) == 0) {
144
19.7k
            if (i > 0 && BIO_printf(out, "\n") <= 0)
145
0
                return 0;
146
19.7k
            if (BIO_printf(out, "    ") <= 0)
147
0
                return 0;
148
19.7k
        }
149
150
257k
        if (BIO_printf(out, "%02x%s", buf[i],
151
257k
                                 (i == buflen - 1) ? "" : ":") <= 0)
152
0
            return 0;
153
257k
    }
154
6.51k
    if (BIO_printf(out, "\n") <= 0)
155
0
        return 0;
156
157
6.51k
    return 1;
158
6.51k
}
159
#endif
160
161
#if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_DSA)
162
static int ffc_params_to_text(BIO *out, const FFC_PARAMS *ffc)
163
5.64k
{
164
5.64k
    if (ffc->nid != NID_undef) {
165
0
#ifndef OPENSSL_NO_DH
166
0
        const DH_NAMED_GROUP *group = ossl_ffc_uid_to_dh_named_group(ffc->nid);
167
0
        const char *name = ossl_ffc_named_group_get_name(group);
168
169
0
        if (name == NULL)
170
0
            goto err;
171
0
        if (BIO_printf(out, "GROUP: %s\n", name) <= 0)
172
0
            goto err;
173
0
        return 1;
174
#else
175
        /* How could this be? We should not have a nid in a no-dh build. */
176
        goto err;
177
#endif
178
0
    }
179
180
5.64k
    if (!print_labeled_bignum(out, "P:   ", ffc->p))
181
0
        goto err;
182
5.64k
    if (ffc->q != NULL) {
183
4.46k
        if (!print_labeled_bignum(out, "Q:   ", ffc->q))
184
0
            goto err;
185
4.46k
    }
186
5.64k
    if (!print_labeled_bignum(out, "G:   ", ffc->g))
187
0
        goto err;
188
5.64k
    if (ffc->j != NULL) {
189
104
        if (!print_labeled_bignum(out, "J:   ", ffc->j))
190
0
            goto err;
191
104
    }
192
5.64k
    if (ffc->seed != NULL) {
193
58
        if (!print_labeled_buf(out, "SEED:", ffc->seed, ffc->seedlen))
194
0
            goto err;
195
58
    }
196
5.64k
    if (ffc->gindex != -1) {
197
0
        if (BIO_printf(out, "gindex: %d\n", ffc->gindex) <= 0)
198
0
            goto err;
199
0
    }
200
5.64k
    if (ffc->pcounter != -1) {
201
74
        if (BIO_printf(out, "pcounter: %d\n", ffc->pcounter) <= 0)
202
0
            goto err;
203
74
    }
204
5.64k
    if (ffc->h != 0) {
205
0
        if (BIO_printf(out, "h: %d\n", ffc->h) <= 0)
206
0
            goto err;
207
0
    }
208
5.64k
    return 1;
209
0
err:
210
0
    return 0;
211
5.64k
}
212
#endif
213
214
/* ---------------------------------------------------------------------- */
215
216
#ifndef OPENSSL_NO_DH
217
static int dh_to_text(BIO *out, const void *key, int selection)
218
13.1k
{
219
13.1k
    const DH *dh = key;
220
13.1k
    const char *type_label = NULL;
221
13.1k
    const BIGNUM *priv_key = NULL, *pub_key = NULL;
222
13.1k
    const FFC_PARAMS *params = NULL;
223
13.1k
    const BIGNUM *p = NULL;
224
13.1k
    long length;
225
226
13.1k
    if (out == NULL || dh == NULL) {
227
0
        ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
228
0
        return 0;
229
0
    }
230
231
13.1k
    if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
232
4.24k
        type_label = "DH Private-Key";
233
8.91k
    else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
234
4.75k
        type_label = "DH Public-Key";
235
4.16k
    else if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
236
4.16k
        type_label = "DH Parameters";
237
238
13.1k
    if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
239
4.24k
        priv_key = DH_get0_priv_key(dh);
240
4.24k
        if (priv_key == NULL) {
241
4.15k
            ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PRIVATE_KEY);
242
4.15k
            return 0;
243
4.15k
        }
244
4.24k
    }
245
9.00k
    if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
246
4.83k
        pub_key = DH_get0_pub_key(dh);
247
4.83k
        if (pub_key == NULL) {
248
3.90k
            ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY);
249
3.90k
            return 0;
250
3.90k
        }
251
4.83k
    }
252
5.09k
    if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
253
5.09k
        params = ossl_dh_get0_params((DH *)dh);
254
5.09k
        if (params == NULL) {
255
0
            ERR_raise(ERR_LIB_PROV, PROV_R_NOT_PARAMETERS);
256
0
            return 0;
257
0
        }
258
5.09k
    }
259
260
5.09k
    p = DH_get0_p(dh);
261
5.09k
    if (p == NULL) {
262
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
263
0
        return 0;
264
0
    }
265
266
5.09k
    if (BIO_printf(out, "%s: (%d bit)\n", type_label, BN_num_bits(p)) <= 0)
267
0
        return 0;
268
5.09k
    if (priv_key != NULL
269
5.09k
        && !print_labeled_bignum(out, "private-key:", priv_key))
270
0
        return 0;
271
5.09k
    if (pub_key != NULL
272
5.09k
        && !print_labeled_bignum(out, "public-key:", pub_key))
273
0
        return 0;
274
5.09k
    if (params != NULL
275
5.09k
        && !ffc_params_to_text(out, params))
276
0
        return 0;
277
5.09k
    length = DH_get_length(dh);
278
5.09k
    if (length > 0
279
5.09k
        && BIO_printf(out, "recommended-private-length: %ld bits\n",
280
274
                      length) <= 0)
281
0
        return 0;
282
283
5.09k
    return 1;
284
5.09k
}
285
286
# define dh_input_type          "DH"
287
# define dhx_input_type         "DHX"
288
#endif
289
290
/* ---------------------------------------------------------------------- */
291
292
#ifndef OPENSSL_NO_DSA
293
static int dsa_to_text(BIO *out, const void *key, int selection)
294
4.66k
{
295
4.66k
    const DSA *dsa = key;
296
4.66k
    const char *type_label = NULL;
297
4.66k
    const BIGNUM *priv_key = NULL, *pub_key = NULL;
298
4.66k
    const FFC_PARAMS *params = NULL;
299
4.66k
    const BIGNUM *p = NULL;
300
301
4.66k
    if (out == NULL || dsa == NULL) {
302
0
        ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
303
0
        return 0;
304
0
    }
305
306
4.66k
    if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
307
2.68k
        type_label = "Private-Key";
308
1.97k
    else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
309
1.04k
        type_label = "Public-Key";
310
933
    else if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
311
933
        type_label = "DSA-Parameters";
312
313
4.66k
    if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
314
2.68k
        priv_key = DSA_get0_priv_key(dsa);
315
2.68k
        if (priv_key == NULL) {
316
46
            ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PRIVATE_KEY);
317
46
            return 0;
318
46
        }
319
2.68k
    }
320
4.61k
    if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
321
3.68k
        pub_key = DSA_get0_pub_key(dsa);
322
3.68k
        if (pub_key == NULL) {
323
0
            ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY);
324
0
            return 0;
325
0
        }
326
3.68k
    }
327
4.61k
    if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
328
4.61k
        params = ossl_dsa_get0_params((DSA *)dsa);
329
4.61k
        if (params == NULL) {
330
0
            ERR_raise(ERR_LIB_PROV, PROV_R_NOT_PARAMETERS);
331
0
            return 0;
332
0
        }
333
4.61k
    }
334
335
4.61k
    p = DSA_get0_p(dsa);
336
4.61k
    if (p == NULL) {
337
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
338
0
        return 0;
339
0
    }
340
341
4.61k
    if (BIO_printf(out, "%s: (%d bit)\n", type_label, BN_num_bits(p)) <= 0)
342
0
        return 0;
343
4.61k
    if (priv_key != NULL
344
4.61k
        && !print_labeled_bignum(out, "priv:", priv_key))
345
0
        return 0;
346
4.61k
    if (pub_key != NULL
347
4.61k
        && !print_labeled_bignum(out, "pub: ", pub_key))
348
0
        return 0;
349
4.61k
    if (params != NULL
350
4.61k
        && !ffc_params_to_text(out, params))
351
0
        return 0;
352
353
4.61k
    return 1;
354
4.61k
}
355
356
# define dsa_input_type         "DSA"
357
#endif
358
359
/* ---------------------------------------------------------------------- */
360
361
#ifndef OPENSSL_NO_EC
362
static int ec_param_explicit_curve_to_text(BIO *out, const EC_GROUP *group,
363
                                           BN_CTX *ctx)
364
881
{
365
881
    const char *plabel = "Prime:";
366
881
    BIGNUM *p = NULL, *a = NULL, *b = NULL;
367
368
881
    p = BN_CTX_get(ctx);
369
881
    a = BN_CTX_get(ctx);
370
881
    b = BN_CTX_get(ctx);
371
881
    if (b == NULL
372
881
        || !EC_GROUP_get_curve(group, p, a, b, ctx))
373
0
        return 0;
374
375
881
    if (EC_GROUP_get_field_type(group) == NID_X9_62_characteristic_two_field) {
376
0
        int basis_type = EC_GROUP_get_basis_type(group);
377
378
        /* print the 'short name' of the base type OID */
379
0
        if (basis_type == NID_undef
380
0
            || BIO_printf(out, "Basis Type: %s\n", OBJ_nid2sn(basis_type)) <= 0)
381
0
            return 0;
382
0
        plabel = "Polynomial:";
383
0
    }
384
881
    return print_labeled_bignum(out, plabel, p)
385
881
        && print_labeled_bignum(out, "A:   ", a)
386
881
        && print_labeled_bignum(out, "B:   ", b);
387
881
}
388
389
static int ec_param_explicit_gen_to_text(BIO *out, const EC_GROUP *group,
390
                                         BN_CTX *ctx)
391
881
{
392
881
    int ret;
393
881
    size_t buflen;
394
881
    point_conversion_form_t form;
395
881
    const EC_POINT *point = NULL;
396
881
    const char *glabel = NULL;
397
881
    unsigned char *buf = NULL;
398
399
881
    form = EC_GROUP_get_point_conversion_form(group);
400
881
    point = EC_GROUP_get0_generator(group);
401
402
881
    if (point == NULL)
403
0
        return 0;
404
405
881
    switch (form) {
406
708
    case POINT_CONVERSION_COMPRESSED:
407
708
       glabel = "Generator (compressed):";
408
708
       break;
409
119
    case POINT_CONVERSION_UNCOMPRESSED:
410
119
        glabel = "Generator (uncompressed):";
411
119
        break;
412
54
    case POINT_CONVERSION_HYBRID:
413
54
        glabel = "Generator (hybrid):";
414
54
        break;
415
0
    default:
416
0
        return 0;
417
881
    }
418
419
881
    buflen = EC_POINT_point2buf(group, point, form, &buf, ctx);
420
881
    if (buflen == 0)
421
0
        return 0;
422
423
881
    ret = print_labeled_buf(out, glabel, buf, buflen);
424
881
    OPENSSL_clear_free(buf, buflen);
425
881
    return ret;
426
881
}
427
428
/* Print explicit parameters */
429
static int ec_param_explicit_to_text(BIO *out, const EC_GROUP *group,
430
                                     OSSL_LIB_CTX *libctx)
431
881
{
432
881
    int ret = 0, tmp_nid;
433
881
    BN_CTX *ctx = NULL;
434
881
    const BIGNUM *order = NULL, *cofactor = NULL;
435
881
    const unsigned char *seed;
436
881
    size_t seed_len = 0;
437
438
881
    ctx = BN_CTX_new_ex(libctx);
439
881
    if (ctx == NULL)
440
0
        return 0;
441
881
    BN_CTX_start(ctx);
442
443
881
    tmp_nid = EC_GROUP_get_field_type(group);
444
881
    order = EC_GROUP_get0_order(group);
445
881
    if (order == NULL)
446
0
        goto err;
447
448
881
    seed = EC_GROUP_get0_seed(group);
449
881
    if (seed != NULL)
450
466
        seed_len = EC_GROUP_get_seed_len(group);
451
881
    cofactor = EC_GROUP_get0_cofactor(group);
452
453
    /* print the 'short name' of the field type */
454
881
    if (BIO_printf(out, "Field Type: %s\n", OBJ_nid2sn(tmp_nid)) <= 0
455
881
        || !ec_param_explicit_curve_to_text(out, group, ctx)
456
881
        || !ec_param_explicit_gen_to_text(out, group, ctx)
457
881
        || !print_labeled_bignum(out, "Order: ", order)
458
881
        || (cofactor != NULL
459
881
            && !print_labeled_bignum(out, "Cofactor: ", cofactor))
460
881
        || (seed != NULL
461
881
            && !print_labeled_buf(out, "Seed:", seed, seed_len)))
462
0
        goto err;
463
881
    ret = 1;
464
881
err:
465
881
    BN_CTX_end(ctx);
466
881
    BN_CTX_free(ctx);
467
881
    return ret;
468
881
}
469
470
static int ec_param_to_text(BIO *out, const EC_GROUP *group,
471
                            OSSL_LIB_CTX *libctx)
472
7.24k
{
473
7.24k
    if (EC_GROUP_get_asn1_flag(group) & OPENSSL_EC_NAMED_CURVE) {
474
6.36k
        const char *curve_name;
475
6.36k
        int curve_nid = EC_GROUP_get_curve_name(group);
476
477
        /* Explicit parameters */
478
6.36k
        if (curve_nid == NID_undef)
479
0
            return 0;
480
481
6.36k
        if (BIO_printf(out, "%s: %s\n", "ASN1 OID", OBJ_nid2sn(curve_nid)) <= 0)
482
0
            return 0;
483
484
6.36k
        curve_name = EC_curve_nid2nist(curve_nid);
485
6.36k
        return (curve_name == NULL
486
6.36k
                || BIO_printf(out, "%s: %s\n", "NIST CURVE", curve_name) > 0);
487
6.36k
    } else {
488
881
        return ec_param_explicit_to_text(out, group, libctx);
489
881
    }
490
7.24k
}
491
492
static int ec_to_text(BIO *out, const void *key, int selection)
493
2.11k
{
494
2.11k
    const EC_KEY *ec = key;
495
2.11k
    const char *type_label = NULL;
496
2.11k
    unsigned char *priv = NULL, *pub = NULL;
497
2.11k
    size_t priv_len = 0, pub_len = 0;
498
2.11k
    const EC_GROUP *group;
499
2.11k
    int ret = 0;
500
501
2.11k
    if (out == NULL || ec == NULL) {
502
0
        ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
503
0
        return 0;
504
0
    }
505
506
2.11k
    if ((group = EC_KEY_get0_group(ec)) == NULL) {
507
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
508
0
        return 0;
509
0
    }
510
511
2.11k
    if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
512
1.18k
        type_label = "Private-Key";
513
929
    else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
514
929
        type_label = "Public-Key";
515
0
    else if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
516
0
        type_label = "EC-Parameters";
517
518
2.11k
    if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
519
1.18k
        const BIGNUM *priv_key = EC_KEY_get0_private_key(ec);
520
521
1.18k
        if (priv_key == NULL) {
522
0
            ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PRIVATE_KEY);
523
0
            goto err;
524
0
        }
525
1.18k
        priv_len = EC_KEY_priv2buf(ec, &priv);
526
1.18k
        if (priv_len == 0)
527
427
            goto err;
528
1.18k
    }
529
1.68k
    if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
530
1.68k
        const EC_POINT *pub_pt = EC_KEY_get0_public_key(ec);
531
532
1.68k
        if (pub_pt == NULL) {
533
0
            ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY);
534
0
            goto err;
535
0
        }
536
537
1.68k
        pub_len = EC_KEY_key2buf(ec, EC_KEY_get_conv_form(ec), &pub, NULL);
538
1.68k
        if (pub_len == 0)
539
345
            goto err;
540
1.68k
    }
541
542
1.34k
    if (BIO_printf(out, "%s: (%d bit)\n", type_label,
543
1.34k
                   EC_GROUP_order_bits(group)) <= 0)
544
0
        goto err;
545
1.34k
    if (priv != NULL
546
1.34k
        && !print_labeled_buf(out, "priv:", priv, priv_len))
547
0
        goto err;
548
1.34k
    if (pub != NULL
549
1.34k
        && !print_labeled_buf(out, "pub:", pub, pub_len))
550
0
        goto err;
551
1.34k
    if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
552
1.34k
        ret = ec_param_to_text(out, group, ossl_ec_key_get_libctx(ec));
553
2.11k
err:
554
2.11k
    OPENSSL_clear_free(priv, priv_len);
555
2.11k
    OPENSSL_free(pub);
556
2.11k
    return ret;
557
1.34k
}
558
559
# define ec_input_type          "EC"
560
561
# ifndef OPENSSL_NO_SM2
562
#  define sm2_input_type        "SM2"
563
# endif
564
#endif
565
566
/* ---------------------------------------------------------------------- */
567
568
#ifndef OPENSSL_NO_EC
569
static int ecx_to_text(BIO *out, const void *key, int selection)
570
381
{
571
381
    const ECX_KEY *ecx = key;
572
381
    const char *type_label = NULL;
573
574
381
    if (out == NULL || ecx == NULL) {
575
0
        ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
576
0
        return 0;
577
0
    }
578
579
381
    switch (ecx->type) {
580
40
    case ECX_KEY_TYPE_X25519:
581
40
        type_label = "X25519";
582
40
        break;
583
45
    case ECX_KEY_TYPE_X448:
584
45
        type_label = "X448";
585
45
        break;
586
173
    case ECX_KEY_TYPE_ED25519:
587
173
        type_label = "ED25519";
588
173
        break;
589
123
    case ECX_KEY_TYPE_ED448:
590
123
        type_label = "ED448";
591
123
        break;
592
381
    }
593
594
381
    if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
595
48
        if (ecx->privkey == NULL) {
596
8
            ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PRIVATE_KEY);
597
8
            return 0;
598
8
        }
599
600
40
        if (BIO_printf(out, "%s Private-Key:\n", type_label) <= 0)
601
0
            return 0;
602
40
        if (!print_labeled_buf(out, "priv:", ecx->privkey, ecx->keylen))
603
0
            return 0;
604
333
    } else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
605
        /* ecx->pubkey is an array, not a pointer... */
606
311
        if (!ecx->haspubkey) {
607
0
            ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY);
608
0
            return 0;
609
0
        }
610
611
311
        if (BIO_printf(out, "%s Public-Key:\n", type_label) <= 0)
612
0
            return 0;
613
311
    }
614
615
373
    if (!print_labeled_buf(out, "pub:", ecx->pubkey, ecx->keylen))
616
0
        return 0;
617
618
373
    return 1;
619
373
}
620
621
# define ed25519_input_type     "ED25519"
622
# define ed448_input_type       "ED448"
623
# define x25519_input_type      "X25519"
624
# define x448_input_type        "X448"
625
#endif
626
627
/* ---------------------------------------------------------------------- */
628
629
static int rsa_to_text(BIO *out, const void *key, int selection)
630
9.14k
{
631
9.14k
    const RSA *rsa = key;
632
9.14k
    const char *type_label = "RSA key";
633
9.14k
    const char *modulus_label = NULL;
634
9.14k
    const char *exponent_label = NULL;
635
9.14k
    const BIGNUM *rsa_d = NULL, *rsa_n = NULL, *rsa_e = NULL;
636
9.14k
    STACK_OF(BIGNUM_const) *factors = NULL;
637
9.14k
    STACK_OF(BIGNUM_const) *exps = NULL;
638
9.14k
    STACK_OF(BIGNUM_const) *coeffs = NULL;
639
9.14k
    int primes;
640
9.14k
    const RSA_PSS_PARAMS_30 *pss_params = ossl_rsa_get0_pss_params_30((RSA *)rsa);
641
9.14k
    int ret = 0;
642
643
9.14k
    if (out == NULL || rsa == NULL) {
644
0
        ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
645
0
        goto err;
646
0
    }
647
648
9.14k
    factors = sk_BIGNUM_const_new_null();
649
9.14k
    exps = sk_BIGNUM_const_new_null();
650
9.14k
    coeffs = sk_BIGNUM_const_new_null();
651
652
9.14k
    if (factors == NULL || exps == NULL || coeffs == NULL) {
653
0
        ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
654
0
        goto err;
655
0
    }
656
657
9.14k
    if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
658
2.46k
        type_label = "Private-Key";
659
2.46k
        modulus_label = "modulus:";
660
2.46k
        exponent_label = "publicExponent:";
661
6.67k
    } else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
662
5.33k
        type_label = "Public-Key";
663
5.33k
        modulus_label = "Modulus:";
664
5.33k
        exponent_label = "Exponent:";
665
5.33k
    }
666
667
9.14k
    RSA_get0_key(rsa, &rsa_n, &rsa_e, &rsa_d);
668
9.14k
    ossl_rsa_get0_all_params((RSA *)rsa, factors, exps, coeffs);
669
9.14k
    primes = sk_BIGNUM_const_num(factors);
670
671
9.14k
    if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
672
2.46k
        if (BIO_printf(out, "%s: (%d bit, %d primes)\n",
673
2.46k
                       type_label, BN_num_bits(rsa_n), primes) <= 0)
674
0
            goto err;
675
6.67k
    } else {
676
6.67k
        if (BIO_printf(out, "%s: (%d bit)\n",
677
6.67k
                       type_label, BN_num_bits(rsa_n)) <= 0)
678
0
            goto err;
679
6.67k
    }
680
681
9.14k
    if (!print_labeled_bignum(out, modulus_label, rsa_n))
682
0
        goto err;
683
9.14k
    if (!print_labeled_bignum(out, exponent_label, rsa_e))
684
0
        goto err;
685
9.14k
    if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
686
2.46k
        int i;
687
688
2.46k
        if (!print_labeled_bignum(out, "privateExponent:", rsa_d))
689
629
            goto err;
690
1.83k
        if (!print_labeled_bignum(out, "prime1:",
691
1.83k
                                  sk_BIGNUM_const_value(factors, 0)))
692
0
            goto err;
693
1.83k
        if (!print_labeled_bignum(out, "prime2:",
694
1.83k
                                  sk_BIGNUM_const_value(factors, 1)))
695
0
            goto err;
696
1.83k
        if (!print_labeled_bignum(out, "exponent1:",
697
1.83k
                                  sk_BIGNUM_const_value(exps, 0)))
698
0
            goto err;
699
1.83k
        if (!print_labeled_bignum(out, "exponent2:",
700
1.83k
                                  sk_BIGNUM_const_value(exps, 1)))
701
0
            goto err;
702
1.83k
        if (!print_labeled_bignum(out, "coefficient:",
703
1.83k
                                  sk_BIGNUM_const_value(coeffs, 0)))
704
0
            goto err;
705
81.3k
        for (i = 2; i < sk_BIGNUM_const_num(factors); i++) {
706
79.5k
            if (BIO_printf(out, "prime%d:", i + 1) <= 0)
707
0
                goto err;
708
79.5k
            if (!print_labeled_bignum(out, NULL,
709
79.5k
                                      sk_BIGNUM_const_value(factors, i)))
710
0
                goto err;
711
79.5k
            if (BIO_printf(out, "exponent%d:", i + 1) <= 0)
712
0
                goto err;
713
79.5k
            if (!print_labeled_bignum(out, NULL,
714
79.5k
                                      sk_BIGNUM_const_value(exps, i)))
715
0
                goto err;
716
79.5k
            if (BIO_printf(out, "coefficient%d:", i + 1) <= 0)
717
0
                goto err;
718
79.5k
            if (!print_labeled_bignum(out, NULL,
719
79.5k
                                      sk_BIGNUM_const_value(coeffs, i - 1)))
720
0
                goto err;
721
79.5k
        }
722
1.83k
    }
723
724
8.51k
    if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0) {
725
8.51k
        switch (RSA_test_flags(rsa, RSA_FLAG_TYPE_MASK)) {
726
8.39k
        case RSA_FLAG_TYPE_RSA:
727
8.39k
            if (!ossl_rsa_pss_params_30_is_unrestricted(pss_params)) {
728
0
                if (BIO_printf(out, "(INVALID PSS PARAMETERS)\n") <= 0)
729
0
                    goto err;
730
0
            }
731
8.39k
            break;
732
8.39k
        case RSA_FLAG_TYPE_RSASSAPSS:
733
114
            if (ossl_rsa_pss_params_30_is_unrestricted(pss_params)) {
734
23
                if (BIO_printf(out, "No PSS parameter restrictions\n") <= 0)
735
0
                    goto err;
736
91
            } else {
737
91
                int hashalg_nid = ossl_rsa_pss_params_30_hashalg(pss_params);
738
91
                int maskgenalg_nid =
739
91
                    ossl_rsa_pss_params_30_maskgenalg(pss_params);
740
91
                int maskgenhashalg_nid =
741
91
                    ossl_rsa_pss_params_30_maskgenhashalg(pss_params);
742
91
                int saltlen = ossl_rsa_pss_params_30_saltlen(pss_params);
743
91
                int trailerfield =
744
91
                    ossl_rsa_pss_params_30_trailerfield(pss_params);
745
746
91
                if (BIO_printf(out, "PSS parameter restrictions:\n") <= 0)
747
0
                    goto err;
748
91
                if (BIO_printf(out, "  Hash Algorithm: %s%s\n",
749
91
                               ossl_rsa_oaeppss_nid2name(hashalg_nid),
750
91
                               (hashalg_nid == NID_sha1
751
91
                                ? " (default)" : "")) <= 0)
752
0
                    goto err;
753
91
                if (BIO_printf(out, "  Mask Algorithm: %s with %s%s\n",
754
91
                               ossl_rsa_mgf_nid2name(maskgenalg_nid),
755
91
                               ossl_rsa_oaeppss_nid2name(maskgenhashalg_nid),
756
91
                               (maskgenalg_nid == NID_mgf1
757
91
                                && maskgenhashalg_nid == NID_sha1
758
91
                                ? " (default)" : "")) <= 0)
759
0
                    goto err;
760
91
                if (BIO_printf(out, "  Minimum Salt Length: %d%s\n",
761
91
                               saltlen,
762
91
                               (saltlen == 20 ? " (default)" : "")) <= 0)
763
0
                    goto err;
764
91
                if (BIO_printf(out, "  Trailer Field: 0x%x%s\n",
765
91
                               trailerfield,
766
91
                               (trailerfield == 1 ? " (default)" : "")) <= 0)
767
0
                    goto err;
768
91
            }
769
114
            break;
770
8.51k
        }
771
8.51k
    }
772
773
8.51k
    ret = 1;
774
9.14k
 err:
775
9.14k
    sk_BIGNUM_const_free(factors);
776
9.14k
    sk_BIGNUM_const_free(exps);
777
9.14k
    sk_BIGNUM_const_free(coeffs);
778
9.14k
    return ret;
779
8.51k
}
780
781
#define rsa_input_type          "RSA"
782
#define rsapss_input_type       "RSA-PSS"
783
784
/* ---------------------------------------------------------------------- */
785
786
static void *key2text_newctx(void *provctx)
787
43.7k
{
788
43.7k
    return provctx;
789
43.7k
}
790
791
static void key2text_freectx(ossl_unused void *vctx)
792
43.7k
{
793
43.7k
}
794
795
static int key2text_encode(void *vctx, const void *key, int selection,
796
                           OSSL_CORE_BIO *cout,
797
                           int (*key2text)(BIO *out, const void *key,
798
                                           int selection),
799
                           OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
800
37.0k
{
801
37.0k
    BIO *out = ossl_bio_new_from_core_bio(vctx, cout);
802
37.0k
    int ret;
803
804
37.0k
    if (out == NULL)
805
0
        return 0;
806
807
37.0k
    ret = key2text(out, key, selection);
808
37.0k
    BIO_free(out);
809
810
37.0k
    return ret;
811
37.0k
}
812
813
#define MAKE_TEXT_ENCODER(impl, type)                                   \
814
    static OSSL_FUNC_encoder_import_object_fn                           \
815
    impl##2text_import_object;                                          \
816
    static OSSL_FUNC_encoder_free_object_fn                             \
817
    impl##2text_free_object;                                            \
818
    static OSSL_FUNC_encoder_encode_fn impl##2text_encode;              \
819
                                                                        \
820
    static void *impl##2text_import_object(void *ctx, int selection,    \
821
                                           const OSSL_PARAM params[])   \
822
0
    {                                                                   \
823
0
        return ossl_prov_import_key(ossl_##impl##_keymgmt_functions,    \
824
0
                                    ctx, selection, params);            \
825
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:rsa2text_import_object
Unexecuted instantiation: encode_key2text.c:rsapss2text_import_object
826
    static void impl##2text_free_object(void *key)                      \
827
0
    {                                                                   \
828
0
        ossl_prov_free_key(ossl_##impl##_keymgmt_functions, key);       \
829
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:rsa2text_free_object
Unexecuted instantiation: encode_key2text.c:rsapss2text_free_object
830
    static int impl##2text_encode(void *vctx, OSSL_CORE_BIO *cout,      \
831
                                  const void *key,                      \
832
                                  const OSSL_PARAM key_abstract[],      \
833
                                  int selection,                        \
834
                                  OSSL_PASSPHRASE_CALLBACK *cb,         \
835
                                  void *cbarg)                          \
836
36.9k
    {                                                                   \
837
36.9k
        /* We don't deal with abstract objects */                       \
838
36.9k
        if (key_abstract != NULL) {                                     \
839
0
            ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);     \
840
0
            return 0;                                                   \
841
0
        }                                                               \
842
36.9k
        return key2text_encode(vctx, key, selection, cout,              \
843
36.9k
                               type##_to_text, cb, cbarg);              \
844
36.9k
    }                                                                   \
encode_key2text.c:dh2text_encode
Line
Count
Source
836
4.84k
    {                                                                   \
837
4.84k
        /* We don't deal with abstract objects */                       \
838
4.84k
        if (key_abstract != NULL) {                                     \
839
0
            ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);     \
840
0
            return 0;                                                   \
841
0
        }                                                               \
842
4.84k
        return key2text_encode(vctx, key, selection, cout,              \
843
4.84k
                               type##_to_text, cb, cbarg);              \
844
4.84k
    }                                                                   \
encode_key2text.c:dhx2text_encode
Line
Count
Source
836
8.31k
    {                                                                   \
837
8.31k
        /* We don't deal with abstract objects */                       \
838
8.31k
        if (key_abstract != NULL) {                                     \
839
0
            ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);     \
840
0
            return 0;                                                   \
841
0
        }                                                               \
842
8.31k
        return key2text_encode(vctx, key, selection, cout,              \
843
8.31k
                               type##_to_text, cb, cbarg);              \
844
8.31k
    }                                                                   \
encode_key2text.c:dsa2text_encode
Line
Count
Source
836
4.66k
    {                                                                   \
837
4.66k
        /* We don't deal with abstract objects */                       \
838
4.66k
        if (key_abstract != NULL) {                                     \
839
0
            ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);     \
840
0
            return 0;                                                   \
841
0
        }                                                               \
842
4.66k
        return key2text_encode(vctx, key, selection, cout,              \
843
4.66k
                               type##_to_text, cb, cbarg);              \
844
4.66k
    }                                                                   \
encode_key2text.c:ec2text_encode
Line
Count
Source
836
9.52k
    {                                                                   \
837
9.52k
        /* We don't deal with abstract objects */                       \
838
9.52k
        if (key_abstract != NULL) {                                     \
839
0
            ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);     \
840
0
            return 0;                                                   \
841
0
        }                                                               \
842
9.52k
        return key2text_encode(vctx, key, selection, cout,              \
843
9.52k
                               type##_to_text, cb, cbarg);              \
844
9.52k
    }                                                                   \
encode_key2text.c:sm22text_encode
Line
Count
Source
836
91
    {                                                                   \
837
91
        /* We don't deal with abstract objects */                       \
838
91
        if (key_abstract != NULL) {                                     \
839
0
            ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);     \
840
0
            return 0;                                                   \
841
0
        }                                                               \
842
91
        return key2text_encode(vctx, key, selection, cout,              \
843
91
                               type##_to_text, cb, cbarg);              \
844
91
    }                                                                   \
encode_key2text.c:ed255192text_encode
Line
Count
Source
836
173
    {                                                                   \
837
173
        /* We don't deal with abstract objects */                       \
838
173
        if (key_abstract != NULL) {                                     \
839
0
            ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);     \
840
0
            return 0;                                                   \
841
0
        }                                                               \
842
173
        return key2text_encode(vctx, key, selection, cout,              \
843
173
                               type##_to_text, cb, cbarg);              \
844
173
    }                                                                   \
encode_key2text.c:ed4482text_encode
Line
Count
Source
836
123
    {                                                                   \
837
123
        /* We don't deal with abstract objects */                       \
838
123
        if (key_abstract != NULL) {                                     \
839
0
            ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);     \
840
0
            return 0;                                                   \
841
0
        }                                                               \
842
123
        return key2text_encode(vctx, key, selection, cout,              \
843
123
                               type##_to_text, cb, cbarg);              \
844
123
    }                                                                   \
encode_key2text.c:x255192text_encode
Line
Count
Source
836
40
    {                                                                   \
837
40
        /* We don't deal with abstract objects */                       \
838
40
        if (key_abstract != NULL) {                                     \
839
0
            ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);     \
840
0
            return 0;                                                   \
841
0
        }                                                               \
842
40
        return key2text_encode(vctx, key, selection, cout,              \
843
40
                               type##_to_text, cb, cbarg);              \
844
40
    }                                                                   \
encode_key2text.c:x4482text_encode
Line
Count
Source
836
45
    {                                                                   \
837
45
        /* We don't deal with abstract objects */                       \
838
45
        if (key_abstract != NULL) {                                     \
839
0
            ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);     \
840
0
            return 0;                                                   \
841
0
        }                                                               \
842
45
        return key2text_encode(vctx, key, selection, cout,              \
843
45
                               type##_to_text, cb, cbarg);              \
844
45
    }                                                                   \
encode_key2text.c:rsa2text_encode
Line
Count
Source
836
9.02k
    {                                                                   \
837
9.02k
        /* We don't deal with abstract objects */                       \
838
9.02k
        if (key_abstract != NULL) {                                     \
839
0
            ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);     \
840
0
            return 0;                                                   \
841
0
        }                                                               \
842
9.02k
        return key2text_encode(vctx, key, selection, cout,              \
843
9.02k
                               type##_to_text, cb, cbarg);              \
844
9.02k
    }                                                                   \
encode_key2text.c:rsapss2text_encode
Line
Count
Source
836
119
    {                                                                   \
837
119
        /* We don't deal with abstract objects */                       \
838
119
        if (key_abstract != NULL) {                                     \
839
0
            ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);     \
840
0
            return 0;                                                   \
841
0
        }                                                               \
842
119
        return key2text_encode(vctx, key, selection, cout,              \
843
119
                               type##_to_text, cb, cbarg);              \
844
119
    }                                                                   \
845
    const OSSL_DISPATCH ossl_##impl##_to_text_encoder_functions[] = {   \
846
        { OSSL_FUNC_ENCODER_NEWCTX,                                     \
847
          (void (*)(void))key2text_newctx },                            \
848
        { OSSL_FUNC_ENCODER_FREECTX,                                    \
849
          (void (*)(void))key2text_freectx },                           \
850
        { OSSL_FUNC_ENCODER_IMPORT_OBJECT,                              \
851
          (void (*)(void))impl##2text_import_object },                  \
852
        { OSSL_FUNC_ENCODER_FREE_OBJECT,                                \
853
          (void (*)(void))impl##2text_free_object },                    \
854
        { OSSL_FUNC_ENCODER_ENCODE,                                     \
855
          (void (*)(void))impl##2text_encode },                         \
856
        { 0, NULL }                                                     \
857
    }
858
859
#ifndef OPENSSL_NO_DH
860
MAKE_TEXT_ENCODER(dh, dh);
861
MAKE_TEXT_ENCODER(dhx, dh);
862
#endif
863
#ifndef OPENSSL_NO_DSA
864
MAKE_TEXT_ENCODER(dsa, dsa);
865
#endif
866
#ifndef OPENSSL_NO_EC
867
MAKE_TEXT_ENCODER(ec, ec);
868
# ifndef OPENSSL_NO_SM2
869
MAKE_TEXT_ENCODER(sm2, ec);
870
# endif
871
MAKE_TEXT_ENCODER(ed25519, ecx);
872
MAKE_TEXT_ENCODER(ed448, ecx);
873
MAKE_TEXT_ENCODER(x25519, ecx);
874
MAKE_TEXT_ENCODER(x448, ecx);
875
#endif
876
MAKE_TEXT_ENCODER(rsa, rsa);
877
MAKE_TEXT_ENCODER(rsapss, rsa);