Coverage Report

Created: 2026-02-22 06:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/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
#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
0
#define SELECT_PARAMETERS OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
58
59
#ifndef FIPS_MODULE
60
int EVP_PKEY_get_bits(const EVP_PKEY *pkey)
61
0
{
62
0
    int size = 0;
63
64
0
    if (pkey != NULL) {
65
0
        size = pkey->cache.bits;
66
0
        if (pkey->ameth != NULL && pkey->ameth->pkey_bits != NULL)
67
0
            size = pkey->ameth->pkey_bits(pkey);
68
0
    }
69
0
    if (size <= 0) {
70
0
        ERR_raise(ERR_LIB_EVP, EVP_R_UNKNOWN_BITS);
71
0
        return 0;
72
0
    }
73
0
    return size;
74
0
}
75
76
int EVP_PKEY_get_security_bits(const EVP_PKEY *pkey)
77
0
{
78
0
    int size = 0;
79
80
0
    if (pkey != NULL) {
81
0
        size = pkey->cache.security_bits;
82
0
        if (pkey->ameth != NULL && pkey->ameth->pkey_security_bits != NULL)
83
0
            size = pkey->ameth->pkey_security_bits(pkey);
84
0
    }
85
0
    if (size <= 0) {
86
0
        ERR_raise(ERR_LIB_EVP, EVP_R_UNKNOWN_SECURITY_BITS);
87
0
        return 0;
88
0
    }
89
0
    return size;
90
0
}
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
0
{
133
    /*
134
     * Clean up legacy stuff from this function when legacy support is gone.
135
     */
136
137
0
    EVP_PKEY *downgraded_from = NULL;
138
0
    int ok = 0;
139
140
0
#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
0
    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
0
#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
0
    if (evp_pkey_is_blank(to)) {
165
0
#ifndef FIPS_MODULE
166
0
        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
0
#endif /* !FIPS_MODULE */
171
0
        {
172
0
            if (EVP_PKEY_set_type_by_keymgmt(to, from->keymgmt) == 0)
173
0
                goto end;
174
0
        }
175
0
    }
176
0
#ifndef FIPS_MODULE
177
0
    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
0
#endif /* !FIPS_MODULE */
184
185
0
    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
0
    if (!EVP_PKEY_missing_parameters(to)) {
191
0
        if (EVP_PKEY_parameters_eq(to, from) == 1)
192
0
            ok = 1;
193
0
        else
194
0
            ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_PARAMETERS);
195
0
        goto end;
196
0
    }
197
198
    /* For purely provided keys, we just call the keymgmt utility */
199
0
    if (to->keymgmt != NULL && from->keymgmt != NULL) {
200
0
        ok = evp_keymgmt_util_copy(to, (EVP_PKEY *)from, SELECT_PARAMETERS);
201
0
        goto end;
202
0
    }
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
0
end:
236
0
    EVP_PKEY_free(downgraded_from);
237
0
    return ok;
