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

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