e_dasync.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. /*
  2. * Copyright 2015-2018 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. #if defined(_WIN32)
  10. # include <windows.h>
  11. #endif
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <openssl/engine.h>
  15. #include <openssl/sha.h>
  16. #include <openssl/aes.h>
  17. #include <openssl/rsa.h>
  18. #include <openssl/evp.h>
  19. #include <openssl/async.h>
  20. #include <openssl/bn.h>
  21. #include <openssl/crypto.h>
  22. #include <openssl/ssl.h>
  23. #include <openssl/modes.h>
  24. #if defined(OPENSSL_SYS_UNIX) && defined(OPENSSL_THREADS)
  25. # undef ASYNC_POSIX
  26. # define ASYNC_POSIX
  27. # include <unistd.h>
  28. #elif defined(_WIN32)
  29. # undef ASYNC_WIN
  30. # define ASYNC_WIN
  31. #endif
  32. #include "e_dasync_err.c"
  33. /* Engine Id and Name */
  34. static const char *engine_dasync_id = "dasync";
  35. static const char *engine_dasync_name = "Dummy Async engine support";
  36. /* Engine Lifetime functions */
  37. static int dasync_destroy(ENGINE *e);
  38. static int dasync_init(ENGINE *e);
  39. static int dasync_finish(ENGINE *e);
  40. void engine_load_dasync_int(void);
  41. /* Set up digests. Just SHA1 for now */
  42. static int dasync_digests(ENGINE *e, const EVP_MD **digest,
  43. const int **nids, int nid);
  44. static void dummy_pause_job(void);
  45. /* SHA1 */
  46. static int dasync_sha1_init(EVP_MD_CTX *ctx);
  47. static int dasync_sha1_update(EVP_MD_CTX *ctx, const void *data,
  48. size_t count);
  49. static int dasync_sha1_final(EVP_MD_CTX *ctx, unsigned char *md);
  50. /*
  51. * Holds the EVP_MD object for sha1 in this engine. Set up once only during
  52. * engine bind and can then be reused many times.
  53. */
  54. static EVP_MD *_hidden_sha1_md = NULL;
  55. static const EVP_MD *dasync_sha1(void)
  56. {
  57. return _hidden_sha1_md;
  58. }
  59. static void destroy_digests(void)
  60. {
  61. EVP_MD_meth_free(_hidden_sha1_md);
  62. _hidden_sha1_md = NULL;
  63. }
  64. static int dasync_digest_nids(const int **nids)
  65. {
  66. static int digest_nids[2] = { 0, 0 };
  67. static int pos = 0;
  68. static int init = 0;
  69. if (!init) {
  70. const EVP_MD *md;
  71. if ((md = dasync_sha1()) != NULL)
  72. digest_nids[pos++] = EVP_MD_type(md);
  73. digest_nids[pos] = 0;
  74. init = 1;
  75. }
  76. *nids = digest_nids;
  77. return pos;
  78. }
  79. /* RSA */
  80. static int dasync_pub_enc(int flen, const unsigned char *from,
  81. unsigned char *to, RSA *rsa, int padding);
  82. static int dasync_pub_dec(int flen, const unsigned char *from,
  83. unsigned char *to, RSA *rsa, int padding);
  84. static int dasync_rsa_priv_enc(int flen, const unsigned char *from,
  85. unsigned char *to, RSA *rsa, int padding);
  86. static int dasync_rsa_priv_dec(int flen, const unsigned char *from,
  87. unsigned char *to, RSA *rsa, int padding);
  88. static int dasync_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
  89. BN_CTX *ctx);
  90. static int dasync_rsa_init(RSA *rsa);
  91. static int dasync_rsa_finish(RSA *rsa);
  92. static RSA_METHOD *dasync_rsa_method = NULL;
  93. /* AES */
  94. static int dasync_aes128_cbc_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
  95. void *ptr);
  96. static int dasync_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  97. const unsigned char *iv, int enc);
  98. static int dasync_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  99. const unsigned char *in, size_t inl);
  100. static int dasync_aes128_cbc_cleanup(EVP_CIPHER_CTX *ctx);
  101. static int dasync_aes128_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX *ctx, int type,
  102. int arg, void *ptr);
  103. static int dasync_aes128_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX *ctx,
  104. const unsigned char *key,
  105. const unsigned char *iv,
  106. int enc);
  107. static int dasync_aes128_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx,
  108. unsigned char *out,
  109. const unsigned char *in,
  110. size_t inl);
  111. static int dasync_aes128_cbc_hmac_sha1_cleanup(EVP_CIPHER_CTX *ctx);
  112. struct dasync_pipeline_ctx {
  113. void *inner_cipher_data;
  114. unsigned int numpipes;
  115. unsigned char **inbufs;
  116. unsigned char **outbufs;
  117. size_t *lens;
  118. unsigned char tlsaad[SSL_MAX_PIPELINES][EVP_AEAD_TLS1_AAD_LEN];
  119. unsigned int aadctr;
  120. };
  121. /*
  122. * Holds the EVP_CIPHER object for aes_128_cbc in this engine. Set up once only
  123. * during engine bind and can then be reused many times.
  124. */
  125. static EVP_CIPHER *_hidden_aes_128_cbc = NULL;
  126. static const EVP_CIPHER *dasync_aes_128_cbc(void)
  127. {
  128. return _hidden_aes_128_cbc;
  129. }
  130. /*
  131. * Holds the EVP_CIPHER object for aes_128_cbc_hmac_sha1 in this engine. Set up
  132. * once only during engine bind and can then be reused many times.
  133. *
  134. * This 'stitched' cipher depends on the EVP_aes_128_cbc_hmac_sha1() cipher,
  135. * which is implemented only if the AES-NI instruction set extension is available
  136. * (see OPENSSL_IA32CAP(3)). If that's not the case, then this cipher will not
  137. * be available either.
  138. *
  139. * Note: Since it is a legacy mac-then-encrypt cipher, modern TLS peers (which
  140. * negotiate the encrypt-then-mac extension) won't negotiate it anyway.
  141. */
  142. static EVP_CIPHER *_hidden_aes_128_cbc_hmac_sha1 = NULL;
  143. static const EVP_CIPHER *dasync_aes_128_cbc_hmac_sha1(void)
  144. {
  145. return _hidden_aes_128_cbc_hmac_sha1;
  146. }
  147. static void destroy_ciphers(void)
  148. {
  149. EVP_CIPHER_meth_free(_hidden_aes_128_cbc);
  150. EVP_CIPHER_meth_free(_hidden_aes_128_cbc_hmac_sha1);
  151. _hidden_aes_128_cbc = NULL;
  152. _hidden_aes_128_cbc_hmac_sha1 = NULL;
  153. }
  154. static int dasync_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
  155. const int **nids, int nid);
  156. static int dasync_cipher_nids[] = {
  157. NID_aes_128_cbc,
  158. NID_aes_128_cbc_hmac_sha1,
  159. 0
  160. };
  161. static int bind_dasync(ENGINE *e)
  162. {
  163. /* Setup RSA_METHOD */
  164. if ((dasync_rsa_method = RSA_meth_new("Dummy Async RSA method", 0)) == NULL
  165. || RSA_meth_set_pub_enc(dasync_rsa_method, dasync_pub_enc) == 0
  166. || RSA_meth_set_pub_dec(dasync_rsa_method, dasync_pub_dec) == 0
  167. || RSA_meth_set_priv_enc(dasync_rsa_method, dasync_rsa_priv_enc) == 0
  168. || RSA_meth_set_priv_dec(dasync_rsa_method, dasync_rsa_priv_dec) == 0
  169. || RSA_meth_set_mod_exp(dasync_rsa_method, dasync_rsa_mod_exp) == 0
  170. || RSA_meth_set_bn_mod_exp(dasync_rsa_method, BN_mod_exp_mont) == 0
  171. || RSA_meth_set_init(dasync_rsa_method, dasync_rsa_init) == 0
  172. || RSA_meth_set_finish(dasync_rsa_method, dasync_rsa_finish) == 0) {
  173. DASYNCerr(DASYNC_F_BIND_DASYNC, DASYNC_R_INIT_FAILED);
  174. return 0;
  175. }
  176. /* Ensure the dasync error handling is set up */
  177. ERR_load_DASYNC_strings();
  178. if (!ENGINE_set_id(e, engine_dasync_id)
  179. || !ENGINE_set_name(e, engine_dasync_name)
  180. || !ENGINE_set_RSA(e, dasync_rsa_method)
  181. || !ENGINE_set_digests(e, dasync_digests)
  182. || !ENGINE_set_ciphers(e, dasync_ciphers)
  183. || !ENGINE_set_destroy_function(e, dasync_destroy)
  184. || !ENGINE_set_init_function(e, dasync_init)
  185. || !ENGINE_set_finish_function(e, dasync_finish)) {
  186. DASYNCerr(DASYNC_F_BIND_DASYNC, DASYNC_R_INIT_FAILED);
  187. return 0;
  188. }
  189. /*
  190. * Set up the EVP_CIPHER and EVP_MD objects for the ciphers/digests
  191. * supplied by this engine
  192. */
  193. _hidden_sha1_md = EVP_MD_meth_new(NID_sha1, NID_sha1WithRSAEncryption);
  194. if (_hidden_sha1_md == NULL
  195. || !EVP_MD_meth_set_result_size(_hidden_sha1_md, SHA_DIGEST_LENGTH)
  196. || !EVP_MD_meth_set_input_blocksize(_hidden_sha1_md, SHA_CBLOCK)
  197. || !EVP_MD_meth_set_app_datasize(_hidden_sha1_md,
  198. sizeof(EVP_MD *) + sizeof(SHA_CTX))
  199. || !EVP_MD_meth_set_flags(_hidden_sha1_md, EVP_MD_FLAG_DIGALGID_ABSENT)
  200. || !EVP_MD_meth_set_init(_hidden_sha1_md, dasync_sha1_init)
  201. || !EVP_MD_meth_set_update(_hidden_sha1_md, dasync_sha1_update)
  202. || !EVP_MD_meth_set_final(_hidden_sha1_md, dasync_sha1_final)) {
  203. EVP_MD_meth_free(_hidden_sha1_md);
  204. _hidden_sha1_md = NULL;
  205. }
  206. _hidden_aes_128_cbc = EVP_CIPHER_meth_new(NID_aes_128_cbc,
  207. 16 /* block size */,
  208. 16 /* key len */);
  209. if (_hidden_aes_128_cbc == NULL
  210. || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_128_cbc,16)
  211. || !EVP_CIPHER_meth_set_flags(_hidden_aes_128_cbc,
  212. EVP_CIPH_FLAG_DEFAULT_ASN1
  213. | EVP_CIPH_CBC_MODE
  214. | EVP_CIPH_FLAG_PIPELINE)
  215. || !EVP_CIPHER_meth_set_init(_hidden_aes_128_cbc,
  216. dasync_aes128_init_key)
  217. || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_cbc,
  218. dasync_aes128_cbc_cipher)
  219. || !EVP_CIPHER_meth_set_cleanup(_hidden_aes_128_cbc,
  220. dasync_aes128_cbc_cleanup)
  221. || !EVP_CIPHER_meth_set_ctrl(_hidden_aes_128_cbc,
  222. dasync_aes128_cbc_ctrl)
  223. || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_cbc,
  224. sizeof(struct dasync_pipeline_ctx))) {
  225. EVP_CIPHER_meth_free(_hidden_aes_128_cbc);
  226. _hidden_aes_128_cbc = NULL;
  227. }
  228. _hidden_aes_128_cbc_hmac_sha1 = EVP_CIPHER_meth_new(
  229. NID_aes_128_cbc_hmac_sha1,
  230. 16 /* block size */,
  231. 16 /* key len */);
  232. if (_hidden_aes_128_cbc_hmac_sha1 == NULL
  233. || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_128_cbc_hmac_sha1,16)
  234. || !EVP_CIPHER_meth_set_flags(_hidden_aes_128_cbc_hmac_sha1,
  235. EVP_CIPH_CBC_MODE
  236. | EVP_CIPH_FLAG_DEFAULT_ASN1
  237. | EVP_CIPH_FLAG_AEAD_CIPHER
  238. | EVP_CIPH_FLAG_PIPELINE)
  239. || !EVP_CIPHER_meth_set_init(_hidden_aes_128_cbc_hmac_sha1,
  240. dasync_aes128_cbc_hmac_sha1_init_key)
  241. || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_cbc_hmac_sha1,
  242. dasync_aes128_cbc_hmac_sha1_cipher)
  243. || !EVP_CIPHER_meth_set_cleanup(_hidden_aes_128_cbc_hmac_sha1,
  244. dasync_aes128_cbc_hmac_sha1_cleanup)
  245. || !EVP_CIPHER_meth_set_ctrl(_hidden_aes_128_cbc_hmac_sha1,
  246. dasync_aes128_cbc_hmac_sha1_ctrl)
  247. || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_cbc_hmac_sha1,
  248. sizeof(struct dasync_pipeline_ctx))) {
  249. EVP_CIPHER_meth_free(_hidden_aes_128_cbc_hmac_sha1);
  250. _hidden_aes_128_cbc_hmac_sha1 = NULL;
  251. }
  252. return 1;
  253. }
  254. # ifndef OPENSSL_NO_DYNAMIC_ENGINE
  255. static int bind_helper(ENGINE *e, const char *id)
  256. {
  257. if (id && (strcmp(id, engine_dasync_id) != 0))
  258. return 0;
  259. if (!bind_dasync(e))
  260. return 0;
  261. return 1;
  262. }
  263. IMPLEMENT_DYNAMIC_CHECK_FN()
  264. IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
  265. # endif
  266. static ENGINE *engine_dasync(void)
  267. {
  268. ENGINE *ret = ENGINE_new();
  269. if (!ret)
  270. return NULL;
  271. if (!bind_dasync(ret)) {
  272. ENGINE_free(ret);
  273. return NULL;
  274. }
  275. return ret;
  276. }
  277. void engine_load_dasync_int(void)
  278. {
  279. ENGINE *toadd = engine_dasync();
  280. if (!toadd)
  281. return;
  282. ENGINE_add(toadd);
  283. ENGINE_free(toadd);
  284. ERR_clear_error();
  285. }
  286. static int dasync_init(ENGINE *e)
  287. {
  288. return 1;
  289. }
  290. static int dasync_finish(ENGINE *e)
  291. {
  292. return 1;
  293. }
  294. static int dasync_destroy(ENGINE *e)
  295. {
  296. destroy_digests();
  297. destroy_ciphers();
  298. RSA_meth_free(dasync_rsa_method);
  299. ERR_unload_DASYNC_strings();
  300. return 1;
  301. }
  302. static int dasync_digests(ENGINE *e, const EVP_MD **digest,
  303. const int **nids, int nid)
  304. {
  305. int ok = 1;
  306. if (!digest) {
  307. /* We are returning a list of supported nids */
  308. return dasync_digest_nids(nids);
  309. }
  310. /* We are being asked for a specific digest */
  311. switch (nid) {
  312. case NID_sha1:
  313. *digest = dasync_sha1();
  314. break;
  315. default:
  316. ok = 0;
  317. *digest = NULL;
  318. break;
  319. }
  320. return ok;
  321. }
  322. static int dasync_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
  323. const int **nids, int nid)
  324. {
  325. int ok = 1;
  326. if (cipher == NULL) {
  327. /* We are returning a list of supported nids */
  328. *nids = dasync_cipher_nids;
  329. return (sizeof(dasync_cipher_nids) -
  330. 1) / sizeof(dasync_cipher_nids[0]);
  331. }
  332. /* We are being asked for a specific cipher */
  333. switch (nid) {
  334. case NID_aes_128_cbc:
  335. *cipher = dasync_aes_128_cbc();
  336. break;
  337. case NID_aes_128_cbc_hmac_sha1:
  338. *cipher = dasync_aes_128_cbc_hmac_sha1();
  339. break;
  340. default:
  341. ok = 0;
  342. *cipher = NULL;
  343. break;
  344. }
  345. return ok;
  346. }
  347. static void wait_cleanup(ASYNC_WAIT_CTX *ctx, const void *key,
  348. OSSL_ASYNC_FD readfd, void *pvwritefd)
  349. {
  350. OSSL_ASYNC_FD *pwritefd = (OSSL_ASYNC_FD *)pvwritefd;
  351. #if defined(ASYNC_WIN)
  352. CloseHandle(readfd);
  353. CloseHandle(*pwritefd);
  354. #elif defined(ASYNC_POSIX)
  355. close(readfd);
  356. close(*pwritefd);
  357. #endif
  358. OPENSSL_free(pwritefd);
  359. }
  360. #define DUMMY_CHAR 'X'
  361. static void dummy_pause_job(void) {
  362. ASYNC_JOB *job;
  363. ASYNC_WAIT_CTX *waitctx;
  364. ASYNC_callback_fn callback;
  365. void * callback_arg;
  366. OSSL_ASYNC_FD pipefds[2] = {0, 0};
  367. OSSL_ASYNC_FD *writefd;
  368. #if defined(ASYNC_WIN)
  369. DWORD numwritten, numread;
  370. char buf = DUMMY_CHAR;
  371. #elif defined(ASYNC_POSIX)
  372. char buf = DUMMY_CHAR;
  373. #endif
  374. if ((job = ASYNC_get_current_job()) == NULL)
  375. return;
  376. waitctx = ASYNC_get_wait_ctx(job);
  377. if (ASYNC_WAIT_CTX_get_callback(waitctx, &callback, &callback_arg) && callback != NULL) {
  378. /*
  379. * In the Dummy async engine we are cheating. We call the callback that the job
  380. * is complete before the call to ASYNC_pause_job(). A real
  381. * async engine would only call the callback when the job was actually complete
  382. */
  383. (*callback)(callback_arg);
  384. ASYNC_pause_job();
  385. return;
  386. }
  387. if (ASYNC_WAIT_CTX_get_fd(waitctx, engine_dasync_id, &pipefds[0],
  388. (void **)&writefd)) {
  389. pipefds[1] = *writefd;
  390. } else {
  391. writefd = OPENSSL_malloc(sizeof(*writefd));
  392. if (writefd == NULL)
  393. return;
  394. #if defined(ASYNC_WIN)
  395. if (CreatePipe(&pipefds[0], &pipefds[1], NULL, 256) == 0) {
  396. OPENSSL_free(writefd);
  397. return;
  398. }
  399. #elif defined(ASYNC_POSIX)
  400. if (pipe(pipefds) != 0) {
  401. OPENSSL_free(writefd);
  402. return;
  403. }
  404. #endif
  405. *writefd = pipefds[1];
  406. if (!ASYNC_WAIT_CTX_set_wait_fd(waitctx, engine_dasync_id, pipefds[0],
  407. writefd, wait_cleanup)) {
  408. wait_cleanup(waitctx, engine_dasync_id, pipefds[0], writefd);
  409. return;
  410. }
  411. }
  412. /*
  413. * In the Dummy async engine we are cheating. We signal that the job
  414. * is complete by waking it before the call to ASYNC_pause_job(). A real
  415. * async engine would only wake when the job was actually complete
  416. */
  417. #if defined(ASYNC_WIN)
  418. WriteFile(pipefds[1], &buf, 1, &numwritten, NULL);
  419. #elif defined(ASYNC_POSIX)
  420. if (write(pipefds[1], &buf, 1) < 0)
  421. return;
  422. #endif
  423. /* Ignore errors - we carry on anyway */
  424. ASYNC_pause_job();
  425. /* Clear the wake signal */
  426. #if defined(ASYNC_WIN)
  427. ReadFile(pipefds[0], &buf, 1, &numread, NULL);
  428. #elif defined(ASYNC_POSIX)
  429. if (read(pipefds[0], &buf, 1) < 0)
  430. return;
  431. #endif
  432. }
  433. /*
  434. * SHA1 implementation. At the moment we just defer to the standard
  435. * implementation
  436. */
  437. #undef data
  438. #define data(ctx) ((SHA_CTX *)EVP_MD_CTX_md_data(ctx))
  439. static int dasync_sha1_init(EVP_MD_CTX *ctx)
  440. {
  441. dummy_pause_job();
  442. return SHA1_Init(data(ctx));
  443. }
  444. static int dasync_sha1_update(EVP_MD_CTX *ctx, const void *data,
  445. size_t count)
  446. {
  447. dummy_pause_job();
  448. return SHA1_Update(data(ctx), data, (size_t)count);
  449. }
  450. static int dasync_sha1_final(EVP_MD_CTX *ctx, unsigned char *md)
  451. {
  452. dummy_pause_job();
  453. return SHA1_Final(md, data(ctx));
  454. }
  455. /*
  456. * RSA implementation
  457. */
  458. static int dasync_pub_enc(int flen, const unsigned char *from,
  459. unsigned char *to, RSA *rsa, int padding) {
  460. /* Ignore errors - we carry on anyway */
  461. dummy_pause_job();
  462. return RSA_meth_get_pub_enc(RSA_PKCS1_OpenSSL())
  463. (flen, from, to, rsa, padding);
  464. }
  465. static int dasync_pub_dec(int flen, const unsigned char *from,
  466. unsigned char *to, RSA *rsa, int padding) {
  467. /* Ignore errors - we carry on anyway */
  468. dummy_pause_job();
  469. return RSA_meth_get_pub_dec(RSA_PKCS1_OpenSSL())
  470. (flen, from, to, rsa, padding);
  471. }
  472. static int dasync_rsa_priv_enc(int flen, const unsigned char *from,
  473. unsigned char *to, RSA *rsa, int padding)
  474. {
  475. /* Ignore errors - we carry on anyway */
  476. dummy_pause_job();
  477. return RSA_meth_get_priv_enc(RSA_PKCS1_OpenSSL())
  478. (flen, from, to, rsa, padding);
  479. }
  480. static int dasync_rsa_priv_dec(int flen, const unsigned char *from,
  481. unsigned char *to, RSA *rsa, int padding)
  482. {
  483. /* Ignore errors - we carry on anyway */
  484. dummy_pause_job();
  485. return RSA_meth_get_priv_dec(RSA_PKCS1_OpenSSL())
  486. (flen, from, to, rsa, padding);
  487. }
  488. static int dasync_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
  489. {
  490. /* Ignore errors - we carry on anyway */
  491. dummy_pause_job();
  492. return RSA_meth_get_mod_exp(RSA_PKCS1_OpenSSL())(r0, I, rsa, ctx);
  493. }
  494. static int dasync_rsa_init(RSA *rsa)
  495. {
  496. return RSA_meth_get_init(RSA_PKCS1_OpenSSL())(rsa);
  497. }
  498. static int dasync_rsa_finish(RSA *rsa)
  499. {
  500. return RSA_meth_get_finish(RSA_PKCS1_OpenSSL())(rsa);
  501. }
  502. /* Cipher helper functions */
  503. static int dasync_cipher_ctrl_helper(EVP_CIPHER_CTX *ctx, int type, int arg,
  504. void *ptr, int aeadcapable)
  505. {
  506. int ret;
  507. struct dasync_pipeline_ctx *pipe_ctx =
  508. (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
  509. if (pipe_ctx == NULL)
  510. return 0;
  511. switch (type) {
  512. case EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS:
  513. pipe_ctx->numpipes = arg;
  514. pipe_ctx->outbufs = (unsigned char **)ptr;
  515. break;
  516. case EVP_CTRL_SET_PIPELINE_INPUT_BUFS:
  517. pipe_ctx->numpipes = arg;
  518. pipe_ctx->inbufs = (unsigned char **)ptr;
  519. break;
  520. case EVP_CTRL_SET_PIPELINE_INPUT_LENS:
  521. pipe_ctx->numpipes = arg;
  522. pipe_ctx->lens = (size_t *)ptr;
  523. break;
  524. case EVP_CTRL_AEAD_SET_MAC_KEY:
  525. if (!aeadcapable)
  526. return -1;
  527. EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
  528. ret = EVP_CIPHER_meth_get_ctrl(EVP_aes_128_cbc_hmac_sha1())
  529. (ctx, type, arg, ptr);
  530. EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
  531. return ret;
  532. case EVP_CTRL_AEAD_TLS1_AAD:
  533. {
  534. unsigned char *p = ptr;
  535. unsigned int len;
  536. if (!aeadcapable || arg != EVP_AEAD_TLS1_AAD_LEN)
  537. return -1;
  538. if (pipe_ctx->aadctr >= SSL_MAX_PIPELINES)
  539. return -1;
  540. memcpy(pipe_ctx->tlsaad[pipe_ctx->aadctr], ptr,
  541. EVP_AEAD_TLS1_AAD_LEN);
  542. pipe_ctx->aadctr++;
  543. len = p[arg - 2] << 8 | p[arg - 1];
  544. if (EVP_CIPHER_CTX_encrypting(ctx)) {
  545. if ((p[arg - 4] << 8 | p[arg - 3]) >= TLS1_1_VERSION) {
  546. if (len < AES_BLOCK_SIZE)
  547. return 0;
  548. len -= AES_BLOCK_SIZE;
  549. }
  550. return ((len + SHA_DIGEST_LENGTH + AES_BLOCK_SIZE)
  551. & -AES_BLOCK_SIZE) - len;
  552. } else {
  553. return SHA_DIGEST_LENGTH;
  554. }
  555. }
  556. default:
  557. return 0;
  558. }
  559. return 1;
  560. }
  561. static int dasync_cipher_init_key_helper(EVP_CIPHER_CTX *ctx,
  562. const unsigned char *key,
  563. const unsigned char *iv, int enc,
  564. const EVP_CIPHER *cipher)
  565. {
  566. int ret;
  567. struct dasync_pipeline_ctx *pipe_ctx =
  568. (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
  569. if (pipe_ctx->inner_cipher_data == NULL
  570. && EVP_CIPHER_impl_ctx_size(cipher) != 0) {
  571. pipe_ctx->inner_cipher_data = OPENSSL_zalloc(
  572. EVP_CIPHER_impl_ctx_size(cipher));
  573. if (pipe_ctx->inner_cipher_data == NULL) {
  574. DASYNCerr(DASYNC_F_DASYNC_CIPHER_INIT_KEY_HELPER,
  575. ERR_R_MALLOC_FAILURE);
  576. return 0;
  577. }
  578. }
  579. pipe_ctx->numpipes = 0;
  580. pipe_ctx->aadctr = 0;
  581. EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
  582. ret = EVP_CIPHER_meth_get_init(cipher)(ctx, key, iv, enc);
  583. EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
  584. return ret;
  585. }
  586. static int dasync_cipher_helper(EVP_CIPHER_CTX *ctx, unsigned char *out,
  587. const unsigned char *in, size_t inl,
  588. const EVP_CIPHER *cipher)
  589. {
  590. int ret = 1;
  591. unsigned int i, pipes;
  592. struct dasync_pipeline_ctx *pipe_ctx =
  593. (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
  594. pipes = pipe_ctx->numpipes;
  595. EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
  596. if (pipes == 0) {
  597. if (pipe_ctx->aadctr != 0) {
  598. if (pipe_ctx->aadctr != 1)
  599. return -1;
  600. EVP_CIPHER_meth_get_ctrl(cipher)
  601. (ctx, EVP_CTRL_AEAD_TLS1_AAD,
  602. EVP_AEAD_TLS1_AAD_LEN,
  603. pipe_ctx->tlsaad[0]);
  604. }
  605. ret = EVP_CIPHER_meth_get_do_cipher(cipher)
  606. (ctx, out, in, inl);
  607. } else {
  608. if (pipe_ctx->aadctr > 0 && pipe_ctx->aadctr != pipes)
  609. return -1;
  610. for (i = 0; i < pipes; i++) {
  611. if (pipe_ctx->aadctr > 0) {
  612. EVP_CIPHER_meth_get_ctrl(cipher)
  613. (ctx, EVP_CTRL_AEAD_TLS1_AAD,
  614. EVP_AEAD_TLS1_AAD_LEN,
  615. pipe_ctx->tlsaad[i]);
  616. }
  617. ret = ret && EVP_CIPHER_meth_get_do_cipher(cipher)
  618. (ctx, pipe_ctx->outbufs[i], pipe_ctx->inbufs[i],
  619. pipe_ctx->lens[i]);
  620. }
  621. pipe_ctx->numpipes = 0;
  622. }
  623. pipe_ctx->aadctr = 0;
  624. EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
  625. return ret;
  626. }
  627. static int dasync_cipher_cleanup_helper(EVP_CIPHER_CTX *ctx,
  628. const EVP_CIPHER *cipher)
  629. {
  630. struct dasync_pipeline_ctx *pipe_ctx =
  631. (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
  632. OPENSSL_clear_free(pipe_ctx->inner_cipher_data,
  633. EVP_CIPHER_impl_ctx_size(cipher));
  634. return 1;
  635. }
  636. /*
  637. * AES128 CBC Implementation
  638. */
  639. static int dasync_aes128_cbc_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
  640. void *ptr)
  641. {
  642. return dasync_cipher_ctrl_helper(ctx, type, arg, ptr, 0);
  643. }
  644. static int dasync_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  645. const unsigned char *iv, int enc)
  646. {
  647. return dasync_cipher_init_key_helper(ctx, key, iv, enc, EVP_aes_128_cbc());
  648. }
  649. static int dasync_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  650. const unsigned char *in, size_t inl)
  651. {
  652. return dasync_cipher_helper(ctx, out, in, inl, EVP_aes_128_cbc());
  653. }
  654. static int dasync_aes128_cbc_cleanup(EVP_CIPHER_CTX *ctx)
  655. {
  656. return dasync_cipher_cleanup_helper(ctx, EVP_aes_128_cbc());
  657. }
  658. /*
  659. * AES128 CBC HMAC SHA1 Implementation
  660. */
  661. static int dasync_aes128_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX *ctx, int type,
  662. int arg, void *ptr)
  663. {
  664. return dasync_cipher_ctrl_helper(ctx, type, arg, ptr, 1);
  665. }
  666. static int dasync_aes128_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX *ctx,
  667. const unsigned char *key,
  668. const unsigned char *iv,
  669. int enc)
  670. {
  671. /*
  672. * We can safely assume that EVP_aes_128_cbc_hmac_sha1() != NULL,
  673. * see comment before the definition of dasync_aes_128_cbc_hmac_sha1().
  674. */
  675. return dasync_cipher_init_key_helper(ctx, key, iv, enc,
  676. EVP_aes_128_cbc_hmac_sha1());
  677. }
  678. static int dasync_aes128_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx,
  679. unsigned char *out,
  680. const unsigned char *in,
  681. size_t inl)
  682. {
  683. return dasync_cipher_helper(ctx, out, in, inl, EVP_aes_128_cbc_hmac_sha1());
  684. }
  685. static int dasync_aes128_cbc_hmac_sha1_cleanup(EVP_CIPHER_CTX *ctx)
  686. {
  687. /*
  688. * We can safely assume that EVP_aes_128_cbc_hmac_sha1() != NULL,
  689. * see comment before the definition of dasync_aes_128_cbc_hmac_sha1().
  690. */
  691. return dasync_cipher_cleanup_helper(ctx, EVP_aes_128_cbc_hmac_sha1());
  692. }