| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "crypto/openssl_util.h" | 5 #include "crypto/openssl_util.h" |
| 6 | 6 |
| 7 #include <openssl/err.h> | 7 #include <openssl/err.h> |
| 8 #include <openssl/ssl.h> | 8 #include <openssl/ssl.h> |
| 9 #include <openssl/cpu.h> | |
| 10 | 9 |
| 11 #include "base/logging.h" | 10 #include "base/logging.h" |
| 12 #include "base/memory/scoped_vector.h" | 11 #include "base/memory/scoped_vector.h" |
| 13 #include "base/memory/singleton.h" | 12 #include "base/memory/singleton.h" |
| 14 #include "base/strings/string_piece.h" | 13 #include "base/strings/string_piece.h" |
| 15 #include "base/synchronization/lock.h" | 14 #include "base/synchronization/lock.h" |
| 16 #include "build/build_config.h" | 15 #include "build/build_config.h" |
| 17 | 16 |
| 18 #if defined(OS_ANDROID) && defined(ARCH_CPU_ARMEL) | 17 #if defined(OS_ANDROID) && defined(ARCH_CPU_ARMEL) |
| 19 #include <cpu-features.h> | 18 #include <cpu-features.h> |
| 20 #endif | 19 #endif |
| 21 | 20 |
| 22 namespace crypto { | 21 namespace crypto { |
| 23 | 22 |
| 24 namespace { | 23 namespace { |
| 25 | 24 |
| 26 void CurrentThreadId(CRYPTO_THREADID* id) { | 25 unsigned long CurrentThreadId() { |
| 27 CRYPTO_THREADID_set_numeric( | 26 return static_cast<unsigned long>(base::PlatformThread::CurrentId()); |
| 28 id, static_cast<unsigned long>(base::PlatformThread::CurrentId())); | |
| 29 } | 27 } |
| 30 | 28 |
| 31 // Singleton for initializing and cleaning up the OpenSSL library. | 29 // Singleton for initializing and cleaning up the OpenSSL library. |
| 32 class OpenSSLInitSingleton { | 30 class OpenSSLInitSingleton { |
| 33 public: | 31 public: |
| 34 static OpenSSLInitSingleton* GetInstance() { | 32 static OpenSSLInitSingleton* GetInstance() { |
| 35 // We allow the SSL environment to leak for multiple reasons: | 33 // We allow the SSL environment to leak for multiple reasons: |
| 36 // - it is used from a non-joinable worker thread that is not stopped on | 34 // - it is used from a non-joinable worker thread that is not stopped on |
| 37 // shutdown, hence may still be using OpenSSL library after the AtExit | 35 // shutdown, hence may still be using OpenSSL library after the AtExit |
| 38 // runner has completed. | 36 // runner has completed. |
| 39 // - There are other OpenSSL related singletons (e.g. the client socket | 37 // - There are other OpenSSL related singletons (e.g. the client socket |
| 40 // context) who's cleanup depends on the global environment here, but | 38 // context) who's cleanup depends on the global environment here, but |
| 41 // we can't control the order the AtExit handlers will run in so | 39 // we can't control the order the AtExit handlers will run in so |
| 42 // allowing the global environment to leak at least ensures it is | 40 // allowing the global environment to leak at least ensures it is |
| 43 // available for those other singletons to reliably cleanup. | 41 // available for those other singletons to reliably cleanup. |
| 44 return Singleton<OpenSSLInitSingleton, | 42 return Singleton<OpenSSLInitSingleton, |
| 45 LeakySingletonTraits<OpenSSLInitSingleton> >::get(); | 43 LeakySingletonTraits<OpenSSLInitSingleton> >::get(); |
| 46 } | 44 } |
| 47 private: | 45 private: |
| 48 friend struct DefaultSingletonTraits<OpenSSLInitSingleton>; | 46 friend struct DefaultSingletonTraits<OpenSSLInitSingleton>; |
| 49 OpenSSLInitSingleton() { | 47 OpenSSLInitSingleton() { |
| 50 SSL_load_error_strings(); | 48 SSL_load_error_strings(); |
| 51 SSL_library_init(); | 49 SSL_library_init(); |
| 52 OpenSSL_add_all_algorithms(); | 50 OpenSSL_add_all_algorithms(); |
| 53 int num_locks = CRYPTO_num_locks(); | 51 int num_locks = CRYPTO_num_locks(); |
| 54 locks_.reserve(num_locks); | 52 locks_.reserve(num_locks); |
| 55 for (int i = 0; i < num_locks; ++i) | 53 for (int i = 0; i < num_locks; ++i) |
| 56 locks_.push_back(new base::Lock()); | 54 locks_.push_back(new base::Lock()); |
| 57 CRYPTO_set_locking_callback(LockingCallback); | 55 CRYPTO_set_locking_callback(LockingCallback); |
| 58 CRYPTO_THREADID_set_callback(CurrentThreadId); | 56 CRYPTO_set_id_callback(CurrentThreadId); |
| 59 | 57 |
| 60 #if defined(OS_ANDROID) && defined(ARCH_CPU_ARMEL) | 58 #if defined(OS_ANDROID) && defined(ARCH_CPU_ARMEL) |
| 61 const bool has_neon = | 59 const bool has_neon = |
| 62 (android_getCpuFeatures() & ANDROID_CPU_ARM_FEATURE_NEON) != 0; | 60 (android_getCpuFeatures() & ANDROID_CPU_ARM_FEATURE_NEON) != 0; |
| 63 if (has_neon) | 61 if (has_neon) |
| 64 CRYPTO_set_NEON_capable(1); | 62 CRYPTO_set_NEON_capable(1); |
| 65 #endif | 63 #endif |
| 66 } | 64 } |
| 67 | 65 |
| 68 ~OpenSSLInitSingleton() { | 66 ~OpenSSLInitSingleton() { |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 std::string message; | 116 std::string message; |
| 119 location.Write(true, true, &message); | 117 location.Write(true, true, &message); |
| 120 DVLOG(1) << "OpenSSL ERR_get_error stack from " << message; | 118 DVLOG(1) << "OpenSSL ERR_get_error stack from " << message; |
| 121 ERR_print_errors_cb(&OpenSSLErrorCallback, NULL); | 119 ERR_print_errors_cb(&OpenSSLErrorCallback, NULL); |
| 122 } else { | 120 } else { |
| 123 ERR_clear_error(); | 121 ERR_clear_error(); |
| 124 } | 122 } |
| 125 } | 123 } |
| 126 | 124 |
| 127 } // namespace crypto | 125 } // namespace crypto |
| OLD | NEW |