238
0
}
239
240
int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey)
241
0
{
242
0
    if (pkey != NULL) {
243
#ifdef FIPS_MODULE
244
        return !evp_keymgmt_util_has((EVP_PKEY *)pkey, SELECT_PARAMETERS);
245
#else
246
0
        if (pkey->keymgmt != NULL)
247
0
            return !evp_keymgmt_util_has((EVP_PKEY *)pkey, SELECT_PARAMETERS);
248
0
        if (pkey->ameth != NULL && pkey->ameth->param_missing != NULL)
249
0
            return pkey->ameth->param_missing(pkey);
250
0
#endif /* FIPS_MODULE */
251
0
    }
252
0
    return 0;
253
0
}
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
0
{
263
#ifdef FIPS_MODULE
264
    return evp_keymgmt_util_match((EVP_PKEY *)a, (EVP_PKEY *)b, selection);
265
#else
266
0
    EVP_KEYMGMT *keymgmt1 = NULL, *keymgmt2 = NULL;
267
0
    void *keydata1 = NULL, *keydata2 = NULL, *tmp_keydata = NULL;
268
269
    /* If none of them are provided, this function shouldn't have been called */
270
0
    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
0
    if (evp_pkey_is_provided(a) && evp_pkey_is_provided(b))
275
0
        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
0
    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
0
    if (evp_pkey_is_legacy(b)
285
0
        && !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
0
    keymgmt1 = a->keymgmt;
294
0
    keydata1 = a->keydata;
295
0
    keymgmt2 = b->keymgmt;
296
0
    keydata2 = b->keydata;
297
298
0
    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
0
    if (tmp_keydata == NULL && keymgmt1 != NULL && keymgmt1->match != NULL) {
306
0
        tmp_keydata = evp_pkey_export_to_provider((EVP_PKEY *)b, NULL, &keymgmt1, NULL);
307
0
        if (tmp_keydata != NULL) {
308
0
            keymgmt2 = keymgmt1;
309
0
            keydata2 = tmp_keydata;
310
0
        }
311
0
    }
312
313
    /* If we still don't have matching keymgmt implementations, we give up */
314
0
    if (keymgmt1 != keymgmt2)
315
0
        return -2;
316
317
    /* If the keymgmt implementations are NULL, the export failed */
318
0
    if (keymgmt1 == NULL)
319
0
        return -2;
320
321
0
    return evp_keymgmt_match(keymgmt1, keydata1, keydata2, selection);
322
0
#endif /* FIPS_MODULE */
323
0
}
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
0
{
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
0
    if (a->keymgmt != NULL || b->keymgmt != NULL)
345
0
        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
0
{
367
    /*
368
     * This will just call evp_keymgmt_util_match when legacy support
369
     * is gone.
370
     */
371
372
    /* Trivial shortcuts */
373
0
    if (a == b)
374
0
        return 1;
375
0
    if (a == NULL || b == NULL)
376
0
        return 0;
377
378
0
#ifndef FIPS_MODULE
379
0
    if (a->keymgmt != NULL || b->keymgmt != NULL)
380
0
#endif /* !FIPS_MODULE */
381
0
    {
382
0
        int selection = SELECT_PARAMETERS;
383
384
0
        if (evp_keymgmt_util_has((EVP_PKEY *)a, OSSL_KEYMGMT_SELECT_PUBLIC_KEY)
385
0
            && evp_keymgmt_util_has((EVP_PKEY *)b, OSSL_KEYMGMT_SELECT_PUBLIC_KEY))
386
0
            selection |= OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
387
0
        else
388
0
            selection |= OSSL_KEYMGMT_SELECT_KEYPAIR;
389
0
        return evp_pkey_cmp_any(a, b, selection);
390
0
    }
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
0
{
423
0
    EVP_PKEY *pkey = NULL;
424
0
    EVP_PKEY_CTX *ctx = NULL;
425
0
    int result = 0;
426
427
0
    ctx = EVP_PKEY_CTX_new_from_name(libctx,
428
0
        strtype != NULL ? strtype
429
0
                        : OBJ_nid2sn(nidtype),
430
0
        propq);
431
0
    if (ctx == NULL)
432
0
        goto err;
433
    /* May fail if no provider available */
434
0
    ERR_set_mark();
435
0
    if (EVP_PKEY_fromdata_init(ctx) == 1) {
436
0
        OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
437
438
0
        ERR_clear_last_mark();
439
0
        params[0] = OSSL_PARAM_construct_octet_string(
440
0
            key_is_priv ? OSSL_PKEY_PARAM_PRIV_KEY
441
0
                        : OSSL_PKEY_PARAM_PUB_KEY,
442
0
            (void *)key, len);
443
444
0
        if (EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEYPAIR, params) != 1) {
445
0
            ERR_raise(ERR_LIB_EVP, EVP_R_KEY_SETUP_FAILED);
446
0
            goto err;
447
0
        }
448
449
0
        EVP_PKEY_CTX_free(ctx);
450
451
0
        return pkey;
452
0
    }
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
0
err:
496
0
    if (!result) {
497
0
        EVP_PKEY_free(pkey);
498
0
        pkey = NULL;
499
0
    }
500
0
    EVP_PKEY_CTX_free(ctx);
501
0
    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
0
{
509
0
    return new_raw_key_int(libctx, keytype, propq, EVP_PKEY_NONE, priv,
510
0
        len, 1);
511
0
}
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
0
{
526
0
    return new_raw_key_int(libctx, keytype, propq, EVP_PKEY_NONE, pub,
527
0
        len, 0);
528
0
}
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
0
{
694
0
    return pkey_set_type(pkey, type, NULL, -1, NULL);
695
0
}
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
0
{
705
0
    switch (pkey->type) {
706
0
    case EVP_PKEY_RSA:
707
0
    case EVP_PKEY_RSA_PSS:
708
0
        pkey->foreign = pkey->pkey.rsa != NULL
709
0
            && ossl_rsa_is_foreign(pkey->pkey.rsa);
710
0
        break;
711
0
#ifndef OPENSSL_NO_EC
712
0
    case EVP_PKEY_SM2:
713
0
        break;
714
0
    case EVP_PKEY_EC:
715
0
        pkey->foreign = pkey->pkey.ec != NULL
716
0
            && ossl_ec_key_is_foreign(pkey->pkey.ec);
717
0
        break;
718
0
#endif
719
0
#ifndef OPENSSL_NO_DSA
720
0
    case EVP_PKEY_DSA:
721
0
        pkey->foreign = pkey->pkey.dsa != NULL
722
0
            && ossl_dsa_is_foreign(pkey->pkey.dsa);
723
0
        break;
724
0
#endif
725
0
#ifndef OPENSSL_NO_DH
726
0
    case EVP_PKEY_DH:
727
0
        pkey->foreign = pkey->pkey.dh != NULL
728
0
            && ossl_dh_is_foreign(pkey->pkey.dh);
729
0
        break;
730
0
#endif
731
0
    default:
732
0
        pkey->foreign = 0;
733
0
        break;
734
0
    }
735
0
}
736
737
int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key)
738
0
{
739
0
#ifndef OPENSSL_NO_EC
740
0
    int pktype;
741
742
0
    pktype = EVP_PKEY_type(type);
743
0
    if ((key != NULL) && (pktype == EVP_PKEY_EC || pktype == EVP_PKEY_SM2)) {
744
0
        const EC_GROUP *group = EC_KEY_get0_group(key);
745
746
0
        if (group != NULL) {
747
0
            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
0
            if (curve == NID_sm2 && pktype == EVP_PKEY_EC)
754
0
                type = EVP_PKEY_SM2;
755
0
            else if (curve != NID_sm2 && pktype == EVP_PKEY_SM2)
756
0
                type = EVP_PKEY_EC;
757
0
        }
758
0
    }
759
0
#endif
760
761
0
    if (pkey == NULL || !EVP_PKEY_set_type(pkey, type))
762
0
        return 0;
763
764
0
    pkey->pkey.ptr = key;
765
0
    detect_foreign_key(pkey);
766
767
0
    return (key != NULL);
768
0
}
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
0
{
835
0
    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
0
    return evp_pkey_get_legacy((EVP_PKEY *)pkey);
840
0
}
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
0
{
863
0
    DSA *ret = evp_pkey_get0_DSA_int(pkey);
864
865
0
    if (ret != NULL && !DSA_up_ref(ret))
866
0
        return NULL;
867
868
0
    return ret;
869
0
}
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
0
{
875
0
    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
0
    return evp_pkey_get_legacy((EVP_PKEY *)pkey);
880
0
}
881
882
static ECX_KEY *evp_pkey_get1_ECX_KEY(EVP_PKEY *pkey, int type)
883
0
{
884
0
    ECX_KEY *ret = (ECX_KEY *)evp_pkey_get0_ECX_KEY(pkey, type);
885
886
0
    if (ret != NULL && !ossl_ecx_key_up_ref(ret))
887
0
        ret = NULL;
888
0
    return ret;
889
0
}
890
891
#define IMPLEMENT_ECX_VARIANT(NAME)                          \
892
    ECX_KEY *ossl_evp_pkey_get1_##NAME(EVP_PKEY *pkey)       \
893
0
    {                                                        \
894
0
        return evp_pkey_get1_ECX_KEY(pkey, EVP_PKEY_##NAME); \
895
0
    }
Unexecuted instantiation: ossl_evp_pkey_get1_X25519
Unexecuted instantiation: ossl_evp_pkey_get1_X448
Unexecuted instantiation: ossl_evp_pkey_get1_ED25519
Unexecuted instantiation: ossl_evp_pkey_get1_ED448
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
0
{
946
0
    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
0
    return evp_pkey_get_legacy((EVP_PKEY *)pkey);
951
0
}
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
0
{
960
0
    DH *ret = evp_pkey_get0_DH_int(pkey);
961
962
0
    if (ret != NULL && !DH_up_ref(ret))
963
0
        ret = NULL;
964
965
0
    return ret;
966
0
}
967
#endif
968
969
int EVP_PKEY_get_id(const EVP_PKEY *pkey)
970
0
{
971
0
    return pkey->type;
972
0
}
973
974
int EVP_PKEY_get_base_id(const EVP_PKEY *pkey)
975
0
{
976
0
    return EVP_PKEY_type(pkey->type);
977
0
}
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
0
{
1008
0
    int type;
1009
0
    size_t i;
1010
1011
0
    for (i = 0; i < OSSL_NELEM(standard_name2type); i++) {
1012
0
        if (OPENSSL_strcasecmp(name, standard_name2type[i].ptr) == 0)
1013
0
            return (int)standard_name2type[i].id;
1014
0
    }
1015
1016
0
    if ((type = EVP_PKEY_type(OBJ_sn2nid(name))) != NID_undef)
1017
0
        return type;
1018
0
    return EVP_PKEY_type(OBJ_ln2nid(name));
1019
0
}
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
0
{
1035
0
    if (pkey == NULL)
1036
0
        return 0;
1037
0
    if (pkey->keymgmt == NULL)
1038
0
        return pkey->type == evp_pkey_name2type(name);
1039
0
    return EVP_KEYMGMT_is_a(pkey->keymgmt, name);
1040
0
}
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
0
{
1060
0
    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
0
    } else {
1080
0
        const OSSL_PROVIDER *prov = EVP_KEYMGMT_get0_provider(pkey->keymgmt);
1081
0
        OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
1082
0
        EVP_SIGNATURE *sig;
1083
0
        const char *name;
1084
1085
0
        name = evp_keymgmt_util_query_operation_name(pkey->keymgmt,
1086
0
            OSSL_OP_SIGNATURE);
1087
0
        sig = EVP_SIGNATURE_fetch(libctx, name, NULL);
1088
0
        if (sig != NULL) {
1089
0
            EVP_SIGNATURE_free(sig);
1090
0
            return 1;
1091
0
        }
1092
0
    }
1093
0
    return 0;
1094
0
}
1095
1096
static int print_reset_indent(BIO **out, int pop_f_prefix, long saved_indent)
1097
0
{
1098
0
    BIO_set_indent(*out, saved_indent);
1099
0
    if (pop_f_prefix) {
1100
0
        BIO *next = BIO_pop(*out);
1101
1102
0
        BIO_free(*out);
1103
0
        *out = next;
1104
0
    }
1105
0
    return 1;
1106
0
}
1107
1108
static int print_set_indent(BIO **out, int *pop_f_prefix, long *saved_indent,
1109
    long indent)
1110
0
{
1111
0
    *pop_f_prefix = 0;
1112
0
    *saved_indent = 0;
1113
0
    if (indent > 0) {
1114
0
        long i = BIO_get_indent(*out);
1115
1116
0
        *saved_indent = (i < 0 ? 0 : i);
1117
0
        if (BIO_set_indent(*out, indent) <= 0) {
1118
0
            BIO *prefbio = BIO_new(BIO_f_prefix());
1119
1120
0
            if (prefbio == NULL)
1121
0
                return 0;
1122
0
            *out = BIO_push(prefbio, *out);
1123
0
            *pop_f_prefix = 1;
1124
0
        }
1125
0
        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
0
    }
1130
0
    return 1;
1131
0
}
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
0
{
1149
0
    int pop_f_prefix;
1150
0
    long saved_indent;
1151
0
    OSSL_ENCODER_CTX *ctx = NULL;
1152
0
    int ret = -2; /* default to unsupported */
1153
1154
0
    if (!print_set_indent(&out, &pop_f_prefix, &saved_indent, indent))
1155
0
        return 0;
1156
1157
0
    ctx = OSSL_ENCODER_CTX_new_for_pkey(pkey, selection, "TEXT", NULL,
1158
0
        propquery);
1159
0
    if (OSSL_ENCODER_CTX_get_num_encoders(ctx) != 0)
1160
0
        ret = OSSL_ENCODER_to_bio(ctx, out);
1161
0
    OSSL_ENCODER_CTX_free(ctx);
1162
1163
0
    if (ret != -2)
1164
0
        goto end;
1165
1166
    /* legacy fallback */
1167
0
    if (legacy_print != NULL)
1168
0
        ret = legacy_print(out, pkey, 0, legacy_pctx);
1169
0
    else
1170
0
        ret = unsup_alg(out, pkey, 0, "Public Key");
1171
1172
0
end:
1173
0
    print_reset_indent(&out, pop_f_prefix, saved_indent);
1174
0
    return ret;
1175
0
}
1176
1177
int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey,
1178
    int indent, ASN1_PCTX *pctx)
1179
0
{
1180
0
    return print_pkey(pkey, out, indent, EVP_PKEY_PUBLIC_KEY, NULL,
1181
0
        (pkey->ameth != NULL ? pkey->ameth->pub_print : NULL),
1182
0
        pctx);
1183
0
}
1184
1185
int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey,
1186
    int indent, ASN1_PCTX *pctx)
