Coverage Report

Created: 2026-10-28 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl41/crypto/evp/p_lib.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2026 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
 * DSA low level APIs are deprecated for public use, but still ok for
12
 * internal use.
13
 */
14
#include "internal/deprecated.h"
15
16
#include <assert.h>
17
#include <stdio.h>
18
#include "internal/cryptlib.h"
19
#include "internal/refcount.h"
20
#include "internal/namemap.h"
21
#include <openssl/bn.h>
22
#include <openssl/err.h>
23
#include <openssl/objects.h>
24
#include <openssl/evp.h>
25
#include <openssl/rsa.h>
26
#include <openssl/dsa.h>
27
#include <openssl/dh.h>
28
#include <openssl/ec.h>
29
#include <openssl/cmac.h>
30
#include <openssl/params.h>
31
#include <openssl/param_build.h>
32
#include <openssl/encoder.h>
33
#include <openssl/core_names.h>
34
#include <openssl/provider.h>
35
36
#include "internal/numbers.h" /* includes SIZE_MAX */
37
#include "internal/ffc.h"
38
#include "crypto/evp.h"
39
#include "crypto/dh.h"
40
#include "crypto/dsa.h"
41
#include "crypto/ec.h"
42
#include "crypto/ecx.h"
43
#include "crypto/rsa.h"
44
#ifndef FIPS_MODULE
45
#include "crypto/asn1.h"
46
#include "crypto/x509.h"
47
#endif
48
#include "internal/provider.h"
49
#include "internal/common.h"
50
#include "evp_local.h"
51
52
static int pkey_set_type(EVP_PKEY *pkey, int type, const char *str,
53
    int len, EVP_KEYMGMT *keymgmt);
54
static void evp_pkey_free_it(EVP_PKEY *key);
55
56
/* The type of parameters selected in key parameter functions */
57
622k
#define SELECT_PARAMETERS OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
58
59
#ifndef FIPS_MODULE
60
int EVP_PKEY_get_bits(const EVP_PKEY *pkey)
61
29.2k
{
62
29.2k
    int size = 0;
63
64
29.2k
    if (pkey != NULL) {
65
29.2k
        size = pkey->cache.bits;
66
29.2k
        if (pkey->ameth != NULL && pkey->ameth->pkey_bits != NULL)
67
0
            size = pkey->ameth->pkey_bits(pkey);
68
29.2k
    }
69
29.2k
    if (size <= 0) {
70
876
        ERR_raise(ERR_LIB_EVP, EVP_R_UNKNOWN_BITS);
71
876
        return 0;
72
876
    }
73
28.4k
    return size;
74
29.2k
}
75
76
int EVP_PKEY_get_security_bits(const EVP_PKEY *pkey)
77
205k
{
78
205k
    int size = 0;
79
80
205k
    if (pkey != NULL) {
81
205k
        size = pkey->cache.security_bits;
82
205k
        if (pkey->ameth != NULL && pkey->ameth->pkey_security_bits != NULL)
83
0
            size = pkey->ameth->pkey_security_bits(pkey);
84
205k
    }
85
205k
    if (size <= 0) {
86
91
        ERR_raise(ERR_LIB_EVP, EVP_R_UNKNOWN_SECURITY_BITS);
87
91
        return 0;
88
91
    }
89
204k
    return size;
90
205k
}
91
92
int EVP_PKEY_get_security_category(const EVP_PKEY *pkey)
93
0
{
94
0
    return pkey != NULL ? pkey->cache.security_category : -1;
95
0
}
96
97
int EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode)
98
0
{
99
0
#ifndef OPENSSL_NO_DSA
100
0
    if (pkey->type == EVP_PKEY_DSA) {
101
0
        int ret = pkey->save_parameters;
102
103
0
        if (mode >= 0)
104
0
            pkey->save_parameters = mode;
105
0
        return ret;
106
0
    }
107
0
#endif
108
0
#ifndef OPENSSL_NO_EC
109
0
    if (pkey->type == EVP_PKEY_EC) {
110
0
        int ret = pkey->save_parameters;
111
112
0
        if (mode >= 0)
113
0
            pkey->save_parameters = mode;
114
0
        return ret;
115
0
    }
116
0
#endif
117
0
    return 0;
118
0
}
119
120
int EVP_PKEY_set_ex_data(EVP_PKEY *key, int idx, void *arg)
121
0
{
122
0
    return CRYPTO_set_ex_data(&key->ex_data, idx, arg);
123
0
}
124
125
void *EVP_PKEY_get_ex_data(const EVP_PKEY *key, int idx)
126
0
{
127
0
    return CRYPTO_get_ex_data(&key->ex_data, idx);
128
0
}
129
#endif /* !FIPS_MODULE */
130
131
int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
132
188k
{
133
    /*
134
     * Clean up legacy stuff from this function when legacy support is gone.
135
     */
136
137
188k
    EVP_PKEY *downgraded_from = NULL;
138
188k
    int ok = 0;
139
140
188k
#ifndef FIPS_MODULE
141
    /*
142
     * If |to| is a legacy key and |from| isn't, we must make a downgraded
143
     * copy of |from|.  If that fails, this function fails.
144
     */
145
188k
    if (evp_pkey_is_legacy(to) && evp_pkey_is_provided(from)) {
146
0
        if (!evp_pkey_copy_downgraded(&downgraded_from, from))
147
0
            goto end;
148
0
        from = downgraded_from;
149
0
    }
150
188k
#endif /* !FIPS_MODULE */
151
152
    /*
153
     * Make sure |to| is typed.  Content is less important at this early
154
     * stage.
155
     *
156
     * 1.  If |to| is untyped, assign |from|'s key type to it.
157
     * 2.  If |to| contains a legacy key, compare its |type| to |from|'s.
158
     *     (|from| was already downgraded above)
159
     *
160
     * If |to| is a provided key, there's nothing more to do here, functions
161
     * like evp_keymgmt_util_copy() and evp_pkey_export_to_provider() called
162
     * further down help us find out if they are the same or not.
163
     */
164
188k
    if (evp_pkey_is_blank(to)) {
165
35.0k
#ifndef FIPS_MODULE
166
35.0k
        if (evp_pkey_is_legacy(from)) {
167
0
            if (EVP_PKEY_set_type(to, from->type) == 0)
168
0
                goto end;
169
0
        } else
170
35.0k
#endif /* !FIPS_MODULE */
171
35.0k
        {
172
35.0k
            if (EVP_PKEY_set_type_by_keymgmt(to, from->keymgmt) == 0)
173
0
                goto end;
174
35.0k
        }
175
35.0k
    }
176
153k
#ifndef FIPS_MODULE
177
153k
    else if (evp_pkey_is_legacy(to)) {
178
0
        if (to->type != from->type) {
179
0
            ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_KEY_TYPES);
180
0
            goto end;
181
0
        }
182
0
    }
183
188k
#endif /* !FIPS_MODULE */
184
185
188k
    if (EVP_PKEY_missing_parameters(from)) {
186
0
        ERR_raise(ERR_LIB_EVP, EVP_R_MISSING_PARAMETERS);
187
0
        goto end;
188
0
    }
189
190
188k
    if (!EVP_PKEY_missing_parameters(to)) {
191
153k
        if (EVP_PKEY_parameters_eq(to, from) == 1)
192
153k
            ok = 1;
193
0
        else
194
153k
            ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_PARAMETERS);
195
153k
        goto end;
196
153k
    }
197
198
    /* For purely provided keys, we just call the keymgmt utility */
199
35.0k
    if (to->keymgmt != NULL && from->keymgmt != NULL) {
200
35.0k
        ok = evp_keymgmt_util_copy(to, (EVP_PKEY *)from, SELECT_PARAMETERS);
201
35.0k
        goto end;
202
35.0k
    }
203
204
0
#ifndef FIPS_MODULE
205
    /*
206
     * If |to| is provided, we know that |from| is legacy at this point.
207
     * Try exporting |from| to |to|'s keymgmt, then use evp_keymgmt_dup()
208
     * to copy the appropriate data to |to|'s keydata.
209
     * We cannot override existing data so do it only if there is no keydata
210
     * in |to| yet.
211
     */
212
0
    if (to->keymgmt != NULL && to->keydata == NULL) {
213
0
        EVP_KEYMGMT *to_keymgmt = to->keymgmt;
214
0
        void *from_keydata = evp_pkey_export_to_provider((EVP_PKEY *)from, NULL, &to_keymgmt,
215
0
            NULL);
216
217
        /*
218
         * If we get a NULL, it could be an internal error, or it could be
219
         * that there's a key mismatch.  We're pretending the latter...
220
         */
221
0
        if (from_keydata == NULL)
222
0
            ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_KEY_TYPES);
223
0
        else
224
0
            ok = (to->keydata = evp_keymgmt_dup(to->keymgmt,
225
0
                      from_keydata,
226
0
                      SELECT_PARAMETERS))
227
0
                != NULL;
228
0
        goto end;
229
0
    }
230
231
    /* Both keys are legacy */
232
0
    if (from->ameth != NULL && from->ameth->param_copy != NULL)
233
0
        ok = from->ameth->param_copy(to, from);
234
0
#endif /* !FIPS_MODULE */
235
188k
end:
236
188k
    EVP_PKEY_free(downgraded_from);
237
188k
    return ok;
238
0
}
239
240
int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey)
241
339k
{
242
339k
    if (pkey != NULL) {
243
#ifdef FIPS_MODULE
244
        return !evp_keymgmt_util_has((EVP_PKEY *)pkey, SELECT_PARAMETERS);
245
#else
246
339k
        if (pkey->keymgmt != NULL)
247
257k
            return !evp_keymgmt_util_has((EVP_PKEY *)pkey, SELECT_PARAMETERS);
248
81.8k
        if (pkey->ameth != NULL && pkey->ameth->param_missing != NULL)
249
48.6k
            return pkey->ameth->param_missing(pkey);
250
81.8k
#endif /* FIPS_MODULE */
251
81.8k
    }
252
33.2k
    return 0;
253
339k
}
254
255
/*
256
 * This function is called for any mixture of keys except pure legacy pair.
257
 * When legacy keys are gone, we replace a call to this functions with
258
 * a call to evp_keymgmt_util_match().
259
 */
260
static int evp_pkey_cmp_any(const EVP_PKEY *a, const EVP_PKEY *b,
261
    int selection)
