OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include <openssl/ssl.h> | |
6 | |
7 #include "base/lock.h" | |
8 #include "base/scoped_vector.h" | |
9 #include "base/singleton.h" | |
10 | |
11 namespace net { | |
12 | |
13 // A helper class that takes care of destroying OpenSSL objects when it goes out | |
14 // of scope. | |
15 template <typename T, void (*destructor)(T*)> | |
16 class ScopedSSL { | |
17 public: | |
18 explicit ScopedSSL(T* ptr_) : ptr_(ptr_) { } | |
19 ~ScopedSSL() { if (ptr_) (*destructor)(ptr_); } | |
20 | |
21 T* get() const { return ptr_; } | |
22 | |
23 private: | |
24 T* ptr_; | |
25 }; | |
26 | |
27 // Singleton for initializing / cleaning up OpenSSL and holding a X509 store. | |
28 // Access it via GetOpenSSLInitSingleton(). | |
29 class OpenSSLInitSingleton { | |
30 public: | |
31 SSL_CTX* ssl_ctx() const { return ssl_ctx_.get(); } | |
32 X509_STORE* x509_store() const { return store_.get(); } | |
33 | |
34 private: | |
35 friend struct DefaultSingletonTraits<OpenSSLInitSingleton>; | |
36 OpenSSLInitSingleton(); | |
37 ~OpenSSLInitSingleton(); | |
38 | |
39 static void LockingCallback(int mode, int n, const char* file, int line); | |
40 void OnLockingCallback(int mode, int n, const char* file, int line); | |
41 | |
42 ScopedSSL<SSL_CTX, SSL_CTX_free> ssl_ctx_; | |
43 ScopedSSL<X509_STORE, X509_STORE_free> store_; | |
44 // These locks are used and managed by OpenSSL via LockingCallback(). | |
45 ScopedVector<Lock> locks_; | |
46 | |
47 DISALLOW_COPY_AND_ASSIGN(OpenSSLInitSingleton); | |
48 }; | |
49 | |
50 OpenSSLInitSingleton* GetOpenSSLInitSingleton(); | |
51 | |
52 // Initialize OpenSSL if it isn't already initialized. This must be called | |
53 // before any other OpenSSL functions (except GetOpenSSLInitSingleton above). | |
54 // This function is thread-safe, and OpenSSL will only ever be initialized once. | |
55 // OpenSSL will be properly shut down on program exit. | |
56 void EnsureOpenSSLInit(); | |
57 | |
58 } // namespace net | |
59 | |
OLD | NEW |