2
0

e_dasync.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  1. /*
  2. * Copyright 2015-2021 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_aes256_ctr_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
  120. void *ptr);
  121. static int dasync_aes256_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  122. const unsigned char *iv, int enc);
  123. static int dasync_aes256_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  124. const unsigned char *in, size_t inl);
  125. static int dasync_aes256_ctr_cleanup(EVP_CIPHER_CTX *ctx);
  126. static int dasync_aes128_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX *ctx, int type,
  127. int arg, void *ptr);
  128. static int dasync_aes128_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX *ctx,
  129. const unsigned char *key,
  130. const unsigned char *iv,
  131. int enc);
  132. static int dasync_aes128_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx,
  133. unsigned char *out,
  134. const unsigned char *in,
  135. size_t inl);
  136. static int dasync_aes128_cbc_hmac_sha1_cleanup(EVP_CIPHER_CTX *ctx);
  137. struct dasync_pipeline_ctx {
  138. void *inner_cipher_data;
  139. unsigned int numpipes;
  140. unsigned char **inbufs;
  141. unsigned char **outbufs;
  142. size_t *lens;
  143. unsigned char tlsaad[SSL_MAX_PIPELINES][EVP_AEAD_TLS1_AAD_LEN];
  144. unsigned int aadctr;
  145. };
  146. /*
  147. * Holds the EVP_CIPHER object for aes_128_cbc in this engine. Set up once only
  148. * during engine bind and can then be reused many times.
  149. */
  150. static EVP_CIPHER *_hidden_aes_128_cbc = NULL;
  151. static const EVP_CIPHER *dasync_aes_128_cbc(void)
  152. {
  153. return _hidden_aes_128_cbc;
  154. }
  155. static EVP_CIPHER *_hidden_aes_256_ctr = NULL;
  156. static const EVP_CIPHER *dasync_aes_256_ctr(void)
  157. {
  158. return _hidden_aes_256_ctr;
  159. }
  160. /*
  161. * Holds the EVP_CIPHER object for aes_128_cbc_hmac_sha1 in this engine. Set up
  162. * once only during engine bind and can then be reused many times.
  163. *
  164. * This 'stitched' cipher depends on the EVP_aes_128_cbc_hmac_sha1() cipher,
  165. * which is implemented only if the AES-NI instruction set extension is available
  166. * (see OPENSSL_IA32CAP(3)). If that's not the case, then this cipher will not
  167. * be available either.
  168. *
  169. * Note: Since it is a legacy mac-then-encrypt cipher, modern TLS peers (which
  170. * negotiate the encrypt-then-mac extension) won't negotiate it anyway.
  171. */
  172. static EVP_CIPHER *_hidden_aes_128_cbc_hmac_sha1 = NULL;
  173. static const EVP_CIPHER *dasync_aes_128_cbc_hmac_sha1(void)
  174. {
  175. return _hidden_aes_128_cbc_hmac_sha1;
  176. }
  177. static void destroy_ciphers(void)
  178. {
  179. EVP_CIPHER_meth_free(_hidden_aes_128_cbc);
  180. EVP_CIPHER_meth_free(_hidden_aes_256_ctr);
  181. EVP_CIPHER_meth_free(_hidden_aes_128_cbc_hmac_sha1);
  182. _hidden_aes_128_cbc = NULL;
  183. _hidden_aes_256_ctr = NULL;
  184. _hidden_aes_128_cbc_hmac_sha1 = NULL;
  185. }
  186. static int dasync_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
  187. const int **nids, int nid);
  188. static int dasync_cipher_nids[] = {
  189. NID_aes_128_cbc,
  190. NID_aes_256_ctr,
  191. NID_aes_128_cbc_hmac_sha1,
  192. 0
  193. };
  194. static int bind_dasync(ENGINE *e)
  195. {
  196. /* Setup RSA */
  197. ;
  198. if ((dasync_rsa_orig = EVP_PKEY_meth_find(EVP_PKEY_RSA)) == NULL
  199. || (dasync_rsa = EVP_PKEY_meth_new(EVP_PKEY_RSA,
  200. EVP_PKEY_FLAG_AUTOARGLEN)) == NULL)
  201. return 0;
  202. EVP_PKEY_meth_set_init(dasync_rsa, dasync_rsa_init);
  203. EVP_PKEY_meth_set_cleanup(dasync_rsa, dasync_rsa_cleanup);
  204. EVP_PKEY_meth_set_paramgen(dasync_rsa, dasync_rsa_paramgen_init,
  205. dasync_rsa_paramgen);
  206. EVP_PKEY_meth_set_keygen(dasync_rsa, dasync_rsa_keygen_init,
  207. dasync_rsa_keygen);
  208. EVP_PKEY_meth_set_encrypt(dasync_rsa, dasync_rsa_encrypt_init,
  209. dasync_rsa_encrypt);
  210. EVP_PKEY_meth_set_decrypt(dasync_rsa, dasync_rsa_decrypt_init,
  211. dasync_rsa_decrypt);
  212. EVP_PKEY_meth_set_ctrl(dasync_rsa, dasync_rsa_ctrl,
  213. dasync_rsa_ctrl_str);
  214. /* Ensure the dasync error handling is set up */
  215. ERR_load_DASYNC_strings();
  216. if (!ENGINE_set_id(e, engine_dasync_id)
  217. || !ENGINE_set_name(e, engine_dasync_name)
  218. || !ENGINE_set_pkey_meths(e, dasync_pkey)
  219. || !ENGINE_set_digests(e, dasync_digests)
  220. || !ENGINE_set_ciphers(e, dasync_ciphers)
  221. || !ENGINE_set_destroy_function(e, dasync_destroy)
  222. || !ENGINE_set_init_function(e, dasync_init)
  223. || !ENGINE_set_finish_function(e, dasync_finish)) {
  224. DASYNCerr(DASYNC_F_BIND_DASYNC, DASYNC_R_INIT_FAILED);
  225. return 0;
  226. }
  227. /*
  228. * Set up the EVP_CIPHER and EVP_MD objects for the ciphers/digests
  229. * supplied by this engine
  230. */
  231. _hidden_sha1_md = EVP_MD_meth_new(NID_sha1, NID_sha1WithRSAEncryption);
  232. if (_hidden_sha1_md == NULL
  233. || !EVP_MD_meth_set_result_size(_hidden_sha1_md, SHA_DIGEST_LENGTH)
  234. || !EVP_MD_meth_set_input_blocksize(_hidden_sha1_md, SHA_CBLOCK)
  235. || !EVP_MD_meth_set_app_datasize(_hidden_sha1_md,
  236. sizeof(EVP_MD *) + sizeof(SHA_CTX))
  237. || !EVP_MD_meth_set_flags(_hidden_sha1_md, EVP_MD_FLAG_DIGALGID_ABSENT)
  238. || !EVP_MD_meth_set_init(_hidden_sha1_md, dasync_sha1_init)
  239. || !EVP_MD_meth_set_update(_hidden_sha1_md, dasync_sha1_update)
  240. || !EVP_MD_meth_set_final(_hidden_sha1_md, dasync_sha1_final)) {
  241. EVP_MD_meth_free(_hidden_sha1_md);
  242. _hidden_sha1_md = NULL;
  243. }
  244. _hidden_aes_128_cbc = EVP_CIPHER_meth_new(NID_aes_128_cbc,
  245. 16 /* block size */,
  246. 16 /* key len */);
  247. if (_hidden_aes_128_cbc == NULL
  248. || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_128_cbc,16)
  249. || !EVP_CIPHER_meth_set_flags(_hidden_aes_128_cbc,
  250. EVP_CIPH_FLAG_DEFAULT_ASN1
  251. | EVP_CIPH_CBC_MODE
  252. | EVP_CIPH_FLAG_PIPELINE
  253. | EVP_CIPH_CUSTOM_COPY)
  254. || !EVP_CIPHER_meth_set_init(_hidden_aes_128_cbc,
  255. dasync_aes128_init_key)
  256. || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_cbc,
  257. dasync_aes128_cbc_cipher)
  258. || !EVP_CIPHER_meth_set_cleanup(_hidden_aes_128_cbc,
  259. dasync_aes128_cbc_cleanup)
  260. || !EVP_CIPHER_meth_set_ctrl(_hidden_aes_128_cbc,
  261. dasync_aes128_cbc_ctrl)
  262. || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_cbc,
  263. sizeof(struct dasync_pipeline_ctx))) {
  264. EVP_CIPHER_meth_free(_hidden_aes_128_cbc);
  265. _hidden_aes_128_cbc = NULL;
  266. }
  267. _hidden_aes_256_ctr = EVP_CIPHER_meth_new(NID_aes_256_ctr,
  268. 1 /* block size */,
  269. 32 /* key len */);
  270. if (_hidden_aes_256_ctr == NULL
  271. || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_256_ctr,16)
  272. || !EVP_CIPHER_meth_set_flags(_hidden_aes_256_ctr,
  273. EVP_CIPH_FLAG_DEFAULT_ASN1
  274. | EVP_CIPH_CTR_MODE
  275. | EVP_CIPH_FLAG_PIPELINE
  276. | EVP_CIPH_CUSTOM_COPY)
  277. || !EVP_CIPHER_meth_set_init(_hidden_aes_256_ctr,
  278. dasync_aes256_init_key)
  279. || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_256_ctr,
  280. dasync_aes256_ctr_cipher)
  281. || !EVP_CIPHER_meth_set_cleanup(_hidden_aes_256_ctr,
  282. dasync_aes256_ctr_cleanup)
  283. || !EVP_CIPHER_meth_set_ctrl(_hidden_aes_256_ctr,
  284. dasync_aes256_ctr_ctrl)
  285. || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_256_ctr,
  286. sizeof(struct dasync_pipeline_ctx))) {
  287. EVP_CIPHER_meth_free(_hidden_aes_256_ctr);
  288. _hidden_aes_256_ctr = NULL;
  289. }
  290. _hidden_aes_128_cbc_hmac_sha1 = EVP_CIPHER_meth_new(
  291. NID_aes_128_cbc_hmac_sha1,
  292. 16 /* block size */,
  293. 16 /* key len */);
  294. if (_hidden_aes_128_cbc_hmac_sha1 == NULL
  295. || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_128_cbc_hmac_sha1,16)
  296. || !EVP_CIPHER_meth_set_flags(_hidden_aes_128_cbc_hmac_sha1,
  297. EVP_CIPH_CBC_MODE
  298. | EVP_CIPH_FLAG_DEFAULT_ASN1
  299. | EVP_CIPH_FLAG_AEAD_CIPHER
  300. | EVP_CIPH_FLAG_PIPELINE
  301. | EVP_CIPH_CUSTOM_COPY)
  302. || !EVP_CIPHER_meth_set_init(_hidden_aes_128_cbc_hmac_sha1,
  303. dasync_aes128_cbc_hmac_sha1_init_key)
  304. || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_cbc_hmac_sha1,
  305. dasync_aes128_cbc_hmac_sha1_cipher)
  306. || !EVP_CIPHER_meth_set_cleanup(_hidden_aes_128_cbc_hmac_sha1,
  307. dasync_aes128_cbc_hmac_sha1_cleanup)
  308. || !EVP_CIPHER_meth_set_ctrl(_hidden_aes_128_cbc_hmac_sha1,
  309. dasync_aes128_cbc_hmac_sha1_ctrl)
  310. || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_cbc_hmac_sha1,
  311. sizeof(struct dasync_pipeline_ctx))) {
  312. EVP_CIPHER_meth_free(_hidden_aes_128_cbc_hmac_sha1);
  313. _hidden_aes_128_cbc_hmac_sha1 = NULL;
  314. }
  315. return 1;
  316. }
  317. static void destroy_pkey(void)
  318. {
  319. /*
  320. * We don't actually need to free the dasync_rsa method since this is
  321. * automatically freed for us by libcrypto.
  322. */
  323. dasync_rsa_orig = NULL;
  324. dasync_rsa = NULL;
  325. }
  326. # ifndef OPENSSL_NO_DYNAMIC_ENGINE
  327. static int bind_helper(ENGINE *e, const char *id)
  328. {
  329. if (id && (strcmp(id, engine_dasync_id) != 0))
  330. return 0;
  331. if (!bind_dasync(e))
  332. return 0;
  333. return 1;
  334. }
  335. IMPLEMENT_DYNAMIC_CHECK_FN()
  336. IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
  337. # endif
  338. static ENGINE *engine_dasync(void)
  339. {
  340. ENGINE *ret = ENGINE_new();
  341. if (!ret)
  342. return NULL;
  343. if (!bind_dasync(ret)) {
  344. ENGINE_free(ret);
  345. return NULL;
  346. }
  347. return ret;
  348. }
  349. void engine_load_dasync_int(void)
  350. {
  351. ENGINE *toadd = engine_dasync();
  352. if (!toadd)
  353. return;
  354. ERR_set_mark();
  355. ENGINE_add(toadd);
  356. /*
  357. * If the "add" worked, it gets a structural reference. So either way, we
  358. * release our just-created reference.
  359. */
  360. ENGINE_free(toadd);
  361. /*
  362. * If the "add" didn't work, it was probably a conflict because it was
  363. * already added (eg. someone calling ENGINE_load_blah then calling
  364. * ENGINE_load_builtin_engines() perhaps).
  365. */
  366. ERR_pop_to_mark();
  367. }
  368. static int dasync_init(ENGINE *e)
  369. {
  370. return 1;
  371. }
  372. static int dasync_finish(ENGINE *e)
  373. {
  374. return 1;
  375. }
  376. static int dasync_destroy(ENGINE *e)
  377. {
  378. destroy_digests();
  379. destroy_ciphers();
  380. destroy_pkey();
  381. ERR_unload_DASYNC_strings();
  382. return 1;
  383. }
  384. static int dasync_pkey(ENGINE *e, EVP_PKEY_METHOD **pmeth,
  385. const int **pnids, int nid)
  386. {
  387. static const int rnid = EVP_PKEY_RSA;
  388. if (pmeth == NULL) {
  389. *pnids = &rnid;
  390. return 1;
  391. }
  392. if (nid == EVP_PKEY_RSA) {
  393. *pmeth = dasync_rsa;
  394. return 1;
  395. }
  396. *pmeth = NULL;
  397. return 0;
  398. }
  399. static int dasync_digests(ENGINE *e, const EVP_MD **digest,
  400. const int **nids, int nid)
  401. {
  402. int ok = 1;
  403. if (!digest) {
  404. /* We are returning a list of supported nids */
  405. return dasync_digest_nids(nids);
  406. }
  407. /* We are being asked for a specific digest */
  408. switch (nid) {
  409. case NID_sha1:
  410. *digest = dasync_sha1();
  411. break;
  412. default:
  413. ok = 0;
  414. *digest = NULL;
  415. break;
  416. }
  417. return ok;
  418. }
  419. static int dasync_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
  420. const int **nids, int nid)
  421. {
  422. int ok = 1;
  423. if (cipher == NULL) {
  424. /* We are returning a list of supported nids */
  425. *nids = dasync_cipher_nids;
  426. return (sizeof(dasync_cipher_nids) -
  427. 1) / sizeof(dasync_cipher_nids[0]);
  428. }
  429. /* We are being asked for a specific cipher */
  430. switch (nid) {
  431. case NID_aes_128_cbc:
  432. *cipher = dasync_aes_128_cbc();
  433. break;
  434. case NID_aes_256_ctr:
  435. *cipher = dasync_aes_256_ctr();
  436. break;
  437. case NID_aes_128_cbc_hmac_sha1:
  438. *cipher = dasync_aes_128_cbc_hmac_sha1();
  439. break;
  440. default:
  441. ok = 0;
  442. *cipher = NULL;
  443. break;
  444. }
  445. return ok;
  446. }
  447. static void wait_cleanup(ASYNC_WAIT_CTX *ctx, const void *key,
  448. OSSL_ASYNC_FD readfd, void *pvwritefd)
  449. {
  450. OSSL_ASYNC_FD *pwritefd = (OSSL_ASYNC_FD *)pvwritefd;
  451. #if defined(ASYNC_WIN)
  452. CloseHandle(readfd);
  453. CloseHandle(*pwritefd);
  454. #elif defined(ASYNC_POSIX)
  455. close(readfd);
  456. close(*pwritefd);
  457. #endif
  458. OPENSSL_free(pwritefd);
  459. }
  460. #define DUMMY_CHAR 'X'
  461. static void dummy_pause_job(void) {
  462. ASYNC_JOB *job;
  463. ASYNC_WAIT_CTX *waitctx;
  464. ASYNC_callback_fn callback;
  465. void * callback_arg;
  466. OSSL_ASYNC_FD pipefds[2] = {0, 0};
  467. OSSL_ASYNC_FD *writefd;
  468. #if defined(ASYNC_WIN)
  469. DWORD numwritten, numread;
  470. char buf = DUMMY_CHAR;
  471. #elif defined(ASYNC_POSIX)
  472. char buf = DUMMY_CHAR;
  473. #endif
  474. if ((job = ASYNC_get_current_job()) == NULL)
  475. return;
  476. waitctx = ASYNC_get_wait_ctx(job);
  477. if (ASYNC_WAIT_CTX_get_callback(waitctx, &callback, &callback_arg) && callback != NULL) {
  478. /*
  479. * In the Dummy async engine we are cheating. We call the callback that the job
  480. * is complete before the call to ASYNC_pause_job(). A real
  481. * async engine would only call the callback when the job was actually complete
  482. */
  483. (*callback)(callback_arg);
  484. ASYNC_pause_job();
  485. return;
  486. }
  487. if (ASYNC_WAIT_CTX_get_fd(waitctx, engine_dasync_id, &pipefds[0],
  488. (void **)&writefd)) {
  489. pipefds[1] = *writefd;
  490. } else {
  491. writefd = OPENSSL_malloc(sizeof(*writefd));
  492. if (writefd == NULL)
  493. return;
  494. #if defined(ASYNC_WIN)
  495. if (CreatePipe(&pipefds[0], &pipefds[1], NULL, 256) == 0) {
  496. OPENSSL_free(writefd);
  497. return;
  498. }
  499. #elif defined(ASYNC_POSIX)
  500. if (pipe(pipefds) != 0) {
  501. OPENSSL_free(writefd);
  502. return;
  503. }
  504. #endif
  505. *writefd = pipefds[1];
  506. if (!ASYNC_WAIT_CTX_set_wait_fd(waitctx, engine_dasync_id, pipefds[0],
  507. writefd, wait_cleanup)) {
  508. wait_cleanup(waitctx, engine_dasync_id, pipefds[0], writefd);
  509. return;
  510. }
  511. }
  512. /*
  513. * In the Dummy async engine we are cheating. We signal that the job
  514. * is complete by waking it before the call to ASYNC_pause_job(). A real
  515. * async engine would only wake when the job was actually complete
  516. */
  517. #if defined(ASYNC_WIN)
  518. WriteFile(pipefds[1], &buf, 1, &numwritten, NULL);
  519. #elif defined(ASYNC_POSIX)
  520. if (write(pipefds[1], &buf, 1) < 0)
  521. return;
  522. #endif
  523. /* Ignore errors - we carry on anyway */
  524. ASYNC_pause_job();
  525. /* Clear the wake signal */
  526. #if defined(ASYNC_WIN)
  527. ReadFile(pipefds[0], &buf, 1, &numread, NULL);
  528. #elif defined(ASYNC_POSIX)
  529. if (read(pipefds[0], &buf, 1) < 0)
  530. return;
  531. #endif
  532. }
  533. /*
  534. * SHA1 implementation. At the moment we just defer to the standard
  535. * implementation
  536. */
  537. static int dasync_sha1_init(EVP_MD_CTX *ctx)
  538. {
  539. dummy_pause_job();
  540. return EVP_MD_meth_get_init(EVP_sha1())(ctx);
  541. }
  542. static int dasync_sha1_update(EVP_MD_CTX *ctx, const void *data,
  543. size_t count)
  544. {
  545. dummy_pause_job();
  546. return EVP_MD_meth_get_update(EVP_sha1())(ctx, data, count);
  547. }
  548. static int dasync_sha1_final(EVP_MD_CTX *ctx, unsigned char *md)
  549. {
  550. dummy_pause_job();
  551. return EVP_MD_meth_get_final(EVP_sha1())(ctx, md);
  552. }
  553. /* Cipher helper functions */
  554. static int dasync_cipher_ctrl_helper(EVP_CIPHER_CTX *ctx, int type, int arg,
  555. void *ptr, int aeadcapable,
  556. const EVP_CIPHER *ciph)
  557. {
  558. int ret;
  559. struct dasync_pipeline_ctx *pipe_ctx =
  560. (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
  561. if (pipe_ctx == NULL)
  562. return 0;
  563. switch (type) {
  564. case EVP_CTRL_COPY:
  565. {
  566. size_t sz = EVP_CIPHER_impl_ctx_size(ciph);
  567. void *inner_cipher_data = OPENSSL_malloc(sz);
  568. if (inner_cipher_data == NULL)
  569. return -1;
  570. memcpy(inner_cipher_data, pipe_ctx->inner_cipher_data, sz);
  571. pipe_ctx->inner_cipher_data = inner_cipher_data;
  572. }
  573. break;
  574. case EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS:
  575. pipe_ctx->numpipes = arg;
  576. pipe_ctx->outbufs = (unsigned char **)ptr;
  577. break;
  578. case EVP_CTRL_SET_PIPELINE_INPUT_BUFS:
  579. pipe_ctx->numpipes = arg;
  580. pipe_ctx->inbufs = (unsigned char **)ptr;
  581. break;
  582. case EVP_CTRL_SET_PIPELINE_INPUT_LENS:
  583. pipe_ctx->numpipes = arg;
  584. pipe_ctx->lens = (size_t *)ptr;
  585. break;
  586. case EVP_CTRL_AEAD_SET_MAC_KEY:
  587. if (!aeadcapable)
  588. return -1;
  589. EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
  590. ret = EVP_CIPHER_meth_get_ctrl(EVP_aes_128_cbc_hmac_sha1())
  591. (ctx, type, arg, ptr);
  592. EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
  593. return ret;
  594. case EVP_CTRL_AEAD_TLS1_AAD:
  595. {
  596. unsigned char *p = ptr;
  597. unsigned int len;
  598. if (!aeadcapable || arg != EVP_AEAD_TLS1_AAD_LEN)
  599. return -1;
  600. if (pipe_ctx->aadctr >= SSL_MAX_PIPELINES)
  601. return -1;
  602. memcpy(pipe_ctx->tlsaad[pipe_ctx->aadctr], ptr,
  603. EVP_AEAD_TLS1_AAD_LEN);
  604. pipe_ctx->aadctr++;
  605. len = p[arg - 2] << 8 | p[arg - 1];
  606. if (EVP_CIPHER_CTX_is_encrypting(ctx)) {
  607. if ((p[arg - 4] << 8 | p[arg - 3]) >= TLS1_1_VERSION) {
  608. if (len < AES_BLOCK_SIZE)
  609. return 0;
  610. len -= AES_BLOCK_SIZE;
  611. }
  612. return ((len + SHA_DIGEST_LENGTH + AES_BLOCK_SIZE)
  613. & -AES_BLOCK_SIZE) - len;
  614. } else {
  615. return SHA_DIGEST_LENGTH;
  616. }
  617. }
  618. default:
  619. return 0;
  620. }
  621. return 1;
  622. }
  623. static int dasync_cipher_init_key_helper(EVP_CIPHER_CTX *ctx,
  624. const unsigned char *key,
  625. const unsigned char *iv, int enc,
  626. const EVP_CIPHER *cipher)
  627. {
  628. int ret;
  629. struct dasync_pipeline_ctx *pipe_ctx =
  630. (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
  631. if (pipe_ctx->inner_cipher_data == NULL
  632. && EVP_CIPHER_impl_ctx_size(cipher) != 0) {
  633. pipe_ctx->inner_cipher_data = OPENSSL_zalloc(
  634. EVP_CIPHER_impl_ctx_size(cipher));
  635. if (pipe_ctx->inner_cipher_data == NULL) {
  636. DASYNCerr(DASYNC_F_DASYNC_CIPHER_INIT_KEY_HELPER,
  637. ERR_R_MALLOC_FAILURE);
  638. return 0;
  639. }
  640. }
  641. pipe_ctx->numpipes = 0;
  642. pipe_ctx->aadctr = 0;
  643. EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
  644. ret = EVP_CIPHER_meth_get_init(cipher)(ctx, key, iv, enc);
  645. EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
  646. return ret;
  647. }
  648. static int dasync_cipher_helper(EVP_CIPHER_CTX *ctx, unsigned char *out,
  649. const unsigned char *in, size_t inl,
  650. const EVP_CIPHER *cipher)
  651. {
  652. int ret = 1;
  653. unsigned int i, pipes;
  654. struct dasync_pipeline_ctx *pipe_ctx =
  655. (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
  656. pipes = pipe_ctx->numpipes;
  657. EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
  658. if (pipes == 0) {
  659. if (pipe_ctx->aadctr != 0) {
  660. if (pipe_ctx->aadctr != 1)
  661. return -1;
  662. EVP_CIPHER_meth_get_ctrl(cipher)
  663. (ctx, EVP_CTRL_AEAD_TLS1_AAD,
  664. EVP_AEAD_TLS1_AAD_LEN,
  665. pipe_ctx->tlsaad[0]);
  666. }
  667. ret = EVP_CIPHER_meth_get_do_cipher(cipher)
  668. (ctx, out, in, inl);
  669. } else {
  670. if (pipe_ctx->aadctr > 0 && pipe_ctx->aadctr != pipes)
  671. return -1;
  672. for (i = 0; i < pipes; i++) {
  673. if (pipe_ctx->aadctr > 0) {
  674. EVP_CIPHER_meth_get_ctrl(cipher)
  675. (ctx, EVP_CTRL_AEAD_TLS1_AAD,
  676. EVP_AEAD_TLS1_AAD_LEN,
  677. pipe_ctx->tlsaad[i]);
  678. }
  679. ret = ret && EVP_CIPHER_meth_get_do_cipher(cipher)
  680. (ctx, pipe_ctx->outbufs[i], pipe_ctx->inbufs[i],
  681. pipe_ctx->lens[i]);
  682. }
  683. pipe_ctx->numpipes = 0;
  684. }
  685. pipe_ctx->aadctr = 0;
  686. EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
  687. return ret;
  688. }
  689. static int dasync_cipher_cleanup_helper(EVP_CIPHER_CTX *ctx,
  690. const EVP_CIPHER *cipher)
  691. {
  692. struct dasync_pipeline_ctx *pipe_ctx =
  693. (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
  694. OPENSSL_clear_free(pipe_ctx->inner_cipher_data,
  695. EVP_CIPHER_impl_ctx_size(cipher));
  696. return 1;
  697. }
  698. /*
  699. * AES128 CBC Implementation
  700. */
  701. static int dasync_aes128_cbc_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
  702. void *ptr)
  703. {
  704. return dasync_cipher_ctrl_helper(ctx, type, arg, ptr, 0, EVP_aes_128_cbc());
  705. }
  706. static int dasync_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  707. const unsigned char *iv, int enc)
  708. {
  709. return dasync_cipher_init_key_helper(ctx, key, iv, enc, EVP_aes_128_cbc());
  710. }
  711. static int dasync_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  712. const unsigned char *in, size_t inl)
  713. {
  714. return dasync_cipher_helper(ctx, out, in, inl, EVP_aes_128_cbc());
  715. }
  716. static int dasync_aes128_cbc_cleanup(EVP_CIPHER_CTX *ctx)
  717. {
  718. return dasync_cipher_cleanup_helper(ctx, EVP_aes_128_cbc());
  719. }
  720. static int dasync_aes256_ctr_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
  721. void *ptr)
  722. {
  723. return dasync_cipher_ctrl_helper(ctx, type, arg, ptr, 0, EVP_aes_256_ctr());
  724. }
  725. static int dasync_aes256_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  726. const unsigned char *iv, int enc)
  727. {
  728. return dasync_cipher_init_key_helper(ctx, key, iv, enc, EVP_aes_256_ctr());
  729. }
  730. static int dasync_aes256_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  731. const unsigned char *in, size_t inl)
  732. {
  733. return dasync_cipher_helper(ctx, out, in, inl, EVP_aes_256_ctr());
  734. }
  735. static int dasync_aes256_ctr_cleanup(EVP_CIPHER_CTX *ctx)
  736. {
  737. return dasync_cipher_cleanup_helper(ctx, EVP_aes_256_ctr());
  738. }
  739. /*
  740. * AES128 CBC HMAC SHA1 Implementation
  741. */
  742. static int dasync_aes128_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX *ctx, int type,
  743. int arg, void *ptr)
  744. {
  745. return dasync_cipher_ctrl_helper(ctx, type, arg, ptr, 1, EVP_aes_128_cbc_hmac_sha1());
  746. }
  747. static int dasync_aes128_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX *ctx,
  748. const unsigned char *key,
  749. const unsigned char *iv,
  750. int enc)
  751. {
  752. /*
  753. * We can safely assume that EVP_aes_128_cbc_hmac_sha1() != NULL,
  754. * see comment before the definition of dasync_aes_128_cbc_hmac_sha1().
  755. */
  756. return dasync_cipher_init_key_helper(ctx, key, iv, enc,
  757. EVP_aes_128_cbc_hmac_sha1());
  758. }
  759. static int dasync_aes128_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx,
  760. unsigned char *out,
  761. const unsigned char *in,
  762. size_t inl)
  763. {
  764. return dasync_cipher_helper(ctx, out, in, inl, EVP_aes_128_cbc_hmac_sha1());
  765. }
  766. static int dasync_aes128_cbc_hmac_sha1_cleanup(EVP_CIPHER_CTX *ctx)
  767. {
  768. /*
  769. * We can safely assume that EVP_aes_128_cbc_hmac_sha1() != NULL,
  770. * see comment before the definition of dasync_aes_128_cbc_hmac_sha1().
  771. */
  772. return dasync_cipher_cleanup_helper(ctx, EVP_aes_128_cbc_hmac_sha1());
  773. }
  774. /*
  775. * RSA implementation
  776. */
  777. static int dasync_rsa_init(EVP_PKEY_CTX *ctx)
  778. {
  779. static int (*pinit)(EVP_PKEY_CTX *ctx);
  780. if (pinit == NULL)
  781. EVP_PKEY_meth_get_init(dasync_rsa_orig, &pinit);
  782. return pinit(ctx);
  783. }
  784. static void dasync_rsa_cleanup(EVP_PKEY_CTX *ctx)
  785. {
  786. static void (*pcleanup)(EVP_PKEY_CTX *ctx);
  787. if (pcleanup == NULL)
  788. EVP_PKEY_meth_get_cleanup(dasync_rsa_orig, &pcleanup);
  789. pcleanup(ctx);
  790. }
  791. static int dasync_rsa_paramgen_init(EVP_PKEY_CTX *ctx)
  792. {
  793. static int (*pparamgen_init)(EVP_PKEY_CTX *ctx);
  794. if (pparamgen_init == NULL)
  795. EVP_PKEY_meth_get_paramgen(dasync_rsa_orig, &pparamgen_init, NULL);
  796. return pparamgen_init != NULL ? pparamgen_init(ctx) : 1;
  797. }
  798. static int dasync_rsa_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
  799. {
  800. static int (*pparamgen)(EVP_PKEY_CTX *c, EVP_PKEY *pkey);
  801. if (pparamgen == NULL)
  802. EVP_PKEY_meth_get_paramgen(dasync_rsa_orig, NULL, &pparamgen);
  803. return pparamgen != NULL ? pparamgen(ctx, pkey) : 1;
  804. }
  805. static int dasync_rsa_keygen_init(EVP_PKEY_CTX *ctx)
  806. {
  807. static int (*pkeygen_init)(EVP_PKEY_CTX *ctx);
  808. if (pkeygen_init == NULL)
  809. EVP_PKEY_meth_get_keygen(dasync_rsa_orig, &pkeygen_init, NULL);
  810. return pkeygen_init != NULL ? pkeygen_init(ctx) : 1;
  811. }
  812. static int dasync_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
  813. {
  814. static int (*pkeygen)(EVP_PKEY_CTX *c, EVP_PKEY *pkey);
  815. if (pkeygen == NULL)
  816. EVP_PKEY_meth_get_keygen(dasync_rsa_orig, NULL, &pkeygen);
  817. return pkeygen(ctx, pkey);
  818. }
  819. static int dasync_rsa_encrypt_init(EVP_PKEY_CTX *ctx)
  820. {
  821. static int (*pencrypt_init)(EVP_PKEY_CTX *ctx);
  822. if (pencrypt_init == NULL)
  823. EVP_PKEY_meth_get_encrypt(dasync_rsa_orig, &pencrypt_init, NULL);
  824. return pencrypt_init != NULL ? pencrypt_init(ctx) : 1;
  825. }
  826. static int dasync_rsa_encrypt(EVP_PKEY_CTX *ctx, unsigned char *out,
  827. size_t *outlen, const unsigned char *in,
  828. size_t inlen)
  829. {
  830. static int (*pencryptfn)(EVP_PKEY_CTX *ctx, unsigned char *out,
  831. size_t *outlen, const unsigned char *in,
  832. size_t inlen);
  833. if (pencryptfn == NULL)
  834. EVP_PKEY_meth_get_encrypt(dasync_rsa_orig, NULL, &pencryptfn);
  835. return pencryptfn(ctx, out, outlen, in, inlen);
  836. }
  837. static int dasync_rsa_decrypt_init(EVP_PKEY_CTX *ctx)
  838. {
  839. static int (*pdecrypt_init)(EVP_PKEY_CTX *ctx);
  840. if (pdecrypt_init == NULL)
  841. EVP_PKEY_meth_get_decrypt(dasync_rsa_orig, &pdecrypt_init, NULL);
  842. return pdecrypt_init != NULL ? pdecrypt_init(ctx) : 1;
  843. }
  844. static int dasync_rsa_decrypt(EVP_PKEY_CTX *ctx, unsigned char *out,
  845. size_t *outlen, const unsigned char *in,
  846. size_t inlen)
  847. {
  848. static int (*pdecrypt)(EVP_PKEY_CTX *ctx, unsigned char *out,
  849. size_t *outlen, const unsigned char *in,
  850. size_t inlen);
  851. if (pdecrypt == NULL)
  852. EVP_PKEY_meth_get_decrypt(dasync_rsa_orig, NULL, &pdecrypt);
  853. return pdecrypt(ctx, out, outlen, in, inlen);
  854. }
  855. static int dasync_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
  856. {
  857. static int (*pctrl)(EVP_PKEY_CTX *ctx, int type, int p1, void *p2);
  858. if (pctrl == NULL)
  859. EVP_PKEY_meth_get_ctrl(dasync_rsa_orig, &pctrl, NULL);
  860. return pctrl(ctx, type, p1, p2);
  861. }
  862. static int dasync_rsa_ctrl_str(EVP_PKEY_CTX *ctx, const char *type,
  863. const char *value)
  864. {
  865. static int (*pctrl_str)(EVP_PKEY_CTX *ctx, const char *type,
  866. const char *value);
  867. if (pctrl_str == NULL)
  868. EVP_PKEY_meth_get_ctrl(dasync_rsa_orig, NULL, &pctrl_str);
  869. return pctrl_str(ctx, type, value);
  870. }