2
0

e_dasync.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  1. /*
  2. * Copyright 2015-2022 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_get_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. if ((dasync_rsa_orig = EVP_PKEY_meth_find(EVP_PKEY_RSA)) == NULL
  183. || (dasync_rsa = EVP_PKEY_meth_new(EVP_PKEY_RSA,
  184. EVP_PKEY_FLAG_AUTOARGLEN)) == 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_CIPH_CUSTOM_COPY)
  238. || !EVP_CIPHER_meth_set_init(_hidden_aes_128_cbc,
  239. dasync_aes128_init_key)
  240. || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_cbc,
  241. dasync_aes128_cbc_cipher)
  242. || !EVP_CIPHER_meth_set_cleanup(_hidden_aes_128_cbc,
  243. dasync_aes128_cbc_cleanup)
  244. || !EVP_CIPHER_meth_set_ctrl(_hidden_aes_128_cbc,
  245. dasync_aes128_cbc_ctrl)
  246. || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_cbc,
  247. sizeof(struct dasync_pipeline_ctx))) {
  248. EVP_CIPHER_meth_free(_hidden_aes_128_cbc);
  249. _hidden_aes_128_cbc = NULL;
  250. }
  251. _hidden_aes_128_cbc_hmac_sha1 = EVP_CIPHER_meth_new(
  252. NID_aes_128_cbc_hmac_sha1,
  253. 16 /* block size */,
  254. 16 /* key len */);
  255. if (_hidden_aes_128_cbc_hmac_sha1 == NULL
  256. || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_128_cbc_hmac_sha1,16)
  257. || !EVP_CIPHER_meth_set_flags(_hidden_aes_128_cbc_hmac_sha1,
  258. EVP_CIPH_CBC_MODE
  259. | EVP_CIPH_FLAG_DEFAULT_ASN1
  260. | EVP_CIPH_FLAG_AEAD_CIPHER
  261. | EVP_CIPH_FLAG_PIPELINE
  262. | EVP_CIPH_CUSTOM_COPY)
  263. || !EVP_CIPHER_meth_set_init(_hidden_aes_128_cbc_hmac_sha1,
  264. dasync_aes128_cbc_hmac_sha1_init_key)
  265. || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_cbc_hmac_sha1,
  266. dasync_aes128_cbc_hmac_sha1_cipher)
  267. || !EVP_CIPHER_meth_set_cleanup(_hidden_aes_128_cbc_hmac_sha1,
  268. dasync_aes128_cbc_hmac_sha1_cleanup)
  269. || !EVP_CIPHER_meth_set_ctrl(_hidden_aes_128_cbc_hmac_sha1,
  270. dasync_aes128_cbc_hmac_sha1_ctrl)
  271. || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_cbc_hmac_sha1,
  272. sizeof(struct dasync_pipeline_ctx))) {
  273. EVP_CIPHER_meth_free(_hidden_aes_128_cbc_hmac_sha1);
  274. _hidden_aes_128_cbc_hmac_sha1 = NULL;
  275. }
  276. return 1;
  277. }
  278. static void destroy_pkey(void)
  279. {
  280. /*
  281. * We don't actually need to free the dasync_rsa method since this is
  282. * automatically freed for us by libcrypto.
  283. */
  284. dasync_rsa_orig = NULL;
  285. dasync_rsa = NULL;
  286. }
  287. # ifndef OPENSSL_NO_DYNAMIC_ENGINE
  288. static int bind_helper(ENGINE *e, const char *id)
  289. {
  290. if (id && (strcmp(id, engine_dasync_id) != 0))
  291. return 0;
  292. if (!bind_dasync(e))
  293. return 0;
  294. return 1;
  295. }
  296. IMPLEMENT_DYNAMIC_CHECK_FN()
  297. IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
  298. # endif
  299. static ENGINE *engine_dasync(void)
  300. {
  301. ENGINE *ret = ENGINE_new();
  302. if (!ret)
  303. return NULL;
  304. if (!bind_dasync(ret)) {
  305. ENGINE_free(ret);
  306. return NULL;
  307. }
  308. return ret;
  309. }
  310. void engine_load_dasync_int(void)
  311. {
  312. ENGINE *toadd = engine_dasync();
  313. if (!toadd)
  314. return;
  315. ERR_set_mark();
  316. ENGINE_add(toadd);
  317. /*
  318. * If the "add" worked, it gets a structural reference. So either way, we
  319. * release our just-created reference.
  320. */
  321. ENGINE_free(toadd);
  322. /*
  323. * If the "add" didn't work, it was probably a conflict because it was
  324. * already added (eg. someone calling ENGINE_load_blah then calling
  325. * ENGINE_load_builtin_engines() perhaps).
  326. */
  327. ERR_pop_to_mark();
  328. }
  329. static int dasync_init(ENGINE *e)
  330. {
  331. return 1;
  332. }
  333. static int dasync_finish(ENGINE *e)
  334. {
  335. return 1;
  336. }
  337. static int dasync_destroy(ENGINE *e)
  338. {
  339. destroy_digests();
  340. destroy_ciphers();
  341. destroy_pkey();
  342. ERR_unload_DASYNC_strings();
  343. return 1;
  344. }
  345. static int dasync_pkey(ENGINE *e, EVP_PKEY_METHOD **pmeth,
  346. const int **pnids, int nid)
  347. {
  348. static const int rnid = EVP_PKEY_RSA;
  349. if (pmeth == NULL) {
  350. *pnids = &rnid;
  351. return 1;
  352. }
  353. if (nid == EVP_PKEY_RSA) {
  354. *pmeth = dasync_rsa;
  355. return 1;
  356. }
  357. *pmeth = NULL;
  358. return 0;
  359. }
  360. static int dasync_digests(ENGINE *e, const EVP_MD **digest,
  361. const int **nids, int nid)
  362. {
  363. int ok = 1;
  364. if (!digest) {
  365. /* We are returning a list of supported nids */
  366. return dasync_digest_nids(nids);
  367. }
  368. /* We are being asked for a specific digest */
  369. switch (nid) {
  370. case NID_sha1:
  371. *digest = dasync_sha1();
  372. break;
  373. default:
  374. ok = 0;
  375. *digest = NULL;
  376. break;
  377. }
  378. return ok;
  379. }
  380. static int dasync_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
  381. const int **nids, int nid)
  382. {
  383. int ok = 1;
  384. if (cipher == NULL) {
  385. /* We are returning a list of supported nids */
  386. *nids = dasync_cipher_nids;
  387. return (sizeof(dasync_cipher_nids) -
  388. 1) / sizeof(dasync_cipher_nids[0]);
  389. }
  390. /* We are being asked for a specific cipher */
  391. switch (nid) {
  392. case NID_aes_128_cbc:
  393. *cipher = dasync_aes_128_cbc();
  394. break;
  395. case NID_aes_128_cbc_hmac_sha1:
  396. *cipher = dasync_aes_128_cbc_hmac_sha1();
  397. break;
  398. default:
  399. ok = 0;
  400. *cipher = NULL;
  401. break;
  402. }
  403. return ok;
  404. }
  405. static void wait_cleanup(ASYNC_WAIT_CTX *ctx, const void *key,
  406. OSSL_ASYNC_FD readfd, void *pvwritefd)
  407. {
  408. OSSL_ASYNC_FD *pwritefd = (OSSL_ASYNC_FD *)pvwritefd;
  409. #if defined(ASYNC_WIN)
  410. CloseHandle(readfd);
  411. CloseHandle(*pwritefd);
  412. #elif defined(ASYNC_POSIX)
  413. close(readfd);
  414. close(*pwritefd);
  415. #endif
  416. OPENSSL_free(pwritefd);
  417. }
  418. #define DUMMY_CHAR 'X'
  419. static void dummy_pause_job(void) {
  420. ASYNC_JOB *job;
  421. ASYNC_WAIT_CTX *waitctx;
  422. ASYNC_callback_fn callback;
  423. void * callback_arg;
  424. OSSL_ASYNC_FD pipefds[2] = {0, 0};
  425. OSSL_ASYNC_FD *writefd;
  426. #if defined(ASYNC_WIN)
  427. DWORD numwritten, numread;
  428. char buf = DUMMY_CHAR;
  429. #elif defined(ASYNC_POSIX)
  430. char buf = DUMMY_CHAR;
  431. #endif
  432. if ((job = ASYNC_get_current_job()) == NULL)
  433. return;
  434. waitctx = ASYNC_get_wait_ctx(job);
  435. if (ASYNC_WAIT_CTX_get_callback(waitctx, &callback, &callback_arg) && callback != NULL) {
  436. /*
  437. * In the Dummy async engine we are cheating. We call the callback that the job
  438. * is complete before the call to ASYNC_pause_job(). A real
  439. * async engine would only call the callback when the job was actually complete
  440. */
  441. (*callback)(callback_arg);
  442. ASYNC_pause_job();
  443. return;
  444. }
  445. if (ASYNC_WAIT_CTX_get_fd(waitctx, engine_dasync_id, &pipefds[0],
  446. (void **)&writefd)) {
  447. pipefds[1] = *writefd;
  448. } else {
  449. writefd = OPENSSL_malloc(sizeof(*writefd));
  450. if (writefd == NULL)
  451. return;
  452. #if defined(ASYNC_WIN)
  453. if (CreatePipe(&pipefds[0], &pipefds[1], NULL, 256) == 0) {
  454. OPENSSL_free(writefd);
  455. return;
  456. }
  457. #elif defined(ASYNC_POSIX)
  458. if (pipe(pipefds) != 0) {
  459. OPENSSL_free(writefd);
  460. return;
  461. }
  462. #endif
  463. *writefd = pipefds[1];
  464. if (!ASYNC_WAIT_CTX_set_wait_fd(waitctx, engine_dasync_id, pipefds[0],
  465. writefd, wait_cleanup)) {
  466. wait_cleanup(waitctx, engine_dasync_id, pipefds[0], writefd);
  467. return;
  468. }
  469. }
  470. /*
  471. * In the Dummy async engine we are cheating. We signal that the job
  472. * is complete by waking it before the call to ASYNC_pause_job(). A real
  473. * async engine would only wake when the job was actually complete
  474. */
  475. #if defined(ASYNC_WIN)
  476. WriteFile(pipefds[1], &buf, 1, &numwritten, NULL);
  477. #elif defined(ASYNC_POSIX)
  478. if (write(pipefds[1], &buf, 1) < 0)
  479. return;
  480. #endif
  481. /* Ignore errors - we carry on anyway */
  482. ASYNC_pause_job();
  483. /* Clear the wake signal */
  484. #if defined(ASYNC_WIN)
  485. ReadFile(pipefds[0], &buf, 1, &numread, NULL);
  486. #elif defined(ASYNC_POSIX)
  487. if (read(pipefds[0], &buf, 1) < 0)
  488. return;
  489. #endif
  490. }
  491. /*
  492. * SHA1 implementation. At the moment we just defer to the standard
  493. * implementation
  494. */
  495. static int dasync_sha1_init(EVP_MD_CTX *ctx)
  496. {
  497. dummy_pause_job();
  498. return EVP_MD_meth_get_init(EVP_sha1())(ctx);
  499. }
  500. static int dasync_sha1_update(EVP_MD_CTX *ctx, const void *data,
  501. size_t count)
  502. {
  503. dummy_pause_job();
  504. return EVP_MD_meth_get_update(EVP_sha1())(ctx, data, count);
  505. }
  506. static int dasync_sha1_final(EVP_MD_CTX *ctx, unsigned char *md)
  507. {
  508. dummy_pause_job();
  509. return EVP_MD_meth_get_final(EVP_sha1())(ctx, md);
  510. }
  511. /* Cipher helper functions */
  512. static int dasync_cipher_ctrl_helper(EVP_CIPHER_CTX *ctx, int type, int arg,
  513. void *ptr, int aeadcapable,
  514. const EVP_CIPHER *ciph)
  515. {
  516. int ret;
  517. struct dasync_pipeline_ctx *pipe_ctx =
  518. (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
  519. if (pipe_ctx == NULL)
  520. return 0;
  521. switch (type) {
  522. case EVP_CTRL_COPY:
  523. {
  524. size_t sz = EVP_CIPHER_impl_ctx_size(ciph);
  525. void *inner_cipher_data = OPENSSL_malloc(sz);
  526. if (inner_cipher_data == NULL)
  527. return -1;
  528. memcpy(inner_cipher_data, pipe_ctx->inner_cipher_data, sz);
  529. pipe_ctx->inner_cipher_data = inner_cipher_data;
  530. }
  531. break;
  532. case EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS:
  533. pipe_ctx->numpipes = arg;
  534. pipe_ctx->outbufs = (unsigned char **)ptr;
  535. break;
  536. case EVP_CTRL_SET_PIPELINE_INPUT_BUFS:
  537. pipe_ctx->numpipes = arg;
  538. pipe_ctx->inbufs = (unsigned char **)ptr;
  539. break;
  540. case EVP_CTRL_SET_PIPELINE_INPUT_LENS:
  541. pipe_ctx->numpipes = arg;
  542. pipe_ctx->lens = (size_t *)ptr;
  543. break;
  544. case EVP_CTRL_AEAD_SET_MAC_KEY:
  545. if (!aeadcapable)
  546. return -1;
  547. EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
  548. ret = EVP_CIPHER_meth_get_ctrl(EVP_aes_128_cbc_hmac_sha1())
  549. (ctx, type, arg, ptr);
  550. EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
  551. return ret;
  552. case EVP_CTRL_AEAD_TLS1_AAD:
  553. {
  554. unsigned char *p = ptr;
  555. unsigned int len;
  556. if (!aeadcapable || arg != EVP_AEAD_TLS1_AAD_LEN)
  557. return -1;
  558. if (pipe_ctx->aadctr >= SSL_MAX_PIPELINES)
  559. return -1;
  560. memcpy(pipe_ctx->tlsaad[pipe_ctx->aadctr], ptr,
  561. EVP_AEAD_TLS1_AAD_LEN);
  562. pipe_ctx->aadctr++;
  563. len = p[arg - 2] << 8 | p[arg - 1];
  564. if (EVP_CIPHER_CTX_is_encrypting(ctx)) {
  565. if ((p[arg - 4] << 8 | p[arg - 3]) >= TLS1_1_VERSION) {
  566. if (len < AES_BLOCK_SIZE)
  567. return 0;
  568. len -= AES_BLOCK_SIZE;
  569. }
  570. return ((len + SHA_DIGEST_LENGTH + AES_BLOCK_SIZE)
  571. & -AES_BLOCK_SIZE) - len;
  572. } else {
  573. return SHA_DIGEST_LENGTH;
  574. }
  575. }
  576. default:
  577. return 0;
  578. }
  579. return 1;
  580. }
  581. static int dasync_cipher_init_key_helper(EVP_CIPHER_CTX *ctx,
  582. const unsigned char *key,
  583. const unsigned char *iv, int enc,
  584. const EVP_CIPHER *cipher)
  585. {
  586. int ret;
  587. struct dasync_pipeline_ctx *pipe_ctx =
  588. (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
  589. if (pipe_ctx->inner_cipher_data == NULL
  590. && EVP_CIPHER_impl_ctx_size(cipher) != 0) {
  591. pipe_ctx->inner_cipher_data = OPENSSL_zalloc(
  592. EVP_CIPHER_impl_ctx_size(cipher));
  593. if (pipe_ctx->inner_cipher_data == NULL) {
  594. DASYNCerr(DASYNC_F_DASYNC_CIPHER_INIT_KEY_HELPER,
  595. ERR_R_MALLOC_FAILURE);
  596. return 0;
  597. }
  598. }
  599. pipe_ctx->numpipes = 0;
  600. pipe_ctx->aadctr = 0;
  601. EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
  602. ret = EVP_CIPHER_meth_get_init(cipher)(ctx, key, iv, enc);
  603. EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
  604. return ret;
  605. }
  606. static int dasync_cipher_helper(EVP_CIPHER_CTX *ctx, unsigned char *out,
  607. const unsigned char *in, size_t inl,
  608. const EVP_CIPHER *cipher)
  609. {
  610. int ret = 1;
  611. unsigned int i, pipes;
  612. struct dasync_pipeline_ctx *pipe_ctx =
  613. (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
  614. pipes = pipe_ctx->numpipes;
  615. EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
  616. if (pipes == 0) {
  617. if (pipe_ctx->aadctr != 0) {
  618. if (pipe_ctx->aadctr != 1)
  619. return -1;
  620. EVP_CIPHER_meth_get_ctrl(cipher)
  621. (ctx, EVP_CTRL_AEAD_TLS1_AAD,
  622. EVP_AEAD_TLS1_AAD_LEN,
  623. pipe_ctx->tlsaad[0]);
  624. }
  625. ret = EVP_CIPHER_meth_get_do_cipher(cipher)
  626. (ctx, out, in, inl);
  627. } else {
  628. if (pipe_ctx->aadctr > 0 && pipe_ctx->aadctr != pipes)
  629. return -1;
  630. for (i = 0; i < pipes; i++) {
  631. if (pipe_ctx->aadctr > 0) {
  632. EVP_CIPHER_meth_get_ctrl(cipher)
  633. (ctx, EVP_CTRL_AEAD_TLS1_AAD,
  634. EVP_AEAD_TLS1_AAD_LEN,
  635. pipe_ctx->tlsaad[i]);
  636. }
  637. ret = ret && EVP_CIPHER_meth_get_do_cipher(cipher)
  638. (ctx, pipe_ctx->outbufs[i], pipe_ctx->inbufs[i],
  639. pipe_ctx->lens[i]);
  640. }
  641. pipe_ctx->numpipes = 0;
  642. }
  643. pipe_ctx->aadctr = 0;
  644. EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
  645. return ret;
  646. }
  647. static int dasync_cipher_cleanup_helper(EVP_CIPHER_CTX *ctx,
  648. const EVP_CIPHER *cipher)
  649. {
  650. struct dasync_pipeline_ctx *pipe_ctx =
  651. (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
  652. OPENSSL_clear_free(pipe_ctx->inner_cipher_data,
  653. EVP_CIPHER_impl_ctx_size(cipher));
  654. return 1;
  655. }
  656. /*
  657. * AES128 CBC Implementation
  658. */
  659. static int dasync_aes128_cbc_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
  660. void *ptr)
  661. {
  662. return dasync_cipher_ctrl_helper(ctx, type, arg, ptr, 0, EVP_aes_128_cbc());
  663. }
  664. static int dasync_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  665. const unsigned char *iv, int enc)
  666. {
  667. return dasync_cipher_init_key_helper(ctx, key, iv, enc, EVP_aes_128_cbc());
  668. }
  669. static int dasync_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  670. const unsigned char *in, size_t inl)
  671. {
  672. return dasync_cipher_helper(ctx, out, in, inl, EVP_aes_128_cbc());
  673. }
  674. static int dasync_aes128_cbc_cleanup(EVP_CIPHER_CTX *ctx)
  675. {
  676. return dasync_cipher_cleanup_helper(ctx, EVP_aes_128_cbc());
  677. }
  678. /*
  679. * AES128 CBC HMAC SHA1 Implementation
  680. */
  681. static int dasync_aes128_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX *ctx, int type,
  682. int arg, void *ptr)
  683. {
  684. return dasync_cipher_ctrl_helper(ctx, type, arg, ptr, 1, EVP_aes_128_cbc_hmac_sha1());
  685. }
  686. static int dasync_aes128_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX *ctx,
  687. const unsigned char *key,
  688. const unsigned char *iv,
  689. int enc)
  690. {
  691. /*
  692. * We can safely assume that EVP_aes_128_cbc_hmac_sha1() != NULL,
  693. * see comment before the definition of dasync_aes_128_cbc_hmac_sha1().
  694. */
  695. return dasync_cipher_init_key_helper(ctx, key, iv, enc,
  696. EVP_aes_128_cbc_hmac_sha1());
  697. }
  698. static int dasync_aes128_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx,
  699. unsigned char *out,
  700. const unsigned char *in,
  701. size_t inl)
  702. {
  703. return dasync_cipher_helper(ctx, out, in, inl, EVP_aes_128_cbc_hmac_sha1());
  704. }
  705. static int dasync_aes128_cbc_hmac_sha1_cleanup(EVP_CIPHER_CTX *ctx)
  706. {
  707. /*
  708. * We can safely assume that EVP_aes_128_cbc_hmac_sha1() != NULL,
  709. * see comment before the definition of dasync_aes_128_cbc_hmac_sha1().
  710. */
  711. return dasync_cipher_cleanup_helper(ctx, EVP_aes_128_cbc_hmac_sha1());
  712. }
  713. /*
  714. * RSA implementation
  715. */
  716. static int dasync_rsa_init(EVP_PKEY_CTX *ctx)
  717. {
  718. static int (*pinit)(EVP_PKEY_CTX *ctx);
  719. if (pinit == NULL)
  720. EVP_PKEY_meth_get_init(dasync_rsa_orig, &pinit);
  721. return pinit(ctx);
  722. }
  723. static void dasync_rsa_cleanup(EVP_PKEY_CTX *ctx)
  724. {
  725. static void (*pcleanup)(EVP_PKEY_CTX *ctx);
  726. if (pcleanup == NULL)
  727. EVP_PKEY_meth_get_cleanup(dasync_rsa_orig, &pcleanup);
  728. pcleanup(ctx);
  729. }
  730. static int dasync_rsa_paramgen_init(EVP_PKEY_CTX *ctx)
  731. {
  732. static int (*pparamgen_init)(EVP_PKEY_CTX *ctx);
  733. if (pparamgen_init == NULL)
  734. EVP_PKEY_meth_get_paramgen(dasync_rsa_orig, &pparamgen_init, NULL);
  735. return pparamgen_init != NULL ? pparamgen_init(ctx) : 1;
  736. }
  737. static int dasync_rsa_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
  738. {
  739. static int (*pparamgen)(EVP_PKEY_CTX *c, EVP_PKEY *pkey);
  740. if (pparamgen == NULL)
  741. EVP_PKEY_meth_get_paramgen(dasync_rsa_orig, NULL, &pparamgen);
  742. return pparamgen != NULL ? pparamgen(ctx, pkey) : 1;
  743. }
  744. static int dasync_rsa_keygen_init(EVP_PKEY_CTX *ctx)
  745. {
  746. static int (*pkeygen_init)(EVP_PKEY_CTX *ctx);
  747. if (pkeygen_init == NULL)
  748. EVP_PKEY_meth_get_keygen(dasync_rsa_orig, &pkeygen_init, NULL);
  749. return pkeygen_init != NULL ? pkeygen_init(ctx) : 1;
  750. }
  751. static int dasync_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
  752. {
  753. static int (*pkeygen)(EVP_PKEY_CTX *c, EVP_PKEY *pkey);
  754. if (pkeygen == NULL)
  755. EVP_PKEY_meth_get_keygen(dasync_rsa_orig, NULL, &pkeygen);
  756. return pkeygen(ctx, pkey);
  757. }
  758. static int dasync_rsa_encrypt_init(EVP_PKEY_CTX *ctx)
  759. {
  760. static int (*pencrypt_init)(EVP_PKEY_CTX *ctx);
  761. if (pencrypt_init == NULL)
  762. EVP_PKEY_meth_get_encrypt(dasync_rsa_orig, &pencrypt_init, NULL);
  763. return pencrypt_init != NULL ? pencrypt_init(ctx) : 1;
  764. }
  765. static int dasync_rsa_encrypt(EVP_PKEY_CTX *ctx, unsigned char *out,
  766. size_t *outlen, const unsigned char *in,
  767. size_t inlen)
  768. {
  769. static int (*pencryptfn)(EVP_PKEY_CTX *ctx, unsigned char *out,
  770. size_t *outlen, const unsigned char *in,
  771. size_t inlen);
  772. if (pencryptfn == NULL)
  773. EVP_PKEY_meth_get_encrypt(dasync_rsa_orig, NULL, &pencryptfn);
  774. return pencryptfn(ctx, out, outlen, in, inlen);
  775. }
  776. static int dasync_rsa_decrypt_init(EVP_PKEY_CTX *ctx)
  777. {
  778. static int (*pdecrypt_init)(EVP_PKEY_CTX *ctx);
  779. if (pdecrypt_init == NULL)
  780. EVP_PKEY_meth_get_decrypt(dasync_rsa_orig, &pdecrypt_init, NULL);
  781. return pdecrypt_init != NULL ? pdecrypt_init(ctx) : 1;
  782. }
  783. static int dasync_rsa_decrypt(EVP_PKEY_CTX *ctx, unsigned char *out,
  784. size_t *outlen, const unsigned char *in,
  785. size_t inlen)
  786. {
  787. static int (*pdecrypt)(EVP_PKEY_CTX *ctx, unsigned char *out,
  788. size_t *outlen, const unsigned char *in,
  789. size_t inlen);
  790. if (pdecrypt == NULL)
  791. EVP_PKEY_meth_get_encrypt(dasync_rsa_orig, NULL, &pdecrypt);
  792. return pdecrypt(ctx, out, outlen, in, inlen);
  793. }
  794. static int dasync_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
  795. {
  796. static int (*pctrl)(EVP_PKEY_CTX *ctx, int type, int p1, void *p2);
  797. if (pctrl == NULL)
  798. EVP_PKEY_meth_get_ctrl(dasync_rsa_orig, &pctrl, NULL);
  799. return pctrl(ctx, type, p1, p2);
  800. }
  801. static int dasync_rsa_ctrl_str(EVP_PKEY_CTX *ctx, const char *type,
  802. const char *value)
  803. {
  804. static int (*pctrl_str)(EVP_PKEY_CTX *ctx, const char *type,
  805. const char *value);
  806. if (pctrl_str == NULL)
  807. EVP_PKEY_meth_get_ctrl(dasync_rsa_orig, NULL, &pctrl_str);
  808. return pctrl_str(ctx, type, value);
  809. }