Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / net / cert / nss_cert_database.h
blobf1611698e391584eb4be398e57f1fc345a8deb6c
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
3 // found in the LICENSE file.
5 #ifndef NET_CERT_NSS_CERT_DATABASE_H_
6 #define NET_CERT_NSS_CERT_DATABASE_H_
8 #include <string>
9 #include <vector>
11 #include "base/basictypes.h"
12 #include "base/callback_forward.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/strings/string16.h"
16 #include "crypto/scoped_nss_types.h"
17 #include "net/base/net_errors.h"
18 #include "net/base/net_export.h"
19 #include "net/cert/cert_type.h"
20 #include "net/cert/x509_certificate.h"
22 namespace base {
23 class TaskRunner;
25 template <class ObserverType> class ObserverListThreadSafe;
27 namespace net {
29 class CryptoModule;
30 typedef std::vector<scoped_refptr<CryptoModule> > CryptoModuleList;
32 // Provides functions to manipulate the NSS certificate stores.
33 // Forwards notifications about certificate changes to the global CertDatabase
34 // singleton.
35 class NET_EXPORT NSSCertDatabase {
36 public:
37 class NET_EXPORT Observer {
38 public:
39 virtual ~Observer() {}
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) {}
46 // Will be called when a certificate is removed.
47 virtual void OnCertRemoved(const X509Certificate* cert) {}
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) {}
54 protected:
55 Observer() {}
57 private:
58 DISALLOW_COPY_AND_ASSIGN(Observer);
61 // Stores per-certificate error codes for import failures.
62 struct NET_EXPORT ImportCertFailure {
63 public:
64 ImportCertFailure(const scoped_refptr<X509Certificate>& cert, int err);
65 ~ImportCertFailure();
67 scoped_refptr<X509Certificate> certificate;
68 int net_error;
70 typedef std::vector<ImportCertFailure> ImportCertFailureList;
72 // Constants that define which usages a certificate is trusted for.
73 // They are used in combination with CertType to specify trust for each type
74 // of certificate.
75 // For a CA_CERT, they specify that the CA is trusted for issuing server and
76 // client certs of each type.
77 // For SERVER_CERT, only TRUSTED_SSL makes sense, and specifies the cert is
78 // trusted as a server.
79 // For EMAIL_CERT, only TRUSTED_EMAIL makes sense, and specifies the cert is
80 // trusted for email.
81 // DISTRUSTED_* specifies that the cert should not be trusted for the given
82 // usage, regardless of whether it would otherwise inherit trust from the
83 // issuer chain.
84 // Use TRUST_DEFAULT to inherit trust as normal.
85 // NOTE: The actual constants are defined using an enum instead of static
86 // consts due to compilation/linkage constraints with template functions.
87 typedef uint32 TrustBits;
88 enum {
89 TRUST_DEFAULT = 0,
90 TRUSTED_SSL = 1 << 0,
91 TRUSTED_EMAIL = 1 << 1,
92 TRUSTED_OBJ_SIGN = 1 << 2,
93 DISTRUSTED_SSL = 1 << 3,
94 DISTRUSTED_EMAIL = 1 << 4,
95 DISTRUSTED_OBJ_SIGN = 1 << 5,
98 typedef base::Callback<void(scoped_ptr<CertificateList> certs)>
99 ListCertsCallback;
101 typedef base::Callback<void(bool)> DeleteCertCallback;
103 // Creates a NSSCertDatabase that will store public information (such as
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();
115 // Get a list of unique certificates in the certificate database (one
116 // instance of all certificates).
117 // DEPRECATED by |ListCerts|. See http://crbug.com/340460.
118 virtual void ListCertsSync(CertificateList* certs);
120 // Asynchronously get a list of unique certificates in the certificate
121 // database (one instance of all certificates). Note that the callback may be
122 // run even after the database is deleted.
123 virtual void ListCerts(const ListCertsCallback& callback);
125 // Get a list of certificates in the certificate database of the given slot.
126 // Note that the callback may be run even after the database is deleted.
127 // Must be called on the IO thread and it calls |callback| on the IO thread.
128 // This does not block by retrieving the certs asynchronously on a worker
129 // thread. Never calls |callback| synchronously.
130 virtual void ListCertsInSlot(const ListCertsCallback& callback,
131 PK11SlotInfo* slot);
133 #if defined(OS_CHROMEOS)
134 // Get the slot for system-wide key data. May be NULL if the system token was
135 // not explicitly set.
136 // Note: The System slot is set after the NSSCertDatabase is constructed and
137 // this call returns synchronously. Thus, it is possible to call this function
138 // before SetSystemSlot is called and get a NULL result.
139 // See https://crbug.com/399554 .
140 virtual crypto::ScopedPK11Slot GetSystemSlot() const;
141 #endif
143 // Get the default slot for public key data.
144 crypto::ScopedPK11Slot GetPublicSlot() const;
146 // Get the default slot for private key or mixed private/public key data.
147 crypto::ScopedPK11Slot GetPrivateSlot() const;
149 // Get the default module for public key data.
150 // The returned pointer must be stored in a scoped_refptr<CryptoModule>.
151 // DEPRECATED: use GetPublicSlot instead.
152 // TODO(mattm): remove usage of this method and remove it.
153 CryptoModule* GetPublicModule() const;
155 // Get the default module for private key or mixed private/public key data.
156 // The returned pointer must be stored in a scoped_refptr<CryptoModule>.
157 // DEPRECATED: use GetPrivateSlot instead.
158 // TODO(mattm): remove usage of this method and remove it.
159 CryptoModule* GetPrivateModule() const;
161 // Get all modules.
162 // If |need_rw| is true, only writable modules will be returned.
163 // TODO(mattm): come up with better alternative to CryptoModuleList.
164 virtual void ListModules(CryptoModuleList* modules, bool need_rw) const;
166 // Import certificates and private keys from PKCS #12 blob into the module.
167 // If |is_extractable| is false, mark the private key as being unextractable
168 // from the module.
169 // Returns OK or a network error code such as ERR_PKCS12_IMPORT_BAD_PASSWORD
170 // or ERR_PKCS12_IMPORT_ERROR. |imported_certs|, if non-NULL, returns a list
171 // of certs that were imported.
172 int ImportFromPKCS12(CryptoModule* module,
173 const std::string& data,
174 const base::string16& password,
175 bool is_extractable,
176 CertificateList* imported_certs);
178 // Export the given certificates and private keys into a PKCS #12 blob,
179 // storing into |output|.
180 // Returns the number of certificates successfully exported.
181 int ExportToPKCS12(const CertificateList& certs,
182 const base::string16& password,
183 std::string* output) const;
185 // Uses similar logic to nsNSSCertificateDB::handleCACertDownload to find the
186 // root. Assumes the list is an ordered hierarchy with the root being either
187 // the first or last element.
188 // TODO(mattm): improve this to handle any order.
189 X509Certificate* FindRootInList(const CertificateList& certificates) const;
191 // Import CA certificates.
192 // Tries to import all the certificates given. The root will be trusted
193 // according to |trust_bits|. Any certificates that could not be imported
194 // will be listed in |not_imported|.
195 // Returns false if there is an internal error, otherwise true is returned and
196 // |not_imported| should be checked for any certificates that were not
197 // imported.
198 bool ImportCACerts(const CertificateList& certificates,
199 TrustBits trust_bits,
200 ImportCertFailureList* not_imported);
202 // Import server certificate. The first cert should be the server cert. Any
203 // additional certs should be intermediate/CA certs and will be imported but
204 // not given any trust.
205 // Any certificates that could not be imported will be listed in
206 // |not_imported|.
207 // |trust_bits| can be set to explicitly trust or distrust the certificate, or
208 // use TRUST_DEFAULT to inherit trust as normal.
209 // Returns false if there is an internal error, otherwise true is returned and
210 // |not_imported| should be checked for any certificates that were not
211 // imported.
212 bool ImportServerCert(const CertificateList& certificates,
213 TrustBits trust_bits,
214 ImportCertFailureList* not_imported);
216 // Get trust bits for certificate.
217 TrustBits GetCertTrust(const X509Certificate* cert, CertType type) const;
219 // IsUntrusted returns true if |cert| is specifically untrusted. These
220 // certificates are stored in the database for the specific purpose of
221 // rejecting them.
222 bool IsUntrusted(const X509Certificate* cert) const;
224 // Set trust values for certificate.
225 // Returns true on success or false on failure.
226 bool SetCertTrust(const X509Certificate* cert,
227 CertType type,
228 TrustBits trust_bits);
230 // Delete certificate and associated private key (if one exists).
231 // |cert| is still valid when this function returns. Returns true on
232 // success.
233 bool DeleteCertAndKey(X509Certificate* cert);
235 // Like DeleteCertAndKey but does not block by running the removal on a worker
236 // thread. This must be called on IO thread and it will run |callback| on IO
237 // thread. Never calls |callback| synchronously.
238 void DeleteCertAndKeyAsync(const scoped_refptr<X509Certificate>& cert,
239 const DeleteCertCallback& callback);
241 // Check whether cert is stored in a readonly slot.
242 bool IsReadOnly(const X509Certificate* cert) const;
244 // Check whether cert is stored in a hardware slot.
245 bool IsHardwareBacked(const X509Certificate* cert) const;
247 // Overrides task runner that's used for running slow tasks.
248 void SetSlowTaskRunnerForTest(
249 const scoped_refptr<base::TaskRunner>& task_runner);
251 protected:
252 // Certificate listing implementation used by |ListCerts*| and
253 // |ListCertsSync|. Static so it may safely be used on the worker thread.
254 // If |slot| is NULL, obtains the certs of all slots, otherwise only of
255 // |slot|.
256 static void ListCertsImpl(crypto::ScopedPK11Slot slot,
257 CertificateList* certs);
259 // Gets task runner that should be used for slow tasks like certificate
260 // listing. Defaults to a base::WorkerPool runner, but may be overriden
261 // in tests (see SetSlowTaskRunnerForTest).
262 scoped_refptr<base::TaskRunner> GetSlowTaskRunner() const;
264 private:
265 // Registers |observer| to receive notifications of certificate changes. The
266 // thread on which this is called is the thread on which |observer| will be
267 // called back with notifications.
268 // NOTE: Observers registered here will only receive notifications generated
269 // directly through the NSSCertDatabase, but not those from the CertDatabase.
270 // CertDatabase observers will receive all certificate notifications.
271 void AddObserver(Observer* observer);
273 // Unregisters |observer| from receiving notifications. This must be called
274 // on the same thread on which AddObserver() was called.
275 void RemoveObserver(Observer* observer);
277 // Notifies observers of the removal of |cert| and calls |callback| with
278 // |success| as argument.
279 void NotifyCertRemovalAndCallBack(scoped_refptr<X509Certificate> cert,
280 const DeleteCertCallback& callback,
281 bool success);
283 // Broadcasts notifications to all registered observers.
284 void NotifyObserversOfCertAdded(const X509Certificate* cert);
285 void NotifyObserversOfCertRemoved(const X509Certificate* cert);
286 void NotifyObserversOfCACertChanged(const X509Certificate* cert);
288 // Certificate removal implementation used by |DeleteCertAndKey*|. Static so
289 // it may safely be used on the worker thread.
290 static bool DeleteCertAndKeyImpl(scoped_refptr<X509Certificate> cert);
292 crypto::ScopedPK11Slot public_slot_;
293 crypto::ScopedPK11Slot private_slot_;
295 // A helper observer that forwards events from this database to CertDatabase.
296 scoped_ptr<Observer> cert_notification_forwarder_;
298 // Task runner that should be used in tests if set.
299 scoped_refptr<base::TaskRunner> slow_task_runner_for_test_;
301 const scoped_refptr<ObserverListThreadSafe<Observer> > observer_list_;
303 base::WeakPtrFactory<NSSCertDatabase> weak_factory_;
305 DISALLOW_COPY_AND_ASSIGN(NSSCertDatabase);
308 } // namespace net
310 #endif // NET_CERT_NSS_CERT_DATABASE_H_