Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(431)

Side by Side Diff: chromeos/network/client_cert_resolver.cc

Issue 22327005: Automatically resolve ClientCertificatePatterns. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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.
4
5 #include "chromeos/network/client_cert_resolver.h"
6
7 #include <cert.h>
8 #include <certt.h> // for (SECCertUsageEnum) certUsageAnyCA
9 #include <pk11pub.h>
10 #include <algorithm>
11 #include <string>
12
13 #include "base/stl_util.h"
14 #include "base/task_runner.h"
15 #include "base/threading/worker_pool.h"
16 #include "base/time/time.h"
17 #include "chromeos/cert_loader.h"
18 #include "chromeos/dbus/dbus_thread_manager.h"
19 #include "chromeos/dbus/shill_service_client.h"
20 #include "chromeos/network/certificate_pattern.h"
21 #include "chromeos/network/client_cert_util.h"
22 #include "chromeos/network/managed_network_configuration_handler.h"
23 #include "chromeos/network/network_state.h"
24 #include "chromeos/network/network_state_handler.h"
25 #include "chromeos/network/network_ui_data.h"
26 #include "chromeos/network/onc/onc_constants.h"
27 #include "dbus/object_path.h"
28 #include "net/cert/x509_certificate.h"
29
30 namespace {
31
32 template <class T>
33 bool ContainsValue(const std::vector<T>& vector, const T& value) {
34 return find(vector.begin(), vector.end(), value) != vector.end();
35 }
36
37 bool HasPrivateKey(const net::X509Certificate& cert) {
38 PK11SlotInfo* slot = PK11_KeyForCertExists(cert.os_cert_handle(), NULL, NULL);
39 if (!slot)
40 return false;
41
42 PK11_FreeSlot(slot);
43 return true;
44 }
45
46 } // namespace
47
48 namespace chromeos {
49
50 struct ClientCertResolver::NetworkAndMatchingCert {
51 NetworkAndMatchingCert(const std::string& network_path,
52 client_cert::ConfigType config_type,
53 const std::string& cert_id)
54 : service_path(network_path),
55 cert_config_type(config_type),
56 pkcs11_id(cert_id) {}
57 std::string service_path;
58 client_cert::ConfigType cert_config_type;
59 // The id of the matching certificate.
60 std::string pkcs11_id;
61 };
stevenjb 2013/08/07 19:04:51 This is kind of confusing up here, maybe move to j
pneubeck (no reviews) 2013/08/08 12:41:03 It's used below in FindCertificateMatches, thus C+
stevenjb 2013/08/08 16:35:11 That helps I think, thanks.
62
63 namespace {
64
65 struct CertAndIssuer {
66 CertAndIssuer(const scoped_refptr<net::X509Certificate>& certificate,
67 const std::string& issuer)
68 : cert(certificate), pem_encoded_issuer(issuer) {}
stevenjb 2013/08/07 19:04:51 one member per line
pneubeck (no reviews) 2013/08/08 12:41:03 Done.
69 scoped_refptr<net::X509Certificate> cert;
70 std::string pem_encoded_issuer;
71 };
72
73 bool CompareCertExpiration(const CertAndIssuer& a,
74 const CertAndIssuer& b) {
75 return (a.cert->valid_expiry() < b.cert->valid_expiry());
76 }
77
78 struct NetworkAndCertPattern {
79 NetworkAndCertPattern(const std::string& network_path,
80 client_cert::ConfigType config_type,
81 const CertificatePattern& pattern)
82 : service_path(network_path),
83 cert_config_type(config_type),
84 client_cert_pattern(pattern) {}
85 std::string service_path;
86 client_cert::ConfigType cert_config_type;
87 CertificatePattern client_cert_pattern;
88 };
89
90 struct MatchCertWithPattern {
91 MatchCertWithPattern(const NetworkAndCertPattern& pattern)
92 : net_and_pattern(pattern) {}
93
94 bool operator()(const CertAndIssuer& cert_and_issuer) {
95 const CertificatePattern& pattern = net_and_pattern.client_cert_pattern;
96 if (!pattern.issuer().Empty() &&
97 !client_cert::CertPrincipalMatches(pattern.issuer(),
98 cert_and_issuer.cert->issuer())) {
99 return false;
100 }
101 if (!pattern.issuer().Empty() &&
102 !client_cert::CertPrincipalMatches(pattern.issuer(),
103 cert_and_issuer.cert->subject())) {
104 return false;
105 }
106
107 const std::vector<std::string>& issuer_ca_pems = pattern.issuer_ca_pems();
108 if (!issuer_ca_pems.empty() &&
109 !ContainsValue(issuer_ca_pems, cert_and_issuer.pem_encoded_issuer)) {
110 return false;
111 }
112 return true;
113 }
114
115 NetworkAndCertPattern net_and_pattern;
116 };
117
118 void FindCertificateMatches(
119 const net::CertificateList& certs,
120 std::vector<NetworkAndCertPattern>* networks,
121 std::vector<ClientCertResolver::NetworkAndMatchingCert>* matches) {
stevenjb 2013/08/07 19:04:51 nit: typedef these vectors for better readability
pneubeck (no reviews) 2013/08/08 12:41:03 Done.
122 std::vector<CertAndIssuer> client_certs;
123 for (net::CertificateList::const_iterator it = certs.begin();
124 it != certs.end();
125 ++it) {
126 const net::X509Certificate& cert = **it;
127 if (cert.valid_expiry().is_null() || cert.HasExpired() ||
128 !HasPrivateKey(cert)) {
129 continue;
130 }
131 CERTCertificate* issuer = CERT_FindCertIssuer(
132 cert.os_cert_handle(), PR_Now(), certUsageAnyCA);
133 if (!issuer) {
134 LOG(ERROR) << "Couldn't find cert issuer.";
135 continue;
136 }
137 std::string pem_encoded_issuer;
138 if (!net::X509Certificate::GetPEMEncoded(issuer, &pem_encoded_issuer)) {
139 LOG(ERROR) << "Couldn't PEM-encode certificate.";
140 continue;
141 }
142 client_certs.push_back(CertAndIssuer(*it, pem_encoded_issuer));
143 }
144
145 std::sort(client_certs.begin(), client_certs.end(), &CompareCertExpiration);
146
147 for (std::vector<NetworkAndCertPattern>::const_iterator it =
148 networks->begin();
149 it != networks->end();
150 ++it) {
151 std::vector<CertAndIssuer>::iterator cert_it = std::find_if(
152 client_certs.begin(), client_certs.end(), MatchCertWithPattern(*it));
153 if (cert_it == client_certs.end()) {
154 LOG(WARNING) << "Couldn't find a matching client cert for network "
155 << it->service_path;
156 continue;
157 }
158 std::string pkcs11_id = CertLoader::GetPkcs11IdForCert(*cert_it->cert);
159 if (pkcs11_id.empty()) {
160 LOG(ERROR) << "Couldn't determine PKCS#11 ID.";
161 continue;
162 }
163 matches->push_back(ClientCertResolver::NetworkAndMatchingCert(
164 it->service_path, it->cert_config_type, pkcs11_id));
165 }
166 }
167
168 bool OncToClientCertConfigurationType(
169 const base::DictionaryValue& network_config,
170 client_cert::ConfigType* config_type) {
171 using namespace ::chromeos::onc;
172
173 const base::DictionaryValue* wifi = NULL;
174 network_config.GetDictionaryWithoutPathExpansion(network_config::kWiFi,
175 &wifi);
176 if (wifi) {
177 const base::DictionaryValue* eap = NULL;
178 wifi->GetDictionaryWithoutPathExpansion(wifi::kEAP, &eap);
179 if (!eap)
180 return false;
181 *config_type = client_cert::CONFIG_TYPE_EAP;
182 return true;
183 }
184
185 const base::DictionaryValue* vpn = NULL;
186 network_config.GetDictionaryWithoutPathExpansion(network_config::kVPN,
187 &vpn);
188 if (vpn) {
189 const base::DictionaryValue* openvpn = NULL;
190 vpn->GetDictionaryWithoutPathExpansion(vpn::kOpenVPN, &openvpn);
191 if (openvpn) {
192 *config_type = client_cert::CONFIG_TYPE_OPENVPN;
193 return true;
194 }
195
196 const base::DictionaryValue* ipsec = NULL;
197 vpn->GetDictionaryWithoutPathExpansion(vpn::kIPsec, &ipsec);
198 if (ipsec) {
199 *config_type = client_cert::CONFIG_TYPE_IPSEC;
200 return true;
201 }
202
203 return false;
204 }
205
206 const base::DictionaryValue* ethernet = NULL;
207 network_config.GetDictionaryWithoutPathExpansion(network_config::kEthernet,
208 &ethernet);
209 if (ethernet) {
210 const base::DictionaryValue* eap = NULL;
211 ethernet->GetDictionaryWithoutPathExpansion(wifi::kEAP, &eap);
212 if (!eap)
213 return false;
214 *config_type = client_cert::CONFIG_TYPE_EAP;
215 return true;
216 }
217
218 return false;
219 }
220
221 void LogError(const std::string& service_path,
222 const std::string& dbus_error_name,
223 const std::string& dbus_error_message) {
224 network_handler::ShillErrorCallbackFunction(
225 "ClientCertResolver.SetProperties failed",
226 service_path,
227 network_handler::ErrorCallback(),
228 dbus_error_name,
229 dbus_error_message);
230 }
231
232 bool CertificatesLoaded() {
233 if(!CertLoader::Get()->certificates_loaded()) {
234 VLOG(1) << "Certificates not loaded yet.";
235 return false;
236 }
237 if (!CertLoader::Get()->IsHardwareBacked()) {
238 VLOG(1) << "TPM is not available.";
239 return false;
240 }
241 return true;
242 }
243
244 } // namespace
245
246 ClientCertResolver::ClientCertResolver()
247 : network_state_handler_(NULL),
248 managed_network_config_handler_(NULL),
249 weak_ptr_factory_(this) {}
stevenjb 2013/08/07 19:04:51 nit: } on next line
pneubeck (no reviews) 2013/08/08 12:41:03 Done.
250
251 ClientCertResolver::~ClientCertResolver() {
252 if (network_state_handler_)
253 network_state_handler_->RemoveObserver(this, FROM_HERE);
254 if (CertLoader::IsInitialized())
255 CertLoader::Get()->RemoveObserver(this);
256 if (managed_network_config_handler_)
257 managed_network_config_handler_->RemoveObserver(this);
258 }
259
260 void ClientCertResolver::Init(
261 NetworkStateHandler* network_state_handler,
262 ManagedNetworkConfigurationHandler* managed_network_config_handler) {
263 DCHECK(network_state_handler);
264 network_state_handler_ = network_state_handler;
265 network_state_handler_->AddObserver(this, FROM_HERE);
266
267 DCHECK(managed_network_config_handler);
268 managed_network_config_handler_ = managed_network_config_handler;
269 managed_network_config_handler_->AddObserver(this);
270
271 CertLoader::Get()->AddObserver(this);
272 }
273
274 void ClientCertResolver::SetSlowTaskRunnerForTest(
275 const scoped_refptr<base::TaskRunner>& task_runner) {
276 slow_task_runner_for_test_ = task_runner;
277 }
278
279 void ClientCertResolver::NetworkListChanged() {
280 if (!CertificatesLoaded())
281 return;
282 // Configure only networks that were not configured before.
283
284 // We'll drop networks from the set, which are not known anymore.
285 std::set<std::string> old_resolved_networks;
286 old_resolved_networks.swap(resolved_networks_);
287
288 NetworkStateList networks;
289 network_state_handler_->GetNetworkList(&networks);
290
291 NetworkStateList networks_to_check;
292 for (NetworkStateList::const_iterator it = networks.begin();
293 it != networks.end();
294 ++it) {
295 const std::string& service_path = (*it)->path();
296 if (ContainsKey(old_resolved_networks, service_path)) {
297 resolved_networks_.insert(service_path);
298 continue;
299 }
300 networks_to_check.push_back(*it);
301 }
stevenjb 2013/08/07 19:04:51 I'm a little concerned with this. Because Shill fo
pneubeck (no reviews) 2013/08/08 12:41:03 That's not right. The new list (resolved_networks_
stevenjb 2013/08/08 16:35:11 Ahh... right... it's a swap not a merge. OK, that'
302
303 ResolveNetworks(networks_to_check);
304 }
305
306 void ClientCertResolver::OnCertificatesLoaded(
307 const net::CertificateList& cert_list,
308 bool initial_load) {
309 if (!CertificatesLoaded())
310 return;
311 // Compare all networks with all certificates.
312 NetworkStateList networks;
313 network_state_handler_->GetNetworkList(&networks);
314 ResolveNetworks(networks);
315 }
316
317 void ClientCertResolver::PolicyApplied(const std::string& service_path) {
318 if (!CertificatesLoaded())
319 return;
320 // Compare this network with all certificates.
321 const NetworkState* network =
322 network_state_handler_->GetNetworkState(service_path);
323 if (!network) {
324 LOG(ERROR) << "service path '" << service_path << "' unknown.";
325 return;
326 }
327 NetworkStateList networks;
328 networks.push_back(network);
329 ResolveNetworks(networks);
330 }
331
332 void ClientCertResolver::ResolveNetworks(
333 const NetworkStateList& networks) {
334 scoped_ptr<std::vector<NetworkAndCertPattern> > networks_with_pattern(
335 new std::vector<NetworkAndCertPattern>);
336
337 // Filter networks with ClientCertPattern. As ClientCertPatterns can only be
338 // set by policy, we check there.
339 for (NetworkStateList::const_iterator it = networks.begin();
340 it != networks.end();
341 ++it) {
342 const NetworkState& network = **it;
stevenjb 2013/08/07 19:04:51 Fwiw, most of the rest of the code uses a const* i
pneubeck (no reviews) 2013/08/08 12:41:03 I strongly prefer the enforcement of not-null by t
343
344 // In any case, don't check this network again in NetworkListChanged.
345 resolved_networks_.insert(network.path());
346
347 // If this network is not managed, thus cannot have a ClientCertPattern.
stevenjb 2013/08/07 19:04:51 s/thus/it
pneubeck (no reviews) 2013/08/08 12:41:03 Done.
348 if (network.guid().empty())
349 continue;
stevenjb 2013/08/07 19:04:51 We can also use this to filter out networks to con
pneubeck (no reviews) 2013/08/08 12:41:03 Done.
350
351 DCHECK(!network.profile_path().empty());
stevenjb 2013/08/07 19:04:51 Is this a valid assertion? Shill will never set a
pneubeck (no reviews) 2013/08/08 12:41:03 Done.
352 const base::DictionaryValue* policy =
353 managed_network_config_handler_->FindPolicyByGuidAndProfile(
354 network.guid(), network.profile_path());
355
356 if (!policy) {
357 VLOG(1) << "The policy for network " << network.path() << " with GUID "
358 << network.guid() << " is not available yet.";
359 // Skip this network for now. Once the policy is loaded, PolicyApplied()
360 // will retry.
361 continue;
362 }
363
364 VLOG(2) << "Inspecting network " << network.path();
365 // TODO(pneubeck): Access the ClientCertPattern without relying on
366 // NetworkUIData, which also parses other things that we don't need here.
367 // The ONCSource is irrelevant here.
368 scoped_ptr<NetworkUIData> ui_data(
369 NetworkUIData::CreateFromONC(onc::ONC_SOURCE_NONE, *policy));
370
371 // Skip networks that don't have a ClientCertPattern.
372 if (ui_data->certificate_type() != CLIENT_CERT_TYPE_PATTERN)
373 continue;
374
375 client_cert::ConfigType config_type =
376 client_cert::CONFIG_TYPE_OPENVPN; // Initialize to arbitrary value.
377 if (!OncToClientCertConfigurationType(*policy, &config_type)) {
378 LOG(ERROR) << "UIData contains a CertificatePattern, but the policy "
379 << "doesn't. Network: " << network.path();
380 continue;
381 }
382
383 networks_with_pattern->push_back(NetworkAndCertPattern(
384 network.path(), config_type, ui_data->certificate_pattern()));
385 }
386 if (networks_with_pattern->empty())
387 return;
388
389 VLOG(2) << "Start task for resolving client cert patterns.";
390 base::TaskRunner* task_runner = slow_task_runner_for_test_.get();
391 if (!task_runner)
392 task_runner = base::WorkerPool::GetTaskRunner(true /* task is slow */);
393
394 std::vector<NetworkAndMatchingCert>* matches =
395 new std::vector<NetworkAndMatchingCert>;
396 task_runner->PostTaskAndReply(
397 FROM_HERE,
398 base::Bind(&FindCertificateMatches,
399 CertLoader::Get()->cert_list(),
400 base::Owned(networks_with_pattern.release()),
401 matches),
402 base::Bind(&ClientCertResolver::ConfigureCertificates,
403 weak_ptr_factory_.GetWeakPtr(),
404 base::Owned(matches)));
405 }
406
407 void ClientCertResolver::ConfigureCertificates(
408 std::vector<NetworkAndMatchingCert>* matches) {
409 for (std::vector<NetworkAndMatchingCert>::const_iterator it =
410 matches->begin();
411 it != matches->end();
412 ++it) {
413 VLOG(1) << "Configuring certificate of network " << it->service_path;
414 CertLoader* cert_loader = CertLoader::Get();
415 base::DictionaryValue shill_properties;
416 client_cert::SetShillProperties(it->cert_config_type,
417 cert_loader->tpm_token_slot(),
418 cert_loader->tpm_user_pin(),
419 it->pkcs11_id,
420 &shill_properties);
421 DBusThreadManager::Get()->GetShillServiceClient()
422 ->SetProperties(dbus::ObjectPath(it->service_path),
stevenjb 2013/08/07 19:04:51 nit: -> on prev line
pneubeck (no reviews) 2013/08/08 12:41:03 as all other formatting issues, this is what clang
stevenjb 2013/08/08 16:35:11 Yeah, the clang formatting came up somewhere else.
423 shill_properties,
424 base::Bind(&base::DoNothing),
425 base::Bind(&LogError, it->service_path));
426 network_state_handler_->RequestUpdateForNetwork(it->service_path);
427 }
428 }
429
430 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698