Coverage Report

Created: 2026-09-17 06:47

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-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
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
int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key)
704
0
{
705
0
#ifndef OPENSSL_NO_EC
706
0
    int pktype;
707
708
0
    pktype = EVP_PKEY_type(type);
709
0
    if ((key != NULL) && (pktype == EVP_PKEY_EC || pktype == EVP_PKEY_SM2)) {
710
0
        const EC_GROUP *group = EC_KEY_get0_group(key);
711
712
0
        if (group != NULL) {
713
0
            int curve = EC_GROUP_get_curve_name(group);
714
715
            /*
716
             * Regardless of what is requested the SM2 curve must be SM2 type,
717
             * and non SM2 curves are EC type.
718
             */
719
0
            if (curve == NID_sm2 && pktype == EVP_PKEY_EC)
720
0
                type = EVP_PKEY_SM2;
721
0
            else if (curve != NID_sm2 && pktype == EVP_PKEY_SM2)
722
0
                type = EVP_PKEY_EC;
723
0
        }
724
0
    }
725
0
#endif
726
727
0
    if (pkey == NULL || !EVP_PKEY_set_type(pkey, type))
728
0
        return 0;
729
730
0
    pkey->pkey.ptr = key;
731
732
0
    return (key != NULL);
733
0
}
734
#endif
735
736
void *EVP_PKEY_get0(const EVP_PKEY *pkey)
737
0
{
738
0
    if (pkey == NULL)
739
0
        return NULL;
740
741
0
    if (!evp_pkey_is_provided(pkey))
742
0
        return pkey->pkey.ptr;
743
744
0
    return NULL;
745
0
}
746
747
const unsigned char *EVP_PKEY_get0_hmac(const EVP_PKEY *pkey, size_t *len)
748
0
{
749
0
    const ASN1_OCTET_STRING *os = NULL;
750
0
    if (pkey->type != EVP_PKEY_HMAC) {
751
0
        ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_AN_HMAC_KEY);
752
0
        return NULL;
753
0
    }
754
0
    os = evp_pkey_get_legacy((EVP_PKEY *)pkey);
755
0
    if (os != NULL) {
756
0
        *len = os->length;
757
0
        return os->data;
758
0
    }
759
0
    return NULL;
760
0
}
761
762
#ifndef OPENSSL_NO_POLY1305
763
const unsigned char *EVP_PKEY_get0_poly1305(const EVP_PKEY *pkey, size_t *len)
764
0
{
765
0
    const ASN1_OCTET_STRING *os = NULL;
766
0
    if (pkey->type != EVP_PKEY_POLY1305) {
767
0
        ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_POLY1305_KEY);
768
0
        return NULL;
769
0
    }
770
0
    os = evp_pkey_get_legacy((EVP_PKEY *)pkey);
771
0
    if (os != NULL) {
772
0
        *len = os->length;
773
0
        return os->data;
774
0
    }
775
0
    return NULL;
776
0
}
777
#endif
778
779
#ifndef OPENSSL_NO_SIPHASH
780
const unsigned char *EVP_PKEY_get0_siphash(const EVP_PKEY *pkey, size_t *len)
781
0
{
782
0
    const ASN1_OCTET_STRING *os = NULL;
783
784
0
    if (pkey->type != EVP_PKEY_SIPHASH) {
785
0
        ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_SIPHASH_KEY);
786
0
        return NULL;
787
0
    }
788
0
    os = evp_pkey_get_legacy((EVP_PKEY *)pkey);
789
0
    if (os != NULL) {
790
0
        *len = os->length;
791
0
        return os->data;
792
0
    }
793
0
    return NULL;
794
0
}
795
#endif
796
797
#ifndef OPENSSL_NO_DSA
798
static DSA *evp_pkey_get0_DSA_int(const EVP_PKEY *pkey)
799
0
{
800
0
    if (pkey->type != EVP_PKEY_DSA) {
801
0
        ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_DSA_KEY);
802
0
        return NULL;
803
0
    }
804
0
    return evp_pkey_get_legacy((EVP_PKEY *)pkey);
805
0
}
806
807
const DSA *EVP_PKEY_get0_DSA(const EVP_PKEY *pkey)
808
0
{
809
0
    return evp_pkey_get0_DSA_int(pkey);
810
0
}
811
812
int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key)
813
0
{
814
0
    int ret;
815
816
0
    if (!DSA_up_ref(key))
817
0
        return 0;
818
819
0
    ret = EVP_PKEY_assign_DSA(pkey, key);
820
821
0
    if (!ret)
822
0
        DSA_free(key);
823
824
0
    return ret;
825
0
}
826
DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey)
827
0
{
828
0
    DSA *ret = evp_pkey_get0_DSA_int(pkey);
829
830
0
    if (ret != NULL && !DSA_up_ref(ret))
831
0
        return NULL;
832
833
0
    return ret;
834
0
}
835
#endif /*  OPENSSL_NO_DSA */
836
837
#ifndef OPENSSL_NO_ECX
838
static const ECX_KEY *evp_pkey_get0_ECX_KEY(const EVP_PKEY *pkey, int type)
839
0
{
840
0
    if (EVP_PKEY_get_base_id(pkey) != type) {
841
0
        ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_ECX_KEY);
842
0
        return NULL;
843
0
    }
844
0
    return evp_pkey_get_legacy((EVP_PKEY *)pkey);
845
0
}
846
847
static ECX_KEY *evp_pkey_get1_ECX_KEY(EVP_PKEY *pkey, int type)
848
0
{
849
0
    ECX_KEY *ret = (ECX_KEY *)evp_pkey_get0_ECX_KEY(pkey, type);
850
851
0
    if (ret != NULL && !ossl_ecx_key_up_ref(ret))
852
0
        ret = NULL;
853
0
    return ret;
854
0
}
855
856
#define IMPLEMENT_ECX_VARIANT(NAME)                          \
857
    ECX_KEY *ossl_evp_pkey_get1_##NAME(EVP_PKEY *pkey)       \
858
0
    {                                                        \
859
0
        return evp_pkey_get1_ECX_KEY(pkey, EVP_PKEY_##NAME); \
860
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
861
IMPLEMENT_ECX_VARIANT(X25519)
862
IMPLEMENT_ECX_VARIANT(X448)
863
IMPLEMENT_ECX_VARIANT(ED25519)
864
IMPLEMENT_ECX_VARIANT(ED448)
865
866
#endif /* OPENSSL_NO_ECX */
867
868
#if !defined(OPENSSL_NO_DH) && !defined(OPENSSL_NO_DEPRECATED_3_0)
869
870
int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *dhkey)
871
0
{
872
0
    int ret, type;
873
874
    /*
875
     * ossl_dh_is_named_safe_prime_group() returns 1 for named safe prime groups
876
     * related to ffdhe and modp (which cache q = (p - 1) / 2),
877
     * and returns 0 for all other dh parameter generation types including
878
     * RFC5114 named groups.
879
     *
880
     * The EVP_PKEY_DH type is used for dh parameter generation types:
881
     *  - named safe prime groups related to ffdhe and modp
882
     *  - safe prime generator
883
     *
884
     * The type EVP_PKEY_DHX is used for dh parameter generation types
885
     *  - fips186-4 and fips186-2
886
     *  - rfc5114 named groups.
887
     *
888
     * The EVP_PKEY_DH type is used to save PKCS#3 data than can be stored
889
     * without a q value.
890
     * The EVP_PKEY_DHX type is used to save X9.42 data that requires the
891
     * q value to be stored.
892
     */
893
0
    if (ossl_dh_is_named_safe_prime_group(dhkey))
894
0
        type = EVP_PKEY_DH;
895
0
    else
896
0
        type = DH_get0_q(dhkey) == NULL ? EVP_PKEY_DH : EVP_PKEY_DHX;
897
898
0
    if (!DH_up_ref(dhkey))
899
0
        return 0;
900
901
0
    ret = EVP_PKEY_assign(pkey, type, dhkey);
902
903
0
    if (!ret)
904
0
        DH_free(dhkey);
905
906
0
    return ret;
907
0
}
908
909
DH *evp_pkey_get0_DH_int(const EVP_PKEY *pkey)
910
0
{
911
0
    if (pkey->type != EVP_PKEY_DH && pkey->type != EVP_PKEY_DHX) {
912
0
        ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_DH_KEY);
913
0
        return NULL;
914
0
    }
915
0
    return evp_pkey_get_legacy((EVP_PKEY *)pkey);
916
0
}
917
918
const DH *EVP_PKEY_get0_DH(const EVP_PKEY *pkey)
919
0
{
920
0
    return evp_pkey_get0_DH_int(pkey);
921
0
}
922
923
DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey)
924
0
{
925
0
    DH *ret = evp_pkey_get0_DH_int(pkey);
926
927
0
    if (ret != NULL && !DH_up_ref(ret))
928
0
        ret = NULL;
929
930
0
    return ret;
931
0
}
932
#endif
933
934
int EVP_PKEY_get_id(const EVP_PKEY *pkey)
935
0
{
936
0
    return pkey->type;
937
0
}
938
939
int EVP_PKEY_get_base_id(const EVP_PKEY *pkey)
940
0
{
941
0
    return EVP_PKEY_type(pkey->type);
942
0
}
943
944
/*
945
 * These hard coded cases are pure hackery to get around the fact
946
 * that names in crypto/objects/objects.txt are a mess.  There is
947
 * no "EC", and "RSA" leads to the NID for 2.5.8.1.1, an OID that's
948
 * fallen out in favor of { pkcs-1 1 }, i.e. 1.2.840.113549.1.1.1,
949
 * the NID of which is used for EVP_PKEY_RSA.  Strangely enough,
950
 * "DSA" is accurate...  but still, better be safe and hard-code
951
 * names that we know.
952
 * On a similar topic, EVP_PKEY_type(EVP_PKEY_SM2) will result in
953
 * EVP_PKEY_EC, because of aliasing.
954
 * This should be cleaned away along with all other #legacy support.
955
 */
