OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 "components/user_prefs/tracked/pref_hash_store_impl.h" | 5 #include "components/user_prefs/tracked/pref_hash_store_impl.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/memory/ptr_util.h" | |
12 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
13 #include "base/values.h" | 14 #include "base/values.h" |
14 #include "components/user_prefs/tracked/hash_store_contents.h" | 15 #include "components/user_prefs/tracked/hash_store_contents.h" |
15 #include "components/user_prefs/tracked/pref_hash_store_transaction.h" | 16 #include "components/user_prefs/tracked/pref_hash_store_transaction.h" |
16 | 17 |
17 class PrefHashStoreImpl::PrefHashStoreTransactionImpl | 18 class PrefHashStoreImpl::PrefHashStoreTransactionImpl |
18 : public PrefHashStoreTransaction { | 19 : public PrefHashStoreTransaction { |
19 public: | 20 public: |
20 // Constructs a PrefHashStoreTransactionImpl which can use the private | 21 // Constructs a PrefHashStoreTransactionImpl which can use the private |
21 // members of its |outer| PrefHashStoreImpl. | 22 // members of its |outer| PrefHashStoreImpl. |
22 PrefHashStoreTransactionImpl(PrefHashStoreImpl* outer, | 23 PrefHashStoreTransactionImpl(PrefHashStoreImpl* outer, |
23 std::unique_ptr<HashStoreContents> storage); | 24 HashStoreContents* storage); |
24 ~PrefHashStoreTransactionImpl() override; | 25 ~PrefHashStoreTransactionImpl() override; |
25 | 26 |
26 // PrefHashStoreTransaction implementation. | 27 // PrefHashStoreTransaction implementation. |
28 std::string GetStoreUMASuffix() const override; | |
27 ValueState CheckValue(const std::string& path, | 29 ValueState CheckValue(const std::string& path, |
28 const base::Value* value) const override; | 30 const base::Value* value) const override; |
29 void StoreHash(const std::string& path, const base::Value* value) override; | 31 void StoreHash(const std::string& path, const base::Value* value) override; |
30 ValueState CheckSplitValue( | 32 ValueState CheckSplitValue( |
31 const std::string& path, | 33 const std::string& path, |
32 const base::DictionaryValue* initial_split_value, | 34 const base::DictionaryValue* initial_split_value, |
33 std::vector<std::string>* invalid_keys) const override; | 35 std::vector<std::string>* invalid_keys) const override; |
34 void StoreSplitHash(const std::string& path, | 36 void StoreSplitHash(const std::string& path, |
35 const base::DictionaryValue* split_value) override; | 37 const base::DictionaryValue* split_value) override; |
36 bool HasHash(const std::string& path) const override; | 38 bool HasHash(const std::string& path) const override; |
37 void ImportHash(const std::string& path, const base::Value* hash) override; | 39 void ImportHash(const std::string& path, const base::Value* hash) override; |
38 void ClearHash(const std::string& path) override; | 40 void ClearHash(const std::string& path) override; |
39 bool IsSuperMACValid() const override; | 41 bool IsSuperMACValid() const override; |
40 bool StampSuperMac() override; | 42 bool StampSuperMac() override; |
41 | 43 |
42 private: | 44 private: |
43 PrefHashStoreImpl* outer_; | 45 PrefHashStoreImpl* outer_; |
44 std::unique_ptr<HashStoreContents> contents_; | 46 HashStoreContents* contents_; |
45 | 47 |
46 bool super_mac_valid_; | 48 bool super_mac_valid_; |
47 bool super_mac_dirty_; | 49 bool super_mac_dirty_; |
48 | 50 |
49 DISALLOW_COPY_AND_ASSIGN(PrefHashStoreTransactionImpl); | 51 DISALLOW_COPY_AND_ASSIGN(PrefHashStoreTransactionImpl); |
50 }; | 52 }; |
51 | 53 |
52 PrefHashStoreImpl::PrefHashStoreImpl(const std::string& seed, | 54 PrefHashStoreImpl::PrefHashStoreImpl(const std::string& seed, |
53 const std::string& device_id, | 55 const std::string& device_id, |
54 bool use_super_mac) | 56 bool use_super_mac) |
55 : pref_hash_calculator_(seed, device_id), use_super_mac_(use_super_mac) { | 57 : pref_hash_calculator_(seed, device_id), use_super_mac_(use_super_mac) { |
56 } | 58 } |
57 | 59 |
58 PrefHashStoreImpl::~PrefHashStoreImpl() { | 60 PrefHashStoreImpl::~PrefHashStoreImpl() { |
59 } | 61 } |
60 | 62 |
61 std::unique_ptr<PrefHashStoreTransaction> PrefHashStoreImpl::BeginTransaction( | 63 std::unique_ptr<PrefHashStoreTransaction> PrefHashStoreImpl::BeginTransaction( |
62 std::unique_ptr<HashStoreContents> storage) { | 64 HashStoreContents* storage) { |
63 return std::unique_ptr<PrefHashStoreTransaction>( | 65 return std::unique_ptr<PrefHashStoreTransaction>( |
64 new PrefHashStoreTransactionImpl(this, std::move(storage))); | 66 new PrefHashStoreTransactionImpl(this, storage)); |
67 } | |
68 | |
69 std::string PrefHashStoreImpl::ComputeMac(const std::string& path, | |
70 const base::Value* value) { | |
71 return pref_hash_calculator_.Calculate(path, value); | |
72 } | |
73 | |
74 std::unique_ptr<base::DictionaryValue> PrefHashStoreImpl::ComputeSplitMacs( | |
75 const std::string& path, | |
76 const base::DictionaryValue* split_values) { | |
77 if (!split_values) | |
gab
2016/08/08 04:37:46
Does it ever make sense to call this when |!split_
proberge
2016/08/31 17:30:17
Done.
| |
78 return nullptr; | |
79 | |
80 std::string keyed_path(path); | |
81 keyed_path.push_back('.'); | |
82 const size_t common_part_length = keyed_path.length(); | |
83 | |
84 std::unique_ptr<base::DictionaryValue> split_macs(new base::DictionaryValue); | |
85 | |
86 for (base::DictionaryValue::Iterator it(*split_values); !it.IsAtEnd(); | |
87 it.Advance()) { | |
88 // Keep the common part from the old |keyed_path| and replace the key to | |
89 // get the new |keyed_path|. | |
90 keyed_path.replace(common_part_length, std::string::npos, it.key()); | |
91 | |
92 split_macs->SetStringWithoutPathExpansion( | |
93 it.key(), ComputeMac(keyed_path, &it.value())); | |
94 } | |
95 | |
96 return split_macs; | |
65 } | 97 } |
66 | 98 |
67 PrefHashStoreImpl::PrefHashStoreTransactionImpl::PrefHashStoreTransactionImpl( | 99 PrefHashStoreImpl::PrefHashStoreTransactionImpl::PrefHashStoreTransactionImpl( |
68 PrefHashStoreImpl* outer, | 100 PrefHashStoreImpl* outer, |
69 std::unique_ptr<HashStoreContents> storage) | 101 HashStoreContents* storage) |
70 : outer_(outer), | 102 : outer_(outer), |
71 contents_(std::move(storage)), | 103 contents_(storage), |
72 super_mac_valid_(false), | 104 super_mac_valid_(false), |
73 super_mac_dirty_(false) { | 105 super_mac_dirty_(false) { |
74 if (!outer_->use_super_mac_) | 106 if (!outer_->use_super_mac_) |
75 return; | 107 return; |
76 | 108 |
77 // The store must have a valid super MAC to be trusted. | 109 // The store must have a valid super MAC to be trusted. |
78 std::string super_mac = contents_->GetSuperMac(); | 110 std::string super_mac = contents_->GetSuperMac(); |
79 if (super_mac.empty()) | 111 if (super_mac.empty()) |
80 return; | 112 return; |
81 | 113 |
82 super_mac_valid_ = | 114 super_mac_valid_ = |
83 outer_->pref_hash_calculator_.Validate( | 115 outer_->pref_hash_calculator_.Validate( |
84 "", contents_->GetContents(), super_mac) == PrefHashCalculator::VALID; | 116 "", contents_->GetContents(), super_mac) == PrefHashCalculator::VALID; |
85 } | 117 } |
86 | 118 |
87 PrefHashStoreImpl::PrefHashStoreTransactionImpl:: | 119 PrefHashStoreImpl::PrefHashStoreTransactionImpl:: |
88 ~PrefHashStoreTransactionImpl() { | 120 ~PrefHashStoreTransactionImpl() { |
89 if (super_mac_dirty_ && outer_->use_super_mac_) { | 121 if (super_mac_dirty_ && outer_->use_super_mac_) { |
90 // Get the dictionary of hashes (or NULL if it doesn't exist). | 122 // Get the dictionary of hashes (or NULL if it doesn't exist). |
91 const base::DictionaryValue* hashes_dict = contents_->GetContents(); | 123 const base::DictionaryValue* hashes_dict = contents_->GetContents(); |
92 contents_->SetSuperMac( | 124 contents_->SetSuperMac(outer_->ComputeMac("", hashes_dict)); |
93 outer_->pref_hash_calculator_.Calculate("", hashes_dict)); | |
94 } | 125 } |
95 } | 126 } |
96 | 127 |
128 std::string PrefHashStoreImpl::PrefHashStoreTransactionImpl::GetStoreUMASuffix() | |
129 const { | |
130 return contents_->GetUMASuffix(); | |
131 } | |
132 | |
97 PrefHashStoreTransaction::ValueState | 133 PrefHashStoreTransaction::ValueState |
98 PrefHashStoreImpl::PrefHashStoreTransactionImpl::CheckValue( | 134 PrefHashStoreImpl::PrefHashStoreTransactionImpl::CheckValue( |
99 const std::string& path, | 135 const std::string& path, |
100 const base::Value* initial_value) const { | 136 const base::Value* initial_value) const { |
101 std::string last_hash; | 137 std::string last_hash; |
102 contents_->GetMac(path, &last_hash); | 138 contents_->GetMac(path, &last_hash); |
103 | 139 |
104 if (last_hash.empty()) { | 140 if (last_hash.empty()) { |
105 // In the absence of a hash for this pref, always trust a NULL value, but | 141 // In the absence of a hash for this pref, always trust a NULL value, but |
106 // only trust an existing value if the initial hashes dictionary is trusted. | 142 // only trust an existing value if the initial hashes dictionary is trusted. |
(...skipping 16 matching lines...) Expand all Loading... | |
123 return initial_value ? CHANGED : CLEARED; | 159 return initial_value ? CHANGED : CLEARED; |
124 } | 160 } |
125 NOTREACHED() << "Unexpected PrefHashCalculator::ValidationResult: " | 161 NOTREACHED() << "Unexpected PrefHashCalculator::ValidationResult: " |
126 << validation_result; | 162 << validation_result; |
127 return UNTRUSTED_UNKNOWN_VALUE; | 163 return UNTRUSTED_UNKNOWN_VALUE; |
128 } | 164 } |
129 | 165 |
130 void PrefHashStoreImpl::PrefHashStoreTransactionImpl::StoreHash( | 166 void PrefHashStoreImpl::PrefHashStoreTransactionImpl::StoreHash( |
131 const std::string& path, | 167 const std::string& path, |
132 const base::Value* new_value) { | 168 const base::Value* new_value) { |
133 const std::string mac = | 169 const std::string mac = outer_->ComputeMac(path, new_value); |
134 outer_->pref_hash_calculator_.Calculate(path, new_value); | |
135 contents_->SetMac(path, mac); | 170 contents_->SetMac(path, mac); |
136 super_mac_dirty_ = true; | 171 super_mac_dirty_ = true; |
137 } | 172 } |
138 | 173 |
139 PrefHashStoreTransaction::ValueState | 174 PrefHashStoreTransaction::ValueState |
140 PrefHashStoreImpl::PrefHashStoreTransactionImpl::CheckSplitValue( | 175 PrefHashStoreImpl::PrefHashStoreTransactionImpl::CheckSplitValue( |
141 const std::string& path, | 176 const std::string& path, |
142 const base::DictionaryValue* initial_split_value, | 177 const base::DictionaryValue* initial_split_value, |
143 std::vector<std::string>* invalid_keys) const { | 178 std::vector<std::string>* invalid_keys) const { |
144 DCHECK(invalid_keys && invalid_keys->empty()); | 179 DCHECK(invalid_keys && invalid_keys->empty()); |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
200 ? (has_secure_legacy_id_hashes ? SECURE_LEGACY : UNCHANGED) | 235 ? (has_secure_legacy_id_hashes ? SECURE_LEGACY : UNCHANGED) |
201 : CHANGED; | 236 : CHANGED; |
202 } | 237 } |
203 | 238 |
204 void PrefHashStoreImpl::PrefHashStoreTransactionImpl::StoreSplitHash( | 239 void PrefHashStoreImpl::PrefHashStoreTransactionImpl::StoreSplitHash( |
205 const std::string& path, | 240 const std::string& path, |
206 const base::DictionaryValue* split_value) { | 241 const base::DictionaryValue* split_value) { |
207 contents_->RemoveEntry(path); | 242 contents_->RemoveEntry(path); |
208 | 243 |
209 if (split_value) { | 244 if (split_value) { |
210 std::string keyed_path(path); | 245 std::unique_ptr<base::DictionaryValue> split_macs = |
211 keyed_path.push_back('.'); | 246 outer_->ComputeSplitMacs(path, split_value); |
212 const size_t common_part_length = keyed_path.length(); | 247 |
213 for (base::DictionaryValue::Iterator it(*split_value); !it.IsAtEnd(); | 248 for (base::DictionaryValue::Iterator it(*split_macs); !it.IsAtEnd(); |
214 it.Advance()) { | 249 it.Advance()) { |
215 // Keep the common part from the old |keyed_path| and replace the key to | 250 const base::StringValue* value_as_string; |
216 // get the new |keyed_path|. | 251 bool is_string = it.value().GetAsString(&value_as_string); |
217 keyed_path.replace(common_part_length, std::string::npos, it.key()); | 252 DCHECK(is_string); |
218 contents_->SetSplitMac( | 253 |
219 path, it.key(), | 254 contents_->SetSplitMac(path, it.key(), value_as_string->GetString()); |
220 outer_->pref_hash_calculator_.Calculate(keyed_path, &it.value())); | |
221 } | 255 } |
222 } | 256 } |
223 super_mac_dirty_ = true; | 257 super_mac_dirty_ = true; |
224 } | 258 } |
225 | 259 |
226 bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::HasHash( | 260 bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::HasHash( |
227 const std::string& path) const { | 261 const std::string& path) const { |
228 std::string out_value; | 262 std::string out_value; |
229 std::map<std::string, std::string> out_values; | 263 std::map<std::string, std::string> out_values; |
230 return contents_->GetMac(path, &out_value) || | 264 return contents_->GetMac(path, &out_value) || |
(...skipping 21 matching lines...) Expand all Loading... | |
252 bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::IsSuperMACValid() const { | 286 bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::IsSuperMACValid() const { |
253 return super_mac_valid_; | 287 return super_mac_valid_; |
254 } | 288 } |
255 | 289 |
256 bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::StampSuperMac() { | 290 bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::StampSuperMac() { |
257 if (!outer_->use_super_mac_ || super_mac_valid_) | 291 if (!outer_->use_super_mac_ || super_mac_valid_) |
258 return false; | 292 return false; |
259 super_mac_dirty_ = true; | 293 super_mac_dirty_ = true; |
260 return true; | 294 return true; |
261 } | 295 } |
OLD | NEW |