Coverage Report

Created: 2025-12-08 06:22

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