Chromium Code Reviews| 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 GetHMACHex(const std::string& key, const std::string& message) { | |
| 21 crypto::HMAC hmac(crypto::HMAC::SHA256); | |
| 22 std::vector<uint8> digest(hmac.DigestLength()); | |
|
gab
2014/01/07 19:18:07
This is nicer than the previous method hardcoding
erikwright (departed)
2014/01/08 22:11:24
We now have two HMAC calculations. They are virtua
gab
2014/01/08 23:48:20
Of course, my bad, I'd overlooked this initially.
| |
| 23 if (!hmac.Init(key) || !hmac.Sign(message, &digest[0], digest.size())) { | |
|
Bernhard Bauer
2014/01/07 19:20:45
When does this fail? Is it really necessary to han
erikwright (departed)
2014/01/08 22:11:24
It should presumably never fail, but it's inapprop
Bernhard Bauer
2014/01/09 08:51:35
Yes, but in production the code will look like thi
| |
| 24 NOTREACHED(); | |
| 25 return std::string(); | |
| 26 } | |
| 27 return base::HexEncode(digest.data(), digest.size()); | |
| 28 } | |
| 29 | |
| 19 // Renders |value| as a string. |value| may be NULL, in which case the result | 30 // Renders |value| as a string. |value| may be NULL, in which case the result |
| 20 // is an empty string. | 31 // is an empty string. |
| 21 std::string ValueAsString(const base::Value* value) { | 32 std::string ValueAsString(const base::Value* value) { |
| 22 // Dictionary values may contain empty lists and sub-dictionaries. Make a | 33 // Dictionary values may contain empty lists and sub-dictionaries. Make a |
| 23 // deep copy with those removed to make the hash more stable. | 34 // deep copy with those removed to make the hash more stable. |
| 24 const base::DictionaryValue* dict_value; | 35 const base::DictionaryValue* dict_value; |
| 25 scoped_ptr<DictionaryValue> canonical_dict_value; | 36 scoped_ptr<DictionaryValue> canonical_dict_value; |
| 26 if (value && value->GetAsDictionary(&dict_value)) { | 37 if (value && value->GetAsDictionary(&dict_value)) { |
| 27 canonical_dict_value.reset(dict_value->DeepCopyWithoutEmptyChildren()); | 38 canonical_dict_value.reset(dict_value->DeepCopyWithoutEmptyChildren()); |
| 28 value = canonical_dict_value.get(); | 39 value = canonical_dict_value.get(); |
| 29 } | 40 } |
| 30 | 41 |
| 31 std::string value_as_string; | 42 std::string value_as_string; |
| 32 if (value) { | 43 if (value) { |
| 33 JSONStringValueSerializer serializer(&value_as_string); | 44 JSONStringValueSerializer serializer(&value_as_string); |
| 34 serializer.Serialize(*value); | 45 serializer.Serialize(*value); |
| 35 } | 46 } |
| 36 | 47 |
| 37 return value_as_string; | 48 return value_as_string; |
| 38 } | 49 } |
| 39 | 50 |
| 40 // Common helper for all hash algorithms. | 51 // Common helper for all hash algorithms. |
| 41 std::string CalculateFromValueAndComponents( | 52 std::string CalculateFromValueAndComponents( |
| 42 const std::string& seed, | 53 const std::string& seed, |
| 43 const base::Value* value, | 54 const base::Value* value, |
| 44 const std::vector<std::string>& extra_components) { | 55 const std::vector<std::string>& extra_components) { |
| 45 static const size_t kSHA256DigestSize = 32; | 56 std::string message = JoinString(extra_components, "") + ValueAsString(value); |
| 57 return GetHMACHex(seed, message); | |
| 58 } | |
| 46 | 59 |
| 47 std::string message = JoinString(extra_components, "") + ValueAsString(value); | 60 // Calculate a hash using a deprecated hash algorithm. For validating old |
| 61 // hashes during migration. | |
| 62 std::string CalculateLegacyHash( | |
| 63 const std::string& seed, const base::Value* value) { | |
| 64 return CalculateFromValueAndComponents( | |
| 65 seed, value, std::vector<std::string>()); | |
| 66 } | |
| 48 | 67 |
| 49 crypto::HMAC hmac(crypto::HMAC::SHA256); | 68 // Generates a device ID based on the input device ID. The derived device ID has |
| 50 unsigned char digest[kSHA256DigestSize]; | 69 // 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))) { | 70 // consistent with previous implementations. |
| 52 NOTREACHED(); | 71 std::string GenerateDeviceIdLikePrefMetricsServiceDid( |
| 72 const std::string& original_device_id) { | |
| 73 if (original_device_id.empty()) | |
| 53 return std::string(); | 74 return std::string(); |
| 54 } | 75 return StringToLowerASCII( |
| 55 | 76 GetHMACHex(original_device_id, "PrefMetricsService")); |
| 56 return base::HexEncode(digest, arraysize(digest)); | |
| 57 } | 77 } |
| 58 | 78 |
| 59 } // namespace | 79 } // namespace |
| 60 | 80 |
| 61 PrefHashCalculator::PrefHashCalculator(const std::string& seed, | 81 PrefHashCalculator::PrefHashCalculator(const std::string& seed, |
| 62 const std::string& device_id) | 82 const std::string& device_id) |
| 63 : seed_(seed), device_id_(device_id) {} | 83 : seed_(seed), |
| 84 device_id_(GenerateDeviceIdLikePrefMetricsServiceDid(device_id)) {} | |
| 64 | 85 |
| 65 std::string PrefHashCalculator::Calculate(const std::string& path, | 86 std::string PrefHashCalculator::Calculate(const std::string& path, |
| 66 const base::Value* value) const { | 87 const base::Value* value) const { |
| 67 std::vector<std::string> components; | 88 std::vector<std::string> components; |
| 89 | |
| 68 if (!device_id_.empty()) | 90 if (!device_id_.empty()) |
| 69 components.push_back(device_id_); | 91 components.push_back(device_id_); |
| 70 components.push_back(path); | 92 components.push_back(path); |
| 71 return CalculateFromValueAndComponents(seed_, value, components); | 93 return CalculateFromValueAndComponents(seed_, value, components); |
| 72 } | 94 } |
| 73 | 95 |
| 74 PrefHashCalculator::ValidationResult PrefHashCalculator::Validate( | 96 PrefHashCalculator::ValidationResult PrefHashCalculator::Validate( |
| 75 const std::string& path, | 97 const std::string& path, |
| 76 const base::Value* value, | 98 const base::Value* value, |
| 77 const std::string& hash) const { | 99 const std::string& hash) const { |
| 78 if (hash == Calculate(path, value)) | 100 if (hash == Calculate(path, value)) |
| 79 return VALID; | 101 return VALID; |
| 80 if (hash == CalculateLegacyHash(path, value)) | 102 if (hash == CalculateLegacyHash(seed_, value)) |
| 81 return VALID_LEGACY; | 103 return VALID_LEGACY; |
| 82 return INVALID; | 104 return INVALID; |
| 83 } | 105 } |
| 84 | |
| 85 std::string PrefHashCalculator::CalculateLegacyHash( | |
| 86 const std::string& path, const base::Value* value) const { | |
|
gab
2014/01/07 19:18:07
Why move this? The move doesn't appear to be relat
| |
| 87 return CalculateFromValueAndComponents(seed_, | |
| 88 value, | |
| 89 std::vector<std::string>()); | |
| 90 } | |
| OLD | NEW |