262
329k
{
263
#ifdef FIPS_MODULE
264
    return evp_keymgmt_util_match((EVP_PKEY *)a, (EVP_PKEY *)b, selection);
265
#else
266
329k
    EVP_KEYMGMT *keymgmt1 = NULL, *keymgmt2 = NULL;
267
329k
    void *keydata1 = NULL, *keydata2 = NULL, *tmp_keydata = NULL;
268
269
    /* If none of them are provided, this function shouldn't have been called */
270
329k
    if (!ossl_assert(evp_pkey_is_provided(a) || evp_pkey_is_provided(b)))
271
0
        return -2;
272
273
    /* For purely provided keys, we just call the keymgmt utility */
274
329k
    if (evp_pkey_is_provided(a) && evp_pkey_is_provided(b))
275
110k
        return evp_keymgmt_util_match((EVP_PKEY *)a, (EVP_PKEY *)b, selection);
276
277
    /*
278
     * At this point, one of them is provided, the other not.  This allows
279
     * us to compare types using legacy NIDs.
280
     */
281
219k
    if (evp_pkey_is_legacy(a)
282
0
        && !EVP_KEYMGMT_is_a(b->keymgmt, OBJ_nid2sn(a->type)))
283
0
        return -1; /* not the same key type */
284
219k
    if (evp_pkey_is_legacy(b)
285
219k
        && !EVP_KEYMGMT_is_a(a->keymgmt, OBJ_nid2sn(b->type)))
286
0
        return -1; /* not the same key type */
287
288
    /*
289
     * We've determined that they both are the same keytype, so the next
290
     * step is to do a bit of cross export to ensure we have keydata for
291
     * both keys in the same keymgmt.
292
     */
293
219k
    keymgmt1 = a->keymgmt;
294
219k
    keydata1 = a->keydata;
295
219k
    keymgmt2 = b->keymgmt;
296
219k
    keydata2 = b->keydata;
297
298
219k
    if (keymgmt2 != NULL && keymgmt2->match != NULL) {
299
0
        tmp_keydata = evp_pkey_export_to_provider((EVP_PKEY *)a, NULL, &keymgmt2, NULL);
300
0
        if (tmp_keydata != NULL) {
301
0
            keymgmt1 = keymgmt2;
302
0
            keydata1 = tmp_keydata;
303
0
        }
304
0
    }
305
219k
    if (tmp_keydata == NULL && keymgmt1 != NULL && keymgmt1->match != NULL) {
306
219k
        tmp_keydata = evp_pkey_export_to_provider((EVP_PKEY *)b, NULL, &keymgmt1, NULL);
307
219k
        if (tmp_keydata != NULL) {
308
219k
            keymgmt2 = keymgmt1;
309
219k
            keydata2 = tmp_keydata;
310
219k
        }
311
219k
    }
312
313
    /* If we still don't have matching keymgmt implementations, we give up */
314
219k
    if (keymgmt1 != keymgmt2)
315
0
        return -2;
316
317
    /* If the keymgmt implementations are NULL, the export failed */
318
219k
    if (keymgmt1 == NULL)
319
0
        return -2;
320
321
219k
    return evp_keymgmt_match(keymgmt1, keydata1, keydata2, selection);
322
219k
#endif /* FIPS_MODULE */
323
219k
}
324
325
#ifndef FIPS_MODULE
326
#ifndef OPENSSL_NO_DEPRECATED_3_0
327
int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
328
0
{
329
0
    return EVP_PKEY_parameters_eq(a, b);
330
0
}
331
#endif
332
#endif /* FIPS_MODULE */
333
334
int EVP_PKEY_parameters_eq(const EVP_PKEY *a, const EVP_PKEY *b)
335
153k
{
336
#ifdef FIPS_MODULE
337
    return evp_pkey_cmp_any(a, b, SELECT_PARAMETERS);
338
#else
339
    /*
340
     * This will just call evp_keymgmt_util_match when legacy support
341
     * is gone.
342
     */
343
344
153k
    if (a->keymgmt != NULL || b->keymgmt != NULL)
345
153k
        return evp_pkey_cmp_any(a, b, SELECT_PARAMETERS);
346
347
    /* All legacy keys */
348
0
    if (a->type != b->type)
349
0
        return -1;
350
0
    if (a->ameth != NULL && a->ameth->param_cmp != NULL)
351
0
        return a->ameth->param_cmp(a, b);
352
0
    return -2;
353
0
#endif /* !FIPS_MODULE */
354
0
}
355
356
#ifndef FIPS_MODULE
357
#ifndef OPENSSL_NO_DEPRECATED_3_0
358
int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
359
0
{
360
0
    return EVP_PKEY_eq(a, b);
361
0
}
362
#endif
363
#endif /* !FIPS_MODULE */
364
365
int EVP_PKEY_eq(const EVP_PKEY *a, const EVP_PKEY *b)
366
176k
{
367
    /*
368
     * This will just call evp_keymgmt_util_match when legacy support
369
     * is gone.
370
     */
371
372
    /* Trivial shortcuts */
373
176k
    if (a == b)
374
158
        return 1;
375
176k
    if (a == NULL || b == NULL)
376
0
        return 0;
377
378
176k
#ifndef FIPS_MODULE
379
176k
    if (a->keymgmt != NULL || b->keymgmt != NULL)
380
176k
#endif /* !FIPS_MODULE */
381
176k
    {
382
176k
        int selection = SELECT_PARAMETERS;
383
384
176k
        if (evp_keymgmt_util_has((EVP_PKEY *)a, OSSL_KEYMGMT_SELECT_PUBLIC_KEY)
385
169k
            && evp_keymgmt_util_has((EVP_PKEY *)b, OSSL_KEYMGMT_SELECT_PUBLIC_KEY))
386
59.8k
            selection |= OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
387
116k
        else
388
116k
            selection |= OSSL_KEYMGMT_SELECT_KEYPAIR;
389
176k
        return evp_pkey_cmp_any(a, b, selection);
390
176k
    }
391
392
0
#ifndef FIPS_MODULE
393
    /* All legacy keys */
394
0
    if (a->type != b->type)
395
0
        return -1;
396
397
0
    if (a->ameth != NULL) {
398
0
        int ret;
399
        /* Compare parameters if the algorithm has them */
400
0
        if (a->ameth->param_cmp != NULL) {
401
0
            ret = a->ameth->param_cmp(a, b);
402
0
            if (ret <= 0)
403
0
                return ret;
404
0
        }
405
406
0
        if (a->ameth->pub_cmp != NULL)
407
0
            return a->ameth->pub_cmp(a, b);
408
0
    }
409
410
0
    return -2;
411
0
#endif /* !FIPS_MODULE */
412
0
}
413
414
#ifndef FIPS_MODULE
415
static EVP_PKEY *new_raw_key_int(OSSL_LIB_CTX *libctx,
416
    const char *strtype,
417
    const char *propq,
418
    int nidtype,
419
    const unsigned char *key,
420
    size_t len,
421
    int key_is_priv)
422
12.2k
{
423
12.2k
    EVP_PKEY *pkey = NULL;
424
12.2k
    EVP_PKEY_CTX *ctx = NULL;
425
12.2k
    int result = 0;
426
427
12.2k
    ctx = EVP_PKEY_CTX_new_from_name(libctx,
428
12.2k
        strtype != NULL ? strtype
429
12.2k
                        : OBJ_nid2sn(nidtype),
430
12.2k
        propq);
431
12.2k
    if (ctx == NULL)
432
16
        goto err;
433
    /* May fail if no provider available */
434
12.2k
    ERR_set_mark();
435
12.2k
    if (EVP_PKEY_fromdata_init(ctx) == 1) {
436
12.2k
        OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
437
438
12.2k
        ERR_clear_last_mark();
439
12.2k
        params[0] = OSSL_PARAM_construct_octet_string(
440
12.2k
            key_is_priv ? OSSL_PKEY_PARAM_PRIV_KEY
441
12.2k
                        : OSSL_PKEY_PARAM_PUB_KEY,
442
12.2k
            (void *)key, len);
443
444
12.2k
        if (EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEYPAIR, params) != 1) {
445
258
            ERR_raise(ERR_LIB_EVP, EVP_R_KEY_SETUP_FAILED);
446
258
            goto err;
447
258
        }
448
449
11.9k
        EVP_PKEY_CTX_free(ctx);
450
451
11.9k
        return pkey;
452
12.2k
    }
453
0
    ERR_pop_to_mark();
454
    /* else not supported so fallback to legacy */
455
456
    /* Legacy code path */
457
458
0
    pkey = EVP_PKEY_new();
459
0
    if (pkey == NULL) {
460
0
        ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
461
0
        goto err;
462
0
    }
463
464
0
    if (!pkey_set_type(pkey, nidtype, strtype, -1, NULL)) {
465
        /* ERR_raise(ERR_LIB_EVP, ...) already called */
466
0
        goto err;
467
0
    }
468
469
0
    if (!ossl_assert(pkey->ameth != NULL))
470
0
        goto err;
471
472
0
    if (key_is_priv) {
473
0
        if (pkey->ameth->set_priv_key == NULL) {
474
0
            ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
475
0
            goto err;
476
0
        }
477
478
0
        if (!pkey->ameth->set_priv_key(pkey, key, len)) {
479
0
            ERR_raise(ERR_LIB_EVP, EVP_R_KEY_SETUP_FAILED);
480
0
            goto err;
481
0
        }
482
0
    } else {
483
0
        if (pkey->ameth->set_pub_key == NULL) {
484
0
            ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
485
0
            goto err;
486
0
        }
487
488
0
        if (!pkey->ameth->set_pub_key(pkey, key, len)) {
489
0
            ERR_raise(ERR_LIB_EVP, EVP_R_KEY_SETUP_FAILED);
490
0
            goto err;
491
0
        }
492
0
    }
493
494
0
    result = 1;
495
274
err:
496
274
    if (!result) {
497
274
        EVP_PKEY_free(pkey);
498
274
        pkey = NULL;
499
274
    }
500
274
    EVP_PKEY_CTX_free(ctx);
501
274
    return pkey;
502
0
}
503
504
EVP_PKEY *EVP_PKEY_new_raw_private_key_ex(OSSL_LIB_CTX *libctx,
505
    const char *keytype,
506
    const char *propq,
507
    const unsigned char *priv, size_t len)
508
26.3k
{
509
26.3k
    return new_raw_key_int(libctx, keytype, propq, EVP_PKEY_NONE, priv,
510
26.3k
        len, 1);
511
26.3k
}
512
513
EVP_PKEY *EVP_PKEY_new_raw_private_key(int type, ENGINE *e,
514
    const unsigned char *priv,
515
    size_t len)
516
0
{
517
0
    if (!ossl_assert(e == NULL))
518
0
        return NULL;
519
0
    return new_raw_key_int(NULL, NULL, NULL, type, priv, len, 1);
520
0
}
521
522
EVP_PKEY *EVP_PKEY_new_raw_public_key_ex(OSSL_LIB_CTX *libctx,
523
    const char *keytype, const char *propq,
524
    const unsigned char *pub, size_t len)
525
240
{
526
240
    return new_raw_key_int(libctx, keytype, propq, EVP_PKEY_NONE, pub,
527
240
        len, 0);
528
240
}
529
530
EVP_PKEY *EVP_PKEY_new_raw_public_key(int type, ENGINE *e,
531
    const unsigned char *pub,
532
    size_t len)
533
0
{
534
0
    if (!ossl_assert(e == NULL))
535
0
        return NULL;
536
0
    return new_raw_key_int(NULL, NULL, NULL, type, pub, len, 0);
537
0
}
538
539
struct raw_key_details_st {
540
    unsigned char **key;
541
    size_t *len;
542
    int selection;
543
};
544
545
static OSSL_CALLBACK get_raw_key_details;
546
static int get_raw_key_details(const OSSL_PARAM params[], void *arg)
547
0
{
548
0
    const OSSL_PARAM *p = NULL;
549
0
    struct raw_key_details_st *raw_key = arg;
550
551
0
    if (raw_key->selection == OSSL_KEYMGMT_SELECT_PRIVATE_KEY) {
552
0
        if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY))
553
0
            != NULL)
554
0
            return OSSL_PARAM_get_octet_string(p, (void **)raw_key->key,
555
0
                raw_key->key == NULL ? 0 : *raw_key->len,
556
0
                raw_key->len);
557
0
    } else if (raw_key->selection == OSSL_KEYMGMT_SELECT_PUBLIC_KEY) {
558
0
        if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY))
559
0
            != NULL)
560
0
            return OSSL_PARAM_get_octet_string(p, (void **)raw_key->key,
561
0
                raw_key->key == NULL ? 0 : *raw_key->len,
562
0
                raw_key->len);
563
0
    }
564
565
0
    return 0;
566
0
}
567
568
int EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey, unsigned char *priv,
569
    size_t *len)
570
0
{
571
0
    if (pkey->keymgmt != NULL) {
572
0
        struct raw_key_details_st raw_key;
573
574
0
        raw_key.key = priv == NULL ? NULL : &priv;
575
0
        raw_key.len = len;
576
0
        raw_key.selection = OSSL_KEYMGMT_SELECT_PRIVATE_KEY;
577
578
0
        return evp_keymgmt_util_export(pkey, OSSL_KEYMGMT_SELECT_PRIVATE_KEY,
579
0
            get_raw_key_details, &raw_key);
580
0
    }
581
582
0
    if (pkey->ameth == NULL) {
583
0
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
584
0
        return 0;
585
0
    }
586
587
0
    if (pkey->ameth->get_priv_key == NULL) {
588
0
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
589
0
        return 0;
590
0
    }
591
592
0
    if (!pkey->ameth->get_priv_key(pkey, priv, len)) {
593
0
        ERR_raise(ERR_LIB_EVP, EVP_R_GET_RAW_KEY_FAILED);
594
0
        return 0;
595
0
    }
596
597
0
    return 1;
598
0
}
599
600
int EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey, unsigned char *pub,
601
    size_t *len)
602
0
{
603
0
    if (pkey->keymgmt != NULL) {
604
0
        struct raw_key_details_st raw_key;
605
606
0
        raw_key.key = pub == NULL ? NULL : &pub;
607
0
        raw_key.len = len;
608
0
        raw_key.selection = OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
609
610
0
        return evp_keymgmt_util_export(pkey, OSSL_KEYMGMT_SELECT_PUBLIC_KEY,
611
0
            get_raw_key_details, &raw_key);
612
0
    }
613
614
0
    if (pkey->ameth == NULL) {
615
0
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
616
0
        return 0;
617
0
    }
618
619
0
    if (pkey->ameth->get_pub_key == NULL) {
620
0
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
621
0
        return 0;
622
0
    }
623
624
0
    if (!pkey->ameth->get_pub_key(pkey, pub, len)) {
625
0
        ERR_raise(ERR_LIB_EVP, EVP_R_GET_RAW_KEY_FAILED);
626
0
        return 0;
627
0
    }
628
629
0
    return 1;
630
0
}
631
632
static EVP_PKEY *new_cmac_key_int(const unsigned char *priv, size_t len,
633
    const char *cipher_name,
634
    const EVP_CIPHER *cipher,
635
    OSSL_LIB_CTX *libctx,
636
    const char *propq)
637
0
{
638
0
#ifndef OPENSSL_NO_CMAC
639
0
    OSSL_PARAM params[5], *p = params;
640
0
    EVP_PKEY *pkey = NULL;
641
0
    EVP_PKEY_CTX *ctx;
642
643
0
    if (cipher != NULL)
644
0
        cipher_name = EVP_CIPHER_get0_name(cipher);
645
646
0
    if (cipher_name == NULL) {
647
0
        ERR_raise(ERR_LIB_EVP, EVP_R_KEY_SETUP_FAILED);
648
0
        return NULL;
649
0
    }
650
651
0
    ctx = EVP_PKEY_CTX_new_from_name(libctx, "CMAC", propq);
652
0
    if (ctx == NULL)
653
0
        goto err;
654
655
0
    if (EVP_PKEY_fromdata_init(ctx) <= 0) {
656
0
        ERR_raise(ERR_LIB_EVP, EVP_R_KEY_SETUP_FAILED);
657
0
        goto err;
658
0
    }
659
660
0
    *p++ = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_PRIV_KEY,
661
0
        (void *)priv, len);
662
0
    *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_CIPHER,
663
0
        (char *)cipher_name, 0);
664
0
    if (propq != NULL)
665
0
        *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_PROPERTIES,
666
0
            (char *)propq, 0);
