Coverage Report

Created: 2025-12-04 06:33

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