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_
10 #include "base/basictypes.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/observer_list_threadsafe.h"
15 #include "base/threading/thread_checker.h"
16 #include "chromeos/chromeos_export.h"
17 #include "chromeos/dbus/dbus_method_call_status.h"
18 #include "chromeos/login/login_state.h"
19 #include "net/cert/cert_database.h"
20 #include "net/cert/x509_certificate.h"
23 class SequencedTaskRunner
;
33 // This class is responsible for initializing the TPM token and loading
34 // certificates once the TPM is initialized. It is expected to be constructed
35 // on the UI thread and public methods should all be called from the UI thread.
36 // When certificates have been loaded (after login completes), or the cert
37 // database changes, observers are called with OnCertificatesLoaded().
38 class CHROMEOS_EXPORT CertLoader
: public net::CertDatabase::Observer
,
39 public LoginState::Observer
{
43 virtual ~Observer() {}
45 // Called when the certificates, passed for convenience as |cert_list|,
46 // have completed loading. |initial_load| is true the first time this
48 virtual void OnCertificatesLoaded(const net::CertificateList
& cert_list
,
49 bool initial_load
) = 0;
55 DISALLOW_COPY_AND_ASSIGN(Observer
);
58 // Sets the global instance. Must be called before any calls to Get().
59 static void Initialize();
61 // Destroys the global instance.
62 static void Shutdown();
64 // Gets the global instance. Initialize() must be called first.
65 static CertLoader
* Get();
67 // Returns true if the global instance has been initialized.
68 static bool IsInitialized();
70 static std::string
GetPkcs11IdForCert(const net::X509Certificate
& cert
);
72 // By default, CertLoader tries to load the TPMToken only if running in a
73 // ChromeOS environment. Tests can call this function after Initialize() and
74 // before SetCryptoTaskRunner() to enable the TPM initialization.
75 void InitializeTPMForTest();
77 // |crypto_task_runner| is the task runner that any synchronous crypto calls
78 // should be made from, e.g. in Chrome this is the IO thread. Must be called
79 // after the thread is started. Starts TPM initialization and Certificate
81 void SetCryptoTaskRunner(
82 const scoped_refptr
<base::SequencedTaskRunner
>& crypto_task_runner
);
84 // Sets the task runner that any slow calls will be made from, e.g. calls
85 // to the NSS database. If not set, uses base::WorkerPool.
86 void SetSlowTaskRunnerForTest(
87 const scoped_refptr
<base::TaskRunner
>& task_runner
);
89 void AddObserver(CertLoader::Observer
* observer
);
90 void RemoveObserver(CertLoader::Observer
* observer
);
92 // Returns true when the certificate list has been requested but not loaded.
93 bool CertificatesLoading() const;
95 // Returns true if the TPM is available for hardware-backed certificates.
96 bool IsHardwareBacked() const;
98 bool certificates_loaded() const { return certificates_loaded_
; }
100 // TPM info is only valid once the TPM is available (IsHardwareBacked is
101 // true). Otherwise empty strings will be returned.
102 const std::string
& tpm_token_name() const { return tpm_token_name_
; }
103 const std::string
& tpm_token_slot() const { return tpm_token_slot_
; }
104 const std::string
& tpm_user_pin() const { return tpm_user_pin_
; }
106 // This will be empty until certificates_loaded() is true.
107 const net::CertificateList
& cert_list() const { return cert_list_
; }
111 virtual ~CertLoader();
113 void MaybeRequestCertificates();
115 // This is the cyclic chain of callbacks to initialize the TPM token and to
116 // kick off the update of the certificate list.
117 void InitializeTokenAndLoadCertificates();
118 void RetryTokenInitializationLater();
119 void OnPersistentNSSDBOpened();
120 void OnTpmIsEnabled(DBusMethodCallStatus call_status
,
121 bool tpm_is_enabled
);
122 void OnPkcs11IsTpmTokenReady(DBusMethodCallStatus call_status
,
123 bool is_tpm_token_ready
);
124 void OnPkcs11GetTpmTokenInfo(DBusMethodCallStatus call_status
,
125 const std::string
& token_name
,
126 const std::string
& user_pin
);
127 void OnTPMTokenInitialized(bool success
);
129 // These calls handle the updating of the certificate list after the TPM token
132 // Start certificate loading. Must be called at most once.
133 void StartLoadCertificates();
135 // Trigger a certificate load. If a certificate loading task is already in
136 // progress, will start a reload once the current task finised.
137 void LoadCertificates();
139 // Called if a certificate load task is finished.
140 void UpdateCertificates(net::CertificateList
* cert_list
);
142 void NotifyCertificatesLoaded(bool initial_load
);
144 // net::CertDatabase::Observer
145 virtual void OnCertTrustChanged(const net::X509Certificate
* cert
) OVERRIDE
;
146 virtual void OnCertAdded(const net::X509Certificate
* cert
) OVERRIDE
;
147 virtual void OnCertRemoved(const net::X509Certificate
* cert
) OVERRIDE
;
149 // LoginState::Observer
150 virtual void LoggedInStateChanged() OVERRIDE
;
152 bool initialize_tpm_for_test_
;
154 ObserverList
<Observer
> observers_
;
156 bool certificates_requested_
;
157 bool certificates_loaded_
;
158 bool certificates_update_required_
;
159 bool certificates_update_running_
;
161 // The states are traversed in this order but some might get omitted or never
169 TPM_TOKEN_INFO_RECEIVED
,
170 TPM_TOKEN_INITIALIZED
,
172 TPMTokenState tpm_token_state_
;
174 // The current request delay before the next attempt to initialize the
175 // TPM. Will be adapted after each attempt.
176 base::TimeDelta tpm_request_delay_
;
178 // Cached TPM token info.
179 std::string tpm_token_name_
;
180 std::string tpm_token_slot_
;
181 std::string tpm_user_pin_
;
183 // Cached Certificates.
184 net::CertificateList cert_list_
;
186 base::ThreadChecker thread_checker_
;
188 // TaskRunner for crypto calls.
189 scoped_refptr
<base::SequencedTaskRunner
> crypto_task_runner_
;
191 // TaskRunner for other slow tasks. May be set in tests.
192 scoped_refptr
<base::TaskRunner
> slow_task_runner_for_test_
;
194 // This factory should be used only for callbacks during TPMToken
196 base::WeakPtrFactory
<CertLoader
> initialize_token_factory_
;
198 // This factory should be used only for callbacks during updating the
200 base::WeakPtrFactory
<CertLoader
> update_certificates_factory_
;
202 DISALLOW_COPY_AND_ASSIGN(CertLoader
);
205 } // namespace chromeos
207 #endif // CHROMEOS_CERT_LOADER_H_