667
0
    *p = OSSL_PARAM_construct_end();
668
669
0
    if (EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEYPAIR, params) <= 0) {
670
0
        ERR_raise(ERR_LIB_EVP, EVP_R_KEY_SETUP_FAILED);
671
0
        goto err;
672
0
    }
673
674
0
err:
675
0
    EVP_PKEY_CTX_free(ctx);
676
677
0
    return pkey;
678
#else
679
    ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
680
    return NULL;
681
#endif
682
0
}
683
684
EVP_PKEY *EVP_PKEY_new_CMAC_key(ENGINE *e, const unsigned char *priv,
685
    size_t len, const EVP_CIPHER *cipher)
686
0
{
687
0
    if (!ossl_assert(e == NULL))
688
0
        return NULL;
689
0
    return new_cmac_key_int(priv, len, NULL, cipher, NULL, NULL);
690
0
}
691
692
int EVP_PKEY_set_type(EVP_PKEY *pkey, int type)
693
6.44M
{
694
6.44M
    return pkey_set_type(pkey, type, NULL, -1, NULL);
695
6.44M
}
696
697
int EVP_PKEY_set_type_str(EVP_PKEY *pkey, const char *str, int len)
698
0
{
699
0
    return pkey_set_type(pkey, EVP_PKEY_NONE, str, len, NULL);
700
0
}
701
702
#ifndef OPENSSL_NO_DEPRECATED_3_0
703
static void detect_foreign_key(EVP_PKEY *pkey)
704
621k
{
705
621k
    switch (pkey->type) {
706
151k
    case EVP_PKEY_RSA:
707
166k
    case EVP_PKEY_RSA_PSS:
708
166k
        pkey->foreign = pkey->pkey.rsa != NULL
709
166k
            && ossl_rsa_is_foreign(pkey->pkey.rsa);
710
166k
        break;
711
0
#ifndef OPENSSL_NO_EC
712
3.36k
    case EVP_PKEY_SM2:
713
3.36k
        break;
714
274k
    case EVP_PKEY_EC:
715
274k
        pkey->foreign = pkey->pkey.ec != NULL
716
274k
            && ossl_ec_key_is_foreign(pkey->pkey.ec);
717
274k
        break;
718
0
#endif
719
0
#ifndef OPENSSL_NO_DSA
720
132k
    case EVP_PKEY_DSA:
721
132k
        pkey->foreign = pkey->pkey.dsa != NULL
722
132k
            && ossl_dsa_is_foreign(pkey->pkey.dsa);
723
132k
        break;
724
0
#endif
725
0
#ifndef OPENSSL_NO_DH
726
1.56k
    case EVP_PKEY_DH:
727
1.56k
        pkey->foreign = pkey->pkey.dh != NULL
728
1.56k
            && ossl_dh_is_foreign(pkey->pkey.dh);
729
1.56k
        break;
730
0
#endif
731
43.4k
    default:
732
43.4k
        pkey->foreign = 0;
733
43.4k
        break;
734
621k
    }
735
621k
}
736
737
int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key)
738
769k
{
739
769k
#ifndef OPENSSL_NO_EC
740
769k
    int pktype;
741
742
769k
    pktype = EVP_PKEY_type(type);
743
769k
    if ((key != NULL) && (pktype == EVP_PKEY_EC || pktype == EVP_PKEY_SM2)) {
744
363k
        const EC_GROUP *group = EC_KEY_get0_group(key);
745
746
363k
        if (group != NULL) {
747
363k
            int curve = EC_GROUP_get_curve_name(group);
748
749
            /*
750
             * Regardless of what is requested the SM2 curve must be SM2 type,
751
             * and non SM2 curves are EC type.
752
             */
753
363k
            if (curve == NID_sm2 && pktype == EVP_PKEY_EC)
754
4.19k
                type = EVP_PKEY_SM2;
755
359k
            else if (curve != NID_sm2 && pktype == EVP_PKEY_SM2)
756
0
                type = EVP_PKEY_EC;
757
363k
        }
758
363k
    }
759
769k
#endif
760
761
769k
    if (pkey == NULL || !EVP_PKEY_set_type(pkey, type))
762
0
        return 0;
763
764
769k
    pkey->pkey.ptr = key;
765
769k
    detect_foreign_key(pkey);
766
767
769k
    return (key != NULL);
768
769k
}
769
#endif
770
771
void *EVP_PKEY_get0(const EVP_PKEY *pkey)
772
0
{
773
0
    if (pkey == NULL)
774
0
        return NULL;
775
776
0
    if (!evp_pkey_is_provided(pkey))
777
0
        return pkey->pkey.ptr;
778
779
0
    return NULL;
780
0
}
781
782
const unsigned char *EVP_PKEY_get0_hmac(const EVP_PKEY *pkey, size_t *len)
783
0
{
784
0
    const ASN1_OCTET_STRING *os = NULL;
785
0
    if (pkey->type != EVP_PKEY_HMAC) {
786
0
        ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_AN_HMAC_KEY);
787
0
        return NULL;
788
0
    }
789
0
    os = evp_pkey_get_legacy((EVP_PKEY *)pkey);
790
0
    if (os != NULL) {
791
0
        *len = os->length;
792
0
        return os->data;
793
0
    }
794
0
    return NULL;
795
0
}
796
797
#ifndef OPENSSL_NO_POLY1305
798
const unsigned char *EVP_PKEY_get0_poly1305(const EVP_PKEY *pkey, size_t *len)
799
0
{
800
0
    const ASN1_OCTET_STRING *os = NULL;
801
0
    if (pkey->type != EVP_PKEY_POLY1305) {
802
0
        ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_POLY1305_KEY);
803
0
        return NULL;
804
0
    }
805
0
    os = evp_pkey_get_legacy((EVP_PKEY *)pkey);
806
0
    if (os != NULL) {
807
0
        *len = os->length;
808
0
        return os->data;
809
0
    }
810
0
    return NULL;
811
0
}
812
#endif
813
814
#ifndef OPENSSL_NO_SIPHASH
815
const unsigned char *EVP_PKEY_get0_siphash(const EVP_PKEY *pkey, size_t *len)
816
0
{
817
0
    const ASN1_OCTET_STRING *os = NULL;
818
819
0
    if (pkey->type != EVP_PKEY_SIPHASH) {
820
0
        ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_SIPHASH_KEY);
821
0
        return NULL;
822
0
    }
823
0
    os = evp_pkey_get_legacy((EVP_PKEY *)pkey);
824
0
    if (os != NULL) {
825
0
        *len = os->length;
826
0
        return os->data;
827
0
    }
828
0
    return NULL;
829
0
}
830
#endif
831
832
#ifndef OPENSSL_NO_DSA
833
static DSA *evp_pkey_get0_DSA_int(const EVP_PKEY *pkey)
834
112k
{
835
112k
    if (pkey->type != EVP_PKEY_DSA) {
836
0
        ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_DSA_KEY);
837
0
        return NULL;
838
0
    }
839
112k
    return evp_pkey_get_legacy((EVP_PKEY *)pkey);
840
112k
}
841
842
const DSA *EVP_PKEY_get0_DSA(const EVP_PKEY *pkey)
843
0
{
844
0
    return evp_pkey_get0_DSA_int(pkey);
845
0
}
846
847
int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key)
848
0
{
849
0
    int ret;
850
851
0
    if (!DSA_up_ref(key))
852
0
        return 0;
853
854
0
    ret = EVP_PKEY_assign_DSA(pkey, key);
855
856
0
    if (!ret)
857
0
        DSA_free(key);
858
859
0
    return ret;
860
0
}
861
DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey)
862
87.7k
{
863
87.7k
    DSA *ret = evp_pkey_get0_DSA_int(pkey);
864
865
87.7k
    if (ret != NULL && !DSA_up_ref(ret))
866
0
        return NULL;
867
868
87.7k
    return ret;
869
87.7k
}
870
#endif /*  OPENSSL_NO_DSA */
871
872
#ifndef OPENSSL_NO_ECX
873
static const ECX_KEY *evp_pkey_get0_ECX_KEY(const EVP_PKEY *pkey, int type)
874
4.80k
{
875
4.80k
    if (EVP_PKEY_get_base_id(pkey) != type) {
876
0
        ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_ECX_KEY);
877
0
        return NULL;
878
0
    }
879
4.80k
    return evp_pkey_get_legacy((EVP_PKEY *)pkey);
880
4.80k
}
881
882
static ECX_KEY *evp_pkey_get1_ECX_KEY(EVP_PKEY *pkey, int type)
883
4.80k
{
884
4.80k
    ECX_KEY *ret = (ECX_KEY *)evp_pkey_get0_ECX_KEY(pkey, type);
885
886
4.80k
    if (ret != NULL && !ossl_ecx_key_up_ref(ret))
887
0
        ret = NULL;
888
4.80k
    return ret;
889
4.80k
}
890
891
#define IMPLEMENT_ECX_VARIANT(NAME)                          \
892
    ECX_KEY *ossl_evp_pkey_get1_##NAME(EVP_PKEY *pkey)       \