956
static const OSSL_ITEM standard_name2type[] = {
957
    { EVP_PKEY_RSA, "RSA" },
958
    { EVP_PKEY_RSA_PSS, "RSA-PSS" },
959
    { EVP_PKEY_EC, "EC" },
960
    { EVP_PKEY_ED25519, "ED25519" },
961
    { EVP_PKEY_ED448, "ED448" },
962
    { EVP_PKEY_X25519, "X25519" },
963
    { EVP_PKEY_X448, "X448" },
964
    { EVP_PKEY_SM2, "SM2" },
965
    { EVP_PKEY_DH, "DH" },
966
    { EVP_PKEY_DHX, "X9.42 DH" },
967
    { EVP_PKEY_DHX, "DHX" },
968
    { EVP_PKEY_DSA, "DSA" },
969
};
970
971
int evp_pkey_name2type(const char *name)
972
0
{
973
0
    int type;
974
0
    size_t i;
975
976
0
    for (i = 0; i < OSSL_NELEM(standard_name2type); i++) {
977
0
        if (OPENSSL_strcasecmp(name, standard_name2type[i].ptr) == 0)
978
0
            return (int)standard_name2type[i].id;
979
0
    }
980
981
0
    if ((type = EVP_PKEY_type(OBJ_sn2nid(name))) != NID_undef)
982
0
        return type;
983
0
    return EVP_PKEY_type(OBJ_ln2nid(name));
984
0
}
985
986
const char *evp_pkey_type2name(int type)
987
0
{
988
0
    size_t i;
989
990
0
    for (i = 0; i < OSSL_NELEM(standard_name2type); i++) {
991
0
        if (type == (int)standard_name2type[i].id)
992
0
            return standard_name2type[i].ptr;
993
0
    }
994
995
0
    return OBJ_nid2sn(type);
996
0
}
997
998
int EVP_PKEY_is_a(const EVP_PKEY *pkey, const char *name)
999
0
{
1000
0
    if (pkey == NULL)
1001
0
        return 0;
1002
0
    if (pkey->keymgmt == NULL)
1003
0
        return pkey->type == evp_pkey_name2type(name);
1004
0
    return EVP_KEYMGMT_is_a(pkey->keymgmt, name);
1005
0
}
1006
1007
int EVP_PKEY_type_names_do_all(const EVP_PKEY *pkey,
1008
    void (*fn)(const char *name, void *data),
1009
    void *data)
1010
0
{
1011
0
    if (!evp_pkey_is_typed(pkey))
1012
0
        return 0;
1013
1014
0
    if (!evp_pkey_is_provided(pkey)) {
1015
0
        const char *name = OBJ_nid2sn(EVP_PKEY_get_id(pkey));
1016
1017
0
        fn(name, data);
1018
0
        return 1;
1019
0
    }
1020
0
    return EVP_KEYMGMT_names_do_all(pkey->keymgmt, fn, data);
1021
0
}
1022
1023
int EVP_PKEY_can_sign(const EVP_PKEY *pkey)
1024
0
{
1025
0
    if (pkey->keymgmt == NULL) {
1026
0
        switch (EVP_PKEY_get_base_id(pkey)) {
1027
0
        case EVP_PKEY_RSA:
1028
0
        case EVP_PKEY_RSA_PSS:
1029
0
            return 1;
1030
0
#ifndef OPENSSL_NO_DSA
1031
0
        case EVP_PKEY_DSA:
1032
0
            return 1;
1033
0
#endif
1034
0
#ifndef OPENSSL_NO_EC
1035
0
        case EVP_PKEY_ED25519:
1036
0
        case EVP_PKEY_ED448:
1037
0
            return 1;
1038
0
        case EVP_PKEY_EC: /* Including SM2 */
1039
0
            return EC_KEY_can_sign(pkey->pkey.ec);
1040
0
#endif
1041
0
        default:
1042
0
            break;
1043
0
        }
1044
0
    } else {
1045
0
        const OSSL_PROVIDER *prov = EVP_KEYMGMT_get0_provider(pkey->keymgmt);
1046
0
        OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
1047
0
        EVP_SIGNATURE *sig;
1048
0
        const char *name;
1049
1050
0
        name = evp_keymgmt_util_query_operation_name(pkey->keymgmt,
1051
0
            OSSL_OP_SIGNATURE);
1052
0
        sig = EVP_SIGNATURE_fetch(libctx, name, NULL);
1053
0
        if (sig != NULL) {
1054
0
            EVP_SIGNATURE_free(sig);
1055
0
            return 1;
1056
0
        }
1057
0
    }
1058
0
    return 0;
1059
0
}
1060
1061
static int print_reset_indent(BIO **out, int pop_f_prefix, long saved_indent)
1062
0
{
1063
0
    BIO_set_indent(*out, saved_indent);
1064
0
    if (pop_f_prefix) {
1065
0
        BIO *next = BIO_pop(*out);
1066
1067
0
        BIO_free(*out);
1068
0
        *out = next;
1069
0
    }
1070
0
    return 1;
1071
0
}
1072
1073
static int print_set_indent(BIO **out, int *pop_f_prefix, long *saved_indent,
1074
    long indent)
1075
0
{
1076
0
    *pop_f_prefix = 0;
1077
0
    *saved_indent = 0;
1078
0
    if (indent > 0) {
1079
0
        long i = BIO_get_indent(*out);
1080
1081
0
        *saved_indent = (i < 0 ? 0 : i);
1082
0
        if (BIO_set_indent(*out, indent) <= 0) {
1083
0
            BIO *prefbio = BIO_new(BIO_f_prefix());
1084
1085
0
            if (prefbio == NULL)
1086
0
                return 0;
1087
0
            *out = BIO_push(prefbio, *out);
1088
0
            *pop_f_prefix = 1;
1089
0
        }
1090
0
        if (BIO_set_indent(*out, indent) <= 0) {
1091
0
            print_reset_indent(out, *pop_f_prefix, *saved_indent);
1092
0
            return 0;
1093
0
        }
1094
0
    }
1095
0
    return 1;
1096
0
}
1097
1098
static int unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent,
1099
    const char *kstr)
1100
0
{
1101
0
    return BIO_indent(out, indent, 128)
1102
0
        && BIO_printf(out, "%s algorithm \"%s\" unsupported\n",
1103
0
               kstr, OBJ_nid2ln(pkey->type))
1104
0
        > 0;
1105
0
}
1106
1107
static int print_pkey(const EVP_PKEY *pkey, BIO *out, int indent,
1108
    int selection /* For provided encoding */,
1109
    const char *propquery /* For provided encoding */,
1110
    int (*legacy_print)(BIO *out, const EVP_PKEY *pkey,
1111
        int indent, ASN1_PCTX *pctx),
1112
    ASN1_PCTX *legacy_pctx /* For legacy print */)
1113
0
{
1114
0
    int pop_f_prefix;
1115
0
    long saved_indent;
1116
0
    OSSL_ENCODER_CTX *ctx = NULL;
1117
0
    int ret = -2; /* default to unsupported */
1118
1119
0
    if (!print_set_indent(&out, &pop_f_prefix, &saved_indent, indent))
1120
0
        return 0;
1121
1122
0
    ctx = OSSL_ENCODER_CTX_new_for_pkey(pkey, selection, "TEXT", NULL,
1123
0
        propquery);
1124
0
    if (OSSL_ENCODER_CTX_get_num_encoders(ctx) != 0)
1125
0
        ret = OSSL_ENCODER_to_bio(ctx, out);
1126
0
    OSSL_ENCODER_CTX_free(ctx);
1127
1128
0
    if (ret != -2)
1129
0
        goto end;
1130
1131
    /* legacy fallback */
1132
0
    if (legacy_print != NULL)
1133
0
        ret = legacy_print(out, pkey, 0, legacy_pctx);
1134
0
    else
1135
0
        ret = unsup_alg(out, pkey, 0, "Public Key");
1136
1137
0
end:
1138
0
    print_reset_indent(&out, pop_f_prefix, saved_indent);
1139
0
    return ret;
1140
0
}
1141
1142
int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey,
1143
    int indent, ASN1_PCTX *pctx)
1144
0
{
1145
0
    return print_pkey(pkey, out, indent, EVP_PKEY_PUBLIC_KEY, NULL,
1146
0
        (pkey->ameth != NULL ? pkey->ameth->pub_print : NULL),
1147
0
        pctx);
1148
0
}
1149
1150
int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey,
1151
    int indent, ASN1_PCTX *pctx)
