crypto_openssl_mgt.c 11 KB

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