893
4.80k
    {                                                        \
894
4.80k
        return evp_pkey_get1_ECX_KEY(pkey, EVP_PKEY_##NAME); \
895
4.80k
    }
ossl_evp_pkey_get1_X25519
Line
Count
Source
893
2.04k
    {                                                        \
894
2.04k
        return evp_pkey_get1_ECX_KEY(pkey, EVP_PKEY_##NAME); \
895
2.04k
    }
ossl_evp_pkey_get1_X448
Line
Count
Source
893
90
    {                                                        \
894
90
        return evp_pkey_get1_ECX_KEY(pkey, EVP_PKEY_##NAME); \
895
90
    }
ossl_evp_pkey_get1_ED25519
Line
Count
Source
893
1.06k
    {                                                        \
894
1.06k
        return evp_pkey_get1_ECX_KEY(pkey, EVP_PKEY_##NAME); \
895
1.06k
    }
ossl_evp_pkey_get1_ED448
Line
Count
Source
893
1.61k
    {                                                        \
894
1.61k
        return evp_pkey_get1_ECX_KEY(pkey, EVP_PKEY_##NAME); \
895
1.61k
    }
896
IMPLEMENT_ECX_VARIANT(X25519)
897
IMPLEMENT_ECX_VARIANT(X448)
898
IMPLEMENT_ECX_VARIANT(ED25519)
899
IMPLEMENT_ECX_VARIANT(ED448)
900
901
#endif /* OPENSSL_NO_ECX */
902
903
#if !defined(OPENSSL_NO_DH) && !defined(OPENSSL_NO_DEPRECATED_3_0)
904
905
int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *dhkey)
906
0
{
907
0
    int ret, type;
908
909
    /*
910
     * ossl_dh_is_named_safe_prime_group() returns 1 for named safe prime groups
911
     * related to ffdhe and modp (which cache q = (p - 1) / 2),
912
     * and returns 0 for all other dh parameter generation types including
913
     * RFC5114 named groups.
914
     *
915
     * The EVP_PKEY_DH type is used for dh parameter generation types:
916
     *  - named safe prime groups related to ffdhe and modp
917
     *  - safe prime generator
918
     *
919
     * The type EVP_PKEY_DHX is used for dh parameter generation types
920
     *  - fips186-4 and fips186-2
921
     *  - rfc5114 named groups.
922
     *
923
     * The EVP_PKEY_DH type is used to save PKCS#3 data than can be stored
924
     * without a q value.
925
     * The EVP_PKEY_DHX type is used to save X9.42 data that requires the
926
     * q value to be stored.
927
     */
928
0
    if (ossl_dh_is_named_safe_prime_group(dhkey))
929
0
        type = EVP_PKEY_DH;
930
0
    else
931
0
        type = DH_get0_q(dhkey) == NULL ? EVP_PKEY_DH : EVP_PKEY_DHX;
932
933
0
    if (!DH_up_ref(dhkey))
934
0
        return 0;
935
936
0
    ret = EVP_PKEY_assign(pkey, type, dhkey);
937
938
0
    if (!ret)
939
0
        DH_free(dhkey);
940
941
0
    return ret;
942
0
}
943
944
DH *evp_pkey_get0_DH_int(const EVP_PKEY *pkey)
945
47.6k
{
946
47.6k
    if (pkey->type != EVP_PKEY_DH && pkey->type != EVP_PKEY_DHX) {
947
0
        ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_DH_KEY);
948
0
        return NULL;
949
0
    }
950
47.6k
    return evp_pkey_get_legacy((EVP_PKEY *)pkey);
951
47.6k
}
952
953
const DH *EVP_PKEY_get0_DH(const EVP_PKEY *pkey)
954
0
{
955
0
    return evp_pkey_get0_DH_int(pkey);
956
0
}
957
958
DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey)
959
39.9k
{
960
39.9k
    DH *ret = evp_pkey_get0_DH_int(pkey);
961
962
39.9k
    if (ret != NULL && !DH_up_ref(ret))
963
0
        ret = NULL;
964
965
39.9k
    return ret;
966
39.9k
}
967
#endif
968
969
int EVP_PKEY_get_id(const EVP_PKEY *pkey)
970
409k
{
971
409k
    return pkey->type;
972
409k
}
973
974
int EVP_PKEY_get_base_id(const EVP_PKEY *pkey)
975
339k
{
976
339k
    return EVP_PKEY_type(pkey->type);
977
339k
}
978
979
/*
980
 * These hard coded cases are pure hackery to get around the fact
981
 * that names in crypto/objects/objects.txt are a mess.  There is
982
 * no "EC", and "RSA" leads to the NID for 2.5.8.1.1, an OID that's
983
 * fallen out in favor of { pkcs-1 1 }, i.e. 1.2.840.113549.1.1.1,
984
 * the NID of which is used for EVP_PKEY_RSA.  Strangely enough,
985
 * "DSA" is accurate...  but still, better be safe and hard-code
986
 * names that we know.
987
 * On a similar topic, EVP_PKEY_type(EVP_PKEY_SM2) will result in
988
 * EVP_PKEY_EC, because of aliasing.
989
 * This should be cleaned away along with all other #legacy support.
990
 */
991
static const OSSL_ITEM standard_name2type[] = {
992
    { EVP_PKEY_RSA, "RSA" },
993
    { EVP_PKEY_RSA_PSS, "RSA-PSS" },
994
    { EVP_PKEY_EC, "EC" },
995
    { EVP_PKEY_ED25519, "ED25519" },
996
    { EVP_PKEY_ED448, "ED448" },
997
    { EVP_PKEY_X25519, "X25519" },
998
    { EVP_PKEY_X448, "X448" },
999
    { EVP_PKEY_SM2, "SM2" },
1000
    { EVP_PKEY_DH, "DH" },
1001
    { EVP_PKEY_DHX, "X9.42 DH" },
1002
    { EVP_PKEY_DHX, "DHX" },
1003
    { EVP_PKEY_DSA, "DSA" },
1004
};
1005
1006
int evp_pkey_name2type(const char *name)
1007
1.10M
{
1008
1.10M
    int type;
1009
1.10M
    size_t i;
1010
1011
11.2M
    for (i = 0; i < OSSL_NELEM(standard_name2type); i++) {
1012
10.6M
        if (OPENSSL_strcasecmp(name, standard_name2type[i].ptr) == 0)
1013
485k
            return (int)standard_name2type[i].id;
1014
10.6M
    }
1015
1016
622k
    if ((type = EVP_PKEY_type(OBJ_sn2nid(name))) != NID_undef)
1017
278k
        return type;
1018
344k
    return EVP_PKEY_type(OBJ_ln2nid(name));
1019
622k
}
1020
1021
const char *evp_pkey_type2name(int type)
1022
0
{
1023
0
    size_t i;
1024
1025
0
    for (i = 0; i < OSSL_NELEM(standard_name2type); i++) {
1026
0
        if (type == (int)standard_name2type[i].id)
1027
0
            return standard_name2type[i].ptr;
1028
0
    }
1029
1030
0
    return OBJ_nid2sn(type);
1031
0
}
1032
1033
int EVP_PKEY_is_a(const EVP_PKEY *pkey, const char *name)
1034
1.76M
{
1035
1.76M
    if (pkey == NULL)
1036
0
        return 0;
1037
1.76M
    if (pkey->keymgmt == NULL)
1038
446k
        return pkey->type == evp_pkey_name2type(name);
1039
1.32M
    return EVP_KEYMGMT_is_a(pkey->keymgmt, name);
1040
1.76M
}
1041
1042
int EVP_PKEY_type_names_do_all(const EVP_PKEY *pkey,
1043
    void (*fn)(const char *name, void *data),
1044
    void *data)
1045
0
{
1046
0
    if (!evp_pkey_is_typed(pkey))
1047
0
        return 0;
1048
1049
0
    if (!evp_pkey_is_provided(pkey)) {
1050
0
        const char *name = OBJ_nid2sn(EVP_PKEY_get_id(pkey));
1051
1052
0
        fn(name, data);
1053
0
        return 1;
1054
0
    }
1055
0
    return EVP_KEYMGMT_names_do_all(pkey->keymgmt, fn, data);
1056
0
}
1057
1058
int EVP_PKEY_can_sign(const EVP_PKEY *pkey)
1059
51.0k
{
1060
51.0k
    if (pkey->keymgmt == NULL) {
1061
0
        switch (EVP_PKEY_get_base_id(pkey)) {
1062
0
        case EVP_PKEY_RSA:
1063
0
        case EVP_PKEY_RSA_PSS:
1064
0
            return 1;
1065
0
#ifndef OPENSSL_NO_DSA
1066
0
        case EVP_PKEY_DSA:
1067
0
            return 1;
1068
0
#endif
1069
0
#ifndef OPENSSL_NO_EC
1070
0
        case EVP_PKEY_ED25519:
1071
0
        case EVP_PKEY_ED448:
1072
0
            return 1;
1073
0
        case EVP_PKEY_EC: /* Including SM2 */
1074
0
            return EC_KEY_can_sign(pkey->pkey.ec);
1075
0
#endif
1076
0
        default:
1077
0
            break;
1078
0
        }
1079
51.0k
    } else {
1080
51.0k
        const OSSL_PROVIDER *prov = EVP_KEYMGMT_get0_provider(pkey->keymgmt);
1081
51.0k
        OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
1082
51.0k
        EVP_SIGNATURE *sig;
1083
51.0k
        const char *name;
1084
1085
51.0k
        name = evp_keymgmt_util_query_operation_name(pkey->keymgmt,
1086
51.0k
            OSSL_OP_SIGNATURE);
1087
51.0k
        sig = EVP_SIGNATURE_fetch(libctx, name, NULL);
1088
51.0k
        if (sig != NULL) {
1089
51.0k
            EVP_SIGNATURE_free(sig);
1090
51.0k
            return 1;
1091
51.0k
        }
1092
51.0k
    }
1093
0
    return 0;
1094
51.0k
}
1095
1096
static int print_reset_indent(BIO **out, int pop_f_prefix, long saved_indent)
1097
84.9k
{
1098
84.9k
    BIO_set_indent(*out, saved_indent);
1099
84.9k
    if (pop_f_prefix) {
1100
75.6k
        BIO *next = BIO_pop(*out);
1101
1102
75.6k
        BIO_free(*out);
1103
75.6k
        *out = next;
1104
75.6k
    }
1105
84.9k
    return 1;
1106
84.9k
}
1107
1108
static int print_set_indent(BIO **out, int *pop_f_prefix, long *saved_indent,
1109
    long indent)
1110
84.9k
{
1111
84.9k
    *pop_f_prefix = 0;
1112
84.9k
    *saved_indent = 0;
1113
84.9k
    if (indent > 0) {
1114
75.6k
        long i = BIO_get_indent(*out);
1115
1116
75.6k
        *saved_indent = (i < 0 ? 0 : i);
1117
75.6k
        if (BIO_set_indent(*out, indent) <= 0) {
1118
75.6k
            BIO *prefbio = BIO_new(BIO_f_prefix());
1119
1120
75.6k
            if (prefbio == NULL)
1121
0
                return 0;
1122
75.6k
            *out = BIO_push(prefbio, *out);
1123
75.6k
            *pop_f_prefix = 1;
1124
75.6k
        }
1125
75.6k
        if (BIO_set_indent(*out, indent) <= 0) {
1126
0
            print_reset_indent(out, *pop_f_prefix, *saved_indent);
1127
0
            return 0;
1128
0
        }
1129
75.6k
    }
1130
84.9k
    return 1;
1131
84.9k
}
1132
1133
static int unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent,
1134
    const char *kstr)
1135
0
{
1136
0
    return BIO_indent(out, indent, 128)
1137
0
        && BIO_printf(out, "%s algorithm \"%s\" unsupported\n",
1138
0
               kstr, OBJ_nid2ln(pkey->type))
1139
0
        > 0;
1140
0
}
1141
1142
static int print_pkey(const EVP_PKEY *pkey, BIO *out, int indent,
1143
    int selection /* For provided encoding */,
1144
    const char *propquery /* For provided encoding */,
1145
    int (*legacy_print)(BIO *out, const EVP_PKEY *pkey,
1146
        int indent, ASN1_PCTX *pctx),
1147
    ASN1_PCTX *legacy_pctx /* For legacy print */)
1148
84.9k
{
1149
84.9k
    int pop_f_prefix;
1150
84.9k
    long saved_indent;
1151
84.9k
    OSSL_ENCODER_CTX *ctx = NULL;
1152
84.9k
    int ret = -2; /* default to unsupported */
1153
1154
84.9k
    if (!print_set_indent(&out, &pop_f_prefix, &saved_indent, indent))
1155
0
        return 0;
1156
1157
84.9k
    ctx = OSSL_ENCODER_CTX_new_for_pkey(pkey, selection, "TEXT", NULL,
1158
84.9k
        propquery);
1159
84.9k
    if (OSSL_ENCODER_CTX_get_num_encoders(ctx) != 0)
1160
83.6k
        ret = OSSL_ENCODER_to_bio(ctx, out);
1161
84.9k
    OSSL_ENCODER_CTX_free(ctx);
1162
1163
84.9k
    if (ret != -2)
1164
83.6k
        goto end;
1165
1166
    /* legacy fallback */
1167
1.23k
    if (legacy_print != NULL)
1168
1.23k
        ret = legacy_print(out, pkey, 0, legacy_pctx);
1169
0
    else
1170
0
        ret = unsup_alg(out, pkey, 0, "Public Key");
1171
1172
84.9k
end:
1173
84.9k
    print_reset_indent(&out, pop_f_prefix, saved_indent);
1174
84.9k
    return ret;
1175
1.23k
}
1176
1177
int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey,
1178
    int indent, ASN1_PCTX *pctx)
1179
30.5k
{
1180
30.5k
    return print_pkey(pkey, out, indent, EVP_PKEY_PUBLIC_KEY, NULL,
1181
30.5k
        (pkey->ameth != NULL ? pkey->ameth->pub_print : NULL),
1182
30.5k
        pctx);
1183
30.5k
}
1184
1185
int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey,
1186
    int indent, ASN1_PCTX *pctx)
1187
31.8k
{
1188
31.8k
    return print_pkey(pkey, out, indent, EVP_PKEY_PRIVATE_KEY, NULL,
1189
31.8k
        (pkey->ameth != NULL ? pkey->ameth->priv_print : NULL),
1190
31.8k
        pctx);
1191
31.8k
}
1192
1193
int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey,
1194
    int indent, ASN1_PCTX *pctx)
1195
22.5k
{
1196
22.5k
    return print_pkey(pkey, out, indent, EVP_PKEY_KEY_PARAMETERS, NULL,
1197
22.5k
        (pkey->ameth != NULL ? pkey->ameth->param_print : NULL),
1198
22.5k
        pctx);
1199
22.5k
}
1200
1201
#ifndef OPENSSL_NO_STDIO
1202
int EVP_PKEY_print_public_fp(FILE *fp, const EVP_PKEY *pkey,
1203
    int indent, ASN1_PCTX *pctx)
1204
0
{
1205
0
    int ret;
1206
0
    BIO *b = BIO_new_fp(fp, BIO_NOCLOSE);
1207
1208
0
    if (b == NULL)
1209
0
        return 0;
1210
0
    ret = EVP_PKEY_print_public(b, pkey, indent, pctx);
1211
0
    BIO_free(b);
1212
0
    return ret;
1213
0
}
1214
1215
int EVP_PKEY_print_private_fp(FILE *fp, const EVP_PKEY *pkey,
1216
    int indent, ASN1_PCTX *pctx)
1217
0
{
1218
0
    int ret;
1219
0
    BIO *b = BIO_new_fp(fp, BIO_NOCLOSE);
1220
1221
0
    if (b == NULL)
1222
0
        return 0;
1223
0
    ret = EVP_PKEY_print_private(b, pkey, indent, pctx);
1224
0
    BIO_free(b);
1225
0
    return ret;
1226
0
}
1227
1228
int EVP_PKEY_print_params_fp(FILE *fp, const EVP_PKEY *pkey,
1229
    int indent, ASN1_PCTX *pctx)
1230
0
{
1231
0
    int ret;
1232
0
    BIO *b = BIO_new_fp(fp, BIO_NOCLOSE);
1233
1234
0
    if (b == NULL)
1235
0
        return 0;
1236
0
    ret = EVP_PKEY_print_params(b, pkey, indent, pctx);
1237
0
    BIO_free(b);
1238
0
    return ret;
1239
0
}
1240
#endif
1241
1242
static void mdname2nid(const char *mdname, void *data)
1243
4.76k
{
1244
4.76k
    int *nid = (int *)data;
1245
1246
4.76k
    if (*nid != NID_undef)
1247
3.52k
        return;
1248
1249
1.23k
    *nid = OBJ_sn2nid(mdname);
1250
1.23k
    if (*nid == NID_undef)
1251
0
        *nid = OBJ_ln2nid(mdname);
1252
1.23k
}
1253
1254
static int legacy_asn1_ctrl_to_param(EVP_PKEY *pkey, int op,
1255
    int arg1, void *arg2)
