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 | 9 |
| 10 #include "base/lock.h" |
9 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/scoped_vector.h" |
| 13 #include "base/singleton.h" |
10 | 14 |
11 namespace base { | 15 namespace base { |
12 | 16 |
| 17 namespace { |
| 18 |
| 19 unsigned long CurrentThreadId() { |
| 20 return static_cast<unsigned long>(PlatformThread::CurrentId()); |
| 21 } |
| 22 |
| 23 // Singleton for initializing and cleaning up the OpenSSL library. |
| 24 class OpenSSLInitSingleton { |
| 25 private: |
| 26 friend struct DefaultSingletonTraits<OpenSSLInitSingleton>; |
| 27 OpenSSLInitSingleton() { |
| 28 SSL_load_error_strings(); |
| 29 SSL_library_init(); |
| 30 OpenSSL_add_all_algorithms(); |
| 31 int num_locks = CRYPTO_num_locks(); |
| 32 locks_.reserve(num_locks); |
| 33 for (int i = 0; i < num_locks; ++i) |
| 34 locks_.push_back(new Lock()); |
| 35 CRYPTO_set_locking_callback(LockingCallback); |
| 36 CRYPTO_set_id_callback(CurrentThreadId); |
| 37 } |
| 38 |
| 39 ~OpenSSLInitSingleton() { |
| 40 CRYPTO_set_locking_callback(NULL); |
| 41 EVP_cleanup(); |
| 42 ERR_free_strings(); |
| 43 } |
| 44 |
| 45 static void LockingCallback(int mode, int n, const char* file, int line) { |
| 46 Singleton<OpenSSLInitSingleton>::get()->OnLockingCallback(mode, n, file, |
| 47 line); |
| 48 } |
| 49 |
| 50 void OnLockingCallback(int mode, int n, const char* file, int line) { |
| 51 CHECK_LT(static_cast<size_t>(n), locks_.size()); |
| 52 if (mode & CRYPTO_LOCK) |
| 53 locks_[n]->Acquire(); |
| 54 else |
| 55 locks_[n]->Release(); |
| 56 } |
| 57 |
| 58 // These locks are used and managed by OpenSSL via LockingCallback(). |
| 59 ScopedVector<Lock> locks_; |
| 60 |
| 61 DISALLOW_COPY_AND_ASSIGN(OpenSSLInitSingleton); |
| 62 }; |
| 63 |
| 64 } // namespace |
| 65 |
| 66 void EnsureOpenSSLInit() { |
| 67 (void)Singleton<OpenSSLInitSingleton>::get(); |
| 68 } |
| 69 |
13 void ClearOpenSSLERRStack() { | 70 void ClearOpenSSLERRStack() { |
14 if (logging::DEBUG_MODE && VLOG_IS_ON(1)) { | 71 if (logging::DEBUG_MODE && VLOG_IS_ON(1)) { |
15 int error_num = ERR_get_error(); | 72 int error_num = ERR_get_error(); |
16 if (error_num == 0) | 73 if (error_num == 0) |
17 return; | 74 return; |
18 | 75 |
19 DVLOG(1) << "OpenSSL ERR_get_error stack:"; | 76 DVLOG(1) << "OpenSSL ERR_get_error stack:"; |
20 char buf[140]; | 77 char buf[140]; |
21 do { | 78 do { |
22 ERR_error_string_n(error_num, buf, arraysize(buf)); | 79 ERR_error_string_n(error_num, buf, arraysize(buf)); |
23 DVLOG(1) << "\t" << error_num << ": " << buf; | 80 DVLOG(1) << "\t" << error_num << ": " << buf; |
24 error_num = ERR_get_error(); | 81 error_num = ERR_get_error(); |
25 } while (error_num != 0); | 82 } while (error_num != 0); |
26 } else { | 83 } else { |
27 ERR_clear_error(); | 84 ERR_clear_error(); |
28 } | 85 } |
29 } | 86 } |
30 | 87 |
31 } // namespace base | 88 } // namespace base |
OLD | NEW |