e_dasync.c 29 KB

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