Coverage Report

Created: 2024-07-27 06:39

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