Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 <openssl/ssl.h> | 5 #include <openssl/ssl.h> |
| 6 | 6 |
| 7 #include "base/lock.h" | 7 #include "base/lock.h" |
| 8 #include "base/scoped_vector.h" | 8 #include "base/scoped_vector.h" |
| 9 #include "base/singleton.h" | 9 #include "base/singleton.h" |
| 10 | 10 |
| 11 namespace net { | 11 namespace net { |
| 12 | 12 |
| 13 // A helper class that takes care of destroying OpenSSL objects when it goes out | 13 // A helper class that takes care of destroying OpenSSL objects when it goes out |
| 14 // of scope. | 14 // of scope. |
| 15 template <typename T, void (*destructor)(T*)> | 15 template <typename T, void (*destructor)(T*)> |
| 16 class ScopedSSL { | 16 class ScopedSSL { |
| 17 public: | 17 public: |
| 18 explicit ScopedSSL(T* ptr_) : ptr_(ptr_) { } | 18 explicit ScopedSSL(T* ptr_) : ptr_(ptr_) { } |
| 19 ~ScopedSSL() { if (ptr_) (*destructor)(ptr_); } | 19 ~ScopedSSL() { if (ptr_) (*destructor)(ptr_); } |
| 20 | 20 |
| 21 void reset(T* p = NULL) { | |
| 22 if (ptr_ != p) { | |
| 23 (*destructor)(ptr_); | |
| 24 ptr_ = p; | |
| 25 } | |
| 26 } | |
| 27 | |
| 21 T* get() const { return ptr_; } | 28 T* get() const { return ptr_; } |
| 22 | 29 |
| 23 private: | 30 private: |
| 24 T* ptr_; | 31 T* ptr_; |
| 25 }; | 32 }; |
| 26 | 33 |
| 27 // Singleton for initializing / cleaning up OpenSSL and holding a X509 store. | 34 // Singleton for initializing / cleaning up OpenSSL and holding a X509 store. |
| 28 // Access it via GetOpenSSLInitSingleton(). | 35 // Access it via GetOpenSSLInitSingleton(). |
| 29 class OpenSSLInitSingleton { | 36 class OpenSSLInitSingleton { |
| 30 public: | 37 public: |
| 31 SSL_CTX* ssl_ctx() const { return ssl_ctx_.get(); } | 38 SSL_CTX* ssl_ctx() const { return ssl_ctx_.get(); } |
| 32 X509_STORE* x509_store() const { return store_.get(); } | 39 X509_STORE* x509_store() const { return store_.get(); } |
| 33 | 40 |
| 34 private: | 41 private: |
| 35 friend struct DefaultSingletonTraits<OpenSSLInitSingleton>; | 42 friend struct DefaultSingletonTraits<OpenSSLInitSingleton>; |
| 43 friend class TestRootCerts; // For unit tests. | |
| 36 OpenSSLInitSingleton(); | 44 OpenSSLInitSingleton(); |
| 37 ~OpenSSLInitSingleton(); | 45 ~OpenSSLInitSingleton(); |
| 38 | 46 |
| 39 static void LockingCallback(int mode, int n, const char* file, int line); | 47 static void LockingCallback(int mode, int n, const char* file, int line); |
| 40 void OnLockingCallback(int mode, int n, const char* file, int line); | 48 void OnLockingCallback(int mode, int n, const char* file, int line); |
| 41 | 49 |
| 50 // (Re-)initializes |store_| to the default state. This is used to revert | |
| 51 // any modifications that TestRootCerts may have done, by initializing | |
|
wtc
2010/11/18 02:12:49
Nit: remove the parentheses in "(Re-)initializes".
| |
| 52 // |store_| to the default state. | |
| 53 void ReinitializeStore(); | |
| 54 | |
| 42 ScopedSSL<SSL_CTX, SSL_CTX_free> ssl_ctx_; | 55 ScopedSSL<SSL_CTX, SSL_CTX_free> ssl_ctx_; |
| 43 ScopedSSL<X509_STORE, X509_STORE_free> store_; | 56 ScopedSSL<X509_STORE, X509_STORE_free> store_; |
| 44 // These locks are used and managed by OpenSSL via LockingCallback(). | 57 // These locks are used and managed by OpenSSL via LockingCallback(). |
| 45 ScopedVector<Lock> locks_; | 58 ScopedVector<Lock> locks_; |
| 46 | 59 |
| 47 DISALLOW_COPY_AND_ASSIGN(OpenSSLInitSingleton); | 60 DISALLOW_COPY_AND_ASSIGN(OpenSSLInitSingleton); |
| 48 }; | 61 }; |
| 49 | 62 |
| 50 OpenSSLInitSingleton* GetOpenSSLInitSingleton(); | 63 OpenSSLInitSingleton* GetOpenSSLInitSingleton(); |
| 51 | 64 |
| 52 // Initialize OpenSSL if it isn't already initialized. This must be called | 65 // Initialize OpenSSL if it isn't already initialized. This must be called |
| 53 // before any other OpenSSL functions (except GetOpenSSLInitSingleton above). | 66 // before any other OpenSSL functions (except GetOpenSSLInitSingleton above). |
| 54 // This function is thread-safe, and OpenSSL will only ever be initialized once. | 67 // This function is thread-safe, and OpenSSL will only ever be initialized once. |
| 55 // OpenSSL will be properly shut down on program exit. | 68 // OpenSSL will be properly shut down on program exit. |
| 56 void EnsureOpenSSLInit(); | 69 void EnsureOpenSSLInit(); |
| 57 | 70 |
| 58 } // namespace net | 71 } // namespace net |
| 59 | 72 |
| OLD | NEW |