| 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 // Creates a NSSCertDatabase that will store public information (such as |
| 106 static NSSCertDatabase* GetInstance(); | 104 // certificates and trust records) in |public_slot|, and private information |
| 105 // (such as keys) in |private_slot|. |
| 106 // In general, code should avoid creating an NSSCertDatabase directly, |
| 107 // as doing so requires making opinionated decisions about where to store |
| 108 // data, and instead prefer to be passed an existing NSSCertDatabase |
| 109 // instance. |
| 110 // Both slots must not be NULL but can be identical. |
| 111 NSSCertDatabase(crypto::ScopedPK11Slot public_slot, |
| 112 crypto::ScopedPK11Slot private_slot); |
| 113 virtual ~NSSCertDatabase(); |
| 107 | 114 |
| 108 // Get a list of unique certificates in the certificate database (one | 115 // Get a list of unique certificates in the certificate database (one |
| 109 // instance of all certificates). | 116 // instance of all certificates). |
| 110 // DEPRECATED by |ListCerts|. See http://crbug.com/340460. | 117 // DEPRECATED by |ListCerts|. See http://crbug.com/340460. |
| 111 virtual void ListCertsSync(CertificateList* certs); | 118 virtual void ListCertsSync(CertificateList* certs); |
| 112 | 119 |
| 113 // Asynchronously get a list of unique certificates in the certificate | 120 // Asynchronously get a list of unique certificates in the certificate |
| 114 // database (one instance of all certificates). Note that the callback may be | 121 // database (one instance of all certificates). Note that the callback may be |
| 115 // run even after the database is deleted. | 122 // run even after the database is deleted. |
| 116 virtual void ListCerts(const ListCertsCallback& callback); | 123 virtual void ListCerts(const ListCertsCallback& callback); |
| 117 | 124 |
| 118 // Get a list of certificates in the certificate database of the given slot. | 125 // 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. | 126 // 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. | 127 // 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 | 128 // This does not block by retrieving the certs asynchronously on a worker |
| 122 // thread. Never calls |callback| synchronously. | 129 // thread. Never calls |callback| synchronously. |
| 123 virtual void ListCertsInSlot(const ListCertsCallback& callback, | 130 virtual void ListCertsInSlot(const ListCertsCallback& callback, |
| 124 PK11SlotInfo* slot); | 131 PK11SlotInfo* slot); |
| 125 | 132 |
| 126 // Get the default slot for public key data. | 133 // Get the default slot for public key data. |
| 127 virtual crypto::ScopedPK11Slot GetPublicSlot() const; | 134 crypto::ScopedPK11Slot GetPublicSlot() const; |
| 128 | 135 |
| 129 // Get the default slot for private key or mixed private/public key data. | 136 // Get the default slot for private key or mixed private/public key data. |
| 130 virtual crypto::ScopedPK11Slot GetPrivateSlot() const; | 137 crypto::ScopedPK11Slot GetPrivateSlot() const; |
| 131 | 138 |
| 132 // Get the default module for public key data. | 139 // Get the default module for public key data. |
| 133 // The returned pointer must be stored in a scoped_refptr<CryptoModule>. | 140 // The returned pointer must be stored in a scoped_refptr<CryptoModule>. |
| 134 // DEPRECATED: use GetPublicSlot instead. | 141 // DEPRECATED: use GetPublicSlot instead. |
| 135 // TODO(mattm): remove usage of this method and remove it. | 142 // TODO(mattm): remove usage of this method and remove it. |
| 136 CryptoModule* GetPublicModule() const; | 143 CryptoModule* GetPublicModule() const; |
| 137 | 144 |
| 138 // Get the default module for private key or mixed private/public key data. | 145 // 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>. | 146 // The returned pointer must be stored in a scoped_refptr<CryptoModule>. |
| 140 // DEPRECATED: use GetPrivateSlot instead. | 147 // DEPRECATED: use GetPrivateSlot instead. |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 bool IsReadOnly(const X509Certificate* cert) const; | 232 bool IsReadOnly(const X509Certificate* cert) const; |
| 226 | 233 |
| 227 // Check whether cert is stored in a hardware slot. | 234 // Check whether cert is stored in a hardware slot. |
| 228 bool IsHardwareBacked(const X509Certificate* cert) const; | 235 bool IsHardwareBacked(const X509Certificate* cert) const; |
| 229 | 236 |
| 230 // Overrides task runner that's used for running slow tasks. | 237 // Overrides task runner that's used for running slow tasks. |
| 231 void SetSlowTaskRunnerForTest( | 238 void SetSlowTaskRunnerForTest( |
| 232 const scoped_refptr<base::TaskRunner>& task_runner); | 239 const scoped_refptr<base::TaskRunner>& task_runner); |
| 233 | 240 |
| 234 protected: | 241 protected: |
| 235 NSSCertDatabase(); | |
| 236 virtual ~NSSCertDatabase(); | |
| 237 | |
| 238 // Certificate listing implementation used by |ListCerts*| and | 242 // Certificate listing implementation used by |ListCerts*| and |
| 239 // |ListCertsSync|. Static so it may safely be used on the worker thread. | 243 // |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 | 244 // If |slot| is NULL, obtains the certs of all slots, otherwise only of |
| 241 // |slot|. | 245 // |slot|. |
| 242 static void ListCertsImpl(crypto::ScopedPK11Slot slot, | 246 static void ListCertsImpl(crypto::ScopedPK11Slot slot, |
| 243 CertificateList* certs); | 247 CertificateList* certs); |
| 244 | 248 |
| 245 // Gets task runner that should be used for slow tasks like certificate | 249 // Gets task runner that should be used for slow tasks like certificate |
| 246 // listing. Defaults to a base::WorkerPool runner, but may be overriden | 250 // listing. Defaults to a base::WorkerPool runner, but may be overriden |
| 247 // in tests (see SetSlowTaskRunnerForTest). | 251 // in tests (see SetSlowTaskRunnerForTest). |
| 248 scoped_refptr<base::TaskRunner> GetSlowTaskRunner() const; | 252 scoped_refptr<base::TaskRunner> GetSlowTaskRunner() const; |
| 249 | 253 |
| 250 private: | 254 private: |
| 251 friend struct base::DefaultLazyInstanceTraits<NSSCertDatabase>; | |
| 252 | |
| 253 // Registers |observer| to receive notifications of certificate changes. The | 255 // Registers |observer| to receive notifications of certificate changes. The |
| 254 // thread on which this is called is the thread on which |observer| will be | 256 // thread on which this is called is the thread on which |observer| will be |
| 255 // called back with notifications. | 257 // called back with notifications. |
| 256 // NOTE: Observers registered here will only receive notifications generated | 258 // NOTE: Observers registered here will only receive notifications generated |
| 257 // directly through the NSSCertDatabase, but not those from the CertDatabase. | 259 // directly through the NSSCertDatabase, but not those from the CertDatabase. |
| 258 // CertDatabase observers will receive all certificate notifications. | 260 // CertDatabase observers will receive all certificate notifications. |
| 259 void AddObserver(Observer* observer); | 261 void AddObserver(Observer* observer); |
| 260 | 262 |
| 261 // Unregisters |observer| from receiving notifications. This must be called | 263 // Unregisters |observer| from receiving notifications. This must be called |
| 262 // on the same thread on which AddObserver() was called. | 264 // on the same thread on which AddObserver() was called. |
| 263 void RemoveObserver(Observer* observer); | 265 void RemoveObserver(Observer* observer); |
| 264 | 266 |
| 265 // Notifies observers of the removal of |cert| and calls |callback| with | 267 // Notifies observers of the removal of |cert| and calls |callback| with |
| 266 // |success| as argument. | 268 // |success| as argument. |
| 267 void NotifyCertRemovalAndCallBack(scoped_refptr<X509Certificate> cert, | 269 void NotifyCertRemovalAndCallBack(scoped_refptr<X509Certificate> cert, |
| 268 const DeleteCertCallback& callback, | 270 const DeleteCertCallback& callback, |
| 269 bool success); | 271 bool success); |
| 270 | 272 |
| 271 // Broadcasts notifications to all registered observers. | 273 // Broadcasts notifications to all registered observers. |
| 272 void NotifyObserversOfCertAdded(const X509Certificate* cert); | 274 void NotifyObserversOfCertAdded(const X509Certificate* cert); |
| 273 void NotifyObserversOfCertRemoved(const X509Certificate* cert); | 275 void NotifyObserversOfCertRemoved(const X509Certificate* cert); |
| 274 void NotifyObserversOfCACertChanged(const X509Certificate* cert); | 276 void NotifyObserversOfCACertChanged(const X509Certificate* cert); |
| 275 | 277 |
| 276 // Certificate removal implementation used by |DeleteCertAndKey*|. Static so | 278 // Certificate removal implementation used by |DeleteCertAndKey*|. Static so |
| 277 // it may safely be used on the worker thread. | 279 // it may safely be used on the worker thread. |
| 278 static bool DeleteCertAndKeyImpl(scoped_refptr<X509Certificate> cert); | 280 static bool DeleteCertAndKeyImpl(scoped_refptr<X509Certificate> cert); |
| 279 | 281 |
| 282 crypto::ScopedPK11Slot public_slot_; |
| 283 crypto::ScopedPK11Slot private_slot_; |
| 284 |
| 280 // A helper observer that forwards events from this database to CertDatabase. | 285 // A helper observer that forwards events from this database to CertDatabase. |
| 281 scoped_ptr<Observer> cert_notification_forwarder_; | 286 scoped_ptr<Observer> cert_notification_forwarder_; |
| 282 | 287 |
| 283 // Task runner that should be used in tests if set. | 288 // Task runner that should be used in tests if set. |
| 284 scoped_refptr<base::TaskRunner> slow_task_runner_for_test_; | 289 scoped_refptr<base::TaskRunner> slow_task_runner_for_test_; |
| 285 | 290 |
| 286 const scoped_refptr<ObserverListThreadSafe<Observer> > observer_list_; | 291 const scoped_refptr<ObserverListThreadSafe<Observer> > observer_list_; |
| 287 | 292 |
| 288 base::WeakPtrFactory<NSSCertDatabase> weak_factory_; | 293 base::WeakPtrFactory<NSSCertDatabase> weak_factory_; |
| 289 | 294 |
| 290 DISALLOW_COPY_AND_ASSIGN(NSSCertDatabase); | 295 DISALLOW_COPY_AND_ASSIGN(NSSCertDatabase); |
| 291 }; | 296 }; |
| 292 | 297 |
| 293 } // namespace net | 298 } // namespace net |
| 294 | 299 |
| 295 #endif // NET_CERT_NSS_CERT_DATABASE_H_ | 300 #endif // NET_CERT_NSS_CERT_DATABASE_H_ |
| OLD | NEW |