OLD | NEW |
---|---|
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 <keyhi.h> | 7 #include <keyhi.h> |
8 #include <pk11pub.h> | 8 #include <pk11pub.h> |
9 | 9 |
10 #include "base/base64.h" | 10 #include "base/base64.h" |
11 #include "base/json/json_value_serializer.h" | 11 #include "base/json/json_value_serializer.h" |
12 #include "base/json/json_writer.h" // for debug output only. | 12 #include "base/json/json_writer.h" // for debug output only. |
13 #include "base/stringprintf.h" | 13 #include "base/stringprintf.h" |
14 #include "base/values.h" | 14 #include "base/values.h" |
15 #include "chrome/browser/chromeos/cros/native_network_constants.h" | 15 #include "chrome/browser/chromeos/cros/native_network_constants.h" |
16 #include "chrome/browser/chromeos/cros/native_network_parser.h" | 16 #include "chrome/browser/chromeos/cros/native_network_parser.h" |
17 #include "chrome/browser/chromeos/cros/network_library.h" | 17 #include "chrome/browser/chromeos/cros/network_library.h" |
18 #include "chrome/browser/chromeos/cros/onc_constants.h" | 18 #include "chrome/browser/chromeos/cros/onc_constants.h" |
19 #include "chrome/common/net/x509_certificate_model.h" | 19 #include "chrome/common/net/x509_certificate_model.h" |
20 #include "crypto/encryptor.h" | |
21 #include "crypto/hmac.h" | |
22 #include "crypto/scoped_nss_types.h" | |
23 #include "crypto/symmetric_key.h" | |
20 #include "grit/generated_resources.h" | 24 #include "grit/generated_resources.h" |
21 #include "net/base/cert_database.h" | 25 #include "net/base/cert_database.h" |
22 #include "net/base/crypto_module.h" | 26 #include "net/base/crypto_module.h" |
23 #include "net/base/net_errors.h" | 27 #include "net/base/net_errors.h" |
24 #include "net/base/x509_certificate.h" | 28 #include "net/base/x509_certificate.h" |
25 #include "third_party/cros_system_api/dbus/service_constants.h" | 29 #include "third_party/cros_system_api/dbus/service_constants.h" |
26 #include "ui/base/l10n/l10n_util.h" | 30 #include "ui/base/l10n/l10n_util.h" |
27 | 31 |
28 namespace chromeos { | 32 namespace chromeos { |
29 | 33 |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
192 std::string value_json; | 196 std::string value_json; |
193 base::JSONWriter::Write(&value, false, &value_json); | 197 base::JSONWriter::Write(&value, false, &value_json); |
194 return value_json; | 198 return value_json; |
195 } | 199 } |
196 | 200 |
197 } // namespace | 201 } // namespace |
198 | 202 |
199 // -------------------- OncNetworkParser -------------------- | 203 // -------------------- OncNetworkParser -------------------- |
200 | 204 |
201 OncNetworkParser::OncNetworkParser(const std::string& onc_blob, | 205 OncNetworkParser::OncNetworkParser(const std::string& onc_blob, |
206 const std::string& passphrase, | |
202 NetworkUIData::ONCSource onc_source) | 207 NetworkUIData::ONCSource onc_source) |
203 : NetworkParser(get_onc_mapper()), | 208 : NetworkParser(get_onc_mapper()), |
204 onc_source_(onc_source), | 209 onc_source_(onc_source), |
205 network_configs_(NULL), | 210 network_configs_(NULL), |
206 certificates_(NULL) { | 211 certificates_(NULL) { |
207 VLOG(2) << __func__ << ": OncNetworkParser called on " << onc_blob; | 212 VLOG(2) << __func__ << ": OncNetworkParser called on " << onc_blob; |
208 JSONStringValueSerializer deserializer(onc_blob); | 213 JSONStringValueSerializer deserializer(onc_blob); |
209 deserializer.set_allow_trailing_comma(true); | 214 deserializer.set_allow_trailing_comma(true); |
210 scoped_ptr<base::Value> root(deserializer.Deserialize(NULL, &parse_error_)); | 215 scoped_ptr<base::Value> root(deserializer.Deserialize(NULL, &parse_error_)); |
211 | 216 |
212 if (!root.get() || root->GetType() != base::Value::TYPE_DICTIONARY) { | 217 if (!root.get() || root->GetType() != base::Value::TYPE_DICTIONARY) { |
213 LOG(WARNING) << "OncNetworkParser received bad ONC file: " << parse_error_; | 218 LOG(WARNING) << "OncNetworkParser received bad ONC file: " << parse_error_; |
214 } else { | 219 } else { |
215 root_dict_.reset(static_cast<DictionaryValue*>(root.release())); | 220 root_dict_.reset(static_cast<DictionaryValue*>(root.release())); |
221 | |
222 // Check and see if this is an encrypted ONC file. If so, decrypt it. | |
223 std::string ciphertext_test; | |
224 if (root_dict_->GetString("Ciphertext", &ciphertext_test)) | |
225 root_dict_.reset(Decrypt(passphrase, root_dict_.get())); | |
226 | |
227 // Decryption failed, errors will be in parse_error_; | |
228 if (!root_dict_.get()) | |
229 return; | |
230 | |
216 // At least one of NetworkConfigurations or Certificates is required. | 231 // At least one of NetworkConfigurations or Certificates is required. |
217 bool has_network_configurations = | 232 bool has_network_configurations = |
218 root_dict_->GetList("NetworkConfigurations", &network_configs_); | 233 root_dict_->GetList("NetworkConfigurations", &network_configs_); |
219 bool has_certificates = | 234 bool has_certificates = |
220 root_dict_->GetList("Certificates", &certificates_); | 235 root_dict_->GetList("Certificates", &certificates_); |
221 VLOG(2) << "ONC file has " << GetNetworkConfigsSize() << " networks and " | 236 VLOG(2) << "ONC file has " << GetNetworkConfigsSize() << " networks and " |
222 << GetCertificatesSize() << " certificates"; | 237 << GetCertificatesSize() << " certificates"; |
223 LOG_IF(WARNING, (!has_network_configurations && !has_certificates)) | 238 LOG_IF(WARNING, (!has_network_configurations && !has_certificates)) |
224 << "ONC file has no NetworkConfigurations or Certificates."; | 239 << "ONC file has no NetworkConfigurations or Certificates."; |
225 } | 240 } |
226 } | 241 } |
227 | 242 |
228 OncNetworkParser::OncNetworkParser() | 243 OncNetworkParser::OncNetworkParser() |
229 : NetworkParser(get_onc_mapper()), | 244 : NetworkParser(get_onc_mapper()), |
230 network_configs_(NULL), | 245 network_configs_(NULL), |
231 certificates_(NULL) { | 246 certificates_(NULL) { |
232 } | 247 } |
233 | 248 |
234 OncNetworkParser::~OncNetworkParser() { | 249 OncNetworkParser::~OncNetworkParser() { |
235 } | 250 } |
236 | 251 |
237 // static | 252 // static |
238 const EnumMapper<PropertyIndex>* OncNetworkParser::property_mapper() { | 253 const EnumMapper<PropertyIndex>* OncNetworkParser::property_mapper() { |
239 return get_onc_mapper(); | 254 return get_onc_mapper(); |
240 } | 255 } |
241 | 256 |
257 base::DictionaryValue* OncNetworkParser::Decrypt( | |
258 const std::string& passphrase, | |
259 base::DictionaryValue* root) { | |
260 const int key_size_in_bits = 256; | |
agl
2012/01/06 15:59:56
(ignore if you like.) I'd expect a constant to be
Greg Spencer (Chromium)
2012/01/06 21:45:55
Done.
| |
261 std::string onc_type; | |
262 std::string initial_vector_str; | |
263 std::string salt; | |
264 std::string cipher; | |
265 std::string stretch_method; | |
266 std::string hmac_method; | |
267 std::string hmac; | |
268 int iterations; | |
269 std::string ciphertext; | |
270 | |
271 if (!root->GetString("Type", &onc_type) || | |
272 onc_type != "EncryptedConfiguration" || | |
273 !root->GetString("IV", &initial_vector_str) || | |
274 !root->GetString("Salt", &salt) || | |
275 !root->GetString("Cipher", &cipher) || | |
276 !root->GetString("Stretch", &stretch_method) || | |
277 !root->GetString("HMACMethod", &hmac_method) || | |
278 !root->GetString("HMAC", &hmac) || | |
279 !root->GetInteger("Iterations", &iterations) || | |
280 !root->GetString("Ciphertext", &ciphertext)) { | |
281 parse_error_ = l10n_util::GetStringUTF8( | |
282 IDS_NETWORK_CONFIG_ERROR_ENCRYPTED_ONC_MALFORMED); | |
283 return NULL; | |
284 } | |
285 | |
286 if (hmac_method != "SHA1" || | |
287 cipher != "AES256" || | |
288 stretch_method != "PBKDF2") { | |
289 parse_error_ = l10n_util::GetStringUTF8( | |
290 IDS_NETWORK_CONFIG_ERROR_ENCRYPTED_ONC_UNSUPPORTED_ENCRYPTION); | |
291 return NULL; | |
292 } | |
293 | |
294 if (!base::Base64Decode(salt, &salt)) { | |
295 parse_error_ = l10n_util::GetStringUTF8( | |
296 IDS_NETWORK_CONFIG_ERROR_ENCRYPTED_ONC_UNABLE_TO_DECODE); | |
297 return NULL; | |
298 } | |
299 | |
300 scoped_ptr<crypto::SymmetricKey> key( | |
301 crypto::SymmetricKey::DeriveKeyFromPassword(crypto::SymmetricKey::AES, | |
302 passphrase, | |
303 salt, | |
304 iterations, | |
agl
2012/01/06 15:59:56
I know that Ryan has already commented on this, bu
Greg Spencer (Chromium)
2012/01/06 21:45:55
I added a check for values > 500,000.
That should
| |
305 key_size_in_bits)); | |
306 | |
agl
2012/01/06 15:59:56
(ignore if you like.) Extra blank line?
Greg Spencer (Chromium)
2012/01/06 21:45:55
Removed.
| |
307 | |
308 crypto::Encryptor decryptor; | |
309 std::string initial_vector; | |
310 std::string plaintext; | |
311 if (!base::Base64Decode(initial_vector_str, &initial_vector)) { | |
312 parse_error_ = l10n_util::GetStringUTF8( | |
313 IDS_NETWORK_CONFIG_ERROR_ENCRYPTED_ONC_UNABLE_TO_DECODE); | |
314 return NULL; | |
315 } | |
316 if (!base::Base64Decode(ciphertext, &ciphertext)) { | |
317 parse_error_ = l10n_util::GetStringUTF8( | |
318 IDS_NETWORK_CONFIG_ERROR_ENCRYPTED_ONC_UNABLE_TO_DECODE); | |
319 return NULL; | |
320 } | |
321 if (!base::Base64Decode(hmac, &hmac)) { | |
322 parse_error_ = l10n_util::GetStringUTF8( | |
323 IDS_NETWORK_CONFIG_ERROR_ENCRYPTED_ONC_UNABLE_TO_DECODE); | |
324 return NULL; | |
325 } | |
326 | |
327 if (!decryptor.Init(key.get(), crypto::Encryptor::CBC, initial_vector)) { | |
agl
2012/01/06 15:59:56
(ignore if you like.) Move decryptor.Init below th
Greg Spencer (Chromium)
2012/01/06 21:45:55
Done.
| |
328 parse_error_ = l10n_util::GetStringUTF8( | |
329 IDS_NETWORK_CONFIG_ERROR_ENCRYPTED_ONC_UNABLE_TO_DECRYPT); | |
330 return NULL; | |
331 } | |
332 // Verify the HMAC. If it fails, then we will still try to decrypt because | |
agl
2012/01/06 15:59:56
I don't think that you need to worry about the tim
Greg Spencer (Chromium)
2012/01/06 21:45:55
OK, I'll fail earlier on a bad HMAC then.
| |
333 // we don't want to leak timing information. | |
334 bool hmac_failed = false; | |
335 crypto::HMAC hmac_verifier(crypto::HMAC::SHA1); | |
336 if (!hmac_verifier.Init(key.get()) || !hmac_verifier.Verify(ciphertext, hmac)) | |
337 hmac_failed = true; | |
338 | |
339 if (!decryptor.Decrypt(ciphertext, &plaintext) || hmac_failed) { | |
340 parse_error_ = l10n_util::GetStringUTF8( | |
341 IDS_NETWORK_CONFIG_ERROR_ENCRYPTED_ONC_UNABLE_TO_DECRYPT); | |
342 return NULL; | |
343 } | |
344 | |
345 // Now we've decrypted it, let's deserialize the decrypted data. | |
346 JSONStringValueSerializer deserializer(plaintext); | |
347 deserializer.set_allow_trailing_comma(true); | |
348 scoped_ptr<base::Value> new_root(deserializer.Deserialize(NULL, | |
349 &parse_error_)); | |
350 if (!new_root.get() || !new_root->IsType(base::Value::TYPE_DICTIONARY)) { | |
351 if (parse_error_.empty()) | |
352 parse_error_ = l10n_util::GetStringUTF8( | |
353 IDS_NETWORK_CONFIG_ERROR_NETWORK_PROP_DICT_MALFORMED); | |
354 return NULL; | |
355 } | |
356 return static_cast<base::DictionaryValue*>(new_root.release()); | |
357 } | |
358 | |
242 int OncNetworkParser::GetNetworkConfigsSize() const { | 359 int OncNetworkParser::GetNetworkConfigsSize() const { |
243 return network_configs_ ? network_configs_->GetSize() : 0; | 360 return network_configs_ ? network_configs_->GetSize() : 0; |
244 } | 361 } |
245 | 362 |
246 const base::DictionaryValue* OncNetworkParser::GetNetworkConfig(int n) { | 363 const base::DictionaryValue* OncNetworkParser::GetNetworkConfig(int n) { |
247 CHECK(network_configs_); | 364 CHECK(network_configs_); |
248 CHECK(static_cast<size_t>(n) < network_configs_->GetSize()); | 365 CHECK(static_cast<size_t>(n) < network_configs_->GetSize()); |
249 CHECK_GE(n, 0); | 366 CHECK_GE(n, 0); |
250 base::DictionaryValue* info = NULL; | 367 base::DictionaryValue* info = NULL; |
251 if (!network_configs_->GetDictionary(n, &info)) { | 368 if (!network_configs_->GetDictionary(n, &info)) { |
(...skipping 1017 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1269 // on the value of AuthenticationType. | 1386 // on the value of AuthenticationType. |
1270 { "L2TP-IPsec", PROVIDER_TYPE_L2TP_IPSEC_PSK }, | 1387 { "L2TP-IPsec", PROVIDER_TYPE_L2TP_IPSEC_PSK }, |
1271 { "OpenVPN", PROVIDER_TYPE_OPEN_VPN }, | 1388 { "OpenVPN", PROVIDER_TYPE_OPEN_VPN }, |
1272 }; | 1389 }; |
1273 CR_DEFINE_STATIC_LOCAL(EnumMapper<ProviderType>, parser, | 1390 CR_DEFINE_STATIC_LOCAL(EnumMapper<ProviderType>, parser, |
1274 (table, arraysize(table), PROVIDER_TYPE_MAX)); | 1391 (table, arraysize(table), PROVIDER_TYPE_MAX)); |
1275 return parser.Get(type); | 1392 return parser.Get(type); |
1276 } | 1393 } |
1277 | 1394 |
1278 } // namespace chromeos | 1395 } // namespace chromeos |
OLD | NEW |