crypto_openssl_mgt.c 3.8 KB

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