430-e_devcrypto-make-the-dev-crypto-engine-dynamic.patch 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. From f3cef70b34afde3afd13ce3636232d41533b0162 Mon Sep 17 00:00:00 2001
  2. From: Eneas U de Queiroz <[email protected]>
  3. Date: Tue, 6 Nov 2018 10:57:03 -0200
  4. Subject: e_devcrypto: make the /dev/crypto engine dynamic
  5. Engine has been moved from crypto/engine/eng_devcrypto.c to
  6. engines/e_devcrypto.c.
  7. Signed-off-by: Eneas U de Queiroz <[email protected]>
  8. diff --git a/crypto/engine/build.info b/crypto/engine/build.info
  9. index e00802a3fd..47fe948966 100644
  10. --- a/crypto/engine/build.info
  11. +++ b/crypto/engine/build.info
  12. @@ -6,6 +6,3 @@ SOURCE[../../libcrypto]=\
  13. tb_cipher.c tb_digest.c tb_pkmeth.c tb_asnmth.c tb_eckey.c \
  14. eng_openssl.c eng_cnf.c eng_dyn.c \
  15. eng_rdrand.c
  16. -IF[{- !$disabled{devcryptoeng} -}]
  17. - SOURCE[../../libcrypto]=eng_devcrypto.c
  18. -ENDIF
  19. diff --git a/crypto/init.c b/crypto/init.c
  20. index 9fc0e8ef68..b387559920 100644
  21. --- a/crypto/init.c
  22. +++ b/crypto/init.c
  23. @@ -329,18 +329,6 @@ DEFINE_RUN_ONCE_STATIC(ossl_init_engine_openssl)
  24. engine_load_openssl_int();
  25. return 1;
  26. }
  27. -# ifndef OPENSSL_NO_DEVCRYPTOENG
  28. -static CRYPTO_ONCE engine_devcrypto = CRYPTO_ONCE_STATIC_INIT;
  29. -DEFINE_RUN_ONCE_STATIC(ossl_init_engine_devcrypto)
  30. -{
  31. -# ifdef OPENSSL_INIT_DEBUG
  32. - fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_devcrypto: "
  33. - "engine_load_devcrypto_int()\n");
  34. -# endif
  35. - engine_load_devcrypto_int();
  36. - return 1;
  37. -}
  38. -# endif
  39. # ifndef OPENSSL_NO_RDRAND
  40. static CRYPTO_ONCE engine_rdrand = CRYPTO_ONCE_STATIC_INIT;
  41. @@ -365,6 +353,18 @@ DEFINE_RUN_ONCE_STATIC(ossl_init_engine_dynamic)
  42. return 1;
  43. }
  44. # ifndef OPENSSL_NO_STATIC_ENGINE
  45. +# ifndef OPENSSL_NO_DEVCRYPTOENG
  46. +static CRYPTO_ONCE engine_devcrypto = CRYPTO_ONCE_STATIC_INIT;
  47. +DEFINE_RUN_ONCE_STATIC(ossl_init_engine_devcrypto)
  48. +{
  49. +# ifdef OPENSSL_INIT_DEBUG
  50. + fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_devcrypto: "
  51. + "engine_load_devcrypto_int()\n");
  52. +# endif
  53. + engine_load_devcrypto_int();
  54. + return 1;
  55. +}
  56. +# endif
  57. # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
  58. static CRYPTO_ONCE engine_padlock = CRYPTO_ONCE_STATIC_INIT;
  59. DEFINE_RUN_ONCE_STATIC(ossl_init_engine_padlock)
  60. @@ -713,11 +713,6 @@ int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
  61. if ((opts & OPENSSL_INIT_ENGINE_OPENSSL)
  62. && !RUN_ONCE(&engine_openssl, ossl_init_engine_openssl))
  63. return 0;
  64. -# if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_DEVCRYPTOENG)
  65. - if ((opts & OPENSSL_INIT_ENGINE_CRYPTODEV)
  66. - && !RUN_ONCE(&engine_devcrypto, ossl_init_engine_devcrypto))
  67. - return 0;
  68. -# endif
  69. # ifndef OPENSSL_NO_RDRAND
  70. if ((opts & OPENSSL_INIT_ENGINE_RDRAND)
  71. && !RUN_ONCE(&engine_rdrand, ossl_init_engine_rdrand))
  72. @@ -727,6 +722,11 @@ int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
  73. && !RUN_ONCE(&engine_dynamic, ossl_init_engine_dynamic))
  74. return 0;
  75. # ifndef OPENSSL_NO_STATIC_ENGINE
  76. +# ifndef OPENSSL_NO_DEVCRYPTOENG
  77. + if ((opts & OPENSSL_INIT_ENGINE_CRYPTODEV)
  78. + && !RUN_ONCE(&engine_devcrypto, ossl_init_engine_devcrypto))
  79. + return 0;
  80. +# endif
  81. # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
  82. if ((opts & OPENSSL_INIT_ENGINE_PADLOCK)
  83. && !RUN_ONCE(&engine_padlock, ossl_init_engine_padlock))
  84. diff --git a/engines/build.info b/engines/build.info
  85. index 1db771971c..33a25d7004 100644
  86. --- a/engines/build.info
  87. +++ b/engines/build.info
  88. @@ -11,6 +11,9 @@ IF[{- !$disabled{"engine"} -}]
  89. IF[{- !$disabled{afalgeng} -}]
  90. SOURCE[../libcrypto]=e_afalg.c
  91. ENDIF
  92. + IF[{- !$disabled{"devcryptoeng"} -}]
  93. + SOURCE[../libcrypto]=e_devcrypto.c
  94. + ENDIF
  95. ELSE
  96. IF[{- !$disabled{hw} && !$disabled{'hw-padlock'} -}]
  97. ENGINES=padlock
  98. @@ -30,6 +33,12 @@ IF[{- !$disabled{"engine"} -}]
  99. DEPEND[afalg]=../libcrypto
  100. INCLUDE[afalg]= ../include
  101. ENDIF
  102. + IF[{- !$disabled{"devcryptoeng"} -}]
  103. + ENGINES=devcrypto
  104. + SOURCE[devcrypto]=e_devcrypto.c
  105. + DEPEND[devcrypto]=../libcrypto
  106. + INCLUDE[devcrypto]=../include
  107. + ENDIF
  108. ENGINES_NO_INST=ossltest dasync
  109. SOURCE[dasync]=e_dasync.c
  110. diff --git a/crypto/engine/eng_devcrypto.c b/engines/e_devcrypto.c
  111. similarity index 95%
  112. rename from crypto/engine/eng_devcrypto.c
  113. rename to engines/e_devcrypto.c
  114. index 64dc6b891d..fb5c6e1636 100644
  115. --- a/crypto/engine/eng_devcrypto.c
  116. +++ b/engines/e_devcrypto.c
  117. @@ -7,7 +7,7 @@
  118. * https://www.openssl.org/source/license.html
  119. */
  120. -#include "e_os.h"
  121. +#include "../e_os.h"
  122. #include <string.h>
  123. #include <sys/types.h>
  124. #include <sys/stat.h>
  125. @@ -23,26 +23,26 @@
  126. #include <openssl/objects.h>
  127. #include <crypto/cryptodev.h>
  128. -#include "internal/engine.h"
  129. -
  130. /* #define ENGINE_DEVCRYPTO_DEBUG */
  131. #if CRYPTO_ALGORITHM_MIN < CRYPTO_ALGORITHM_MAX
  132. # define CHECK_BSD_STYLE_MACROS
  133. #endif
  134. +#define engine_devcrypto_id "devcrypto"
  135. +
  136. /*
  137. * ONE global file descriptor for all sessions. This allows operations
  138. * such as digest session data copying (see digest_copy()), but is also
  139. * saner... why re-open /dev/crypto for every session?
  140. */
  141. -static int cfd;
  142. +static int cfd = -1;
  143. #define DEVCRYPTO_REQUIRE_ACCELERATED 0 /* require confirmation of acceleration */
  144. #define DEVCRYPTO_USE_SOFTWARE 1 /* allow software drivers */
  145. #define DEVCRYPTO_REJECT_SOFTWARE 2 /* only disallow confirmed software drivers */
  146. -#define DEVCRYPTO_DEFAULT_USE_SOFDTRIVERS DEVCRYPTO_REJECT_SOFTWARE
  147. -static int use_softdrivers = DEVCRYPTO_DEFAULT_USE_SOFDTRIVERS;
  148. +#define DEVCRYPTO_DEFAULT_USE_SOFTDRIVERS DEVCRYPTO_REJECT_SOFTWARE
  149. +static int use_softdrivers = DEVCRYPTO_DEFAULT_USE_SOFTDRIVERS;
  150. /*
  151. * cipher/digest status & acceleration definitions
  152. @@ -66,6 +66,10 @@ struct driver_info_st {
  153. char *driver_name;
  154. };
  155. +#ifdef OPENSSL_NO_DYNAMIC_ENGINE
  156. +void engine_load_devcrypto_int(void);
  157. +#endif
  158. +
  159. static int clean_devcrypto_session(struct session_op *sess) {
  160. if (ioctl(cfd, CIOCFSESSION, &sess->ses) < 0) {
  161. SYSerr(SYS_F_IOCTL, errno);
  162. @@ -341,6 +345,7 @@ static int cipher_ctrl(EVP_CIPHER_CTX *ctx, int type, int p1, void* p2)
  163. struct cipher_ctx *to_cipher_ctx;
  164. switch (type) {
  165. +
  166. case EVP_CTRL_COPY:
  167. if (cipher_ctx == NULL)
  168. return 1;
  169. @@ -702,7 +707,6 @@ static int digest_init(EVP_MD_CTX *ctx)
  170. SYSerr(SYS_F_IOCTL, errno);
  171. return 0;
  172. }
  173. -
  174. return 1;
  175. }
  176. @@ -1058,7 +1062,7 @@ static const ENGINE_CMD_DEFN devcrypto_cmds[] = {
  177. OPENSSL_MSTR(DEVCRYPTO_USE_SOFTWARE) "=allow all drivers, "
  178. OPENSSL_MSTR(DEVCRYPTO_REJECT_SOFTWARE)
  179. "=use if acceleration can't be determined) [default="
  180. - OPENSSL_MSTR(DEVCRYPTO_DEFAULT_USE_SOFDTRIVERS) "]",
  181. + OPENSSL_MSTR(DEVCRYPTO_DEFAULT_USE_SOFTDRIVERS) "]",
  182. ENGINE_CMD_FLAG_NUMERIC},
  183. #endif
  184. @@ -1166,55 +1170,70 @@ static int devcrypto_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
  185. *
  186. *****/
  187. -static int devcrypto_unload(ENGINE *e)
  188. -{
  189. - destroy_all_cipher_methods();
  190. -#ifdef IMPLEMENT_DIGEST
  191. - destroy_all_digest_methods();
  192. -#endif
  193. -
  194. - close(cfd);
  195. -
  196. - return 1;
  197. -}
  198. /*
  199. - * This engine is always built into libcrypto, so it doesn't offer any
  200. - * ability to be dynamically loadable.
  201. + * Opens /dev/crypto
  202. */
  203. -void engine_load_devcrypto_int()
  204. +static int open_devcrypto(void)
  205. {
  206. - ENGINE *e = NULL;
  207. + if (cfd >= 0)
  208. + return 1;
  209. if ((cfd = open("/dev/crypto", O_RDWR, 0)) < 0) {
  210. #ifndef ENGINE_DEVCRYPTO_DEBUG
  211. if (errno != ENOENT)
  212. #endif
  213. fprintf(stderr, "Could not open /dev/crypto: %s\n", strerror(errno));
  214. - return;
  215. + return 0;
  216. }
  217. - if ((e = ENGINE_new()) == NULL
  218. - || !ENGINE_set_destroy_function(e, devcrypto_unload)) {
  219. - ENGINE_free(e);
  220. - /*
  221. - * We know that devcrypto_unload() won't be called when one of the
  222. - * above two calls have failed, so we close cfd explicitly here to
  223. - * avoid leaking resources.
  224. - */
  225. - close(cfd);
  226. - return;
  227. + return 1;
  228. +}
  229. +
  230. +static int close_devcrypto(void)
  231. +{
  232. + int ret;
  233. +
  234. + if (cfd < 0)
  235. + return 1;
  236. + ret = close(cfd);
  237. + cfd = -1;
  238. + if (ret != 0) {
  239. + fprintf(stderr, "Error closing /dev/crypto: %s\n", strerror(errno));
  240. + return 0;
  241. }
  242. + return 1;
  243. +}
  244. - prepare_cipher_methods();
  245. +static int devcrypto_unload(ENGINE *e)
  246. +{
  247. + destroy_all_cipher_methods();
  248. #ifdef IMPLEMENT_DIGEST
  249. - prepare_digest_methods();
  250. + destroy_all_digest_methods();
  251. #endif
  252. - if (!ENGINE_set_id(e, "devcrypto")
  253. + close_devcrypto();
  254. +
  255. + return 1;
  256. +}
  257. +
  258. +static int bind_devcrypto(ENGINE *e) {
  259. +
  260. + if (!ENGINE_set_id(e, engine_devcrypto_id)
  261. || !ENGINE_set_name(e, "/dev/crypto engine")
  262. + || !ENGINE_set_destroy_function(e, devcrypto_unload)
  263. || !ENGINE_set_cmd_defns(e, devcrypto_cmds)
  264. - || !ENGINE_set_ctrl_function(e, devcrypto_ctrl)
  265. + || !ENGINE_set_ctrl_function(e, devcrypto_ctrl))
  266. + return 0;
  267. +
  268. + prepare_cipher_methods();
  269. +#ifdef IMPLEMENT_DIGEST
  270. + prepare_digest_methods();
  271. +#endif
  272. + return (ENGINE_set_ciphers(e, devcrypto_ciphers)
  273. +#ifdef IMPLEMENT_DIGEST
  274. + && ENGINE_set_digests(e, devcrypto_digests)
  275. +#endif
  276. /*
  277. * Asymmetric ciphers aren't well supported with /dev/crypto. Among the BSD
  278. * implementations, it seems to only exist in FreeBSD, and regarding the
  279. @@ -1237,23 +1256,36 @@ void engine_load_devcrypto_int()
  280. */
  281. #if 0
  282. # ifndef OPENSSL_NO_RSA
  283. - || !ENGINE_set_RSA(e, devcrypto_rsa)
  284. + && ENGINE_set_RSA(e, devcrypto_rsa)
  285. # endif
  286. # ifndef OPENSSL_NO_DSA
  287. - || !ENGINE_set_DSA(e, devcrypto_dsa)
  288. + && ENGINE_set_DSA(e, devcrypto_dsa)
  289. # endif
  290. # ifndef OPENSSL_NO_DH
  291. - || !ENGINE_set_DH(e, devcrypto_dh)
  292. + && ENGINE_set_DH(e, devcrypto_dh)
  293. # endif
  294. # ifndef OPENSSL_NO_EC
  295. - || !ENGINE_set_EC(e, devcrypto_ec)
  296. + && ENGINE_set_EC(e, devcrypto_ec)
  297. # endif
  298. #endif
  299. - || !ENGINE_set_ciphers(e, devcrypto_ciphers)
  300. -#ifdef IMPLEMENT_DIGEST
  301. - || !ENGINE_set_digests(e, devcrypto_digests)
  302. -#endif
  303. - ) {
  304. + );
  305. +}
  306. +
  307. +#ifdef OPENSSL_NO_DYNAMIC_ENGINE
  308. +/*
  309. + * In case this engine is built into libcrypto, then it doesn't offer any
  310. + * ability to be dynamically loadable.
  311. + */
  312. +void engine_load_devcrypto_int(void)
  313. +{
  314. + ENGINE *e = NULL;
  315. +
  316. + if (!open_devcrypto())
  317. + return;
  318. +
  319. + if ((e = ENGINE_new()) == NULL
  320. + || !bind_devcrypto(e)) {
  321. + close_devcrypto();
  322. ENGINE_free(e);
  323. return;
  324. }
  325. @@ -1262,3 +1294,22 @@ void engine_load_devcrypto_int()
  326. ENGINE_free(e); /* Loose our local reference */
  327. ERR_clear_error();
  328. }
  329. +
  330. +#else
  331. +
  332. +static int bind_helper(ENGINE *e, const char *id)
  333. +{
  334. + if ((id && (strcmp(id, engine_devcrypto_id) != 0))
  335. + || !open_devcrypto())
  336. + return 0;
  337. + if (!bind_devcrypto(e)) {
  338. + close_devcrypto();
  339. + return 0;
  340. + }
  341. + return 1;
  342. +}
  343. +
  344. +IMPLEMENT_DYNAMIC_CHECK_FN()
  345. +IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
  346. +
  347. +#endif