123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- #include "compat_openssl.h"
- #include "crypto_openssl_mgt.h"
- DISABLE_GCC_WARNING(redundant-decls)
- #include <openssl/err.h>
- #include <openssl/rsa.h>
- #include <openssl/pem.h>
- #include <openssl/evp.h>
- #include <openssl/engine.h>
- #include <openssl/rand.h>
- #include <openssl/bn.h>
- #include <openssl/dh.h>
- #include <openssl/conf.h>
- #include <openssl/hmac.h>
- #include <openssl/crypto.h>
- ENABLE_GCC_WARNING(redundant-decls)
- #ifndef NEW_THREAD_API
- static tor_mutex_t **openssl_mutexes_ = NULL;
- static int n_openssl_mutexes_ = 0;
- #endif
- STATIC char * parse_openssl_version_str(const char *raw_version);
- #ifndef NEW_THREAD_API
- STATIC void openssl_locking_cb_(int mode, int n, const char *file, int line);
- STATIC void tor_set_openssl_thread_id(CRYPTO_THREADID *threadid);
- #endif
- STATIC char *
- parse_openssl_version_str(const char *raw_version)
- {
- const char *end_of_version = NULL;
-
- if (!strcmpstart(raw_version, "OpenSSL ")) {
- raw_version += strlen("OpenSSL ");
- end_of_version = strchr(raw_version, ' ');
- }
- if (end_of_version)
- return tor_strndup(raw_version,
- end_of_version-raw_version);
- else
- return tor_strdup(raw_version);
- }
- char *crypto_openssl_version_str = NULL;
- const char *
- crypto_openssl_get_version_str(void)
- {
- if (crypto_openssl_version_str == NULL) {
- const char *raw_version = OpenSSL_version(OPENSSL_VERSION);
- crypto_openssl_version_str = parse_openssl_version_str(raw_version);
- }
- return crypto_openssl_version_str;
- }
- char *crypto_openssl_header_version_str = NULL;
- const char *
- crypto_openssl_get_header_version_str(void)
- {
- if (crypto_openssl_header_version_str == NULL) {
- crypto_openssl_header_version_str =
- parse_openssl_version_str(OPENSSL_VERSION_TEXT);
- }
- return crypto_openssl_header_version_str;
- }
- #ifndef OPENSSL_THREADS
- #error OpenSSL has been built without thread support. Tor requires an \
- OpenSSL library with thread support enabled.
- #endif
- #ifndef NEW_THREAD_API
- STATIC void
- openssl_locking_cb_(int mode, int n, const char *file, int line)
- {
- (void)file;
- (void)line;
- if (!openssl_mutexes_)
-
- return;
- if (mode & CRYPTO_LOCK)
- tor_mutex_acquire(openssl_mutexes_[n]);
- else
- tor_mutex_release(openssl_mutexes_[n]);
- }
- STATIC void
- tor_set_openssl_thread_id(CRYPTO_THREADID *threadid)
- {
- CRYPTO_THREADID_set_numeric(threadid, tor_get_thread_id());
- }
- #endif
- int
- setup_openssl_threading(void)
- {
- #ifndef NEW_THREAD_API
- int i;
- int n = CRYPTO_num_locks();
- n_openssl_mutexes_ = n;
- openssl_mutexes_ = tor_calloc(n, sizeof(tor_mutex_t *));
- for (i=0; i < n; ++i)
- openssl_mutexes_[i] = tor_mutex_new();
- CRYPTO_set_locking_callback(openssl_locking_cb_);
- CRYPTO_THREADID_set_callback(tor_set_openssl_thread_id);
- #endif
- return 0;
- }
- void
- crypto_openssl_free_all(void)
- {
- tor_free(crypto_openssl_version_str);
- tor_free(crypto_openssl_header_version_str);
- }
|