1256
1.24k
{
1257
1.24k
    if (pkey->keymgmt == NULL)
1258
0
        return 0;
1259
1.24k
    switch (op) {
1260
1.24k
    case ASN1_PKEY_CTRL_DEFAULT_MD_NID: {
1261
1.24k
        char mdname[80] = "";
1262
1.24k
        int rv = EVP_PKEY_get_default_digest_name(pkey, mdname,
1263
1.24k
            sizeof(mdname));
1264
1265
1.24k
        if (rv > 0) {
1266
1.23k
            int mdnum;
1267
1.23k
            OSSL_LIB_CTX *libctx = ossl_provider_libctx(pkey->keymgmt->prov);
1268
            /* Make sure the MD is in the namemap if available */
1269
1.23k
            EVP_MD *md;
1270
1.23k
            OSSL_NAMEMAP *namemap;
1271
1.23k
            int nid = NID_undef;
1272
1273
1.23k
            (void)ERR_set_mark();
1274
1.23k
            md = EVP_MD_fetch(libctx, mdname, NULL);
1275
1.23k
            (void)ERR_pop_to_mark();
1276
1.23k
            namemap = ossl_namemap_stored(libctx);
1277
1278
            /*
1279
             * The only reason to fetch the MD was to make sure it is in the
1280
             * namemap. We can immediately free it.
1281
             */
1282
1.23k
            EVP_MD_free(md);
1283
1.23k
            mdnum = ossl_namemap_name2num(namemap, mdname);
1284
1.23k
            if (mdnum == 0)
1285
0
                return 0;
1286
1287
            /*
1288
             * We have the namemap number - now we need to find the
1289
             * associated nid
1290
             */
1291
1.23k
            if (!ossl_namemap_doall_names(namemap, mdnum, mdname2nid, &nid))
1292
0
                return 0;
1293
1.23k
            *(int *)arg2 = nid;
1294
1.23k
        }
1295
1.24k
        return rv;
1296
1.24k
    }
1297
0
    default:
1298
0
        return -2;
1299
1.24k
    }
1300
1.24k
}
1301
1302
static int evp_pkey_asn1_ctrl(EVP_PKEY *pkey, int op, int arg1, void *arg2)
1303
1.24k
{
1304
1.24k
    if (pkey->ameth == NULL)
1305
1.24k
        return legacy_asn1_ctrl_to_param(pkey, op, arg1, arg2);
1306
0
    if (pkey->ameth->pkey_ctrl == NULL)
1307
0
        return -2;
1308
0
    return pkey->ameth->pkey_ctrl(pkey, op, arg1, arg2);
1309
0
}
1310
1311
int EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid)
1312
1.24k
{
1313
1.24k
    if (pkey == NULL)
1314
0
        return 0;
1315
1.24k
    return evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID, 0, pnid);
1316
1.24k
}
1317
1318
int EVP_PKEY_get_default_digest_name(EVP_PKEY *pkey,
1319
    char *mdname, size_t mdname_sz)
1320
2.36k
{
1321
2.36k
    if (pkey->ameth == NULL)
1322
2.36k
        return evp_keymgmt_util_get_deflt_digest_name(pkey->keymgmt,
1323
2.36k
            pkey->keydata,
1324
2.36k
            mdname, mdname_sz);
1325
1326
0
    {
1327
0
        int nid = NID_undef;
1328
0
        int rv = EVP_PKEY_get_default_digest_nid(pkey, &nid);
1329
0
        const char *name = rv > 0 ? OBJ_nid2sn(nid) : NULL;
1330
1331
0
        if (rv > 0)
1332
0
            OPENSSL_strlcpy(mdname, name, mdname_sz);
1333
0
        return rv;
1334
2.36k
    }
1335
2.36k
}
1336
1337
int EVP_PKEY_get_group_name(const EVP_PKEY *pkey, char *gname, size_t gname_sz,
1338
    size_t *gname_len)
1339
31.5k
{
1340
31.5k
    return EVP_PKEY_get_utf8_string_param(pkey, OSSL_PKEY_PARAM_GROUP_NAME,
1341
31.5k
        gname, gname_sz, gname_len);
1342
31.5k
}
1343
1344
int EVP_PKEY_digestsign_supports_digest(EVP_PKEY *pkey, OSSL_LIB_CTX *libctx,
1345
    const char *name, const char *propq)
1346
34.3k
{
1347
34.3k
    int rv;
1348
34.3k
    EVP_MD_CTX *ctx = NULL;
1349
1350
34.3k
    if ((ctx = EVP_MD_CTX_new()) == NULL)
1351
0
        return -1;
1352
1353
34.3k
    ERR_set_mark();
1354
34.3k
    rv = EVP_DigestSignInit_ex(ctx, NULL, name, libctx,
1355
34.3k
        propq, pkey, NULL);
1356
34.3k
    ERR_pop_to_mark();
1357
1358
34.3k
    EVP_MD_CTX_free(ctx);
1359
34.3k
    return rv;
1360
34.3k
}
1361
#endif /* !FIPS_MODULE */
1362
1363
int EVP_PKEY_set1_encoded_public_key(EVP_PKEY *pkey, const unsigned char *pub,
1364
    size_t publen)
1365
46.2k
{
1366
46.2k
    if (pkey == NULL)
1367
0
        return 0;
1368
46.2k
#ifndef FIPS_MODULE
1369
46.2k
    if (evp_pkey_is_provided(pkey))
1370
46.2k
#endif /* !FIPS_MODULE */
1371
46.2k
        return EVP_PKEY_set_octet_string_param(pkey,
1372
46.2k
            OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
1373
46.2k
            (unsigned char *)pub, publen);
1374
1375
0
#ifndef FIPS_MODULE
1376
0
    if (publen > INT_MAX)
1377
0
        return 0;
1378
    /* Historically this function was EVP_PKEY_set1_tls_encodedpoint */
1379
0
    if (evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SET1_TLS_ENCPT, (int)publen,
1380
0
            (void *)pub)
1381
0
        <= 0)
1382
0
        return 0;
1383
0
    return 1;
1384
0
#endif /* !FIPS_MODULE */
1385
0
}
1386
1387
size_t EVP_PKEY_get1_encoded_public_key(EVP_PKEY *pkey, unsigned char **ppub)
1388
213k
{
1389
213k
    if (pkey == NULL)
1390
0
        return 0;
1391
213k
#ifndef FIPS_MODULE
1392
213k
    if (evp_pkey_is_provided(pkey))
1393
213k
#endif
1394
213k
    {
1395
213k
        size_t return_size = OSSL_PARAM_UNMODIFIED;
1396
213k
        unsigned char *buf;
1397
1398
        /*
1399
         * We know that this is going to fail, but it will give us a size
1400
         * to allocate.
1401
         */
1402
213k
        EVP_PKEY_get_octet_string_param(pkey,
1403
213k
            OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
1404
213k
            NULL, 0, &return_size);
1405
213k
        if (return_size == OSSL_PARAM_UNMODIFIED)
1406
0
            return 0;
1407
1408
213k
        *ppub = NULL;
1409
213k
        buf = OPENSSL_malloc(return_size);
1410
213k
        if (buf == NULL)
1411
0
            return 0;
1412
1413
213k
        if (!EVP_PKEY_get_octet_string_param(pkey,
1414
213k
                OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
1415
213k
                buf, return_size, NULL)) {
1416
0
            OPENSSL_free(buf);
1417
0
            return 0;
1418
0
        }
1419
213k
        *ppub = buf;
1420
213k
        return return_size;
1421
213k
    }
1422
1423
0
#ifndef FIPS_MODULE
1424
0
    {
1425
0
        int rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_GET1_TLS_ENCPT, 0, ppub);
1426
0
        if (rv <= 0)
1427
0
            return 0;
1428
0
        return rv;
1429
0
    }
1430
0
#endif /* !FIPS_MODULE */
1431
0
}
1432
1433
/*- All methods below can also be used in FIPS_MODULE */
1434
1435
EVP_PKEY *EVP_PKEY_new(void)
1436
2.69M
{
1437
2.69M
    EVP_PKEY *ret = OPENSSL_zalloc(sizeof(*ret));
1438
1439
2.69M
    if (ret == NULL)
1440
0
        return NULL;
1441
1442
2.69M
    ret->type = EVP_PKEY_NONE;
1443
2.69M
    ret->save_type = EVP_PKEY_NONE;
1444
1445
2.69M
    if (!CRYPTO_NEW_REF(&ret->references, 1))
1446
0
        goto err;
1447
1448
2.69M
    ret->lock = CRYPTO_THREAD_lock_new();
1449
2.69M
    if (ret->lock == NULL) {
1450
0
        ERR_raise(ERR_LIB_EVP, ERR_R_CRYPTO_LIB);
1451
0
        goto err;
1452
0
    }
1453
1454
2.69M
#ifndef FIPS_MODULE
1455
2.69M
    ret->save_parameters = 1;
1456
2.69M
    if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_EVP_PKEY, ret, &ret->ex_data)) {
1457
0
        ERR_raise(ERR_LIB_EVP, ERR_R_CRYPTO_LIB);
1458
0
        goto err;
1459
0
    }
1460
2.69M
#endif
1461
2.69M
    return ret;
1462
1463
0
err:
1464
0
    CRYPTO_FREE_REF(&ret->references);
1465
0
    CRYPTO_THREAD_lock_free(ret->lock);
1466
0
    OPENSSL_free(ret);
1467
0
    return NULL;
1468
2.69M
}
1469
1470
/*
1471
 * Setup a public key management method.
1472
 *
1473
 * For legacy keys, either |type| or |str| is expected to have the type
1474
 * information. In this case, the setup consists of finding an ASN1 method
1475
 * and setting those fields in |pkey|.
1476
 *
1477
 * For provider side keys, |keymgmt| is expected to be non-NULL.  In this
1478
 * case, the setup consists of setting the |keymgmt| field in |pkey|.
1479
 *
1480
 * If pkey is NULL just return 1 or 0 if the key management method exists.
1481
 */
1482
1483
static int pkey_set_type(EVP_PKEY *pkey, int type, const char *str,
1484
    int len, EVP_KEYMGMT *keymgmt)
1485
5.59M
{
1486
5.59M
#ifndef FIPS_MODULE
1487
5.59M
    const EVP_PKEY_ASN1_METHOD *ameth = NULL;
1488
5.59M
#endif
1489
1490
    /*
1491
     * The setups can't set both legacy and provider side methods.
1492
     * It is forbidden
1493
     */
1494
5.59M
    if (!ossl_assert(type == EVP_PKEY_NONE || keymgmt == NULL)) {
1495
0
        ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
1496
0
        return 0;
1497
0
    }
1498
1499
5.59M
    if (pkey != NULL) {
1500
3.62M
        int free_it = 0;
1501
1502
3.62M
#ifndef FIPS_MODULE
1503
3.62M
        free_it = free_it || pkey->pkey.ptr != NULL;
1504
3.62M
#endif
1505
3.62M
        free_it = free_it || pkey->keydata != NULL;
1506
3.62M
        if (free_it)
1507
0
            evp_pkey_free_it(pkey);
1508
3.62M
#ifndef FIPS_MODULE
1509
        /*
1510
         * If key type matches and a method exists then this lookup has
1511
         * succeeded once so just indicate success.
1512
         */
1513
3.62M
        if (pkey->type != EVP_PKEY_NONE
1514
2.34M
            && type == pkey->save_type
1515
1.85M
            && pkey->ameth != NULL)
1516
1.85M
            return 1;
1517
3.62M
#endif
1518
3.62M
    }
1519
3.74M
#ifndef FIPS_MODULE
1520
3.74M
    if (str != NULL)
1521
2.46M
        ameth = evp_pkey_asn1_find_str(str, len);
1522
1.27M
    else if (type != EVP_PKEY_NONE)
1523
1.14M
        ameth = evp_pkey_asn1_find(type);
1524
3.74M
#endif
1525
1526
3.74M
    {
1527
3.74M
        int check = 1;
1528
1529
3.74M
#ifndef FIPS_MODULE
1530
3.74M
        check = check && ameth == NULL;
1531
3.74M
#endif
1532
3.74M
        check = check && keymgmt == NULL;
1533
3.74M
        if (check) {
1534
1.47M
            ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_ALGORITHM);
1535
1.47M
            return 0;
1536
1.47M
        }
1537
3.74M
    }
1538
2.26M
    if (pkey != NULL) {
1539
1.77M
        if (keymgmt != NULL && !EVP_KEYMGMT_up_ref(keymgmt)) {
1540
0
            ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
1541
0
            return 0;
1542
0
        }
1543
1544
1.77M
        pkey->keymgmt = keymgmt;
1545
1546
1.77M
        pkey->save_type = type;
1547
1.77M
        pkey->type = type;
1548
1549
1.77M
#ifndef FIPS_MODULE
1550
        /*
1551
         * If the internal "origin" key is provider side, don't save |ameth|.
1552
         * The main reason is that |ameth| is one factor to detect that the
1553
         * internal "origin" key is a legacy one.
1554
         */
1555
1.77M
        if (keymgmt == NULL)
1556
1.14M
            pkey->ameth = ameth;
1557
1558
        /*
1559
         * The EVP_PKEY_ASN1_METHOD |pkey_id| retains its legacy key purpose
1560
         * for any key type that has a legacy implementation, regardless of
1561
         * if the internal key is a legacy or a provider side one.  When
1562
         * there is no legacy implementation for the key, the type becomes
1563
         * EVP_PKEY_KEYMGMT, which indicates that one should be cautious
1564
         * with functions that expect legacy internal keys.
1565
         */
1566
1.77M
        if (ameth != NULL) {
1567
1.64M
            if (type == EVP_PKEY_NONE)
1568
495k
                pkey->type = ameth->pkey_id;
1569
1.64M
        } else {
1570
127k
            pkey->type = EVP_PKEY_KEYMGMT;
1571
127k
        }
1572
1.77M
#endif
1573
1.77M
    }
1574
2.26M
    return 1;
