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

Side by Side Diff: chromeos/network/onc/onc_utils.cc

Issue 11299236: This moves the ONC parsing code into chromeos/network/onc (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Review changes Created 8 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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/network_settings/onc_utils.h" 5 #include "chromeos/network/onc/onc_utils.h"
6 6
7 #include "base/base64.h" 7 #include "base/base64.h"
8 #include "base/json/json_reader.h" 8 #include "base/json/json_reader.h"
9 #include "base/values.h" 9 #include "base/values.h"
10 #include "chrome/browser/chromeos/cros/onc_constants.h" 10 #include "chromeos/network/network_event_log.h"
11 #include "chromeos/network/onc/onc_constants.h"
11 #include "crypto/encryptor.h" 12 #include "crypto/encryptor.h"
12 #include "crypto/hmac.h" 13 #include "crypto/hmac.h"
13 #include "crypto/symmetric_key.h" 14 #include "crypto/symmetric_key.h"
14 #include "grit/generated_resources.h"
15 #include "ui/base/l10n/l10n_util.h"
16 15
17 namespace chromeos { 16 namespace chromeos {
18 namespace onc { 17 namespace onc {
19 18
19 namespace {
20
21 const char kUnableToDecrypt[] = "Unable to decrypt encrypted ONC";
22 const char kUnableToDecode[] = "Unable to decode encrypted ONC";
23
24 } // namespace
25
20 scoped_ptr<base::DictionaryValue> ReadDictionaryFromJson( 26 scoped_ptr<base::DictionaryValue> ReadDictionaryFromJson(
21 const std::string& json, 27 const std::string& json) {
22 std::string* error) { 28 std::string error;
23 base::Value* root = base::JSONReader::ReadAndReturnError( 29 base::Value* root = base::JSONReader::ReadAndReturnError(
24 json, base::JSON_ALLOW_TRAILING_COMMAS, NULL, error); 30 json, base::JSON_ALLOW_TRAILING_COMMAS, NULL, &error);
25 31
26 base::DictionaryValue* dict_ptr = NULL; 32 base::DictionaryValue* dict_ptr = NULL;
27 if (root != NULL && !root->GetAsDictionary(&dict_ptr)) { 33 if (!root || !root->GetAsDictionary(&dict_ptr)) {
28 if (error) { 34 NET_LOG_ERROR("Invalid JSON Dictionary: " + error);
29 *error = l10n_util::GetStringUTF8(
30 IDS_NETWORK_CONFIG_ERROR_NETWORK_NOT_A_JSON_DICTIONARY);
31 }
32 delete root; 35 delete root;
33 } 36 }
34 37
35 return make_scoped_ptr(dict_ptr); 38 return make_scoped_ptr(dict_ptr);
36 } 39 }
37 40
38 scoped_ptr<base::DictionaryValue> Decrypt(const std::string& passphrase, 41 scoped_ptr<base::DictionaryValue> Decrypt(const std::string& passphrase,
39 const base::DictionaryValue& root, 42 const base::DictionaryValue& root) {
40 std::string* error) {
41 const int kKeySizeInBits = 256; 43 const int kKeySizeInBits = 256;
42 const int kMaxIterationCount = 500000; 44 const int kMaxIterationCount = 500000;
43 std::string onc_type; 45 std::string onc_type;
44 std::string initial_vector; 46 std::string initial_vector;
45 std::string salt; 47 std::string salt;
46 std::string cipher; 48 std::string cipher;
47 std::string stretch_method; 49 std::string stretch_method;
48 std::string hmac_method; 50 std::string hmac_method;
49 std::string hmac; 51 std::string hmac;
50 int iterations; 52 int iterations;
51 std::string ciphertext; 53 std::string ciphertext;
52 54
53 if (!root.GetString(encrypted::kCiphertext, &ciphertext) || 55 if (!root.GetString(encrypted::kCiphertext, &ciphertext) ||
54 !root.GetString(encrypted::kCipher, &cipher) || 56 !root.GetString(encrypted::kCipher, &cipher) ||
55 !root.GetString(encrypted::kHMAC, &hmac) || 57 !root.GetString(encrypted::kHMAC, &hmac) ||
56 !root.GetString(encrypted::kHMACMethod, &hmac_method) || 58 !root.GetString(encrypted::kHMACMethod, &hmac_method) ||
57 !root.GetString(encrypted::kIV, &initial_vector) || 59 !root.GetString(encrypted::kIV, &initial_vector) ||
58 !root.GetInteger(encrypted::kIterations, &iterations) || 60 !root.GetInteger(encrypted::kIterations, &iterations) ||
59 !root.GetString(encrypted::kSalt, &salt) || 61 !root.GetString(encrypted::kSalt, &salt) ||
60 !root.GetString(encrypted::kStretch, &stretch_method) || 62 !root.GetString(encrypted::kStretch, &stretch_method) ||
61 !root.GetString(encrypted::kType, &onc_type) || 63 !root.GetString(encrypted::kType, &onc_type) ||
62 onc_type != kEncryptedConfiguration) { 64 onc_type != kEncryptedConfiguration) {
63 *error = l10n_util::GetStringUTF8( 65
64 IDS_NETWORK_CONFIG_ERROR_ENCRYPTED_ONC_MALFORMED); 66 NET_LOG_ERROR("Encrypted ONC malformed.");
65 return scoped_ptr<base::DictionaryValue>(); 67 return scoped_ptr<base::DictionaryValue>();
66 } 68 }
67 69
68 if (hmac_method != encrypted::kSHA1 || 70 if (hmac_method != encrypted::kSHA1 ||
69 cipher != encrypted::kAES256 || 71 cipher != encrypted::kAES256 ||
70 stretch_method != encrypted::kPBKDF2) { 72 stretch_method != encrypted::kPBKDF2) {
71 *error = l10n_util::GetStringUTF8( 73 NET_LOG_ERROR("Encrypted ONC unsupported encryption scheme.");
72 IDS_NETWORK_CONFIG_ERROR_ENCRYPTED_ONC_UNSUPPORTED_ENCRYPTION);
73 return scoped_ptr<base::DictionaryValue>(); 74 return scoped_ptr<base::DictionaryValue>();
74 } 75 }
75 76
76 // Make sure iterations != 0, since that's not valid. 77 // Make sure iterations != 0, since that's not valid.
77 if (iterations == 0) { 78 if (iterations == 0) {
78 *error = l10n_util::GetStringUTF8( 79 NET_LOG_ERROR(kUnableToDecrypt);
79 IDS_NETWORK_CONFIG_ERROR_ENCRYPTED_ONC_UNABLE_TO_DECRYPT);
80 return scoped_ptr<base::DictionaryValue>(); 80 return scoped_ptr<base::DictionaryValue>();
81 } 81 }
82 82
83 // Simply a sanity check to make sure we can't lock up the machine 83 // Simply a sanity check to make sure we can't lock up the machine
84 // for too long with a huge number (or a negative number). 84 // for too long with a huge number (or a negative number).
85 if (iterations < 0 || iterations > kMaxIterationCount) { 85 if (iterations < 0 || iterations > kMaxIterationCount) {
86 *error = l10n_util::GetStringUTF8( 86 NET_LOG_ERROR("Too many iterations in encrypted ONC");
87 IDS_NETWORK_CONFIG_ERROR_ENCRYPTED_ONC_TOO_MANY_ITERATIONS);
88 return scoped_ptr<base::DictionaryValue>(); 87 return scoped_ptr<base::DictionaryValue>();
89 } 88 }
90 89
91 if (!base::Base64Decode(salt, &salt)) { 90 if (!base::Base64Decode(salt, &salt)) {
92 *error = l10n_util::GetStringUTF8( 91 NET_LOG_ERROR(kUnableToDecode);
93 IDS_NETWORK_CONFIG_ERROR_ENCRYPTED_ONC_UNABLE_TO_DECODE);
94 return scoped_ptr<base::DictionaryValue>(); 92 return scoped_ptr<base::DictionaryValue>();
95 } 93 }
96 94
97 scoped_ptr<crypto::SymmetricKey> key( 95 scoped_ptr<crypto::SymmetricKey> key(
98 crypto::SymmetricKey::DeriveKeyFromPassword(crypto::SymmetricKey::AES, 96 crypto::SymmetricKey::DeriveKeyFromPassword(crypto::SymmetricKey::AES,
99 passphrase, 97 passphrase,
100 salt, 98 salt,
101 iterations, 99 iterations,
102 kKeySizeInBits)); 100 kKeySizeInBits));
103 101
104 if (!base::Base64Decode(initial_vector, &initial_vector)) { 102 if (!base::Base64Decode(initial_vector, &initial_vector)) {
105 *error = l10n_util::GetStringUTF8( 103 NET_LOG_ERROR(kUnableToDecode);
106 IDS_NETWORK_CONFIG_ERROR_ENCRYPTED_ONC_UNABLE_TO_DECODE);
107 return scoped_ptr<base::DictionaryValue>(); 104 return scoped_ptr<base::DictionaryValue>();
108 } 105 }
109 if (!base::Base64Decode(ciphertext, &ciphertext)) { 106 if (!base::Base64Decode(ciphertext, &ciphertext)) {
110 *error = l10n_util::GetStringUTF8( 107 NET_LOG_ERROR(kUnableToDecode);
111 IDS_NETWORK_CONFIG_ERROR_ENCRYPTED_ONC_UNABLE_TO_DECODE);
112 return scoped_ptr<base::DictionaryValue>(); 108 return scoped_ptr<base::DictionaryValue>();
113 } 109 }
114 if (!base::Base64Decode(hmac, &hmac)) { 110 if (!base::Base64Decode(hmac, &hmac)) {
115 *error = l10n_util::GetStringUTF8( 111 NET_LOG_ERROR(kUnableToDecode);
116 IDS_NETWORK_CONFIG_ERROR_ENCRYPTED_ONC_UNABLE_TO_DECODE);
117 return scoped_ptr<base::DictionaryValue>(); 112 return scoped_ptr<base::DictionaryValue>();
118 } 113 }
119 114
120 crypto::HMAC hmac_verifier(crypto::HMAC::SHA1); 115 crypto::HMAC hmac_verifier(crypto::HMAC::SHA1);
121 if (!hmac_verifier.Init(key.get()) || 116 if (!hmac_verifier.Init(key.get()) ||
122 !hmac_verifier.Verify(ciphertext, hmac)) { 117 !hmac_verifier.Verify(ciphertext, hmac)) {
123 *error = l10n_util::GetStringUTF8( 118 NET_LOG_ERROR(kUnableToDecrypt);
124 IDS_NETWORK_CONFIG_ERROR_ENCRYPTED_ONC_UNABLE_TO_DECRYPT);
125 return scoped_ptr<base::DictionaryValue>(); 119 return scoped_ptr<base::DictionaryValue>();
126 } 120 }
127 121
128 crypto::Encryptor decryptor; 122 crypto::Encryptor decryptor;
129 if (!decryptor.Init(key.get(), crypto::Encryptor::CBC, initial_vector)) { 123 if (!decryptor.Init(key.get(), crypto::Encryptor::CBC, initial_vector)) {
130 *error = l10n_util::GetStringUTF8( 124 NET_LOG_ERROR(kUnableToDecrypt);
131 IDS_NETWORK_CONFIG_ERROR_ENCRYPTED_ONC_UNABLE_TO_DECRYPT);
132 return scoped_ptr<base::DictionaryValue>(); 125 return scoped_ptr<base::DictionaryValue>();
133 } 126 }
134 127
135 std::string plaintext; 128 std::string plaintext;
136 if (!decryptor.Decrypt(ciphertext, &plaintext)) { 129 if (!decryptor.Decrypt(ciphertext, &plaintext)) {
137 *error = l10n_util::GetStringUTF8( 130 NET_LOG_ERROR(kUnableToDecrypt);
138 IDS_NETWORK_CONFIG_ERROR_ENCRYPTED_ONC_UNABLE_TO_DECRYPT);
139 return scoped_ptr<base::DictionaryValue>(); 131 return scoped_ptr<base::DictionaryValue>();
140 } 132 }
141 133
142 scoped_ptr<base::DictionaryValue> new_root = 134 scoped_ptr<base::DictionaryValue> new_root =
143 ReadDictionaryFromJson(plaintext, error); 135 ReadDictionaryFromJson(plaintext);
144 if (new_root.get() == NULL && error->empty()) { 136 if (new_root.get() == NULL) {
145 *error = l10n_util::GetStringUTF8( 137 NET_LOG_ERROR("Property dictionary malformed.");
146 IDS_NETWORK_CONFIG_ERROR_NETWORK_PROP_DICT_MALFORMED); 138 return scoped_ptr<base::DictionaryValue>();
147 } 139 }
140
148 return new_root.Pass(); 141 return new_root.Pass();
149 } 142 }
150 143
151 } // chromeos 144 } // chromeos
152 } // onc 145 } // onc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698