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/weak_ptr.h"
13 #include "base/observer_list.h"
14 #include "base/threading/thread_checker.h"
15 #include "chromeos/chromeos_export.h"
16 #include "chromeos/tpm_token_loader.h"
17 #include "net/cert/cert_database.h"
24 class X509Certificate;
29 // This class is responsible for loading certificates once the TPM is
30 // initialized. It is expected to be constructed on the UI thread and public
31 // methods should all be called from the UI thread.
32 // When certificates have been loaded (after login completes and tpm token is
33 // initialized), or the cert database changes, observers are called with
34 // OnCertificatesLoaded().
35 // TODO(tbarzic): Remove direct dependency on TPMTokenLoader. The reason
36 // TPMTokenLoader has to be observed is to make sure singleton NSS DB is
37 // initialized before certificate loading starts. CertLoader should use
38 // (primary) user specific NSS DB, whose loading already takes this into
39 // account (crypto::GetPrivateSlotForChromeOSUser waits until TPM token is
41 class CHROMEOS_EXPORT CertLoader : public net::CertDatabase::Observer,
42 public TPMTokenLoader::Observer {
46 // Called when the certificates, passed for convenience as |cert_list|,
47 // have completed loading. |initial_load| is true the first time this
49 virtual void OnCertificatesLoaded(const net::CertificateList& cert_list,
50 bool initial_load) = 0;
53 virtual ~Observer() {}
56 // Sets the global instance. Must be called before any calls to Get().
57 static void Initialize();
59 // Destroys the global instance.
60 static void Shutdown();
62 // Gets the global instance. Initialize() must be called first.
63 static CertLoader* Get();
65 // Returns true if the global instance has been initialized.
66 static bool IsInitialized();
68 static std::string GetPkcs11IdForCert(const net::X509Certificate& cert);
70 // Sets the task runner that any slow calls will be made from, e.g. calls
71 // to the NSS database. If not set, uses base::WorkerPool.
72 void SetSlowTaskRunnerForTest(
73 const scoped_refptr<base::TaskRunner>& task_runner);
75 void AddObserver(CertLoader::Observer* observer);
76 void RemoveObserver(CertLoader::Observer* observer);
78 // Returns true if the TPM is available for hardware-backed certificates.
79 bool IsHardwareBacked() const;
81 // Returns true when the certificate list has been requested but not loaded.
82 bool CertificatesLoading() const;
84 bool certificates_loaded() const { return certificates_loaded_; }
86 // This will be empty until certificates_loaded() is true.
87 const net::CertificateList& cert_list() const { return cert_list_; }
89 // Getters for cached TPM token info.
90 std::string tpm_user_pin() const { return tpm_user_pin_; }
91 std::string tpm_token_name() const { return tpm_token_name_; }
92 int tpm_token_slot_id() const { return tpm_token_slot_id_; }
96 virtual ~CertLoader();
98 // Starts certificate loading.
99 void RequestCertificates();
101 // Trigger a certificate load. If a certificate loading task is already in
102 // progress, will start a reload once the current task finished.
103 void LoadCertificates();
105 // Called if a certificate load task is finished.
106 void UpdateCertificates(net::CertificateList* cert_list);
108 void NotifyCertificatesLoaded(bool initial_load);
110 // net::CertDatabase::Observer
111 virtual void OnCACertChanged(const net::X509Certificate* cert) OVERRIDE;
112 virtual void OnCertAdded(const net::X509Certificate* cert) OVERRIDE;
113 virtual void OnCertRemoved(const net::X509Certificate* cert) OVERRIDE;
115 // chromeos::TPMTokenLoader::Observer
116 virtual void OnTPMTokenReady(const std::string& tpm_user_pin,
117 const std::string& tpm_token_name,
118 int tpm_token_slot_id) OVERRIDE;
120 ObserverList<Observer> observers_;
122 // Flags describing current CertLoader state.
123 bool certificates_requested_;
124 bool certificates_loaded_;
125 bool certificates_update_required_;
126 bool certificates_update_running_;
128 // Cached TPM token info. Set when the |OnTPMTokenReady| gets called.
129 std::string tpm_user_pin_;
130 std::string tpm_token_name_;
131 int tpm_token_slot_id_;
133 // Cached Certificates.
134 net::CertificateList cert_list_;
136 base::ThreadChecker thread_checker_;
138 // TaskRunner for other slow tasks. May be set in tests.
139 scoped_refptr<base::TaskRunner> slow_task_runner_for_test_;
141 base::WeakPtrFactory<CertLoader> weak_factory_;
143 DISALLOW_COPY_AND_ASSIGN(CertLoader);
146 } // namespace chromeos
148 #endif // CHROMEOS_CERT_LOADER_H_