1187
0
{
1188
0
    return print_pkey(pkey, out, indent, EVP_PKEY_PRIVATE_KEY, NULL,
1189
0
        (pkey->ameth != NULL ? pkey->ameth->priv_print : NULL),
1190
0
        pctx);
1191
0
}
1192
1193
int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey,
1194
    int indent, ASN1_PCTX *pctx)
1195
0
{
1196
0
    return print_pkey(pkey, out, indent, EVP_PKEY_KEY_PARAMETERS, NULL,
1197
0
        (pkey->ameth != NULL ? pkey->ameth->param_print : NULL),
1198
0
        pctx);
1199
0
}
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
0
{
1340
0
    return EVP_PKEY_get_utf8_string_param(pkey, OSSL_PKEY_PARAM_GROUP_NAME,
1341
0
        gname, gname_sz, gname_len);
1342
0
}
1343
1344
int EVP_PKEY_digestsign_supports_digest(EVP_PKEY *pkey, OSSL_LIB_CTX *libctx,
1345
    const char *name, const char *propq)
1346
0
{
1347
0
    int rv;
1348
0
    EVP_MD_CTX *ctx = NULL;
1349
1350
0
    if ((ctx = EVP_MD_CTX_new()) == NULL)
1351
0
        return -1;
1352
1353
0
    ERR_set_mark();
1354
0
    rv = EVP_DigestSignInit_ex(ctx, NULL, name, libctx,
1355
0
        propq, pkey, NULL);
1356
0
    ERR_pop_to_mark();
1357
1358
0
    EVP_MD_CTX_free(ctx);
1359
0
    return rv;
1360
0
}
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
0
{
1366
0
    if (pkey == NULL)
1367
0
        return 0;
1368
0
#ifndef FIPS_MODULE
1369
0
    if (evp_pkey_is_provided(pkey))
1370
0
#endif /* !FIPS_MODULE */
1371
0
        return EVP_PKEY_set_octet_string_param(pkey,
1372
0
            OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
1373
0
            (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
0
{
1389
0
    if (pkey == NULL)
1390
0
        return 0;
1391
0
#ifndef FIPS_MODULE
1392
0
    if (evp_pkey_is_provided(pkey))
1393
0
#endif
1394
0
    {
1395
0
        size_t return_size = OSSL_PARAM_UNMODIFIED;
1396
0
        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
0
        EVP_PKEY_get_octet_string_param(pkey,
1403
0
            OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
1404
0
            NULL, 0, &return_size);
1405
0
        if (return_size == OSSL_PARAM_UNMODIFIED)
1406
0
            return 0;
1407
1408
0
        *ppub = NULL;
1409
0
        buf = OPENSSL_malloc(return_size);
1410
0
        if (buf == NULL)
1411
0
            return 0;
1412
1413
0
        if (!EVP_PKEY_get_octet_string_param(pkey,
1414
0
                OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
1415
0
                buf, return_size, NULL)) {
1416
0
            OPENSSL_free(buf);
1417
0
            return 0;
1418
0
        }
1419
0
        *ppub = buf;
1420
0
        return return_size;
1421
0
    }
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
0
{
1437
0
    EVP_PKEY *ret = OPENSSL_zalloc(sizeof(*ret));
1438
1439
0
    if (ret == NULL)
1440
0
        return NULL;
1441
1442
0
    ret->type = EVP_PKEY_NONE;
1443
0
    ret->save_type = EVP_PKEY_NONE;
1444
1445
0
    if (!CRYPTO_NEW_REF(&ret->references, 1))
1446
0
        goto err;
1447
1448
0
    ret->lock = CRYPTO_THREAD_lock_new();
1449
0
    if (ret->lock == NULL) {
1450
0
        ERR_raise(ERR_LIB_EVP, ERR_R_CRYPTO_LIB);
1451
0
        goto err;
1452
0
    }
1453
1454
0
#ifndef FIPS_MODULE
1455
0
    ret->save_parameters = 1;
1456
0
    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
0
#endif
1461
0
    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
0
}
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
0
{
1486
0
#ifndef FIPS_MODULE
1487
0
    const EVP_PKEY_ASN1_METHOD *ameth = NULL;
1488
0
#endif
1489
1490
    /*
1491
     * The setups can't set both legacy and provider side methods.
1492
     * It is forbidden
1493
     */
1494
0
    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
0
    if (pkey != NULL) {
1500
0
        int free_it = 0;
1501
1502
0
#ifndef FIPS_MODULE
1503
0
        free_it = free_it || pkey->pkey.ptr != NULL;
1504
0
#endif
1505
0
        free_it = free_it || pkey->keydata != NULL;
1506
0
        if (free_it)
1507
0
            evp_pkey_free_it(pkey);
1508
0
#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
0
        if (pkey->type != EVP_PKEY_NONE
1514
0
            && type == pkey->save_type
1515
0
            && pkey->ameth != NULL)
1516
0
            return 1;
1517
0
#endif
1518
0
    }
1519
0
#ifndef FIPS_MODULE
1520
0
    if (str != NULL)
1521
0
        ameth = evp_pkey_asn1_find_str(str, len);
1522
0
    else if (type != EVP_PKEY_NONE)
1523
0
        ameth = evp_pkey_asn1_find(type);
1524
0
#endif
1525
1526
0
    {
1527
0
        int check = 1;
1528
1529
0
#ifndef FIPS_MODULE
1530
0
        check = check && ameth == NULL;
1531
0
#endif
1532
0
        check = check && keymgmt == NULL;
1533
0
        if (check) {
1534
0
            ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_ALGORITHM);
1535
0
            return 0;
1536
0
        }
1537
0
    }
1538
0
    if (pkey != NULL) {
1539
0
        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
0
        pkey->keymgmt = keymgmt;
1545
1546
0
        pkey->save_type = type;
1547
0
        pkey->type = type;
1548
1549
0
#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
0
        if (keymgmt == NULL)
1556
0
            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
0
        if (ameth != NULL) {
1567
0
            if (type == EVP_PKEY_NONE)
1568
0
                pkey->type = ameth->pkey_id;
1569
0
        } else {
1570
0
            pkey->type = EVP_PKEY_KEYMGMT;
1571
0
        }
1572
0
#endif
1573
0
    }
1574
0
    return 1;
1575
0
}
1576
1577
#ifndef FIPS_MODULE
1578
static void find_ameth(const char *name, void *data)
1579
0
{
1580
0
    const char **str = data;
1581
1582
    /*
1583
     * The error messages from pkey_set_type() are uninteresting here,
1584
     * and misleading.
1585
     */
1586
0
    ERR_set_mark();
1587
1588
0
    if (pkey_set_type(NULL, EVP_PKEY_NONE, name, (int)strlen(name),
1589
0
            NULL)) {
1590
0
        if (str[0] == NULL)
1591
0
            str[0] = name;
1592
0
        else if (str[1] == NULL)
1593
0
            str[1] = name;
1594
0
    }
1595
1596
0
    ERR_pop_to_mark();
1597
0
}
1598
#endif
1599
1600
int EVP_PKEY_set_type_by_keymgmt(EVP_PKEY *pkey, EVP_KEYMGMT *keymgmt)
1601
0
{
1602
0
#ifndef FIPS_MODULE
1603
0
#define EVP_PKEY_TYPE_STR str[0]
1604
0
#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
0
    const char *str[2] = { NULL, NULL };
1611
1612
0
    if (!EVP_KEYMGMT_names_do_all(keymgmt, find_ameth, &str)
1613
0
        || 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
0
    return pkey_set_type(pkey, EVP_PKEY_NONE,
1622
0
        EVP_PKEY_TYPE_STR, EVP_PKEY_TYPE_STRLEN,
1623
0
        keymgmt);
1624
1625
0
#undef EVP_PKEY_TYPE_STR
1626
0
#undef EVP_PKEY_TYPE_STRLEN
1627
0
}
1628
1629
int EVP_PKEY_up_ref(EVP_PKEY *pkey)
1630
0
{
1631
0
    int i;
1632
1633
0
    if (CRYPTO_UP_REF(&pkey->references, &i) <= 0)
1634
0
        return 0;
1635
1636
0
    REF_PRINT_COUNT("EVP_PKEY", i, pkey);
1637
0
    REF_ASSERT_ISNT(i < 2);
1638
0
    return ((i > 1) ? 1 : 0);
1639
0
}
1640
1641
EVP_PKEY *EVP_PKEY_dup(EVP_PKEY *pkey)
1642
0
{
1643
0
    EVP_PKEY *dup_pk;
1644
1645
0
    if (pkey == NULL) {
1646
0
        ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
1647
0
        return NULL;
1648
0
    }
1649
1650
0
    if ((dup_pk = EVP_PKEY_new()) == NULL)
1651
0
        return NULL;
1652
1653
0
    if (evp_pkey_is_blank(pkey))
1654
0
        goto done;
1655
1656
0
#ifndef FIPS_MODULE
1657
0
    if (evp_pkey_is_provided(pkey))
1658
0
#endif /* !FIPS_MODULE */
1659
0
    {
1660
0
        if (!evp_keymgmt_util_copy(dup_pk, pkey,
1661
0
                OSSL_KEYMGMT_SELECT_ALL))
1662
0
            goto err;
1663
0
        goto done;
1664
0
    }
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
0
done:
1685
0
#ifndef FIPS_MODULE
1686
    /* copy auxiliary data */
1687
0
    if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_EVP_PKEY,
1688
0
            &dup_pk->ex_data, &pkey->ex_data))
1689
0
        goto err;
1690
1691
0
    if (pkey->attributes != NULL) {
1692
0
        if ((dup_pk->attributes = ossl_x509at_dup(pkey->attributes)) == NULL)
1693
0
            goto err;
1694
0
    }
1695
0
#endif /* !FIPS_MODULE */
1696
0
    return dup_pk;
1697
0
err:
1698
0
    EVP_PKEY_free(dup_pk);
1699
0
    return NULL;
1700
0
}
1701
1702
#ifndef FIPS_MODULE
1703
void evp_pkey_free_legacy(EVP_PKEY *x)
1704
0
{
1705
0
    const EVP_PKEY_ASN1_METHOD *ameth = x->ameth;
1706
1707
0
    if (ameth == NULL && x->legacy_cache_pkey.ptr != NULL)
1708
0
        ameth = evp_pkey_asn1_find(x->type);
1709
1710
0
    if (ameth != NULL) {
1711
0
        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
0
            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
0
            x->pkey = x->legacy_cache_pkey;
1722
0
            x->legacy_cache_pkey.ptr = NULL;
1723
0
        }
1724
0
        if (ameth->pkey_free != NULL)
1725
0
            ameth->pkey_free(x);
1726
0
        x->pkey.ptr = NULL;
1727
0
    }
1728
0
}
1729
#endif /* FIPS_MODULE */
1730
1731
static void evp_pkey_free_it(EVP_PKEY *x)
1732
0
{
1733
    /* internal function; x is never NULL */
1734
0
    evp_keymgmt_util_clear_operation_cache(x);
1735
0
#ifndef FIPS_MODULE
1736
0
    evp_pkey_free_legacy(x);
1737
0
#endif
1738
1739
0
    if (x->keymgmt != NULL) {
1740
0
        evp_keymgmt_freedata(x->keymgmt, x->keydata);
1741
0
        EVP_KEYMGMT_free(x->keymgmt);
1742
0
        x->keymgmt = NULL;
1743
0
        x->keydata = NULL;
1744
0
    }
1745
0
    x->type = EVP_PKEY_NONE;
1746
0
}
1747
1748
void EVP_PKEY_free(EVP_PKEY *x)
1749
0
{
1750
0
    int i;
1751
1752
0
    if (x == NULL)
1753
0
        return;
1754
1755
0
    CRYPTO_DOWN_REF(&x->references, &i);
1756
0
    REF_PRINT_COUNT("EVP_PKEY", i, x);
1757
0
    if (i > 0)
1758
0
        return;
1759
0
    REF_ASSERT_ISNT(i < 0);
1760
0
    evp_pkey_free_it(x);
1761
0
#ifndef FIPS_MODULE
1762
0
    CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EVP_PKEY, x, &x->ex_data);
1763
0
#endif
1764
0
    CRYPTO_THREAD_lock_free(x->lock);
1765
0
    CRYPTO_FREE_REF(&x->references);
1766
0
#ifndef FIPS_MODULE
1767
0
    sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free);
