diff options
author | 2018-05-03 19:35:30 -0700 | |
---|---|---|
committer | 2018-05-03 19:35:30 -0700 | |
commit | 4e85a3aaf725238640fd59fbdb450cdedd5e47f8 (patch) | |
tree | 482b41fa155ac49162c12b6c458a8a2e5400da64 /src/core/tsi | |
parent | 84d44071b7ab13e7f58b09218365722d9ad36f0c (diff) | |
parent | 106f73fb1043515041072d2bda2ff5684cabdb1c (diff) |
Merge pull request #15143 from jiangtaoli2016/init_openssl
Init OpenSSL callbacks if it has not been initialized.
Diffstat (limited to 'src/core/tsi')
-rw-r--r-- | src/core/tsi/ssl_transport_security.cc | 33 |
1 files changed, 20 insertions, 13 deletions
diff --git a/src/core/tsi/ssl_transport_security.cc b/src/core/tsi/ssl_transport_security.cc index 0ba6587678..a2301be40a 100644 --- a/src/core/tsi/ssl_transport_security.cc +++ b/src/core/tsi/ssl_transport_security.cc @@ -120,12 +120,14 @@ typedef struct { /* --- Library Initialization. ---*/ static gpr_once g_init_openssl_once = GPR_ONCE_INIT; -static gpr_mu* g_openssl_mutexes = nullptr; static int g_ssl_ctx_ex_factory_index = -1; +static const unsigned char kSslSessionIdContext[] = {'g', 'r', 'p', 'c'}; + +#if OPENSSL_VERSION_NUMBER < 0x10100000 +static gpr_mu* g_openssl_mutexes = nullptr; static void openssl_locking_cb(int mode, int type, const char* file, int line) GRPC_UNUSED; static unsigned long openssl_thread_id_cb(void) GRPC_UNUSED; -static const unsigned char kSslSessionIdContext[] = {'g', 'r', 'p', 'c'}; static void openssl_locking_cb(int mode, int type, const char* file, int line) { if (mode & CRYPTO_LOCK) { @@ -138,22 +140,27 @@ static void openssl_locking_cb(int mode, int type, const char* file, int line) { static unsigned long openssl_thread_id_cb(void) { return static_cast<unsigned long>(gpr_thd_currentid()); } +#endif static void init_openssl(void) { - int i; - int num_locks; SSL_library_init(); SSL_load_error_strings(); OpenSSL_add_all_algorithms(); - num_locks = CRYPTO_num_locks(); - GPR_ASSERT(num_locks > 0); - g_openssl_mutexes = static_cast<gpr_mu*>( - gpr_malloc(static_cast<size_t>(num_locks) * sizeof(gpr_mu))); - for (i = 0; i < CRYPTO_num_locks(); i++) { - gpr_mu_init(&g_openssl_mutexes[i]); - } - CRYPTO_set_locking_callback(openssl_locking_cb); - CRYPTO_set_id_callback(openssl_thread_id_cb); +#if OPENSSL_VERSION_NUMBER < 0x10100000 + if (!CRYPTO_get_locking_callback()) { + int num_locks = CRYPTO_num_locks(); + GPR_ASSERT(num_locks > 0); + g_openssl_mutexes = static_cast<gpr_mu*>( + gpr_malloc(static_cast<size_t>(num_locks) * sizeof(gpr_mu))); + for (int i = 0; i < num_locks; i++) { + gpr_mu_init(&g_openssl_mutexes[i]); + } + CRYPTO_set_locking_callback(openssl_locking_cb); + CRYPTO_set_id_callback(openssl_thread_id_cb); + } else { + gpr_log(GPR_INFO, "OpenSSL callback has already been set."); + } +#endif g_ssl_ctx_ex_factory_index = SSL_CTX_get_ex_new_index(0, nullptr, nullptr, nullptr, nullptr); GPR_ASSERT(g_ssl_ctx_ex_factory_index != -1); |