xref: /aosp_15_r20/external/cronet/net/cert/nss_cert_database.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1*6777b538SAndroid Build Coastguard Worker // Copyright 2012 The Chromium Authors
2*6777b538SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*6777b538SAndroid Build Coastguard Worker // found in the LICENSE file.
4*6777b538SAndroid Build Coastguard Worker 
5*6777b538SAndroid Build Coastguard Worker #ifndef NET_CERT_NSS_CERT_DATABASE_H_
6*6777b538SAndroid Build Coastguard Worker #define NET_CERT_NSS_CERT_DATABASE_H_
7*6777b538SAndroid Build Coastguard Worker 
8*6777b538SAndroid Build Coastguard Worker #include <stdint.h>
9*6777b538SAndroid Build Coastguard Worker 
10*6777b538SAndroid Build Coastguard Worker #include <memory>
11*6777b538SAndroid Build Coastguard Worker #include <string>
12*6777b538SAndroid Build Coastguard Worker #include <vector>
13*6777b538SAndroid Build Coastguard Worker 
14*6777b538SAndroid Build Coastguard Worker #include "base/functional/callback_forward.h"
15*6777b538SAndroid Build Coastguard Worker #include "base/memory/scoped_refptr.h"
16*6777b538SAndroid Build Coastguard Worker #include "base/memory/weak_ptr.h"
17*6777b538SAndroid Build Coastguard Worker #include "base/observer_list_threadsafe.h"
18*6777b538SAndroid Build Coastguard Worker #include "build/build_config.h"
19*6777b538SAndroid Build Coastguard Worker #include "build/chromeos_buildflags.h"
20*6777b538SAndroid Build Coastguard Worker #include "crypto/scoped_nss_types.h"
21*6777b538SAndroid Build Coastguard Worker #include "net/base/net_errors.h"
22*6777b538SAndroid Build Coastguard Worker #include "net/base/net_export.h"
23*6777b538SAndroid Build Coastguard Worker #include "net/cert/cert_type.h"
24*6777b538SAndroid Build Coastguard Worker #include "net/cert/scoped_nss_types.h"
25*6777b538SAndroid Build Coastguard Worker #include "net/cert/x509_certificate.h"
26*6777b538SAndroid Build Coastguard Worker 
27*6777b538SAndroid Build Coastguard Worker namespace net {
28*6777b538SAndroid Build Coastguard Worker 
29*6777b538SAndroid Build Coastguard Worker // Provides functions to manipulate the NSS certificate stores.
30*6777b538SAndroid Build Coastguard Worker // Forwards notifications about certificate changes to the global CertDatabase
31*6777b538SAndroid Build Coastguard Worker // singleton.
32*6777b538SAndroid Build Coastguard Worker class NET_EXPORT NSSCertDatabase {
33*6777b538SAndroid Build Coastguard Worker  public:
34*6777b538SAndroid Build Coastguard Worker   class NET_EXPORT Observer {
35*6777b538SAndroid Build Coastguard Worker    public:
36*6777b538SAndroid Build Coastguard Worker     Observer(const Observer&) = delete;
37*6777b538SAndroid Build Coastguard Worker     Observer& operator=(const Observer&) = delete;
38*6777b538SAndroid Build Coastguard Worker 
39*6777b538SAndroid Build Coastguard Worker     virtual ~Observer() = default;
40*6777b538SAndroid Build Coastguard Worker 
41*6777b538SAndroid Build Coastguard Worker     // Will be called when a certificate is added, removed, or trust settings
42*6777b538SAndroid Build Coastguard Worker     // are changed.
OnTrustStoreChanged()43*6777b538SAndroid Build Coastguard Worker     virtual void OnTrustStoreChanged() {}
OnClientCertStoreChanged()44*6777b538SAndroid Build Coastguard Worker     virtual void OnClientCertStoreChanged() {}
45*6777b538SAndroid Build Coastguard Worker 
46*6777b538SAndroid Build Coastguard Worker    protected:
47*6777b538SAndroid Build Coastguard Worker     Observer() = default;
48*6777b538SAndroid Build Coastguard Worker   };
49*6777b538SAndroid Build Coastguard Worker 
50*6777b538SAndroid Build Coastguard Worker   // Holds an NSS certificate along with additional information.
51*6777b538SAndroid Build Coastguard Worker   struct CertInfo {
52*6777b538SAndroid Build Coastguard Worker     CertInfo();
53*6777b538SAndroid Build Coastguard Worker     CertInfo(CertInfo&& other);
54*6777b538SAndroid Build Coastguard Worker     ~CertInfo();
55*6777b538SAndroid Build Coastguard Worker     CertInfo& operator=(CertInfo&& other);
56*6777b538SAndroid Build Coastguard Worker 
57*6777b538SAndroid Build Coastguard Worker     // The certificate itself.
58*6777b538SAndroid Build Coastguard Worker     ScopedCERTCertificate cert;
59*6777b538SAndroid Build Coastguard Worker 
60*6777b538SAndroid Build Coastguard Worker     // The certificate is stored on a read-only slot.
61*6777b538SAndroid Build Coastguard Worker     bool on_read_only_slot = false;
62*6777b538SAndroid Build Coastguard Worker 
63*6777b538SAndroid Build Coastguard Worker     // The certificate is untrusted.
64*6777b538SAndroid Build Coastguard Worker     bool untrusted = false;
65*6777b538SAndroid Build Coastguard Worker 
66*6777b538SAndroid Build Coastguard Worker     // The certificate is trusted for web navigations according to the trust
67*6777b538SAndroid Build Coastguard Worker     // bits stored in the database.
68*6777b538SAndroid Build Coastguard Worker     bool web_trust_anchor = false;
69*6777b538SAndroid Build Coastguard Worker 
70*6777b538SAndroid Build Coastguard Worker     // The certificate is hardware-backed.
71*6777b538SAndroid Build Coastguard Worker     bool hardware_backed = false;
72*6777b538SAndroid Build Coastguard Worker 
73*6777b538SAndroid Build Coastguard Worker     // The certificate is device-wide.
74*6777b538SAndroid Build Coastguard Worker     // Note: can be true only on Chrome OS.
75*6777b538SAndroid Build Coastguard Worker     bool device_wide = false;
76*6777b538SAndroid Build Coastguard Worker   };
77*6777b538SAndroid Build Coastguard Worker 
78*6777b538SAndroid Build Coastguard Worker   // Stores per-certificate error codes for import failures.
79*6777b538SAndroid Build Coastguard Worker   struct NET_EXPORT ImportCertFailure {
80*6777b538SAndroid Build Coastguard Worker    public:
81*6777b538SAndroid Build Coastguard Worker     ImportCertFailure(ScopedCERTCertificate cert, int err);
82*6777b538SAndroid Build Coastguard Worker     ImportCertFailure(ImportCertFailure&& other);
83*6777b538SAndroid Build Coastguard Worker     ~ImportCertFailure();
84*6777b538SAndroid Build Coastguard Worker 
85*6777b538SAndroid Build Coastguard Worker     ScopedCERTCertificate certificate;
86*6777b538SAndroid Build Coastguard Worker     int net_error;
87*6777b538SAndroid Build Coastguard Worker   };
88*6777b538SAndroid Build Coastguard Worker   typedef std::vector<ImportCertFailure> ImportCertFailureList;
89*6777b538SAndroid Build Coastguard Worker 
90*6777b538SAndroid Build Coastguard Worker   // Constants that define which usages a certificate is trusted for.
91*6777b538SAndroid Build Coastguard Worker   // They are used in combination with CertType to specify trust for each type
92*6777b538SAndroid Build Coastguard Worker   // of certificate.
93*6777b538SAndroid Build Coastguard Worker   // For a CA_CERT, they specify that the CA is trusted for issuing server and
94*6777b538SAndroid Build Coastguard Worker   // client certs of each type.
95*6777b538SAndroid Build Coastguard Worker   // For SERVER_CERT, only TRUSTED_SSL makes sense, and specifies the cert is
96*6777b538SAndroid Build Coastguard Worker   // trusted as a server.
97*6777b538SAndroid Build Coastguard Worker   // For EMAIL_CERT, only TRUSTED_EMAIL makes sense, and specifies the cert is
98*6777b538SAndroid Build Coastguard Worker   // trusted for email.
99*6777b538SAndroid Build Coastguard Worker   // DISTRUSTED_* specifies that the cert should not be trusted for the given
100*6777b538SAndroid Build Coastguard Worker   // usage, regardless of whether it would otherwise inherit trust from the
101*6777b538SAndroid Build Coastguard Worker   // issuer chain.
102*6777b538SAndroid Build Coastguard Worker   // Use TRUST_DEFAULT to inherit trust as normal.
103*6777b538SAndroid Build Coastguard Worker   // NOTE: The actual constants are defined using an enum instead of static
104*6777b538SAndroid Build Coastguard Worker   // consts due to compilation/linkage constraints with template functions.
105*6777b538SAndroid Build Coastguard Worker   typedef uint32_t TrustBits;
106*6777b538SAndroid Build Coastguard Worker   enum {
107*6777b538SAndroid Build Coastguard Worker     TRUST_DEFAULT         =      0,
108*6777b538SAndroid Build Coastguard Worker     TRUSTED_SSL           = 1 << 0,
109*6777b538SAndroid Build Coastguard Worker     TRUSTED_EMAIL         = 1 << 1,
110*6777b538SAndroid Build Coastguard Worker     TRUSTED_OBJ_SIGN      = 1 << 2,
111*6777b538SAndroid Build Coastguard Worker     DISTRUSTED_SSL        = 1 << 3,
112*6777b538SAndroid Build Coastguard Worker     DISTRUSTED_EMAIL      = 1 << 4,
113*6777b538SAndroid Build Coastguard Worker     DISTRUSTED_OBJ_SIGN   = 1 << 5,
114*6777b538SAndroid Build Coastguard Worker   };
115*6777b538SAndroid Build Coastguard Worker 
116*6777b538SAndroid Build Coastguard Worker   using CertInfoList = std::vector<CertInfo>;
117*6777b538SAndroid Build Coastguard Worker 
118*6777b538SAndroid Build Coastguard Worker   using ListCertsInfoCallback =
119*6777b538SAndroid Build Coastguard Worker       base::OnceCallback<void(CertInfoList certs_info)>;
120*6777b538SAndroid Build Coastguard Worker 
121*6777b538SAndroid Build Coastguard Worker   using ListCertsCallback =
122*6777b538SAndroid Build Coastguard Worker       base::OnceCallback<void(ScopedCERTCertificateList certs)>;
123*6777b538SAndroid Build Coastguard Worker 
124*6777b538SAndroid Build Coastguard Worker   using DeleteCertCallback = base::OnceCallback<void(bool)>;
125*6777b538SAndroid Build Coastguard Worker 
126*6777b538SAndroid Build Coastguard Worker   // Creates a NSSCertDatabase that will store public information (such as
127*6777b538SAndroid Build Coastguard Worker   // certificates and trust records) in |public_slot|, and private information
128*6777b538SAndroid Build Coastguard Worker   // (such as keys) in |private_slot|.
129*6777b538SAndroid Build Coastguard Worker   // In general, code should avoid creating an NSSCertDatabase directly,
130*6777b538SAndroid Build Coastguard Worker   // as doing so requires making opinionated decisions about where to store
131*6777b538SAndroid Build Coastguard Worker   // data, and instead prefer to be passed an existing NSSCertDatabase
132*6777b538SAndroid Build Coastguard Worker   // instance.
133*6777b538SAndroid Build Coastguard Worker   // |public_slot| must not be NULL, |private_slot| can be NULL. Both slots can
134*6777b538SAndroid Build Coastguard Worker   // be identical.
135*6777b538SAndroid Build Coastguard Worker   NSSCertDatabase(crypto::ScopedPK11Slot public_slot,
136*6777b538SAndroid Build Coastguard Worker                   crypto::ScopedPK11Slot private_slot);
137*6777b538SAndroid Build Coastguard Worker 
138*6777b538SAndroid Build Coastguard Worker   NSSCertDatabase(const NSSCertDatabase&) = delete;
139*6777b538SAndroid Build Coastguard Worker   NSSCertDatabase& operator=(const NSSCertDatabase&) = delete;
140*6777b538SAndroid Build Coastguard Worker 
141*6777b538SAndroid Build Coastguard Worker   virtual ~NSSCertDatabase();
142*6777b538SAndroid Build Coastguard Worker 
143*6777b538SAndroid Build Coastguard Worker   // Asynchronously get a list of unique certificates in the certificate
144*6777b538SAndroid Build Coastguard Worker   // database (one instance of all certificates). Note that the callback may be
145*6777b538SAndroid Build Coastguard Worker   // run even after the database is deleted.
146*6777b538SAndroid Build Coastguard Worker   virtual void ListCerts(ListCertsCallback callback);
147*6777b538SAndroid Build Coastguard Worker 
148*6777b538SAndroid Build Coastguard Worker   // Get a list of certificates in the certificate database of the given slot.
149*6777b538SAndroid Build Coastguard Worker   // Note that the callback may be run even after the database is deleted. Must
150*6777b538SAndroid Build Coastguard Worker   // be called on the IO thread. This does not block by retrieving the certs
151*6777b538SAndroid Build Coastguard Worker   // asynchronously on a worker thread.
152*6777b538SAndroid Build Coastguard Worker   virtual void ListCertsInSlot(ListCertsCallback callback, PK11SlotInfo* slot);
153*6777b538SAndroid Build Coastguard Worker 
154*6777b538SAndroid Build Coastguard Worker   enum class NSSRootsHandling {
155*6777b538SAndroid Build Coastguard Worker     kInclude,
156*6777b538SAndroid Build Coastguard Worker     kExclude,
157*6777b538SAndroid Build Coastguard Worker   };
158*6777b538SAndroid Build Coastguard Worker   // Asynchronously get a list of certificates along with additional
159*6777b538SAndroid Build Coastguard Worker   // information. Note that the callback may be run even after the database is
160*6777b538SAndroid Build Coastguard Worker   // deleted.
161*6777b538SAndroid Build Coastguard Worker   // The `nss_roots_handling` parameter controls whether to include or exclude
162*6777b538SAndroid Build Coastguard Worker   // NSS built-in roots from the returned list.
163*6777b538SAndroid Build Coastguard Worker   // TODO(https://crbug.com/1412591): remove the `nss_roots_handling` parameter.
164*6777b538SAndroid Build Coastguard Worker   virtual void ListCertsInfo(ListCertsInfoCallback callback,
165*6777b538SAndroid Build Coastguard Worker                              NSSRootsHandling nss_roots_handling);
166*6777b538SAndroid Build Coastguard Worker 
167*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_CHROMEOS)
168*6777b538SAndroid Build Coastguard Worker   // Get the slot for system-wide key data. May be NULL if the system token was
169*6777b538SAndroid Build Coastguard Worker   // not enabled for this database.
170*6777b538SAndroid Build Coastguard Worker   virtual crypto::ScopedPK11Slot GetSystemSlot() const;
171*6777b538SAndroid Build Coastguard Worker 
172*6777b538SAndroid Build Coastguard Worker   // Checks whether |cert| is stored on |slot|.
173*6777b538SAndroid Build Coastguard Worker   static bool IsCertificateOnSlot(CERTCertificate* cert, PK11SlotInfo* slot);
174*6777b538SAndroid Build Coastguard Worker #endif  // BUILDFLAG(IS_CHROMEOS)
175*6777b538SAndroid Build Coastguard Worker 
176*6777b538SAndroid Build Coastguard Worker   // Get the default slot for public key data.
177*6777b538SAndroid Build Coastguard Worker   crypto::ScopedPK11Slot GetPublicSlot() const;
178*6777b538SAndroid Build Coastguard Worker 
179*6777b538SAndroid Build Coastguard Worker   // Get the default slot for private key or mixed private/public key data.
180*6777b538SAndroid Build Coastguard Worker   // Can return NULL.
181*6777b538SAndroid Build Coastguard Worker   crypto::ScopedPK11Slot GetPrivateSlot() const;
182*6777b538SAndroid Build Coastguard Worker 
183*6777b538SAndroid Build Coastguard Worker   // Get all modules.
184*6777b538SAndroid Build Coastguard Worker   // If |need_rw| is true, only writable modules will be returned.
185*6777b538SAndroid Build Coastguard Worker   virtual void ListModules(std::vector<crypto::ScopedPK11Slot>* modules,
186*6777b538SAndroid Build Coastguard Worker                            bool need_rw) const;
187*6777b538SAndroid Build Coastguard Worker 
188*6777b538SAndroid Build Coastguard Worker   // Set trust values for certificate.
189*6777b538SAndroid Build Coastguard Worker   // Returns true on success or false on failure.
190*6777b538SAndroid Build Coastguard Worker   virtual bool SetCertTrust(CERTCertificate* cert,
191*6777b538SAndroid Build Coastguard Worker                             CertType type,
192*6777b538SAndroid Build Coastguard Worker                             TrustBits trust_bits);
193*6777b538SAndroid Build Coastguard Worker 
194*6777b538SAndroid Build Coastguard Worker   // Import certificates and private keys from PKCS #12 blob into the module.
195*6777b538SAndroid Build Coastguard Worker   // If |is_extractable| is false, mark the private key as being unextractable
196*6777b538SAndroid Build Coastguard Worker   // from the module.
197*6777b538SAndroid Build Coastguard Worker   // Returns OK or a network error code such as ERR_PKCS12_IMPORT_BAD_PASSWORD
198*6777b538SAndroid Build Coastguard Worker   // or ERR_PKCS12_IMPORT_ERROR. |imported_certs|, if non-NULL, returns a list
199*6777b538SAndroid Build Coastguard Worker   // of certs that were imported.
200*6777b538SAndroid Build Coastguard Worker   int ImportFromPKCS12(PK11SlotInfo* slot_info,
201*6777b538SAndroid Build Coastguard Worker                        const std::string& data,
202*6777b538SAndroid Build Coastguard Worker                        const std::u16string& password,
203*6777b538SAndroid Build Coastguard Worker                        bool is_extractable,
204*6777b538SAndroid Build Coastguard Worker                        ScopedCERTCertificateList* imported_certs);
205*6777b538SAndroid Build Coastguard Worker 
206*6777b538SAndroid Build Coastguard Worker   // Export the given certificates and private keys into a PKCS #12 blob,
207*6777b538SAndroid Build Coastguard Worker   // storing into |output|.
208*6777b538SAndroid Build Coastguard Worker   // Returns the number of certificates successfully exported. NSS has to be
209*6777b538SAndroid Build Coastguard Worker   // initialized before the method is called.
210*6777b538SAndroid Build Coastguard Worker   static int ExportToPKCS12(const ScopedCERTCertificateList& certs,
211*6777b538SAndroid Build Coastguard Worker                             const std::u16string& password,
212*6777b538SAndroid Build Coastguard Worker                             std::string* output);
213*6777b538SAndroid Build Coastguard Worker 
214*6777b538SAndroid Build Coastguard Worker   // Uses similar logic to nsNSSCertificateDB::handleCACertDownload to find the
215*6777b538SAndroid Build Coastguard Worker   // root.  Assumes the list is an ordered hierarchy with the root being either
216*6777b538SAndroid Build Coastguard Worker   // the first or last element.
217*6777b538SAndroid Build Coastguard Worker   // TODO(mattm): improve this to handle any order.
218*6777b538SAndroid Build Coastguard Worker   CERTCertificate* FindRootInList(
219*6777b538SAndroid Build Coastguard Worker       const ScopedCERTCertificateList& certificates) const;
220*6777b538SAndroid Build Coastguard Worker 
221*6777b538SAndroid Build Coastguard Worker   // Import a user certificate. The private key for the user certificate must
222*6777b538SAndroid Build Coastguard Worker   // already be installed, otherwise we return ERR_NO_PRIVATE_KEY_FOR_CERT.
223*6777b538SAndroid Build Coastguard Worker   // Returns OK or a network error code.
224*6777b538SAndroid Build Coastguard Worker   int ImportUserCert(const std::string& data);
225*6777b538SAndroid Build Coastguard Worker   int ImportUserCert(CERTCertificate* cert);
226*6777b538SAndroid Build Coastguard Worker 
227*6777b538SAndroid Build Coastguard Worker   // Import CA certificates.
228*6777b538SAndroid Build Coastguard Worker   // Tries to import all the certificates given.  The root will be trusted
229*6777b538SAndroid Build Coastguard Worker   // according to |trust_bits|.  Any certificates that could not be imported
230*6777b538SAndroid Build Coastguard Worker   // will be listed in |not_imported|.
231*6777b538SAndroid Build Coastguard Worker   // Returns false if there is an internal error, otherwise true is returned and
232*6777b538SAndroid Build Coastguard Worker   // |not_imported| should be checked for any certificates that were not
233*6777b538SAndroid Build Coastguard Worker   // imported.
234*6777b538SAndroid Build Coastguard Worker   bool ImportCACerts(const ScopedCERTCertificateList& certificates,
235*6777b538SAndroid Build Coastguard Worker                      TrustBits trust_bits,
236*6777b538SAndroid Build Coastguard Worker                      ImportCertFailureList* not_imported);
237*6777b538SAndroid Build Coastguard Worker 
238*6777b538SAndroid Build Coastguard Worker   // Import server certificate.  The first cert should be the server cert.  Any
239*6777b538SAndroid Build Coastguard Worker   // additional certs should be intermediate/CA certs and will be imported but
240*6777b538SAndroid Build Coastguard Worker   // not given any trust.
241*6777b538SAndroid Build Coastguard Worker   // Any certificates that could not be imported will be listed in
242*6777b538SAndroid Build Coastguard Worker   // |not_imported|.
243*6777b538SAndroid Build Coastguard Worker   // |trust_bits| can be set to explicitly trust or distrust the certificate, or
244*6777b538SAndroid Build Coastguard Worker   // use TRUST_DEFAULT to inherit trust as normal.
245*6777b538SAndroid Build Coastguard Worker   // Returns false if there is an internal error, otherwise true is returned and
246*6777b538SAndroid Build Coastguard Worker   // |not_imported| should be checked for any certificates that were not
247*6777b538SAndroid Build Coastguard Worker   // imported.
248*6777b538SAndroid Build Coastguard Worker   bool ImportServerCert(const ScopedCERTCertificateList& certificates,
249*6777b538SAndroid Build Coastguard Worker                         TrustBits trust_bits,
250*6777b538SAndroid Build Coastguard Worker                         ImportCertFailureList* not_imported);
251*6777b538SAndroid Build Coastguard Worker 
252*6777b538SAndroid Build Coastguard Worker   // Get trust bits for certificate.
253*6777b538SAndroid Build Coastguard Worker   TrustBits GetCertTrust(const CERTCertificate* cert, CertType type) const;
254*6777b538SAndroid Build Coastguard Worker 
255*6777b538SAndroid Build Coastguard Worker   // Delete certificate and associated private key (if one exists).
256*6777b538SAndroid Build Coastguard Worker   // |cert| is still valid when this function returns. Returns true on
257*6777b538SAndroid Build Coastguard Worker   // success.
258*6777b538SAndroid Build Coastguard Worker   bool DeleteCertAndKey(CERTCertificate* cert);
259*6777b538SAndroid Build Coastguard Worker 
260*6777b538SAndroid Build Coastguard Worker   // Like DeleteCertAndKey but does not block by running the removal on a worker
261*6777b538SAndroid Build Coastguard Worker   // thread. This must be called on IO thread and it will run |callback| on IO
262*6777b538SAndroid Build Coastguard Worker   // thread. Never calls |callback| synchronously.
263*6777b538SAndroid Build Coastguard Worker   void DeleteCertAndKeyAsync(ScopedCERTCertificate cert,
264*6777b538SAndroid Build Coastguard Worker                              DeleteCertCallback callback);
265*6777b538SAndroid Build Coastguard Worker 
266*6777b538SAndroid Build Coastguard Worker   // IsUntrusted returns true if |cert| is specifically untrusted. These
267*6777b538SAndroid Build Coastguard Worker   // certificates are stored in the database for the specific purpose of
268*6777b538SAndroid Build Coastguard Worker   // rejecting them.
269*6777b538SAndroid Build Coastguard Worker   // TODO(mattm): that's not actually what this method does. (It also marks
270*6777b538SAndroid Build Coastguard Worker   // certs that are self-issued and don't have any specific trust as untrusted,
271*6777b538SAndroid Build Coastguard Worker   // which is wrong.)
272*6777b538SAndroid Build Coastguard Worker   static bool IsUntrusted(const CERTCertificate* cert);
273*6777b538SAndroid Build Coastguard Worker 
274*6777b538SAndroid Build Coastguard Worker   // IsWebTrustAnchor returns true if |cert| is explicitly trusted for web
275*6777b538SAndroid Build Coastguard Worker   // navigations according to the trust bits stored in the database.
276*6777b538SAndroid Build Coastguard Worker   static bool IsWebTrustAnchor(const CERTCertificate* cert);
277*6777b538SAndroid Build Coastguard Worker 
278*6777b538SAndroid Build Coastguard Worker   // Check whether cert is stored in a readonly slot.
279*6777b538SAndroid Build Coastguard Worker   // TODO(mattm): this is ill-defined if the cert exists on both readonly and
280*6777b538SAndroid Build Coastguard Worker   // non-readonly slots.
281*6777b538SAndroid Build Coastguard Worker   static bool IsReadOnly(const CERTCertificate* cert);
282*6777b538SAndroid Build Coastguard Worker 
283*6777b538SAndroid Build Coastguard Worker   // Check whether cert is stored in a hardware slot.
284*6777b538SAndroid Build Coastguard Worker   // This should only be invoked on a worker thread due to expensive operations
285*6777b538SAndroid Build Coastguard Worker   // behind it.
286*6777b538SAndroid Build Coastguard Worker   static bool IsHardwareBacked(const CERTCertificate* cert);
287*6777b538SAndroid Build Coastguard Worker 
288*6777b538SAndroid Build Coastguard Worker   // Registers |observer| to receive notifications of certificate changes.  The
289*6777b538SAndroid Build Coastguard Worker   // thread on which this is called is the thread on which |observer| will be
290*6777b538SAndroid Build Coastguard Worker   // called back with notifications.
291*6777b538SAndroid Build Coastguard Worker   // NOTE: Observers registered here will only receive notifications generated
292*6777b538SAndroid Build Coastguard Worker   // directly through the NSSCertDatabase, but not those from the CertDatabase.
293*6777b538SAndroid Build Coastguard Worker   // CertDatabase observers will receive all certificate notifications.
294*6777b538SAndroid Build Coastguard Worker   void AddObserver(Observer* observer);
295*6777b538SAndroid Build Coastguard Worker 
296*6777b538SAndroid Build Coastguard Worker   // Unregisters |observer| from receiving notifications.  This must be called
297*6777b538SAndroid Build Coastguard Worker   // on the same thread on which AddObserver() was called.
298*6777b538SAndroid Build Coastguard Worker   void RemoveObserver(Observer* observer);
299*6777b538SAndroid Build Coastguard Worker 
300*6777b538SAndroid Build Coastguard Worker  protected:
301*6777b538SAndroid Build Coastguard Worker   // Returns a list of certificates extracted from |certs_info| list ignoring
302*6777b538SAndroid Build Coastguard Worker   // additional information.
303*6777b538SAndroid Build Coastguard Worker   static ScopedCERTCertificateList ExtractCertificates(CertInfoList certs_info);
304*6777b538SAndroid Build Coastguard Worker 
305*6777b538SAndroid Build Coastguard Worker   // Certificate listing implementation used by |ListCerts*|. Static so it may
306*6777b538SAndroid Build Coastguard Worker   // safely be used on the worker thread. If |slot| is nullptr, obtains the
307*6777b538SAndroid Build Coastguard Worker   // certs of all slots, otherwise only of |slot|.
308*6777b538SAndroid Build Coastguard Worker   static ScopedCERTCertificateList ListCertsImpl(crypto::ScopedPK11Slot slot);
309*6777b538SAndroid Build Coastguard Worker 
310*6777b538SAndroid Build Coastguard Worker   // Implements the logic behind returning a list of certificates along with
311*6777b538SAndroid Build Coastguard Worker   // additional information about every certificate.
312*6777b538SAndroid Build Coastguard Worker   // If |add_certs_info| is false, doesn't compute the certificate additional
313*6777b538SAndroid Build Coastguard Worker   // information, the corresponding CertInfo struct fields will be left on their
314*6777b538SAndroid Build Coastguard Worker   // default values.
315*6777b538SAndroid Build Coastguard Worker   // Static so it may safely be used on the worker thread. If |slot| is nullptr,
316*6777b538SAndroid Build Coastguard Worker   // obtains the certs of all slots, otherwise only of |slot|.
317*6777b538SAndroid Build Coastguard Worker   // The |nss_roots_handling| parameter controls whether to include or exclude
318*6777b538SAndroid Build Coastguard Worker   // NSS built-in roots from the resulting cert list.
319*6777b538SAndroid Build Coastguard Worker   static CertInfoList ListCertsInfoImpl(crypto::ScopedPK11Slot slot,
320*6777b538SAndroid Build Coastguard Worker                                         bool add_certs_info,
321*6777b538SAndroid Build Coastguard Worker                                         NSSRootsHandling nss_roots_handling);
322*6777b538SAndroid Build Coastguard Worker 
323*6777b538SAndroid Build Coastguard Worker   // Broadcasts notifications to all registered observers.
324*6777b538SAndroid Build Coastguard Worker   void NotifyObserversTrustStoreChanged();
325*6777b538SAndroid Build Coastguard Worker   void NotifyObserversClientCertStoreChanged();
326*6777b538SAndroid Build Coastguard Worker 
327*6777b538SAndroid Build Coastguard Worker  private:
328*6777b538SAndroid Build Coastguard Worker   enum class DeleteCertAndKeyResult {
329*6777b538SAndroid Build Coastguard Worker     ERROR,
330*6777b538SAndroid Build Coastguard Worker     OK_FOUND_KEY,
331*6777b538SAndroid Build Coastguard Worker     OK_NO_KEY,
332*6777b538SAndroid Build Coastguard Worker   };
333*6777b538SAndroid Build Coastguard Worker   // Notifies observers of the removal of a cert and calls |callback| with
334*6777b538SAndroid Build Coastguard Worker   // |success| as argument.
335*6777b538SAndroid Build Coastguard Worker   void NotifyCertRemovalAndCallBack(DeleteCertCallback callback,
336*6777b538SAndroid Build Coastguard Worker                                     DeleteCertAndKeyResult result);
337*6777b538SAndroid Build Coastguard Worker 
338*6777b538SAndroid Build Coastguard Worker   // Certificate removal implementation used by |DeleteCertAndKey*|. Static so
339*6777b538SAndroid Build Coastguard Worker   // it may safely be used on the worker thread.
340*6777b538SAndroid Build Coastguard Worker   static DeleteCertAndKeyResult DeleteCertAndKeyImpl(CERTCertificate* cert);
341*6777b538SAndroid Build Coastguard Worker   // Like above, but taking a ScopedCERTCertificate. This is a workaround for
342*6777b538SAndroid Build Coastguard Worker   // base::Bind not having a way to own a unique_ptr but pass it to the
343*6777b538SAndroid Build Coastguard Worker   // function as a raw pointer.
344*6777b538SAndroid Build Coastguard Worker   static DeleteCertAndKeyResult DeleteCertAndKeyImplScoped(
345*6777b538SAndroid Build Coastguard Worker       ScopedCERTCertificate cert);
346*6777b538SAndroid Build Coastguard Worker 
347*6777b538SAndroid Build Coastguard Worker   crypto::ScopedPK11Slot public_slot_;
348*6777b538SAndroid Build Coastguard Worker   crypto::ScopedPK11Slot private_slot_;
349*6777b538SAndroid Build Coastguard Worker 
350*6777b538SAndroid Build Coastguard Worker   // A helper observer that forwards events from this database to CertDatabase.
351*6777b538SAndroid Build Coastguard Worker   std::unique_ptr<Observer> cert_notification_forwarder_;
352*6777b538SAndroid Build Coastguard Worker 
353*6777b538SAndroid Build Coastguard Worker   const scoped_refptr<base::ObserverListThreadSafe<Observer>> observer_list_;
354*6777b538SAndroid Build Coastguard Worker 
355*6777b538SAndroid Build Coastguard Worker   base::WeakPtrFactory<NSSCertDatabase> weak_factory_{this};
356*6777b538SAndroid Build Coastguard Worker };
357*6777b538SAndroid Build Coastguard Worker 
358*6777b538SAndroid Build Coastguard Worker }  // namespace net
359*6777b538SAndroid Build Coastguard Worker 
360*6777b538SAndroid Build Coastguard Worker #endif  // NET_CERT_NSS_CERT_DATABASE_H_
361