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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. From 0000000000000000000000000000000000000000 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 1b0d523bea..ee3e2eb075 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 2c1b52d572..eff1ed3a7d 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. @@ -31,18 +31,20 @@
  126. # define CHECK_BSD_STYLE_MACROS
  127. #endif
  128. +#define engine_devcrypto_id "devcrypto"
  129. +
  130. /*
  131. * ONE global file descriptor for all sessions. This allows operations
  132. * such as digest session data copying (see digest_copy()), but is also
  133. * saner... why re-open /dev/crypto for every session?
  134. */
  135. -static int cfd;
  136. +static int cfd = -1;
  137. #define DEVCRYPTO_REQUIRE_ACCELERATED 0 /* require confirmation of acceleration */
  138. #define DEVCRYPTO_USE_SOFTWARE 1 /* allow software drivers */
  139. #define DEVCRYPTO_REJECT_SOFTWARE 2 /* only disallow confirmed software drivers */
  140. -#define DEVCRYPTO_DEFAULT_USE_SOFDTRIVERS DEVCRYPTO_REJECT_SOFTWARE
  141. -static int use_softdrivers = DEVCRYPTO_DEFAULT_USE_SOFDTRIVERS;
  142. +#define DEVCRYPTO_DEFAULT_USE_SOFTDRIVERS DEVCRYPTO_REJECT_SOFTWARE
  143. +static int use_softdrivers = DEVCRYPTO_DEFAULT_USE_SOFTDRIVERS;
  144. /*
  145. * cipher/digest status & acceleration definitions
  146. @@ -1058,7 +1060,7 @@ static const ENGINE_CMD_DEFN devcrypto_cmds[] = {
  147. OPENSSL_MSTR(DEVCRYPTO_USE_SOFTWARE) "=allow all drivers, "
  148. OPENSSL_MSTR(DEVCRYPTO_REJECT_SOFTWARE)
  149. "=use if acceleration can't be determined) [default="
  150. - OPENSSL_MSTR(DEVCRYPTO_DEFAULT_USE_SOFDTRIVERS) "]",
  151. + OPENSSL_MSTR(DEVCRYPTO_DEFAULT_USE_SOFTDRIVERS) "]",
  152. ENGINE_CMD_FLAG_NUMERIC},
  153. #endif
  154. @@ -1166,32 +1168,22 @@ static int devcrypto_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
  155. *
  156. *****/
  157. -static int devcrypto_unload(ENGINE *e)
  158. -{
  159. - destroy_all_cipher_methods();
  160. -#ifdef IMPLEMENT_DIGEST
  161. - destroy_all_digest_methods();
  162. -#endif
  163. -
  164. - close(cfd);
  165. -
  166. - return 1;
  167. -}
  168. /*
  169. - * This engine is always built into libcrypto, so it doesn't offer any
  170. - * ability to be dynamically loadable.
  171. + * Opens /dev/crypto
  172. */
  173. -void engine_load_devcrypto_int()
  174. +static int open_devcrypto(void)
  175. {
  176. - ENGINE *e = NULL;
  177. int fd;
  178. + if (cfd >= 0)
  179. + return 1;
  180. +
  181. if ((fd = open("/dev/crypto", O_RDWR, 0)) < 0) {
  182. #ifndef ENGINE_DEVCRYPTO_DEBUG
  183. if (errno != ENOENT)
  184. #endif
  185. fprintf(stderr, "Could not open /dev/crypto: %s\n", strerror(errno));
  186. - return;
  187. + return 0;
  188. }
  189. #ifdef CRIOGET
  190. @@ -1199,35 +1191,61 @@ void engine_load_devcrypto_int()
  191. fprintf(stderr, "Could not create crypto fd: %s\n", strerror(errno));
  192. close(fd);
  193. cfd = -1;
  194. - return;
  195. + return 0;
  196. }
  197. close(fd);
  198. #else
  199. cfd = fd;
  200. #endif
  201. - if ((e = ENGINE_new()) == NULL
  202. - || !ENGINE_set_destroy_function(e, devcrypto_unload)) {
  203. - ENGINE_free(e);
  204. - /*
  205. - * We know that devcrypto_unload() won't be called when one of the
  206. - * above two calls have failed, so we close cfd explicitly here to
  207. - * avoid leaking resources.
  208. - */
  209. - close(cfd);
  210. - return;
  211. + return 1;
  212. +}
  213. +
  214. +static int close_devcrypto(void)
  215. +{
  216. + int ret;
  217. +
  218. + if (cfd < 0)
  219. + return 1;
  220. + ret = close(cfd);
  221. + cfd = -1;
  222. + if (ret != 0) {
  223. + fprintf(stderr, "Error closing /dev/crypto: %s\n", strerror(errno));
  224. + return 0;
  225. }
  226. + return 1;
  227. +}
  228. - prepare_cipher_methods();
  229. +static int devcrypto_unload(ENGINE *e)
  230. +{
  231. + destroy_all_cipher_methods();
  232. #ifdef IMPLEMENT_DIGEST
  233. - prepare_digest_methods();
  234. + destroy_all_digest_methods();
  235. #endif
  236. - if (!ENGINE_set_id(e, "devcrypto")
  237. + close_devcrypto();
  238. +
  239. + return 1;
  240. +}
  241. +
  242. +static int bind_devcrypto(ENGINE *e) {
  243. +
  244. + if (!ENGINE_set_id(e, engine_devcrypto_id)
  245. || !ENGINE_set_name(e, "/dev/crypto engine")
  246. + || !ENGINE_set_destroy_function(e, devcrypto_unload)
  247. || !ENGINE_set_cmd_defns(e, devcrypto_cmds)
  248. - || !ENGINE_set_ctrl_function(e, devcrypto_ctrl)
  249. + || !ENGINE_set_ctrl_function(e, devcrypto_ctrl))
  250. + return 0;
  251. + prepare_cipher_methods();
  252. +#ifdef IMPLEMENT_DIGEST
  253. + prepare_digest_methods();
  254. +#endif
  255. +
  256. + return (ENGINE_set_ciphers(e, devcrypto_ciphers)
  257. +#ifdef IMPLEMENT_DIGEST
  258. + && ENGINE_set_digests(e, devcrypto_digests)
  259. +#endif
  260. /*
  261. * Asymmetric ciphers aren't well supported with /dev/crypto. Among the BSD
  262. * implementations, it seems to only exist in FreeBSD, and regarding the
  263. @@ -1250,23 +1268,36 @@ void engine_load_devcrypto_int()
  264. */
  265. #if 0
  266. # ifndef OPENSSL_NO_RSA
  267. - || !ENGINE_set_RSA(e, devcrypto_rsa)
  268. + && ENGINE_set_RSA(e, devcrypto_rsa)
  269. # endif
  270. # ifndef OPENSSL_NO_DSA
  271. - || !ENGINE_set_DSA(e, devcrypto_dsa)
  272. + && ENGINE_set_DSA(e, devcrypto_dsa)
  273. # endif
  274. # ifndef OPENSSL_NO_DH
  275. - || !ENGINE_set_DH(e, devcrypto_dh)
  276. + && ENGINE_set_DH(e, devcrypto_dh)
  277. # endif
  278. # ifndef OPENSSL_NO_EC
  279. - || !ENGINE_set_EC(e, devcrypto_ec)
  280. + && ENGINE_set_EC(e, devcrypto_ec)
  281. # endif
  282. #endif
  283. - || !ENGINE_set_ciphers(e, devcrypto_ciphers)
  284. -#ifdef IMPLEMENT_DIGEST
  285. - || !ENGINE_set_digests(e, devcrypto_digests)
  286. -#endif
  287. - ) {
  288. + );
  289. +}
  290. +
  291. +#ifdef OPENSSL_NO_DYNAMIC_ENGINE
  292. +/*
  293. + * In case this engine is built into libcrypto, then it doesn't offer any
  294. + * ability to be dynamically loadable.
  295. + */
  296. +void engine_load_devcrypto_int(void)
  297. +{
  298. + ENGINE *e = NULL;
  299. +
  300. + if (!open_devcrypto())
  301. + return;
  302. +
  303. + if ((e = ENGINE_new()) == NULL
  304. + || !bind_devcrypto(e)) {
  305. + close_devcrypto();
  306. ENGINE_free(e);
  307. return;
  308. }
  309. @@ -1275,3 +1306,22 @@ void engine_load_devcrypto_int()
  310. ENGINE_free(e); /* Loose our local reference */
  311. ERR_clear_error();
  312. }
  313. +
  314. +#else
  315. +
  316. +static int bind_helper(ENGINE *e, const char *id)
  317. +{
  318. + if ((id && (strcmp(id, engine_devcrypto_id) != 0))
  319. + || !open_devcrypto())
  320. + return 0;
  321. + if (!bind_devcrypto(e)) {
  322. + close_devcrypto();
  323. + return 0;
  324. + }
  325. + return 1;
  326. +}
  327. +
  328. +IMPLEMENT_DYNAMIC_CHECK_FN()
  329. +IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
  330. +
  331. +#endif