1152
0
{
1153
0
    return print_pkey(pkey, out, indent, EVP_PKEY_PRIVATE_KEY, NULL,
1154
0
        (pkey->ameth != NULL ? pkey->ameth->priv_print : NULL),
1155
0
        pctx);
1156
0
}
1157
1158
int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey,
1159
    int indent, ASN1_PCTX *pctx)
1160
0
{
1161
0
    return print_pkey(pkey, out, indent, EVP_PKEY_KEY_PARAMETERS, NULL,
1162
0
        (pkey->ameth != NULL ? pkey->ameth->param_print : NULL),
1163
0
        pctx);
1164
0
}
1165
1166
#ifndef OPENSSL_NO_STDIO
1167
int EVP_PKEY_print_public_fp(FILE *fp, const EVP_PKEY *pkey,
1168
    int indent, ASN1_PCTX *pctx)
1169
0
{
1170
0
    int ret;
1171
0
    BIO *b = BIO_new_fp(fp, BIO_NOCLOSE);
1172
1173
0
    if (b == NULL)
1174
0
        return 0;
1175
0
    ret = EVP_PKEY_print_public(b, pkey, indent, pctx);
1176
0
    BIO_free(b);
1177
0
    return ret;
1178
0
}
1179
1180
int EVP_PKEY_print_private_fp(FILE *fp, const EVP_PKEY *pkey,
1181
    int indent, ASN1_PCTX *pctx)
1182
0
{
1183
0
    int ret;
1184
0
    BIO *b = BIO_new_fp(fp, BIO_NOCLOSE);
1185
1186
0
    if (b == NULL)
1187
0
        return 0;
1188
0
    ret = EVP_PKEY_print_private(b, pkey, indent, pctx);
1189
0
    BIO_free(b);
1190
0
    return ret;
1191
0
}
1192
1193
int EVP_PKEY_print_params_fp(FILE *fp, const EVP_PKEY *pkey,
1194
    int indent, ASN1_PCTX *pctx)
1195
0
{
1196
0
    int ret;
1197
0
    BIO *b = BIO_new_fp(fp, BIO_NOCLOSE);
1198
1199
0
    if (b == NULL)
1200
0
        return 0;
1201
0
    ret = EVP_PKEY_print_params(b, pkey, indent, pctx);
1202
0
    BIO_free(b);
1203
0
    return ret;
1204
0
}
1205
#endif
1206
1207
static void mdname2nid(const char *mdname, void *data)
1208
0
{
1209
0
    int *nid = (int *)data;
1210
1211
0
    if (*nid != NID_undef)
1212
0
        return;
1213
1214
0
    *nid = OBJ_sn2nid(mdname);
1215
0
    if (*nid == NID_undef)
1216
0
        *nid = OBJ_ln2nid(mdname);
1217
0
}
1218
1219
static int legacy_asn1_ctrl_to_param(EVP_PKEY *pkey, int op,
1220
    int arg1, void *arg2)
1221
0
{
1222
0
    if (pkey->keymgmt == NULL)
1223
0
        return 0;
1224
0
    switch (op) {
1225
0
    case ASN1_PKEY_CTRL_DEFAULT_MD_NID: {
1226
0
        char mdname[80] = "";
1227
0
        int rv = EVP_PKEY_get_default_digest_name(pkey, mdname,
1228
0
            sizeof(mdname));
1229
1230
0
        if (rv > 0) {
1231
0
            int mdnum;
1232
0
            OSSL_LIB_CTX *libctx = ossl_provider_libctx(pkey->keymgmt->prov);
1233
            /* Make sure the MD is in the namemap if available */
1234
0
            EVP_MD *md;
1235
0
            OSSL_NAMEMAP *namemap;
1236
0
            int nid = NID_undef;
1237
1238
0
            (void)ERR_set_mark();
1239
0
            md = EVP_MD_fetch(libctx, mdname, NULL);
1240
0
            (void)ERR_pop_to_mark();
1241
0
            namemap = ossl_namemap_stored(libctx);
1242
1243
            /*
1244
             * The only reason to fetch the MD was to make sure it is in the
1245
             * namemap. We can immediately free it.
1246
             */
1247
0
            EVP_MD_free(md);
1248
0
            mdnum = ossl_namemap_name2num(namemap, mdname);
1249
0
            if (mdnum == 0)
1250
0
                return 0;
1251
1252
            /*
1253
             * We have the namemap number - now we need to find the
1254
             * associated nid
1255
             */
1256
0
            if (!ossl_namemap_doall_names(namemap, mdnum, mdname2nid, &nid))
1257
0
                return 0;
1258
0
            *(int *)arg2 = nid;
1259
0
        }
1260
0
        return rv;
1261
0
    }
1262
0
    default:
1263
0
        return -2;
1264
0
    }
1265
0
}
1266
1267
static int evp_pkey_asn1_ctrl(EVP_PKEY *pkey, int op, int arg1, void *arg2)
1268
0
{
1269
0
    if (pkey->ameth == NULL)
1270
0
        return legacy_asn1_ctrl_to_param(pkey, op, arg1, arg2);
1271
0
    if (pkey->ameth->pkey_ctrl == NULL)
1272
0
        return -2;
1273
0
    return pkey->ameth->pkey_ctrl(pkey, op, arg1, arg2);
1274
0
}
1275
1276
int EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid)
1277
0
{
1278
0
    if (pkey == NULL)
1279
0
        return 0;
1280
0
    return evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID, 0, pnid);
1281
0
}
1282
1283
int EVP_PKEY_get_default_digest_name(EVP_PKEY *pkey,
1284
    char *mdname, size_t mdname_sz)
1285
0
{
1286
0
    if (pkey->ameth == NULL)
1287
0
        return evp_keymgmt_util_get_deflt_digest_name(pkey->keymgmt,
1288
0
            pkey->keydata,
1289
0
            mdname, mdname_sz);
1290
1291
0
    {
1292
0
        int nid = NID_undef;
1293
0
        int rv = EVP_PKEY_get_default_digest_nid(pkey, &nid);
1294
0
        const char *name = rv > 0 ? OBJ_nid2sn(nid) : NULL;
1295
1296
0
        if (rv > 0)
1297
0
            OPENSSL_strlcpy(mdname, name, mdname_sz);
1298
0
        return rv;
1299
0
    }
1300
0
}
1301
1302
int EVP_PKEY_get_group_name(const EVP_PKEY *pkey, char *gname, size_t gname_sz,
1303
    size_t *gname_len)
1304
0
{
1305
0
    return EVP_PKEY_get_utf8_string_param(pkey, OSSL_PKEY_PARAM_GROUP_NAME,
1306
0
        gname, gname_sz, gname_len);
1307
0
}
1308
1309
int EVP_PKEY_digestsign_supports_digest(EVP_PKEY *pkey, OSSL_LIB_CTX *libctx,
1310
    const char *name, const char *propq)
1311
0
{
1312
0
    int rv;
1313
0
    EVP_MD_CTX *ctx = NULL;
1314
1315
0
    if ((ctx = EVP_MD_CTX_new()) == NULL)
1316
0
        return -1;
1317
1318
0
    ERR_set_mark();
1319
0
    rv = EVP_DigestSignInit_ex(ctx, NULL, name, libctx,
1320
0
        propq, pkey, NULL);
1321
0
    ERR_pop_to_mark();
1322
1323
0
    EVP_MD_CTX_free(ctx);
1324
0
    return rv;
1325
0
}
1326
#endif /* !FIPS_MODULE */
1327
1328
int EVP_PKEY_set1_encoded_public_key(EVP_PKEY *pkey, const unsigned char *pub,
1329
    size_t publen)
1330
0
{
1331
0
    if (pkey == NULL)
1332
0
        return 0;
1333
0
#ifndef FIPS_MODULE
1334
0
    if (evp_pkey_is_provided(pkey))
1335
0
#endif /* !FIPS_MODULE */
1336
0
        return EVP_PKEY_set_octet_string_param(pkey,
1337
0
            OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
1338
0
            (unsigned char *)pub, publen);
1339
1340
0
#ifndef FIPS_MODULE
1341
0
    if (publen > INT_MAX)
1342
0
        return 0;
1343
    /* Historically this function was EVP_PKEY_set1_tls_encodedpoint */
1344
0
    if (evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SET1_TLS_ENCPT, (int)publen,
1345
0
            (void *)pub)
1346
0
        <= 0)
1347
0
        return 0;
1348
0
    return 1;
