Coverage Report

Created: 2025-11-16 06:40

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