e_dasync.c 24 KB

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