Roll src/third_party/WebKit 9f7fb92:f103b33 (svn 202621:202622)
[chromium-blink-merge.git] / components / user_prefs / tracked / pref_hash_calculator.cc
blob3d5b7fa37c2340dfac937184233b5786df8890e2
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 "components/user_prefs/tracked/pref_hash_calculator.h"
7 #include <vector>
9 #include "base/bind.h"
10 #include "base/json/json_string_value_serializer.h"
11 #include "base/logging.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/string_util.h"
14 #include "base/values.h"
15 #include "crypto/hmac.h"
17 namespace {
19 // Calculates an HMAC of |message| using |key|, encoded as a hexadecimal string.
20 std::string GetDigestString(const std::string& key,
21 const std::string& message) {
22 crypto::HMAC hmac(crypto::HMAC::SHA256);
23 std::vector<uint8> digest(hmac.DigestLength());
24 if (!hmac.Init(key) || !hmac.Sign(message, &digest[0], digest.size())) {
25 NOTREACHED();
26 return std::string();
28 return base::HexEncode(&digest[0], digest.size());
31 // Verifies that |digest_string| is a valid HMAC of |message| using |key|.
32 // |digest_string| must be encoded as a hexadecimal string.
33 bool VerifyDigestString(const std::string& key,
34 const std::string& message,
35 const std::string& digest_string) {
36 crypto::HMAC hmac(crypto::HMAC::SHA256);
37 std::vector<uint8> digest;
38 return base::HexStringToBytes(digest_string, &digest) && hmac.Init(key) &&
39 hmac.Verify(message,
40 base::StringPiece(reinterpret_cast<char*>(&digest[0]),
41 digest.size()));
44 // Renders |value| as a string. |value| may be NULL, in which case the result
45 // is an empty string. This method can be expensive and its result should be
46 // re-used rather than recomputed where possible.
47 std::string ValueAsString(const base::Value* value) {
48 // Dictionary values may contain empty lists and sub-dictionaries. Make a
49 // deep copy with those removed to make the hash more stable.
50 const base::DictionaryValue* dict_value;
51 scoped_ptr<base::DictionaryValue> canonical_dict_value;
52 if (value && value->GetAsDictionary(&dict_value)) {
53 canonical_dict_value = dict_value->DeepCopyWithoutEmptyChildren();
54 value = canonical_dict_value.get();
57 std::string value_as_string;
58 if (value) {
59 JSONStringValueSerializer serializer(&value_as_string);
60 serializer.Serialize(*value);
63 return value_as_string;
66 // Concatenates |device_id|, |path|, and |value_as_string| to give the hash
67 // input.
68 std::string GetMessage(const std::string& device_id,
69 const std::string& path,
70 const std::string& value_as_string) {
71 std::string message;
72 message.reserve(device_id.size() + path.size() + value_as_string.size());
73 message.append(device_id);
74 message.append(path);
75 message.append(value_as_string);
76 return message;
79 // Generates a device ID based on the input device ID. The derived device ID has
80 // no useful properties beyond those of the input device ID except that it is
81 // consistent with previous implementations.
82 // TODO(gab): Remove this once UMA reports for
83 // Settings.TrackedPreferenceMigratedLegacyDeviceId become insignificant.
84 std::string GenerateDeviceIdLikePrefMetricsServiceDid(
85 const std::string& original_device_id) {
86 if (original_device_id.empty())
87 return std::string();
88 return base::ToLowerASCII(
89 GetDigestString(original_device_id, "PrefMetricsService"));
92 } // namespace
94 PrefHashCalculator::PrefHashCalculator(const std::string& seed,
95 const std::string& device_id)
96 : seed_(seed),
97 device_id_(device_id),
98 legacy_device_id_(GenerateDeviceIdLikePrefMetricsServiceDid(device_id)) {
101 PrefHashCalculator::~PrefHashCalculator() {
104 std::string PrefHashCalculator::Calculate(const std::string& path,
105 const base::Value* value) const {
106 return GetDigestString(seed_,
107 GetMessage(device_id_, path, ValueAsString(value)));
110 PrefHashCalculator::ValidationResult PrefHashCalculator::Validate(
111 const std::string& path,
112 const base::Value* value,
113 const std::string& digest_string) const {
114 const std::string value_as_string(ValueAsString(value));
115 if (VerifyDigestString(seed_, GetMessage(device_id_, path, value_as_string),
116 digest_string)) {
117 return VALID;
119 if (VerifyDigestString(seed_,
120 GetMessage(legacy_device_id_, path, value_as_string),
121 digest_string)) {
122 return VALID_SECURE_LEGACY;
124 return INVALID;