OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #ifndef NET_CERT_CERT_DATABASE_H_ | 5 #ifndef NET_CERT_CERT_DATABASE_H_ |
6 #define NET_CERT_CERT_DATABASE_H_ | 6 #define NET_CERT_CERT_DATABASE_H_ |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "net/base/net_export.h" | 11 #include "net/base/net_export.h" |
12 #include "net/cert/x509_certificate.h" | 12 #include "net/cert/x509_certificate.h" |
13 | 13 |
14 template <typename T> struct DefaultSingletonTraits; | 14 template <typename T> struct DefaultSingletonTraits; |
15 template <class ObserverType> class ObserverListThreadSafe; | 15 template <class ObserverType> class ObserverListThreadSafe; |
16 | 16 |
17 namespace net { | 17 namespace net { |
18 | 18 |
19 // This class provides cross-platform functions to verify and add user | 19 class NET_EXPORT CertDatabaseSource { |
20 // certificates, and to observe changes to the underlying certificate stores. | |
21 | |
22 // TODO(gauravsh): This class could be augmented with methods | |
23 // for all operations that manipulate the underlying system | |
24 // certificate store. | |
25 | |
26 class NET_EXPORT CertDatabase { | |
27 public: | 20 public: |
28 // A CertDatabase::Observer will be notified on certificate database changes. | 21 // A CertDatabase::Observer will be notified on certificate database changes. |
29 // The change could be either a new user certificate is added or trust on | 22 // The change could be either a new user certificate is added or trust on |
30 // a certificate is changed. Observers can register themselves | 23 // a certificate is changed. Observers can register themselves |
31 // via CertDatabase::AddObserver, and can un-register with | 24 // via CertDatabase::AddObserver, and can un-register with |
32 // CertDatabase::RemoveObserver. | 25 // CertDatabase::RemoveObserver. |
33 class NET_EXPORT Observer { | 26 class NET_EXPORT Observer { |
34 public: | 27 public: |
35 virtual ~Observer() {} | 28 virtual ~Observer() {} |
36 | 29 |
37 // Will be called when a new certificate is added. | 30 // Will be called when a new certificate is added. |
38 virtual void OnCertAdded(const X509Certificate* cert) {} | 31 virtual void OnCertAdded(const X509Certificate* cert) {} |
39 | 32 |
40 // Will be called when a certificate is removed. | 33 // Will be called when a certificate is removed. |
41 virtual void OnCertRemoved(const X509Certificate* cert) {} | 34 virtual void OnCertRemoved(const X509Certificate* cert) {} |
42 | 35 |
43 // Will be called when a CA certificate was added, removed, or its trust | 36 // Will be called when a CA certificate was added, removed, or its trust |
44 // changed. This can also mean that a client certificate's trust changed. | 37 // changed. This can also mean that a client certificate's trust changed. |
45 virtual void OnCACertChanged(const X509Certificate* cert) {} | 38 virtual void OnCACertChanged(const X509Certificate* cert) {} |
46 | 39 |
47 protected: | 40 protected: |
48 Observer() {} | 41 Observer() {} |
49 | 42 |
50 private: | 43 private: |
51 DISALLOW_COPY_AND_ASSIGN(Observer); | 44 DISALLOW_COPY_AND_ASSIGN(Observer); |
52 }; | 45 }; |
53 | 46 |
| 47 // Registers |observer| to receive notifications of certificate changes. The |
| 48 // thread on which this is called is the thread on which |observer| will be |
| 49 // called back with notifications. |
| 50 virtual void AddObserver(Observer* observer) = 0; |
| 51 |
| 52 // Unregisters |observer| from receiving notifications. This must be called |
| 53 // on the same thread on which AddObserver() was called. |
| 54 virtual void RemoveObserver(Observer* observer) = 0; |
| 55 }; |
| 56 |
| 57 // This class provides cross-platform functions to verify and add user |
| 58 // certificates, and to observe changes to the underlying certificate stores. |
| 59 |
| 60 // TODO(gauravsh): This class could be augmented with methods |
| 61 // for all operations that manipulate the underlying system |
| 62 // certificate store. |
| 63 |
| 64 class NET_EXPORT CertDatabase : public CertDatabaseSource { |
| 65 public: |
54 // Returns the CertDatabase singleton. | 66 // Returns the CertDatabase singleton. |
55 static CertDatabase* GetInstance(); | 67 static CertDatabase* GetInstance(); |
56 | 68 |
57 // Check whether this is a valid user cert that we have the private key for. | 69 // Check whether this is a valid user cert that we have the private key for. |
58 // Returns OK or a network error code such as ERR_CERT_CONTAINS_ERRORS. | 70 // Returns OK or a network error code such as ERR_CERT_CONTAINS_ERRORS. |
59 int CheckUserCert(X509Certificate* cert); | 71 int CheckUserCert(X509Certificate* cert); |
60 | 72 |
61 // Store user (client) certificate. Assumes CheckUserCert has already passed. | 73 // Store user (client) certificate. Assumes CheckUserCert has already passed. |
62 // Returns OK, or ERR_ADD_USER_CERT_FAILED if there was a problem saving to | 74 // Returns OK, or ERR_ADD_USER_CERT_FAILED if there was a problem saving to |
63 // the platform cert database, or possibly other network error codes. | 75 // the platform cert database, or possibly other network error codes. |
64 int AddUserCert(X509Certificate* cert); | 76 int AddUserCert(X509Certificate* cert); |
65 | 77 |
66 // Registers |observer| to receive notifications of certificate changes. The | 78 // CertDatabaseSource implementation: |
67 // thread on which this is called is the thread on which |observer| will be | 79 virtual void AddObserver(Observer* observer) OVERRIDE; |
68 // called back with notifications. | 80 virtual void RemoveObserver(Observer* observer) OVERRIDE; |
69 void AddObserver(Observer* observer); | |
70 | |
71 // Unregisters |observer| from receiving notifications. This must be called | |
72 // on the same thread on which AddObserver() was called. | |
73 void RemoveObserver(Observer* observer); | |
74 | 81 |
75 #if defined(OS_MACOSX) && !defined(OS_IOS) | 82 #if defined(OS_MACOSX) && !defined(OS_IOS) |
76 // Configures the current message loop to observe and forward events from | 83 // Configures the current message loop to observe and forward events from |
77 // Keychain services. The MessageLoop must have an associated CFRunLoop, | 84 // Keychain services. The MessageLoop must have an associated CFRunLoop, |
78 // which means that this must be called from a MessageLoop of TYPE_UI. | 85 // which means that this must be called from a MessageLoop of TYPE_UI. |
79 void SetMessageLoopForKeychainEvents(); | 86 void SetMessageLoopForKeychainEvents(); |
80 #endif | 87 #endif |
81 | 88 |
82 #if defined(OS_ANDROID) | 89 #if defined(OS_ANDROID) |
83 // On android, the system database is used. When the system notifies the | 90 // On android, the system database is used. When the system notifies the |
84 // application that the certificates changed, the observers must be notified. | 91 // application that the certificates changed, the observers must be notified. |
85 void OnAndroidKeyChainChanged(); | 92 void OnAndroidKeyChainChanged(); |
86 #endif | 93 #endif |
87 | 94 |
| 95 #if defined(USE_NSS) |
| 96 // Observe events and forward them to observers of this |
| 97 // CertDatabase. It is assumed that the CertDatabase will outlive the |
| 98 // source, so we don't need to bother with unregistering. //XXX |
| 99 void AddSource(CertDatabaseSource* source); |
| 100 #endif |
| 101 |
88 private: | 102 private: |
89 friend struct DefaultSingletonTraits<CertDatabase>; | 103 friend struct DefaultSingletonTraits<CertDatabase>; |
90 | 104 |
91 CertDatabase(); | 105 CertDatabase(); |
92 ~CertDatabase(); | 106 virtual ~CertDatabase(); |
93 | 107 |
94 // Broadcasts notifications to all registered observers. | 108 // Broadcasts notifications to all registered observers. |
95 void NotifyObserversOfCertAdded(const X509Certificate* cert); | 109 void NotifyObserversOfCertAdded(const X509Certificate* cert); |
96 void NotifyObserversOfCertRemoved(const X509Certificate* cert); | 110 void NotifyObserversOfCertRemoved(const X509Certificate* cert); |
97 void NotifyObserversOfCACertChanged(const X509Certificate* cert); | 111 void NotifyObserversOfCACertChanged(const X509Certificate* cert); |
98 | 112 |
99 const scoped_refptr<ObserverListThreadSafe<Observer> > observer_list_; | 113 const scoped_refptr<ObserverListThreadSafe<Observer> > observer_list_; |
100 | 114 |
101 #if defined(USE_NSS) || (defined(OS_MACOSX) && !defined(OS_IOS)) | 115 #if defined(USE_NSS) || (defined(OS_MACOSX) && !defined(OS_IOS)) |
102 class Notifier; | 116 class Notifier; |
103 friend class Notifier; | 117 friend class Notifier; |
104 scoped_ptr<Notifier> notifier_; | 118 scoped_ptr<Notifier> notifier_; |
105 #endif | 119 #endif |
106 | 120 |
107 DISALLOW_COPY_AND_ASSIGN(CertDatabase); | 121 DISALLOW_COPY_AND_ASSIGN(CertDatabase); |
108 }; | 122 }; |
109 | 123 |
110 } // namespace net | 124 } // namespace net |
111 | 125 |
112 #endif // NET_CERT_CERT_DATABASE_H_ | 126 #endif // NET_CERT_CERT_DATABASE_H_ |
OLD | NEW |