1768
0
#endif
1769
0
    OPENSSL_free(x);
1770
0
}
1771
1772
int EVP_PKEY_get_size(const EVP_PKEY *pkey)
1773
0
{
1774
0
    int size = 0;
1775
1776
0
    if (pkey != NULL) {
1777
0
        size = pkey->cache.size;
1778
0
#ifndef FIPS_MODULE
1779
0
        if (pkey->ameth != NULL && pkey->ameth->pkey_size != NULL)
1780
0
            size = pkey->ameth->pkey_size(pkey);
1781
0
#endif
1782
0
    }
1783
0
    if (size <= 0) {
1784
0
        ERR_raise(ERR_LIB_EVP, EVP_R_UNKNOWN_MAX_SIZE);
1785
0
        return 0;
1786
0
    }
1787
0
    return size;
1788
0
}
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
0
{
1808
0
    EVP_KEYMGMT *allocated_keymgmt = NULL;
1809
0
    EVP_KEYMGMT *tmp_keymgmt = NULL;
1810
0
    int selection = OSSL_KEYMGMT_SELECT_ALL;
1811
0
    void *keydata = NULL;
1812
0
    int check;
1813
1814
0
    if (pk == NULL)
1815
0
        return NULL;
1816
1817
    /* No key data => nothing to export */
1818
0
    check = 1;
1819
0
#ifndef FIPS_MODULE
1820
0
    check = check && pk->pkey.ptr == NULL;
1821
0
#endif
1822
0
    check = check && pk->keydata == NULL;
1823
0
    if (check)
1824
0
        return NULL;
1825
1826
0
#ifndef FIPS_MODULE
1827
0
    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
0
        if (pk->ameth->dirty_cnt == NULL || pk->ameth->export_to == NULL)
1833
0
            return NULL;
1834
0
    }