1575
2.26M
}
1576
1577
#ifndef FIPS_MODULE
1578
static void find_ameth(const char *name, void *data)
1579
3.96M
{
1580
3.96M
    const char **str = data;
1581
1582
    /*
1583
     * The error messages from pkey_set_type() are uninteresting here,
1584
     * and misleading.
1585
     */
1586
3.96M
    ERR_set_mark();
1587
1588
3.96M
    if (pkey_set_type(NULL, EVP_PKEY_NONE, name, (int)strlen(name),
1589
3.96M
            NULL)) {
1590
1.01M
        if (str[0] == NULL)
1591
1.01M
            str[0] = name;
1592
0
        else if (str[1] == NULL)
1593
0
            str[1] = name;
1594
1.01M
    }
1595
1596
3.96M
    ERR_pop_to_mark();
1597
3.96M
}
1598
#endif
1599
1600
int EVP_PKEY_set_type_by_keymgmt(EVP_PKEY *pkey, EVP_KEYMGMT *keymgmt)
1601
1.22M
{
1602
1.22M
#ifndef FIPS_MODULE
1603
1.22M
#define EVP_PKEY_TYPE_STR str[0]
1604
1.22M
#define EVP_PKEY_TYPE_STRLEN (str[0] == NULL ? -1 : (int)strlen(str[0]))
1605
    /*
1606
     * Find at most two strings that have an associated EVP_PKEY_ASN1_METHOD
1607
     * Ideally, only one should be found.  If two (or more) are found, the
1608
     * match is ambiguous.  This should never happen, but...
1609
     */
1610
1.22M
    const char *str[2] = { NULL, NULL };
1611
1612
1.22M
    if (!EVP_KEYMGMT_names_do_all(keymgmt, find_ameth, &str)
1613
1.22M
        || str[1] != NULL) {
1614
0
        ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
1615
0
        return 0;
1616
0
    }
1617
#else
1618
#define EVP_PKEY_TYPE_STR NULL
1619
#define EVP_PKEY_TYPE_STRLEN -1
1620
#endif
1621
1.22M
    return pkey_set_type(pkey, EVP_PKEY_NONE,
1622
1.22M
        EVP_PKEY_TYPE_STR, EVP_PKEY_TYPE_STRLEN,
1623
1.22M
        keymgmt);
1624
1625
1.22M
#undef EVP_PKEY_TYPE_STR
1626
1.22M
#undef EVP_PKEY_TYPE_STRLEN
1627
1.22M
}
1628
1629
int EVP_PKEY_up_ref(EVP_PKEY *pkey)
1630
2.60M
{
1631
2.60M
    int i;
1632
1633
2.60M
    if (!CRYPTO_UP_REF(&pkey->references, &i))
1634
0
        return 0;
1635
1636
2.60M
    REF_PRINT_COUNT("EVP_PKEY", i, pkey);
1637
2.60M
    REF_ASSERT_ISNT(i < 2);
1638
2.60M
    return ((i > 1) ? 1 : 0);
1639
2.60M
}
1640
1641
EVP_PKEY *EVP_PKEY_dup(EVP_PKEY *pkey)
1642
22.5k
{
1643
22.5k
    EVP_PKEY *dup_pk;
1644
1645
22.5k
    if (pkey == NULL) {
1646
0
        ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
1647
0
        return NULL;
1648
0
    }
1649
1650
22.5k
    if ((dup_pk = EVP_PKEY_new()) == NULL)
1651
0
        return NULL;
1652
1653
22.5k
    if (evp_pkey_is_blank(pkey))
1654
0
        goto done;
1655
1656
22.5k
#ifndef FIPS_MODULE
1657
22.5k
    if (evp_pkey_is_provided(pkey))
1658
22.5k
#endif /* !FIPS_MODULE */
1659
22.5k
    {
1660
22.5k
        if (!evp_keymgmt_util_copy(dup_pk, pkey,
1661
22.5k
                OSSL_KEYMGMT_SELECT_ALL))
1662
0
            goto err;
1663
22.5k
        goto done;
1664
22.5k
    }
1665
1666
0
#ifndef FIPS_MODULE
1667
0
    if (evp_pkey_is_legacy(pkey)) {
1668
0
        const EVP_PKEY_ASN1_METHOD *ameth = pkey->ameth;
1669
1670
0
        if (ameth == NULL || ameth->copy == NULL) {
1671
0
            if (pkey->pkey.ptr == NULL /* empty key, just set type */
1672
0
                && EVP_PKEY_set_type(dup_pk, pkey->type) != 0)
1673
0
                goto done;
1674
0
            ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEY_TYPE);
1675
0
            goto err;
1676
0
        }
1677
0
        if (!ameth->copy(dup_pk, pkey))
1678
0
            goto err;
1679
0
        goto done;
1680
0
    }
1681
0
#endif /* !FIPS_MODULE */
1682
1683
0
    goto err;
1684
22.5k
done:
1685
22.5k
#ifndef FIPS_MODULE
1686
    /* copy auxiliary data */
1687
22.5k
    if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_EVP_PKEY,
1688
22.5k
            &dup_pk->ex_data, &pkey->ex_data))
1689
0
        goto err;
1690
1691
22.5k
    if (pkey->attributes != NULL) {
1692
0
        if ((dup_pk->attributes = ossl_x509at_dup(pkey->attributes)) == NULL)
1693
0
            goto err;
1694
0
    }
1695
22.5k
#endif /* !FIPS_MODULE */
1696
22.5k
    return dup_pk;
1697
0
err:
1698
0
    EVP_PKEY_free(dup_pk);
1699
0
    return NULL;
1700
22.5k
}
1701
1702
#ifndef FIPS_MODULE
1703
void evp_pkey_free_legacy(EVP_PKEY *x)
1704
3.09M
{
1705
3.09M
    const EVP_PKEY_ASN1_METHOD *ameth = x->ameth;
1706
1707
3.09M
    if (ameth == NULL && x->legacy_cache_pkey.ptr != NULL)
1708
81.9k
        ameth = evp_pkey_asn1_find(x->type);
1709
1710
3.09M
    if (ameth != NULL) {
1711
1.54M
        if (x->legacy_cache_pkey.ptr != NULL) {
1712
            /*
1713
             * We should never have both a legacy origin key, and a key in the
1714
             * legacy cache.
1715
             */
1716
81.9k
            assert(x->pkey.ptr == NULL);
1717
            /*
1718
             * For the purposes of freeing we make the legacy cache look like
1719
             * a legacy origin key.
1720
             */
1721
81.9k
            x->pkey = x->legacy_cache_pkey;
1722
81.9k
            x->legacy_cache_pkey.ptr = NULL;
1723
81.9k
        }
1724
1.54M
        if (ameth->pkey_free != NULL)
1725
1.54M
            ameth->pkey_free(x);
1726
1.54M
        x->pkey.ptr = NULL;
1727
1.54M
    }
1728
3.09M
}
1729
#endif /* FIPS_MODULE */
1730
1731
static void evp_pkey_free_it(EVP_PKEY *x)
1732
2.69M
{
1733
    /* internal function; x is never NULL */
1734
2.69M
    evp_keymgmt_util_clear_operation_cache(x);
1735
2.69M
#ifndef FIPS_MODULE
1736
2.69M
    evp_pkey_free_legacy(x);
1737
2.69M
#endif
1738
1739
2.69M
    if (x->keymgmt != NULL) {
1740
1.22M
        evp_keymgmt_freedata(x->keymgmt, x->keydata);
1741
1.22M
        EVP_KEYMGMT_free(x->keymgmt);
1742
1.22M
        x->keymgmt = NULL;
1743
1.22M
        x->keydata = NULL;
1744
1.22M
    }
1745
2.69M
    x->type = EVP_PKEY_NONE;
1746
2.69M
}
1747
1748
void EVP_PKEY_free(EVP_PKEY *x)
1749
29.5M
{
1750
29.5M
    int i;
1751
1752
29.5M
    if (x == NULL)
1753
19.2M
        return;
1754
1755
10.2M
    CRYPTO_DOWN_REF(&x->references, &i);
1756
10.2M
    REF_PRINT_COUNT("EVP_PKEY", i, x);
1757
10.2M
    if (i > 0)
1758
7.51M
        return;
1759
2.69M
    REF_ASSERT_ISNT(i < 0);
1760
2.69M
    evp_pkey_free_it(x);
1761
2.69M
#ifndef FIPS_MODULE
1762
2.69M
    CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EVP_PKEY, x, &x->ex_data);
1763
2.69M
#endif
1764
2.69M
    CRYPTO_THREAD_lock_free(x->lock);
1765
2.69M
    CRYPTO_FREE_REF(&x->references);
1766
2.69M
#ifndef FIPS_MODULE
1767
2.69M
    sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free);
1768
2.69M
#endif
1769
2.69M
    OPENSSL_free(x);
1770
2.69M
}
1771
1772
int EVP_PKEY_get_size(const EVP_PKEY *pkey)
1773
5.86k
{
1774
5.86k
    int size = 0;
1775
1776
5.86k
    if (pkey != NULL) {
1777
5.86k
        size = pkey->cache.size;
1778
5.86k
#ifndef FIPS_MODULE
1779
5.86k
        if (pkey->ameth != NULL && pkey->ameth->pkey_size != NULL)
1780
1.57k
            size = pkey->ameth->pkey_size(pkey);
1781
5.86k
#endif
1782
5.86k
    }
1783
5.86k
    if (size <= 0) {
1784
0
        ERR_raise(ERR_LIB_EVP, EVP_R_UNKNOWN_MAX_SIZE);
1785
0
        return 0;
1786
0
    }
1787
5.86k
    return size;
1788
5.86k
}
1789
1790
const char *EVP_PKEY_get0_description(const EVP_PKEY *pkey)
1791
0
{
1792
0
    if (!evp_pkey_is_assigned(pkey))
1793
0
        return NULL;
1794
1795
0
    if (evp_pkey_is_provided(pkey) && pkey->keymgmt->description != NULL)
1796
0
        return pkey->keymgmt->description;
1797
0
#ifndef FIPS_MODULE
1798
0
    if (pkey->ameth != NULL)
1799
0
        return pkey->ameth->info;
1800
0
#endif
1801
0
    return NULL;
1802
0
}
1803
1804
void *evp_pkey_export_to_provider(EVP_PKEY *pk, OSSL_LIB_CTX *libctx,
1805
    EVP_KEYMGMT **keymgmt,
1806
    const char *propquery)
1807
153k
{
1808
153k
    EVP_KEYMGMT *allocated_keymgmt = NULL;
1809
153k
    EVP_KEYMGMT *tmp_keymgmt = NULL;
1810
153k
    int selection = OSSL_KEYMGMT_SELECT_ALL;
1811
153k
    void *keydata = NULL;
1812
153k
    int check;
1813
1814
153k
    if (pk == NULL)
1815
0
        return NULL;
1816
1817
    /* No key data => nothing to export */
1818
153k
    check = 1;
1819
153k
#ifndef FIPS_MODULE
1820
153k
    check = check && pk->pkey.ptr == NULL;
1821
153k
#endif
1822
153k
    check = check && pk->keydata == NULL;
1823
153k
    if (check)
1824
0
        return NULL;
1825
1826
153k
#ifndef FIPS_MODULE
1827
153k
    if (pk->pkey.ptr != NULL) {
1828
        /*
1829
         * If the legacy key doesn't have an dirty counter or export function,
1830
         * give up
1831
         */
1832
26.3k
        if (pk->ameth->dirty_cnt == NULL || pk->ameth->export_to == NULL)
1833
0
            return NULL;
1834
26.3k
    }
1835
153k
#endif
1836
1837
153k
    if (keymgmt != NULL) {
1838
153k
        tmp_keymgmt = *keymgmt;
1839
153k
        *keymgmt = NULL;
1840
153k
    }
1841
1842
    /*
1843
     * If no keymgmt was given or found, get a default keymgmt.  We do so by
1844
     * letting EVP_PKEY_CTX_new_from_pkey() do it for us, then we steal it.
1845
     */
1846
153k
    if (tmp_keymgmt == NULL) {
1847
0
        EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pk, propquery);
1848
1849
0
        if (ctx == NULL)
1850
0
            goto end;
1851
0
        allocated_keymgmt = tmp_keymgmt = ctx->keymgmt;
1852
0
        ctx->keymgmt = NULL;
1853
0
        EVP_PKEY_CTX_free(ctx);
1854
0
    }
1855
1856
    /* If there's still no keymgmt to be had, give up */
1857
153k
    if (tmp_keymgmt == NULL)
1858
0
        goto end;
