Coverage Report

Created: 2024-05-21 06:52

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