1835
0
#endif
1836
1837
0
    if (keymgmt != NULL) {
1838
0
        tmp_keymgmt = *keymgmt;
1839
0
        *keymgmt = NULL;
1840
0
    }
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
0
    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
0
    if (tmp_keymgmt == NULL)
1858
0
        goto end;
1859
1860
0
#ifndef FIPS_MODULE
1861
0
    if (pk->pkey.ptr != NULL) {
1862
0
        OP_CACHE_ELEM *op;
1863
0
        OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
1864
0
        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
0
        if (pk->ameth->dirty_cnt(pk) == pk->dirty_cnt_copy) {
1872
0
            if (!CRYPTO_THREAD_read_lock(pk->lock))
1873
0
                goto end;
1874
0
            op = evp_keymgmt_util_find_operation_cache(pk, tmp_keymgmt,
1875
0
                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
0
            if (op != NULL && op->keymgmt != NULL) {
1892
0
                keydata = op->keydata;
1893
0
                CRYPTO_THREAD_unlock(pk->lock);
1894
0
                goto end;
1895
0
            }
1896
0
            CRYPTO_THREAD_unlock(pk->lock);
1897
0
        }
1898
1899
        /* Make sure that the keymgmt key type matches the legacy NID */
1900
0
        if (!EVP_KEYMGMT_is_a(tmp_keymgmt, OBJ_nid2sn(pk->type)))
1901
0
            goto end;
1902
1903
0
        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
0
            params[0] = OSSL_PARAM_construct_octet_ptr("legacy-object",
1913
0
                &pk->pkey.ptr, sizeof(pk->pkey.ptr));
1914
0
            p = params;
1915
0
        }
1916
0
        keydata = evp_keymgmt_newdata(tmp_keymgmt, p);
1917
0
        if (keydata == NULL)
1918
0
            goto end;
1919
1920
        /*
1921
         * We skip the export if the key data we got back is actually the same
1922
         * as the low level object we passed in
1923
         */
1924
0
        if (keydata != pk->pkey.ptr
1925
0
            && !pk->ameth->export_to(pk, keydata, tmp_keymgmt->import, libctx, propquery)) {
1926
0
            evp_keymgmt_freedata(tmp_keymgmt, keydata);
1927
0
            keydata = NULL;
1928
0
            goto end;
1929
0
        }
1930
1931
        /*
1932
         * If the dirty counter changed since last time, then clear the
1933
         * operation cache.  In that case, we know that |i| is zero.  Just
1934
         * in case this is a re-export, we increment then decrement the
1935
         * keymgmt reference counter.
1936
         */
1937
0
        if (!EVP_KEYMGMT_up_ref(tmp_keymgmt)) { /* refcnt++ */
1938
0
            evp_keymgmt_freedata(tmp_keymgmt, keydata);
1939
0
            keydata = NULL;
1940
0
            goto end;
1941
0
        }
1942
1943
0
        if (!CRYPTO_THREAD_write_lock(pk->lock))
1944
0
            goto end;
1945
1946
0
        if (pk->ameth->dirty_cnt(pk) != pk->dirty_cnt_copy)
1947
0
            evp_keymgmt_util_clear_operation_cache(pk);
1948
1949
0
        EVP_KEYMGMT_free(tmp_keymgmt); /* refcnt-- */
1950
1951
        /* Check to make sure some other thread didn't get there first */
1952
0
        op = evp_keymgmt_util_find_operation_cache(pk, tmp_keymgmt, selection);
1953
0
        if (op != NULL && op->keymgmt != NULL) {
1954
0
            void *tmp_keydata = op->keydata;
1955
1956
0
            CRYPTO_THREAD_unlock(pk->lock);
1957
0
            evp_keymgmt_freedata(tmp_keymgmt, keydata);
1958
0
            keydata = tmp_keydata;
1959
0
            goto end;
1960
0
        }
1961
1962
        /* Add the new export to the operation cache */
1963
0
        if (!evp_keymgmt_util_cache_keydata(pk, tmp_keymgmt, keydata,
1964
0
                selection)) {
1965
0
            CRYPTO_THREAD_unlock(pk->lock);
1966
0
            evp_keymgmt_freedata(tmp_keymgmt, keydata);
1967
0
            keydata = NULL;
1968
0
            goto end;
1969
0
        }
1970
1971
        /* Synchronize the dirty count */
1972
0
        pk->dirty_cnt_copy = pk->ameth->dirty_cnt(pk);
1973
1974
0
        CRYPTO_THREAD_unlock(pk->lock);
1975
0
        goto end;
1976
0
    }