1349
0
#endif /* !FIPS_MODULE */
1350
0
}
1351
1352
size_t EVP_PKEY_get1_encoded_public_key(EVP_PKEY *pkey, unsigned char **ppub)
1353
0
{
1354
0
    if (pkey == NULL)
1355
0
        return 0;
1356
0
#ifndef FIPS_MODULE
1357
0
    if (evp_pkey_is_provided(pkey))
1358
0
#endif
1359
0
    {
1360
0
        size_t return_size = OSSL_PARAM_UNMODIFIED;
1361
0
        unsigned char *buf;
1362
1363
        /*
1364
         * We know that this is going to fail, but it will give us a size
1365
         * to allocate.
1366
         */
1367
0
        EVP_PKEY_get_octet_string_param(pkey,
1368
0
            OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
1369
0
            NULL, 0, &return_size);
1370
0
        if (return_size == OSSL_PARAM_UNMODIFIED)
1371
0
            return 0;
1372
1373
0
        *ppub = NULL;
1374
0
        buf = OPENSSL_malloc(return_size);
1375
0
        if (buf == NULL)
1376
0
            return 0;
1377
1378
0
        if (!EVP_PKEY_get_octet_string_param(pkey,
1379
0
                OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
1380
0
                buf, return_size, NULL)) {
1381
0
            OPENSSL_free(buf);
1382
0
            return 0;
1383
0
        }
1384
0
        *ppub = buf;
1385
0
        return return_size;
1386
0
    }
1387
1388
0
#ifndef FIPS_MODULE
1389
0
    {
1390
0
        int rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_GET1_TLS_ENCPT, 0, ppub);
1391
0
        if (rv <= 0)
1392
0
            return 0;
1393
0
        return rv;
1394
0
    }
1395
0
#endif /* !FIPS_MODULE */
1396
0
}
1397
1398
/*- All methods below can also be used in FIPS_MODULE */
1399
1400
EVP_PKEY *EVP_PKEY_new(void)
1401
0
{
1402
0
    EVP_PKEY *ret = OPENSSL_zalloc(sizeof(*ret));
1403
1404
0
    if (ret == NULL)
1405
0
        return NULL;
1406
1407
0
    ret->type = EVP_PKEY_NONE;
1408
0
    ret->save_type = EVP_PKEY_NONE;
1409
1410
0
    if (!CRYPTO_NEW_REF(&ret->references, 1))
1411
0
        goto err;
1412
1413
0
    ret->lock = CRYPTO_THREAD_lock_new();
1414
0
    if (ret->lock == NULL) {
1415
0
        ERR_raise(ERR_LIB_EVP, ERR_R_CRYPTO_LIB);
1416
0
        goto err;
1417
0
    }
1418
1419
0
#ifndef FIPS_MODULE
1420
0
    ret->save_parameters = 1;
1421
0
    if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_EVP_PKEY, ret, &ret->ex_data)) {
1422
0
        ERR_raise(ERR_LIB_EVP, ERR_R_CRYPTO_LIB);
1423
0
        goto err;
1424
0
    }
1425
0
#endif
1426
0
    return ret;
1427
1428
0
err:
1429
0
    CRYPTO_FREE_REF(&ret->references);
1430
0
    CRYPTO_THREAD_lock_free(ret->lock);
1431
0
    OPENSSL_free(ret);
1432
0
    return NULL;
1433
0
}
1434
1435
/*
1436
 * Setup a public key management method.
1437
 *
1438
 * For legacy keys, either |type| or |str| is expected to have the type
1439
 * information. In this case, the setup consists of finding an ASN1 method
1440
 * and setting those fields in |pkey|.
1441
 *
1442
 * For provider side keys, |keymgmt| is expected to be non-NULL.  In this
1443
 * case, the setup consists of setting the |keymgmt| field in |pkey|.
1444
 *
1445
 * If pkey is NULL just return 1 or 0 if the key management method exists.
1446
 */
1447
1448
static int pkey_set_type(EVP_PKEY *pkey, int type, const char *str,
1449
    int len, EVP_KEYMGMT *keymgmt)
1450
0
{
1451
0
#ifndef FIPS_MODULE
1452
0
    const EVP_PKEY_ASN1_METHOD *ameth = NULL;
1453
0
#endif
1454
1455
    /*
1456
     * The setups can't set both legacy and provider side methods.
1457
     * It is forbidden
1458
     */
1459
0
    if (!ossl_assert(type == EVP_PKEY_NONE || keymgmt == NULL)) {
1460
0
        ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
1461
0
        return 0;
1462
0
    }
1463
1464
0
    if (pkey != NULL) {
1465
0
        int free_it = 0;
1466
1467
0
#ifndef FIPS_MODULE
1468
0
        free_it = free_it || pkey->pkey.ptr != NULL;
1469
0
#endif
1470
0
        free_it = free_it || pkey->keydata != NULL;
1471
0
        if (free_it)
1472
0
            evp_pkey_free_it(pkey);
1473
0
#ifndef FIPS_MODULE
1474
        /*
1475
         * If key type matches and a method exists then this lookup has
1476
         * succeeded once so just indicate success.
1477
         */
1478
0
        if (pkey->type != EVP_PKEY_NONE
1479
0
            && type == pkey->save_type
1480
0
            && pkey->ameth != NULL)
1481
0
            return 1;
1482
0
#endif
1483
0
    }
1484
0
#ifndef FIPS_MODULE
1485
0
    if (str != NULL)
1486
0
        ameth = evp_pkey_asn1_find_str(str, len);
1487
0
    else if (type != EVP_PKEY_NONE)
1488
0
        ameth = evp_pkey_asn1_find(type);
1489
0
#endif
1490
1491
0
    {
1492
0
        int check = 1;
1493
1494
0
#ifndef FIPS_MODULE
1495
0
        check = check && ameth == NULL;
1496
0
#endif
1497
0
        check = check && keymgmt == NULL;
1498
0
        if (check) {
1499
0
            ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_ALGORITHM);
1500
0
            return 0;
1501
0
        }
1502
0
    }
1503
0
    if (pkey != NULL) {
1504
0
        if (keymgmt != NULL && !EVP_KEYMGMT_up_ref(keymgmt)) {
1505
0
            ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
1506
0
            return 0;
1507
0
        }
1508
1509
0
        pkey->keymgmt = keymgmt;
1510
1511
0
        pkey->save_type = type;
1512
0
        pkey->type = type;
1513
1514
0
#ifndef FIPS_MODULE
1515
        /*
1516
         * If the internal "origin" key is provider side, don't save |ameth|.
1517
         * The main reason is that |ameth| is one factor to detect that the
1518
         * internal "origin" key is a legacy one.
1519
         */
1520
0
        if (keymgmt == NULL)
1521
0
            pkey->ameth = ameth;
1522
1523
        /*
1524
         * The EVP_PKEY_ASN1_METHOD |pkey_id| retains its legacy key purpose
1525
         * for any key type that has a legacy implementation, regardless of
1526
         * if the internal key is a legacy or a provider side one.  When
1527
         * there is no legacy implementation for the key, the type becomes
1528
         * EVP_PKEY_KEYMGMT, which indicates that one should be cautious
1529
         * with functions that expect legacy internal keys.
1530
         */
1531
0
        if (ameth != NULL) {
1532
0
            if (type == EVP_PKEY_NONE)
1533
0
                pkey->type = ameth->pkey_id;
1534
0
        } else {
1535
0
            pkey->type = EVP_PKEY_KEYMGMT;
1536
0
        }
1537
0
#endif
1538
0
    }
1539
0
    return 1;
