Coverage Report

Created: 2025-06-13 06:55

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