| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "base/openssl_util.h" | 5 #include "base/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 | 9 |
| 10 #include "base/lock.h" | 10 #include "base/lock.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/scoped_vector.h" | 12 #include "base/scoped_vector.h" |
| 13 #include "base/singleton.h" | 13 #include "base/singleton.h" |
| 14 #include "base/string_piece.h" | 14 #include "base/string_piece.h" |
| 15 | 15 |
| 16 namespace base { | 16 namespace base { |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 unsigned long CurrentThreadId() { | 20 unsigned long CurrentThreadId() { |
| 21 return static_cast<unsigned long>(PlatformThread::CurrentId()); | 21 return static_cast<unsigned long>(PlatformThread::CurrentId()); |
| 22 } | 22 } |
| 23 | 23 |
| 24 // Singleton for initializing and cleaning up the OpenSSL library. | 24 // Singleton for initializing and cleaning up the OpenSSL library. |
| 25 class OpenSSLInitSingleton { | 25 class OpenSSLInitSingleton { |
| 26 public: | 26 public: |
| 27 static OpenSSLInitSingleton* Get() { | 27 static OpenSSLInitSingleton* GetInstance() { |
| 28 // We allow the SSL environment to leak for multiple reasons: | 28 // We allow the SSL environment to leak for multiple reasons: |
| 29 // - it is used from a non-joinable worker thread that is not stopped on | 29 // - it is used from a non-joinable worker thread that is not stopped on |
| 30 // shutdown, hence may still be using OpenSSL library after the AtExit | 30 // shutdown, hence may still be using OpenSSL library after the AtExit |
| 31 // runner has completed. | 31 // runner has completed. |
| 32 // - There are other OpenSSL related singletons (e.g. the client socket | 32 // - There are other OpenSSL related singletons (e.g. the client socket |
| 33 // context) who's cleanup depends on the global environment here, but | 33 // context) who's cleanup depends on the global environment here, but |
| 34 // we can't control the order the AtExit handlers will run in so | 34 // we can't control the order the AtExit handlers will run in so |
| 35 // allowing the global environment to leak at least ensures it is | 35 // allowing the global environment to leak at least ensures it is |
| 36 // available for those other singletons to reliably cleanup. | 36 // available for those other singletons to reliably cleanup. |
| 37 return Singleton<OpenSSLInitSingleton, | 37 return Singleton<OpenSSLInitSingleton, |
| (...skipping 13 matching lines...) Expand all Loading... |
| 51 CRYPTO_set_id_callback(CurrentThreadId); | 51 CRYPTO_set_id_callback(CurrentThreadId); |
| 52 } | 52 } |
| 53 | 53 |
| 54 ~OpenSSLInitSingleton() { | 54 ~OpenSSLInitSingleton() { |
| 55 CRYPTO_set_locking_callback(NULL); | 55 CRYPTO_set_locking_callback(NULL); |
| 56 EVP_cleanup(); | 56 EVP_cleanup(); |
| 57 ERR_free_strings(); | 57 ERR_free_strings(); |
| 58 } | 58 } |
| 59 | 59 |
| 60 static void LockingCallback(int mode, int n, const char* file, int line) { | 60 static void LockingCallback(int mode, int n, const char* file, int line) { |
| 61 OpenSSLInitSingleton::Get()->OnLockingCallback(mode, n, file, line); | 61 OpenSSLInitSingleton::GetInstance()->OnLockingCallback(mode, n, file, line); |
| 62 } | 62 } |
| 63 | 63 |
| 64 void OnLockingCallback(int mode, int n, const char* file, int line) { | 64 void OnLockingCallback(int mode, int n, const char* file, int line) { |
| 65 CHECK_LT(static_cast<size_t>(n), locks_.size()); | 65 CHECK_LT(static_cast<size_t>(n), locks_.size()); |
| 66 if (mode & CRYPTO_LOCK) | 66 if (mode & CRYPTO_LOCK) |
| 67 locks_[n]->Acquire(); | 67 locks_[n]->Acquire(); |
| 68 else | 68 else |
| 69 locks_[n]->Release(); | 69 locks_[n]->Release(); |
| 70 } | 70 } |
| 71 | 71 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 85 // error queue and return, otherwise it will continue calling this function | 85 // error queue and return, otherwise it will continue calling this function |
| 86 // until all errors have been removed from the queue. | 86 // until all errors have been removed from the queue. |
| 87 int OpenSSLErrorCallback(const char* str, size_t len, void* context) { | 87 int OpenSSLErrorCallback(const char* str, size_t len, void* context) { |
| 88 DVLOG(1) << "\t" << StringPiece(str, len); | 88 DVLOG(1) << "\t" << StringPiece(str, len); |
| 89 return 1; | 89 return 1; |
| 90 } | 90 } |
| 91 | 91 |
| 92 } // namespace | 92 } // namespace |
| 93 | 93 |
| 94 void EnsureOpenSSLInit() { | 94 void EnsureOpenSSLInit() { |
| 95 (void)OpenSSLInitSingleton::Get(); | 95 (void)OpenSSLInitSingleton::GetInstance(); |
| 96 } | 96 } |
| 97 | 97 |
| 98 void ClearOpenSSLERRStack(const tracked_objects::Location& location) { | 98 void ClearOpenSSLERRStack(const tracked_objects::Location& location) { |
| 99 if (logging::DEBUG_MODE && VLOG_IS_ON(1)) { | 99 if (logging::DEBUG_MODE && VLOG_IS_ON(1)) { |
| 100 int error_num = ERR_peek_error(); | 100 int error_num = ERR_peek_error(); |
| 101 if (error_num == 0) | 101 if (error_num == 0) |
| 102 return; | 102 return; |
| 103 | 103 |
| 104 std::string message; | 104 std::string message; |
| 105 location.Write(true, true, &message); | 105 location.Write(true, true, &message); |
| 106 DVLOG(1) << "OpenSSL ERR_get_error stack from " << message; | 106 DVLOG(1) << "OpenSSL ERR_get_error stack from " << message; |
| 107 ERR_print_errors_cb(&OpenSSLErrorCallback, NULL); | 107 ERR_print_errors_cb(&OpenSSLErrorCallback, NULL); |
| 108 } else { | 108 } else { |
| 109 ERR_clear_error(); | 109 ERR_clear_error(); |
| 110 } | 110 } |
| 111 } | 111 } |
| 112 | 112 |
| 113 } // namespace base | 113 } // namespace base |
| OLD | NEW |