1540
0
}
1541
1542
#ifndef FIPS_MODULE
1543
static void find_ameth(const char *name, void *data)
1544
0
{
1545
0
    const char **str = data;
1546
1547
    /*
1548
     * The error messages from pkey_set_type() are uninteresting here,
1549
     * and misleading.
1550
     */
1551
0
    ERR_set_mark();
1552
1553
0
    if (pkey_set_type(NULL, EVP_PKEY_NONE, name, (int)strlen(name),
1554
0
            NULL)) {
1555
0
        if (str[0] == NULL)
1556
0
            str[0] = name;
1557
0
        else if (str[1] == NULL)
1558
0
            str[1] = name;
1559
0
    }
1560
1561
0
    ERR_pop_to_mark();
1562
0
}
1563
#endif
1564
1565
int EVP_PKEY_set_type_by_keymgmt(EVP_PKEY *pkey, EVP_KEYMGMT *keymgmt)
1566
0
{
1567
0
#ifndef FIPS_MODULE
1568
0
#define EVP_PKEY_TYPE_STR str[0]
1569
0
#define EVP_PKEY_TYPE_STRLEN (str[0] == NULL ? -1 : (int)strlen(str[0]))
1570
    /*
1571
     * Find at most two strings that have an associated EVP_PKEY_ASN1_METHOD
1572
     * Ideally, only one should be found.  If two (or more) are found, the
1573
     * match is ambiguous.  This should never happen, but...
1574
     */
1575
0
    const char *str[2] = { NULL, NULL };
1576
1577
0
    if (!EVP_KEYMGMT_names_do_all(keymgmt, find_ameth, &str)
1578
0
        || str[1] != NULL) {
1579
0
        ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
1580
0
        return 0;
1581
0
    }
1582
#else
1583
#define EVP_PKEY_TYPE_STR NULL
1584
#define EVP_PKEY_TYPE_STRLEN -1
1585
#endif
1586
0
    return pkey_set_type(pkey, EVP_PKEY_NONE,
1587
0
        EVP_PKEY_TYPE_STR, EVP_PKEY_TYPE_STRLEN,
1588
0
        keymgmt);
1589
1590
0
#undef EVP_PKEY_TYPE_STR
1591
0
#undef EVP_PKEY_TYPE_STRLEN
1592
0
}
1593
1594
int EVP_PKEY_up_ref(EVP_PKEY *pkey)
1595
0
{
1596
0
    int i;
1597
1598
0
    if (!CRYPTO_UP_REF(&pkey->references, &i))
1599
0
        return 0;
1600
1601
0
    REF_PRINT_COUNT("EVP_PKEY", i, pkey);
1602
0
    REF_ASSERT_ISNT(i < 2);
1603
0
    return ((i > 1) ? 1 : 0);
1604
0
}
1605
1606
EVP_PKEY *EVP_PKEY_dup(EVP_PKEY *pkey)
1607
0
{
1608
0
    EVP_PKEY *dup_pk;
1609
1610
0
    if (pkey == NULL) {
1611
0
        ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
1612
0
        return NULL;
1613
0
    }
1614
1615
0
    if ((dup_pk = EVP_PKEY_new()) == NULL)
1616
0
        return NULL;
1617
1618
0
    if (evp_pkey_is_blank(pkey))
1619
0
        goto done;
1620
1621
0
#ifndef FIPS_MODULE
1622
0
    if (evp_pkey_is_provided(pkey))
1623
0
#endif /* !FIPS_MODULE */
1624
0
    {
1625
0
        if (!evp_keymgmt_util_copy(dup_pk, pkey,
1626
0
                OSSL_KEYMGMT_SELECT_ALL))
1627
0
            goto err;
1628
0
        goto done;
1629
0
    }
1630
1631
0
#ifndef FIPS_MODULE
1632
0
    if (evp_pkey_is_legacy(pkey)) {
1633
0
        const EVP_PKEY_ASN1_METHOD *ameth = pkey->ameth;
1634
1635
0
        if (ameth == NULL || ameth->copy == NULL) {
1636
0
            if (pkey->pkey.ptr == NULL /* empty key, just set type */
1637
0
                && EVP_PKEY_set_type(dup_pk, pkey->type) != 0)
1638
0
                goto done;
1639
0
            ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEY_TYPE);
1640
0
            goto err;
1641
0
        }
1642
0
        if (!ameth->copy(dup_pk, pkey))
1643
0
            goto err;
1644
0
        goto done;
1645
0
    }
1646
0
#endif /* !FIPS_MODULE */
1647
1648
0
    goto err;
1649
0
done:
1650
0
#ifndef FIPS_MODULE
1651
    /* copy auxiliary data */
1652
0
    if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_EVP_PKEY,
1653
0
            &dup_pk->ex_data, &pkey->ex_data))
1654
0
        goto err;
1655
1656
0
    if (pkey->attributes != NULL) {
1657
0
        if ((dup_pk->attributes = ossl_x509at_dup(pkey->attributes)) == NULL)
1658
0
            goto err;
1659
0
    }
1660
0
#endif /* !FIPS_MODULE */
1661
0
    return dup_pk;
1662
0
err:
1663
0
    EVP_PKEY_free(dup_pk);
1664
0
    return NULL;
1665
0
}
1666
1667
#ifndef FIPS_MODULE
1668
void evp_pkey_free_legacy(EVP_PKEY *x)
1669
0
{
1670
0
    const EVP_PKEY_ASN1_METHOD *ameth = x->ameth;
1671
1672
0
    if (ameth == NULL && x->legacy_cache_pkey.ptr != NULL)
1673
0
        ameth = evp_pkey_asn1_find(x->type);
1674
1675
0
    if (ameth != NULL) {
1676
0
        if (x->legacy_cache_pkey.ptr != NULL) {
1677
            /*
1678
             * We should never have both a legacy origin key, and a key in the
1679
             * legacy cache.
1680
             */
1681
0
            assert(x->pkey.ptr == NULL);
1682
            /*
1683
             * For the purposes of freeing we make the legacy cache look like
1684
             * a legacy origin key.
1685
             */
1686
0
            x->pkey = x->legacy_cache_pkey;
1687
0
            x->legacy_cache_pkey.ptr = NULL;
1688
0
        }
1689
0
        if (ameth->pkey_free != NULL)
1690
0
            ameth->pkey_free(x);
1691
0
        x->pkey.ptr = NULL;
1692
0
    }
1693
0
}
1694
#endif /* FIPS_MODULE */
1695
1696
static void evp_pkey_free_it(EVP_PKEY *x)
1697
0
{
1698
    /* internal function; x is never NULL */
1699
0
    evp_keymgmt_util_clear_operation_cache(x);
1700
0
#ifndef FIPS_MODULE
1701
0
    evp_pkey_free_legacy(x);
1702
0
#endif
1703
1704
0
    if (x->keymgmt != NULL) {
1705
0
        evp_keymgmt_freedata(x->keymgmt, x->keydata);
1706
0
        EVP_KEYMGMT_free(x->keymgmt);
1707
0
        x->keymgmt = NULL;
1708
0
        x->keydata = NULL;
1709
0
    }
1710
0
    x->type = EVP_PKEY_NONE;
1711
0
}
1712
1713
void EVP_PKEY_free(EVP_PKEY *x)
1714
0
{
1715
0
    int i;
1716
1717
0
    if (x == NULL)
1718
0
        return;
1719
1720
0
    CRYPTO_DOWN_REF(&x->references, &i);
1721
0
    REF_PRINT_COUNT("EVP_PKEY", i, x);
1722
0
    if (i > 0)
1723
0
        return;
1724
0
    REF_ASSERT_ISNT(i < 0);
1725
0
    evp_pkey_free_it(x);
1726
0
#ifndef FIPS_MODULE
1727
0
    CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EVP_PKEY, x, &x->ex_data);
1728
0
#endif
1729
0
    CRYPTO_THREAD_lock_free(x->lock);
1730
0
    CRYPTO_FREE_REF(&x->references);
1731
0
#ifndef FIPS_MODULE
1732
0
    sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free);
1733
0
#endif
1734
0
    OPENSSL_free(x);
1735
0
}
1736
1737
int EVP_PKEY_get_size(const EVP_PKEY *pkey)
1738
0
{
1739
0
    int size = 0;
1740
1741
0
    if (pkey != NULL) {
1742
0
        size = pkey->cache.size;
1743
0
#ifndef FIPS_MODULE
1744
0
        if (pkey->ameth != NULL && pkey->ameth->pkey_size != NULL)
1745
0
            size = pkey->ameth->pkey_size(pkey);
1746
0
#endif
1747
0
    }
1748
0
    if (size <= 0) {
1749
0
        ERR_raise(ERR_LIB_EVP, EVP_R_UNKNOWN_MAX_SIZE);
1750
0
        return 0;
1751
0
    }
1752
0
    return size;
1753
0
}
1754
1755
const char *EVP_PKEY_get0_description(const EVP_PKEY *pkey)
1756
0
{
1757
0
    if (!evp_pkey_is_assigned(pkey))
1758
0
        return NULL;
1759
1760
0
    if (evp_pkey_is_provided(pkey) && pkey->keymgmt->description != NULL)
1761
0
        return pkey->keymgmt->description;
1762
0
#ifndef FIPS_MODULE
1763
0
    if (pkey->ameth != NULL)
1764
0
        return pkey->ameth->info;
1765
0
#endif
1766
0
    return NULL;
1767
0
}
1768
1769
void *evp_pkey_export_to_provider(EVP_PKEY *pk, OSSL_LIB_CTX *libctx,
1770
    EVP_KEYMGMT **keymgmt,
1771
    const char *propquery)
1772
0
{
1773
0
    EVP_KEYMGMT *allocated_keymgmt = NULL;
1774
0
    EVP_KEYMGMT *tmp_keymgmt = NULL;
1775
0
    int selection = OSSL_KEYMGMT_SELECT_ALL;
1776
0
    void *keydata = NULL;
1777
0
    int check;
1778
1779
0
    if (pk == NULL)
1780
0
        return NULL;
1781
1782
    /* No key data => nothing to export */
1783
0
    check = 1;
1784
0
#ifndef FIPS_MODULE
1785
0
    check = check && pk->pkey.ptr == NULL;
1786
0
#endif
1787
0
    check = check && pk->keydata == NULL;
1788
0
    if (check)
1789
0
        return NULL;
1790
1791
0
#ifndef FIPS_MODULE
1792
0
    if (pk->pkey.ptr != NULL) {
1793
        /*
1794
         * If the legacy key doesn't have an dirty counter or export function,
1795
         * give up
1796
         */
1797
0
        if (pk->ameth->dirty_cnt == NULL || pk->ameth->export_to == NULL)
1798
0
            return NULL;
1799
0
    }
1800
0
#endif
1801
1802
0
    if (keymgmt != NULL) {
1803
0
        tmp_keymgmt = *keymgmt;
1804
0
        *keymgmt = NULL;
1805
0
    }
1806
1807
    /*
1808
     * If no keymgmt was given or found, get a default keymgmt.  We do so by
1809
     * letting EVP_PKEY_CTX_new_from_pkey() do it for us, then we steal it.
1810
     */
1811
0
    if (tmp_keymgmt == NULL) {
1812
0
        EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pk, propquery);
1813
1814
0
        if (ctx == NULL)
1815
0
            goto end;
1816
0
        allocated_keymgmt = tmp_keymgmt = ctx->keymgmt;
1817
0
        ctx->keymgmt = NULL;
1818
0
        EVP_PKEY_CTX_free(ctx);
1819
0
    }
