crypto_openssl_mgt.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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.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/string/util_string.h"
  14. DISABLE_GCC_WARNING(redundant-decls)
  15. #include <openssl/err.h>
  16. #include <openssl/rsa.h>
  17. #include <openssl/pem.h>
  18. #include <openssl/evp.h>
  19. #include <openssl/engine.h>
  20. #include <openssl/rand.h>
  21. #include <openssl/bn.h>
  22. #include <openssl/dh.h>
  23. #include <openssl/conf.h>
  24. #include <openssl/hmac.h>
  25. #include <openssl/crypto.h>
  26. ENABLE_GCC_WARNING(redundant-decls)
  27. #ifndef NEW_THREAD_API
  28. /** A number of preallocated mutexes for use by OpenSSL. */
  29. static tor_mutex_t **openssl_mutexes_ = NULL;
  30. /** How many mutexes have we allocated for use by OpenSSL? */
  31. static int n_openssl_mutexes_ = 0;
  32. #endif /* !defined(NEW_THREAD_API) */
  33. /** Declare STATIC functions */
  34. STATIC char * parse_openssl_version_str(const char *raw_version);
  35. #ifndef NEW_THREAD_API
  36. STATIC void openssl_locking_cb_(int mode, int n, const char *file, int line);
  37. STATIC void tor_set_openssl_thread_id(CRYPTO_THREADID *threadid);
  38. #endif
  39. /* Returns a trimmed and human-readable version of an openssl version string
  40. * <b>raw_version</b>. They are usually in the form of 'OpenSSL 1.0.0b 10
  41. * May 2012' and this will parse them into a form similar to '1.0.0b' */
  42. STATIC char *
  43. parse_openssl_version_str(const char *raw_version)
  44. {
  45. const char *end_of_version = NULL;
  46. /* The output should be something like "OpenSSL 1.0.0b 10 May 2012. Let's
  47. trim that down. */
  48. if (!strcmpstart(raw_version, "OpenSSL ")) {
  49. raw_version += strlen("OpenSSL ");
  50. end_of_version = strchr(raw_version, ' ');
  51. }
  52. if (end_of_version)
  53. return tor_strndup(raw_version,
  54. end_of_version-raw_version);
  55. else
  56. return tor_strdup(raw_version);
  57. }
  58. static char *crypto_openssl_version_str = NULL;
  59. /* Return a human-readable version of the run-time openssl version number. */
  60. const char *
  61. crypto_openssl_get_version_str(void)
  62. {
  63. if (crypto_openssl_version_str == NULL) {
  64. const char *raw_version = OpenSSL_version(OPENSSL_VERSION);
  65. crypto_openssl_version_str = parse_openssl_version_str(raw_version);
  66. }
  67. return crypto_openssl_version_str;
  68. }
  69. static char *crypto_openssl_header_version_str = NULL;
  70. /* Return a human-readable version of the compile-time openssl version
  71. * number. */
  72. const char *
  73. crypto_openssl_get_header_version_str(void)
  74. {
  75. if (crypto_openssl_header_version_str == NULL) {
  76. crypto_openssl_header_version_str =
  77. parse_openssl_version_str(OPENSSL_VERSION_TEXT);
  78. }
  79. return crypto_openssl_header_version_str;
  80. }
  81. #ifndef OPENSSL_THREADS
  82. #error OpenSSL has been built without thread support. Tor requires an \
  83. OpenSSL library with thread support enabled.
  84. #endif
  85. #ifndef NEW_THREAD_API
  86. /** Helper: OpenSSL uses this callback to manipulate mutexes. */
  87. STATIC void
  88. openssl_locking_cb_(int mode, int n, const char *file, int line)
  89. {
  90. (void)file;
  91. (void)line;
  92. if (!openssl_mutexes_)
  93. /* This is not a really good fix for the
  94. * "release-freed-lock-from-separate-thread-on-shutdown" problem, but
  95. * it can't hurt. */
  96. return;
  97. if (mode & CRYPTO_LOCK)
  98. tor_mutex_acquire(openssl_mutexes_[n]);
  99. else
  100. tor_mutex_release(openssl_mutexes_[n]);
  101. }
  102. STATIC void
  103. tor_set_openssl_thread_id(CRYPTO_THREADID *threadid)
  104. {
  105. CRYPTO_THREADID_set_numeric(threadid, tor_get_thread_id());
  106. }
  107. #endif /* !defined(NEW_THREAD_API) */
  108. /** Helper: Construct mutexes, and set callbacks to help OpenSSL handle being
  109. * multithreaded. Returns 0. */
  110. int
  111. setup_openssl_threading(void)
  112. {
  113. #ifndef NEW_THREAD_API
  114. int i;
  115. int n = CRYPTO_num_locks();
  116. n_openssl_mutexes_ = n;
  117. openssl_mutexes_ = tor_calloc(n, sizeof(tor_mutex_t *));
  118. for (i=0; i < n; ++i)
  119. openssl_mutexes_[i] = tor_mutex_new();
  120. CRYPTO_set_locking_callback(openssl_locking_cb_);
  121. CRYPTO_THREADID_set_callback(tor_set_openssl_thread_id);
  122. #endif /* !defined(NEW_THREAD_API) */
  123. return 0;
  124. }
  125. /** free OpenSSL variables */
  126. void
  127. crypto_openssl_free_all(void)
  128. {
  129. tor_free(crypto_openssl_version_str);
  130. tor_free(crypto_openssl_header_version_str);
  131. #ifndef NEW_THREAD_API
  132. if (n_openssl_mutexes_) {
  133. int n = n_openssl_mutexes_;
  134. tor_mutex_t **ms = openssl_mutexes_;
  135. int i;
  136. openssl_mutexes_ = NULL;
  137. n_openssl_mutexes_ = 0;
  138. for (i=0;i<n;++i) {
  139. tor_mutex_free(ms[i]);
  140. }
  141. tor_free(ms);
  142. }
  143. #endif /* !defined(NEW_THREAD_API) */
  144. }