1 // Copyright (c) 2013 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 CHROMEOS_CERT_LOADER_H_
6 #define CHROMEOS_CERT_LOADER_H_
11 #include "base/basictypes.h"
12 #include "base/compiler_specific.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/observer_list.h"
16 #include "base/threading/thread_checker.h"
17 #include "chromeos/chromeos_export.h"
18 #include "net/cert/cert_database.h"
21 class NSSCertDatabase
;
22 class X509Certificate
;
23 typedef std::vector
<scoped_refptr
<X509Certificate
> > CertificateList
;
28 // This class is responsible for loading certificates once the TPM is
29 // initialized. It is expected to be constructed on the UI thread and public
30 // methods should all be called from the UI thread.
31 // When certificates have been loaded (after login completes and tpm token is
32 // initialized), or the cert database changes, observers are called with
33 // OnCertificatesLoaded().
34 class CHROMEOS_EXPORT CertLoader
: public net::CertDatabase::Observer
{
38 // Called when the certificates, passed for convenience as |cert_list|,
39 // have completed loading. |initial_load| is true the first time this
41 virtual void OnCertificatesLoaded(const net::CertificateList
& cert_list
,
42 bool initial_load
) = 0;
45 virtual ~Observer() {}
48 // Sets the global instance. Must be called before any calls to Get().
49 static void Initialize();
51 // Destroys the global instance.
52 static void Shutdown();
54 // Gets the global instance. Initialize() must be called first.
55 static CertLoader
* Get();
57 // Returns true if the global instance has been initialized.
58 static bool IsInitialized();
60 static std::string
GetPkcs11IdForCert(const net::X509Certificate
& cert
);
62 // Starts the CertLoader with the NSS cert database.
63 // The CertLoader will _not_ take the ownership of the database, but it
64 // expects it to stay alive at least until the shutdown starts on the main
65 // thread. This assumes that |StartWithNSSDB| and other methods directly
66 // using |database_| are not called during shutdown.
67 void StartWithNSSDB(net::NSSCertDatabase
* database
);
69 void AddObserver(CertLoader::Observer
* observer
);
70 void RemoveObserver(CertLoader::Observer
* observer
);
72 int TPMTokenSlotID() const;
73 bool IsHardwareBacked() const;
75 // Whether the certificate is hardware backed. Returns false if the CertLoader
76 // was not yet started (both |CertificatesLoading()| and
77 // |certificates_loaded()| are false).
78 bool IsCertificateHardwareBacked(const net::X509Certificate
* cert
) const;
80 // Returns true when the certificate list has been requested but not loaded.
81 bool CertificatesLoading() const;
83 bool certificates_loaded() const { return certificates_loaded_
; }
85 // This will be empty until certificates_loaded() is true.
86 const net::CertificateList
& cert_list() const { return *cert_list_
; }
88 void force_hardware_backed_for_test() {
89 force_hardware_backed_for_test_
= true;
94 virtual ~CertLoader();
96 // Trigger a certificate load. If a certificate loading task is already in
97 // progress, will start a reload once the current task is finished.
98 void LoadCertificates();
100 // Called if a certificate load task is finished.
101 void UpdateCertificates(scoped_ptr
<net::CertificateList
> cert_list
);
103 void NotifyCertificatesLoaded(bool initial_load
);
105 // net::CertDatabase::Observer
106 virtual void OnCACertChanged(const net::X509Certificate
* cert
) OVERRIDE
;
107 virtual void OnCertAdded(const net::X509Certificate
* cert
) OVERRIDE
;
108 virtual void OnCertRemoved(const net::X509Certificate
* cert
) OVERRIDE
;
110 ObserverList
<Observer
> observers_
;
112 // Flags describing current CertLoader state.
113 bool certificates_loaded_
;
114 bool certificates_update_required_
;
115 bool certificates_update_running_
;
117 // The user-specific NSS certificate database from which the certificates
119 net::NSSCertDatabase
* database_
;
121 // Set during tests if |IsHardwareBacked()| should always return true.
122 bool force_hardware_backed_for_test_
;
124 // Cached Certificates loaded from the database.
125 scoped_ptr
<net::CertificateList
> cert_list_
;
127 base::ThreadChecker thread_checker_
;
129 base::WeakPtrFactory
<CertLoader
> weak_factory_
;
131 DISALLOW_COPY_AND_ASSIGN(CertLoader
);
134 } // namespace chromeos
136 #endif // CHROMEOS_CERT_LOADER_H_