e_dasync.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. * Written by Matt Caswell (matt@openssl.org) for the OpenSSL project.
  3. */
  4. /* ====================================================================
  5. * Copyright (c) 2015 The OpenSSL Project. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  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
  17. * distribution.
  18. *
  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/)"
  23. *
  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.
  28. *
  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.
  32. *
  33. * 6. Redistributions of any form whatsoever must retain the following
  34. * acknowledgment:
  35. * "This product includes software developed by the OpenSSL Project
  36. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  37. *
  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. * ====================================================================
  51. */
  52. #include <stdio.h>
  53. #include <string.h>
  54. #include <openssl/engine.h>
  55. #include <openssl/sha.h>
  56. #include <openssl/rsa.h>
  57. #include <openssl/evp.h>
  58. #include <openssl/async.h>
  59. #include <openssl/bn.h>
  60. #include <openssl/crypto.h>
  61. #define DASYNC_LIB_NAME "DASYNC"
  62. #include "e_dasync_err.c"
  63. /* Engine Id and Name */
  64. static const char *engine_dasync_id = "dasync";
  65. static const char *engine_dasync_name = "Dummy Async engine support";
  66. /* Engine Lifetime functions */
  67. static int dasync_destroy(ENGINE *e);
  68. static int dasync_init(ENGINE *e);
  69. static int dasync_finish(ENGINE *e);
  70. void engine_load_dasync_internal(void);
  71. /* Set up digests. Just SHA1 for now */
  72. static int dasync_digests(ENGINE *e, const EVP_MD **digest,
  73. const int **nids, int nid);
  74. static void dummy_pause_job(void);
  75. /* SHA1 */
  76. static int dasync_sha1_init(EVP_MD_CTX *ctx);
  77. static int dasync_sha1_update(EVP_MD_CTX *ctx, const void *data,
  78. size_t count);
  79. static int dasync_sha1_final(EVP_MD_CTX *ctx, unsigned char *md);
  80. static EVP_MD *_hidden_sha1_md = NULL;
  81. static const EVP_MD *dasync_sha1(void)
  82. {
  83. if (_hidden_sha1_md == NULL) {
  84. EVP_MD *md;
  85. if ((md = EVP_MD_meth_new(NID_sha1, NID_sha1WithRSAEncryption)) == NULL
  86. || !EVP_MD_meth_set_result_size(md, SHA_DIGEST_LENGTH)
  87. || !EVP_MD_meth_set_input_blocksize(md, SHA_CBLOCK)
  88. || !EVP_MD_meth_set_app_datasize(md,
  89. sizeof(EVP_MD *) + sizeof(SHA_CTX))
  90. || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_DIGALGID_ABSENT)
  91. || !EVP_MD_meth_set_init(md, dasync_sha1_init)
  92. || !EVP_MD_meth_set_update(md, dasync_sha1_update)
  93. || !EVP_MD_meth_set_final(md, dasync_sha1_final)) {
  94. EVP_MD_meth_free(md);
  95. md = NULL;
  96. }
  97. _hidden_sha1_md = md;
  98. }
  99. return _hidden_sha1_md;
  100. }
  101. static void destroy_digests(void)
  102. {
  103. EVP_MD_meth_free(_hidden_sha1_md);
  104. _hidden_sha1_md = NULL;
  105. }
  106. static int dasync_digest_nids(const int **nids)
  107. {
  108. static int digest_nids[2] = { 0, 0 };
  109. static int pos = 0;
  110. static int init = 0;
  111. if (!init) {
  112. const EVP_MD *md;
  113. if ((md = dasync_sha1()) != NULL)
  114. digest_nids[pos++] = EVP_MD_type(md);
  115. digest_nids[pos] = 0;
  116. init = 1;
  117. }
  118. *nids = digest_nids;
  119. return pos;
  120. }
  121. /* RSA */
  122. static int dasync_pub_enc(int flen, const unsigned char *from,
  123. unsigned char *to, RSA *rsa, int padding);
  124. static int dasync_pub_dec(int flen, const unsigned char *from,
  125. unsigned char *to, RSA *rsa, int padding);
  126. static int dasync_rsa_priv_enc(int flen, const unsigned char *from,
  127. unsigned char *to, RSA *rsa, int padding);
  128. static int dasync_rsa_priv_dec(int flen, const unsigned char *from,
  129. unsigned char *to, RSA *rsa, int padding);
  130. static int dasync_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
  131. BN_CTX *ctx);
  132. static int dasync_rsa_init(RSA *rsa);
  133. static int dasync_rsa_finish(RSA *rsa);
  134. static RSA_METHOD dasync_rsa_method = {
  135. "Dummy Async RSA method",
  136. dasync_pub_enc, /* pub_enc */
  137. dasync_pub_dec, /* pub_dec */
  138. dasync_rsa_priv_enc, /* priv_enc */
  139. dasync_rsa_priv_dec, /* priv_dec */
  140. dasync_rsa_mod_exp, /* rsa_mod_exp */
  141. BN_mod_exp_mont, /* bn_mod_exp */
  142. dasync_rsa_init, /* init */
  143. dasync_rsa_finish, /* finish */
  144. 0, /* flags */
  145. NULL, /* app_data */
  146. 0, /* rsa_sign */
  147. 0, /* rsa_verify */
  148. NULL /* rsa_keygen */
  149. };
  150. static int bind_dasync(ENGINE *e)
  151. {
  152. /* Ensure the dasync error handling is set up */
  153. ERR_load_DASYNC_strings();
  154. if (!ENGINE_set_id(e, engine_dasync_id)
  155. || !ENGINE_set_name(e, engine_dasync_name)
  156. || !ENGINE_set_RSA(e, &dasync_rsa_method)
  157. || !ENGINE_set_digests(e, dasync_digests)
  158. || !ENGINE_set_destroy_function(e, dasync_destroy)
  159. || !ENGINE_set_init_function(e, dasync_init)
  160. || !ENGINE_set_finish_function(e, dasync_finish)) {
  161. DASYNCerr(DASYNC_F_BIND_DASYNC, DASYNC_R_INIT_FAILED);
  162. return 0;
  163. }
  164. return 1;
  165. }
  166. # ifndef OPENSSL_NO_DYNAMIC_ENGINE
  167. static int bind_helper(ENGINE *e, const char *id)
  168. {
  169. if (id && (strcmp(id, engine_dasync_id) != 0))
  170. return 0;
  171. if (!bind_dasync(e))
  172. return 0;
  173. return 1;
  174. }
  175. IMPLEMENT_DYNAMIC_CHECK_FN()
  176. IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
  177. # endif
  178. static ENGINE *engine_dasync(void)
  179. {
  180. ENGINE *ret = ENGINE_new();
  181. if (!ret)
  182. return NULL;
  183. if (!bind_dasync(ret)) {
  184. ENGINE_free(ret);
  185. return NULL;
  186. }
  187. return ret;
  188. }
  189. void engine_load_dasync_internal(void)
  190. {
  191. ENGINE *toadd = engine_dasync();
  192. if (!toadd)
  193. return;
  194. ENGINE_add(toadd);
  195. ENGINE_free(toadd);
  196. ERR_clear_error();
  197. }
  198. static int dasync_init(ENGINE *e)
  199. {
  200. return 1;
  201. }
  202. static int dasync_finish(ENGINE *e)
  203. {
  204. return 1;
  205. }
  206. static int dasync_destroy(ENGINE *e)
  207. {
  208. destroy_digests();
  209. ERR_unload_DASYNC_strings();
  210. return 1;
  211. }
  212. static int dasync_digests(ENGINE *e, const EVP_MD **digest,
  213. const int **nids, int nid)
  214. {
  215. int ok = 1;
  216. if (!digest) {
  217. /* We are returning a list of supported nids */
  218. return dasync_digest_nids(nids);
  219. }
  220. /* We are being asked for a specific digest */
  221. switch (nid) {
  222. case NID_sha1:
  223. *digest = dasync_sha1();
  224. break;
  225. default:
  226. ok = 0;
  227. *digest = NULL;
  228. break;
  229. }
  230. return ok;
  231. }
  232. static void dummy_pause_job(void) {
  233. ASYNC_JOB *job;
  234. if ((job = ASYNC_get_current_job()) == NULL)
  235. return;
  236. /*
  237. * In the Dummy async engine we are cheating. We signal that the job
  238. * is complete by waking it before the call to ASYNC_pause_job(). A real
  239. * async engine would only wake when the job was actually complete
  240. */
  241. ASYNC_wake(job);
  242. /* Ignore errors - we carry on anyway */
  243. ASYNC_pause_job();
  244. ASYNC_clear_wake(job);
  245. }
  246. /*
  247. * SHA1 implementation. At the moment we just defer to the standard
  248. * implementation
  249. */
  250. #undef data
  251. #define data(ctx) ((SHA_CTX *)EVP_MD_CTX_md_data(ctx))
  252. static int dasync_sha1_init(EVP_MD_CTX *ctx)
  253. {
  254. dummy_pause_job();
  255. return SHA1_Init(data(ctx));
  256. }
  257. static int dasync_sha1_update(EVP_MD_CTX *ctx, const void *data,
  258. size_t count)
  259. {
  260. dummy_pause_job();
  261. return SHA1_Update(data(ctx), data, (size_t)count);
  262. }
  263. static int dasync_sha1_final(EVP_MD_CTX *ctx, unsigned char *md)
  264. {
  265. dummy_pause_job();
  266. return SHA1_Final(md, data(ctx));
  267. }
  268. /*
  269. * RSA implementation
  270. */
  271. static int dasync_pub_enc(int flen, const unsigned char *from,
  272. unsigned char *to, RSA *rsa, int padding) {
  273. /* Ignore errors - we carry on anyway */
  274. dummy_pause_job();
  275. return RSA_PKCS1_OpenSSL()->rsa_pub_enc(flen, from, to, rsa, padding);
  276. }
  277. static int dasync_pub_dec(int flen, const unsigned char *from,
  278. unsigned char *to, RSA *rsa, int padding) {
  279. /* Ignore errors - we carry on anyway */
  280. dummy_pause_job();
  281. return RSA_PKCS1_OpenSSL()->rsa_pub_dec(flen, from, to, rsa, padding);
  282. }
  283. static int dasync_rsa_priv_enc(int flen, const unsigned char *from,
  284. unsigned char *to, RSA *rsa, int padding)
  285. {
  286. /* Ignore errors - we carry on anyway */
  287. dummy_pause_job();
  288. return RSA_PKCS1_OpenSSL()->rsa_priv_enc(flen, from, to, rsa, padding);
  289. }
  290. static int dasync_rsa_priv_dec(int flen, const unsigned char *from,
  291. unsigned char *to, RSA *rsa, int padding)
  292. {
  293. /* Ignore errors - we carry on anyway */
  294. dummy_pause_job();
  295. return RSA_PKCS1_OpenSSL()->rsa_priv_dec(flen, from, to, rsa, padding);
  296. }
  297. static int dasync_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
  298. {
  299. /* Ignore errors - we carry on anyway */
  300. dummy_pause_job();
  301. return RSA_PKCS1_OpenSSL()->rsa_mod_exp(r0, I, rsa, ctx);
  302. }
  303. static int dasync_rsa_init(RSA *rsa)
  304. {
  305. return RSA_PKCS1_OpenSSL()->init(rsa);
  306. }
  307. static int dasync_rsa_finish(RSA *rsa)
  308. {
  309. return RSA_PKCS1_OpenSSL()->finish(rsa);
  310. }