e_dasync.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  1. /*
  2. * Copyright 2015-2021 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_hmac_sha1,
  158. NID_aes_128_cbc,
  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_CIPH_CUSTOM_COPY)
  216. || !EVP_CIPHER_meth_set_init(_hidden_aes_128_cbc,
  217. dasync_aes128_init_key)
  218. || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_cbc,
  219. dasync_aes128_cbc_cipher)
  220. || !EVP_CIPHER_meth_set_cleanup(_hidden_aes_128_cbc,
  221. dasync_aes128_cbc_cleanup)
  222. || !EVP_CIPHER_meth_set_ctrl(_hidden_aes_128_cbc,
  223. dasync_aes128_cbc_ctrl)
  224. || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_cbc,
  225. sizeof(struct dasync_pipeline_ctx))) {
  226. EVP_CIPHER_meth_free(_hidden_aes_128_cbc);
  227. _hidden_aes_128_cbc = NULL;
  228. }
  229. _hidden_aes_128_cbc_hmac_sha1 = EVP_CIPHER_meth_new(
  230. NID_aes_128_cbc_hmac_sha1,
  231. 16 /* block size */,
  232. 16 /* key len */);
  233. if (_hidden_aes_128_cbc_hmac_sha1 == NULL
  234. || EVP_aes_128_cbc_hmac_sha1() == NULL
  235. || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_128_cbc_hmac_sha1,16)
  236. || !EVP_CIPHER_meth_set_flags(_hidden_aes_128_cbc_hmac_sha1,
  237. EVP_CIPH_CBC_MODE
  238. | EVP_CIPH_FLAG_DEFAULT_ASN1
  239. | EVP_CIPH_FLAG_AEAD_CIPHER
  240. | EVP_CIPH_FLAG_PIPELINE
  241. | EVP_CIPH_CUSTOM_COPY)
  242. || !EVP_CIPHER_meth_set_init(_hidden_aes_128_cbc_hmac_sha1,
  243. dasync_aes128_cbc_hmac_sha1_init_key)
  244. || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_cbc_hmac_sha1,
  245. dasync_aes128_cbc_hmac_sha1_cipher)
  246. || !EVP_CIPHER_meth_set_cleanup(_hidden_aes_128_cbc_hmac_sha1,
  247. dasync_aes128_cbc_hmac_sha1_cleanup)
  248. || !EVP_CIPHER_meth_set_ctrl(_hidden_aes_128_cbc_hmac_sha1,
  249. dasync_aes128_cbc_hmac_sha1_ctrl)
  250. || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_cbc_hmac_sha1,
  251. sizeof(struct dasync_pipeline_ctx))) {
  252. EVP_CIPHER_meth_free(_hidden_aes_128_cbc_hmac_sha1);
  253. _hidden_aes_128_cbc_hmac_sha1 = NULL;
  254. }
  255. return 1;
  256. }
  257. # ifndef OPENSSL_NO_DYNAMIC_ENGINE
  258. static int bind_helper(ENGINE *e, const char *id)
  259. {
  260. if (id && (strcmp(id, engine_dasync_id) != 0))
  261. return 0;
  262. if (!bind_dasync(e))
  263. return 0;
  264. return 1;
  265. }
  266. IMPLEMENT_DYNAMIC_CHECK_FN()
  267. IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
  268. # endif
  269. static ENGINE *engine_dasync(void)
  270. {
  271. ENGINE *ret = ENGINE_new();
  272. if (!ret)
  273. return NULL;
  274. if (!bind_dasync(ret)) {
  275. ENGINE_free(ret);
  276. return NULL;
  277. }
  278. return ret;
  279. }
  280. void engine_load_dasync_int(void)
  281. {
  282. ENGINE *toadd = engine_dasync();
  283. if (!toadd)
  284. return;
  285. ENGINE_add(toadd);
  286. ENGINE_free(toadd);
  287. ERR_clear_error();
  288. }
  289. static int dasync_init(ENGINE *e)
  290. {
  291. return 1;
  292. }
  293. static int dasync_finish(ENGINE *e)
  294. {
  295. return 1;
  296. }
  297. static int dasync_destroy(ENGINE *e)
  298. {
  299. destroy_digests();
  300. destroy_ciphers();
  301. RSA_meth_free(dasync_rsa_method);
  302. ERR_unload_DASYNC_strings();
  303. return 1;
  304. }
  305. static int dasync_digests(ENGINE *e, const EVP_MD **digest,
  306. const int **nids, int nid)
  307. {
  308. int ok = 1;
  309. if (!digest) {
  310. /* We are returning a list of supported nids */
  311. return dasync_digest_nids(nids);
  312. }
  313. /* We are being asked for a specific digest */
  314. switch (nid) {
  315. case NID_sha1:
  316. *digest = dasync_sha1();
  317. break;
  318. default:
  319. ok = 0;
  320. *digest = NULL;
  321. break;
  322. }
  323. return ok;
  324. }
  325. static int dasync_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
  326. const int **nids, int nid)
  327. {
  328. int ok = 1;
  329. if (cipher == NULL) {
  330. /* We are returning a list of supported nids */
  331. if (dasync_aes_128_cbc_hmac_sha1() == NULL) {
  332. *nids = dasync_cipher_nids + 1;
  333. return 1;
  334. }
  335. *nids = dasync_cipher_nids;
  336. return (sizeof(dasync_cipher_nids) -
  337. 1) / sizeof(dasync_cipher_nids[0]);
  338. }
  339. /* We are being asked for a specific cipher */
  340. switch (nid) {
  341. case NID_aes_128_cbc:
  342. *cipher = dasync_aes_128_cbc();
  343. break;
  344. case NID_aes_128_cbc_hmac_sha1:
  345. *cipher = dasync_aes_128_cbc_hmac_sha1();
  346. break;
  347. default:
  348. ok = 0;
  349. *cipher = NULL;
  350. break;
  351. }
  352. return ok;
  353. }
  354. static void wait_cleanup(ASYNC_WAIT_CTX *ctx, const void *key,
  355. OSSL_ASYNC_FD readfd, void *pvwritefd)
  356. {
  357. OSSL_ASYNC_FD *pwritefd = (OSSL_ASYNC_FD *)pvwritefd;
  358. #if defined(ASYNC_WIN)
  359. CloseHandle(readfd);
  360. CloseHandle(*pwritefd);
  361. #elif defined(ASYNC_POSIX)
  362. close(readfd);
  363. close(*pwritefd);
  364. #endif
  365. OPENSSL_free(pwritefd);
  366. }
  367. #define DUMMY_CHAR 'X'
  368. static void dummy_pause_job(void) {
  369. ASYNC_JOB *job;
  370. ASYNC_WAIT_CTX *waitctx;
  371. OSSL_ASYNC_FD pipefds[2] = {0, 0};
  372. OSSL_ASYNC_FD *writefd;
  373. #if defined(ASYNC_WIN)
  374. DWORD numwritten, numread;
  375. char buf = DUMMY_CHAR;
  376. #elif defined(ASYNC_POSIX)
  377. char buf = DUMMY_CHAR;
  378. #endif
  379. if ((job = ASYNC_get_current_job()) == NULL)
  380. return;
  381. waitctx = ASYNC_get_wait_ctx(job);
  382. if (ASYNC_WAIT_CTX_get_fd(waitctx, engine_dasync_id, &pipefds[0],
  383. (void **)&writefd)) {
  384. pipefds[1] = *writefd;
  385. } else {
  386. writefd = OPENSSL_malloc(sizeof(*writefd));
  387. if (writefd == NULL)
  388. return;
  389. #if defined(ASYNC_WIN)
  390. if (CreatePipe(&pipefds[0], &pipefds[1], NULL, 256) == 0) {
  391. OPENSSL_free(writefd);
  392. return;
  393. }
  394. #elif defined(ASYNC_POSIX)
  395. if (pipe(pipefds) != 0) {
  396. OPENSSL_free(writefd);
  397. return;
  398. }
  399. #endif
  400. *writefd = pipefds[1];
  401. if (!ASYNC_WAIT_CTX_set_wait_fd(waitctx, engine_dasync_id, pipefds[0],
  402. writefd, wait_cleanup)) {
  403. wait_cleanup(waitctx, engine_dasync_id, pipefds[0], writefd);
  404. return;
  405. }
  406. }
  407. /*
  408. * In the Dummy async engine we are cheating. We signal that the job
  409. * is complete by waking it before the call to ASYNC_pause_job(). A real
  410. * async engine would only wake when the job was actually complete
  411. */
  412. #if defined(ASYNC_WIN)
  413. WriteFile(pipefds[1], &buf, 1, &numwritten, NULL);
  414. #elif defined(ASYNC_POSIX)
  415. if (write(pipefds[1], &buf, 1) < 0)
  416. return;
  417. #endif
  418. /* Ignore errors - we carry on anyway */
  419. ASYNC_pause_job();
  420. /* Clear the wake signal */
  421. #if defined(ASYNC_WIN)
  422. ReadFile(pipefds[0], &buf, 1, &numread, NULL);
  423. #elif defined(ASYNC_POSIX)
  424. if (read(pipefds[0], &buf, 1) < 0)
  425. return;
  426. #endif
  427. }
  428. /*
  429. * SHA1 implementation. At the moment we just defer to the standard
  430. * implementation
  431. */
  432. #undef data
  433. #define data(ctx) ((SHA_CTX *)EVP_MD_CTX_md_data(ctx))
  434. static int dasync_sha1_init(EVP_MD_CTX *ctx)
  435. {
  436. dummy_pause_job();
  437. return SHA1_Init(data(ctx));
  438. }
  439. static int dasync_sha1_update(EVP_MD_CTX *ctx, const void *data,
  440. size_t count)
  441. {
  442. dummy_pause_job();
  443. return SHA1_Update(data(ctx), data, (size_t)count);
  444. }
  445. static int dasync_sha1_final(EVP_MD_CTX *ctx, unsigned char *md)
  446. {
  447. dummy_pause_job();
  448. return SHA1_Final(md, data(ctx));
  449. }
  450. /*
  451. * RSA implementation
  452. */
  453. static int dasync_pub_enc(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_enc(RSA_PKCS1_OpenSSL())
  458. (flen, from, to, rsa, padding);
  459. }
  460. static int dasync_pub_dec(int flen, const unsigned char *from,
  461. unsigned char *to, RSA *rsa, int padding) {
  462. /* Ignore errors - we carry on anyway */
  463. dummy_pause_job();
  464. return RSA_meth_get_pub_dec(RSA_PKCS1_OpenSSL())
  465. (flen, from, to, rsa, padding);
  466. }
  467. static int dasync_rsa_priv_enc(int flen, const unsigned char *from,
  468. unsigned char *to, RSA *rsa, int padding)
  469. {
  470. /* Ignore errors - we carry on anyway */
  471. dummy_pause_job();
  472. return RSA_meth_get_priv_enc(RSA_PKCS1_OpenSSL())
  473. (flen, from, to, rsa, padding);
  474. }
  475. static int dasync_rsa_priv_dec(int flen, const unsigned char *from,
  476. unsigned char *to, RSA *rsa, int padding)
  477. {
  478. /* Ignore errors - we carry on anyway */
  479. dummy_pause_job();
  480. return RSA_meth_get_priv_dec(RSA_PKCS1_OpenSSL())
  481. (flen, from, to, rsa, padding);
  482. }
  483. static int dasync_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
  484. {
  485. /* Ignore errors - we carry on anyway */
  486. dummy_pause_job();
  487. return RSA_meth_get_mod_exp(RSA_PKCS1_OpenSSL())(r0, I, rsa, ctx);
  488. }
  489. static int dasync_rsa_init(RSA *rsa)
  490. {
  491. return RSA_meth_get_init(RSA_PKCS1_OpenSSL())(rsa);
  492. }
  493. static int dasync_rsa_finish(RSA *rsa)
  494. {
  495. return RSA_meth_get_finish(RSA_PKCS1_OpenSSL())(rsa);
  496. }
  497. /* Cipher helper functions */
  498. static int dasync_cipher_ctrl_helper(EVP_CIPHER_CTX *ctx, int type, int arg,
  499. void *ptr, int aeadcapable)
  500. {
  501. int ret;
  502. struct dasync_pipeline_ctx *pipe_ctx =
  503. (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
  504. if (pipe_ctx == NULL)
  505. return 0;
  506. switch (type) {
  507. case EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS:
  508. pipe_ctx->numpipes = arg;
  509. pipe_ctx->outbufs = (unsigned char **)ptr;
  510. break;
  511. case EVP_CTRL_SET_PIPELINE_INPUT_BUFS:
  512. pipe_ctx->numpipes = arg;
  513. pipe_ctx->inbufs = (unsigned char **)ptr;
  514. break;
  515. case EVP_CTRL_SET_PIPELINE_INPUT_LENS:
  516. pipe_ctx->numpipes = arg;
  517. pipe_ctx->lens = (size_t *)ptr;
  518. break;
  519. case EVP_CTRL_AEAD_SET_MAC_KEY:
  520. if (!aeadcapable)
  521. return -1;
  522. EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
  523. ret = EVP_CIPHER_meth_get_ctrl(EVP_aes_128_cbc_hmac_sha1())
  524. (ctx, type, arg, ptr);
  525. EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
  526. return ret;
  527. case EVP_CTRL_AEAD_TLS1_AAD:
  528. {
  529. unsigned char *p = ptr;
  530. unsigned int len;
  531. if (!aeadcapable || arg != EVP_AEAD_TLS1_AAD_LEN)
  532. return -1;
  533. if (pipe_ctx->aadctr >= SSL_MAX_PIPELINES)
  534. return -1;
  535. memcpy(pipe_ctx->tlsaad[pipe_ctx->aadctr], ptr,
  536. EVP_AEAD_TLS1_AAD_LEN);
  537. pipe_ctx->aadctr++;
  538. len = p[arg - 2] << 8 | p[arg - 1];
  539. if (EVP_CIPHER_CTX_encrypting(ctx)) {
  540. if ((p[arg - 4] << 8 | p[arg - 3]) >= TLS1_1_VERSION) {
  541. if (len < AES_BLOCK_SIZE)
  542. return 0;
  543. len -= AES_BLOCK_SIZE;
  544. }
  545. return ((len + SHA_DIGEST_LENGTH + AES_BLOCK_SIZE)
  546. & -AES_BLOCK_SIZE) - len;
  547. } else {
  548. return SHA_DIGEST_LENGTH;
  549. }
  550. }
  551. case EVP_CTRL_COPY:
  552. {
  553. const EVP_CIPHER *cipher = aeadcapable
  554. ? EVP_aes_128_cbc_hmac_sha1()
  555. : EVP_aes_128_cbc();
  556. size_t data_size = EVP_CIPHER_impl_ctx_size(cipher);
  557. void *cipher_data = OPENSSL_malloc(data_size);
  558. if (cipher_data == NULL)
  559. return 0;
  560. memcpy(cipher_data, pipe_ctx->inner_cipher_data, data_size);
  561. pipe_ctx->inner_cipher_data = cipher_data;
  562. return 1;
  563. }
  564. default:
  565. return 0;
  566. }
  567. return 1;
  568. }
  569. static int dasync_cipher_init_key_helper(EVP_CIPHER_CTX *ctx,
  570. const unsigned char *key,
  571. const unsigned char *iv, int enc,
  572. const EVP_CIPHER *cipher)
  573. {
  574. int ret;
  575. struct dasync_pipeline_ctx *pipe_ctx =
  576. (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
  577. if (pipe_ctx->inner_cipher_data == NULL
  578. && EVP_CIPHER_impl_ctx_size(cipher) != 0) {
  579. pipe_ctx->inner_cipher_data = OPENSSL_zalloc(
  580. EVP_CIPHER_impl_ctx_size(cipher));
  581. if (pipe_ctx->inner_cipher_data == NULL) {
  582. DASYNCerr(DASYNC_F_DASYNC_CIPHER_INIT_KEY_HELPER,
  583. ERR_R_MALLOC_FAILURE);
  584. return 0;
  585. }
  586. }
  587. pipe_ctx->numpipes = 0;
  588. pipe_ctx->aadctr = 0;
  589. EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
  590. ret = EVP_CIPHER_meth_get_init(cipher)(ctx, key, iv, enc);
  591. EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
  592. return ret;
  593. }
  594. static int dasync_cipher_helper(EVP_CIPHER_CTX *ctx, unsigned char *out,
  595. const unsigned char *in, size_t inl,
  596. const EVP_CIPHER *cipher)
  597. {
  598. int ret = 1;
  599. unsigned int i, pipes;
  600. struct dasync_pipeline_ctx *pipe_ctx =
  601. (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
  602. pipes = pipe_ctx->numpipes;
  603. EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
  604. if (pipes == 0) {
  605. if (pipe_ctx->aadctr != 0) {
  606. if (pipe_ctx->aadctr != 1)
  607. return -1;
  608. EVP_CIPHER_meth_get_ctrl(cipher)
  609. (ctx, EVP_CTRL_AEAD_TLS1_AAD,
  610. EVP_AEAD_TLS1_AAD_LEN,
  611. pipe_ctx->tlsaad[0]);
  612. }
  613. ret = EVP_CIPHER_meth_get_do_cipher(cipher)
  614. (ctx, out, in, inl);
  615. } else {
  616. if (pipe_ctx->aadctr > 0 && pipe_ctx->aadctr != pipes)
  617. return -1;
  618. for (i = 0; i < pipes; i++) {
  619. if (pipe_ctx->aadctr > 0) {
  620. EVP_CIPHER_meth_get_ctrl(cipher)
  621. (ctx, EVP_CTRL_AEAD_TLS1_AAD,
  622. EVP_AEAD_TLS1_AAD_LEN,
  623. pipe_ctx->tlsaad[i]);
  624. }
  625. ret = ret && EVP_CIPHER_meth_get_do_cipher(cipher)
  626. (ctx, pipe_ctx->outbufs[i], pipe_ctx->inbufs[i],
  627. pipe_ctx->lens[i]);
  628. }
  629. pipe_ctx->numpipes = 0;
  630. }
  631. pipe_ctx->aadctr = 0;
  632. EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
  633. return ret;
  634. }
  635. static int dasync_cipher_cleanup_helper(EVP_CIPHER_CTX *ctx,
  636. const EVP_CIPHER *cipher)
  637. {
  638. struct dasync_pipeline_ctx *pipe_ctx =
  639. (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
  640. OPENSSL_clear_free(pipe_ctx->inner_cipher_data,
  641. EVP_CIPHER_impl_ctx_size(cipher));
  642. return 1;
  643. }
  644. /*
  645. * AES128 CBC Implementation
  646. */
  647. static int dasync_aes128_cbc_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
  648. void *ptr)
  649. {
  650. return dasync_cipher_ctrl_helper(ctx, type, arg, ptr, 0);
  651. }
  652. static int dasync_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  653. const unsigned char *iv, int enc)
  654. {
  655. return dasync_cipher_init_key_helper(ctx, key, iv, enc, EVP_aes_128_cbc());
  656. }
  657. static int dasync_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  658. const unsigned char *in, size_t inl)
  659. {
  660. return dasync_cipher_helper(ctx, out, in, inl, EVP_aes_128_cbc());
  661. }
  662. static int dasync_aes128_cbc_cleanup(EVP_CIPHER_CTX *ctx)
  663. {
  664. return dasync_cipher_cleanup_helper(ctx, EVP_aes_128_cbc());
  665. }
  666. /*
  667. * AES128 CBC HMAC SHA1 Implementation
  668. */
  669. static int dasync_aes128_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX *ctx, int type,
  670. int arg, void *ptr)
  671. {
  672. return dasync_cipher_ctrl_helper(ctx, type, arg, ptr, 1);
  673. }
  674. static int dasync_aes128_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX *ctx,
  675. const unsigned char *key,
  676. const unsigned char *iv,
  677. int enc)
  678. {
  679. /*
  680. * We can safely assume that EVP_aes_128_cbc_hmac_sha1() != NULL,
  681. * see comment before the definition of dasync_aes_128_cbc_hmac_sha1().
  682. */
  683. return dasync_cipher_init_key_helper(ctx, key, iv, enc,
  684. EVP_aes_128_cbc_hmac_sha1());
  685. }
  686. static int dasync_aes128_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx,
  687. unsigned char *out,
  688. const unsigned char *in,
  689. size_t inl)
  690. {
  691. return dasync_cipher_helper(ctx, out, in, inl, EVP_aes_128_cbc_hmac_sha1());
  692. }
  693. static int dasync_aes128_cbc_hmac_sha1_cleanup(EVP_CIPHER_CTX *ctx)
  694. {
  695. /*
  696. * We can safely assume that EVP_aes_128_cbc_hmac_sha1() != NULL,
  697. * see comment before the definition of dasync_aes_128_cbc_hmac_sha1().
  698. */
  699. return dasync_cipher_cleanup_helper(ctx, EVP_aes_128_cbc_hmac_sha1());
  700. }