1977
0
#endif /* FIPS_MODULE */
1978
1979
0
    keydata = evp_keymgmt_util_export_to_provider(pk, tmp_keymgmt, selection);
1980
1981
0
end:
1982
    /*
1983
     * If nothing was exported, |tmp_keymgmt| might point at a freed
1984
     * EVP_KEYMGMT, so we clear it to be safe.  It shouldn't be useful for
1985
     * the caller either way in that case.
1986
     */
1987
0
    if (keydata == NULL)
1988
0
        tmp_keymgmt = NULL;
1989
1990
0
    if (keymgmt != NULL && tmp_keymgmt != NULL) {
1991
0
        *keymgmt = tmp_keymgmt;
1992
0
        allocated_keymgmt = NULL;
1993
0
    }
1994
1995
0
    EVP_KEYMGMT_free(allocated_keymgmt);
1996
0
    return keydata;
1997
0
}
1998
1999
#ifndef FIPS_MODULE
2000
int evp_pkey_copy_downgraded(EVP_PKEY **dest, const EVP_PKEY *src)
2001
0
{
2002
0
    EVP_PKEY *allocpkey = NULL;
2003
2004
0
    if (!ossl_assert(dest != NULL))
2005
0
        return 0;
2006
2007
0
    if (evp_pkey_is_assigned(src) && evp_pkey_is_provided(src)) {
2008
0
        EVP_KEYMGMT *keymgmt = src->keymgmt;
2009
0
        void *keydata = src->keydata;
2010
0
        int type = src->type;
2011
0
        const char *keytype = NULL;
2012
2013
0
        keytype = EVP_KEYMGMT_get0_name(keymgmt);
2014
2015
        /*
2016
         * If the type is EVP_PKEY_NONE, then we have a problem somewhere
2017
         * else in our code.  If it's not one of the well known EVP_PKEY_xxx
2018
         * values, it should at least be EVP_PKEY_KEYMGMT at this point.
2019
         * The check is kept as a safety measure.
2020
         */
2021
0
        if (!ossl_assert(type != EVP_PKEY_NONE)) {
2022
0
            ERR_raise_data(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR,
2023
0
                "keymgmt key type = %s but legacy type = EVP_PKEY_NONE",
2024
0
                keytype);
2025
0
            return 0;
2026
0
        }
2027
2028
        /* Prefer the legacy key type name for error reporting */
2029
0
        if (type != EVP_PKEY_KEYMGMT)
2030
0
            keytype = OBJ_nid2sn(type);
2031
2032
        /* Make sure we have a clean slate to copy into */
2033
0
        if (*dest == NULL) {
2034
0
            allocpkey = *dest = EVP_PKEY_new();
2035
0
            if (*dest == NULL) {
2036
0
                ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
2037
0
                return 0;
2038
0
            }
2039
0
        } else {
2040
0
            evp_pkey_free_it(*dest);
2041
0
        }
2042
2043
0
        if (EVP_PKEY_set_type(*dest, type)) {
2044
            /* If the key is typed but empty, we're done */
2045
0
            if (keydata == NULL)
2046
0
                return 1;
2047
2048
0
            if ((*dest)->ameth->import_from == NULL) {
2049
0
                ERR_raise_data(ERR_LIB_EVP, EVP_R_NO_IMPORT_FUNCTION,
2050
0
                    "key type = %s", keytype);
2051
0
            } else {
2052
                /*
2053
                 * We perform the export in the same libctx as the keymgmt
2054
                 * that we are using.
2055
                 */
2056
0
                OSSL_LIB_CTX *libctx = ossl_provider_libctx(keymgmt->prov);
2057
0
                EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_from_pkey(libctx, *dest, NULL);
2058
2059
0
                if (pctx == NULL)
2060
0
                    ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
2061
2062
0
                if (pctx != NULL
2063
0
                    && evp_keymgmt_export(keymgmt, keydata,
2064
0
                        OSSL_KEYMGMT_SELECT_ALL,
2065
0
                        (*dest)->ameth->import_from,
2066
0
                        pctx)) {
2067
                    /* Synchronize the dirty count */
2068
0
                    (*dest)->dirty_cnt_copy = (*dest)->ameth->dirty_cnt(*dest);
2069
2070
0
                    EVP_PKEY_CTX_free(pctx);
2071
0
                    return 1;
2072
0
                }
2073
0
                EVP_PKEY_CTX_free(pctx);
2074
0
            }
2075
2076
0
            ERR_raise_data(ERR_LIB_EVP, EVP_R_KEYMGMT_EXPORT_FAILURE,
2077
0
                "key type = %s", keytype);
2078
0
        }
2079
0
    }
2080
2081
0
    if (allocpkey != NULL) {
2082
0
        EVP_PKEY_free(allocpkey);
2083
0
        *dest = NULL;
2084
0
    }
2085
0
    return 0;
2086
0
}
2087
2088
void *evp_pkey_get_legacy(EVP_PKEY *pk)
2089
0
{
2090
0
    EVP_PKEY *tmp_copy = NULL;
2091
0
    void *ret = NULL;
2092
2093
0
    if (!ossl_assert(pk != NULL))
2094
0
        return NULL;
2095
2096
    /*
2097
     * If this isn't an assigned provider side key, we just use any existing
2098
     * origin legacy key.
2099
     */
2100
0
    if (!evp_pkey_is_assigned(pk))
2101
0
        return NULL;
2102
0
    if (!evp_pkey_is_provided(pk))
2103
0
        return pk->pkey.ptr;
2104
2105
0
    if (!CRYPTO_THREAD_read_lock(pk->lock))
2106
0
        return NULL;
2107
2108
0
    ret = pk->legacy_cache_pkey.ptr;
2109
2110
0
    if (!CRYPTO_THREAD_unlock(pk->lock))
2111
0
        return NULL;
2112
2113
0
    if (ret != NULL)
2114
0
        return ret;
2115
2116
0
    if (!evp_pkey_copy_downgraded(&tmp_copy, pk))
2117
0
        goto err;
2118
2119
0
    if (!CRYPTO_THREAD_write_lock(pk->lock))
2120
0
        goto err;
2121
2122
    /* Check again in case some other thread has updated it in the meantime */
2123
0
    ret = pk->legacy_cache_pkey.ptr;
2124
0
    if (ret == NULL) {
2125
        /* Steal the legacy key reference from the temporary copy */
2126
0
        ret = pk->legacy_cache_pkey.ptr = tmp_copy->pkey.ptr;
2127
0
        tmp_copy->pkey.ptr = NULL;
2128
0
    }
2129
2130
0
    if (!CRYPTO_THREAD_unlock(pk->lock)) {
2131
0
        ret = NULL;
2132
0
        goto err;
2133
0
    }
2134
2135
0
err:
2136
0
    EVP_PKEY_free(tmp_copy);
2137
2138
0
    return ret;
2139
0
}
2140
#endif /* FIPS_MODULE */
2141
2142
int EVP_PKEY_get_bn_param(const EVP_PKEY *pkey, const char *key_name,
2143
    BIGNUM **bn)
