| 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_database.h" |
| 19 #include "net/cert/cert_type.h" | 20 #include "net/cert/cert_type.h" |
| 20 #include "net/cert/x509_certificate.h" | 21 #include "net/cert/x509_certificate.h" |
| 21 | 22 |
| 22 namespace base { | 23 namespace base { |
| 23 template <typename T> struct DefaultLazyInstanceTraits; | 24 template <typename T> struct DefaultLazyInstanceTraits; |
| 24 class TaskRunner; | 25 class TaskRunner; |
| 25 } | 26 } |
| 26 template <class ObserverType> class ObserverListThreadSafe; | 27 template <class ObserverType> class ObserverListThreadSafe; |
| 27 | 28 |
| 28 namespace net { | 29 namespace net { |
| 29 | 30 |
| 30 class CryptoModule; | 31 class CryptoModule; |
| 31 typedef std::vector<scoped_refptr<CryptoModule> > CryptoModuleList; | 32 typedef std::vector<scoped_refptr<CryptoModule> > CryptoModuleList; |
| 32 | 33 |
| 33 // Provides functions to manipulate the NSS certificate stores. | 34 // Provides functions to manipulate the NSS certificate stores. |
| 34 class NET_EXPORT NSSCertDatabase { | 35 class NET_EXPORT NSSCertDatabase { |
| 35 public: | 36 public: |
| 36 | |
| 37 class NET_EXPORT Observer { | |
| 38 public: | |
| 39 virtual ~Observer() {} | |
| 40 | |
| 41 // Will be called when a new certificate is added. | |
| 42 // Called with |cert| == NULL after importing a list of certificates | |
| 43 // in ImportFromPKCS12(). | |
| 44 virtual void OnCertAdded(const X509Certificate* cert) {} | |
| 45 | |
| 46 // Will be called when a certificate is removed. | |
| 47 virtual void OnCertRemoved(const X509Certificate* cert) {} | |
| 48 | |
| 49 // Will be called when a CA certificate is changed. | |
| 50 // Called with |cert| == NULL after importing a list of certificates | |
| 51 // in ImportCACerts(). | |
| 52 virtual void OnCACertChanged(const X509Certificate* cert) {} | |
| 53 | |
| 54 protected: | |
| 55 Observer() {} | |
| 56 | |
| 57 private: | |
| 58 DISALLOW_COPY_AND_ASSIGN(Observer); | |
| 59 }; | |
| 60 | |
| 61 // Stores per-certificate error codes for import failures. | 37 // Stores per-certificate error codes for import failures. |
| 62 struct NET_EXPORT ImportCertFailure { | 38 struct NET_EXPORT ImportCertFailure { |
| 63 public: | 39 public: |
| 64 ImportCertFailure(const scoped_refptr<X509Certificate>& cert, int err); | 40 ImportCertFailure(const scoped_refptr<X509Certificate>& cert, int err); |
| 65 ~ImportCertFailure(); | 41 ~ImportCertFailure(); |
| 66 | 42 |
| 67 scoped_refptr<X509Certificate> certificate; | 43 scoped_refptr<X509Certificate> certificate; |
| 68 int net_error; | 44 int net_error; |
| 69 }; | 45 }; |
| 70 typedef std::vector<ImportCertFailure> ImportCertFailureList; | 46 typedef std::vector<ImportCertFailure> ImportCertFailureList; |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 // thread. Never calls |callback| synchronously. | 194 // thread. Never calls |callback| synchronously. |
| 219 void DeleteCertAndKeyAsync(const scoped_refptr<X509Certificate>& cert, | 195 void DeleteCertAndKeyAsync(const scoped_refptr<X509Certificate>& cert, |
| 220 const DeleteCertCallback& callback); | 196 const DeleteCertCallback& callback); |
| 221 | 197 |
| 222 // Check whether cert is stored in a readonly slot. | 198 // Check whether cert is stored in a readonly slot. |
| 223 bool IsReadOnly(const X509Certificate* cert) const; | 199 bool IsReadOnly(const X509Certificate* cert) const; |
| 224 | 200 |
| 225 // Check whether cert is stored in a hardware slot. | 201 // Check whether cert is stored in a hardware slot. |
| 226 bool IsHardwareBacked(const X509Certificate* cert) const; | 202 bool IsHardwareBacked(const X509Certificate* cert) const; |
| 227 | 203 |
| 228 // Registers |observer| to receive notifications of certificate changes. The | |
| 229 // thread on which this is called is the thread on which |observer| will be | |
| 230 // called back with notifications. | |
| 231 // NOTE: CertDatabase::AddObserver should be preferred. Observers registered | |
| 232 // here will only receive notifications generated directly through the | |
| 233 // NSSCertDatabase, but not those from the CertDatabase. The CertDatabase | |
| 234 // observers will receive both. | |
| 235 void AddObserver(Observer* observer); | |
| 236 | |
| 237 // Unregisters |observer| from receiving notifications. This must be called | |
| 238 // on the same thread on which AddObserver() was called. | |
| 239 void RemoveObserver(Observer* observer); | |
| 240 | |
| 241 // Overrides task runner that's used for running slow tasks. | 204 // Overrides task runner that's used for running slow tasks. |
| 242 void SetSlowTaskRunnerForTest( | 205 void SetSlowTaskRunnerForTest( |
| 243 const scoped_refptr<base::TaskRunner>& task_runner); | 206 const scoped_refptr<base::TaskRunner>& task_runner); |
| 244 | 207 |
| 245 protected: | 208 protected: |
| 246 NSSCertDatabase(); | 209 NSSCertDatabase(); |
| 247 virtual ~NSSCertDatabase(); | 210 virtual ~NSSCertDatabase(); |
| 248 | 211 |
| 249 // Certificate listing implementation used by |ListCerts*| and | 212 // Certificate listing implementation used by |ListCerts*| and |
| 250 // |ListCertsSync|. Static so it may safely be used on the worker thread. | 213 // |ListCertsSync|. Static so it may safely be used on the worker thread. |
| 251 // If |slot| is NULL, obtains the certs of all slots, otherwise only of | 214 // If |slot| is NULL, obtains the certs of all slots, otherwise only of |
| 252 // |slot|. | 215 // |slot|. |
| 253 static void ListCertsImpl(crypto::ScopedPK11Slot slot, | 216 static void ListCertsImpl(crypto::ScopedPK11Slot slot, |
| 254 CertificateList* certs); | 217 CertificateList* certs); |
| 255 | 218 |
| 256 // Gets task runner that should be used for slow tasks like certificate | 219 // Gets task runner that should be used for slow tasks like certificate |
| 257 // listing. Defaults to a base::WorkerPool runner, but may be overriden | 220 // listing. Defaults to a base::WorkerPool runner, but may be overriden |
| 258 // in tests (see SetSlowTaskRunnerForTest). | 221 // in tests (see SetSlowTaskRunnerForTest). |
| 259 scoped_refptr<base::TaskRunner> GetSlowTaskRunner() const; | 222 scoped_refptr<base::TaskRunner> GetSlowTaskRunner() const; |
| 260 | 223 |
| 261 private: | 224 private: |
| 262 friend struct base::DefaultLazyInstanceTraits<NSSCertDatabase>; | 225 friend struct base::DefaultLazyInstanceTraits<NSSCertDatabase>; |
| 263 | 226 |
| 227 typedef CertDatabase::Observer Observer; |
| 228 |
| 229 // Registers |observer| to receive notifications of certificate changes. The |
| 230 // thread on which this is called is the thread on which |observer| will be |
| 231 // called back with notifications. |
| 232 // This is forward notifications a CertDatabase. |
| 233 void AddObserver(Observer* observer); |
| 234 |
| 235 // Unregisters |observer| from receiving notifications. This must be called |
| 236 // on the same thread on which AddObserver() was called. |
| 237 void RemoveObserver(Observer* observer); |
| 238 |
| 264 // Notifies observers of the removal of |cert| and calls |callback| with | 239 // Notifies observers of the removal of |cert| and calls |callback| with |
| 265 // |success| as argument. | 240 // |success| as argument. |
| 266 void NotifyCertRemovalAndCallBack(scoped_refptr<X509Certificate> cert, | 241 void NotifyCertRemovalAndCallBack(scoped_refptr<X509Certificate> cert, |
| 267 const DeleteCertCallback& callback, | 242 const DeleteCertCallback& callback, |
| 268 bool success); | 243 bool success); |
| 269 | 244 |
| 270 // Broadcasts notifications to all registered observers. | 245 // Broadcasts notifications to all registered observers. |
| 271 void NotifyObserversOfCertAdded(const X509Certificate* cert); | 246 void NotifyObserversOfCertAdded(const X509Certificate* cert); |
| 272 void NotifyObserversOfCertRemoved(const X509Certificate* cert); | 247 void NotifyObserversOfCertRemoved(const X509Certificate* cert); |
| 273 void NotifyObserversOfCACertChanged(const X509Certificate* cert); | 248 void NotifyObserversOfCACertChanged(const X509Certificate* cert); |
| 274 | 249 |
| 275 // Certificate removal implementation used by |DeleteCertAndKey*|. Static so | 250 // Certificate removal implementation used by |DeleteCertAndKey*|. Static so |
| 276 // it may safely be used on the worker thread. | 251 // it may safely be used on the worker thread. |
| 277 static bool DeleteCertAndKeyImpl(scoped_refptr<X509Certificate> cert); | 252 static bool DeleteCertAndKeyImpl(scoped_refptr<X509Certificate> cert); |
| 278 | 253 |
| 279 // Task runner that should be used in tests if set. | 254 // Task runner that should be used in tests if set. |
| 280 scoped_refptr<base::TaskRunner> slow_task_runner_for_test_; | 255 scoped_refptr<base::TaskRunner> slow_task_runner_for_test_; |
| 281 | 256 |
| 282 const scoped_refptr<ObserverListThreadSafe<Observer> > observer_list_; | 257 const scoped_refptr<ObserverListThreadSafe<Observer> > observer_list_; |
| 283 | 258 |
| 284 base::WeakPtrFactory<NSSCertDatabase> weak_factory_; | 259 base::WeakPtrFactory<NSSCertDatabase> weak_factory_; |
| 285 | 260 |
| 286 DISALLOW_COPY_AND_ASSIGN(NSSCertDatabase); | 261 DISALLOW_COPY_AND_ASSIGN(NSSCertDatabase); |
| 287 }; | 262 }; |
| 288 | 263 |
| 289 } // namespace net | 264 } // namespace net |
| 290 | 265 |
| 291 #endif // NET_CERT_NSS_CERT_DATABASE_H_ | 266 #endif // NET_CERT_NSS_CERT_DATABASE_H_ |
| OLD | NEW |