openssl: update to 1.1.1e
[openwrt/openwrt.git] / package / libs / openssl / patches / 430-e_devcrypto-make-the-dev-crypto-engine-dynamic.patch
1 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2 From: Eneas U de Queiroz <cote2004-github@yahoo.com>
3 Date: Tue, 6 Nov 2018 10:57:03 -0200
4 Subject: e_devcrypto: make the /dev/crypto engine dynamic
5
6 Engine has been moved from crypto/engine/eng_devcrypto.c to
7 engines/e_devcrypto.c.
8
9 Signed-off-by: Eneas U de Queiroz <cote2004-github@yahoo.com>
10
11 diff --git a/crypto/engine/build.info b/crypto/engine/build.info
12 index e00802a3fd..47fe948966 100644
13 --- a/crypto/engine/build.info
14 +++ b/crypto/engine/build.info
15 @@ -6,6 +6,3 @@ SOURCE[../../libcrypto]=\
16 tb_cipher.c tb_digest.c tb_pkmeth.c tb_asnmth.c tb_eckey.c \
17 eng_openssl.c eng_cnf.c eng_dyn.c \
18 eng_rdrand.c
19 -IF[{- !$disabled{devcryptoeng} -}]
20 - SOURCE[../../libcrypto]=eng_devcrypto.c
21 -ENDIF
22 diff --git a/crypto/init.c b/crypto/init.c
23 index 1b0d523bea..ee3e2eb075 100644
24 --- a/crypto/init.c
25 +++ b/crypto/init.c
26 @@ -329,18 +329,6 @@ DEFINE_RUN_ONCE_STATIC(ossl_init_engine_openssl)
27 engine_load_openssl_int();
28 return 1;
29 }
30 -# ifndef OPENSSL_NO_DEVCRYPTOENG
31 -static CRYPTO_ONCE engine_devcrypto = CRYPTO_ONCE_STATIC_INIT;
32 -DEFINE_RUN_ONCE_STATIC(ossl_init_engine_devcrypto)
33 -{
34 -# ifdef OPENSSL_INIT_DEBUG
35 - fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_devcrypto: "
36 - "engine_load_devcrypto_int()\n");
37 -# endif
38 - engine_load_devcrypto_int();
39 - return 1;
40 -}
41 -# endif
42
43 # ifndef OPENSSL_NO_RDRAND
44 static CRYPTO_ONCE engine_rdrand = CRYPTO_ONCE_STATIC_INIT;
45 @@ -365,6 +353,18 @@ DEFINE_RUN_ONCE_STATIC(ossl_init_engine_dynamic)
46 return 1;
47 }
48 # ifndef OPENSSL_NO_STATIC_ENGINE
49 +# ifndef OPENSSL_NO_DEVCRYPTOENG
50 +static CRYPTO_ONCE engine_devcrypto = CRYPTO_ONCE_STATIC_INIT;
51 +DEFINE_RUN_ONCE_STATIC(ossl_init_engine_devcrypto)
52 +{
53 +# ifdef OPENSSL_INIT_DEBUG
54 + fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_devcrypto: "
55 + "engine_load_devcrypto_int()\n");
56 +# endif
57 + engine_load_devcrypto_int();
58 + return 1;
59 +}
60 +# endif
61 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
62 static CRYPTO_ONCE engine_padlock = CRYPTO_ONCE_STATIC_INIT;
63 DEFINE_RUN_ONCE_STATIC(ossl_init_engine_padlock)
64 @@ -713,11 +713,6 @@ int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
65 if ((opts & OPENSSL_INIT_ENGINE_OPENSSL)
66 && !RUN_ONCE(&engine_openssl, ossl_init_engine_openssl))
67 return 0;
68 -# if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_DEVCRYPTOENG)
69 - if ((opts & OPENSSL_INIT_ENGINE_CRYPTODEV)
70 - && !RUN_ONCE(&engine_devcrypto, ossl_init_engine_devcrypto))
71 - return 0;
72 -# endif
73 # ifndef OPENSSL_NO_RDRAND
74 if ((opts & OPENSSL_INIT_ENGINE_RDRAND)
75 && !RUN_ONCE(&engine_rdrand, ossl_init_engine_rdrand))
76 @@ -727,6 +722,11 @@ int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
77 && !RUN_ONCE(&engine_dynamic, ossl_init_engine_dynamic))
78 return 0;
79 # ifndef OPENSSL_NO_STATIC_ENGINE
80 +# ifndef OPENSSL_NO_DEVCRYPTOENG
81 + if ((opts & OPENSSL_INIT_ENGINE_CRYPTODEV)
82 + && !RUN_ONCE(&engine_devcrypto, ossl_init_engine_devcrypto))
83 + return 0;
84 +# endif
85 # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
86 if ((opts & OPENSSL_INIT_ENGINE_PADLOCK)
87 && !RUN_ONCE(&engine_padlock, ossl_init_engine_padlock))
88 diff --git a/engines/build.info b/engines/build.info
89 index 1db771971c..33a25d7004 100644
90 --- a/engines/build.info
91 +++ b/engines/build.info
92 @@ -11,6 +11,9 @@ IF[{- !$disabled{"engine"} -}]
93 IF[{- !$disabled{afalgeng} -}]
94 SOURCE[../libcrypto]=e_afalg.c
95 ENDIF
96 + IF[{- !$disabled{"devcryptoeng"} -}]
97 + SOURCE[../libcrypto]=e_devcrypto.c
98 + ENDIF
99 ELSE
100 IF[{- !$disabled{hw} && !$disabled{'hw-padlock'} -}]
101 ENGINES=padlock
102 @@ -30,6 +33,12 @@ IF[{- !$disabled{"engine"} -}]
103 DEPEND[afalg]=../libcrypto
104 INCLUDE[afalg]= ../include
105 ENDIF
106 + IF[{- !$disabled{"devcryptoeng"} -}]
107 + ENGINES=devcrypto
108 + SOURCE[devcrypto]=e_devcrypto.c
109 + DEPEND[devcrypto]=../libcrypto
110 + INCLUDE[devcrypto]=../include
111 + ENDIF
112
113 ENGINES_NO_INST=ossltest dasync
114 SOURCE[dasync]=e_dasync.c
115 diff --git a/crypto/engine/eng_devcrypto.c b/engines/e_devcrypto.c
116 similarity index 95%
117 rename from crypto/engine/eng_devcrypto.c
118 rename to engines/e_devcrypto.c
119 index 0d420e50aa..3fcd81de7a 100644
120 --- a/crypto/engine/eng_devcrypto.c
121 +++ b/engines/e_devcrypto.c
122 @@ -7,7 +7,7 @@
123 * https://www.openssl.org/source/license.html
124 */
125
126 -#include "e_os.h"
127 +#include "../e_os.h"
128 #include <string.h>
129 #include <sys/types.h>
130 #include <sys/stat.h>
131 @@ -31,18 +31,20 @@
132 # define CHECK_BSD_STYLE_MACROS
133 #endif
134
135 +#define engine_devcrypto_id "devcrypto"
136 +
137 /*
138 * ONE global file descriptor for all sessions. This allows operations
139 * such as digest session data copying (see digest_copy()), but is also
140 * saner... why re-open /dev/crypto for every session?
141 */
142 -static int cfd;
143 +static int cfd = -1;
144 #define DEVCRYPTO_REQUIRE_ACCELERATED 0 /* require confirmation of acceleration */
145 #define DEVCRYPTO_USE_SOFTWARE 1 /* allow software drivers */
146 #define DEVCRYPTO_REJECT_SOFTWARE 2 /* only disallow confirmed software drivers */
147
148 -#define DEVCRYPTO_DEFAULT_USE_SOFDTRIVERS DEVCRYPTO_REJECT_SOFTWARE
149 -static int use_softdrivers = DEVCRYPTO_DEFAULT_USE_SOFDTRIVERS;
150 +#define DEVCRYPTO_DEFAULT_USE_SOFTDRIVERS DEVCRYPTO_REJECT_SOFTWARE
151 +static int use_softdrivers = DEVCRYPTO_DEFAULT_USE_SOFTDRIVERS;
152
153 /*
154 * cipher/digest status & acceleration definitions
155 @@ -341,6 +343,7 @@ static int cipher_ctrl(EVP_CIPHER_CTX *ctx, int type, int p1, void* p2)
156 struct cipher_ctx *to_cipher_ctx;
157
158 switch (type) {
159 +
160 case EVP_CTRL_COPY:
161 if (cipher_ctx == NULL)
162 return 1;
163 @@ -702,7 +705,6 @@ static int digest_init(EVP_MD_CTX *ctx)
164 SYSerr(SYS_F_IOCTL, errno);
165 return 0;
166 }
167 -
168 return 1;
169 }
170
171 @@ -1058,7 +1060,7 @@ static const ENGINE_CMD_DEFN devcrypto_cmds[] = {
172 OPENSSL_MSTR(DEVCRYPTO_USE_SOFTWARE) "=allow all drivers, "
173 OPENSSL_MSTR(DEVCRYPTO_REJECT_SOFTWARE)
174 "=use if acceleration can't be determined) [default="
175 - OPENSSL_MSTR(DEVCRYPTO_DEFAULT_USE_SOFDTRIVERS) "]",
176 + OPENSSL_MSTR(DEVCRYPTO_DEFAULT_USE_SOFTDRIVERS) "]",
177 ENGINE_CMD_FLAG_NUMERIC},
178 #endif
179
180 @@ -1166,55 +1168,70 @@ static int devcrypto_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
181 *
182 *****/
183
184 -static int devcrypto_unload(ENGINE *e)
185 -{
186 - destroy_all_cipher_methods();
187 -#ifdef IMPLEMENT_DIGEST
188 - destroy_all_digest_methods();
189 -#endif
190 -
191 - close(cfd);
192 -
193 - return 1;
194 -}
195 /*
196 - * This engine is always built into libcrypto, so it doesn't offer any
197 - * ability to be dynamically loadable.
198 + * Opens /dev/crypto
199 */
200 -void engine_load_devcrypto_int()
201 +static int open_devcrypto(void)
202 {
203 - ENGINE *e = NULL;
204 + if (cfd >= 0)
205 + return 1;
206
207 if ((cfd = open("/dev/crypto", O_RDWR, 0)) < 0) {
208 #ifndef ENGINE_DEVCRYPTO_DEBUG
209 if (errno != ENOENT)
210 #endif
211 fprintf(stderr, "Could not open /dev/crypto: %s\n", strerror(errno));
212 - return;
213 + return 0;
214 }
215
216 - if ((e = ENGINE_new()) == NULL
217 - || !ENGINE_set_destroy_function(e, devcrypto_unload)) {
218 - ENGINE_free(e);
219 - /*
220 - * We know that devcrypto_unload() won't be called when one of the
221 - * above two calls have failed, so we close cfd explicitly here to
222 - * avoid leaking resources.
223 - */
224 - close(cfd);
225 - return;
226 + return 1;
227 +}
228 +
229 +static int close_devcrypto(void)
230 +{
231 + int ret;
232 +
233 + if (cfd < 0)
234 + return 1;
235 + ret = close(cfd);
236 + cfd = -1;
237 + if (ret != 0) {
238 + fprintf(stderr, "Error closing /dev/crypto: %s\n", strerror(errno));
239 + return 0;
240 }
241 + return 1;
242 +}
243
244 - prepare_cipher_methods();
245 +static int devcrypto_unload(ENGINE *e)
246 +{
247 + destroy_all_cipher_methods();
248 #ifdef IMPLEMENT_DIGEST
249 - prepare_digest_methods();
250 + destroy_all_digest_methods();
251 #endif
252
253 - if (!ENGINE_set_id(e, "devcrypto")
254 + close_devcrypto();
255 +
256 + return 1;
257 +}
258 +
259 +static int bind_devcrypto(ENGINE *e) {
260 +
261 + if (!ENGINE_set_id(e, engine_devcrypto_id)
262 || !ENGINE_set_name(e, "/dev/crypto engine")
263 + || !ENGINE_set_destroy_function(e, devcrypto_unload)
264 || !ENGINE_set_cmd_defns(e, devcrypto_cmds)
265 - || !ENGINE_set_ctrl_function(e, devcrypto_ctrl)
266 + || !ENGINE_set_ctrl_function(e, devcrypto_ctrl))
267 + return 0;
268
269 + prepare_cipher_methods();
270 +#ifdef IMPLEMENT_DIGEST
271 + prepare_digest_methods();
272 +#endif
273 +
274 + return (ENGINE_set_ciphers(e, devcrypto_ciphers)
275 +#ifdef IMPLEMENT_DIGEST
276 + && ENGINE_set_digests(e, devcrypto_digests)
277 +#endif
278 /*
279 * Asymmetric ciphers aren't well supported with /dev/crypto. Among the BSD
280 * implementations, it seems to only exist in FreeBSD, and regarding the
281 @@ -1237,23 +1254,36 @@ void engine_load_devcrypto_int()
282 */
283 #if 0
284 # ifndef OPENSSL_NO_RSA
285 - || !ENGINE_set_RSA(e, devcrypto_rsa)
286 + && ENGINE_set_RSA(e, devcrypto_rsa)
287 # endif
288 # ifndef OPENSSL_NO_DSA
289 - || !ENGINE_set_DSA(e, devcrypto_dsa)
290 + && ENGINE_set_DSA(e, devcrypto_dsa)
291 # endif
292 # ifndef OPENSSL_NO_DH
293 - || !ENGINE_set_DH(e, devcrypto_dh)
294 + && ENGINE_set_DH(e, devcrypto_dh)
295 # endif
296 # ifndef OPENSSL_NO_EC
297 - || !ENGINE_set_EC(e, devcrypto_ec)
298 + && ENGINE_set_EC(e, devcrypto_ec)
299 # endif
300 #endif
301 - || !ENGINE_set_ciphers(e, devcrypto_ciphers)
302 -#ifdef IMPLEMENT_DIGEST
303 - || !ENGINE_set_digests(e, devcrypto_digests)
304 -#endif
305 - ) {
306 + );
307 +}
308 +
309 +#ifdef OPENSSL_NO_DYNAMIC_ENGINE
310 +/*
311 + * In case this engine is built into libcrypto, then it doesn't offer any
312 + * ability to be dynamically loadable.
313 + */
314 +void engine_load_devcrypto_int(void)
315 +{
316 + ENGINE *e = NULL;
317 +
318 + if (!open_devcrypto())
319 + return;
320 +
321 + if ((e = ENGINE_new()) == NULL
322 + || !bind_devcrypto(e)) {
323 + close_devcrypto();
324 ENGINE_free(e);
325 return;
326 }
327 @@ -1262,3 +1292,22 @@ void engine_load_devcrypto_int()
328 ENGINE_free(e); /* Loose our local reference */
329 ERR_clear_error();
330 }
331 +
332 +#else
333 +
334 +static int bind_helper(ENGINE *e, const char *id)
335 +{
336 + if ((id && (strcmp(id, engine_devcrypto_id) != 0))
337 + || !open_devcrypto())
338 + return 0;
339 + if (!bind_devcrypto(e)) {
340 + close_devcrypto();
341 + return 0;
342 + }
343 + return 1;
344 +}
345 +
346 +IMPLEMENT_DYNAMIC_CHECK_FN()
347 +IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
348 +
349 +#endif