Coverage Report

Created: 2023-06-08 06:40

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