Coverage Report

Created: 2026-09-12 06:55

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