/src/openssl/crypto/engine/eng_pkey.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2001-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 | | /* We need to use some engine deprecated APIs */ |
11 | | #define OPENSSL_SUPPRESS_DEPRECATED |
12 | | |
13 | | #include "eng_local.h" |
14 | | |
15 | | /* Basic get/set stuff */ |
16 | | |
17 | | int ENGINE_set_load_privkey_function(ENGINE *e, |
18 | | ENGINE_LOAD_KEY_PTR loadpriv_f) |
19 | 0 | { |
20 | 0 | e->load_privkey = loadpriv_f; |
21 | 0 | return 1; |
22 | 0 | } |
23 | | |
24 | | int ENGINE_set_load_pubkey_function(ENGINE *e, ENGINE_LOAD_KEY_PTR loadpub_f) |
25 | 0 | { |
26 | 0 | e->load_pubkey = loadpub_f; |
27 | 0 | return 1; |
28 | 0 | } |
29 | | |
30 | | int ENGINE_set_load_ssl_client_cert_function(ENGINE *e, |
31 | | ENGINE_SSL_CLIENT_CERT_PTR |
32 | | loadssl_f) |
33 | 0 | { |
34 | 0 | e->load_ssl_client_cert = loadssl_f; |
35 | 0 | return 1; |
36 | 0 | } |
37 | | |
38 | | ENGINE_LOAD_KEY_PTR ENGINE_get_load_privkey_function(const ENGINE *e) |
39 | 0 | { |
40 | 0 | return e->load_privkey; |
41 | 0 | } |
42 | | |
43 | | ENGINE_LOAD_KEY_PTR ENGINE_get_load_pubkey_function(const ENGINE *e) |
44 | 0 | { |
45 | 0 | return e->load_pubkey; |
46 | 0 | } |
47 | | |
48 | | ENGINE_SSL_CLIENT_CERT_PTR ENGINE_get_ssl_client_cert_function(const ENGINE |
49 | | *e) |
50 | 0 | { |
51 | 0 | return e->load_ssl_client_cert; |
52 | 0 | } |
53 | | |
54 | | /* API functions to load public/private keys */ |
55 | | |
56 | | EVP_PKEY *ENGINE_load_private_key(ENGINE *e, const char *key_id, |
57 | | UI_METHOD *ui_method, void *callback_data) |
58 | 0 | { |
59 | 0 | EVP_PKEY *pkey; |
60 | |
|
61 | 0 | if (e == NULL) { |
62 | 0 | ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER); |
63 | 0 | return NULL; |
64 | 0 | } |
65 | 0 | if (!CRYPTO_THREAD_write_lock(global_engine_lock)) |
66 | 0 | return NULL; |
67 | 0 | if (e->funct_ref == 0) { |
68 | 0 | CRYPTO_THREAD_unlock(global_engine_lock); |
69 | 0 | ERR_raise(ERR_LIB_ENGINE, ENGINE_R_NOT_INITIALISED); |
70 | 0 | return NULL; |
71 | 0 | } |
72 | 0 | CRYPTO_THREAD_unlock(global_engine_lock); |
73 | 0 | if (!e->load_privkey) { |
74 | 0 | ERR_raise(ERR_LIB_ENGINE, ENGINE_R_NO_LOAD_FUNCTION); |
75 | 0 | return NULL; |
76 | 0 | } |
77 | 0 | pkey = e->load_privkey(e, key_id, ui_method, callback_data); |
78 | 0 | if (pkey == NULL) { |
79 | 0 | ERR_raise(ERR_LIB_ENGINE, ENGINE_R_FAILED_LOADING_PRIVATE_KEY); |
80 | 0 | return NULL; |
81 | 0 | } |
82 | | /* We enforce check for legacy key */ |
83 | 0 | switch (EVP_PKEY_get_id(pkey)) { |
84 | 0 | case EVP_PKEY_RSA: |
85 | 0 | { |
86 | 0 | RSA *rsa = EVP_PKEY_get1_RSA(pkey); |
87 | 0 | EVP_PKEY_set1_RSA(pkey, rsa); |
88 | 0 | RSA_free(rsa); |
89 | 0 | } |
90 | 0 | break; |
91 | 0 | # ifndef OPENSSL_NO_EC |
92 | 0 | case EVP_PKEY_SM2: |
93 | 0 | case EVP_PKEY_EC: |
94 | 0 | { |
95 | 0 | EC_KEY *ec = EVP_PKEY_get1_EC_KEY(pkey); |
96 | 0 | EVP_PKEY_set1_EC_KEY(pkey, ec); |
97 | 0 | EC_KEY_free(ec); |
98 | 0 | } |
99 | 0 | break; |
100 | 0 | # endif |
101 | 0 | # ifndef OPENSSL_NO_DSA |
102 | 0 | case EVP_PKEY_DSA: |
103 | 0 | { |
104 | 0 | DSA *dsa = EVP_PKEY_get1_DSA(pkey); |
105 | 0 | EVP_PKEY_set1_DSA(pkey, dsa); |
106 | 0 | DSA_free(dsa); |
107 | 0 | } |
108 | 0 | break; |
109 | 0 | #endif |
110 | 0 | # ifndef OPENSSL_NO_DH |
111 | 0 | case EVP_PKEY_DH: |
112 | 0 | { |
113 | 0 | DH *dh = EVP_PKEY_get1_DH(pkey); |
114 | 0 | EVP_PKEY_set1_DH(pkey, dh); |
115 | 0 | DH_free(dh); |
116 | 0 | } |
117 | 0 | break; |
118 | 0 | #endif |
119 | 0 | default: |
120 | | /*Do nothing */ |
121 | 0 | break; |
122 | 0 | } |
123 | | |
124 | 0 | return pkey; |
125 | 0 | } |
126 | | |
127 | | EVP_PKEY *ENGINE_load_public_key(ENGINE *e, const char *key_id, |
128 | | UI_METHOD *ui_method, void *callback_data) |
129 | 0 | { |
130 | 0 | EVP_PKEY *pkey; |
131 | |
|
132 | 0 | if (e == NULL) { |
133 | 0 | ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER); |
134 | 0 | return NULL; |
135 | 0 | } |
136 | 0 | if (!CRYPTO_THREAD_write_lock(global_engine_lock)) |
137 | 0 | return NULL; |
138 | 0 | if (e->funct_ref == 0) { |
139 | 0 | CRYPTO_THREAD_unlock(global_engine_lock); |
140 | 0 | ERR_raise(ERR_LIB_ENGINE, ENGINE_R_NOT_INITIALISED); |
141 | 0 | return NULL; |
142 | 0 | } |
143 | 0 | CRYPTO_THREAD_unlock(global_engine_lock); |
144 | 0 | if (!e->load_pubkey) { |
145 | 0 | ERR_raise(ERR_LIB_ENGINE, ENGINE_R_NO_LOAD_FUNCTION); |
146 | 0 | return NULL; |
147 | 0 | } |
148 | 0 | pkey = e->load_pubkey(e, key_id, ui_method, callback_data); |
149 | 0 | if (pkey == NULL) { |
150 | 0 | ERR_raise(ERR_LIB_ENGINE, ENGINE_R_FAILED_LOADING_PUBLIC_KEY); |
151 | 0 | return NULL; |
152 | 0 | } |
153 | 0 | return pkey; |
154 | 0 | } |
155 | | |
156 | | int ENGINE_load_ssl_client_cert(ENGINE *e, SSL *s, |
157 | | STACK_OF(X509_NAME) *ca_dn, X509 **pcert, |
158 | | EVP_PKEY **ppkey, STACK_OF(X509) **pother, |
159 | | UI_METHOD *ui_method, void *callback_data) |
160 | 0 | { |
161 | |
|
162 | 0 | if (e == NULL) { |
163 | 0 | ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER); |
164 | 0 | return 0; |
165 | 0 | } |
166 | 0 | if (!CRYPTO_THREAD_write_lock(global_engine_lock)) |
167 | 0 | return 0; |
168 | 0 | if (e->funct_ref == 0) { |
169 | 0 | CRYPTO_THREAD_unlock(global_engine_lock); |
170 | 0 | ERR_raise(ERR_LIB_ENGINE, ENGINE_R_NOT_INITIALISED); |
171 | 0 | return 0; |
172 | 0 | } |
173 | 0 | CRYPTO_THREAD_unlock(global_engine_lock); |
174 | 0 | if (!e->load_ssl_client_cert) { |
175 | 0 | ERR_raise(ERR_LIB_ENGINE, ENGINE_R_NO_LOAD_FUNCTION); |
176 | 0 | return 0; |
177 | 0 | } |
178 | 0 | return e->load_ssl_client_cert(e, s, ca_dn, pcert, ppkey, pother, |
179 | 0 | ui_method, callback_data); |
180 | 0 | } |