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"
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"
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())) {
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
) &&
40 base::StringPiece(reinterpret_cast<char*>(&digest
[0]),
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
;
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
68 std::string
GetMessage(const std::string
& device_id
,
69 const std::string
& path
,
70 const std::string
& value_as_string
) {
72 message
.reserve(device_id
.size() + path
.size() + value_as_string
.size());
73 message
.append(device_id
);
75 message
.append(value_as_string
);
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())
88 return base::ToLowerASCII(
89 GetDigestString(original_device_id
, "PrefMetricsService"));
94 PrefHashCalculator::PrefHashCalculator(const std::string
& seed
,
95 const std::string
& device_id
)
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
),
119 if (VerifyDigestString(seed_
,
120 GetMessage(legacy_device_id_
, path
, value_as_string
),
122 return VALID_SECURE_LEGACY
;