2144
0
{
2145
0
    int ret = 0;
2146
0
    OSSL_PARAM params[2];
2147
0
    unsigned char buffer[2048];
2148
0
    unsigned char *buf = NULL;
2149
0
    size_t buf_sz = 0;
2150
2151
0
    if (key_name == NULL
2152
0
        || bn == NULL)
2153
0
        return 0;
2154
2155
0
    memset(buffer, 0, sizeof(buffer));
2156
0
    params[0] = OSSL_PARAM_construct_BN(key_name, buffer, sizeof(buffer));
2157
0
    params[1] = OSSL_PARAM_construct_end();
2158
0
    if (!EVP_PKEY_get_params(pkey, params)) {
2159
0
        if (!OSSL_PARAM_modified(params) || params[0].return_size == 0)
2160
0
            return 0;
2161
0
        buf_sz = params[0].return_size;
2162
        /*
2163
         * If it failed because the buffer was too small then allocate the
2164
         * required buffer size and retry.
2165
         */
2166
0
        buf = OPENSSL_zalloc(buf_sz);
2167
0
        if (buf == NULL)
2168
0
            return 0;
2169
0
        params[0].data = buf;
2170
0
        params[0].data_size = buf_sz;
2171
2172
0
        if (!EVP_PKEY_get_params(pkey, params))
2173
0
            goto err;
2174
0
    }
2175
    /* Fail if the param was not found */
2176
0
    if (!OSSL_PARAM_modified(params))
2177
0
        goto err;
2178
0
    ret = OSSL_PARAM_get_BN(params, bn);
2179
0
err:
2180
0
    if (buf != NULL) {
2181
0
        if (OSSL_PARAM_modified(params))
2182
0
            OPENSSL_clear_free(buf, buf_sz);
2183
0
        else
2184
0
            OPENSSL_free(buf);
2185
0
    } else if (OSSL_PARAM_modified(params)) {
2186
0
        OPENSSL_cleanse(buffer, params[0].data_size);
2187
0
    }
2188
0
    return ret;
2189
0
}
2190
2191
int EVP_PKEY_get_octet_string_param(const EVP_PKEY *pkey, const char *key_name,
2192
    unsigned char *buf, size_t max_buf_sz,
2193
    size_t *out_len)
2194
0
{
2195
0
    OSSL_PARAM params[2];
2196
0
    int ret1 = 0, ret2 = 0;
2197
2198
0
    if (key_name == NULL)
2199
0
        return 0;
2200
2201
0
    params[0] = OSSL_PARAM_construct_octet_string(key_name, buf, max_buf_sz);
2202
0
    params[1] = OSSL_PARAM_construct_end();
2203
0
    if ((ret1 = EVP_PKEY_get_params(pkey, params)))
2204
0
        ret2 = OSSL_PARAM_modified(params);
2205
0
    if (ret2 && out_len != NULL)
2206
0
        *out_len = params[0].return_size;
2207
0
    return ret1 && ret2;
2208
0
}
2209
2210
int EVP_PKEY_get_utf8_string_param(const EVP_PKEY *pkey, const char *key_name,
2211
    char *str, size_t max_buf_sz,
2212
    size_t *out_len)
2213
0
{
2214
0
    OSSL_PARAM params[2];
2215
0
    int ret1 = 0, ret2 = 0;
2216
2217
0
    if (key_name == NULL)
2218
0
        return 0;
2219
2220
0
    params[0] = OSSL_PARAM_construct_utf8_string(key_name, str, max_buf_sz);
2221
0
    params[1] = OSSL_PARAM_construct_end();
2222
0
    if ((ret1 = EVP_PKEY_get_params(pkey, params)))
2223
0
        ret2 = OSSL_PARAM_modified(params);
2224
0
    if (ret2 && out_len != NULL)
2225
0
        *out_len = params[0].return_size;
2226
2227
0
    if (ret2 && params[0].return_size == max_buf_sz)
2228
        /* There was no space for a NUL byte */
2229
0
        return 0;
2230
    /* Add a terminating NUL byte for good measure */
2231
0
    if (ret2 && str != NULL)
2232
0
        str[params[0].return_size] = '\0';
2233
2234
0
    return ret1 && ret2;
2235
0
}
2236
2237
int EVP_PKEY_get_int_param(const EVP_PKEY *pkey, const char *key_name,
2238
    int *out)
2239
0
{
2240
0
    OSSL_PARAM params[2];
2241
2242
0
    if (key_name == NULL)
2243
0
        return 0;
2244
2245
0
    params[0] = OSSL_PARAM_construct_int(key_name, out);
2246
0
    params[1] = OSSL_PARAM_construct_end();
2247
0
    return EVP_PKEY_get_params(pkey, params)
2248
0
        && OSSL_PARAM_modified(params);
2249
0
}
2250
2251
int EVP_PKEY_get_size_t_param(const EVP_PKEY *pkey, const char *key_name,
2252
    size_t *out)
2253
0
{
2254
0
    OSSL_PARAM params[2];
2255
2256
0
    if (key_name == NULL)
2257
0
        return 0;
2258
2259
0
    params[0] = OSSL_PARAM_construct_size_t(key_name, out);
2260
0
    params[1] = OSSL_PARAM_construct_end();
2261
0
    return EVP_PKEY_get_params(pkey, params)
2262
0
        && OSSL_PARAM_modified(params);
2263
0
}
2264
2265
int EVP_PKEY_set_int_param(EVP_PKEY *pkey, const char *key_name, int in)
2266
0
{
2267
0
    OSSL_PARAM params[2];
2268
2269
0
    if (key_name == NULL)
2270
0
        return 0;
2271
2272
0
    params[0] = OSSL_PARAM_construct_int(key_name, &in);
2273
0
    params[1] = OSSL_PARAM_construct_end();
2274
0
    return EVP_PKEY_set_params(pkey, params);
2275
0
}
2276
2277
int EVP_PKEY_set_size_t_param(EVP_PKEY *pkey, const char *key_name, size_t in)
2278
0
{
2279
0
    OSSL_PARAM params[2];
2280
2281
0
    if (key_name == NULL)
2282
0
        return 0;
2283
2284
0
    params[0] = OSSL_PARAM_construct_size_t(key_name, &in);
2285
0
    params[1] = OSSL_PARAM_construct_end();
2286
0
    return EVP_PKEY_set_params(pkey, params);
2287
0
}
2288
2289
int EVP_PKEY_set_bn_param(EVP_PKEY *pkey, const char *key_name,
2290
    const BIGNUM *bn)