1859
1860
153k
#ifndef FIPS_MODULE
1861
153k
    if (pk->pkey.ptr != NULL) {
1862
26.3k
        OP_CACHE_ELEM *op;
1863
26.3k
        OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
1864
26.3k
        OSSL_PARAM *p = NULL;
1865
1866
        /*
1867
         * If the legacy "origin" hasn't changed since last time, we try
1868
         * to find our keymgmt in the operation cache.  If it has changed,
1869
         * |i| remains zero, and we will clear the cache further down.
1870
         */
1871
26.3k
        if (pk->ameth->dirty_cnt(pk) == pk->dirty_cnt_copy) {
1872
26.3k
            if (!CRYPTO_THREAD_read_lock(pk->lock))
1873
0
                goto end;
1874
26.3k
            op = evp_keymgmt_util_find_operation_cache(pk, tmp_keymgmt,
1875
26.3k
                selection);
1876
1877
            /*
1878
             * If |tmp_keymgmt| is present in the operation cache, it means
1879
             * that export doesn't need to be redone.  In that case, we take
1880
             * token copies of the cached pointers, to have token success
1881
             * values to return. It is possible (e.g. in a no-cached-fetch
1882
             * build), for op->keymgmt to be a different pointer to tmp_keymgmt
1883
             * even though the name/provider must be the same. In other words
1884
             * the keymgmt instance may be different but still equivalent, i.e.
1885
             * same algorithm/provider instance - but we make the simplifying
1886
             * assumption that the keydata can be used with either keymgmt
1887
             * instance. Not doing so introduces significant complexity and
1888
             * probably requires refactoring - since we would have to ripple
1889
             * the change in keymgmt instance up the call chain.
1890
             */
1891
26.3k
            if (op != NULL && op->keymgmt != NULL) {
1892
17.3k
                keydata = op->keydata;
1893
17.3k
                CRYPTO_THREAD_unlock(pk->lock);
1894
17.3k
                goto end;
1895
17.3k
            }
1896
8.95k
            CRYPTO_THREAD_unlock(pk->lock);
1897
8.95k
        }
1898
1899
        /* Make sure that the keymgmt key type matches the legacy NID */
1900
8.95k
        if (!EVP_KEYMGMT_is_a(tmp_keymgmt, OBJ_nid2sn(pk->type)))
1901
0
            goto end;
1902
1903
8.95k
        if (strcmp(OSSL_PROVIDER_get0_name(EVP_KEYMGMT_get0_provider(tmp_keymgmt)), "default") == 0) {
1904
            /*
1905
             * We attempt to pass the low-level object to the keymgmt. We only
1906
             * support this via an internal use only parameter. We break the
1907
             * normal rules that prevent passing complex objects via OSSL_PARAM,
1908
             * but this is only for the default provider where we can get away
1909
             * with this. This is necessary here for backwards compatibility
1910
             * reasons.
1911
             */
1912
8.95k
            params[0] = OSSL_PARAM_construct_octet_ptr("legacy-object",
1913
8.95k
                &pk->pkey.ptr, sizeof(pk->pkey.ptr));
1914
8.95k
            p = params;
1915
8.95k
        } else if (propquery != NULL) {
1916
0
            params[0] = OSSL_PARAM_construct_utf8_string(
1917
0
                OSSL_PKEY_PARAM_PROPERTIES, (char *)propquery, 0);
1918
0
            p = params;
1919
0
        }
1920
8.95k
        keydata = evp_keymgmt_newdata(tmp_keymgmt, p);
1921
8.95k
        if (keydata == NULL)
1922
0
            goto end;
1923
1924
        /*
1925
         * We skip the export if the key data we got back is actually the same
1926
         * as the low level object we passed in
1927
         */
1928
8.95k
        if (keydata != pk->pkey.ptr
1929
0
            && !pk->ameth->export_to(pk, keydata, tmp_keymgmt->import, libctx, propquery)) {
1930
0
            evp_keymgmt_freedata(tmp_keymgmt, keydata);
1931
0
            keydata = NULL;
1932
0
            goto end;
1933
0
        }
1934
1935
        /*
1936
         * If the dirty counter changed since last time, then clear the
1937
         * operation cache.  In that case, we know that |i| is zero.  Just
1938
         * in case this is a re-export, we increment then decrement the
1939
         * keymgmt reference counter.
1940
         */
1941
8.95k
        if (!EVP_KEYMGMT_up_ref(tmp_keymgmt)) { /* refcnt++ */
1942
0
            evp_keymgmt_freedata(tmp_keymgmt, keydata);
1943
0
            keydata = NULL;
1944
0
            goto end;
1945
0
        }
1946
1947
8.95k
        if (!CRYPTO_THREAD_write_lock(pk->lock))
1948
0
            goto end;
1949
1950
8.95k
        if (pk->ameth->dirty_cnt(pk) != pk->dirty_cnt_copy)
1951
0
            evp_keymgmt_util_clear_operation_cache(pk);
1952
1953
8.95k
        EVP_KEYMGMT_free(tmp_keymgmt); /* refcnt-- */
1954
1955
        /* Check to make sure some other thread didn't get there first */
1956
8.95k
        op = evp_keymgmt_util_find_operation_cache(pk, tmp_keymgmt, selection);
1957
8.95k
        if (op != NULL && op->keymgmt != NULL) {
1958
0
            void *tmp_keydata = op->keydata;
1959
1960
0
            CRYPTO_THREAD_unlock(pk->lock);
1961
0
            evp_keymgmt_freedata(tmp_keymgmt, keydata);
1962
0
            keydata = tmp_keydata;
1963
0
            goto end;
1964
0
        }
1965
1966
        /* Add the new export to the operation cache */
1967
8.95k
        if (!evp_keymgmt_util_cache_keydata(pk, tmp_keymgmt, keydata,
1968
8.95k
                selection)) {
1969
0
            CRYPTO_THREAD_unlock(pk->lock);
1970
0
            evp_keymgmt_freedata(tmp_keymgmt, keydata);
1971
0
            keydata = NULL;
1972
0
            goto end;
1973
0
        }
1974
1975
        /* Synchronize the dirty count */
1976
8.95k
        pk->dirty_cnt_copy = pk->ameth->dirty_cnt(pk);
1977
1978
8.95k
        CRYPTO_THREAD_unlock(pk->lock);
1979
8.95k
        goto end;
1980
8.95k
    }
1981
127k
#endif /* FIPS_MODULE */
1982
1983
127k
    keydata = evp_keymgmt_util_export_to_provider(pk, tmp_keymgmt, selection);
1984
1985
153k
end:
1986
    /*
1987
     * If nothing was exported, |tmp_keymgmt| might point at a freed
1988
     * EVP_KEYMGMT, so we clear it to be safe.  It shouldn't be useful for
1989
     * the caller either way in that case.
1990
     */
1991
153k
    if (keydata == NULL)
1992
0
        tmp_keymgmt = NULL;
1993
1994
153k
    if (keymgmt != NULL && tmp_keymgmt != NULL) {
1995
153k
        *keymgmt = tmp_keymgmt;
1996
153k
        allocated_keymgmt = NULL;
1997
153k
    }
1998
1999
153k
    EVP_KEYMGMT_free(allocated_keymgmt);
2000
153k
    return keydata;
2001
127k
}
2002
2003
#ifndef FIPS_MODULE
2004
int evp_pkey_copy_downgraded(EVP_PKEY **dest, const EVP_PKEY *src)
2005
81.9k
{
2006
81.9k
    EVP_PKEY *allocpkey = NULL;
2007
2008
81.9k
    if (!ossl_assert(dest != NULL))
2009
0
        return 0;
2010
2011
81.9k
    if (evp_pkey_is_assigned(src) && evp_pkey_is_provided(src)) {
2012
81.9k
        EVP_KEYMGMT *keymgmt = src->keymgmt;
2013
81.9k
        void *keydata = src->keydata;
2014
81.9k
        int type = src->type;
2015
81.9k
        const char *keytype = NULL;
2016
2017
81.9k
        keytype = EVP_KEYMGMT_get0_name(keymgmt);
2018
2019
        /*
2020
         * If the type is EVP_PKEY_NONE, then we have a problem somewhere
2021
         * else in our code.  If it's not one of the well known EVP_PKEY_xxx
2022
         * values, it should at least be EVP_PKEY_KEYMGMT at this point.
2023
         * The check is kept as a safety measure.
2024
         */
2025
81.9k
        if (!ossl_assert(type != EVP_PKEY_NONE)) {
2026
0
            ERR_raise_data(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR,
2027
0
                "keymgmt key type = %s but legacy type = EVP_PKEY_NONE",
2028
0
                keytype);
2029
0
            return 0;
2030
0
        }
2031
2032
        /* Prefer the legacy key type name for error reporting */
2033
81.9k
        if (type != EVP_PKEY_KEYMGMT)
2034
81.9k
            keytype = OBJ_nid2sn(type);
2035
2036
        /* Make sure we have a clean slate to copy into */
2037
81.9k
        if (*dest == NULL) {
2038
81.9k
            allocpkey = *dest = EVP_PKEY_new();
2039
81.9k
            if (*dest == NULL) {
2040
0
                ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
2041
0
                return 0;
2042
0
            }
2043
81.9k
        } else {
2044
0
            evp_pkey_free_it(*dest);
2045
0
        }
2046
2047
81.9k
        if (EVP_PKEY_set_type(*dest, type)) {
2048
            /* If the key is typed but empty, we're done */
2049
81.9k
            if (keydata == NULL)
2050
0
                return 1;
2051
2052
81.9k
            if ((*dest)->ameth->import_from == NULL) {
2053
0
                ERR_raise_data(ERR_LIB_EVP, EVP_R_NO_IMPORT_FUNCTION,
2054
0
                    "key type = %s", keytype);
2055
81.9k
            } else {
2056
                /*
2057
                 * We perform the export in the same libctx as the keymgmt
2058
                 * that we are using.
2059
                 */
2060
81.9k
                OSSL_LIB_CTX *libctx = ossl_provider_libctx(keymgmt->prov);
2061
81.9k
                EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_from_pkey(libctx, *dest, NULL);
2062
2063
81.9k
                if (pctx == NULL)
2064
81.9k
                    ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
2065
2066
81.9k
                if (pctx != NULL
2067
81.9k
                    && evp_keymgmt_export(keymgmt, keydata,
2068
81.9k
                        OSSL_KEYMGMT_SELECT_ALL,
2069
81.9k
                        (*dest)->ameth->import_from,
2070
81.9k
                        pctx)) {
2071
                    /* Synchronize the dirty count */
2072
81.9k
                    (*dest)->dirty_cnt_copy = (*dest)->ameth->dirty_cnt(*dest);
2073
2074
81.9k
                    EVP_PKEY_CTX_free(pctx);
2075
81.9k
                    return 1;
2076
81.9k
                }
2077
0
                EVP_PKEY_CTX_free(pctx);
2078
0
            }
2079
2080
0
            ERR_raise_data(ERR_LIB_EVP, EVP_R_KEYMGMT_EXPORT_FAILURE,
2081
0
                "key type = %s", keytype);
2082
0
        }
2083
81.9k
    }
2084
2085
0
    if (allocpkey != NULL) {
2086
0
        EVP_PKEY_free(allocpkey);
2087
0
        *dest = NULL;
2088
0
    }
2089
0
    return 0;
2090
81.9k
}
2091
2092
void *evp_pkey_get_legacy(EVP_PKEY *pk)
2093
660k
{
2094
660k
    EVP_PKEY *tmp_copy = NULL;
2095
660k
    void *ret = NULL;
2096
2097
660k
    if (!ossl_assert(pk != NULL))
2098
0
        return NULL;
2099
2100
    /*
2101
     * If this isn't an assigned provider side key, we just use any existing
2102
     * origin legacy key.
2103
     */
2104
660k
    if (!evp_pkey_is_assigned(pk))
2105
0
        return NULL;
2106
660k
    if (!evp_pkey_is_provided(pk))
2107
578k
        return pk->pkey.ptr;
2108
2109
81.9k
    if (!CRYPTO_THREAD_read_lock(pk->lock))
2110
0
        return NULL;
2111
2112
81.9k
    ret = pk->legacy_cache_pkey.ptr;
2113
2114
81.9k
    if (!CRYPTO_THREAD_unlock(pk->lock))
2115
0
        return NULL;
2116
2117
81.9k
    if (ret != NULL)
2118
0
        return ret;
2119
2120
81.9k
    if (!evp_pkey_copy_downgraded(&tmp_copy, pk))
2121
0
        goto err;
2122
2123
81.9k
    if (!CRYPTO_THREAD_write_lock(pk->lock))
2124
0
        goto err;
2125
2126
    /* Check again in case some other thread has updated it in the meantime */
2127
81.9k
    ret = pk->legacy_cache_pkey.ptr;
2128
81.9k
    if (ret == NULL) {
2129
        /* Steal the legacy key reference from the temporary copy */
2130
81.9k
        ret = pk->legacy_cache_pkey.ptr = tmp_copy->pkey.ptr;
2131
81.9k
        tmp_copy->pkey.ptr = NULL;
2132
81.9k
    }
2133
2134
81.9k
    if (!CRYPTO_THREAD_unlock(pk->lock)) {
2135
0
        ret = NULL;
2136
0
        goto err;
2137
0
    }
2138
2139
81.9k
err:
2140
81.9k
    EVP_PKEY_free(tmp_copy);
2141
2142
81.9k
    return ret;
2143
81.9k
}
2144
#endif /* FIPS_MODULE */
2145
2146
int EVP_PKEY_get_bn_param(const EVP_PKEY *pkey, const char *key_name,
2147
    BIGNUM **bn)
