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_NSS_CERT_DATABASE_H_ | 5 #ifndef NET_CERT_NSS_CERT_DATABASE_H_ |
6 #define NET_CERT_NSS_CERT_DATABASE_H_ | 6 #define NET_CERT_NSS_CERT_DATABASE_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/callback_forward.h" | 12 #include "base/callback_forward.h" |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
15 #include "base/strings/string16.h" | 15 #include "base/strings/string16.h" |
16 #include "crypto/scoped_nss_types.h" | 16 #include "crypto/scoped_nss_types.h" |
17 #include "net/base/net_errors.h" | 17 #include "net/base/net_errors.h" |
18 #include "net/base/net_export.h" | 18 #include "net/base/net_export.h" |
19 #include "net/cert/cert_type.h" | 19 #include "net/cert/cert_type.h" |
20 #include "net/cert/x509_certificate.h" | 20 #include "net/cert/x509_certificate.h" |
21 | 21 |
22 namespace base { | 22 namespace base { |
23 template <typename T> struct DefaultLazyInstanceTraits; | |
24 class TaskRunner; | 23 class TaskRunner; |
25 } | 24 } |
26 template <class ObserverType> class ObserverListThreadSafe; | 25 template <class ObserverType> class ObserverListThreadSafe; |
27 | 26 |
28 namespace net { | 27 namespace net { |
29 | 28 |
30 class CryptoModule; | 29 class CryptoModule; |
31 typedef std::vector<scoped_refptr<CryptoModule> > CryptoModuleList; | 30 typedef std::vector<scoped_refptr<CryptoModule> > CryptoModuleList; |
32 | 31 |
33 // Provides functions to manipulate the NSS certificate stores. | 32 // Provides functions to manipulate the NSS certificate stores. |
34 // Forwards notifications about certificate changes to the global CertDatabase | 33 // Forwards notifications about certificate changes to the global CertDatabase |
35 // singleton. | 34 // singleton. |
36 class NET_EXPORT NSSCertDatabase { | 35 class NET_EXPORT NSSCertDatabase { |
37 public: | 36 public: |
38 | |
39 class NET_EXPORT Observer { | 37 class NET_EXPORT Observer { |
40 public: | 38 public: |
41 virtual ~Observer() {} | 39 virtual ~Observer() {} |
42 | 40 |
43 // Will be called when a new certificate is added. | 41 // Will be called when a new certificate is added. |
44 // Called with |cert| == NULL after importing a list of certificates | 42 // Called with |cert| == NULL after importing a list of certificates |
45 // in ImportFromPKCS12(). | 43 // in ImportFromPKCS12(). |
46 virtual void OnCertAdded(const X509Certificate* cert) {} | 44 virtual void OnCertAdded(const X509Certificate* cert) {} |
47 | 45 |
48 // Will be called when a certificate is removed. | 46 // Will be called when a certificate is removed. |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
95 DISTRUSTED_SSL = 1 << 3, | 93 DISTRUSTED_SSL = 1 << 3, |
96 DISTRUSTED_EMAIL = 1 << 4, | 94 DISTRUSTED_EMAIL = 1 << 4, |
97 DISTRUSTED_OBJ_SIGN = 1 << 5, | 95 DISTRUSTED_OBJ_SIGN = 1 << 5, |
98 }; | 96 }; |
99 | 97 |
100 typedef base::Callback<void(scoped_ptr<CertificateList> certs)> | 98 typedef base::Callback<void(scoped_ptr<CertificateList> certs)> |
101 ListCertsCallback; | 99 ListCertsCallback; |
102 | 100 |
103 typedef base::Callback<void(bool)> DeleteCertCallback; | 101 typedef base::Callback<void(bool)> DeleteCertCallback; |
104 | 102 |
105 // DEPRECATED: See http://crbug.com/329735. | 103 // Only use this outside of unit test if you really know what you're doing. |
106 static NSSCertDatabase* GetInstance(); | 104 // Both slots must not be NULL but can be identical. |
Ryan Sleevi
2014/07/24 23:19:10
Well that's not very helpful of a comment. How do
pneubeck (no reviews)
2014/07/25 08:15:21
Done.
| |
105 NSSCertDatabase(crypto::ScopedPK11Slot public_slot, | |
106 crypto::ScopedPK11Slot private_slot); | |
107 virtual ~NSSCertDatabase(); | |
107 | 108 |
108 // Get a list of unique certificates in the certificate database (one | 109 // Get a list of unique certificates in the certificate database (one |
109 // instance of all certificates). | 110 // instance of all certificates). |
110 // DEPRECATED by |ListCerts|. See http://crbug.com/340460. | 111 // DEPRECATED by |ListCerts|. See http://crbug.com/340460. |
111 virtual void ListCertsSync(CertificateList* certs); | 112 virtual void ListCertsSync(CertificateList* certs); |
112 | 113 |
113 // Asynchronously get a list of unique certificates in the certificate | 114 // Asynchronously get a list of unique certificates in the certificate |
114 // database (one instance of all certificates). Note that the callback may be | 115 // database (one instance of all certificates). Note that the callback may be |
115 // run even after the database is deleted. | 116 // run even after the database is deleted. |
116 virtual void ListCerts(const ListCertsCallback& callback); | 117 virtual void ListCerts(const ListCertsCallback& callback); |
117 | 118 |
118 // Get a list of certificates in the certificate database of the given slot. | 119 // Get a list of certificates in the certificate database of the given slot. |
119 // Note that the callback may be run even after the database is deleted. | 120 // Note that the callback may be run even after the database is deleted. |
120 // Must be called on the IO thread and it calls |callback| on the IO thread. | 121 // Must be called on the IO thread and it calls |callback| on the IO thread. |
121 // This does not block by retrieving the certs asynchronously on a worker | 122 // This does not block by retrieving the certs asynchronously on a worker |
122 // thread. Never calls |callback| synchronously. | 123 // thread. Never calls |callback| synchronously. |
123 virtual void ListCertsInSlot(const ListCertsCallback& callback, | 124 virtual void ListCertsInSlot(const ListCertsCallback& callback, |
124 PK11SlotInfo* slot); | 125 PK11SlotInfo* slot); |
125 | 126 |
126 // Get the default slot for public key data. | 127 // Get the default slot for public key data. |
127 virtual crypto::ScopedPK11Slot GetPublicSlot() const; | 128 crypto::ScopedPK11Slot GetPublicSlot() const; |
128 | 129 |
129 // Get the default slot for private key or mixed private/public key data. | 130 // Get the default slot for private key or mixed private/public key data. |
130 virtual crypto::ScopedPK11Slot GetPrivateSlot() const; | 131 crypto::ScopedPK11Slot GetPrivateSlot() const; |
131 | 132 |
132 // Get the default module for public key data. | 133 // Get the default module for public key data. |
133 // The returned pointer must be stored in a scoped_refptr<CryptoModule>. | 134 // The returned pointer must be stored in a scoped_refptr<CryptoModule>. |
134 // DEPRECATED: use GetPublicSlot instead. | 135 // DEPRECATED: use GetPublicSlot instead. |
135 // TODO(mattm): remove usage of this method and remove it. | 136 // TODO(mattm): remove usage of this method and remove it. |
136 CryptoModule* GetPublicModule() const; | 137 CryptoModule* GetPublicModule() const; |
137 | 138 |
138 // Get the default module for private key or mixed private/public key data. | 139 // Get the default module for private key or mixed private/public key data. |
139 // The returned pointer must be stored in a scoped_refptr<CryptoModule>. | 140 // The returned pointer must be stored in a scoped_refptr<CryptoModule>. |
140 // DEPRECATED: use GetPrivateSlot instead. | 141 // DEPRECATED: use GetPrivateSlot instead. |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
225 bool IsReadOnly(const X509Certificate* cert) const; | 226 bool IsReadOnly(const X509Certificate* cert) const; |
226 | 227 |
227 // Check whether cert is stored in a hardware slot. | 228 // Check whether cert is stored in a hardware slot. |
228 bool IsHardwareBacked(const X509Certificate* cert) const; | 229 bool IsHardwareBacked(const X509Certificate* cert) const; |
229 | 230 |
230 // Overrides task runner that's used for running slow tasks. | 231 // Overrides task runner that's used for running slow tasks. |
231 void SetSlowTaskRunnerForTest( | 232 void SetSlowTaskRunnerForTest( |
232 const scoped_refptr<base::TaskRunner>& task_runner); | 233 const scoped_refptr<base::TaskRunner>& task_runner); |
233 | 234 |
234 protected: | 235 protected: |
235 NSSCertDatabase(); | |
236 virtual ~NSSCertDatabase(); | |
237 | |
238 // Certificate listing implementation used by |ListCerts*| and | 236 // Certificate listing implementation used by |ListCerts*| and |
239 // |ListCertsSync|. Static so it may safely be used on the worker thread. | 237 // |ListCertsSync|. Static so it may safely be used on the worker thread. |
240 // If |slot| is NULL, obtains the certs of all slots, otherwise only of | 238 // If |slot| is NULL, obtains the certs of all slots, otherwise only of |
241 // |slot|. | 239 // |slot|. |
242 static void ListCertsImpl(crypto::ScopedPK11Slot slot, | 240 static void ListCertsImpl(crypto::ScopedPK11Slot slot, |
243 CertificateList* certs); | 241 CertificateList* certs); |
244 | 242 |
245 // Gets task runner that should be used for slow tasks like certificate | 243 // Gets task runner that should be used for slow tasks like certificate |
246 // listing. Defaults to a base::WorkerPool runner, but may be overriden | 244 // listing. Defaults to a base::WorkerPool runner, but may be overriden |
247 // in tests (see SetSlowTaskRunnerForTest). | 245 // in tests (see SetSlowTaskRunnerForTest). |
248 scoped_refptr<base::TaskRunner> GetSlowTaskRunner() const; | 246 scoped_refptr<base::TaskRunner> GetSlowTaskRunner() const; |
249 | 247 |
250 private: | 248 private: |
251 friend struct base::DefaultLazyInstanceTraits<NSSCertDatabase>; | |
252 | |
253 // Registers |observer| to receive notifications of certificate changes. The | 249 // Registers |observer| to receive notifications of certificate changes. The |
254 // thread on which this is called is the thread on which |observer| will be | 250 // thread on which this is called is the thread on which |observer| will be |
255 // called back with notifications. | 251 // called back with notifications. |
256 // NOTE: Observers registered here will only receive notifications generated | 252 // NOTE: Observers registered here will only receive notifications generated |
257 // directly through the NSSCertDatabase, but not those from the CertDatabase. | 253 // directly through the NSSCertDatabase, but not those from the CertDatabase. |
258 // CertDatabase observers will receive all certificate notifications. | 254 // CertDatabase observers will receive all certificate notifications. |
259 void AddObserver(Observer* observer); | 255 void AddObserver(Observer* observer); |
260 | 256 |
261 // Unregisters |observer| from receiving notifications. This must be called | 257 // Unregisters |observer| from receiving notifications. This must be called |
262 // on the same thread on which AddObserver() was called. | 258 // on the same thread on which AddObserver() was called. |
263 void RemoveObserver(Observer* observer); | 259 void RemoveObserver(Observer* observer); |
264 | 260 |
265 // Notifies observers of the removal of |cert| and calls |callback| with | 261 // Notifies observers of the removal of |cert| and calls |callback| with |
266 // |success| as argument. | 262 // |success| as argument. |
267 void NotifyCertRemovalAndCallBack(scoped_refptr<X509Certificate> cert, | 263 void NotifyCertRemovalAndCallBack(scoped_refptr<X509Certificate> cert, |
268 const DeleteCertCallback& callback, | 264 const DeleteCertCallback& callback, |
269 bool success); | 265 bool success); |
270 | 266 |
271 // Broadcasts notifications to all registered observers. | 267 // Broadcasts notifications to all registered observers. |
272 void NotifyObserversOfCertAdded(const X509Certificate* cert); | 268 void NotifyObserversOfCertAdded(const X509Certificate* cert); |
273 void NotifyObserversOfCertRemoved(const X509Certificate* cert); | 269 void NotifyObserversOfCertRemoved(const X509Certificate* cert); |
274 void NotifyObserversOfCACertChanged(const X509Certificate* cert); | 270 void NotifyObserversOfCACertChanged(const X509Certificate* cert); |
275 | 271 |
276 // Certificate removal implementation used by |DeleteCertAndKey*|. Static so | 272 // Certificate removal implementation used by |DeleteCertAndKey*|. Static so |
277 // it may safely be used on the worker thread. | 273 // it may safely be used on the worker thread. |
278 static bool DeleteCertAndKeyImpl(scoped_refptr<X509Certificate> cert); | 274 static bool DeleteCertAndKeyImpl(scoped_refptr<X509Certificate> cert); |
279 | 275 |
276 crypto::ScopedPK11Slot public_slot_; | |
277 crypto::ScopedPK11Slot private_slot_; | |
278 | |
280 // A helper observer that forwards events from this database to CertDatabase. | 279 // A helper observer that forwards events from this database to CertDatabase. |
281 scoped_ptr<Observer> cert_notification_forwarder_; | 280 scoped_ptr<Observer> cert_notification_forwarder_; |
282 | 281 |
283 // Task runner that should be used in tests if set. | 282 // Task runner that should be used in tests if set. |
284 scoped_refptr<base::TaskRunner> slow_task_runner_for_test_; | 283 scoped_refptr<base::TaskRunner> slow_task_runner_for_test_; |
285 | 284 |
286 const scoped_refptr<ObserverListThreadSafe<Observer> > observer_list_; | 285 const scoped_refptr<ObserverListThreadSafe<Observer> > observer_list_; |
287 | 286 |
288 base::WeakPtrFactory<NSSCertDatabase> weak_factory_; | 287 base::WeakPtrFactory<NSSCertDatabase> weak_factory_; |
289 | 288 |
290 DISALLOW_COPY_AND_ASSIGN(NSSCertDatabase); | 289 DISALLOW_COPY_AND_ASSIGN(NSSCertDatabase); |
291 }; | 290 }; |
292 | 291 |
293 } // namespace net | 292 } // namespace net |
294 | 293 |
295 #endif // NET_CERT_NSS_CERT_DATABASE_H_ | 294 #endif // NET_CERT_NSS_CERT_DATABASE_H_ |
OLD | NEW |