xref: /freebsd/crypto/openssl/crypto/evp/p_legacy.c (revision e7be843b4a162e68651d3911f0357ed464915629)
1 /*
2  * Copyright 1995-2025 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9 
10 /*
11  * Legacy EVP_PKEY assign/set/get APIs are deprecated for public use, but
12  * still ok for internal use, particularly in providers.
13  */
14 #include "internal/deprecated.h"
15 
16 #include <openssl/types.h>
17 #include <openssl/evp.h>
18 #include <openssl/err.h>
19 #include <openssl/rsa.h>
20 #include <openssl/ec.h>
21 #include "crypto/types.h"
22 #include "crypto/evp.h"
23 #include "evp_local.h"
24 
EVP_PKEY_set1_RSA(EVP_PKEY * pkey,RSA * key)25 int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key)
26 {
27     int ret;
28 
29     if (!RSA_up_ref(key))
30         return 0;
31 
32     ret = EVP_PKEY_assign_RSA(pkey, key);
33 
34     if (!ret)
35         RSA_free(key);
36 
37     return ret;
38 }
39 
evp_pkey_get0_RSA_int(const EVP_PKEY * pkey)40 RSA *evp_pkey_get0_RSA_int(const EVP_PKEY *pkey)
41 {
42     if (pkey->type != EVP_PKEY_RSA && pkey->type != EVP_PKEY_RSA_PSS) {
43         ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_AN_RSA_KEY);
44         return NULL;
45     }
46     return evp_pkey_get_legacy((EVP_PKEY *)pkey);
47 }
48 
EVP_PKEY_get0_RSA(const EVP_PKEY * pkey)49 const RSA *EVP_PKEY_get0_RSA(const EVP_PKEY *pkey)
50 {
51     return evp_pkey_get0_RSA_int(pkey);
52 }
53 
EVP_PKEY_get1_RSA(EVP_PKEY * pkey)54 RSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey)
55 {
56     RSA *ret = evp_pkey_get0_RSA_int(pkey);
57 
58     if (ret != NULL && !RSA_up_ref(ret))
59         ret = NULL;
60 
61     return ret;
62 }
63 
64 #ifndef OPENSSL_NO_EC
EVP_PKEY_set1_EC_KEY(EVP_PKEY * pkey,EC_KEY * key)65 int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key)
66 {
67     if (!EC_KEY_up_ref(key))
68         return 0;
69     if (!EVP_PKEY_assign_EC_KEY(pkey, key)) {
70         EC_KEY_free(key);
71         return 0;
72     }
73     return 1;
74 }
75 
evp_pkey_get0_EC_KEY_int(const EVP_PKEY * pkey)76 EC_KEY *evp_pkey_get0_EC_KEY_int(const EVP_PKEY *pkey)
77 {
78     if (EVP_PKEY_get_base_id(pkey) != EVP_PKEY_EC) {
79         ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_EC_KEY);
80         return NULL;
81     }
82     return evp_pkey_get_legacy((EVP_PKEY *)pkey);
83 }
84 
EVP_PKEY_get0_EC_KEY(const EVP_PKEY * pkey)85 const EC_KEY *EVP_PKEY_get0_EC_KEY(const EVP_PKEY *pkey)
86 {
87     return evp_pkey_get0_EC_KEY_int(pkey);
88 }
89 
EVP_PKEY_get1_EC_KEY(EVP_PKEY * pkey)90 EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey)
91 {
92     EC_KEY *ret = evp_pkey_get0_EC_KEY_int(pkey);
93 
94     if (ret != NULL && !EC_KEY_up_ref(ret))
95         ret = NULL;
96     return ret;
97 }
98 #endif /* OPENSSL_NO_EC */
99