2291
0
{
2292
0
    OSSL_PARAM params[2];
2293
0
    unsigned char buffer[2048];
2294
0
    int bsize = 0;
2295
2296
0
    if (key_name == NULL
2297
0
        || bn == NULL
2298
0
        || pkey == NULL
2299
0
        || !evp_pkey_is_assigned(pkey))
2300
0
        return 0;
2301
2302
0
    bsize = BN_num_bytes(bn);
2303
0
    if (!ossl_assert(bsize <= (int)sizeof(buffer)))
2304
0
        return 0;
2305
2306
0
    if (BN_bn2nativepad(bn, buffer, bsize) < 0)
2307
0
        return 0;
2308
0
    params[0] = OSSL_PARAM_construct_BN(key_name, buffer, bsize);
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_utf8_string_param(EVP_PKEY *pkey, const char *key_name,
2314
    const char *str)
2315
0
{
2316
0
    OSSL_PARAM params[2];
2317
2318
0
    if (key_name == NULL)
2319
0
        return 0;
2320
2321
0
    params[0] = OSSL_PARAM_construct_utf8_string(key_name, (char *)str, 0);
2322
0
    params[1] = OSSL_PARAM_construct_end();
2323
0
    return EVP_PKEY_set_params(pkey, params);
2324
0
}
2325
2326
int EVP_PKEY_set_octet_string_param(EVP_PKEY *pkey, const char *key_name,
2327
    const unsigned char *buf, size_t bsize)
2328
0
{
2329
0
    OSSL_PARAM params[2];
2330
2331
0
    if (key_name == NULL)
2332
0
        return 0;
2333
2334
0
    params[0] = OSSL_PARAM_construct_octet_string(key_name,
2335
0
        (unsigned char *)buf, bsize);
2336
0
    params[1] = OSSL_PARAM_construct_end();
2337
0
    return EVP_PKEY_set_params(pkey, params);
2338
0
}
2339
2340
const OSSL_PARAM *EVP_PKEY_settable_params(const EVP_PKEY *pkey)
2341
0
{
2342
0
    return (pkey != NULL && evp_pkey_is_provided(pkey))
2343
0
        ? EVP_KEYMGMT_settable_params(pkey->keymgmt)
2344
0
        : NULL;
2345
0
}
2346
2347
int EVP_PKEY_set_params(EVP_PKEY *pkey, OSSL_PARAM params[])
2348
0
{
2349
0
    if (pkey != NULL) {
2350
0
        if (evp_pkey_is_provided(pkey)) {
2351
0
            pkey->dirty_cnt++;
2352
0
            return evp_keymgmt_set_params(pkey->keymgmt, pkey->keydata, params);
2353
0
        }
2354
0
#ifndef FIPS_MODULE
2355
        /*
2356
         * We will hopefully never find the need to set individual data in
2357
         * EVP_PKEYs with a legacy internal key, but we can't be entirely
2358
         * sure.  This bit of code can be enabled if we find the need.  If
2359
         * not, it can safely be removed when #legacy support is removed.
2360
         */
2361
#if 0
2362
        else if (evp_pkey_is_legacy(pkey)) {
2363
            return evp_pkey_set_params_to_ctrl(pkey, params);
2364
        }
2365
#endif
2366
0
#endif
2367
0
    }
2368
0
    ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY);
2369
0
    return 0;
2370
0
}
2371
2372
const OSSL_PARAM *EVP_PKEY_gettable_params(const EVP_PKEY *pkey)
2373
0
{
2374
0
    return (pkey != NULL && evp_pkey_is_provided(pkey))
2375
0
        ? EVP_KEYMGMT_gettable_params(pkey->keymgmt)
2376
0
        : NULL;
2377
0
}
2378
2379
int EVP_PKEY_get_params(const EVP_PKEY *pkey, OSSL_PARAM params[])
2380
0
{
2381
0
    if (pkey != NULL) {
2382
0
        if (evp_pkey_is_provided(pkey))
2383
0
            return evp_keymgmt_get_params(pkey->keymgmt, pkey->keydata, params) > 0;
2384
0
#ifndef FIPS_MODULE
2385
0
        else if (evp_pkey_is_legacy(pkey))
2386
0
            return evp_pkey_get_params_to_ctrl(pkey, params) > 0;
2387
0
#endif
2388
0
    }
2389
0
    ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY);
2390
0
    return 0;
2391
0
}
2392
2393
#ifndef FIPS_MODULE
2394
int EVP_PKEY_get_ec_point_conv_form(const EVP_PKEY *pkey)
2395
0
{
2396
0
    char name[80];
2397
0
    size_t name_len;
2398
2399
0
    if (pkey == NULL)
2400
0
        return 0;
2401
2402
0
    if (pkey->keymgmt == NULL
2403
0
        || pkey->keydata == NULL) {
2404
0
#ifndef OPENSSL_NO_EC
2405
        /* Might work through the legacy route */
2406
0
        const EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey);
2407
2408
0
        if (ec == NULL)
2409
0
            return 0;
2410
2411
0
        return EC_KEY_get_conv_form(ec);
2412
#else
2413
        return 0;
2414
#endif
2415
0
    }
2416
2417
0
    if (!EVP_PKEY_get_utf8_string_param(pkey,
2418
0
            OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT,
2419
0
            name, sizeof(name), &name_len))
2420
0
        return 0;
2421
2422
0
    if (strcmp(name, "uncompressed") == 0)
2423
0
        return POINT_CONVERSION_UNCOMPRESSED;
2424
2425
0
    if (strcmp(name, "compressed") == 0)
2426
0
        return POINT_CONVERSION_COMPRESSED;
2427
2428
0
    if (strcmp(name, "hybrid") == 0)
2429
0
        return POINT_CONVERSION_HYBRID;
2430
2431
0
    return 0;
2432
0
}
2433
2434
int EVP_PKEY_get_field_type(const EVP_PKEY *pkey)
2435
0
{
2436
0
    char fstr[80];
2437
0
    size_t fstrlen;
2438
2439
0
    if (pkey == NULL)
2440
0
        return 0;
2441
2442
0
    if (pkey->keymgmt == NULL
2443
0
        || pkey->keydata == NULL) {
2444
0
#ifndef OPENSSL_NO_EC
2445
        /* Might work through the legacy route */
2446
0
        const EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey);
2447
0
        const EC_GROUP *grp;
2448
2449
0
        if (ec == NULL)
2450
0
            return 0;
2451
0
        grp = EC_KEY_get0_group(ec);
2452
0
        if (grp == NULL)
2453
0
            return 0;
2454
2455
0
        return EC_GROUP_get_field_type(grp);
2456
#else
2457
        return 0;
2458
#endif
2459
0
    }
2460
2461
0
    if (!EVP_PKEY_get_utf8_string_param(pkey, OSSL_PKEY_PARAM_EC_FIELD_TYPE,
2462
0
            fstr, sizeof(fstr), &fstrlen))
2463
0
        return 0;
2464
2465
0
    if (strcmp(fstr, SN_X9_62_prime_field) == 0)
2466
0
        return NID_X9_62_prime_field;
2467
0
    else if (strcmp(fstr, SN_X9_62_characteristic_two_field))
2468
0
        return NID_X9_62_characteristic_two_field;
2469
2470
0
    return 0;
2471
0
}
2472
#endif