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 #include "chromeos/cert_loader.h" | 5 #include "chromeos/cert_loader.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/location.h" | 11 #include "base/location.h" |
12 #include "base/memory/ptr_util.h" | |
12 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
13 #include "base/task_runner_util.h" | 14 #include "base/task_runner_util.h" |
14 #include "base/threading/worker_pool.h" | 15 #include "base/threading/worker_pool.h" |
15 #include "crypto/nss_util.h" | 16 #include "crypto/nss_util.h" |
16 #include "crypto/scoped_nss_types.h" | 17 #include "crypto/scoped_nss_types.h" |
17 #include "net/cert/nss_cert_database.h" | 18 #include "net/cert/nss_cert_database.h" |
18 #include "net/cert/nss_cert_database_chromeos.h" | 19 #include "net/cert/nss_cert_database_chromeos.h" |
19 #include "net/cert/x509_certificate.h" | 20 #include "net/cert/x509_certificate.h" |
20 | 21 |
21 namespace chromeos { | 22 namespace chromeos { |
22 | 23 |
24 namespace { | |
25 | |
26 // Checks if |certificate| is on the given |slot|. | |
27 bool IsCertificateOnSlot(const net::X509Certificate* certificate, | |
28 PK11SlotInfo* slot) { | |
29 crypto::ScopedPK11SlotList slots_for_cert( | |
30 PK11_GetAllSlotsForCert(certificate->os_cert_handle(), NULL)); | |
emaxx
2017/04/24 21:23:13
nit: Maybe use nullptr? (Sorry for proposing this
pmarko
2017/04/25 12:10:02
Done. (whole file)
| |
31 if (!slots_for_cert) | |
32 return false; | |
33 | |
34 for (PK11SlotListElement* slot_element = | |
35 PK11_GetFirstSafe(slots_for_cert.get()); | |
36 slot_element; slot_element = PK11_GetNextSafe(slots_for_cert.get(), | |
37 slot_element, PR_FALSE)) { | |
38 if (slot_element->slot == slot) { | |
39 PK11_FreeSlotListElement(slots_for_cert.get(), slot_element); | |
emaxx
2017/04/24 21:23:13
nit: Maybe drop an explanatory comment here? Somet
pmarko
2017/04/25 12:10:02
Done. (please see if my comment is good or too muc
| |
40 return true; | |
41 } | |
42 } | |
43 return false; | |
44 } | |
45 | |
46 // Goes through all certificates in |cert_list| and copies those certificates | |
47 // which are on |system_slot| to |system_cert_list|. | |
48 void FilterSystemTokenCertificates(const net::CertificateList* cert_list, | |
49 net::CertificateList* system_cert_list, | |
50 crypto::ScopedPK11Slot system_slot) { | |
51 VLOG(1) << "FilterSystemTokenCertificates"; | |
52 if (!system_slot) | |
53 return; | |
54 // Extract certificates which are in the system token into the | |
55 // system_cert_list_ sublist. | |
56 for (auto cert : *cert_list) { | |
57 if (IsCertificateOnSlot(cert.get(), system_slot.get())) { | |
58 system_cert_list->push_back(cert); | |
59 } | |
60 } | |
61 } | |
62 | |
63 } // namespace | |
64 | |
23 static CertLoader* g_cert_loader = NULL; | 65 static CertLoader* g_cert_loader = NULL; |
24 static bool g_force_hardware_backed_for_test = false; | 66 static bool g_force_hardware_backed_for_test = false; |
25 | 67 |
26 // static | 68 // static |
27 void CertLoader::Initialize() { | 69 void CertLoader::Initialize() { |
28 CHECK(!g_cert_loader); | 70 CHECK(!g_cert_loader); |
29 g_cert_loader = new CertLoader(); | 71 g_cert_loader = new CertLoader(); |
30 } | 72 } |
31 | 73 |
32 // static | 74 // static |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
89 if (g_force_hardware_backed_for_test) | 131 if (g_force_hardware_backed_for_test) |
90 return true; | 132 return true; |
91 PK11SlotInfo* slot = cert->os_cert_handle()->slot; | 133 PK11SlotInfo* slot = cert->os_cert_handle()->slot; |
92 return slot && PK11_IsHW(slot); | 134 return slot && PK11_IsHW(slot); |
93 } | 135 } |
94 | 136 |
95 bool CertLoader::CertificatesLoading() const { | 137 bool CertLoader::CertificatesLoading() const { |
96 return database_ && !certificates_loaded_; | 138 return database_ && !certificates_loaded_; |
97 } | 139 } |
98 | 140 |
141 void CertLoader::SetSlowTaskRunnerForTest( | |
142 const scoped_refptr<base::TaskRunner>& task_runner) { | |
143 slow_task_runner_for_test_ = task_runner; | |
144 } | |
145 | |
99 // static | 146 // static |
100 void CertLoader::ForceHardwareBackedForTesting() { | 147 void CertLoader::ForceHardwareBackedForTesting() { |
101 g_force_hardware_backed_for_test = true; | 148 g_force_hardware_backed_for_test = true; |
102 } | 149 } |
103 | 150 |
104 // static | 151 // static |
105 // | 152 // |
106 // For background see this discussion on dev-tech-crypto.lists.mozilla.org: | 153 // For background see this discussion on dev-tech-crypto.lists.mozilla.org: |
107 // http://web.archiveorange.com/archive/v/6JJW7E40sypfZGtbkzxX | 154 // http://web.archiveorange.com/archive/v/6JJW7E40sypfZGtbkzxX |
108 // | 155 // |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
141 | 188 |
142 if (certificates_update_running_) { | 189 if (certificates_update_running_) { |
143 certificates_update_required_ = true; | 190 certificates_update_required_ = true; |
144 return; | 191 return; |
145 } | 192 } |
146 | 193 |
147 certificates_update_running_ = true; | 194 certificates_update_running_ = true; |
148 certificates_update_required_ = false; | 195 certificates_update_required_ = false; |
149 | 196 |
150 database_->ListCerts( | 197 database_->ListCerts( |
151 base::Bind(&CertLoader::UpdateCertificates, weak_factory_.GetWeakPtr())); | 198 base::Bind(&CertLoader::CertificatesLoaded, weak_factory_.GetWeakPtr())); |
199 } | |
200 | |
201 void CertLoader::CertificatesLoaded( | |
202 std::unique_ptr<net::CertificateList> cert_list) { | |
203 CHECK(thread_checker_.CalledOnValidThread()); | |
204 VLOG(1) << "CertificatesLoaded: " << cert_list->size(); | |
205 | |
206 crypto::ScopedPK11Slot system_slot = database_->GetSystemSlot(); | |
207 std::unique_ptr<net::CertificateList> system_cert_list = | |
208 base::MakeUnique<net::CertificateList>(); | |
209 GetSlowTaskRunner()->PostTaskAndReply( | |
210 FROM_HERE, | |
211 base::Bind( | |
212 &FilterSystemTokenCertificates, base::Unretained(cert_list.get()), | |
213 base::Unretained(system_cert_list.get()), base::Passed(&system_slot)), | |
214 base::Bind(&CertLoader::UpdateCertificates, weak_factory_.GetWeakPtr(), | |
215 base::Passed(&cert_list), base::Passed(&system_cert_list))); | |
152 } | 216 } |
153 | 217 |
154 void CertLoader::UpdateCertificates( | 218 void CertLoader::UpdateCertificates( |
155 std::unique_ptr<net::CertificateList> cert_list) { | 219 std::unique_ptr<net::CertificateList> cert_list, |
220 std::unique_ptr<net::CertificateList> system_cert_list) { | |
156 CHECK(thread_checker_.CalledOnValidThread()); | 221 CHECK(thread_checker_.CalledOnValidThread()); |
157 DCHECK(certificates_update_running_); | 222 DCHECK(certificates_update_running_); |
158 VLOG(1) << "UpdateCertificates: " << cert_list->size(); | 223 VLOG(1) << "UpdateCertificates: " << cert_list->size() << " (" |
224 << system_cert_list->size() << " on system slot)"; | |
159 | 225 |
160 // Ignore any existing certificates. | 226 // Ignore any existing certificates. |
161 cert_list_ = std::move(cert_list); | 227 cert_list_ = std::move(cert_list); |
228 system_cert_list_ = std::move(system_cert_list); | |
162 | 229 |
163 bool initial_load = !certificates_loaded_; | 230 bool initial_load = !certificates_loaded_; |
164 certificates_loaded_ = true; | 231 certificates_loaded_ = true; |
165 NotifyCertificatesLoaded(initial_load); | 232 NotifyCertificatesLoaded(initial_load); |
166 | 233 |
167 certificates_update_running_ = false; | 234 certificates_update_running_ = false; |
168 if (certificates_update_required_) | 235 if (certificates_update_required_) |
169 LoadCertificates(); | 236 LoadCertificates(); |
170 } | 237 } |
171 | 238 |
172 void CertLoader::NotifyCertificatesLoaded(bool initial_load) { | 239 void CertLoader::NotifyCertificatesLoaded(bool initial_load) { |
173 for (auto& observer : observers_) | 240 for (auto& observer : observers_) |
174 observer.OnCertificatesLoaded(*cert_list_, initial_load); | 241 observer.OnCertificatesLoaded(*cert_list_, initial_load); |
175 } | 242 } |
176 | 243 |
177 void CertLoader::OnCertDBChanged() { | 244 void CertLoader::OnCertDBChanged() { |
178 VLOG(1) << "OnCertDBChanged"; | 245 VLOG(1) << "OnCertDBChanged"; |
179 LoadCertificates(); | 246 LoadCertificates(); |
180 } | 247 } |
181 | 248 |
249 scoped_refptr<base::TaskRunner> CertLoader::GetSlowTaskRunner() const { | |
250 if (slow_task_runner_for_test_.get()) | |
251 return slow_task_runner_for_test_; | |
252 return base::WorkerPool::GetTaskRunner(true /*task is slow*/); | |
emaxx
2017/04/24 21:23:13
I'm concerned about using WorkerPool. It's used on
pmarko
2017/04/25 12:10:02
Good find, thanks! I used WorkerPool because that'
emaxx
2017/04/25 15:15:58
Yes, I think the remaining uses of WorkerPool are
| |
253 } | |
254 | |
182 } // namespace chromeos | 255 } // namespace chromeos |
OLD | NEW |