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