1820
1821
    /* If there's still no keymgmt to be had, give up */
1822
0
    if (tmp_keymgmt == NULL)
1823
0
        goto end;
1824
1825
0
#ifndef FIPS_MODULE
1826
0
    if (pk->pkey.ptr != NULL) {
1827
0
        OP_CACHE_ELEM *op;
1828
0
        OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
1829
0
        OSSL_PARAM *p = NULL;
1830
1831
        /*
1832
         * If the legacy "origin" hasn't changed since last time, we try
1833
         * to find our keymgmt in the operation cache.  If it has changed,
1834
         * |i| remains zero, and we will clear the cache further down.
1835
         */
1836
0
        if (pk->ameth->dirty_cnt(pk) == pk->dirty_cnt_copy) {
1837
0
            if (!CRYPTO_THREAD_read_lock(pk->lock))
1838
0
                goto end;
1839
0
            op = evp_keymgmt_util_find_operation_cache(pk, tmp_keymgmt,
1840
0
                selection);
1841
1842
            /*
1843
             * If |tmp_keymgmt| is present in the operation cache, it means
1844
             * that export doesn't need to be redone.  In that case, we take
1845
             * token copies of the cached pointers, to have token success
1846
             * values to return. It is possible (e.g. in a no-cached-fetch
1847
             * build), for op->keymgmt to be a different pointer to tmp_keymgmt
1848
             * even though the name/provider must be the same. In other words
1849
             * the keymgmt instance may be different but still equivalent, i.e.
1850
             * same algorithm/provider instance - but we make the simplifying
1851
             * assumption that the keydata can be used with either keymgmt
1852
             * instance. Not doing so introduces significant complexity and
1853
             * probably requires refactoring - since we would have to ripple
1854
             * the change in keymgmt instance up the call chain.
1855
             */
1856
0
            if (op != NULL && op->keymgmt != NULL) {
1857
0
                keydata = op->keydata;
1858
0
                CRYPTO_THREAD_unlock(pk->lock);
1859
0
                goto end;
1860
0
            }
1861
0
            CRYPTO_THREAD_unlock(pk->lock);
1862
0
        }
1863
1864
        /* Make sure that the keymgmt key type matches the legacy NID */
1865
0
        if (!EVP_KEYMGMT_is_a(tmp_keymgmt, OBJ_nid2sn(pk->type)))
1866
0
            goto end;
1867
1868
0
        if (strcmp(OSSL_PROVIDER_get0_name(EVP_KEYMGMT_get0_provider(tmp_keymgmt)), "default") == 0) {
1869
            /*
1870
             * We attempt to pass the low-level object to the keymgmt. We only
1871
             * support this via an internal use only parameter. We break the
1872
             * normal rules that prevent passing complex objects via OSSL_PARAM,
1873
             * but this is only for the default provider where we can get away
1874
             * with this. This is necessary here for backwards compatibility
1875
             * reasons.
1876
             */
1877
0
            params[0] = OSSL_PARAM_construct_octet_ptr("legacy-object",
1878
0
                &pk->pkey.ptr, sizeof(pk->pkey.ptr));
1879
0
            p = params;
1880
0
        } else if (propquery != NULL) {
1881
0
            params[0] = OSSL_PARAM_construct_utf8_string(
1882
0
                OSSL_PKEY_PARAM_PROPERTIES, (char *)propquery, 0);
1883
0
            p = params;
1884
0
        }
1885
0
        keydata = evp_keymgmt_newdata(tmp_keymgmt, p);
1886
0
        if (keydata == NULL)
1887
0
            goto end;
1888
1889
        /*
1890
         * We skip the export if the key data we got back is actually the same
1891
         * as the low level object we passed in
1892
         */
1893
0
        if (keydata != pk->pkey.ptr
1894
0
            && !pk->ameth->export_to(pk, keydata, tmp_keymgmt->import, libctx, propquery)) {
1895
0
            evp_keymgmt_freedata(tmp_keymgmt, keydata);
1896
0
            keydata = NULL;
1897
0
            goto end;
1898
0
        }
1899
1900
        /*
1901
         * If the dirty counter changed since last time, then clear the
1902
         * operation cache.  In that case, we know that |i| is zero.  Just
1903
         * in case this is a re-export, we increment then decrement the
1904
         * keymgmt reference counter.
1905
         */
1906
0
        if (!EVP_KEYMGMT_up_ref(tmp_keymgmt)) { /* refcnt++ */
1907
0
            evp_keymgmt_freedata(tmp_keymgmt, keydata);
1908
0
            keydata = NULL;
1909
0
            goto end;
1910
0
        }
1911
1912
0
        if (!CRYPTO_THREAD_write_lock(pk->lock))
1913
0
            goto end;
1914
1915
0
        if (pk->ameth->dirty_cnt(pk) != pk->dirty_cnt_copy)
1916
0
            evp_keymgmt_util_clear_operation_cache(pk);
1917
1918
0
        EVP_KEYMGMT_free(tmp_keymgmt); /* refcnt-- */
1919
1920
        /* Check to make sure some other thread didn't get there first */
1921
0
        op = evp_keymgmt_util_find_operation_cache(pk, tmp_keymgmt, selection);
1922
0
        if (op != NULL && op->keymgmt != NULL) {
1923
0
            void *tmp_keydata = op->keydata;
1924
1925
0
            CRYPTO_THREAD_unlock(pk->lock);
1926
0
            evp_keymgmt_freedata(tmp_keymgmt, keydata);
1927
0
            keydata = tmp_keydata;
1928
0
            goto end;
1929
0
        }
1930
1931
        /* Add the new export to the operation cache */
1932
0
        if (!evp_keymgmt_util_cache_keydata(pk, tmp_keymgmt, keydata,
1933
0
                selection)) {
1934
0
            CRYPTO_THREAD_unlock(pk->lock);
1935
0
            evp_keymgmt_freedata(tmp_keymgmt, keydata);
1936
0
            keydata = NULL;
1937
0
            goto end;
1938
0
        }
1939
1940
        /* Synchronize the dirty count */
1941
0
        pk->dirty_cnt_copy = pk->ameth->dirty_cnt(pk);
1942
1943
0
        CRYPTO_THREAD_unlock(pk->lock);
1944
0
        goto end;
1945
0
    }
1946
0
#endif /* FIPS_MODULE */
1947
1948
0
    keydata = evp_keymgmt_util_export_to_provider(pk, tmp_keymgmt, selection);
1949
1950
0
end:
1951
    /*
1952
     * If nothing was exported, |tmp_keymgmt| might point at a freed
1953
     * EVP_KEYMGMT, so we clear it to be safe.  It shouldn't be useful for
1954
     * the caller either way in that case.
1955
     */
1956
0
    if (keydata == NULL)
1957
0
        tmp_keymgmt = NULL;
1958
1959
0
    if (keymgmt != NULL && tmp_keymgmt != NULL) {
1960
0
        *keymgmt = tmp_keymgmt;
1961
0
        allocated_keymgmt = NULL;
1962
0
    }
1963
1964
0
    EVP_KEYMGMT_free(allocated_keymgmt);
1965
0
    return keydata;
