1 /*
2 * Copyright 2015-2022 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 /*
14 * SHA-1 low level APIs are deprecated for public use, but still ok for
15 * internal use. Note, that due to symbols not being exported, only the
16 * #defines and strucures can be accessed, in this case SHA_CBLOCK and
17 * sizeof(SHA_CTX).
18 */
19 #include "internal/deprecated.h"
20
21 #include <openssl/opensslconf.h>
22 #if defined(_WIN32)
23 # include <windows.h>
24 #endif
25
26 #include <stdio.h>
27 #include <string.h>
28
29 #include <openssl/engine.h>
30 #include <openssl/sha.h>
31 #include <openssl/aes.h>
32 #include <openssl/rsa.h>
33 #include <openssl/evp.h>
34 #include <openssl/async.h>
35 #include <openssl/bn.h>
36 #include <openssl/crypto.h>
37 #include <openssl/ssl.h>
38 #include <openssl/modes.h>
39
40 #if defined(OPENSSL_SYS_UNIX) && defined(OPENSSL_THREADS)
41 # undef ASYNC_POSIX
42 # define ASYNC_POSIX
43 # include <unistd.h>
44 #elif defined(_WIN32)
45 # undef ASYNC_WIN
46 # define ASYNC_WIN
47 #endif
48
49 #include "e_dasync_err.c"
50
51 /* Engine Id and Name */
52 static const char *engine_dasync_id = "dasync";
53 static const char *engine_dasync_name = "Dummy Async engine support";
54
55
56 /* Engine Lifetime functions */
57 static int dasync_destroy(ENGINE *e);
58 static int dasync_init(ENGINE *e);
59 static int dasync_finish(ENGINE *e);
60 void engine_load_dasync_int(void);
61
62
63 /* Set up digests. Just SHA1 for now */
64 static int dasync_digests(ENGINE *e, const EVP_MD **digest,
65 const int **nids, int nid);
66
67 static void dummy_pause_job(void);
68
69 /* SHA1 */
70 static int dasync_sha1_init(EVP_MD_CTX *ctx);
71 static int dasync_sha1_update(EVP_MD_CTX *ctx, const void *data,
72 size_t count);
73 static int dasync_sha1_final(EVP_MD_CTX *ctx, unsigned char *md);
74
75 /*
76 * Holds the EVP_MD object for sha1 in this engine. Set up once only during
77 * engine bind and can then be reused many times.
78 */
79 static EVP_MD *_hidden_sha1_md = NULL;
dasync_sha1(void)80 static const EVP_MD *dasync_sha1(void)
81 {
82 return _hidden_sha1_md;
83 }
destroy_digests(void)84 static void destroy_digests(void)
85 {
86 EVP_MD_meth_free(_hidden_sha1_md);
87 _hidden_sha1_md = NULL;
88 }
89
dasync_digest_nids(const int ** nids)90 static int dasync_digest_nids(const int **nids)
91 {
92 static int digest_nids[2] = { 0, 0 };
93 static int pos = 0;
94 static int init = 0;
95
96 if (!init) {
97 const EVP_MD *md;
98 if ((md = dasync_sha1()) != NULL)
99 digest_nids[pos++] = EVP_MD_get_type(md);
100 digest_nids[pos] = 0;
101 init = 1;
102 }
103 *nids = digest_nids;
104 return pos;
105 }
106
107 /* RSA */
108 static int dasync_pkey(ENGINE *e, EVP_PKEY_METHOD **pmeth,
109 const int **pnids, int nid);
110
111 static int dasync_rsa_init(EVP_PKEY_CTX *ctx);
112 static void dasync_rsa_cleanup(EVP_PKEY_CTX *ctx);
113 static int dasync_rsa_paramgen_init(EVP_PKEY_CTX *ctx);
114 static int dasync_rsa_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey);
115 static int dasync_rsa_keygen_init(EVP_PKEY_CTX *ctx);
116 static int dasync_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey);
117 static int dasync_rsa_encrypt_init(EVP_PKEY_CTX *ctx);
118 static int dasync_rsa_encrypt(EVP_PKEY_CTX *ctx, unsigned char *out,
119 size_t *outlen, const unsigned char *in,
120 size_t inlen);
121 static int dasync_rsa_decrypt_init(EVP_PKEY_CTX *ctx);
122 static int dasync_rsa_decrypt(EVP_PKEY_CTX *ctx, unsigned char *out,
123 size_t *outlen, const unsigned char *in,
124 size_t inlen);
125 static int dasync_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2);
126 static int dasync_rsa_ctrl_str(EVP_PKEY_CTX *ctx, const char *type,
127 const char *value);
128
129 static EVP_PKEY_METHOD *dasync_rsa;
130 static const EVP_PKEY_METHOD *dasync_rsa_orig;
131
132 /* AES */
133
134 static int dasync_aes128_cbc_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
135 void *ptr);
136 static int dasync_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
137 const unsigned char *iv, int enc);
138 static int dasync_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
139 const unsigned char *in, size_t inl);
140 static int dasync_aes128_cbc_cleanup(EVP_CIPHER_CTX *ctx);
141
142 static int dasync_aes128_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX *ctx, int type,
143 int arg, void *ptr);
144 static int dasync_aes128_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX *ctx,
145 const unsigned char *key,
146 const unsigned char *iv,
147 int enc);
148 static int dasync_aes128_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx,
149 unsigned char *out,
150 const unsigned char *in,
151 size_t inl);
152 static int dasync_aes128_cbc_hmac_sha1_cleanup(EVP_CIPHER_CTX *ctx);
153
154 struct dasync_pipeline_ctx {
155 void *inner_cipher_data;
156 unsigned int numpipes;
157 unsigned char **inbufs;
158 unsigned char **outbufs;
159 size_t *lens;
160 unsigned char tlsaad[SSL_MAX_PIPELINES][EVP_AEAD_TLS1_AAD_LEN];
161 unsigned int aadctr;
162 };
163
164 /*
165 * Holds the EVP_CIPHER object for aes_128_cbc in this engine. Set up once only
166 * during engine bind and can then be reused many times.
167 */
168 static EVP_CIPHER *_hidden_aes_128_cbc = NULL;
dasync_aes_128_cbc(void)169 static const EVP_CIPHER *dasync_aes_128_cbc(void)
170 {
171 return _hidden_aes_128_cbc;
172 }
173
174 /*
175 * Holds the EVP_CIPHER object for aes_128_cbc_hmac_sha1 in this engine. Set up
176 * once only during engine bind and can then be reused many times.
177 *
178 * This 'stitched' cipher depends on the EVP_aes_128_cbc_hmac_sha1() cipher,
179 * which is implemented only if the AES-NI instruction set extension is available
180 * (see OPENSSL_IA32CAP(3)). If that's not the case, then this cipher will not
181 * be available either.
182 *
183 * Note: Since it is a legacy mac-then-encrypt cipher, modern TLS peers (which
184 * negotiate the encrypt-then-mac extension) won't negotiate it anyway.
185 */
186 static EVP_CIPHER *_hidden_aes_128_cbc_hmac_sha1 = NULL;
dasync_aes_128_cbc_hmac_sha1(void)187 static const EVP_CIPHER *dasync_aes_128_cbc_hmac_sha1(void)
188 {
189 return _hidden_aes_128_cbc_hmac_sha1;
190 }
191
destroy_ciphers(void)192 static void destroy_ciphers(void)
193 {
194 EVP_CIPHER_meth_free(_hidden_aes_128_cbc);
195 EVP_CIPHER_meth_free(_hidden_aes_128_cbc_hmac_sha1);
196 _hidden_aes_128_cbc = NULL;
197 _hidden_aes_128_cbc_hmac_sha1 = NULL;
198 }
199
200 static int dasync_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
201 const int **nids, int nid);
202
203 static int dasync_cipher_nids[] = {
204 NID_aes_128_cbc,
205 NID_aes_128_cbc_hmac_sha1,
206 0
207 };
208
bind_dasync(ENGINE * e)209 static int bind_dasync(ENGINE *e)
210 {
211 /* Setup RSA */
212 if ((dasync_rsa_orig = EVP_PKEY_meth_find(EVP_PKEY_RSA)) == NULL
213 || (dasync_rsa = EVP_PKEY_meth_new(EVP_PKEY_RSA,
214 EVP_PKEY_FLAG_AUTOARGLEN)) == NULL)
215 return 0;
216 EVP_PKEY_meth_set_init(dasync_rsa, dasync_rsa_init);
217 EVP_PKEY_meth_set_cleanup(dasync_rsa, dasync_rsa_cleanup);
218 EVP_PKEY_meth_set_paramgen(dasync_rsa, dasync_rsa_paramgen_init,
219 dasync_rsa_paramgen);
220 EVP_PKEY_meth_set_keygen(dasync_rsa, dasync_rsa_keygen_init,
221 dasync_rsa_keygen);
222 EVP_PKEY_meth_set_encrypt(dasync_rsa, dasync_rsa_encrypt_init,
223 dasync_rsa_encrypt);
224 EVP_PKEY_meth_set_decrypt(dasync_rsa, dasync_rsa_decrypt_init,
225 dasync_rsa_decrypt);
226 EVP_PKEY_meth_set_ctrl(dasync_rsa, dasync_rsa_ctrl,
227 dasync_rsa_ctrl_str);
228
229 /* Ensure the dasync error handling is set up */
230 ERR_load_DASYNC_strings();
231
232 if (!ENGINE_set_id(e, engine_dasync_id)
233 || !ENGINE_set_name(e, engine_dasync_name)
234 || !ENGINE_set_pkey_meths(e, dasync_pkey)
235 || !ENGINE_set_digests(e, dasync_digests)
236 || !ENGINE_set_ciphers(e, dasync_ciphers)
237 || !ENGINE_set_destroy_function(e, dasync_destroy)
238 || !ENGINE_set_init_function(e, dasync_init)
239 || !ENGINE_set_finish_function(e, dasync_finish)) {
240 DASYNCerr(DASYNC_F_BIND_DASYNC, DASYNC_R_INIT_FAILED);
241 return 0;
242 }
243
244 /*
245 * Set up the EVP_CIPHER and EVP_MD objects for the ciphers/digests
246 * supplied by this engine
247 */
248 _hidden_sha1_md = EVP_MD_meth_new(NID_sha1, NID_sha1WithRSAEncryption);
249 if (_hidden_sha1_md == NULL
250 || !EVP_MD_meth_set_result_size(_hidden_sha1_md, SHA_DIGEST_LENGTH)
251 || !EVP_MD_meth_set_input_blocksize(_hidden_sha1_md, SHA_CBLOCK)
252 || !EVP_MD_meth_set_app_datasize(_hidden_sha1_md,
253 sizeof(EVP_MD *) + sizeof(SHA_CTX))
254 || !EVP_MD_meth_set_flags(_hidden_sha1_md, EVP_MD_FLAG_DIGALGID_ABSENT)
255 || !EVP_MD_meth_set_init(_hidden_sha1_md, dasync_sha1_init)
256 || !EVP_MD_meth_set_update(_hidden_sha1_md, dasync_sha1_update)
257 || !EVP_MD_meth_set_final(_hidden_sha1_md, dasync_sha1_final)) {
258 EVP_MD_meth_free(_hidden_sha1_md);
259 _hidden_sha1_md = NULL;
260 }
261
262 _hidden_aes_128_cbc = EVP_CIPHER_meth_new(NID_aes_128_cbc,
263 16 /* block size */,
264 16 /* key len */);
265 if (_hidden_aes_128_cbc == NULL
266 || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_128_cbc,16)
267 || !EVP_CIPHER_meth_set_flags(_hidden_aes_128_cbc,
268 EVP_CIPH_FLAG_DEFAULT_ASN1
269 | EVP_CIPH_CBC_MODE
270 | EVP_CIPH_FLAG_PIPELINE
271 | EVP_CIPH_CUSTOM_COPY)
272 || !EVP_CIPHER_meth_set_init(_hidden_aes_128_cbc,
273 dasync_aes128_init_key)
274 || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_cbc,
275 dasync_aes128_cbc_cipher)
276 || !EVP_CIPHER_meth_set_cleanup(_hidden_aes_128_cbc,
277 dasync_aes128_cbc_cleanup)
278 || !EVP_CIPHER_meth_set_ctrl(_hidden_aes_128_cbc,
279 dasync_aes128_cbc_ctrl)
280 || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_cbc,
281 sizeof(struct dasync_pipeline_ctx))) {
282 EVP_CIPHER_meth_free(_hidden_aes_128_cbc);
283 _hidden_aes_128_cbc = NULL;
284 }
285
286 _hidden_aes_128_cbc_hmac_sha1 = EVP_CIPHER_meth_new(
287 NID_aes_128_cbc_hmac_sha1,
288 16 /* block size */,
289 16 /* key len */);
290 if (_hidden_aes_128_cbc_hmac_sha1 == NULL
291 || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_128_cbc_hmac_sha1,16)
292 || !EVP_CIPHER_meth_set_flags(_hidden_aes_128_cbc_hmac_sha1,
293 EVP_CIPH_CBC_MODE
294 | EVP_CIPH_FLAG_DEFAULT_ASN1
295 | EVP_CIPH_FLAG_AEAD_CIPHER
296 | EVP_CIPH_FLAG_PIPELINE
297 | EVP_CIPH_CUSTOM_COPY)
298 || !EVP_CIPHER_meth_set_init(_hidden_aes_128_cbc_hmac_sha1,
299 dasync_aes128_cbc_hmac_sha1_init_key)
300 || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_cbc_hmac_sha1,
301 dasync_aes128_cbc_hmac_sha1_cipher)
302 || !EVP_CIPHER_meth_set_cleanup(_hidden_aes_128_cbc_hmac_sha1,
303 dasync_aes128_cbc_hmac_sha1_cleanup)
304 || !EVP_CIPHER_meth_set_ctrl(_hidden_aes_128_cbc_hmac_sha1,
305 dasync_aes128_cbc_hmac_sha1_ctrl)
306 || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_cbc_hmac_sha1,
307 sizeof(struct dasync_pipeline_ctx))) {
308 EVP_CIPHER_meth_free(_hidden_aes_128_cbc_hmac_sha1);
309 _hidden_aes_128_cbc_hmac_sha1 = NULL;
310 }
311
312 return 1;
313 }
314
destroy_pkey(void)315 static void destroy_pkey(void)
316 {
317 /*
318 * We don't actually need to free the dasync_rsa method since this is
319 * automatically freed for us by libcrypto.
320 */
321 dasync_rsa_orig = NULL;
322 dasync_rsa = NULL;
323 }
324
325 # ifndef OPENSSL_NO_DYNAMIC_ENGINE
bind_helper(ENGINE * e,const char * id)326 static int bind_helper(ENGINE *e, const char *id)
327 {
328 if (id && (strcmp(id, engine_dasync_id) != 0))
329 return 0;
330 if (!bind_dasync(e))
331 return 0;
332 return 1;
333 }
334
335 IMPLEMENT_DYNAMIC_CHECK_FN()
IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)336 IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
337 # endif
338
339 static ENGINE *engine_dasync(void)
340 {
341 ENGINE *ret = ENGINE_new();
342 if (!ret)
343 return NULL;
344 if (!bind_dasync(ret)) {
345 ENGINE_free(ret);
346 return NULL;
347 }
348 return ret;
349 }
350
engine_load_dasync_int(void)351 void engine_load_dasync_int(void)
352 {
353 ENGINE *toadd = engine_dasync();
354 if (!toadd)
355 return;
356 ERR_set_mark();
357 ENGINE_add(toadd);
358 /*
359 * If the "add" worked, it gets a structural reference. So either way, we
360 * release our just-created reference.
361 */
362 ENGINE_free(toadd);
363 /*
364 * If the "add" didn't work, it was probably a conflict because it was
365 * already added (eg. someone calling ENGINE_load_blah then calling
366 * ENGINE_load_builtin_engines() perhaps).
367 */
368 ERR_pop_to_mark();
369 }
370
dasync_init(ENGINE * e)371 static int dasync_init(ENGINE *e)
372 {
373 return 1;
374 }
375
376
dasync_finish(ENGINE * e)377 static int dasync_finish(ENGINE *e)
378 {
379 return 1;
380 }
381
382
dasync_destroy(ENGINE * e)383 static int dasync_destroy(ENGINE *e)
384 {
385 destroy_digests();
386 destroy_ciphers();
387 destroy_pkey();
388 ERR_unload_DASYNC_strings();
389 return 1;
390 }
391
dasync_pkey(ENGINE * e,EVP_PKEY_METHOD ** pmeth,const int ** pnids,int nid)392 static int dasync_pkey(ENGINE *e, EVP_PKEY_METHOD **pmeth,
393 const int **pnids, int nid)
394 {
395 static const int rnid = EVP_PKEY_RSA;
396
397 if (pmeth == NULL) {
398 *pnids = &rnid;
399 return 1;
400 }
401
402 if (nid == EVP_PKEY_RSA) {
403 *pmeth = dasync_rsa;
404 return 1;
405 }
406
407 *pmeth = NULL;
408 return 0;
409 }
410
dasync_digests(ENGINE * e,const EVP_MD ** digest,const int ** nids,int nid)411 static int dasync_digests(ENGINE *e, const EVP_MD **digest,
412 const int **nids, int nid)
413 {
414 int ok = 1;
415 if (!digest) {
416 /* We are returning a list of supported nids */
417 return dasync_digest_nids(nids);
418 }
419 /* We are being asked for a specific digest */
420 switch (nid) {
421 case NID_sha1:
422 *digest = dasync_sha1();
423 break;
424 default:
425 ok = 0;
426 *digest = NULL;
427 break;
428 }
429 return ok;
430 }
431
dasync_ciphers(ENGINE * e,const EVP_CIPHER ** cipher,const int ** nids,int nid)432 static int dasync_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
433 const int **nids, int nid)
434 {
435 int ok = 1;
436 if (cipher == NULL) {
437 /* We are returning a list of supported nids */
438 *nids = dasync_cipher_nids;
439 return (sizeof(dasync_cipher_nids) -
440 1) / sizeof(dasync_cipher_nids[0]);
441 }
442 /* We are being asked for a specific cipher */
443 switch (nid) {
444 case NID_aes_128_cbc:
445 *cipher = dasync_aes_128_cbc();
446 break;
447 case NID_aes_128_cbc_hmac_sha1:
448 *cipher = dasync_aes_128_cbc_hmac_sha1();
449 break;
450 default:
451 ok = 0;
452 *cipher = NULL;
453 break;
454 }
455 return ok;
456 }
457
wait_cleanup(ASYNC_WAIT_CTX * ctx,const void * key,OSSL_ASYNC_FD readfd,void * pvwritefd)458 static void wait_cleanup(ASYNC_WAIT_CTX *ctx, const void *key,
459 OSSL_ASYNC_FD readfd, void *pvwritefd)
460 {
461 OSSL_ASYNC_FD *pwritefd = (OSSL_ASYNC_FD *)pvwritefd;
462 #if defined(ASYNC_WIN)
463 CloseHandle(readfd);
464 CloseHandle(*pwritefd);
465 #elif defined(ASYNC_POSIX)
466 close(readfd);
467 close(*pwritefd);
468 #endif
469 OPENSSL_free(pwritefd);
470 }
471
472 #define DUMMY_CHAR 'X'
473
dummy_pause_job(void)474 static void dummy_pause_job(void) {
475 ASYNC_JOB *job;
476 ASYNC_WAIT_CTX *waitctx;
477 ASYNC_callback_fn callback;
478 void * callback_arg;
479 OSSL_ASYNC_FD pipefds[2] = {0, 0};
480 OSSL_ASYNC_FD *writefd;
481 #if defined(ASYNC_WIN)
482 DWORD numwritten, numread;
483 char buf = DUMMY_CHAR;
484 #elif defined(ASYNC_POSIX)
485 char buf = DUMMY_CHAR;
486 #endif
487
488 if ((job = ASYNC_get_current_job()) == NULL)
489 return;
490
491 waitctx = ASYNC_get_wait_ctx(job);
492
493 if (ASYNC_WAIT_CTX_get_callback(waitctx, &callback, &callback_arg) && callback != NULL) {
494 /*
495 * In the Dummy async engine we are cheating. We call the callback that the job
496 * is complete before the call to ASYNC_pause_job(). A real
497 * async engine would only call the callback when the job was actually complete
498 */
499 (*callback)(callback_arg);
500 ASYNC_pause_job();
501 return;
502 }
503
504
505 if (ASYNC_WAIT_CTX_get_fd(waitctx, engine_dasync_id, &pipefds[0],
506 (void **)&writefd)) {
507 pipefds[1] = *writefd;
508 } else {
509 writefd = OPENSSL_malloc(sizeof(*writefd));
510 if (writefd == NULL)
511 return;
512 #if defined(ASYNC_WIN)
513 if (CreatePipe(&pipefds[0], &pipefds[1], NULL, 256) == 0) {
514 OPENSSL_free(writefd);
515 return;
516 }
517 #elif defined(ASYNC_POSIX)
518 if (pipe(pipefds) != 0) {
519 OPENSSL_free(writefd);
520 return;
521 }
522 #endif
523 *writefd = pipefds[1];
524
525 if (!ASYNC_WAIT_CTX_set_wait_fd(waitctx, engine_dasync_id, pipefds[0],
526 writefd, wait_cleanup)) {
527 wait_cleanup(waitctx, engine_dasync_id, pipefds[0], writefd);
528 return;
529 }
530 }
531 /*
532 * In the Dummy async engine we are cheating. We signal that the job
533 * is complete by waking it before the call to ASYNC_pause_job(). A real
534 * async engine would only wake when the job was actually complete
535 */
536 #if defined(ASYNC_WIN)
537 WriteFile(pipefds[1], &buf, 1, &numwritten, NULL);
538 #elif defined(ASYNC_POSIX)
539 if (write(pipefds[1], &buf, 1) < 0)
540 return;
541 #endif
542
543 /* Ignore errors - we carry on anyway */
544 ASYNC_pause_job();
545
546 /* Clear the wake signal */
547 #if defined(ASYNC_WIN)
548 ReadFile(pipefds[0], &buf, 1, &numread, NULL);
549 #elif defined(ASYNC_POSIX)
550 if (read(pipefds[0], &buf, 1) < 0)
551 return;
552 #endif
553 }
554
555 /*
556 * SHA1 implementation. At the moment we just defer to the standard
557 * implementation
558 */
dasync_sha1_init(EVP_MD_CTX * ctx)559 static int dasync_sha1_init(EVP_MD_CTX *ctx)
560 {
561 dummy_pause_job();
562
563 return EVP_MD_meth_get_init(EVP_sha1())(ctx);
564 }
565
dasync_sha1_update(EVP_MD_CTX * ctx,const void * data,size_t count)566 static int dasync_sha1_update(EVP_MD_CTX *ctx, const void *data,
567 size_t count)
568 {
569 dummy_pause_job();
570
571 return EVP_MD_meth_get_update(EVP_sha1())(ctx, data, count);
572 }
573
dasync_sha1_final(EVP_MD_CTX * ctx,unsigned char * md)574 static int dasync_sha1_final(EVP_MD_CTX *ctx, unsigned char *md)
575 {
576 dummy_pause_job();
577
578 return EVP_MD_meth_get_final(EVP_sha1())(ctx, md);
579 }
580
581 /* Cipher helper functions */
582
dasync_cipher_ctrl_helper(EVP_CIPHER_CTX * ctx,int type,int arg,void * ptr,int aeadcapable,const EVP_CIPHER * ciph)583 static int dasync_cipher_ctrl_helper(EVP_CIPHER_CTX *ctx, int type, int arg,
584 void *ptr, int aeadcapable,
585 const EVP_CIPHER *ciph)
586 {
587 int ret;
588 struct dasync_pipeline_ctx *pipe_ctx =
589 (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
590
591 if (pipe_ctx == NULL)
592 return 0;
593
594 switch (type) {
595 case EVP_CTRL_COPY:
596 {
597 size_t sz = EVP_CIPHER_impl_ctx_size(ciph);
598 void *inner_cipher_data = OPENSSL_malloc(sz);
599
600 if (inner_cipher_data == NULL)
601 return -1;
602 memcpy(inner_cipher_data, pipe_ctx->inner_cipher_data, sz);
603 pipe_ctx->inner_cipher_data = inner_cipher_data;
604 }
605 break;
606
607 case EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS:
608 pipe_ctx->numpipes = arg;
609 pipe_ctx->outbufs = (unsigned char **)ptr;
610 break;
611
612 case EVP_CTRL_SET_PIPELINE_INPUT_BUFS:
613 pipe_ctx->numpipes = arg;
614 pipe_ctx->inbufs = (unsigned char **)ptr;
615 break;
616
617 case EVP_CTRL_SET_PIPELINE_INPUT_LENS:
618 pipe_ctx->numpipes = arg;
619 pipe_ctx->lens = (size_t *)ptr;
620 break;
621
622 case EVP_CTRL_AEAD_SET_MAC_KEY:
623 if (!aeadcapable)
624 return -1;
625 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
626 ret = EVP_CIPHER_meth_get_ctrl(EVP_aes_128_cbc_hmac_sha1())
627 (ctx, type, arg, ptr);
628 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
629 return ret;
630
631 case EVP_CTRL_AEAD_TLS1_AAD:
632 {
633 unsigned char *p = ptr;
634 unsigned int len;
635
636 if (!aeadcapable || arg != EVP_AEAD_TLS1_AAD_LEN)
637 return -1;
638
639 if (pipe_ctx->aadctr >= SSL_MAX_PIPELINES)
640 return -1;
641
642 memcpy(pipe_ctx->tlsaad[pipe_ctx->aadctr], ptr,
643 EVP_AEAD_TLS1_AAD_LEN);
644 pipe_ctx->aadctr++;
645
646 len = p[arg - 2] << 8 | p[arg - 1];
647
648 if (EVP_CIPHER_CTX_is_encrypting(ctx)) {
649 if ((p[arg - 4] << 8 | p[arg - 3]) >= TLS1_1_VERSION) {
650 if (len < AES_BLOCK_SIZE)
651 return 0;
652 len -= AES_BLOCK_SIZE;
653 }
654
655 return ((len + SHA_DIGEST_LENGTH + AES_BLOCK_SIZE)
656 & -AES_BLOCK_SIZE) - len;
657 } else {
658 return SHA_DIGEST_LENGTH;
659 }
660 }
661
662 default:
663 return 0;
664 }
665
666 return 1;
667 }
668
dasync_cipher_init_key_helper(EVP_CIPHER_CTX * ctx,const unsigned char * key,const unsigned char * iv,int enc,const EVP_CIPHER * cipher)669 static int dasync_cipher_init_key_helper(EVP_CIPHER_CTX *ctx,
670 const unsigned char *key,
671 const unsigned char *iv, int enc,
672 const EVP_CIPHER *cipher)
673 {
674 int ret;
675 struct dasync_pipeline_ctx *pipe_ctx =
676 (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
677
678 if (pipe_ctx->inner_cipher_data == NULL
679 && EVP_CIPHER_impl_ctx_size(cipher) != 0) {
680 pipe_ctx->inner_cipher_data = OPENSSL_zalloc(
681 EVP_CIPHER_impl_ctx_size(cipher));
682 if (pipe_ctx->inner_cipher_data == NULL) {
683 DASYNCerr(DASYNC_F_DASYNC_CIPHER_INIT_KEY_HELPER,
684 ERR_R_MALLOC_FAILURE);
685 return 0;
686 }
687 }
688
689 pipe_ctx->numpipes = 0;
690 pipe_ctx->aadctr = 0;
691
692 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
693 ret = EVP_CIPHER_meth_get_init(cipher)(ctx, key, iv, enc);
694 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
695
696 return ret;
697 }
698
dasync_cipher_helper(EVP_CIPHER_CTX * ctx,unsigned char * out,const unsigned char * in,size_t inl,const EVP_CIPHER * cipher)699 static int dasync_cipher_helper(EVP_CIPHER_CTX *ctx, unsigned char *out,
700 const unsigned char *in, size_t inl,
701 const EVP_CIPHER *cipher)
702 {
703 int ret = 1;
704 unsigned int i, pipes;
705 struct dasync_pipeline_ctx *pipe_ctx =
706 (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
707
708 pipes = pipe_ctx->numpipes;
709 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
710 if (pipes == 0) {
711 if (pipe_ctx->aadctr != 0) {
712 if (pipe_ctx->aadctr != 1)
713 return -1;
714 EVP_CIPHER_meth_get_ctrl(cipher)
715 (ctx, EVP_CTRL_AEAD_TLS1_AAD,
716 EVP_AEAD_TLS1_AAD_LEN,
717 pipe_ctx->tlsaad[0]);
718 }
719 ret = EVP_CIPHER_meth_get_do_cipher(cipher)
720 (ctx, out, in, inl);
721 } else {
722 if (pipe_ctx->aadctr > 0 && pipe_ctx->aadctr != pipes)
723 return -1;
724 for (i = 0; i < pipes; i++) {
725 if (pipe_ctx->aadctr > 0) {
726 EVP_CIPHER_meth_get_ctrl(cipher)
727 (ctx, EVP_CTRL_AEAD_TLS1_AAD,
728 EVP_AEAD_TLS1_AAD_LEN,
729 pipe_ctx->tlsaad[i]);
730 }
731 ret = ret && EVP_CIPHER_meth_get_do_cipher(cipher)
732 (ctx, pipe_ctx->outbufs[i], pipe_ctx->inbufs[i],
733 pipe_ctx->lens[i]);
734 }
735 pipe_ctx->numpipes = 0;
736 }
737 pipe_ctx->aadctr = 0;
738 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
739 return ret;
740 }
741
dasync_cipher_cleanup_helper(EVP_CIPHER_CTX * ctx,const EVP_CIPHER * cipher)742 static int dasync_cipher_cleanup_helper(EVP_CIPHER_CTX *ctx,
743 const EVP_CIPHER *cipher)
744 {
745 struct dasync_pipeline_ctx *pipe_ctx =
746 (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
747
748 OPENSSL_clear_free(pipe_ctx->inner_cipher_data,
749 EVP_CIPHER_impl_ctx_size(cipher));
750
751 return 1;
752 }
753
754 /*
755 * AES128 CBC Implementation
756 */
757
dasync_aes128_cbc_ctrl(EVP_CIPHER_CTX * ctx,int type,int arg,void * ptr)758 static int dasync_aes128_cbc_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
759 void *ptr)
760 {
761 return dasync_cipher_ctrl_helper(ctx, type, arg, ptr, 0, EVP_aes_128_cbc());
762 }
763
dasync_aes128_init_key(EVP_CIPHER_CTX * ctx,const unsigned char * key,const unsigned char * iv,int enc)764 static int dasync_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
765 const unsigned char *iv, int enc)
766 {
767 return dasync_cipher_init_key_helper(ctx, key, iv, enc, EVP_aes_128_cbc());
768 }
769
dasync_aes128_cbc_cipher(EVP_CIPHER_CTX * ctx,unsigned char * out,const unsigned char * in,size_t inl)770 static int dasync_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
771 const unsigned char *in, size_t inl)
772 {
773 return dasync_cipher_helper(ctx, out, in, inl, EVP_aes_128_cbc());
774 }
775
dasync_aes128_cbc_cleanup(EVP_CIPHER_CTX * ctx)776 static int dasync_aes128_cbc_cleanup(EVP_CIPHER_CTX *ctx)
777 {
778 return dasync_cipher_cleanup_helper(ctx, EVP_aes_128_cbc());
779 }
780
781
782 /*
783 * AES128 CBC HMAC SHA1 Implementation
784 */
785
dasync_aes128_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX * ctx,int type,int arg,void * ptr)786 static int dasync_aes128_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX *ctx, int type,
787 int arg, void *ptr)
788 {
789 return dasync_cipher_ctrl_helper(ctx, type, arg, ptr, 1, EVP_aes_128_cbc_hmac_sha1());
790 }
791
dasync_aes128_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX * ctx,const unsigned char * key,const unsigned char * iv,int enc)792 static int dasync_aes128_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX *ctx,
793 const unsigned char *key,
794 const unsigned char *iv,
795 int enc)
796 {
797 /*
798 * We can safely assume that EVP_aes_128_cbc_hmac_sha1() != NULL,
799 * see comment before the definition of dasync_aes_128_cbc_hmac_sha1().
800 */
801 return dasync_cipher_init_key_helper(ctx, key, iv, enc,
802 EVP_aes_128_cbc_hmac_sha1());
803 }
804
dasync_aes128_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX * ctx,unsigned char * out,const unsigned char * in,size_t inl)805 static int dasync_aes128_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx,
806 unsigned char *out,
807 const unsigned char *in,
808 size_t inl)
809 {
810 return dasync_cipher_helper(ctx, out, in, inl, EVP_aes_128_cbc_hmac_sha1());
811 }
812
dasync_aes128_cbc_hmac_sha1_cleanup(EVP_CIPHER_CTX * ctx)813 static int dasync_aes128_cbc_hmac_sha1_cleanup(EVP_CIPHER_CTX *ctx)
814 {
815 /*
816 * We can safely assume that EVP_aes_128_cbc_hmac_sha1() != NULL,
817 * see comment before the definition of dasync_aes_128_cbc_hmac_sha1().
818 */
819 return dasync_cipher_cleanup_helper(ctx, EVP_aes_128_cbc_hmac_sha1());
820 }
821
822
823 /*
824 * RSA implementation
825 */
dasync_rsa_init(EVP_PKEY_CTX * ctx)826 static int dasync_rsa_init(EVP_PKEY_CTX *ctx)
827 {
828 static int (*pinit)(EVP_PKEY_CTX *ctx);
829
830 if (pinit == NULL)
831 EVP_PKEY_meth_get_init(dasync_rsa_orig, &pinit);
832 return pinit(ctx);
833 }
834
dasync_rsa_cleanup(EVP_PKEY_CTX * ctx)835 static void dasync_rsa_cleanup(EVP_PKEY_CTX *ctx)
836 {
837 static void (*pcleanup)(EVP_PKEY_CTX *ctx);
838
839 if (pcleanup == NULL)
840 EVP_PKEY_meth_get_cleanup(dasync_rsa_orig, &pcleanup);
841 pcleanup(ctx);
842 }
843
dasync_rsa_paramgen_init(EVP_PKEY_CTX * ctx)844 static int dasync_rsa_paramgen_init(EVP_PKEY_CTX *ctx)
845 {
846 static int (*pparamgen_init)(EVP_PKEY_CTX *ctx);
847
848 if (pparamgen_init == NULL)
849 EVP_PKEY_meth_get_paramgen(dasync_rsa_orig, &pparamgen_init, NULL);
850 return pparamgen_init != NULL ? pparamgen_init(ctx) : 1;
851 }
852
dasync_rsa_paramgen(EVP_PKEY_CTX * ctx,EVP_PKEY * pkey)853 static int dasync_rsa_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
854 {
855 static int (*pparamgen)(EVP_PKEY_CTX *c, EVP_PKEY *pkey);
856
857 if (pparamgen == NULL)
858 EVP_PKEY_meth_get_paramgen(dasync_rsa_orig, NULL, &pparamgen);
859 return pparamgen != NULL ? pparamgen(ctx, pkey) : 1;
860 }
861
dasync_rsa_keygen_init(EVP_PKEY_CTX * ctx)862 static int dasync_rsa_keygen_init(EVP_PKEY_CTX *ctx)
863 {
864 static int (*pkeygen_init)(EVP_PKEY_CTX *ctx);
865
866 if (pkeygen_init == NULL)
867 EVP_PKEY_meth_get_keygen(dasync_rsa_orig, &pkeygen_init, NULL);
868 return pkeygen_init != NULL ? pkeygen_init(ctx) : 1;
869 }
870
dasync_rsa_keygen(EVP_PKEY_CTX * ctx,EVP_PKEY * pkey)871 static int dasync_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
872 {
873 static int (*pkeygen)(EVP_PKEY_CTX *c, EVP_PKEY *pkey);
874
875 if (pkeygen == NULL)
876 EVP_PKEY_meth_get_keygen(dasync_rsa_orig, NULL, &pkeygen);
877 return pkeygen(ctx, pkey);
878 }
879
dasync_rsa_encrypt_init(EVP_PKEY_CTX * ctx)880 static int dasync_rsa_encrypt_init(EVP_PKEY_CTX *ctx)
881 {
882 static int (*pencrypt_init)(EVP_PKEY_CTX *ctx);
883
884 if (pencrypt_init == NULL)
885 EVP_PKEY_meth_get_encrypt(dasync_rsa_orig, &pencrypt_init, NULL);
886 return pencrypt_init != NULL ? pencrypt_init(ctx) : 1;
887 }
888
dasync_rsa_encrypt(EVP_PKEY_CTX * ctx,unsigned char * out,size_t * outlen,const unsigned char * in,size_t inlen)889 static int dasync_rsa_encrypt(EVP_PKEY_CTX *ctx, unsigned char *out,
890 size_t *outlen, const unsigned char *in,
891 size_t inlen)
892 {
893 static int (*pencryptfn)(EVP_PKEY_CTX *ctx, unsigned char *out,
894 size_t *outlen, const unsigned char *in,
895 size_t inlen);
896
897 if (pencryptfn == NULL)
898 EVP_PKEY_meth_get_encrypt(dasync_rsa_orig, NULL, &pencryptfn);
899 return pencryptfn(ctx, out, outlen, in, inlen);
900 }
901
dasync_rsa_decrypt_init(EVP_PKEY_CTX * ctx)902 static int dasync_rsa_decrypt_init(EVP_PKEY_CTX *ctx)
903 {
904 static int (*pdecrypt_init)(EVP_PKEY_CTX *ctx);
905
906 if (pdecrypt_init == NULL)
907 EVP_PKEY_meth_get_decrypt(dasync_rsa_orig, &pdecrypt_init, NULL);
908 return pdecrypt_init != NULL ? pdecrypt_init(ctx) : 1;
909 }
910
dasync_rsa_decrypt(EVP_PKEY_CTX * ctx,unsigned char * out,size_t * outlen,const unsigned char * in,size_t inlen)911 static int dasync_rsa_decrypt(EVP_PKEY_CTX *ctx, unsigned char *out,
912 size_t *outlen, const unsigned char *in,
913 size_t inlen)
914 {
915 static int (*pdecrypt)(EVP_PKEY_CTX *ctx, unsigned char *out,
916 size_t *outlen, const unsigned char *in,
917 size_t inlen);
918
919 if (pdecrypt == NULL)
920 EVP_PKEY_meth_get_encrypt(dasync_rsa_orig, NULL, &pdecrypt);
921 return pdecrypt(ctx, out, outlen, in, inlen);
922 }
923
dasync_rsa_ctrl(EVP_PKEY_CTX * ctx,int type,int p1,void * p2)924 static int dasync_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
925 {
926 static int (*pctrl)(EVP_PKEY_CTX *ctx, int type, int p1, void *p2);
927
928 if (pctrl == NULL)
929 EVP_PKEY_meth_get_ctrl(dasync_rsa_orig, &pctrl, NULL);
930 return pctrl(ctx, type, p1, p2);
931 }
932
dasync_rsa_ctrl_str(EVP_PKEY_CTX * ctx,const char * type,const char * value)933 static int dasync_rsa_ctrl_str(EVP_PKEY_CTX *ctx, const char *type,
934 const char *value)
935 {
936 static int (*pctrl_str)(EVP_PKEY_CTX *ctx, const char *type,
937 const char *value);
938
939 if (pctrl_str == NULL)
940 EVP_PKEY_meth_get_ctrl(dasync_rsa_orig, NULL, &pctrl_str);
941 return pctrl_str(ctx, type, value);
942 }
943