Rename all getters to use get/get0 in name
[openssl.git] / engines / e_dasync.c
1 /*
2  * Copyright 2015-2020 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;
80 static const EVP_MD *dasync_sha1(void)
81 {
82     return _hidden_sha1_md;
83 }
84 static void destroy_digests(void)
85 {
86     EVP_MD_meth_free(_hidden_sha1_md);
87     _hidden_sha1_md = NULL;
88 }
89
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;
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;
187 static const EVP_CIPHER *dasync_aes_128_cbc_hmac_sha1(void)
188 {
189     return _hidden_aes_128_cbc_hmac_sha1;
190 }
191
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
209 static int bind_dasync(ENGINE *e)
210 {
211     /* Setup RSA */
212     ;
213     if ((dasync_rsa_orig = EVP_PKEY_meth_find(EVP_PKEY_RSA)) == NULL
214         || (dasync_rsa = EVP_PKEY_meth_new(EVP_PKEY_RSA, 0)) == 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_CIPHER_meth_set_init(_hidden_aes_128_cbc,
272                                          dasync_aes128_init_key)
273             || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_cbc,
274                                               dasync_aes128_cbc_cipher)
275             || !EVP_CIPHER_meth_set_cleanup(_hidden_aes_128_cbc,
276                                             dasync_aes128_cbc_cleanup)
277             || !EVP_CIPHER_meth_set_ctrl(_hidden_aes_128_cbc,
278                                          dasync_aes128_cbc_ctrl)
279             || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_cbc,
280                                 sizeof(struct dasync_pipeline_ctx))) {
281         EVP_CIPHER_meth_free(_hidden_aes_128_cbc);
282         _hidden_aes_128_cbc = NULL;
283     }
284
285     _hidden_aes_128_cbc_hmac_sha1 = EVP_CIPHER_meth_new(
286                                                 NID_aes_128_cbc_hmac_sha1,
287                                                 16 /* block size */,
288                                                 16 /* key len */);
289     if (_hidden_aes_128_cbc_hmac_sha1 == NULL
290             || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_128_cbc_hmac_sha1,16)
291             || !EVP_CIPHER_meth_set_flags(_hidden_aes_128_cbc_hmac_sha1,
292                                             EVP_CIPH_CBC_MODE
293                                           | EVP_CIPH_FLAG_DEFAULT_ASN1
294                                           | EVP_CIPH_FLAG_AEAD_CIPHER
295                                           | EVP_CIPH_FLAG_PIPELINE)
296             || !EVP_CIPHER_meth_set_init(_hidden_aes_128_cbc_hmac_sha1,
297                                          dasync_aes128_cbc_hmac_sha1_init_key)
298             || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_cbc_hmac_sha1,
299                                             dasync_aes128_cbc_hmac_sha1_cipher)
300             || !EVP_CIPHER_meth_set_cleanup(_hidden_aes_128_cbc_hmac_sha1,
301                                             dasync_aes128_cbc_hmac_sha1_cleanup)
302             || !EVP_CIPHER_meth_set_ctrl(_hidden_aes_128_cbc_hmac_sha1,
303                                          dasync_aes128_cbc_hmac_sha1_ctrl)
304             || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_cbc_hmac_sha1,
305                                 sizeof(struct dasync_pipeline_ctx))) {
306         EVP_CIPHER_meth_free(_hidden_aes_128_cbc_hmac_sha1);
307         _hidden_aes_128_cbc_hmac_sha1 = NULL;
308     }
309
310     return 1;
311 }
312
313 static void destroy_pkey(void)
314 {
315     EVP_PKEY_meth_free(dasync_rsa);
316     dasync_rsa_orig = NULL;
317     dasync_rsa = NULL;
318 }
319
320 # ifndef OPENSSL_NO_DYNAMIC_ENGINE
321 static int bind_helper(ENGINE *e, const char *id)
322 {
323     if (id && (strcmp(id, engine_dasync_id) != 0))
324         return 0;
325     if (!bind_dasync(e))
326         return 0;
327     return 1;
328 }
329
330 IMPLEMENT_DYNAMIC_CHECK_FN()
331     IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
332 # endif
333
334 static ENGINE *engine_dasync(void)
335 {
336     ENGINE *ret = ENGINE_new();
337     if (!ret)
338         return NULL;
339     if (!bind_dasync(ret)) {
340         ENGINE_free(ret);
341         return NULL;
342     }
343     return ret;
344 }
345
346 void engine_load_dasync_int(void)
347 {
348     ENGINE *toadd = engine_dasync();
349     if (!toadd)
350         return;
351     ERR_set_mark();
352     ENGINE_add(toadd);
353     /*
354      * If the "add" worked, it gets a structural reference. So either way, we
355      * release our just-created reference.
356      */
357     ENGINE_free(toadd);
358     /*
359      * If the "add" didn't work, it was probably a conflict because it was
360      * already added (eg. someone calling ENGINE_load_blah then calling
361      * ENGINE_load_builtin_engines() perhaps).
362      */
363     ERR_pop_to_mark();
364 }
365
366 static int dasync_init(ENGINE *e)
367 {
368     return 1;
369 }
370
371
372 static int dasync_finish(ENGINE *e)
373 {
374     return 1;
375 }
376
377
378 static int dasync_destroy(ENGINE *e)
379 {
380     destroy_digests();
381     destroy_ciphers();
382     destroy_pkey();
383     ERR_unload_DASYNC_strings();
384     return 1;
385 }
386
387 static int dasync_pkey(ENGINE *e, EVP_PKEY_METHOD **pmeth,
388                        const int **pnids, int nid)
389 {
390     static const int rnid = EVP_PKEY_RSA;
391
392     if (pmeth == NULL) {
393         *pnids = &rnid;
394         return 1;
395     }
396
397     if (nid == EVP_PKEY_RSA) {
398         *pmeth = dasync_rsa;
399         return 1;
400     }
401
402     *pmeth = NULL;
403     return 0;
404 }
405
406 static int dasync_digests(ENGINE *e, const EVP_MD **digest,
407                           const int **nids, int nid)
408 {
409     int ok = 1;
410     if (!digest) {
411         /* We are returning a list of supported nids */
412         return dasync_digest_nids(nids);
413     }
414     /* We are being asked for a specific digest */
415     switch (nid) {
416     case NID_sha1:
417         *digest = dasync_sha1();
418         break;
419     default:
420         ok = 0;
421         *digest = NULL;
422         break;
423     }
424     return ok;
425 }
426
427 static int dasync_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
428                                    const int **nids, int nid)
429 {
430     int ok = 1;
431     if (cipher == NULL) {
432         /* We are returning a list of supported nids */
433         *nids = dasync_cipher_nids;
434         return (sizeof(dasync_cipher_nids) -
435                 1) / sizeof(dasync_cipher_nids[0]);
436     }
437     /* We are being asked for a specific cipher */
438     switch (nid) {
439     case NID_aes_128_cbc:
440         *cipher = dasync_aes_128_cbc();
441         break;
442     case NID_aes_128_cbc_hmac_sha1:
443         *cipher = dasync_aes_128_cbc_hmac_sha1();
444         break;
445     default:
446         ok = 0;
447         *cipher = NULL;
448         break;
449     }
450     return ok;
451 }
452
453 static void wait_cleanup(ASYNC_WAIT_CTX *ctx, const void *key,
454                          OSSL_ASYNC_FD readfd, void *pvwritefd)
455 {
456     OSSL_ASYNC_FD *pwritefd = (OSSL_ASYNC_FD *)pvwritefd;
457 #if defined(ASYNC_WIN)
458     CloseHandle(readfd);
459     CloseHandle(*pwritefd);
460 #elif defined(ASYNC_POSIX)
461     close(readfd);
462     close(*pwritefd);
463 #endif
464     OPENSSL_free(pwritefd);
465 }
466
467 #define DUMMY_CHAR 'X'
468
469 static void dummy_pause_job(void) {
470     ASYNC_JOB *job;
471     ASYNC_WAIT_CTX *waitctx;
472     ASYNC_callback_fn callback;
473     void * callback_arg;
474     OSSL_ASYNC_FD pipefds[2] = {0, 0};
475     OSSL_ASYNC_FD *writefd;
476 #if defined(ASYNC_WIN)
477     DWORD numwritten, numread;
478     char buf = DUMMY_CHAR;
479 #elif defined(ASYNC_POSIX)
480     char buf = DUMMY_CHAR;
481 #endif
482
483     if ((job = ASYNC_get_current_job()) == NULL)
484         return;
485
486     waitctx = ASYNC_get_wait_ctx(job);
487
488     if (ASYNC_WAIT_CTX_get_callback(waitctx, &callback, &callback_arg) && callback != NULL) {
489         /*
490          * In the Dummy async engine we are cheating. We call the callback that the job
491          * is complete before the call to ASYNC_pause_job(). A real
492          * async engine would only call the callback when the job was actually complete
493          */
494         (*callback)(callback_arg);
495         ASYNC_pause_job();
496         return;
497     }
498
499
500     if (ASYNC_WAIT_CTX_get_fd(waitctx, engine_dasync_id, &pipefds[0],
501                               (void **)&writefd)) {
502         pipefds[1] = *writefd;
503     } else {
504         writefd = OPENSSL_malloc(sizeof(*writefd));
505         if (writefd == NULL)
506             return;
507 #if defined(ASYNC_WIN)
508         if (CreatePipe(&pipefds[0], &pipefds[1], NULL, 256) == 0) {
509             OPENSSL_free(writefd);
510             return;
511         }
512 #elif defined(ASYNC_POSIX)
513         if (pipe(pipefds) != 0) {
514             OPENSSL_free(writefd);
515             return;
516         }
517 #endif
518         *writefd = pipefds[1];
519
520         if (!ASYNC_WAIT_CTX_set_wait_fd(waitctx, engine_dasync_id, pipefds[0],
521                                         writefd, wait_cleanup)) {
522             wait_cleanup(waitctx, engine_dasync_id, pipefds[0], writefd);
523             return;
524         }
525     }
526     /*
527      * In the Dummy async engine we are cheating. We signal that the job
528      * is complete by waking it before the call to ASYNC_pause_job(). A real
529      * async engine would only wake when the job was actually complete
530      */
531 #if defined(ASYNC_WIN)
532     WriteFile(pipefds[1], &buf, 1, &numwritten, NULL);
533 #elif defined(ASYNC_POSIX)
534     if (write(pipefds[1], &buf, 1) < 0)
535         return;
536 #endif
537
538     /* Ignore errors - we carry on anyway */
539     ASYNC_pause_job();
540
541     /* Clear the wake signal */
542 #if defined(ASYNC_WIN)
543     ReadFile(pipefds[0], &buf, 1, &numread, NULL);
544 #elif defined(ASYNC_POSIX)
545     if (read(pipefds[0], &buf, 1) < 0)
546         return;
547 #endif
548 }
549
550 /*
551  * SHA1 implementation. At the moment we just defer to the standard
552  * implementation
553  */
554 static int dasync_sha1_init(EVP_MD_CTX *ctx)
555 {
556     dummy_pause_job();
557
558     return EVP_MD_meth_get_init(EVP_sha1())(ctx);
559 }
560
561 static int dasync_sha1_update(EVP_MD_CTX *ctx, const void *data,
562                              size_t count)
563 {
564     dummy_pause_job();
565
566     return EVP_MD_meth_get_update(EVP_sha1())(ctx, data, count);
567 }
568
569 static int dasync_sha1_final(EVP_MD_CTX *ctx, unsigned char *md)
570 {
571     dummy_pause_job();
572
573     return EVP_MD_meth_get_final(EVP_sha1())(ctx, md);
574 }
575
576 /* Cipher helper functions */
577
578 static int dasync_cipher_ctrl_helper(EVP_CIPHER_CTX *ctx, int type, int arg,
579                                      void *ptr, int aeadcapable)
580 {
581     int ret;
582     struct dasync_pipeline_ctx *pipe_ctx =
583         (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
584
585     if (pipe_ctx == NULL)
586         return 0;
587
588     switch (type) {
589         case EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS:
590             pipe_ctx->numpipes = arg;
591             pipe_ctx->outbufs = (unsigned char **)ptr;
592             break;
593
594         case EVP_CTRL_SET_PIPELINE_INPUT_BUFS:
595             pipe_ctx->numpipes = arg;
596             pipe_ctx->inbufs = (unsigned char **)ptr;
597             break;
598
599         case EVP_CTRL_SET_PIPELINE_INPUT_LENS:
600             pipe_ctx->numpipes = arg;
601             pipe_ctx->lens = (size_t *)ptr;
602             break;
603
604         case EVP_CTRL_AEAD_SET_MAC_KEY:
605             if (!aeadcapable)
606                 return -1;
607             EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
608             ret = EVP_CIPHER_meth_get_ctrl(EVP_aes_128_cbc_hmac_sha1())
609                                           (ctx, type, arg, ptr);
610             EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
611             return ret;
612
613         case EVP_CTRL_AEAD_TLS1_AAD:
614         {
615             unsigned char *p = ptr;
616             unsigned int len;
617
618             if (!aeadcapable || arg != EVP_AEAD_TLS1_AAD_LEN)
619                 return -1;
620
621             if (pipe_ctx->aadctr >= SSL_MAX_PIPELINES)
622                 return -1;
623
624             memcpy(pipe_ctx->tlsaad[pipe_ctx->aadctr], ptr,
625                    EVP_AEAD_TLS1_AAD_LEN);
626             pipe_ctx->aadctr++;
627
628             len = p[arg - 2] << 8 | p[arg - 1];
629
630             if (EVP_CIPHER_CTX_is_encrypting(ctx)) {
631                 if ((p[arg - 4] << 8 | p[arg - 3]) >= TLS1_1_VERSION) {
632                     if (len < AES_BLOCK_SIZE)
633                         return 0;
634                     len -= AES_BLOCK_SIZE;
635                 }
636
637                 return ((len + SHA_DIGEST_LENGTH + AES_BLOCK_SIZE)
638                         & -AES_BLOCK_SIZE) - len;
639             } else {
640                 return SHA_DIGEST_LENGTH;
641             }
642         }
643
644         default:
645             return 0;
646     }
647
648     return 1;
649 }
650
651 static int dasync_cipher_init_key_helper(EVP_CIPHER_CTX *ctx,
652                                          const unsigned char *key,
653                                          const unsigned char *iv, int enc,
654                                          const EVP_CIPHER *cipher)
655 {
656     int ret;
657     struct dasync_pipeline_ctx *pipe_ctx =
658         (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
659
660     if (pipe_ctx->inner_cipher_data == NULL
661             && EVP_CIPHER_impl_ctx_size(cipher) != 0) {
662         pipe_ctx->inner_cipher_data = OPENSSL_zalloc(
663             EVP_CIPHER_impl_ctx_size(cipher));
664         if (pipe_ctx->inner_cipher_data == NULL) {
665             DASYNCerr(DASYNC_F_DASYNC_CIPHER_INIT_KEY_HELPER,
666                         ERR_R_MALLOC_FAILURE);
667             return 0;
668         }
669     }
670
671     pipe_ctx->numpipes = 0;
672     pipe_ctx->aadctr = 0;
673
674     EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
675     ret = EVP_CIPHER_meth_get_init(cipher)(ctx, key, iv, enc);
676     EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
677
678     return ret;
679 }
680
681 static int dasync_cipher_helper(EVP_CIPHER_CTX *ctx, unsigned char *out,
682                                 const unsigned char *in, size_t inl,
683                                 const EVP_CIPHER *cipher)
684 {
685     int ret = 1;
686     unsigned int i, pipes;
687     struct dasync_pipeline_ctx *pipe_ctx =
688         (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
689
690     pipes = pipe_ctx->numpipes;
691     EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
692     if (pipes == 0) {
693         if (pipe_ctx->aadctr != 0) {
694             if (pipe_ctx->aadctr != 1)
695                 return -1;
696             EVP_CIPHER_meth_get_ctrl(cipher)
697                                     (ctx, EVP_CTRL_AEAD_TLS1_AAD,
698                                      EVP_AEAD_TLS1_AAD_LEN,
699                                      pipe_ctx->tlsaad[0]);
700         }
701         ret = EVP_CIPHER_meth_get_do_cipher(cipher)
702                                            (ctx, out, in, inl);
703     } else {
704         if (pipe_ctx->aadctr > 0 && pipe_ctx->aadctr != pipes)
705             return -1;
706         for (i = 0; i < pipes; i++) {
707             if (pipe_ctx->aadctr > 0) {
708                 EVP_CIPHER_meth_get_ctrl(cipher)
709                                         (ctx, EVP_CTRL_AEAD_TLS1_AAD,
710                                          EVP_AEAD_TLS1_AAD_LEN,
711                                          pipe_ctx->tlsaad[i]);
712             }
713             ret = ret && EVP_CIPHER_meth_get_do_cipher(cipher)
714                                 (ctx, pipe_ctx->outbufs[i], pipe_ctx->inbufs[i],
715                                  pipe_ctx->lens[i]);
716         }
717         pipe_ctx->numpipes = 0;
718     }
719     pipe_ctx->aadctr = 0;
720     EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
721     return ret;
722 }
723
724 static int dasync_cipher_cleanup_helper(EVP_CIPHER_CTX *ctx,
725                                         const EVP_CIPHER *cipher)
726 {
727     struct dasync_pipeline_ctx *pipe_ctx =
728         (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
729
730     OPENSSL_clear_free(pipe_ctx->inner_cipher_data,
731                        EVP_CIPHER_impl_ctx_size(cipher));
732
733     return 1;
734 }
735
736 /*
737  * AES128 CBC Implementation
738  */
739
740 static int dasync_aes128_cbc_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
741                                   void *ptr)
742 {
743     return dasync_cipher_ctrl_helper(ctx, type, arg, ptr, 0);
744 }
745
746 static int dasync_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
747                              const unsigned char *iv, int enc)
748 {
749     return dasync_cipher_init_key_helper(ctx, key, iv, enc, EVP_aes_128_cbc());
750 }
751
752 static int dasync_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
753                                const unsigned char *in, size_t inl)
754 {
755     return dasync_cipher_helper(ctx, out, in, inl, EVP_aes_128_cbc());
756 }
757
758 static int dasync_aes128_cbc_cleanup(EVP_CIPHER_CTX *ctx)
759 {
760     return dasync_cipher_cleanup_helper(ctx, EVP_aes_128_cbc());
761 }
762
763
764 /*
765  * AES128 CBC HMAC SHA1 Implementation
766  */
767
768 static int dasync_aes128_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX *ctx, int type,
769                                              int arg, void *ptr)
770 {
771     return dasync_cipher_ctrl_helper(ctx, type, arg, ptr, 1);
772 }
773
774 static int dasync_aes128_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX *ctx,
775                                                 const unsigned char *key,
776                                                 const unsigned char *iv,
777                                                 int enc)
778 {
779     /*
780      * We can safely assume that EVP_aes_128_cbc_hmac_sha1() != NULL,
781      * see comment before the definition of dasync_aes_128_cbc_hmac_sha1().
782      */
783     return dasync_cipher_init_key_helper(ctx, key, iv, enc,
784                                          EVP_aes_128_cbc_hmac_sha1());
785 }
786
787 static int dasync_aes128_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx,
788                                                unsigned char *out,
789                                                const unsigned char *in,
790                                                size_t inl)
791 {
792     return dasync_cipher_helper(ctx, out, in, inl, EVP_aes_128_cbc_hmac_sha1());
793 }
794
795 static int dasync_aes128_cbc_hmac_sha1_cleanup(EVP_CIPHER_CTX *ctx)
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_cleanup_helper(ctx, EVP_aes_128_cbc_hmac_sha1());
802 }
803
804
805 /*
806  * RSA implementation
807  */
808 static int dasync_rsa_init(EVP_PKEY_CTX *ctx)
809 {
810     static int (*pinit)(EVP_PKEY_CTX *ctx);
811
812     if (pinit == NULL)
813         EVP_PKEY_meth_get_init(dasync_rsa_orig, &pinit);
814     return pinit(ctx);
815 }
816
817 static void dasync_rsa_cleanup(EVP_PKEY_CTX *ctx)
818 {
819     static void (*pcleanup)(EVP_PKEY_CTX *ctx);
820
821     if (pcleanup == NULL)
822         EVP_PKEY_meth_get_cleanup(dasync_rsa_orig, &pcleanup);
823     pcleanup(ctx);
824 }
825
826 static int dasync_rsa_paramgen_init(EVP_PKEY_CTX *ctx)
827 {
828     static int (*pparamgen_init)(EVP_PKEY_CTX *ctx);
829
830     if (pparamgen_init == NULL)
831         EVP_PKEY_meth_get_paramgen(dasync_rsa_orig, &pparamgen_init, NULL);
832     return pparamgen_init(ctx);
833 }
834
835 static int dasync_rsa_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
836 {
837     static int (*pparamgen)(EVP_PKEY_CTX *c, EVP_PKEY *pkey);
838
839     if (pparamgen == NULL)
840         EVP_PKEY_meth_get_paramgen(dasync_rsa_orig, NULL, &pparamgen);
841     return pparamgen(ctx, pkey);
842 }
843
844 static int dasync_rsa_keygen_init(EVP_PKEY_CTX *ctx)
845 {
846     static int (*pkeygen_init)(EVP_PKEY_CTX *ctx);
847
848     if (pkeygen_init == NULL)
849         EVP_PKEY_meth_get_keygen(dasync_rsa_orig, &pkeygen_init, NULL);
850     return pkeygen_init(ctx);
851 }
852
853 static int dasync_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
854 {
855     static int (*pkeygen)(EVP_PKEY_CTX *c, EVP_PKEY *pkey);
856
857     if (pkeygen == NULL)
858         EVP_PKEY_meth_get_keygen(dasync_rsa_orig, NULL, &pkeygen);
859     return pkeygen(ctx, pkey);
860 }
861
862 static int dasync_rsa_encrypt_init(EVP_PKEY_CTX *ctx)
863 {
864     static int (*pencrypt_init)(EVP_PKEY_CTX *ctx);
865
866     if (pencrypt_init == NULL)
867         EVP_PKEY_meth_get_encrypt(dasync_rsa_orig, &pencrypt_init, NULL);
868     return pencrypt_init(ctx);
869 }
870
871 static int dasync_rsa_encrypt(EVP_PKEY_CTX *ctx, unsigned char *out,
872                               size_t *outlen, const unsigned char *in,
873                               size_t inlen)
874 {
875     static int (*pencryptfn)(EVP_PKEY_CTX *ctx, unsigned char *out,
876                              size_t *outlen, const unsigned char *in,
877                              size_t inlen);
878
879     if (pencryptfn == NULL)
880         EVP_PKEY_meth_get_encrypt(dasync_rsa_orig, NULL, &pencryptfn);
881     return pencryptfn(ctx, out, outlen, in, inlen);
882 }
883
884 static int dasync_rsa_decrypt_init(EVP_PKEY_CTX *ctx)
885 {
886     static int (*pdecrypt_init)(EVP_PKEY_CTX *ctx);
887
888     if (pdecrypt_init == NULL)
889         EVP_PKEY_meth_get_decrypt(dasync_rsa_orig, &pdecrypt_init, NULL);
890     return pdecrypt_init(ctx);
891 }
892
893 static int dasync_rsa_decrypt(EVP_PKEY_CTX *ctx, unsigned char *out,
894                               size_t *outlen, const unsigned char *in,
895                               size_t inlen)
896 {
897     static int (*pdecrypt)(EVP_PKEY_CTX *ctx, unsigned char *out,
898                              size_t *outlen, const unsigned char *in,
899                              size_t inlen);
900
901     if (pdecrypt == NULL)
902         EVP_PKEY_meth_get_encrypt(dasync_rsa_orig, NULL, &pdecrypt);
903     return pdecrypt(ctx, out, outlen, in, inlen);
904 }
905
906 static int dasync_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
907 {
908     static int (*pctrl)(EVP_PKEY_CTX *ctx, int type, int p1, void *p2);
909
910     if (pctrl == NULL)
911         EVP_PKEY_meth_get_ctrl(dasync_rsa_orig, &pctrl, NULL);
912     return pctrl(ctx, type, p1, p2);
913 }
914
915 static int dasync_rsa_ctrl_str(EVP_PKEY_CTX *ctx, const char *type,
916                                const char *value)
917 {
918     static int (*pctrl_str)(EVP_PKEY_CTX *ctx, const char *type,
919                             const char *value);
920
921     if (pctrl_str == NULL)
922         EVP_PKEY_meth_get_ctrl(dasync_rsa_orig, NULL, &pctrl_str);
923     return pctrl_str(ctx, type, value);
924 }