1966
0
}
1967
1968
#ifndef FIPS_MODULE
1969
int evp_pkey_copy_downgraded(EVP_PKEY **dest, const EVP_PKEY *src)
1970
0
{
1971
0
    EVP_PKEY *allocpkey = NULL;
1972
1973
0
    if (!ossl_assert(dest != NULL))
1974
0
        return 0;
1975
1976
0
    if (evp_pkey_is_assigned(src) && evp_pkey_is_provided(src)) {
1977
0
        EVP_KEYMGMT *keymgmt = src->keymgmt;
1978
0
        void *keydata = src->keydata;
1979
0
        int type = src->type;
1980
0
        const char *keytype = NULL;
1981
1982
0
        keytype = EVP_KEYMGMT_get0_name(keymgmt);
1983
1984
        /*
1985
         * If the type is EVP_PKEY_NONE, then we have a problem somewhere
1986
         * else in our code.  If it's not one of the well known EVP_PKEY_xxx
1987
         * values, it should at least be EVP_PKEY_KEYMGMT at this point.
1988
         * The check is kept as a safety measure.
1989
         */
1990
0
        if (!ossl_assert(type != EVP_PKEY_NONE)) {
1991
0
            ERR_raise_data(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR,
1992
0
                "keymgmt key type = %s but legacy type = EVP_PKEY_NONE",
1993
0
                keytype);
1994
0
            return 0;
1995
0
        }
1996
1997
        /* Prefer the legacy key type name for error reporting */
1998
0
        if (type != EVP_PKEY_KEYMGMT)
1999
0
            keytype = OBJ_nid2sn(type);
2000
2001
        /* Make sure we have a clean slate to copy into */
2002
0
        if (*dest == NULL) {
2003
0
            allocpkey = *dest = EVP_PKEY_new();
2004
0
            if (*dest == NULL) {
2005
0
                ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
2006
0
                return 0;
2007
0
            }
2008
0
        } else {
2009
0
            evp_pkey_free_it(*dest);
2010
0
        }
2011
2012
0
        if (EVP_PKEY_set_type(*dest, type)) {
2013
            /* If the key is typed but empty, we're done */
2014
0
            if (keydata == NULL)
2015
0
                return 1;
2016
2017
0
            if ((*dest)->ameth->import_from == NULL) {
2018
0
                ERR_raise_data(ERR_LIB_EVP, EVP_R_NO_IMPORT_FUNCTION,
2019
0
                    "key type = %s", keytype);
2020
0
            } else {
2021
                /*
2022
                 * We perform the export in the same libctx as the keymgmt
2023
                 * that we are using.
2024
                 */
2025
0
                OSSL_LIB_CTX *libctx = ossl_provider_libctx(keymgmt->prov);
2026
0
                EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_from_pkey(libctx, *dest, NULL);
2027
2028
0
                if (pctx == NULL)
2029
0
                    ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
2030
2031
0
                if (pctx != NULL
2032
0
                    && evp_keymgmt_export(keymgmt, keydata,
2033
0
                        OSSL_KEYMGMT_SELECT_ALL,
2034
0
                        (*dest)->ameth->import_from,
2035
0
                        pctx)) {
2036
                    /* Synchronize the dirty count */
2037
0
                    (*dest)->dirty_cnt_copy = (*dest)->ameth->dirty_cnt(*dest);
2038
2039
0
                    EVP_PKEY_CTX_free(pctx);
2040
0
                    return 1;
2041
0
                }
2042
0
                EVP_PKEY_CTX_free(pctx);
2043
0
            }
2044
2045
0
            ERR_raise_data(ERR_LIB_EVP, EVP_R_KEYMGMT_EXPORT_FAILURE,
2046
0
                "key type = %s", keytype);
2047
0
        }
2048
0
    }
2049
2050
0
    if (allocpkey != NULL) {
2051
0
        EVP_PKEY_free(allocpkey);
2052
0
        *dest = NULL;
2053
0
    }
2054
0
    return 0;
2055
0
}
2056
2057
void *evp_pkey_get_legacy(EVP_PKEY *pk)
2058
0
{
2059
0
    EVP_PKEY *tmp_copy = NULL;
2060
0
    void *ret = NULL;
2061
2062
0
    if (!ossl_assert(pk != NULL))
2063
0
        return NULL;
2064
2065
    /*
2066
     * If this isn't an assigned provider side key, we just use any existing
2067
     * origin legacy key.
2068
     */
2069
0
    if (!evp_pkey_is_assigned(pk))
2070
0
        return NULL;
2071
0
    if (!evp_pkey_is_provided(pk))
2072
0
        return pk->pkey.ptr;
2073
2074
0
    if (!CRYPTO_THREAD_read_lock(pk->lock))
2075
0
        return NULL;
2076
2077
0
    ret = pk->legacy_cache_pkey.ptr;
2078
2079
0
    if (!CRYPTO_THREAD_unlock(pk->lock))
2080
0
        return NULL;
2081
2082
0
    if (ret != NULL)
2083
0
        return ret;
2084
2085
0
    if (!evp_pkey_copy_downgraded(&tmp_copy, pk))
2086
0
        goto err;
2087
2088
0
    if (!CRYPTO_THREAD_write_lock(pk->lock))
2089
0
        goto err;
2090
2091
    /* Check again in case some other thread has updated it in the meantime */
2092
0
    ret = pk->legacy_cache_pkey.ptr;
2093
0
    if (ret == NULL) {
2094
        /* Steal the legacy key reference from the temporary copy */
2095
0
        ret = pk->legacy_cache_pkey.ptr = tmp_copy->pkey.ptr;
2096
0
        tmp_copy->pkey.ptr = NULL;
2097
0
    }
2098
2099
0
    if (!CRYPTO_THREAD_unlock(pk->lock)) {
2100
0
        ret = NULL;
2101
0
        goto err;
2102
0
    }
2103
2104
0
err:
2105
0
    EVP_PKEY_free(tmp_copy);
2106
2107
0
    return ret;
2108
0
}
2109
#endif /* FIPS_MODULE */
2110
2111
int EVP_PKEY_get_bn_param(const EVP_PKEY *pkey, const char *key_name,
2112
    BIGNUM **bn)
2113
0
{
2114
0
    int ret = 0;
2115
0
    OSSL_PARAM params[2];
2116
0
    unsigned char buffer[2048];
2117
0
    unsigned char *buf = NULL;
2118
0
    size_t buf_sz = 0;
2119
2120
0
    if (key_name == NULL
2121
0
        || bn == NULL)
2122
0
        return 0;
2123
2124
0
    memset(buffer, 0, sizeof(buffer));
2125
0
    params[0] = OSSL_PARAM_construct_BN(key_name, buffer, sizeof(buffer));
2126
0
    params[1] = OSSL_PARAM_construct_end();
2127
0
    if (!EVP_PKEY_get_params(pkey, params)) {
2128
0
        if (!OSSL_PARAM_modified(params) || params[0].return_size == 0)
2129
0
            return 0;
2130
0
        buf_sz = params[0].return_size;
2131
        /*
2132
         * If it failed because the buffer was too small then allocate the
2133
         * required buffer size and retry.
2134
         */
2135
0
        buf = OPENSSL_zalloc(buf_sz);
2136
0
        if (buf == NULL)
2137
0
            return 0;
2138
0
        params[0].data = buf;
2139
0
        params[0].data_size = buf_sz;
2140
2141
0
        if (!EVP_PKEY_get_params(pkey, params))
2142
0
            goto err;
2143
0
    }
2144
    /* Fail if the param was not found */
2145
0
    if (!OSSL_PARAM_modified(params))
2146
0
        goto err;
2147
0
    ret = OSSL_PARAM_get_BN(params, bn);
2148
0
err:
2149
0
    if (buf != NULL) {
2150
0
        if (OSSL_PARAM_modified(params))
2151
0
            OPENSSL_clear_free(buf, buf_sz);
2152
0
        else
2153
0
            OPENSSL_free(buf);
2154
0
    } else if (OSSL_PARAM_modified(params)) {
2155
0
        OPENSSL_cleanse(buffer, params[0].data_size);
2156
0
    }
2157
0
    return ret;
2158
0
}
2159
2160
int EVP_PKEY_get_octet_string_param(const EVP_PKEY *pkey, const char *key_name,
2161
    unsigned char *buf, size_t max_buf_sz,
2162
    size_t *out_len)
2163
0
{
2164
0
    OSSL_PARAM params[2];
2165
0
    int ret1 = 0, ret2 = 0;
2166
2167
0
    if (key_name == NULL)
2168
0
        return 0;
2169
2170
0
    params[0] = OSSL_PARAM_construct_octet_string(key_name, buf, max_buf_sz);
2171
0
    params[1] = OSSL_PARAM_construct_end();
2172
0
    if ((ret1 = EVP_PKEY_get_params(pkey, params)))
2173
0
        ret2 = OSSL_PARAM_modified(params);
2174
0
    if (ret2 && out_len != NULL)
2175
0
        *out_len = params[0].return_size;
2176
0
    return ret1 && ret2;
2177
0
}
2178
2179
int EVP_PKEY_get_utf8_string_param(const EVP_PKEY *pkey, const char *key_name,
2180
    char *str, size_t max_buf_sz,
2181
    size_t *out_len)
2182
0
{
2183
0
    OSSL_PARAM params[2];
2184
0
    int ret1 = 0, ret2 = 0;
2185
2186
0
    if (key_name == NULL)
2187
0
        return 0;
2188
2189
0
    params[0] = OSSL_PARAM_construct_utf8_string(key_name, str, max_buf_sz);
2190
0
    params[1] = OSSL_PARAM_construct_end();
2191
0
    if ((ret1 = EVP_PKEY_get_params(pkey, params)))
2192
0
        ret2 = OSSL_PARAM_modified(params);
2193
0
    if (ret2 && out_len != NULL)
2194
0
        *out_len = params[0].return_size;
2195
2196
0
    if (ret2 && params[0].return_size == max_buf_sz)
2197
        /* There was no space for a NUL byte */
2198
0
        return 0;
2199
    /* Add a terminating NUL byte for good measure */
2200
0
    if (ret2 && str != NULL)
2201
0
        str[params[0].return_size] = '\0';
2202
2203
0
    return ret1 && ret2;
2204
0
}
2205
2206
int EVP_PKEY_get_int_param(const EVP_PKEY *pkey, const char *key_name,
2207
    int *out)
2208
0
{
2209
0
    OSSL_PARAM params[2];
2210
2211
0
    if (key_name == NULL)
2212
0
        return 0;
2213
2214
0
    params[0] = OSSL_PARAM_construct_int(key_name, out);
2215
0
    params[1] = OSSL_PARAM_construct_end();
2216
0
    return EVP_PKEY_get_params(pkey, params)
2217
0
        && OSSL_PARAM_modified(params);
2218
0
}
2219
2220
int EVP_PKEY_get_size_t_param(const EVP_PKEY *pkey, const char *key_name,
2221
    size_t *out)
