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