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

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: fix unit tests 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
« no previous file with comments | « chromeos/network/onc/onc_utils.h ('k') | chromeos/network/onc/onc_utils_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/logging.h"
9 #include "base/values.h" 10 #include "base/values.h"
10 #include "chrome/browser/chromeos/cros/onc_constants.h" 11 #include "chromeos/network/network_event_log.h"
12 #include "chromeos/network/onc/onc_constants.h"
11 #include "crypto/encryptor.h" 13 #include "crypto/encryptor.h"
12 #include "crypto/hmac.h" 14 #include "crypto/hmac.h"
13 #include "crypto/symmetric_key.h" 15 #include "crypto/symmetric_key.h"
14 #include "grit/generated_resources.h" 16
15 #include "ui/base/l10n/l10n_util.h" 17 #define ONC_LOG_WARNING(message) NET_LOG_WARNING("ONC", message)
18 #define ONC_LOG_ERROR(message) NET_LOG_ERROR("ONC", message)
16 19
17 namespace chromeos { 20 namespace chromeos {
18 namespace onc { 21 namespace onc {
19 22
23 namespace {
24
25 const char kUnableToDecrypt[] = "Unable to decrypt encrypted ONC";
26 const char kUnableToDecode[] = "Unable to decode encrypted ONC";
27
28 } // namespace
29
20 scoped_ptr<base::DictionaryValue> ReadDictionaryFromJson( 30 scoped_ptr<base::DictionaryValue> ReadDictionaryFromJson(
21 const std::string& json, 31 const std::string& json) {
22 std::string* error) { 32 std::string error;
23 base::Value* root = base::JSONReader::ReadAndReturnError( 33 base::Value* root = base::JSONReader::ReadAndReturnError(
24 json, base::JSON_ALLOW_TRAILING_COMMAS, NULL, error); 34 json, base::JSON_ALLOW_TRAILING_COMMAS, NULL, &error);
25 35
26 base::DictionaryValue* dict_ptr = NULL; 36 base::DictionaryValue* dict_ptr = NULL;
27 if (root != NULL && !root->GetAsDictionary(&dict_ptr)) { 37 if (!root || !root->GetAsDictionary(&dict_ptr)) {
28 if (error) { 38 ONC_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; 39 delete root;
33 } 40 }
34 41
35 return make_scoped_ptr(dict_ptr); 42 return make_scoped_ptr(dict_ptr);
36 } 43 }
37 44
38 scoped_ptr<base::DictionaryValue> Decrypt(const std::string& passphrase, 45 scoped_ptr<base::DictionaryValue> Decrypt(const std::string& passphrase,
39 const base::DictionaryValue& root, 46 const base::DictionaryValue& root) {
40 std::string* error) {
41 const int kKeySizeInBits = 256; 47 const int kKeySizeInBits = 256;
42 const int kMaxIterationCount = 500000; 48 const int kMaxIterationCount = 500000;
43 std::string onc_type; 49 std::string onc_type;
44 std::string initial_vector; 50 std::string initial_vector;
45 std::string salt; 51 std::string salt;
46 std::string cipher; 52 std::string cipher;
47 std::string stretch_method; 53 std::string stretch_method;
48 std::string hmac_method; 54 std::string hmac_method;
49 std::string hmac; 55 std::string hmac;
50 int iterations; 56 int iterations;
51 std::string ciphertext; 57 std::string ciphertext;
52 58
53 if (!root.GetString(encrypted::kCiphertext, &ciphertext) || 59 if (!root.GetString(encrypted::kCiphertext, &ciphertext) ||
54 !root.GetString(encrypted::kCipher, &cipher) || 60 !root.GetString(encrypted::kCipher, &cipher) ||
55 !root.GetString(encrypted::kHMAC, &hmac) || 61 !root.GetString(encrypted::kHMAC, &hmac) ||
56 !root.GetString(encrypted::kHMACMethod, &hmac_method) || 62 !root.GetString(encrypted::kHMACMethod, &hmac_method) ||
57 !root.GetString(encrypted::kIV, &initial_vector) || 63 !root.GetString(encrypted::kIV, &initial_vector) ||
58 !root.GetInteger(encrypted::kIterations, &iterations) || 64 !root.GetInteger(encrypted::kIterations, &iterations) ||
59 !root.GetString(encrypted::kSalt, &salt) || 65 !root.GetString(encrypted::kSalt, &salt) ||
60 !root.GetString(encrypted::kStretch, &stretch_method) || 66 !root.GetString(encrypted::kStretch, &stretch_method) ||
61 !root.GetString(encrypted::kType, &onc_type) || 67 !root.GetString(encrypted::kType, &onc_type) ||
62 onc_type != kEncryptedConfiguration) { 68 onc_type != kEncryptedConfiguration) {
63 *error = l10n_util::GetStringUTF8( 69
64 IDS_NETWORK_CONFIG_ERROR_ENCRYPTED_ONC_MALFORMED); 70 ONC_LOG_ERROR("Encrypted ONC malformed.");
65 return scoped_ptr<base::DictionaryValue>(); 71 return scoped_ptr<base::DictionaryValue>();
66 } 72 }
67 73
68 if (hmac_method != encrypted::kSHA1 || 74 if (hmac_method != encrypted::kSHA1 ||
69 cipher != encrypted::kAES256 || 75 cipher != encrypted::kAES256 ||
70 stretch_method != encrypted::kPBKDF2) { 76 stretch_method != encrypted::kPBKDF2) {
71 *error = l10n_util::GetStringUTF8( 77 ONC_LOG_ERROR("Encrypted ONC unsupported encryption scheme.");
72 IDS_NETWORK_CONFIG_ERROR_ENCRYPTED_ONC_UNSUPPORTED_ENCRYPTION);
73 return scoped_ptr<base::DictionaryValue>(); 78 return scoped_ptr<base::DictionaryValue>();
74 } 79 }
75 80
76 // Make sure iterations != 0, since that's not valid. 81 // Make sure iterations != 0, since that's not valid.
77 if (iterations == 0) { 82 if (iterations == 0) {
78 *error = l10n_util::GetStringUTF8( 83 ONC_LOG_ERROR(kUnableToDecrypt);
79 IDS_NETWORK_CONFIG_ERROR_ENCRYPTED_ONC_UNABLE_TO_DECRYPT);
80 return scoped_ptr<base::DictionaryValue>(); 84 return scoped_ptr<base::DictionaryValue>();
81 } 85 }
82 86
83 // Simply a sanity check to make sure we can't lock up the machine 87 // 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). 88 // for too long with a huge number (or a negative number).
85 if (iterations < 0 || iterations > kMaxIterationCount) { 89 if (iterations < 0 || iterations > kMaxIterationCount) {
86 *error = l10n_util::GetStringUTF8( 90 ONC_LOG_ERROR("Too many iterations in encrypted ONC");
87 IDS_NETWORK_CONFIG_ERROR_ENCRYPTED_ONC_TOO_MANY_ITERATIONS);
88 return scoped_ptr<base::DictionaryValue>(); 91 return scoped_ptr<base::DictionaryValue>();
89 } 92 }
90 93
91 if (!base::Base64Decode(salt, &salt)) { 94 if (!base::Base64Decode(salt, &salt)) {
92 *error = l10n_util::GetStringUTF8( 95 ONC_LOG_ERROR(kUnableToDecode);
93 IDS_NETWORK_CONFIG_ERROR_ENCRYPTED_ONC_UNABLE_TO_DECODE);
94 return scoped_ptr<base::DictionaryValue>(); 96 return scoped_ptr<base::DictionaryValue>();
95 } 97 }
96 98
97 scoped_ptr<crypto::SymmetricKey> key( 99 scoped_ptr<crypto::SymmetricKey> key(
98 crypto::SymmetricKey::DeriveKeyFromPassword(crypto::SymmetricKey::AES, 100 crypto::SymmetricKey::DeriveKeyFromPassword(crypto::SymmetricKey::AES,
99 passphrase, 101 passphrase,
100 salt, 102 salt,
101 iterations, 103 iterations,
102 kKeySizeInBits)); 104 kKeySizeInBits));
103 105
104 if (!base::Base64Decode(initial_vector, &initial_vector)) { 106 if (!base::Base64Decode(initial_vector, &initial_vector)) {
105 *error = l10n_util::GetStringUTF8( 107 ONC_LOG_ERROR(kUnableToDecode);
106 IDS_NETWORK_CONFIG_ERROR_ENCRYPTED_ONC_UNABLE_TO_DECODE);
107 return scoped_ptr<base::DictionaryValue>(); 108 return scoped_ptr<base::DictionaryValue>();
108 } 109 }
109 if (!base::Base64Decode(ciphertext, &ciphertext)) { 110 if (!base::Base64Decode(ciphertext, &ciphertext)) {
110 *error = l10n_util::GetStringUTF8( 111 ONC_LOG_ERROR(kUnableToDecode);
111 IDS_NETWORK_CONFIG_ERROR_ENCRYPTED_ONC_UNABLE_TO_DECODE);
112 return scoped_ptr<base::DictionaryValue>(); 112 return scoped_ptr<base::DictionaryValue>();
113 } 113 }
114 if (!base::Base64Decode(hmac, &hmac)) { 114 if (!base::Base64Decode(hmac, &hmac)) {
115 *error = l10n_util::GetStringUTF8( 115 ONC_LOG_ERROR(kUnableToDecode);
116 IDS_NETWORK_CONFIG_ERROR_ENCRYPTED_ONC_UNABLE_TO_DECODE);
117 return scoped_ptr<base::DictionaryValue>(); 116 return scoped_ptr<base::DictionaryValue>();
118 } 117 }
119 118
120 crypto::HMAC hmac_verifier(crypto::HMAC::SHA1); 119 crypto::HMAC hmac_verifier(crypto::HMAC::SHA1);
121 if (!hmac_verifier.Init(key.get()) || 120 if (!hmac_verifier.Init(key.get()) ||
122 !hmac_verifier.Verify(ciphertext, hmac)) { 121 !hmac_verifier.Verify(ciphertext, hmac)) {
123 *error = l10n_util::GetStringUTF8( 122 ONC_LOG_ERROR(kUnableToDecrypt);
124 IDS_NETWORK_CONFIG_ERROR_ENCRYPTED_ONC_UNABLE_TO_DECRYPT);
125 return scoped_ptr<base::DictionaryValue>(); 123 return scoped_ptr<base::DictionaryValue>();
126 } 124 }
127 125
128 crypto::Encryptor decryptor; 126 crypto::Encryptor decryptor;
129 if (!decryptor.Init(key.get(), crypto::Encryptor::CBC, initial_vector)) { 127 if (!decryptor.Init(key.get(), crypto::Encryptor::CBC, initial_vector)) {
130 *error = l10n_util::GetStringUTF8( 128 ONC_LOG_ERROR(kUnableToDecrypt);
131 IDS_NETWORK_CONFIG_ERROR_ENCRYPTED_ONC_UNABLE_TO_DECRYPT);
132 return scoped_ptr<base::DictionaryValue>(); 129 return scoped_ptr<base::DictionaryValue>();
133 } 130 }
134 131
135 std::string plaintext; 132 std::string plaintext;
136 if (!decryptor.Decrypt(ciphertext, &plaintext)) { 133 if (!decryptor.Decrypt(ciphertext, &plaintext)) {
137 *error = l10n_util::GetStringUTF8( 134 ONC_LOG_ERROR(kUnableToDecrypt);
138 IDS_NETWORK_CONFIG_ERROR_ENCRYPTED_ONC_UNABLE_TO_DECRYPT);
139 return scoped_ptr<base::DictionaryValue>(); 135 return scoped_ptr<base::DictionaryValue>();
140 } 136 }
141 137
142 scoped_ptr<base::DictionaryValue> new_root = 138 scoped_ptr<base::DictionaryValue> new_root =
143 ReadDictionaryFromJson(plaintext, error); 139 ReadDictionaryFromJson(plaintext);
144 if (new_root.get() == NULL && error->empty()) { 140 if (new_root.get() == NULL) {
145 *error = l10n_util::GetStringUTF8( 141 ONC_LOG_ERROR("Property dictionary malformed.");
146 IDS_NETWORK_CONFIG_ERROR_NETWORK_PROP_DICT_MALFORMED); 142 return scoped_ptr<base::DictionaryValue>();
147 } 143 }
144
148 return new_root.Pass(); 145 return new_root.Pass();
149 } 146 }
150 147
148 std::string GetSourceAsString(ONCSource source) {
149 switch (source) {
150 case ONC_SOURCE_DEVICE_POLICY:
151 return "device policy";
152 case ONC_SOURCE_USER_POLICY:
153 return "user policy";
154 case ONC_SOURCE_NONE:
155 return "none";
156 case ONC_SOURCE_USER_IMPORT:
157 return "user import";
158 }
159 NOTREACHED() << "unknown ONC source " << source;
160 return "unknown";
161 }
162
151 } // chromeos 163 } // chromeos
152 } // onc 164 } // onc
OLDNEW
« no previous file with comments | « chromeos/network/onc/onc_utils.h ('k') | chromeos/network/onc/onc_utils_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698