OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROMEOS_CERT_LOADER_H_ | 5 #ifndef CHROMEOS_CERT_LOADER_H_ |
6 #define CHROMEOS_CERT_LOADER_H_ | 6 #define CHROMEOS_CERT_LOADER_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
13 #include "base/observer_list.h" | 13 #include "base/observer_list.h" |
14 #include "base/threading/thread_checker.h" | 14 #include "base/threading/thread_checker.h" |
15 #include "chromeos/chromeos_export.h" | 15 #include "chromeos/chromeos_export.h" |
16 #include "chromeos/tpm_token_loader.h" | |
17 #include "net/cert/cert_database.h" | 16 #include "net/cert/cert_database.h" |
18 | 17 |
19 namespace base { | 18 namespace base { |
20 class TaskRunner; | 19 class TaskRunner; |
21 } | 20 } |
22 | 21 |
23 namespace net { | 22 namespace net { |
| 23 class NSSCertDatabaseChromeOS; |
24 class X509Certificate; | 24 class X509Certificate; |
25 } | 25 } |
26 | 26 |
27 namespace chromeos { | 27 namespace chromeos { |
28 | 28 |
29 // This class is responsible for loading certificates once the TPM is | 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 | 30 // initialized. It is expected to be constructed on the UI thread and public |
31 // methods should all be called from the UI thread. | 31 // methods should all be called from the UI thread. |
32 // When certificates have been loaded (after login completes and tpm token is | 32 // When certificates have been loaded (after login completes and tpm token is |
33 // initialized), or the cert database changes, observers are called with | 33 // initialized), or the cert database changes, observers are called with |
34 // OnCertificatesLoaded(). | 34 // OnCertificatesLoaded(). |
35 // TODO(tbarzic): Remove direct dependency on TPMTokenLoader. The reason | 35 class CHROMEOS_EXPORT CertLoader : public net::CertDatabase::Observer { |
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 | |
40 // ready). | |
41 class CHROMEOS_EXPORT CertLoader : public net::CertDatabase::Observer, | |
42 public TPMTokenLoader::Observer { | |
43 public: | 36 public: |
44 class Observer { | 37 class Observer { |
45 public: | 38 public: |
46 virtual ~Observer() {} | 39 virtual ~Observer() {} |
47 | 40 |
48 // Called when the certificates, passed for convenience as |cert_list|, | 41 // Called when the certificates, passed for convenience as |cert_list|, |
49 // have completed loading. |initial_load| is true the first time this | 42 // have completed loading. |initial_load| is true the first time this |
50 // is called. | 43 // is called. |
51 virtual void OnCertificatesLoaded(const net::CertificateList& cert_list, | 44 virtual void OnCertificatesLoaded(const net::CertificateList& cert_list, |
52 bool initial_load) = 0; | 45 bool initial_load) = 0; |
53 }; | 46 }; |
54 | 47 |
55 // Sets the global instance. Must be called before any calls to Get(). | 48 // Sets the global instance. Must be called before any calls to Get(). |
56 static void Initialize(); | 49 static void Initialize(); |
57 | 50 |
58 // Destroys the global instance. | 51 // Destroys the global instance. |
59 static void Shutdown(); | 52 static void Shutdown(); |
60 | 53 |
61 // Gets the global instance. Initialize() must be called first. | 54 // Gets the global instance. Initialize() must be called first. |
62 static CertLoader* Get(); | 55 static CertLoader* Get(); |
63 | 56 |
64 // Returns true if the global instance has been initialized. | 57 // Returns true if the global instance has been initialized. |
65 static bool IsInitialized(); | 58 static bool IsInitialized(); |
66 | 59 |
67 static std::string GetPkcs11IdForCert(const net::X509Certificate& cert); | 60 static std::string GetPkcs11IdForCert(const net::X509Certificate& cert); |
68 | 61 |
| 62 // Starts the CertLoader with the user. It requests a user's NSS cert |
| 63 // database instance, and once it gets it, it starts loading certificates from |
| 64 // it. The database can be requested before TPM token is ready, but it will |
| 65 // not be returned before that. It may be called only once. |
| 66 // InitForUser must not be called before crypto::InitializeNSSForChromeOSUser |
| 67 // is called for the user. |
| 68 void StartWithUser(const std::string& userhash); |
| 69 |
| 70 // |crypto_task_runner| is the task runner that any synchronous crypto calls |
| 71 // should be made from, e.g. in Chrome this is the IO thread. Must be called |
| 72 // after the thread is started. CertLoader uses the task runner only for |
| 73 // creating its NSS cert database instance. The actual calls to the database |
| 74 // are done on the base::WorkerPool. |
| 75 void SetCryptoTaskRunner( |
| 76 const scoped_refptr<base::SequencedTaskRunner>& crypto_task_runner); |
| 77 |
69 // Sets the task runner that any slow calls will be made from, e.g. calls | 78 // Sets the task runner that any slow calls will be made from, e.g. calls |
70 // to the NSS database. If not set, uses base::WorkerPool. | 79 // to the NSS database. If not set, uses base::WorkerPool. |
71 void SetSlowTaskRunnerForTest( | 80 void SetSlowTaskRunnerForTest( |
72 const scoped_refptr<base::TaskRunner>& task_runner); | 81 const scoped_refptr<base::TaskRunner>& task_runner); |
73 | 82 |
74 void AddObserver(CertLoader::Observer* observer); | 83 void AddObserver(CertLoader::Observer* observer); |
75 void RemoveObserver(CertLoader::Observer* observer); | 84 void RemoveObserver(CertLoader::Observer* observer); |
76 | 85 |
77 // Returns true if the TPM is available for hardware-backed certificates. | 86 // Whether |cert| belongs to the CertLoader's NSS database's private slot. |
78 bool IsHardwareBacked() const; | 87 // Returns false if certificates are not yet loaded. |
| 88 bool IsCertificateInPrivateSlot(const net::X509Certificate& cert) const; |
79 | 89 |
80 // Returns true when the certificate list has been requested but not loaded. | 90 // Returns true when the certificate list has been requested but not loaded. |
81 bool CertificatesLoading() const; | 91 bool CertificatesLoading() const; |
82 | 92 |
83 bool certificates_loaded() const { return certificates_loaded_; } | 93 bool certificates_loaded() const { return certificates_loaded_; } |
84 | 94 |
85 // This will be empty until certificates_loaded() is true. | 95 // This will be empty until certificates_loaded() is true. |
86 const net::CertificateList& cert_list() const { return cert_list_; } | 96 const net::CertificateList& cert_list() const { return cert_list_; } |
87 | 97 |
88 // Getters for cached TPM token info. | 98 // Note that |is_hardware_backed()| may give false negatives if |
89 std::string tpm_user_pin() const { return tpm_user_pin_; } | 99 // |certificates_loaded()| is false. |
90 std::string tpm_token_name() const { return tpm_token_name_; } | 100 bool is_hardware_backed() const { return is_hardware_backed_; } |
| 101 void set_hardware_backed_for_test() { hardware_backed_for_test_ = true; } |
91 int tpm_token_slot_id() const { return tpm_token_slot_id_; } | 102 int tpm_token_slot_id() const { return tpm_token_slot_id_; } |
92 | 103 |
93 private: | 104 private: |
94 CertLoader(); | 105 CertLoader(); |
95 virtual ~CertLoader(); | 106 virtual ~CertLoader(); |
96 | 107 |
97 // Starts certificate loading. | 108 // Starts the loader. It initiates loading of the user's NSS database. |
98 void RequestCertificates(); | 109 void RequestCertificates(); |
99 | 110 |
| 111 // Callback for user's NSS database loading. It caches user's TPM token info, |
| 112 // starts observing net::CertDatabse and triggers initial certificate load. |
| 113 void OnDatabaseReady(scoped_ptr<net::NSSCertDatabaseChromeOS> database); |
| 114 |
100 // Trigger a certificate load. If a certificate loading task is already in | 115 // Trigger a certificate load. If a certificate loading task is already in |
101 // progress, will start a reload once the current task finished. | 116 // progress, will start a reload once the current task is finished. |
102 void LoadCertificates(); | 117 void LoadCertificates(); |
103 | 118 |
104 // Called if a certificate load task is finished. | 119 // Called if a certificate load task is finished. |
105 void UpdateCertificates(net::CertificateList* cert_list); | 120 void UpdateCertificates(net::CertificateList* cert_list); |
106 | 121 |
107 void NotifyCertificatesLoaded(bool initial_load); | 122 void NotifyCertificatesLoaded(bool initial_load); |
108 | 123 |
109 // net::CertDatabase::Observer | 124 // net::CertDatabase::Observer |
110 virtual void OnCACertChanged(const net::X509Certificate* cert) OVERRIDE; | 125 virtual void OnCACertChanged(const net::X509Certificate* cert) OVERRIDE; |
111 virtual void OnCertAdded(const net::X509Certificate* cert) OVERRIDE; | 126 virtual void OnCertAdded(const net::X509Certificate* cert) OVERRIDE; |
112 virtual void OnCertRemoved(const net::X509Certificate* cert) OVERRIDE; | 127 virtual void OnCertRemoved(const net::X509Certificate* cert) OVERRIDE; |
113 | 128 |
114 // chromeos::TPMTokenLoader::Observer | |
115 virtual void OnTPMTokenReady(const std::string& tpm_user_pin, | |
116 const std::string& tpm_token_name, | |
117 int tpm_token_slot_id) OVERRIDE; | |
118 | |
119 ObserverList<Observer> observers_; | 129 ObserverList<Observer> observers_; |
120 | 130 |
121 // Flags describing current CertLoader state. | 131 // Flags describing current CertLoader state. |
122 bool certificates_requested_; | 132 bool certificates_requested_; |
123 bool certificates_loaded_; | 133 bool certificates_loaded_; |
124 bool certificates_update_required_; | 134 bool certificates_update_required_; |
125 bool certificates_update_running_; | 135 bool certificates_update_running_; |
126 | 136 |
127 // Cached TPM token info. Set when the |OnTPMTokenReady| gets called. | 137 // The username hash for the user the CertLoader is bound to. |
128 std::string tpm_user_pin_; | 138 std::string userhash_; |
129 std::string tpm_token_name_; | 139 |
| 140 // The user-specific NSS certificate database from which the certificates |
| 141 // should be loaded. The database is loaded on crypto_task_runner_. |
| 142 scoped_ptr<net::NSSCertDatabaseChromeOS> database_; |
| 143 |
| 144 // The user NSS database's private slot id. |
130 int tpm_token_slot_id_; | 145 int tpm_token_slot_id_; |
131 | 146 |
132 // Cached Certificates. | 147 // Whether |database_| is hardware backed. The value is not guaranteed to be |
| 148 // correct before |certificates_loaded()| is true. |
| 149 bool is_hardware_backed_; |
| 150 bool hardware_backed_for_test_; |
| 151 |
| 152 // Cached Certificates loaded from the database. |
133 net::CertificateList cert_list_; | 153 net::CertificateList cert_list_; |
134 | 154 |
135 base::ThreadChecker thread_checker_; | 155 base::ThreadChecker thread_checker_; |
136 | 156 |
137 // TaskRunner for other slow tasks. May be set in tests. | 157 // TaskRunner for NSS database loading. |
| 158 scoped_refptr<base::SequencedTaskRunner> crypto_task_runner_; |
| 159 |
| 160 // TaskRunner that, if set, replaces base::WorkerPool. Should only be set in |
| 161 // tests. |
138 scoped_refptr<base::TaskRunner> slow_task_runner_for_test_; | 162 scoped_refptr<base::TaskRunner> slow_task_runner_for_test_; |
139 | 163 |
140 base::WeakPtrFactory<CertLoader> weak_factory_; | 164 base::WeakPtrFactory<CertLoader> weak_factory_; |
141 | 165 |
142 DISALLOW_COPY_AND_ASSIGN(CertLoader); | 166 DISALLOW_COPY_AND_ASSIGN(CertLoader); |
143 }; | 167 }; |
144 | 168 |
145 } // namespace chromeos | 169 } // namespace chromeos |
146 | 170 |
147 #endif // CHROMEOS_CERT_LOADER_H_ | 171 #endif // CHROMEOS_CERT_LOADER_H_ |
OLD | NEW |