OLD | NEW |
| (Empty) |
1 // Copyright 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 "chrome/browser/prefs/tracked/pref_hash_store_impl.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "base/metrics/histogram.h" | |
9 #include "base/values.h" | |
10 #include "chrome/browser/prefs/tracked/hash_store_contents.h" | |
11 #include "chrome/browser/prefs/tracked/pref_hash_store_transaction.h" | |
12 | |
13 class PrefHashStoreImpl::PrefHashStoreTransactionImpl | |
14 : public PrefHashStoreTransaction { | |
15 public: | |
16 // Constructs a PrefHashStoreTransactionImpl which can use the private | |
17 // members of its |outer| PrefHashStoreImpl. | |
18 PrefHashStoreTransactionImpl(PrefHashStoreImpl* outer, | |
19 scoped_ptr<HashStoreContents> storage); | |
20 ~PrefHashStoreTransactionImpl() override; | |
21 | |
22 // PrefHashStoreTransaction implementation. | |
23 ValueState CheckValue(const std::string& path, | |
24 const base::Value* value) const override; | |
25 void StoreHash(const std::string& path, const base::Value* value) override; | |
26 ValueState CheckSplitValue( | |
27 const std::string& path, | |
28 const base::DictionaryValue* initial_split_value, | |
29 std::vector<std::string>* invalid_keys) const override; | |
30 void StoreSplitHash(const std::string& path, | |
31 const base::DictionaryValue* split_value) override; | |
32 bool HasHash(const std::string& path) const override; | |
33 void ImportHash(const std::string& path, const base::Value* hash) override; | |
34 void ClearHash(const std::string& path) override; | |
35 bool IsSuperMACValid() const override; | |
36 bool StampSuperMac() override; | |
37 | |
38 private: | |
39 bool GetSplitMacs(const std::string& path, | |
40 std::map<std::string, std::string>* split_macs) const; | |
41 | |
42 HashStoreContents* contents() { | |
43 return outer_->legacy_hash_store_contents_ | |
44 ? outer_->legacy_hash_store_contents_.get() | |
45 : contents_.get(); | |
46 } | |
47 | |
48 const HashStoreContents* contents() const { | |
49 return outer_->legacy_hash_store_contents_ | |
50 ? outer_->legacy_hash_store_contents_.get() | |
51 : contents_.get(); | |
52 } | |
53 | |
54 PrefHashStoreImpl* outer_; | |
55 scoped_ptr<HashStoreContents> contents_; | |
56 | |
57 bool super_mac_valid_; | |
58 bool super_mac_dirty_; | |
59 | |
60 DISALLOW_COPY_AND_ASSIGN(PrefHashStoreTransactionImpl); | |
61 }; | |
62 | |
63 PrefHashStoreImpl::PrefHashStoreImpl(const std::string& seed, | |
64 const std::string& device_id, | |
65 bool use_super_mac) | |
66 : pref_hash_calculator_(seed, device_id), | |
67 use_super_mac_(use_super_mac) { | |
68 } | |
69 | |
70 PrefHashStoreImpl::~PrefHashStoreImpl() { | |
71 } | |
72 | |
73 void PrefHashStoreImpl::set_legacy_hash_store_contents( | |
74 scoped_ptr<HashStoreContents> legacy_hash_store_contents) { | |
75 legacy_hash_store_contents_ = legacy_hash_store_contents.Pass(); | |
76 } | |
77 | |
78 scoped_ptr<PrefHashStoreTransaction> PrefHashStoreImpl::BeginTransaction( | |
79 scoped_ptr<HashStoreContents> storage) { | |
80 return scoped_ptr<PrefHashStoreTransaction>( | |
81 new PrefHashStoreTransactionImpl(this, storage.Pass())); | |
82 } | |
83 | |
84 PrefHashStoreImpl::PrefHashStoreTransactionImpl::PrefHashStoreTransactionImpl( | |
85 PrefHashStoreImpl* outer, | |
86 scoped_ptr<HashStoreContents> storage) | |
87 : outer_(outer), | |
88 contents_(storage.Pass()), | |
89 super_mac_valid_(false), | |
90 super_mac_dirty_(false) { | |
91 if (!outer_->use_super_mac_) | |
92 return; | |
93 | |
94 // The store must be initialized and have a valid super MAC to be trusted. | |
95 | |
96 const base::DictionaryValue* store_contents = contents()->GetContents(); | |
97 if (!store_contents) | |
98 return; | |
99 | |
100 std::string super_mac = contents()->GetSuperMac(); | |
101 if (super_mac.empty()) | |
102 return; | |
103 | |
104 super_mac_valid_ = | |
105 outer_->pref_hash_calculator_.Validate( | |
106 contents()->hash_store_id(), store_contents, super_mac) == | |
107 PrefHashCalculator::VALID; | |
108 } | |
109 | |
110 PrefHashStoreImpl::PrefHashStoreTransactionImpl:: | |
111 ~PrefHashStoreTransactionImpl() { | |
112 if (super_mac_dirty_ && outer_->use_super_mac_) { | |
113 // Get the dictionary of hashes (or NULL if it doesn't exist). | |
114 const base::DictionaryValue* hashes_dict = contents()->GetContents(); | |
115 contents()->SetSuperMac(outer_->pref_hash_calculator_.Calculate( | |
116 contents()->hash_store_id(), hashes_dict)); | |
117 } | |
118 } | |
119 | |
120 PrefHashStoreTransaction::ValueState | |
121 PrefHashStoreImpl::PrefHashStoreTransactionImpl::CheckValue( | |
122 const std::string& path, | |
123 const base::Value* initial_value) const { | |
124 const base::DictionaryValue* hashes_dict = contents()->GetContents(); | |
125 | |
126 std::string last_hash; | |
127 if (hashes_dict) | |
128 hashes_dict->GetString(path, &last_hash); | |
129 | |
130 if (last_hash.empty()) { | |
131 // In the absence of a hash for this pref, always trust a NULL value, but | |
132 // only trust an existing value if the initial hashes dictionary is trusted. | |
133 if (!initial_value) | |
134 return TRUSTED_NULL_VALUE; | |
135 else if (super_mac_valid_) | |
136 return TRUSTED_UNKNOWN_VALUE; | |
137 else | |
138 return UNTRUSTED_UNKNOWN_VALUE; | |
139 } | |
140 | |
141 PrefHashCalculator::ValidationResult validation_result = | |
142 outer_->pref_hash_calculator_.Validate(path, initial_value, last_hash); | |
143 switch (validation_result) { | |
144 case PrefHashCalculator::VALID: | |
145 return UNCHANGED; | |
146 case PrefHashCalculator::VALID_SECURE_LEGACY: | |
147 return SECURE_LEGACY; | |
148 case PrefHashCalculator::INVALID: | |
149 return initial_value ? CHANGED : CLEARED; | |
150 } | |
151 NOTREACHED() << "Unexpected PrefHashCalculator::ValidationResult: " | |
152 << validation_result; | |
153 return UNTRUSTED_UNKNOWN_VALUE; | |
154 } | |
155 | |
156 void PrefHashStoreImpl::PrefHashStoreTransactionImpl::StoreHash( | |
157 const std::string& path, | |
158 const base::Value* new_value) { | |
159 const std::string mac = | |
160 outer_->pref_hash_calculator_.Calculate(path, new_value); | |
161 (*contents()->GetMutableContents())->SetString(path, mac); | |
162 super_mac_dirty_ = true; | |
163 } | |
164 | |
165 PrefHashStoreTransaction::ValueState | |
166 PrefHashStoreImpl::PrefHashStoreTransactionImpl::CheckSplitValue( | |
167 const std::string& path, | |
168 const base::DictionaryValue* initial_split_value, | |
169 std::vector<std::string>* invalid_keys) const { | |
170 DCHECK(invalid_keys && invalid_keys->empty()); | |
171 | |
172 std::map<std::string, std::string> split_macs; | |
173 const bool has_hashes = GetSplitMacs(path, &split_macs); | |
174 | |
175 // Treat NULL and empty the same; otherwise we would need to store a hash for | |
176 // the entire dictionary (or some other special beacon) to differentiate these | |
177 // two cases which are really the same for dictionaries. | |
178 if (!initial_split_value || initial_split_value->empty()) | |
179 return has_hashes ? CLEARED : UNCHANGED; | |
180 | |
181 if (!has_hashes) | |
182 return super_mac_valid_ ? TRUSTED_UNKNOWN_VALUE : UNTRUSTED_UNKNOWN_VALUE; | |
183 | |
184 bool has_secure_legacy_id_hashes = false; | |
185 std::string keyed_path(path); | |
186 keyed_path.push_back('.'); | |
187 const size_t common_part_length = keyed_path.length(); | |
188 for (base::DictionaryValue::Iterator it(*initial_split_value); !it.IsAtEnd(); | |
189 it.Advance()) { | |
190 std::map<std::string, std::string>::iterator entry = | |
191 split_macs.find(it.key()); | |
192 if (entry == split_macs.end()) { | |
193 invalid_keys->push_back(it.key()); | |
194 } else { | |
195 // Keep the common part from the old |keyed_path| and replace the key to | |
196 // get the new |keyed_path|. | |
197 keyed_path.replace(common_part_length, std::string::npos, it.key()); | |
198 switch (outer_->pref_hash_calculator_.Validate( | |
199 keyed_path, &it.value(), entry->second)) { | |
200 case PrefHashCalculator::VALID: | |
201 break; | |
202 case SECURE_LEGACY: | |
203 // Secure legacy device IDs based hashes are still accepted, but we | |
204 // should make sure to notify the caller for him to update the legacy | |
205 // hashes. | |
206 has_secure_legacy_id_hashes = true; | |
207 break; | |
208 case PrefHashCalculator::INVALID: | |
209 invalid_keys->push_back(it.key()); | |
210 break; | |
211 } | |
212 // Remove processed MACs, remaining MACs at the end will also be | |
213 // considered invalid. | |
214 split_macs.erase(entry); | |
215 } | |
216 } | |
217 | |
218 // Anything left in the map is missing from the data. | |
219 for (std::map<std::string, std::string>::const_iterator it = | |
220 split_macs.begin(); | |
221 it != split_macs.end(); | |
222 ++it) { | |
223 invalid_keys->push_back(it->first); | |
224 } | |
225 | |
226 return invalid_keys->empty() | |
227 ? (has_secure_legacy_id_hashes ? SECURE_LEGACY : UNCHANGED) | |
228 : CHANGED; | |
229 } | |
230 | |
231 void PrefHashStoreImpl::PrefHashStoreTransactionImpl::StoreSplitHash( | |
232 const std::string& path, | |
233 const base::DictionaryValue* split_value) { | |
234 scoped_ptr<HashStoreContents::MutableDictionary> mutable_dictionary = | |
235 contents()->GetMutableContents(); | |
236 (*mutable_dictionary)->Remove(path, NULL); | |
237 | |
238 if (split_value) { | |
239 std::string keyed_path(path); | |
240 keyed_path.push_back('.'); | |
241 const size_t common_part_length = keyed_path.length(); | |
242 for (base::DictionaryValue::Iterator it(*split_value); !it.IsAtEnd(); | |
243 it.Advance()) { | |
244 // Keep the common part from the old |keyed_path| and replace the key to | |
245 // get the new |keyed_path|. | |
246 keyed_path.replace(common_part_length, std::string::npos, it.key()); | |
247 (*mutable_dictionary)->SetString( | |
248 keyed_path, | |
249 outer_->pref_hash_calculator_.Calculate(keyed_path, &it.value())); | |
250 } | |
251 } | |
252 super_mac_dirty_ = true; | |
253 } | |
254 | |
255 bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::GetSplitMacs( | |
256 const std::string& key, | |
257 std::map<std::string, std::string>* split_macs) const { | |
258 DCHECK(split_macs); | |
259 DCHECK(split_macs->empty()); | |
260 | |
261 const base::DictionaryValue* hashes_dict = contents()->GetContents(); | |
262 const base::DictionaryValue* split_mac_dictionary = NULL; | |
263 if (!hashes_dict || !hashes_dict->GetDictionary(key, &split_mac_dictionary)) | |
264 return false; | |
265 for (base::DictionaryValue::Iterator it(*split_mac_dictionary); !it.IsAtEnd(); | |
266 it.Advance()) { | |
267 std::string mac_string; | |
268 if (!it.value().GetAsString(&mac_string)) { | |
269 NOTREACHED(); | |
270 continue; | |
271 } | |
272 split_macs->insert(make_pair(it.key(), mac_string)); | |
273 } | |
274 return true; | |
275 } | |
276 | |
277 bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::HasHash( | |
278 const std::string& path) const { | |
279 const base::DictionaryValue* hashes_dict = contents()->GetContents(); | |
280 return hashes_dict && hashes_dict->Get(path, NULL); | |
281 } | |
282 | |
283 void PrefHashStoreImpl::PrefHashStoreTransactionImpl::ImportHash( | |
284 const std::string& path, | |
285 const base::Value* hash) { | |
286 DCHECK(hash); | |
287 | |
288 (*contents()->GetMutableContents())->Set(path, hash->DeepCopy()); | |
289 | |
290 if (super_mac_valid_) | |
291 super_mac_dirty_ = true; | |
292 } | |
293 | |
294 void PrefHashStoreImpl::PrefHashStoreTransactionImpl::ClearHash( | |
295 const std::string& path) { | |
296 if ((*contents()->GetMutableContents())->RemovePath(path, NULL) && | |
297 super_mac_valid_) { | |
298 super_mac_dirty_ = true; | |
299 } | |
300 } | |
301 | |
302 bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::IsSuperMACValid() const { | |
303 return super_mac_valid_; | |
304 } | |
305 | |
306 bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::StampSuperMac() { | |
307 if (!outer_->use_super_mac_ || super_mac_valid_) | |
308 return false; | |
309 super_mac_dirty_ = true; | |
310 return true; | |
311 } | |
OLD | NEW |