Coverage Report

Created: 2025-12-31 06:58

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