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 "chrome/browser/prefs/pref_hash_calculator.h" | 5 #include "chrome/browser/prefs/pref_hash_calculator.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/json/json_string_value_serializer.h" | 9 #include "base/json/json_string_value_serializer.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
14 #include "base/values.h" | 14 #include "base/values.h" |
15 #include "crypto/hmac.h" | 15 #include "crypto/hmac.h" |
16 | 16 |
17 namespace { | 17 namespace { |
18 | 18 |
| 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(); |
| 27 } |
| 28 return base::HexEncode(&digest[0], digest.size()); |
| 29 } |
| 30 |
| 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) && |
| 39 hmac.Init(key) && |
| 40 hmac.Verify(message, |
| 41 base::StringPiece(reinterpret_cast<char*>(&digest[0]), |
| 42 digest.size())); |
| 43 } |
| 44 |
19 // Renders |value| as a string. |value| may be NULL, in which case the result | 45 // Renders |value| as a string. |value| may be NULL, in which case the result |
20 // is an empty string. | 46 // is an empty string. |
21 std::string ValueAsString(const base::Value* value) { | 47 std::string ValueAsString(const base::Value* value) { |
22 // Dictionary values may contain empty lists and sub-dictionaries. Make a | 48 // Dictionary values may contain empty lists and sub-dictionaries. Make a |
23 // deep copy with those removed to make the hash more stable. | 49 // deep copy with those removed to make the hash more stable. |
24 const base::DictionaryValue* dict_value; | 50 const base::DictionaryValue* dict_value; |
25 scoped_ptr<base::DictionaryValue> canonical_dict_value; | 51 scoped_ptr<base::DictionaryValue> canonical_dict_value; |
26 if (value && value->GetAsDictionary(&dict_value)) { | 52 if (value && value->GetAsDictionary(&dict_value)) { |
27 canonical_dict_value.reset(dict_value->DeepCopyWithoutEmptyChildren()); | 53 canonical_dict_value.reset(dict_value->DeepCopyWithoutEmptyChildren()); |
28 value = canonical_dict_value.get(); | 54 value = canonical_dict_value.get(); |
29 } | 55 } |
30 | 56 |
31 std::string value_as_string; | 57 std::string value_as_string; |
32 if (value) { | 58 if (value) { |
33 JSONStringValueSerializer serializer(&value_as_string); | 59 JSONStringValueSerializer serializer(&value_as_string); |
34 serializer.Serialize(*value); | 60 serializer.Serialize(*value); |
35 } | 61 } |
36 | 62 |
37 return value_as_string; | 63 return value_as_string; |
38 } | 64 } |
39 | 65 |
40 // Common helper for all hash algorithms. | 66 // Common helper for all hash algorithms. |
41 std::string CalculateFromValueAndComponents( | 67 std::string GetMessageFromValueAndComponents( |
42 const std::string& seed, | |
43 const base::Value* value, | 68 const base::Value* value, |
44 const std::vector<std::string>& extra_components) { | 69 const std::vector<std::string>& extra_components) { |
45 static const size_t kSHA256DigestSize = 32; | 70 return JoinString(extra_components, "") + ValueAsString(value); |
| 71 } |
46 | 72 |
47 std::string message = JoinString(extra_components, "") + ValueAsString(value); | |
48 | 73 |
49 crypto::HMAC hmac(crypto::HMAC::SHA256); | 74 // Generates a device ID based on the input device ID. The derived device ID has |
50 unsigned char digest[kSHA256DigestSize]; | 75 // no useful properties beyond those of the input device ID except that it is |
51 if (!hmac.Init(seed) || !hmac.Sign(message, digest, arraysize(digest))) { | 76 // consistent with previous implementations. |
52 NOTREACHED(); | 77 std::string GenerateDeviceIdLikePrefMetricsServiceDid( |
| 78 const std::string& original_device_id) { |
| 79 if (original_device_id.empty()) |
53 return std::string(); | 80 return std::string(); |
54 } | 81 return StringToLowerASCII( |
| 82 GetDigestString(original_device_id, "PrefMetricsService")); |
| 83 } |
55 | 84 |
56 return base::HexEncode(digest, arraysize(digest)); | 85 // Verifies a hash using a deprecated hash algorithm. For validating old |
| 86 // hashes during migration. |
| 87 bool VerifyLegacyHash(const std::string& seed, |
| 88 const base::Value* value, |
| 89 const std::string& digest_string) { |
| 90 return VerifyDigestString( |
| 91 seed, |
| 92 GetMessageFromValueAndComponents(value, std::vector<std::string>()), |
| 93 digest_string); |
57 } | 94 } |
58 | 95 |
59 } // namespace | 96 } // namespace |
60 | 97 |
61 PrefHashCalculator::PrefHashCalculator(const std::string& seed, | 98 PrefHashCalculator::PrefHashCalculator(const std::string& seed, |
62 const std::string& device_id) | 99 const std::string& device_id) |
63 : seed_(seed), device_id_(device_id) {} | 100 : seed_(seed), |
| 101 device_id_(GenerateDeviceIdLikePrefMetricsServiceDid(device_id)) {} |
64 | 102 |
65 std::string PrefHashCalculator::Calculate(const std::string& path, | 103 std::string PrefHashCalculator::Calculate(const std::string& path, |
66 const base::Value* value) const { | 104 const base::Value* value) const { |
67 std::vector<std::string> components; | 105 return GetDigestString(seed_, GetMessage(path, value)); |
68 if (!device_id_.empty()) | |
69 components.push_back(device_id_); | |
70 components.push_back(path); | |
71 return CalculateFromValueAndComponents(seed_, value, components); | |
72 } | 106 } |
73 | 107 |
74 PrefHashCalculator::ValidationResult PrefHashCalculator::Validate( | 108 PrefHashCalculator::ValidationResult PrefHashCalculator::Validate( |
75 const std::string& path, | 109 const std::string& path, |
76 const base::Value* value, | 110 const base::Value* value, |
77 const std::string& hash) const { | 111 const std::string& digest_string) const { |
78 if (hash == Calculate(path, value)) | 112 if (VerifyDigestString(seed_, GetMessage(path, value), digest_string)) |
79 return VALID; | 113 return VALID; |
80 if (hash == CalculateLegacyHash(path, value)) | 114 if (VerifyLegacyHash(seed_, value, digest_string)) |
81 return VALID_LEGACY; | 115 return VALID_LEGACY; |
82 return INVALID; | 116 return INVALID; |
83 } | 117 } |
84 | 118 |
85 std::string PrefHashCalculator::CalculateLegacyHash( | 119 std::string PrefHashCalculator::GetMessage(const std::string& path, |
86 const std::string& path, const base::Value* value) const { | 120 const base::Value* value) const { |
87 return CalculateFromValueAndComponents(seed_, | 121 std::vector<std::string> components; |
88 value, | 122 if (!device_id_.empty()) |
89 std::vector<std::string>()); | 123 components.push_back(device_id_); |
| 124 components.push_back(path); |
| 125 return GetMessageFromValueAndComponents(value, components); |
90 } | 126 } |
OLD | NEW |