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.
5 #include "chrome/browser/prefs/tracked/pref_hash_store_impl.h"
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"
13 class PrefHashStoreImpl::PrefHashStoreTransactionImpl
14 : public PrefHashStoreTransaction
{
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
;
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
;
39 bool GetSplitMacs(const std::string
& path
,
40 std::map
<std::string
, std::string
>* split_macs
) const;
42 HashStoreContents
* contents() {
43 return outer_
->legacy_hash_store_contents_
44 ? outer_
->legacy_hash_store_contents_
.get()
48 const HashStoreContents
* contents() const {
49 return outer_
->legacy_hash_store_contents_
50 ? outer_
->legacy_hash_store_contents_
.get()
54 PrefHashStoreImpl
* outer_
;
55 scoped_ptr
<HashStoreContents
> contents_
;
57 bool super_mac_valid_
;
58 bool super_mac_dirty_
;
60 DISALLOW_COPY_AND_ASSIGN(PrefHashStoreTransactionImpl
);
63 PrefHashStoreImpl::PrefHashStoreImpl(const std::string
& seed
,
64 const std::string
& device_id
,
66 : pref_hash_calculator_(seed
, device_id
),
67 use_super_mac_(use_super_mac
) {
70 PrefHashStoreImpl::~PrefHashStoreImpl() {
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();
78 scoped_ptr
<PrefHashStoreTransaction
> PrefHashStoreImpl::BeginTransaction(
79 scoped_ptr
<HashStoreContents
> storage
) {
80 return scoped_ptr
<PrefHashStoreTransaction
>(
81 new PrefHashStoreTransactionImpl(this, storage
.Pass()));
84 PrefHashStoreImpl::PrefHashStoreTransactionImpl::PrefHashStoreTransactionImpl(
85 PrefHashStoreImpl
* outer
,
86 scoped_ptr
<HashStoreContents
> storage
)
88 contents_(storage
.Pass()),
89 super_mac_valid_(false),
90 super_mac_dirty_(false) {
91 if (!outer_
->use_super_mac_
)
94 // The store must be initialized and have a valid super MAC to be trusted.
96 const base::DictionaryValue
* store_contents
= contents()->GetContents();
100 std::string super_mac
= contents()->GetSuperMac();
101 if (super_mac
.empty())
105 outer_
->pref_hash_calculator_
.Validate(
106 contents()->hash_store_id(), store_contents
, super_mac
) ==
107 PrefHashCalculator::VALID
;
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
));
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();
126 std::string last_hash
;
128 hashes_dict
->GetString(path
, &last_hash
);
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.
134 return TRUSTED_NULL_VALUE
;
135 else if (super_mac_valid_
)
136 return TRUSTED_UNKNOWN_VALUE
;
138 return UNTRUSTED_UNKNOWN_VALUE
;
141 PrefHashCalculator::ValidationResult validation_result
=
142 outer_
->pref_hash_calculator_
.Validate(path
, initial_value
, last_hash
);
143 switch (validation_result
) {
144 case PrefHashCalculator::VALID
:
146 case PrefHashCalculator::VALID_SECURE_LEGACY
:
147 return SECURE_LEGACY
;
148 case PrefHashCalculator::INVALID
:
149 return initial_value
? CHANGED
: CLEARED
;
151 NOTREACHED() << "Unexpected PrefHashCalculator::ValidationResult: "
152 << validation_result
;
153 return UNTRUSTED_UNKNOWN_VALUE
;
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;
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());
172 std::map
<std::string
, std::string
> split_macs
;
173 const bool has_hashes
= GetSplitMacs(path
, &split_macs
);
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
;
182 return super_mac_valid_
? TRUSTED_UNKNOWN_VALUE
: UNTRUSTED_UNKNOWN_VALUE
;
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();
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());
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
:
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
206 has_secure_legacy_id_hashes
= true;
208 case PrefHashCalculator::INVALID
:
209 invalid_keys
->push_back(it
.key());
212 // Remove processed MACs, remaining MACs at the end will also be
213 // considered invalid.
214 split_macs
.erase(entry
);
218 // Anything left in the map is missing from the data.
219 for (std::map
<std::string
, std::string
>::const_iterator it
=
221 it
!= split_macs
.end();
223 invalid_keys
->push_back(it
->first
);
226 return invalid_keys
->empty()
227 ? (has_secure_legacy_id_hashes
? SECURE_LEGACY
: UNCHANGED
)
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
);
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();
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(
249 outer_
->pref_hash_calculator_
.Calculate(keyed_path
, &it
.value()));
252 super_mac_dirty_
= true;
255 bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::GetSplitMacs(
256 const std::string
& key
,
257 std::map
<std::string
, std::string
>* split_macs
) const {
259 DCHECK(split_macs
->empty());
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
))
265 for (base::DictionaryValue::Iterator
it(*split_mac_dictionary
); !it
.IsAtEnd();
267 std::string mac_string
;
268 if (!it
.value().GetAsString(&mac_string
)) {
272 split_macs
->insert(make_pair(it
.key(), mac_string
));
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
);
283 void PrefHashStoreImpl::PrefHashStoreTransactionImpl::ImportHash(
284 const std::string
& path
,
285 const base::Value
* hash
) {
288 (*contents()->GetMutableContents())->Set(path
, hash
->DeepCopy());
290 if (super_mac_valid_
)
291 super_mac_dirty_
= true;
294 void PrefHashStoreImpl::PrefHashStoreTransactionImpl::ClearHash(
295 const std::string
& path
) {
296 if ((*contents()->GetMutableContents())->RemovePath(path
, NULL
) &&
298 super_mac_dirty_
= true;
302 bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::IsSuperMACValid() const {
303 return super_mac_valid_
;
306 bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::StampSuperMac() {
307 if (!outer_
->use_super_mac_
|| super_mac_valid_
)
309 super_mac_dirty_
= true;