2 * Written by Matt Caswell (matt@openssl.org) for the OpenSSL project.
4 /* ====================================================================
5 * Copyright (c) 2015 The OpenSSL Project. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
19 * 3. All advertising materials mentioning features or use of this
20 * software must display the following acknowledgment:
21 * "This product includes software developed by the OpenSSL Project
22 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
25 * endorse or promote products derived from this software without
26 * prior written permission. For written permission, please contact
27 * licensing@OpenSSL.org.
29 * 5. Products derived from this software may not be called "OpenSSL"
30 * nor may "OpenSSL" appear in their names without prior written
31 * permission of the OpenSSL Project.
33 * 6. Redistributions of any form whatsoever must retain the following
35 * "This product includes software developed by the OpenSSL Project
36 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
39 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
41 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
42 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
47 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
49 * OF THE POSSIBILITY OF SUCH DAMAGE.
50 * ====================================================================
56 #include <openssl/engine.h>
57 #include <openssl/sha.h>
58 #include <openssl/rsa.h>
59 #include <openssl/evp.h>
60 #include <openssl/async.h>
61 #include <openssl/bn.h>
62 #include <openssl/crypto.h>
64 #if (defined(OPENSSL_SYS_UNIX) || defined(OPENSSL_SYS_CYGWIN)) && defined(OPENSSL_THREADS)
74 #define DASYNC_LIB_NAME "DASYNC"
75 #include "e_dasync_err.c"
77 /* Engine Id and Name */
78 static const char *engine_dasync_id = "dasync";
79 static const char *engine_dasync_name = "Dummy Async engine support";
82 /* Engine Lifetime functions */
83 static int dasync_destroy(ENGINE *e);
84 static int dasync_init(ENGINE *e);
85 static int dasync_finish(ENGINE *e);
86 void engine_load_dasync_internal(void);
89 /* Set up digests. Just SHA1 for now */
90 static int dasync_digests(ENGINE *e, const EVP_MD **digest,
91 const int **nids, int nid);
93 static void dummy_pause_job(void);
96 static int dasync_sha1_init(EVP_MD_CTX *ctx);
97 static int dasync_sha1_update(EVP_MD_CTX *ctx, const void *data,
99 static int dasync_sha1_final(EVP_MD_CTX *ctx, unsigned char *md);
101 static EVP_MD *_hidden_sha1_md = NULL;
102 static const EVP_MD *dasync_sha1(void)
104 if (_hidden_sha1_md == NULL) {
107 if ((md = EVP_MD_meth_new(NID_sha1, NID_sha1WithRSAEncryption)) == NULL
108 || !EVP_MD_meth_set_result_size(md, SHA_DIGEST_LENGTH)
109 || !EVP_MD_meth_set_input_blocksize(md, SHA_CBLOCK)
110 || !EVP_MD_meth_set_app_datasize(md,
111 sizeof(EVP_MD *) + sizeof(SHA_CTX))
112 || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_DIGALGID_ABSENT)
113 || !EVP_MD_meth_set_init(md, dasync_sha1_init)
114 || !EVP_MD_meth_set_update(md, dasync_sha1_update)
115 || !EVP_MD_meth_set_final(md, dasync_sha1_final)) {
116 EVP_MD_meth_free(md);
119 _hidden_sha1_md = md;
121 return _hidden_sha1_md;
123 static void destroy_digests(void)
125 EVP_MD_meth_free(_hidden_sha1_md);
126 _hidden_sha1_md = NULL;
128 static int dasync_digest_nids(const int **nids)
130 static int digest_nids[2] = { 0, 0 };
136 if ((md = dasync_sha1()) != NULL)
137 digest_nids[pos++] = EVP_MD_type(md);
138 digest_nids[pos] = 0;
147 static int dasync_pub_enc(int flen, const unsigned char *from,
148 unsigned char *to, RSA *rsa, int padding);
149 static int dasync_pub_dec(int flen, const unsigned char *from,
150 unsigned char *to, RSA *rsa, int padding);
151 static int dasync_rsa_priv_enc(int flen, const unsigned char *from,
152 unsigned char *to, RSA *rsa, int padding);
153 static int dasync_rsa_priv_dec(int flen, const unsigned char *from,
154 unsigned char *to, RSA *rsa, int padding);
155 static int dasync_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
158 static int dasync_rsa_init(RSA *rsa);
159 static int dasync_rsa_finish(RSA *rsa);
161 static RSA_METHOD dasync_rsa_method = {
162 "Dummy Async RSA method",
163 dasync_pub_enc, /* pub_enc */
164 dasync_pub_dec, /* pub_dec */
165 dasync_rsa_priv_enc, /* priv_enc */
166 dasync_rsa_priv_dec, /* priv_dec */
167 dasync_rsa_mod_exp, /* rsa_mod_exp */
168 BN_mod_exp_mont, /* bn_mod_exp */
169 dasync_rsa_init, /* init */
170 dasync_rsa_finish, /* finish */
175 NULL /* rsa_keygen */
179 static int bind_dasync(ENGINE *e)
181 /* Ensure the dasync error handling is set up */
182 ERR_load_DASYNC_strings();
184 if (!ENGINE_set_id(e, engine_dasync_id)
185 || !ENGINE_set_name(e, engine_dasync_name)
186 || !ENGINE_set_RSA(e, &dasync_rsa_method)
187 || !ENGINE_set_digests(e, dasync_digests)
188 || !ENGINE_set_destroy_function(e, dasync_destroy)
189 || !ENGINE_set_init_function(e, dasync_init)
190 || !ENGINE_set_finish_function(e, dasync_finish)) {
191 DASYNCerr(DASYNC_F_BIND_DASYNC, DASYNC_R_INIT_FAILED);
198 # ifndef OPENSSL_NO_DYNAMIC_ENGINE
199 static int bind_helper(ENGINE *e, const char *id)
201 if (id && (strcmp(id, engine_dasync_id) != 0))
208 IMPLEMENT_DYNAMIC_CHECK_FN()
209 IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
212 static ENGINE *engine_dasync(void)
214 ENGINE *ret = ENGINE_new();
217 if (!bind_dasync(ret)) {
224 void engine_load_dasync_internal(void)
226 ENGINE *toadd = engine_dasync();
234 static int dasync_init(ENGINE *e)
240 static int dasync_finish(ENGINE *e)
246 static int dasync_destroy(ENGINE *e)
249 ERR_unload_DASYNC_strings();
253 static int dasync_digests(ENGINE *e, const EVP_MD **digest,
254 const int **nids, int nid)
258 /* We are returning a list of supported nids */
259 return dasync_digest_nids(nids);
261 /* We are being asked for a specific digest */
264 *digest = dasync_sha1();
274 static void wait_cleanup(ASYNC_WAIT_CTX *ctx, const void *key,
275 OSSL_ASYNC_FD readfd, void *pvwritefd)
277 OSSL_ASYNC_FD *pwritefd = (OSSL_ASYNC_FD *)pvwritefd;
278 #if defined(ASYNC_WIN)
280 CloseHandle(*pwritefd);
281 #elif defined(ASYNC_POSIX)
285 OPENSSL_free(pwritefd);
288 #define DUMMY_CHAR 'X'
290 static void dummy_pause_job(void) {
292 ASYNC_WAIT_CTX *waitctx;
293 OSSL_ASYNC_FD pipefds[2] = {0, 0};
294 OSSL_ASYNC_FD *writefd;
295 #if defined(ASYNC_WIN)
296 DWORD numwritten, numread;
297 char buf = DUMMY_CHAR;
298 #elif defined(ASYNC_POSIX)
299 char buf = DUMMY_CHAR;
302 if ((job = ASYNC_get_current_job()) == NULL)
305 waitctx = ASYNC_get_wait_ctx(job);
307 if (ASYNC_WAIT_CTX_get_fd(waitctx, engine_dasync_id, &pipefds[0],
308 (void **)&writefd)) {
309 pipefds[1] = *writefd;
311 writefd = OPENSSL_malloc(sizeof(*writefd));
314 #if defined(ASYNC_WIN)
315 if (CreatePipe(&pipefds[0], &pipefds[1], NULL, 256) == 0) {
316 OPENSSL_free(writefd);
319 #elif defined(ASYNC_POSIX)
320 if (pipe(pipefds) != 0) {
321 OPENSSL_free(writefd);
325 *writefd = pipefds[1];
327 if(!ASYNC_WAIT_CTX_set_wait_fd(waitctx, engine_dasync_id, pipefds[0],
328 writefd, wait_cleanup)) {
329 wait_cleanup(waitctx, engine_dasync_id, pipefds[0], writefd);
334 * In the Dummy async engine we are cheating. We signal that the job
335 * is complete by waking it before the call to ASYNC_pause_job(). A real
336 * async engine would only wake when the job was actually complete
338 #if defined(ASYNC_WIN)
339 WriteFile(pipefds[1], &buf, 1, &numwritten, NULL);
340 #elif defined(ASYNC_POSIX)
341 write(pipefds[1], &buf, 1);
344 /* Ignore errors - we carry on anyway */
347 /* Clear the wake signal */
348 #if defined(ASYNC_WIN)
349 ReadFile(pipefds[0], &buf, 1, &numread, NULL);
350 #elif defined(ASYNC_POSIX)
351 read(pipefds[0], &buf, 1);
356 * SHA1 implementation. At the moment we just defer to the standard
360 #define data(ctx) ((SHA_CTX *)EVP_MD_CTX_md_data(ctx))
361 static int dasync_sha1_init(EVP_MD_CTX *ctx)
365 return SHA1_Init(data(ctx));
368 static int dasync_sha1_update(EVP_MD_CTX *ctx, const void *data,
373 return SHA1_Update(data(ctx), data, (size_t)count);
376 static int dasync_sha1_final(EVP_MD_CTX *ctx, unsigned char *md)
380 return SHA1_Final(md, data(ctx));
387 static int dasync_pub_enc(int flen, const unsigned char *from,
388 unsigned char *to, RSA *rsa, int padding) {
389 /* Ignore errors - we carry on anyway */
391 return RSA_PKCS1_OpenSSL()->rsa_pub_enc(flen, from, to, rsa, padding);
394 static int dasync_pub_dec(int flen, const unsigned char *from,
395 unsigned char *to, RSA *rsa, int padding) {
396 /* Ignore errors - we carry on anyway */
398 return RSA_PKCS1_OpenSSL()->rsa_pub_dec(flen, from, to, rsa, padding);
401 static int dasync_rsa_priv_enc(int flen, const unsigned char *from,
402 unsigned char *to, RSA *rsa, int padding)
404 /* Ignore errors - we carry on anyway */
406 return RSA_PKCS1_OpenSSL()->rsa_priv_enc(flen, from, to, rsa, padding);
409 static int dasync_rsa_priv_dec(int flen, const unsigned char *from,
410 unsigned char *to, RSA *rsa, int padding)
412 /* Ignore errors - we carry on anyway */
414 return RSA_PKCS1_OpenSSL()->rsa_priv_dec(flen, from, to, rsa, padding);
417 static int dasync_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
419 /* Ignore errors - we carry on anyway */
421 return RSA_PKCS1_OpenSSL()->rsa_mod_exp(r0, I, rsa, ctx);
424 static int dasync_rsa_init(RSA *rsa)
426 return RSA_PKCS1_OpenSSL()->init(rsa);
428 static int dasync_rsa_finish(RSA *rsa)
430 return RSA_PKCS1_OpenSSL()->finish(rsa);