e_dasync.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. /*
  2. * Copyright 2015-2019 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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. OSSL_ASYNC_FD pipefds[2] = {0, 0};
  365. OSSL_ASYNC_FD *writefd;
  366. #if defined(ASYNC_WIN)
  367. DWORD numwritten, numread;
  368. char buf = DUMMY_CHAR;
  369. #elif defined(ASYNC_POSIX)
  370. char buf = DUMMY_CHAR;
  371. #endif
  372. if ((job = ASYNC_get_current_job()) == NULL)
  373. return;
  374. waitctx = ASYNC_get_wait_ctx(job);
  375. if (ASYNC_WAIT_CTX_get_fd(waitctx, engine_dasync_id, &pipefds[0],
  376. (void **)&writefd)) {
  377. pipefds[1] = *writefd;
  378. } else {
  379. writefd = OPENSSL_malloc(sizeof(*writefd));
  380. if (writefd == NULL)
  381. return;
  382. #if defined(ASYNC_WIN)
  383. if (CreatePipe(&pipefds[0], &pipefds[1], NULL, 256) == 0) {
  384. OPENSSL_free(writefd);
  385. return;
  386. }
  387. #elif defined(ASYNC_POSIX)
  388. if (pipe(pipefds) != 0) {
  389. OPENSSL_free(writefd);
  390. return;
  391. }
  392. #endif
  393. *writefd = pipefds[1];
  394. if (!ASYNC_WAIT_CTX_set_wait_fd(waitctx, engine_dasync_id, pipefds[0],
  395. writefd, wait_cleanup)) {
  396. wait_cleanup(waitctx, engine_dasync_id, pipefds[0], writefd);
  397. return;
  398. }
  399. }
  400. /*
  401. * In the Dummy async engine we are cheating. We signal that the job
  402. * is complete by waking it before the call to ASYNC_pause_job(). A real
  403. * async engine would only wake when the job was actually complete
  404. */
  405. #if defined(ASYNC_WIN)
  406. WriteFile(pipefds[1], &buf, 1, &numwritten, NULL);
  407. #elif defined(ASYNC_POSIX)
  408. if (write(pipefds[1], &buf, 1) < 0)
  409. return;
  410. #endif
  411. /* Ignore errors - we carry on anyway */
  412. ASYNC_pause_job();
  413. /* Clear the wake signal */
  414. #if defined(ASYNC_WIN)
  415. ReadFile(pipefds[0], &buf, 1, &numread, NULL);
  416. #elif defined(ASYNC_POSIX)
  417. if (read(pipefds[0], &buf, 1) < 0)
  418. return;
  419. #endif
  420. }
  421. /*
  422. * SHA1 implementation. At the moment we just defer to the standard
  423. * implementation
  424. */
  425. #undef data
  426. #define data(ctx) ((SHA_CTX *)EVP_MD_CTX_md_data(ctx))
  427. static int dasync_sha1_init(EVP_MD_CTX *ctx)
  428. {
  429. dummy_pause_job();
  430. return SHA1_Init(data(ctx));
  431. }
  432. static int dasync_sha1_update(EVP_MD_CTX *ctx, const void *data,
  433. size_t count)
  434. {
  435. dummy_pause_job();
  436. return SHA1_Update(data(ctx), data, (size_t)count);
  437. }
  438. static int dasync_sha1_final(EVP_MD_CTX *ctx, unsigned char *md)
  439. {
  440. dummy_pause_job();
  441. return SHA1_Final(md, data(ctx));
  442. }
  443. /*
  444. * RSA implementation
  445. */
  446. static int dasync_pub_enc(int flen, const unsigned char *from,
  447. unsigned char *to, RSA *rsa, int padding) {
  448. /* Ignore errors - we carry on anyway */
  449. dummy_pause_job();
  450. return RSA_meth_get_pub_enc(RSA_PKCS1_OpenSSL())
  451. (flen, from, to, rsa, padding);
  452. }
  453. static int dasync_pub_dec(int flen, const unsigned char *from,
  454. unsigned char *to, RSA *rsa, int padding) {
  455. /* Ignore errors - we carry on anyway */
  456. dummy_pause_job();
  457. return RSA_meth_get_pub_dec(RSA_PKCS1_OpenSSL())
  458. (flen, from, to, rsa, padding);
  459. }
  460. static int dasync_rsa_priv_enc(int flen, const unsigned char *from,
  461. unsigned char *to, RSA *rsa, int padding)
  462. {
  463. /* Ignore errors - we carry on anyway */
  464. dummy_pause_job();
  465. return RSA_meth_get_priv_enc(RSA_PKCS1_OpenSSL())
  466. (flen, from, to, rsa, padding);
  467. }
  468. static int dasync_rsa_priv_dec(int flen, const unsigned char *from,
  469. unsigned char *to, RSA *rsa, int padding)
  470. {
  471. /* Ignore errors - we carry on anyway */
  472. dummy_pause_job();
  473. return RSA_meth_get_priv_dec(RSA_PKCS1_OpenSSL())
  474. (flen, from, to, rsa, padding);
  475. }
  476. static int dasync_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
  477. {
  478. /* Ignore errors - we carry on anyway */
  479. dummy_pause_job();
  480. return RSA_meth_get_mod_exp(RSA_PKCS1_OpenSSL())(r0, I, rsa, ctx);
  481. }
  482. static int dasync_rsa_init(RSA *rsa)
  483. {
  484. return RSA_meth_get_init(RSA_PKCS1_OpenSSL())(rsa);
  485. }
  486. static int dasync_rsa_finish(RSA *rsa)
  487. {
  488. return RSA_meth_get_finish(RSA_PKCS1_OpenSSL())(rsa);
  489. }
  490. /* Cipher helper functions */
  491. static int dasync_cipher_ctrl_helper(EVP_CIPHER_CTX *ctx, int type, int arg,
  492. void *ptr, int aeadcapable)
  493. {
  494. int ret;
  495. struct dasync_pipeline_ctx *pipe_ctx =
  496. (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
  497. if (pipe_ctx == NULL)
  498. return 0;
  499. switch (type) {
  500. case EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS:
  501. pipe_ctx->numpipes = arg;
  502. pipe_ctx->outbufs = (unsigned char **)ptr;
  503. break;
  504. case EVP_CTRL_SET_PIPELINE_INPUT_BUFS:
  505. pipe_ctx->numpipes = arg;
  506. pipe_ctx->inbufs = (unsigned char **)ptr;
  507. break;
  508. case EVP_CTRL_SET_PIPELINE_INPUT_LENS:
  509. pipe_ctx->numpipes = arg;
  510. pipe_ctx->lens = (size_t *)ptr;
  511. break;
  512. case EVP_CTRL_AEAD_SET_MAC_KEY:
  513. if (!aeadcapable)
  514. return -1;
  515. EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
  516. ret = EVP_CIPHER_meth_get_ctrl(EVP_aes_128_cbc_hmac_sha1())
  517. (ctx, type, arg, ptr);
  518. EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
  519. return ret;
  520. case EVP_CTRL_AEAD_TLS1_AAD:
  521. {
  522. unsigned char *p = ptr;
  523. unsigned int len;
  524. if (!aeadcapable || arg != EVP_AEAD_TLS1_AAD_LEN)
  525. return -1;
  526. if (pipe_ctx->aadctr >= SSL_MAX_PIPELINES)
  527. return -1;
  528. memcpy(pipe_ctx->tlsaad[pipe_ctx->aadctr], ptr,
  529. EVP_AEAD_TLS1_AAD_LEN);
  530. pipe_ctx->aadctr++;
  531. len = p[arg - 2] << 8 | p[arg - 1];
  532. if (EVP_CIPHER_CTX_encrypting(ctx)) {
  533. if ((p[arg - 4] << 8 | p[arg - 3]) >= TLS1_1_VERSION) {
  534. if (len < AES_BLOCK_SIZE)
  535. return 0;
  536. len -= AES_BLOCK_SIZE;
  537. }
  538. return ((len + SHA_DIGEST_LENGTH + AES_BLOCK_SIZE)
  539. & -AES_BLOCK_SIZE) - len;
  540. } else {
  541. return SHA_DIGEST_LENGTH;
  542. }
  543. }
  544. default:
  545. return 0;
  546. }
  547. return 1;
  548. }
  549. static int dasync_cipher_init_key_helper(EVP_CIPHER_CTX *ctx,
  550. const unsigned char *key,
  551. const unsigned char *iv, int enc,
  552. const EVP_CIPHER *cipher)
  553. {
  554. int ret;
  555. struct dasync_pipeline_ctx *pipe_ctx =
  556. (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
  557. if (pipe_ctx->inner_cipher_data == NULL
  558. && EVP_CIPHER_impl_ctx_size(cipher) != 0) {
  559. pipe_ctx->inner_cipher_data = OPENSSL_zalloc(
  560. EVP_CIPHER_impl_ctx_size(cipher));
  561. if (pipe_ctx->inner_cipher_data == NULL) {
  562. DASYNCerr(DASYNC_F_DASYNC_CIPHER_INIT_KEY_HELPER,
  563. ERR_R_MALLOC_FAILURE);
  564. return 0;
  565. }
  566. }
  567. pipe_ctx->numpipes = 0;
  568. pipe_ctx->aadctr = 0;
  569. EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
  570. ret = EVP_CIPHER_meth_get_init(cipher)(ctx, key, iv, enc);
  571. EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
  572. return ret;
  573. }
  574. static int dasync_cipher_helper(EVP_CIPHER_CTX *ctx, unsigned char *out,
  575. const unsigned char *in, size_t inl,
  576. const EVP_CIPHER *cipher)
  577. {
  578. int ret = 1;
  579. unsigned int i, pipes;
  580. struct dasync_pipeline_ctx *pipe_ctx =
  581. (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
  582. pipes = pipe_ctx->numpipes;
  583. EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
  584. if (pipes == 0) {
  585. if (pipe_ctx->aadctr != 0) {
  586. if (pipe_ctx->aadctr != 1)
  587. return -1;
  588. EVP_CIPHER_meth_get_ctrl(cipher)
  589. (ctx, EVP_CTRL_AEAD_TLS1_AAD,
  590. EVP_AEAD_TLS1_AAD_LEN,
  591. pipe_ctx->tlsaad[0]);
  592. }
  593. ret = EVP_CIPHER_meth_get_do_cipher(cipher)
  594. (ctx, out, in, inl);
  595. } else {
  596. if (pipe_ctx->aadctr > 0 && pipe_ctx->aadctr != pipes)
  597. return -1;
  598. for (i = 0; i < pipes; i++) {
  599. if (pipe_ctx->aadctr > 0) {
  600. EVP_CIPHER_meth_get_ctrl(cipher)
  601. (ctx, EVP_CTRL_AEAD_TLS1_AAD,
  602. EVP_AEAD_TLS1_AAD_LEN,
  603. pipe_ctx->tlsaad[i]);
  604. }
  605. ret = ret && EVP_CIPHER_meth_get_do_cipher(cipher)
  606. (ctx, pipe_ctx->outbufs[i], pipe_ctx->inbufs[i],
  607. pipe_ctx->lens[i]);
  608. }
  609. pipe_ctx->numpipes = 0;
  610. }
  611. pipe_ctx->aadctr = 0;
  612. EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
  613. return ret;
  614. }
  615. static int dasync_cipher_cleanup_helper(EVP_CIPHER_CTX *ctx,
  616. const EVP_CIPHER *cipher)
  617. {
  618. struct dasync_pipeline_ctx *pipe_ctx =
  619. (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
  620. OPENSSL_clear_free(pipe_ctx->inner_cipher_data,
  621. EVP_CIPHER_impl_ctx_size(cipher));
  622. return 1;
  623. }
  624. /*
  625. * AES128 CBC Implementation
  626. */
  627. static int dasync_aes128_cbc_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
  628. void *ptr)
  629. {
  630. return dasync_cipher_ctrl_helper(ctx, type, arg, ptr, 0);
  631. }
  632. static int dasync_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  633. const unsigned char *iv, int enc)
  634. {
  635. return dasync_cipher_init_key_helper(ctx, key, iv, enc, EVP_aes_128_cbc());
  636. }
  637. static int dasync_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  638. const unsigned char *in, size_t inl)
  639. {
  640. return dasync_cipher_helper(ctx, out, in, inl, EVP_aes_128_cbc());
  641. }
  642. static int dasync_aes128_cbc_cleanup(EVP_CIPHER_CTX *ctx)
  643. {
  644. return dasync_cipher_cleanup_helper(ctx, EVP_aes_128_cbc());
  645. }
  646. /*
  647. * AES128 CBC HMAC SHA1 Implementation
  648. */
  649. static int dasync_aes128_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX *ctx, int type,
  650. int arg, void *ptr)
  651. {
  652. return dasync_cipher_ctrl_helper(ctx, type, arg, ptr, 1);
  653. }
  654. static int dasync_aes128_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX *ctx,
  655. const unsigned char *key,
  656. const unsigned char *iv,
  657. int enc)
  658. {
  659. /*
  660. * We can safely assume that EVP_aes_128_cbc_hmac_sha1() != NULL,
  661. * see comment before the definition of dasync_aes_128_cbc_hmac_sha1().
  662. */
  663. return dasync_cipher_init_key_helper(ctx, key, iv, enc,
  664. EVP_aes_128_cbc_hmac_sha1());
  665. }
  666. static int dasync_aes128_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx,
  667. unsigned char *out,
  668. const unsigned char *in,
  669. size_t inl)
  670. {
  671. return dasync_cipher_helper(ctx, out, in, inl, EVP_aes_128_cbc_hmac_sha1());
  672. }
  673. static int dasync_aes128_cbc_hmac_sha1_cleanup(EVP_CIPHER_CTX *ctx)
  674. {
  675. /*
  676. * We can safely assume that EVP_aes_128_cbc_hmac_sha1() != NULL,
  677. * see comment before the definition of dasync_aes_128_cbc_hmac_sha1().
  678. */
  679. return dasync_cipher_cleanup_helper(ctx, EVP_aes_128_cbc_hmac_sha1());
  680. }