Coverage Report

Created: 2025-11-16 06:40

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