2148
5.61k
{
2149
5.61k
    int ret = 0;
2150
5.61k
    OSSL_PARAM params[2];
2151
5.61k
    unsigned char buffer[2048];
2152
5.61k
    unsigned char *buf = NULL;
2153
5.61k
    size_t buf_sz = 0;
2154
2155
5.61k
    if (key_name == NULL
2156
5.61k
        || bn == NULL)
2157
0
        return 0;
2158
2159
5.61k
    memset(buffer, 0, sizeof(buffer));
2160
5.61k
    params[0] = OSSL_PARAM_construct_BN(key_name, buffer, sizeof(buffer));
2161
5.61k
    params[1] = OSSL_PARAM_construct_end();
2162
5.61k
    if (!EVP_PKEY_get_params(pkey, params)) {
2163
0
        if (!OSSL_PARAM_modified(params) || params[0].return_size == 0)
2164
0
            return 0;
2165
0
        buf_sz = params[0].return_size;
2166
        /*
2167
         * If it failed because the buffer was too small then allocate the
2168
         * required buffer size and retry.
2169
         */
2170
0
        buf = OPENSSL_zalloc(buf_sz);
2171
0
        if (buf == NULL)
2172
0
            return 0;
2173
0
        params[0].data = buf;
2174
0
        params[0].data_size = buf_sz;
2175
2176
0
        if (!EVP_PKEY_get_params(pkey, params))
2177
0
            goto err;
2178
0
    }
2179
    /* Fail if the param was not found */
2180
5.61k
    if (!OSSL_PARAM_modified(params))
2181
0
        goto err;
2182
5.61k
    ret = OSSL_PARAM_get_BN(params, bn);
2183
5.61k
err:
2184
5.61k
    if (buf != NULL) {
2185
0
        if (OSSL_PARAM_modified(params))
2186
0
            OPENSSL_clear_free(buf, buf_sz);
2187
0
        else
2188
0
            OPENSSL_free(buf);
2189
5.61k
    } else if (OSSL_PARAM_modified(params)) {
2190
5.61k
        OPENSSL_cleanse(buffer, params[0].data_size);
2191
5.61k
    }
2192
5.61k
    return ret;
2193
5.61k
}
2194
2195
int EVP_PKEY_get_octet_string_param(const EVP_PKEY *pkey, const char *key_name,
2196
    unsigned char *buf, size_t max_buf_sz,
2197
    size_t *out_len)
2198
427k
{
2199
427k
    OSSL_PARAM params[2];
2200
427k
    int ret1 = 0, ret2 = 0;
2201
2202
427k
    if (key_name == NULL)
2203
0
        return 0;
2204
2205
427k
    params[0] = OSSL_PARAM_construct_octet_string(key_name, buf, max_buf_sz);
2206
427k
    params[1] = OSSL_PARAM_construct_end();
2207
427k
    if ((ret1 = EVP_PKEY_get_params(pkey, params)))
2208
427k
        ret2 = OSSL_PARAM_modified(params);
2209
427k
    if (ret2 && out_len != NULL)
2210
213k
        *out_len = params[0].return_size;
2211
427k
    return ret1 && ret2;
2212
427k
}
2213
2214
int EVP_PKEY_get_utf8_string_param(const EVP_PKEY *pkey, const char *key_name,
2215
    char *str, size_t max_buf_sz,
2216
    size_t *out_len)
2217
31.5k
{
2218
31.5k
    OSSL_PARAM params[2];
2219
31.5k
    int ret1 = 0, ret2 = 0;
2220
2221
31.5k
    if (key_name == NULL)
2222
0
        return 0;
2223
2224
31.5k
    params[0] = OSSL_PARAM_construct_utf8_string(key_name, str, max_buf_sz);
2225
31.5k
    params[1] = OSSL_PARAM_construct_end();
2226
31.5k
    if ((ret1 = EVP_PKEY_get_params(pkey, params)))
2227
31.5k
        ret2 = OSSL_PARAM_modified(params);
2228
31.5k
    if (ret2 && out_len != NULL)
2229
0
        *out_len = params[0].return_size;
2230
2231
31.5k
    if (ret2 && params[0].return_size == max_buf_sz)
2232
        /* There was no space for a NUL byte */
2233
0
        return 0;
2234
    /* Add a terminating NUL byte for good measure */
2235
31.5k
    if (ret2 && str != NULL)
2236
31.5k
        str[params[0].return_size] = '\0';
2237
2238
31.5k
    return ret1 && ret2;
2239
31.5k
}
2240
2241
int EVP_PKEY_get_int_param(const EVP_PKEY *pkey, const char *key_name,
2242
    int *out)
2243
2
{
2244
2
    OSSL_PARAM params[2];
2245
2246
2
    if (key_name == NULL)
2247
0
        return 0;
2248
2249
2
    params[0] = OSSL_PARAM_construct_int(key_name, out);
2250
2
    params[1] = OSSL_PARAM_construct_end();
2251
2
    return EVP_PKEY_get_params(pkey, params)
2252
2
        && OSSL_PARAM_modified(params);
2253
2
}
2254
2255
int EVP_PKEY_get_size_t_param(const EVP_PKEY *pkey, const char *key_name,
2256
    size_t *out)
2257
0
{
2258
0
    OSSL_PARAM params[2];
2259
2260
0
    if (key_name == NULL)
2261
0
        return 0;
2262
2263
0
    params[0] = OSSL_PARAM_construct_size_t(key_name, out);
2264
0
    params[1] = OSSL_PARAM_construct_end();
2265
0
    return EVP_PKEY_get_params(pkey, params)
2266
0
        && OSSL_PARAM_modified(params);
2267
0
}
2268
2269
int EVP_PKEY_set_int_param(EVP_PKEY *pkey, const char *key_name, int in)
2270
0
{
2271
0
    OSSL_PARAM params[2];
2272
2273
0
    if (key_name == NULL)
2274
0
        return 0;
2275
2276
0
    params[0] = OSSL_PARAM_construct_int(key_name, &in);
2277
0
    params[1] = OSSL_PARAM_construct_end();
2278
0
    return EVP_PKEY_set_params(pkey, params);
2279
0
}
2280
2281
int EVP_PKEY_set_size_t_param(EVP_PKEY *pkey, const char *key_name, size_t in)
2282
0
{
2283
0
    OSSL_PARAM params[2];
2284
2285
0
    if (key_name == NULL)
2286
0
        return 0;
2287
2288
0
    params[0] = OSSL_PARAM_construct_size_t(key_name, &in);
2289
0
    params[1] = OSSL_PARAM_construct_end();
2290
0
    return EVP_PKEY_set_params(pkey, params);
2291
0
}
2292
2293
int EVP_PKEY_set_bn_param(EVP_PKEY *pkey, const char *key_name,
2294
    const BIGNUM *bn)
2295
0
{
2296
0
    OSSL_PARAM params[2];
2297
0
    unsigned char buffer[2048];
2298
0
    int bsize = 0;
2299
2300
0
    if (key_name == NULL
2301
0
        || bn == NULL
2302
0
        || pkey == NULL
2303
0
        || !evp_pkey_is_assigned(pkey))
2304
0
        return 0;
2305
2306
0
    bsize = BN_num_bytes(bn);
2307
0
    if (!ossl_assert(bsize <= (int)sizeof(buffer)))
2308
0
        return 0;
2309
2310
0
    if (BN_bn2nativepad(bn, buffer, bsize) < 0)
2311
0
        return 0;
2312
0
    params[0] = OSSL_PARAM_construct_BN(key_name, buffer, bsize);
2313
0
    params[1] = OSSL_PARAM_construct_end();
2314
0
    return EVP_PKEY_set_params(pkey, params);
2315
0
}
2316
2317
int EVP_PKEY_set_utf8_string_param(EVP_PKEY *pkey, const char *key_name,
2318
    const char *str)
2319
0
{
2320
0
    OSSL_PARAM params[2];
2321
2322
0
    if (key_name == NULL)
2323
0
        return 0;
2324
2325
0
    params[0] = OSSL_PARAM_construct_utf8_string(key_name, (char *)str, 0);
2326
0
    params[1] = OSSL_PARAM_construct_end();
2327
0
    return EVP_PKEY_set_params(pkey, params);
2328
0
}
2329
2330
int EVP_PKEY_set_octet_string_param(EVP_PKEY *pkey, const char *key_name,
2331
    const unsigned char *buf, size_t bsize)
2332
46.2k
{
2333
46.2k
    OSSL_PARAM params[2];
2334
2335
46.2k
    if (key_name == NULL)
2336
0
        return 0;
2337
2338
46.2k
    params[0] = OSSL_PARAM_construct_octet_string(key_name,
2339
46.2k
        (unsigned char *)buf, bsize);
2340
46.2k
    params[1] = OSSL_PARAM_construct_end();
2341
46.2k
    return EVP_PKEY_set_params(pkey, params);
2342
46.2k
}
2343
2344
const OSSL_PARAM *EVP_PKEY_settable_params(const EVP_PKEY *pkey)
2345
0
{
2346
0
    return (pkey != NULL && evp_pkey_is_provided(pkey))
2347
0
        ? EVP_KEYMGMT_settable_params(pkey->keymgmt)
2348
0
        : NULL;
2349
0
}
2350
2351
int EVP_PKEY_set_params(EVP_PKEY *pkey, OSSL_PARAM params[])
2352
46.2k
{
2353
46.2k
    if (pkey != NULL) {
2354
46.2k
        if (evp_pkey_is_provided(pkey)) {
2355
46.2k
            pkey->dirty_cnt++;
2356
46.2k
            return evp_keymgmt_set_params(pkey->keymgmt, pkey->keydata, params);
2357
46.2k
        }
2358
46.2k
#ifndef FIPS_MODULE
2359
        /*
2360
         * We will hopefully never find the need to set individual data in
2361
         * EVP_PKEYs with a legacy internal key, but we can't be entirely
2362
         * sure.  This bit of code can be enabled if we find the need.  If
2363
         * not, it can safely be removed when #legacy support is removed.
2364
         */
2365
#if 0
2366
        else if (evp_pkey_is_legacy(pkey)) {
2367
            return evp_pkey_set_params_to_ctrl(pkey, params);
2368
        }
2369
#endif
2370
46.2k
#endif
2371
46.2k
    }
2372
46.2k
    ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY);
2373
0
    return 0;
2374
46.2k
}
2375
2376
const OSSL_PARAM *EVP_PKEY_gettable_params(const EVP_PKEY *pkey)
2377
0
{
2378
0
    return (pkey != NULL && evp_pkey_is_provided(pkey))
2379
0
        ? EVP_KEYMGMT_gettable_params(pkey->keymgmt)
2380
0
        : NULL;
2381
0
}
2382
2383
int EVP_PKEY_get_params(const EVP_PKEY *pkey, OSSL_PARAM params[])
2384
464k
{
2385
464k
    if (pkey != NULL) {
2386
464k
        if (evp_pkey_is_provided(pkey))
2387
462k
            return evp_keymgmt_get_params(pkey->keymgmt, pkey->keydata, params) > 0;
2388
2.19k
#ifndef FIPS_MODULE
2389
2.19k
        else if (evp_pkey_is_legacy(pkey))
2390
2.19k
            return evp_pkey_get_params_to_ctrl(pkey, params) > 0;
2391
464k
#endif
2392
464k
    }
2393
464k
    ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY);
2394
0
    return 0;
2395
464k
}
2396
2397
#ifndef FIPS_MODULE
2398
int EVP_PKEY_get_ec_point_conv_form(const EVP_PKEY *pkey)
2399
0
{
2400
0
    char name[80];
2401
0
    size_t name_len;
2402
2403
0
    if (pkey == NULL)
2404
0
        return 0;
2405
2406
0
    if (pkey->keymgmt == NULL
2407
0
        || pkey->keydata == NULL) {
2408
0
#ifndef OPENSSL_NO_EC
2409
        /* Might work through the legacy route */
2410
0
        const EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey);
2411
2412
0
        if (ec != NULL)
2413
0
            return EC_KEY_get_conv_form(ec);
2414
0
#endif
2415
0
        return 0;
2416
0
    }
2417
2418
0
    if (!EVP_PKEY_get_utf8_string_param(pkey,
2419
0
            OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT,
2420
0
            name, sizeof(name), &name_len))
2421
0
        return 0;
2422
2423
0
    if (strcmp(name, "uncompressed") == 0)
2424
0
        return POINT_CONVERSION_UNCOMPRESSED;
2425
2426
0
    if (strcmp(name, "compressed") == 0)
2427
0
        return POINT_CONVERSION_COMPRESSED;
2428
2429
0
    if (strcmp(name, "hybrid") == 0)
2430
0
        return POINT_CONVERSION_HYBRID;
2431
2432
0
    return 0;
2433
0
}
2434
2435
int EVP_PKEY_get_field_type(const EVP_PKEY *pkey)
2436
0
{
2437
0
    char fstr[80];
2438
0
    size_t fstrlen;
2439
2440
0
    if (pkey == NULL)
2441
0
        return 0;
2442
2443
0
    if (pkey->keymgmt == NULL
2444
0
        || pkey->keydata == NULL) {
2445
0
#ifndef OPENSSL_NO_EC
2446
        /* Might work through the legacy route */
2447
0
        const EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey);
2448
0
        const EC_GROUP *grp;
2449
2450
0
        if (ec == NULL)
2451
0
            return 0;
2452
0
        grp = EC_KEY_get0_group(ec);
2453
0
        if (grp == NULL)
2454
0
            return 0;
2455
2456
0
        return EC_GROUP_get_field_type(grp);
2457
#else
2458
        return 0;
2459
#endif
2460
0
    }
2461
2462
0
    if (!EVP_PKEY_get_utf8_string_param(pkey, OSSL_PKEY_PARAM_EC_FIELD_TYPE,
2463
0
            fstr, sizeof(fstr), &fstrlen))
2464
0
        return 0;
2465
2466
0
    if (strcmp(fstr, SN_X9_62_prime_field) == 0)
2467
0
        return NID_X9_62_prime_field;
2468
0
    else if (strcmp(fstr, SN_X9_62_characteristic_two_field))
2469
0
        return NID_X9_62_characteristic_two_field;
2470
2471
0
    return 0;
2472
0
}
2473
#endif