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