crypto_openssl_mgt.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /* Copyright (c) 2001, Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file crypto_openssl_mgt.c
  8. *
  9. * \brief Block of functions related to operations from OpenSSL.
  10. **/
  11. #include "lib/crypt_ops/compat_openssl.h"
  12. #include "lib/crypt_ops/crypto_openssl_mgt.h"
  13. #include "lib/crypt_ops/crypto_rand.h"
  14. #include "lib/crypt_ops/aes.h"
  15. #include "lib/string/util_string.h"
  16. #include "lib/lock/compat_mutex.h"
  17. #include "lib/log/log.h"
  18. #include "lib/testsupport/testsupport.h"
  19. #include "lib/thread/threads.h"
  20. DISABLE_GCC_WARNING(redundant-decls)
  21. #include <openssl/err.h>
  22. #include <openssl/rsa.h>
  23. #include <openssl/pem.h>
  24. #include <openssl/evp.h>
  25. #include <openssl/engine.h>
  26. #include <openssl/rand.h>
  27. #include <openssl/bn.h>
  28. #include <openssl/dh.h>
  29. #include <openssl/conf.h>
  30. #include <openssl/hmac.h>
  31. #include <openssl/crypto.h>
  32. #include <openssl/ssl.h>
  33. ENABLE_GCC_WARNING(redundant-decls)
  34. #include <string.h>
  35. #ifndef NEW_THREAD_API
  36. /** A number of preallocated mutexes for use by OpenSSL. */
  37. static tor_mutex_t **openssl_mutexes_ = NULL;
  38. /** How many mutexes have we allocated for use by OpenSSL? */
  39. static int n_openssl_mutexes_ = 0;
  40. #endif /* !defined(NEW_THREAD_API) */
  41. /** Declare STATIC functions */
  42. STATIC char * parse_openssl_version_str(const char *raw_version);
  43. #ifndef NEW_THREAD_API
  44. STATIC void openssl_locking_cb_(int mode, int n, const char *file, int line);
  45. STATIC void tor_set_openssl_thread_id(CRYPTO_THREADID *threadid);
  46. #endif
  47. /* Returns a trimmed and human-readable version of an openssl version string
  48. * <b>raw_version</b>. They are usually in the form of 'OpenSSL 1.0.0b 10
  49. * May 2012' and this will parse them into a form similar to '1.0.0b' */
  50. STATIC char *
  51. parse_openssl_version_str(const char *raw_version)
  52. {
  53. const char *end_of_version = NULL;
  54. /* The output should be something like "OpenSSL 1.0.0b 10 May 2012. Let's
  55. trim that down. */
  56. if (!strcmpstart(raw_version, "OpenSSL ")) {
  57. raw_version += strlen("OpenSSL ");
  58. end_of_version = strchr(raw_version, ' ');
  59. }
  60. if (end_of_version)
  61. return tor_strndup(raw_version,
  62. end_of_version-raw_version);
  63. else
  64. return tor_strdup(raw_version);
  65. }
  66. static char *crypto_openssl_version_str = NULL;
  67. /* Return a human-readable version of the run-time openssl version number. */
  68. const char *
  69. crypto_openssl_get_version_str(void)
  70. {
  71. if (crypto_openssl_version_str == NULL) {
  72. const char *raw_version = OpenSSL_version(OPENSSL_VERSION);
  73. crypto_openssl_version_str = parse_openssl_version_str(raw_version);
  74. }
  75. return crypto_openssl_version_str;
  76. }
  77. static char *crypto_openssl_header_version_str = NULL;
  78. /* Return a human-readable version of the compile-time openssl version
  79. * number. */
  80. const char *
  81. crypto_openssl_get_header_version_str(void)
  82. {
  83. if (crypto_openssl_header_version_str == NULL) {
  84. crypto_openssl_header_version_str =
  85. parse_openssl_version_str(OPENSSL_VERSION_TEXT);
  86. }
  87. return crypto_openssl_header_version_str;
  88. }
  89. #ifndef OPENSSL_THREADS
  90. #error OpenSSL has been built without thread support. Tor requires an \
  91. OpenSSL library with thread support enabled.
  92. #endif
  93. #ifndef NEW_THREAD_API
  94. /** Helper: OpenSSL uses this callback to manipulate mutexes. */
  95. STATIC void
  96. openssl_locking_cb_(int mode, int n, const char *file, int line)
  97. {
  98. (void)file;
  99. (void)line;
  100. if (!openssl_mutexes_)
  101. /* This is not a really good fix for the
  102. * "release-freed-lock-from-separate-thread-on-shutdown" problem, but
  103. * it can't hurt. */
  104. return;
  105. if (mode & CRYPTO_LOCK)
  106. tor_mutex_acquire(openssl_mutexes_[n]);
  107. else
  108. tor_mutex_release(openssl_mutexes_[n]);
  109. }
  110. STATIC void
  111. tor_set_openssl_thread_id(CRYPTO_THREADID *threadid)
  112. {
  113. CRYPTO_THREADID_set_numeric(threadid, tor_get_thread_id());
  114. }
  115. #endif /* !defined(NEW_THREAD_API) */
  116. /** Helper: Construct mutexes, and set callbacks to help OpenSSL handle being
  117. * multithreaded. Returns 0. */
  118. int
  119. setup_openssl_threading(void)
  120. {
  121. #ifndef NEW_THREAD_API
  122. int i;
  123. int n = CRYPTO_num_locks();
  124. n_openssl_mutexes_ = n;
  125. openssl_mutexes_ = tor_calloc(n, sizeof(tor_mutex_t *));
  126. for (i=0; i < n; ++i)
  127. openssl_mutexes_[i] = tor_mutex_new();
  128. CRYPTO_set_locking_callback(openssl_locking_cb_);
  129. CRYPTO_THREADID_set_callback(tor_set_openssl_thread_id);
  130. #endif /* !defined(NEW_THREAD_API) */
  131. return 0;
  132. }
  133. /** free OpenSSL variables */
  134. static void
  135. crypto_openssl_free_all(void)
  136. {
  137. tor_free(crypto_openssl_version_str);
  138. tor_free(crypto_openssl_header_version_str);
  139. #ifndef NEW_THREAD_API
  140. if (n_openssl_mutexes_) {
  141. int n = n_openssl_mutexes_;
  142. tor_mutex_t **ms = openssl_mutexes_;
  143. int i;
  144. openssl_mutexes_ = NULL;
  145. n_openssl_mutexes_ = 0;
  146. for (i=0;i<n;++i) {
  147. tor_mutex_free(ms[i]);
  148. }
  149. tor_free(ms);
  150. }
  151. #endif /* !defined(NEW_THREAD_API) */
  152. }
  153. /** Perform early (pre-configuration) initialization tasks for OpenSSL. */
  154. void
  155. crypto_openssl_early_init(void)
  156. {
  157. #ifdef OPENSSL_1_1_API
  158. OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS |
  159. OPENSSL_INIT_LOAD_CRYPTO_STRINGS |
  160. OPENSSL_INIT_ADD_ALL_CIPHERS |
  161. OPENSSL_INIT_ADD_ALL_DIGESTS, NULL);
  162. #else
  163. ERR_load_crypto_strings();
  164. OpenSSL_add_all_algorithms();
  165. #endif
  166. setup_openssl_threading();
  167. unsigned long version_num = OpenSSL_version_num();
  168. const char *version_str = OpenSSL_version(OPENSSL_VERSION);
  169. if (version_num == OPENSSL_VERSION_NUMBER &&
  170. !strcmp(version_str, OPENSSL_VERSION_TEXT)) {
  171. log_info(LD_CRYPTO, "OpenSSL version matches version from headers "
  172. "(%lx: %s).", version_num, version_str);
  173. } else {
  174. log_warn(LD_CRYPTO, "OpenSSL version from headers does not match the "
  175. "version we're running with. If you get weird crashes, that "
  176. "might be why. (Compiled with %lx: %s; running with %lx: %s).",
  177. (unsigned long)OPENSSL_VERSION_NUMBER, OPENSSL_VERSION_TEXT,
  178. version_num, version_str);
  179. }
  180. crypto_force_rand_ssleay();
  181. }
  182. #ifndef DISABLE_ENGINES
  183. /** Try to load an engine in a shared library via fully qualified path.
  184. */
  185. static ENGINE *
  186. try_load_engine(const char *path, const char *engine)
  187. {
  188. ENGINE *e = ENGINE_by_id("dynamic");
  189. if (e) {
  190. if (!ENGINE_ctrl_cmd_string(e, "ID", engine, 0) ||
  191. !ENGINE_ctrl_cmd_string(e, "DIR_LOAD", "2", 0) ||
  192. !ENGINE_ctrl_cmd_string(e, "DIR_ADD", path, 0) ||
  193. !ENGINE_ctrl_cmd_string(e, "LOAD", NULL, 0)) {
  194. ENGINE_free(e);
  195. e = NULL;
  196. }
  197. }
  198. return e;
  199. }
  200. #endif /* !defined(DISABLE_ENGINES) */
  201. #ifndef DISABLE_ENGINES
  202. /** Log any OpenSSL engines we're using at NOTICE. */
  203. static void
  204. log_engine(const char *fn, ENGINE *e)
  205. {
  206. if (e) {
  207. const char *name, *id;
  208. name = ENGINE_get_name(e);
  209. id = ENGINE_get_id(e);
  210. log_notice(LD_CRYPTO, "Default OpenSSL engine for %s is %s [%s]",
  211. fn, name?name:"?", id?id:"?");
  212. } else {
  213. log_info(LD_CRYPTO, "Using default implementation for %s", fn);
  214. }
  215. }
  216. #endif /* !defined(DISABLE_ENGINES) */
  217. /** Initialize engines for openssl (if enabled). */
  218. static void
  219. crypto_openssl_init_engines(const char *accelName,
  220. const char *accelDir)
  221. {
  222. #ifdef DISABLE_ENGINES
  223. (void)accelName;
  224. (void)accelDir;
  225. log_warn(LD_CRYPTO, "No OpenSSL hardware acceleration support enabled.");
  226. #else
  227. ENGINE *e = NULL;
  228. log_info(LD_CRYPTO, "Initializing OpenSSL engine support.");
  229. ENGINE_load_builtin_engines();
  230. ENGINE_register_all_complete();
  231. if (accelName) {
  232. if (accelDir) {
  233. log_info(LD_CRYPTO, "Trying to load dynamic OpenSSL engine \"%s\""
  234. " via path \"%s\".", accelName, accelDir);
  235. e = try_load_engine(accelName, accelDir);
  236. } else {
  237. log_info(LD_CRYPTO, "Initializing dynamic OpenSSL engine \"%s\""
  238. " acceleration support.", accelName);
  239. e = ENGINE_by_id(accelName);
  240. }
  241. if (!e) {
  242. log_warn(LD_CRYPTO, "Unable to load dynamic OpenSSL engine \"%s\".",
  243. accelName);
  244. } else {
  245. log_info(LD_CRYPTO, "Loaded dynamic OpenSSL engine \"%s\".",
  246. accelName);
  247. }
  248. }
  249. if (e) {
  250. log_info(LD_CRYPTO, "Loaded OpenSSL hardware acceleration engine,"
  251. " setting default ciphers.");
  252. ENGINE_set_default(e, ENGINE_METHOD_ALL);
  253. }
  254. /* Log, if available, the intersection of the set of algorithms
  255. used by Tor and the set of algorithms available in the engine */
  256. log_engine("RSA", ENGINE_get_default_RSA());
  257. log_engine("DH", ENGINE_get_default_DH());
  258. #ifdef OPENSSL_1_1_API
  259. log_engine("EC", ENGINE_get_default_EC());
  260. #else
  261. log_engine("ECDH", ENGINE_get_default_ECDH());
  262. log_engine("ECDSA", ENGINE_get_default_ECDSA());
  263. #endif /* defined(OPENSSL_1_1_API) */
  264. log_engine("RAND", ENGINE_get_default_RAND());
  265. log_engine("RAND (which we will not use)", ENGINE_get_default_RAND());
  266. log_engine("SHA1", ENGINE_get_digest_engine(NID_sha1));
  267. log_engine("3DES-CBC", ENGINE_get_cipher_engine(NID_des_ede3_cbc));
  268. log_engine("AES-128-ECB", ENGINE_get_cipher_engine(NID_aes_128_ecb));
  269. log_engine("AES-128-CBC", ENGINE_get_cipher_engine(NID_aes_128_cbc));
  270. #ifdef NID_aes_128_ctr
  271. log_engine("AES-128-CTR", ENGINE_get_cipher_engine(NID_aes_128_ctr));
  272. #endif
  273. #ifdef NID_aes_128_gcm
  274. log_engine("AES-128-GCM", ENGINE_get_cipher_engine(NID_aes_128_gcm));
  275. #endif
  276. log_engine("AES-256-CBC", ENGINE_get_cipher_engine(NID_aes_256_cbc));
  277. #ifdef NID_aes_256_gcm
  278. log_engine("AES-256-GCM", ENGINE_get_cipher_engine(NID_aes_256_gcm));
  279. #endif
  280. #endif /* defined(DISABLE_ENGINES) */
  281. }
  282. /** Perform late (post-init) initialization tasks for OpenSSL */
  283. int
  284. crypto_openssl_late_init(int useAccel, const char *accelName,
  285. const char *accelDir)
  286. {
  287. if (useAccel > 0) {
  288. crypto_openssl_init_engines(accelName, accelDir);
  289. } else {
  290. log_info(LD_CRYPTO, "NOT using OpenSSL engine support.");
  291. }
  292. if (crypto_force_rand_ssleay()) {
  293. if (crypto_seed_rng() < 0)
  294. return -1;
  295. }
  296. evaluate_evp_for_aes(-1);
  297. evaluate_ctr_for_aes();
  298. return 0;
  299. }
  300. /** Clean up global resources held by openssl. */
  301. void
  302. crypto_openssl_global_cleanup(void)
  303. {
  304. #ifndef OPENSSL_1_1_API
  305. EVP_cleanup();
  306. #endif
  307. #ifndef NEW_THREAD_API
  308. ERR_remove_thread_state(NULL);
  309. #endif
  310. #ifndef OPENSSL_1_1_API
  311. ERR_free_strings();
  312. #endif
  313. #ifndef DISABLE_ENGINES
  314. #ifndef OPENSSL_1_1_API
  315. ENGINE_cleanup();
  316. #endif
  317. #endif
  318. CONF_modules_unload(1);
  319. #ifndef OPENSSL_1_1_API
  320. CRYPTO_cleanup_all_ex_data();
  321. #endif
  322. crypto_openssl_free_all();
  323. }