2222
0
{
2223
0
    OSSL_PARAM params[2];
2224
2225
0
    if (key_name == NULL)
2226
0
        return 0;
2227
2228
0
    params[0] = OSSL_PARAM_construct_size_t(key_name, out);
2229
0
    params[1] = OSSL_PARAM_construct_end();
2230
0
    return EVP_PKEY_get_params(pkey, params)
2231
0
        && OSSL_PARAM_modified(params);
2232
0
}
2233
2234
int EVP_PKEY_set_int_param(EVP_PKEY *pkey, const char *key_name, int in)
2235
0
{
2236
0
    OSSL_PARAM params[2];
2237
2238
0
    if (key_name == NULL)
2239
0
        return 0;
2240
2241
0
    params[0] = OSSL_PARAM_construct_int(key_name, &in);
2242
0
    params[1] = OSSL_PARAM_construct_end();
2243
0
    return EVP_PKEY_set_params(pkey, params);
2244
0
}
2245
2246
int EVP_PKEY_set_size_t_param(EVP_PKEY *pkey, const char *key_name, size_t in)
2247
0
{
2248
0
    OSSL_PARAM params[2];
2249
2250
0
    if (key_name == NULL)
2251
0
        return 0;
2252
2253
0
    params[0] = OSSL_PARAM_construct_size_t(key_name, &in);
2254
0
    params[1] = OSSL_PARAM_construct_end();
2255
0
    return EVP_PKEY_set_params(pkey, params);
2256
0
}
2257
2258
int EVP_PKEY_set_bn_param(EVP_PKEY *pkey, const char *key_name,
2259
    const BIGNUM *bn)
2260
0
{
2261
0
    OSSL_PARAM params[2];
2262
0
    unsigned char buffer[2048];
2263
0
    int bsize = 0;
2264
2265
0
    if (key_name == NULL
2266
0
        || bn == NULL
2267
0
        || pkey == NULL
2268
0
        || !evp_pkey_is_assigned(pkey))
2269
0
        return 0;
2270
2271
0
    bsize = BN_num_bytes(bn);
2272
0
    if (!ossl_assert(bsize <= (int)sizeof(buffer)))
2273
0
        return 0;
2274
2275
0
    if (BN_bn2nativepad(bn, buffer, bsize) < 0)
2276
0
        return 0;
2277
0
    params[0] = OSSL_PARAM_construct_BN(key_name, buffer, bsize);
2278
0
    params[1] = OSSL_PARAM_construct_end();
2279
0
    return EVP_PKEY_set_params(pkey, params);
2280
0
}
2281
2282
int EVP_PKEY_set_utf8_string_param(EVP_PKEY *pkey, const char *key_name,
2283
    const char *str)
2284
0
{
2285
0
    OSSL_PARAM params[2];
2286
2287
0
    if (key_name == NULL)
2288
0
        return 0;
2289
2290
0
    params[0] = OSSL_PARAM_construct_utf8_string(key_name, (char *)str, 0);
2291
0
    params[1] = OSSL_PARAM_construct_end();
2292
0
    return EVP_PKEY_set_params(pkey, params);
2293
0
}
2294
2295
int EVP_PKEY_set_octet_string_param(EVP_PKEY *pkey, const char *key_name,
2296
    const unsigned char *buf, size_t bsize)
2297
0
{
2298
0
    OSSL_PARAM params[2];
2299
2300
0
    if (key_name == NULL)
2301
0
        return 0;
2302
2303
0
    params[0] = OSSL_PARAM_construct_octet_string(key_name,
2304
0
        (unsigned char *)buf, bsize);
2305
0
    params[1] = OSSL_PARAM_construct_end();
2306
0
    return EVP_PKEY_set_params(pkey, params);
2307
0
}
2308
2309
const OSSL_PARAM *EVP_PKEY_settable_params(const EVP_PKEY *pkey)
2310
0
{
2311
0
    return (pkey != NULL && evp_pkey_is_provided(pkey))
2312
0
        ? EVP_KEYMGMT_settable_params(pkey->keymgmt)
2313
0
        : NULL;
2314
0
}
2315
2316
int EVP_PKEY_set_params(EVP_PKEY *pkey, OSSL_PARAM params[])
2317
0
{
2318
0
    if (pkey != NULL) {
2319
0
        if (evp_pkey_is_provided(pkey)) {
2320
0
            pkey->dirty_cnt++;
2321
0
            return evp_keymgmt_set_params(pkey->keymgmt, pkey->keydata, params);
2322
0
        }
2323
0
#ifndef FIPS_MODULE
2324
        /*
2325
         * We will hopefully never find the need to set individual data in
2326
         * EVP_PKEYs with a legacy internal key, but we can't be entirely
2327
         * sure.  This bit of code can be enabled if we find the need.  If
2328
         * not, it can safely be removed when #legacy support is removed.
2329
         */
2330
#if 0
2331
        else if (evp_pkey_is_legacy(pkey)) {
2332
            return evp_pkey_set_params_to_ctrl(pkey, params);
2333
        }
2334
#endif
2335
0
#endif
2336
0
    }
2337
0
    ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY);
2338
0
    return 0;
2339
0
}
2340
2341
const OSSL_PARAM *EVP_PKEY_gettable_params(const EVP_PKEY *pkey)
2342
0
{
2343
0
    return (pkey != NULL && evp_pkey_is_provided(pkey))
2344
0
        ? EVP_KEYMGMT_gettable_params(pkey->keymgmt)
2345
0
        : NULL;
2346
0
}
2347
2348
int EVP_PKEY_get_params(const EVP_PKEY *pkey, OSSL_PARAM params[])
2349
0
{
2350
0
    if (pkey != NULL) {
2351
0
        if (evp_pkey_is_provided(pkey))
2352
0
            return evp_keymgmt_get_params(pkey->keymgmt, pkey->keydata, params) > 0;
2353
0
#ifndef FIPS_MODULE
2354
0
        else if (evp_pkey_is_legacy(pkey))
2355
0
            return evp_pkey_get_params_to_ctrl(pkey, params) > 0;
2356
0
#endif
2357
0
    }
2358
0
    ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY);
2359
0
    return 0;
2360
0
}
2361
2362
#ifndef FIPS_MODULE
2363
int EVP_PKEY_get_ec_point_conv_form(const EVP_PKEY *pkey)
2364
0
{
2365
0
    char name[80];
2366
0
    size_t name_len;
2367
2368
0
    if (pkey == NULL)
2369
0
        return 0;
2370
2371
0
    if (pkey->keymgmt == NULL
2372
0
        || pkey->keydata == NULL) {
2373
0
#ifndef OPENSSL_NO_EC
2374
        /* Might work through the legacy route */
2375
0
        const EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey);
2376
2377
0
        if (ec != NULL)
2378
0
            return EC_KEY_get_conv_form(ec);
2379
0
#endif
2380
0
        return 0;
2381
0
    }
2382
2383
0
    if (!EVP_PKEY_get_utf8_string_param(pkey,
2384
0
            OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT,
2385
0
            name, sizeof(name), &name_len))
2386
0
        return 0;
2387
2388
0
    if (strcmp(name, "uncompressed") == 0)
2389
0
        return POINT_CONVERSION_UNCOMPRESSED;
2390
2391
0
    if (strcmp(name, "compressed") == 0)
2392
0
        return POINT_CONVERSION_COMPRESSED;
2393
2394
0
    if (strcmp(name, "hybrid") == 0)
2395
0
        return POINT_CONVERSION_HYBRID;
2396
2397
0
    return 0;
2398
0
}
2399
2400
int EVP_PKEY_get_field_type(const EVP_PKEY *pkey)
2401
0
{
2402
0
    char fstr[80];
2403
0
    size_t fstrlen;
2404
2405
0
    if (pkey == NULL)
2406
0
        return 0;
2407
2408
0
    if (pkey->keymgmt == NULL
2409
0
        || pkey->keydata == NULL) {
2410
0
#ifndef OPENSSL_NO_EC
2411
        /* Might work through the legacy route */
2412
0
        const EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey);
2413
0
        const EC_GROUP *grp;
2414
2415
0
        if (ec == NULL)
2416
0
            return 0;
2417
0
        grp = EC_KEY_get0_group(ec);
2418
0
        if (grp == NULL)
2419
0
            return 0;
2420
2421
0
        return EC_GROUP_get_field_type(grp);
2422
#else
2423
        return 0;
2424
#endif
2425
0
    }
2426
2427
0
    if (!EVP_PKEY_get_utf8_string_param(pkey, OSSL_PKEY_PARAM_EC_FIELD_TYPE,
2428
0
            fstr, sizeof(fstr), &fstrlen))
2429
0
        return 0;
2430
2431
0
    if (strcmp(fstr, SN_X9_62_prime_field) == 0)
2432
0
        return NID_X9_62_prime_field;
2433
0
    else if (strcmp(fstr, SN_X9_62_characteristic_two_field))
2434
0
        return NID_X9_62_characteristic_two_field;
2435
2436
0
    return 0;
2437
0
}
2438
#endif