crypto_openssl_mgt.c 4.2 KB

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