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

Side by Side Diff: chrome/browser/chromeos/cros/onc_network_parser.cc

Issue 8566056: This applies GUIDs to certificate and key nicknames when (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: More review changes Created 9 years 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
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "chrome/browser/chromeos/cros/onc_network_parser.h" 5 #include "chrome/browser/chromeos/cros/onc_network_parser.h"
6 6
7 #include "base/base64.h" 7 #include "base/base64.h"
8 #include "base/json/json_value_serializer.h" 8 #include "base/json/json_value_serializer.h"
9 #include "base/stringprintf.h" 9 #include "base/stringprintf.h"
10 #include "base/values.h" 10 #include "base/values.h"
11 #include "chrome/browser/chromeos/cros/native_network_constants.h" 11 #include "chrome/browser/chromeos/cros/native_network_constants.h"
12 #include "chrome/browser/chromeos/cros/native_network_parser.h" 12 #include "chrome/browser/chromeos/cros/native_network_parser.h"
13 #include "chrome/browser/chromeos/cros/network_library.h" 13 #include "chrome/browser/chromeos/cros/network_library.h"
14 #include "net/base/cert_database.h" 14 #include "net/base/cert_database.h"
15 #include "net/base/crypto_module.h" 15 #include "net/base/crypto_module.h"
16 #include "net/base/net_errors.h" 16 #include "net/base/net_errors.h"
17 #include "net/base/x509_certificate.h" 17 #include "net/base/x509_certificate.h"
18 #include "net/base/x509_util_nss.h"
18 #include "third_party/cros_system_api/dbus/service_constants.h" 19 #include "third_party/cros_system_api/dbus/service_constants.h"
19 20
20 namespace chromeos { 21 namespace chromeos {
21 22
22 // Local constants. 23 // Local constants.
23 namespace { 24 namespace {
24 25
25 EnumMapper<PropertyIndex>::Pair property_index_table[] = { 26 EnumMapper<PropertyIndex>::Pair property_index_table[] = {
26 { "GUID", PROPERTY_INDEX_GUID }, 27 { "GUID", PROPERTY_INDEX_GUID },
27 { "Name", PROPERTY_INDEX_NAME }, 28 { "Name", PROPERTY_INDEX_NAME },
(...skipping 28 matching lines...) Expand all
56 57
57 ConnectionType ParseNetworkType(const std::string& type) { 58 ConnectionType ParseNetworkType(const std::string& type) {
58 static EnumMapper<ConnectionType>::Pair table[] = { 59 static EnumMapper<ConnectionType>::Pair table[] = {
59 { "WiFi", TYPE_WIFI }, 60 { "WiFi", TYPE_WIFI },
60 { "VPN", TYPE_VPN }, 61 { "VPN", TYPE_VPN },
61 }; 62 };
62 CR_DEFINE_STATIC_LOCAL(EnumMapper<ConnectionType>, parser, 63 CR_DEFINE_STATIC_LOCAL(EnumMapper<ConnectionType>, parser,
63 (table, arraysize(table), TYPE_UNKNOWN)); 64 (table, arraysize(table), TYPE_UNKNOWN));
64 return parser.Get(type); 65 return parser.Get(type);
65 } 66 }
66
wtc 2011/12/08 00:07:43 Nit: you should keep this blank line to match the
Greg Spencer (Chromium) 2011/12/09 18:51:38 Done.
67 } // namespace 67 } // namespace
68 68
69 // -------------------- OncNetworkParser -------------------- 69 // -------------------- OncNetworkParser --------------------
70 70
71 OncNetworkParser::OncNetworkParser(const std::string& onc_blob) 71 OncNetworkParser::OncNetworkParser(const std::string& onc_blob)
72 : NetworkParser(get_onc_mapper()), 72 : NetworkParser(get_onc_mapper()),
73 network_configs_(NULL), 73 network_configs_(NULL),
74 certificates_(NULL) { 74 certificates_(NULL) {
75 JSONStringValueSerializer deserializer(onc_blob); 75 JSONStringValueSerializer deserializer(onc_blob);
76 deserializer.set_allow_trailing_comma(true); 76 deserializer.set_allow_trailing_comma(true);
(...skipping 26 matching lines...) Expand all
103 } 103 }
104 104
105 int OncNetworkParser::GetNetworkConfigsSize() const { 105 int OncNetworkParser::GetNetworkConfigsSize() const {
106 return network_configs_ ? network_configs_->GetSize() : 0; 106 return network_configs_ ? network_configs_->GetSize() : 0;
107 } 107 }
108 108
109 int OncNetworkParser::GetCertificatesSize() const { 109 int OncNetworkParser::GetCertificatesSize() const {
110 return certificates_ ? certificates_->GetSize() : 0; 110 return certificates_ ? certificates_->GetSize() : 0;
111 } 111 }
112 112
113 bool OncNetworkParser::ParseCertificate(int cert_index) { 113 scoped_refptr<net::X509Certificate> OncNetworkParser::ParseCertificate(
114 int cert_index) {
114 CHECK(certificates_); 115 CHECK(certificates_);
115 CHECK(static_cast<size_t>(cert_index) < certificates_->GetSize()); 116 CHECK(static_cast<size_t>(cert_index) < certificates_->GetSize());
116 CHECK(cert_index >= 0); 117 CHECK(cert_index >= 0);
117 base::DictionaryValue* certificate = NULL; 118 base::DictionaryValue* certificate = NULL;
118 certificates_->GetDictionary(cert_index, &certificate); 119 certificates_->GetDictionary(cert_index, &certificate);
119 CHECK(certificate); 120 CHECK(certificate);
120 121
121 // Get out the attributes of the given cert. 122 // Get out the attributes of the given certificate.
122 std::string guid; 123 std::string guid;
123 bool remove = false; 124 bool remove = false;
124 if (!certificate->GetString("GUID", &guid) || guid.empty()) { 125 if (!certificate->GetString("GUID", &guid) || guid.empty()) {
125 LOG(WARNING) << "ONC File: certificate missing identifier at index" 126 LOG(WARNING) << "ONC File: certificate missing identifier at index"
126 << cert_index; 127 << cert_index;
127 return false; 128 return NULL;
128 } 129 }
129 130
130 if (!certificate->GetBoolean("Remove", &remove)) 131 if (!certificate->GetBoolean("Remove", &remove))
131 remove = false; 132 remove = false;
132 133
133 net::CertDatabase cert_database; 134 net::CertDatabase cert_database;
134 if (remove) 135 if (remove) {
135 return cert_database.DeleteCertAndKeyByLabel(guid); 136 bool success = DeleteCertAndKeyByLabel(guid);
137 DCHECK(success);
138 // TODO(gspencer): return removed certificate?
139 return NULL;
140 }
136 141
137 // Not removing, so let's get the data we need to add this cert. 142 // Not removing, so let's get the data we need to add this certificate.
138 std::string cert_type; 143 std::string cert_type;
139 certificate->GetString("Type", &cert_type); 144 certificate->GetString("Type", &cert_type);
140 if (cert_type == "Server" || cert_type == "Authority") { 145 if (cert_type == "Server" || cert_type == "Authority") {
141 return ParseServerOrCaCertificate(cert_index, cert_type, certificate); 146 return ParseServerOrCaCertificate(cert_index, cert_type, guid, certificate);
142 } 147 }
143 if (cert_type == "Client") { 148 if (cert_type == "Client") {
144 return ParseClientCertificate(cert_index, certificate); 149 return ParseClientCertificate(cert_index, guid, certificate);
145 } 150 }
146 151
147 LOG(WARNING) << "ONC File: certificate of unknown type: " << cert_type 152 LOG(WARNING) << "ONC File: certificate of unknown type: " << cert_type
148 << " at index " << cert_index; 153 << " at index " << cert_index;
149 return false; 154 return NULL;
150 } 155 }
151 156
152 Network* OncNetworkParser::ParseNetwork(int n) { 157 Network* OncNetworkParser::ParseNetwork(int n) {
153 if (!network_configs_) 158 if (!network_configs_)
154 return NULL; 159 return NULL;
155 DictionaryValue* info = NULL; 160 DictionaryValue* info = NULL;
156 if (!network_configs_->GetDictionary(n, &info)) 161 if (!network_configs_->GetDictionary(n, &info))
157 return NULL; 162 return NULL;
158 // Parse Open Network Configuration blob into a temporary Network object. 163 // Parse Open Network Configuration blob into a temporary Network object.
159 return CreateNetworkFromInfo(std::string(), *info); 164 return CreateNetworkFromInfo(std::string(), *info);
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
215 return type_string; 220 return type_string;
216 } 221 }
217 222
218 std::string OncNetworkParser::GetGuidFromDictionary( 223 std::string OncNetworkParser::GetGuidFromDictionary(
219 const base::DictionaryValue& info) { 224 const base::DictionaryValue& info) {
220 std::string guid_string; 225 std::string guid_string;
221 info.GetString("GUID", &guid_string); 226 info.GetString("GUID", &guid_string);
222 return guid_string; 227 return guid_string;
223 } 228 }
224 229
225 bool OncNetworkParser::ParseServerOrCaCertificate( 230 scoped_refptr<net::X509Certificate>
231 OncNetworkParser::ParseServerOrCaCertificate(
226 int cert_index, 232 int cert_index,
227 const std::string& cert_type, 233 const std::string& cert_type,
234 const std::string& guid,
228 base::DictionaryValue* certificate) { 235 base::DictionaryValue* certificate) {
229 net::CertDatabase cert_database; 236 net::CertDatabase cert_database;
230 bool web_trust = false; 237 bool web_trust = false;
231 base::ListValue* trust_list = NULL; 238 base::ListValue* trust_list = NULL;
232 if (certificate->GetList("Trust", &trust_list)) { 239 if (certificate->GetList("Trust", &trust_list)) {
233 for (size_t i = 0; i < trust_list->GetSize(); ++i) { 240 for (size_t i = 0; i < trust_list->GetSize(); ++i) {
234 std::string trust_type; 241 std::string trust_type;
235 if (!trust_list->GetString(i, &trust_type)) { 242 if (!trust_list->GetString(i, &trust_type)) {
236 LOG(WARNING) << "ONC File: certificate trust is invalid at index " 243 LOG(WARNING) << "ONC File: certificate trust is invalid at index "
237 << cert_index; 244 << cert_index;
238 return false; 245 return NULL;
239 } 246 }
240 if (trust_type == "Web") { 247 if (trust_type == "Web") {
241 web_trust = true; 248 web_trust = true;
242 } else { 249 } else {
243 LOG(WARNING) << "ONC File: certificate contains unknown " 250 LOG(WARNING) << "ONC File: certificate contains unknown "
244 << "trust type: " << trust_type 251 << "trust type: " << trust_type
245 << " at index " << cert_index; 252 << " at index " << cert_index;
246 return false; 253 return NULL;
247 } 254 }
248 } 255 }
249 } 256 }
250 257
251 std::string x509_data; 258 std::string x509_data;
252 if (!certificate->GetString("X509", &x509_data) || x509_data.empty()) { 259 if (!certificate->GetString("X509", &x509_data) || x509_data.empty()) {
253 LOG(WARNING) << "ONC File: certificate missing appropriate " 260 LOG(WARNING) << "ONC File: certificate missing appropriate "
254 << "certificate data for type: " << cert_type 261 << "certificate data for type: " << cert_type
255 << " at index " << cert_index; 262 << " at index " << cert_index;
256 return false; 263 return NULL;
257 } 264 }
258 265
259 std::string decoded_x509; 266 std::string decoded_x509;
260 if (!base::Base64Decode(x509_data, &decoded_x509)) { 267 if (!base::Base64Decode(x509_data, &decoded_x509)) {
261 LOG(WARNING) << "Unable to base64 decode X509 data: \"" 268 LOG(WARNING) << "Unable to base64 decode X509 data: \""
262 << x509_data << "\"."; 269 << x509_data << "\".";
263 return false; 270 return NULL;
264 } 271 }
265 272
266 scoped_refptr<net::X509Certificate> x509_cert( 273 scoped_refptr<net::X509Certificate> x509_cert =
267 net::X509Certificate::CreateFromBytes(decoded_x509.c_str(), 274 net::X509Certificate::CreateFromBytesWithNickname(
268 decoded_x509.size())); 275 decoded_x509.c_str(),
276 decoded_x509.size(),
277 guid.c_str());
269 if (!x509_cert.get()) { 278 if (!x509_cert.get()) {
270 LOG(WARNING) << "Unable to create X509 certificate from bytes."; 279 LOG(WARNING) << "Unable to create X509 certificate from bytes.";
271 return false; 280 return NULL;
272 } 281 }
282
283 if (!net::x509_util::SetLabel(x509_cert, guid))
284 return NULL;
wtc 2011/12/08 00:07:43 IMPORTANT: why do you set the label on x509_cert w
Greg Spencer (Chromium) 2011/12/09 18:51:38 I've removed the Label setting code, and I'm now j
285
273 net::CertificateList cert_list; 286 net::CertificateList cert_list;
274 cert_list.push_back(x509_cert); 287 cert_list.push_back(x509_cert);
275 net::CertDatabase::ImportCertFailureList failures; 288 net::CertDatabase::ImportCertFailureList failures;
276 bool success = false; 289 bool success = false;
277 if (cert_type == "Server") { 290 if (cert_type == "Server") {
278 success = cert_database.ImportServerCert(cert_list, &failures); 291 success = cert_database.ImportServerCert(cert_list, &failures);
279 } else { // Authority cert 292 } else { // Authority cert
280 net::CertDatabase::TrustBits trust = web_trust ? 293 net::CertDatabase::TrustBits trust = web_trust ?
281 net::CertDatabase::TRUSTED_SSL : 294 net::CertDatabase::TRUSTED_SSL :
282 net::CertDatabase::UNTRUSTED; 295 net::CertDatabase::UNTRUSTED;
283 success = cert_database.ImportCACerts(cert_list, trust, &failures); 296 success = cert_database.ImportCACerts(cert_list, trust, &failures);
284 } 297 }
285 if (!failures.empty()) { 298 if (!failures.empty()) {
286 LOG(WARNING) << "ONC File: Error (" 299 LOG(WARNING) << "ONC File: Error ("
287 << net::ErrorToString(failures[0].net_error) 300 << net::ErrorToString(failures[0].net_error)
288 << ") importing " << cert_type << " certificate at index " 301 << ") importing " << cert_type << " certificate at index "
289 << cert_index; 302 << cert_index;
290 return false; 303 return NULL;
291 } 304 }
292 if (!success) { 305 if (!success) {
293 LOG(WARNING) << "ONC File: Unknown error importing " << cert_type 306 LOG(WARNING) << "ONC File: Unknown error importing " << cert_type
294 << " certificate at index " << cert_index; 307 << " certificate at index " << cert_index;
295 return false; 308 return NULL;
296 } 309 }
297 return true; 310
311 // Have to set the label again, because PKCS#11 seems to want to set
312 // it to token:nickname. We have to set it the first time so that
313 // it gets properly imported into the low-level token inside of
314 // ImportCACerts or ImportServerCert.
315 if (!net::x509_util::SetLabel(x509_cert, guid))
wtc 2011/12/08 00:07:43 IMPORTANT: this is a sign that we don't fully unde
316 return NULL;
317
318 return x509_cert;
298 } 319 }
299 320
300 bool OncNetworkParser::ParseClientCertificate( 321 scoped_refptr<net::X509Certificate> OncNetworkParser::ParseClientCertificate(
301 int cert_index, 322 int cert_index,
323 const std::string& guid,
302 base::DictionaryValue* certificate) { 324 base::DictionaryValue* certificate) {
303 net::CertDatabase cert_database; 325 net::CertDatabase cert_database;
304 std::string pkcs12_data; 326 std::string pkcs12_data;
305 if (!certificate->GetString("PKCS12", &pkcs12_data) || 327 if (!certificate->GetString("PKCS12", &pkcs12_data) ||
306 pkcs12_data.empty()) { 328 pkcs12_data.empty()) {
307 LOG(WARNING) << "ONC File: PKCS12 data is missing for Client " 329 LOG(WARNING) << "ONC File: PKCS12 data is missing for Client "
308 << "certificate at index " << cert_index; 330 << "certificate at index " << cert_index;
309 return false; 331 return NULL;
310 } 332 }
311 333
312 std::string decoded_pkcs12; 334 std::string decoded_pkcs12;
313 if (!base::Base64Decode(pkcs12_data, &decoded_pkcs12)) { 335 if (!base::Base64Decode(pkcs12_data, &decoded_pkcs12)) {
314 LOG(WARNING) << "Unable to base64 decode PKCS#12 data: \"" 336 LOG(WARNING) << "Unable to base64 decode PKCS#12 data: \""
315 << pkcs12_data << "\"."; 337 << pkcs12_data << "\".";
316 return false; 338 return NULL;
317 } 339 }
318 340
319 // Since this has a private key, always use the private module. 341 // Since this has a private key, always use the private module.
320 scoped_refptr<net::CryptoModule> module(cert_database.GetPrivateModule()); 342 scoped_refptr<net::CryptoModule> module(cert_database.GetPrivateModule());
343 net::CertificateList imported_certs;
321 int result = cert_database.ImportFromPKCS12( 344 int result = cert_database.ImportFromPKCS12(
322 module.get(), decoded_pkcs12, string16(), false); 345 module.get(), decoded_pkcs12, string16(), false, &imported_certs);
323 if (result != net::OK) { 346 if (result != net::OK) {
324 LOG(WARNING) << "ONC File: Unable to import Client certificate at index " 347 LOG(WARNING) << "ONC File: Unable to import Client certificate at index "
325 << cert_index 348 << cert_index
326 << " (error " << net::ErrorToString(result) << ")."; 349 << " (error " << net::ErrorToString(result) << ").";
327 return false; 350 return NULL;
328 } 351 }
329 return true; 352
353 if (imported_certs.size() == 0ul) {
wtc 2011/12/08 00:07:43 Nit: you should be able to use 0 and 1 without the
Greg Spencer (Chromium) 2011/12/09 18:51:38 Yes, I know that, but for some reason I was gettin
wtc 2011/12/10 00:58:43 I suspect you may have used a DCHECK_EQ here befor
354 LOG(WARNING) << "ONC File: PKCS12 data contains no importable certificates"
355 << " at index " << cert_index;
356 return NULL;
357 }
358
359 if (imported_certs.size() != 1ul) {
360 LOG(WARNING) << "ONC File: PKCS12 data at index " << cert_index
361 << " contains more than one certificate. Only the first one will"
362 << " be imported.";
363 }
364
365 scoped_refptr<net::X509Certificate> cert_result = imported_certs[0];
366 if (!net::x509_util::SetLabel(cert_result.get(), guid))
wtc 2011/12/08 00:07:43 IMPORTANT: you can specify the certificate "friend
Greg Spencer (Chromium) 2011/12/09 18:51:38 That may be true, but I don't have control over th
367 return NULL;
368
369 return cert_result;
330 } 370 }
331 371
372 // static
373 bool OncNetworkParser::DeleteCertAndKeyByLabel(const std::string& label) {
374 net::CertificateList cert_list;
375 ListCertsWithLabel(label, &cert_list);
376 net::CertDatabase cert_db;
377 bool result = true;
378 for (net::CertificateList::iterator iter = cert_list.begin();
379 iter != cert_list.end(); ++iter) {
380 // If we fail, we try and delete the rest still.
381 // TODO(gspencer): this isn't very "transactional". If we fail on some, but
382 // not all, then it's possible to leave things in a weird state.
383 // Luckily there should only be one cert with a particular
384 // label, and the cert not being found is one of the few reasons this could
385 // fail, but still...
386 if (!cert_db.DeleteCertAndKey(iter->get()))
387 result = false;
388 }
389 return result;
390 }
391
392 // static
393 void OncNetworkParser::ListCertsWithLabel(const std::string& label,
394 net::CertificateList* result) {
395 net::CertificateList all_certs;
396 net::CertDatabase cert_db;
397 cert_db.ListCerts(&all_certs);
398 net::CertificateList new_list;
399 for (net::CertificateList::iterator iter = all_certs.begin();
400 iter != all_certs.end(); ++iter) {
401 if (net::x509_util::GetLabel(iter->get()).find(label) != std::string::npos)
wtc 2011/12/08 00:07:43 IMPORTANT: document that DeleteCertAndKeyByLabel a
Greg Spencer (Chromium) 2011/12/09 18:51:38 I switched this so that they are using exact match
402 new_list.push_back(*iter);
wtc 2011/12/08 00:07:43 Nit: why don't you just push *iter to |result| dir
Greg Spencer (Chromium) 2011/12/09 18:51:38 Because if there were failures we could abort and
403 }
404 result->swap(new_list);
405 }
406
407
408
332 // -------------------- OncWirelessNetworkParser -------------------- 409 // -------------------- OncWirelessNetworkParser --------------------
333 410
334 OncWirelessNetworkParser::OncWirelessNetworkParser() {} 411 OncWirelessNetworkParser::OncWirelessNetworkParser() {}
335 OncWirelessNetworkParser::~OncWirelessNetworkParser() {} 412 OncWirelessNetworkParser::~OncWirelessNetworkParser() {}
336 413
337 bool OncWirelessNetworkParser::ParseValue(PropertyIndex index, 414 bool OncWirelessNetworkParser::ParseValue(PropertyIndex index,
338 const base::Value& value, 415 const base::Value& value,
339 Network* network) { 416 Network* network) {
340 DCHECK_NE(TYPE_ETHERNET, network->type()); 417 DCHECK_NE(TYPE_ETHERNET, network->type());
341 DCHECK_NE(TYPE_VPN, network->type()); 418 DCHECK_NE(TYPE_VPN, network->type());
(...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after
670 static EnumMapper<ProviderType>::Pair table[] = { 747 static EnumMapper<ProviderType>::Pair table[] = {
671 { flimflam::kProviderL2tpIpsec, PROVIDER_TYPE_L2TP_IPSEC_PSK }, 748 { flimflam::kProviderL2tpIpsec, PROVIDER_TYPE_L2TP_IPSEC_PSK },
672 { flimflam::kProviderOpenVpn, PROVIDER_TYPE_OPEN_VPN }, 749 { flimflam::kProviderOpenVpn, PROVIDER_TYPE_OPEN_VPN },
673 }; 750 };
674 CR_DEFINE_STATIC_LOCAL(EnumMapper<ProviderType>, parser, 751 CR_DEFINE_STATIC_LOCAL(EnumMapper<ProviderType>, parser,
675 (table, arraysize(table), PROVIDER_TYPE_MAX)); 752 (table, arraysize(table), PROVIDER_TYPE_MAX));
676 return parser.Get(type); 753 return parser.Get(type);
677 } 754 }
678 755
679 } // namespace chromeos 756 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698