Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(107)

Side by Side Diff: chrome/browser/prefs/pref_hash_calculator.cc

Issue 110523006: Fix the hash generation algorithm to be consistent with prior implementation. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Correct string constants. Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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());
23 if (!hmac.Init(key) || !hmac.Sign(message, &digest[0], digest.size())) {
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);
48 60
49 crypto::HMAC hmac(crypto::HMAC::SHA256); 61 // Generates a device ID based on the input device ID. The derived device ID has
50 unsigned char digest[kSHA256DigestSize]; 62 // 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))) { 63 // consistent with previous implementations.
52 NOTREACHED(); 64 std::string GenerateDeviceIdLikePrefMetricsServiceDid(
65 const std::string& original_device_id) {
66 if (original_device_id.empty())
53 return std::string(); 67 return std::string();
54 } 68 return StringToLowerASCII(
55 69 GetHMACHex(original_device_id, "PrefMetricsService"));
56 return base::HexEncode(digest, arraysize(digest));
57 } 70 }
58 71
59 } // namespace 72 } // namespace
60 73
61 PrefHashCalculator::PrefHashCalculator(const std::string& seed, 74 PrefHashCalculator::PrefHashCalculator(const std::string& seed,
62 const std::string& device_id) 75 const std::string& device_id)
63 : seed_(seed), device_id_(device_id) {} 76 : seed_(seed),
77 device_id_(GenerateDeviceIdLikePrefMetricsServiceDid(device_id)) {}
64 78
65 std::string PrefHashCalculator::Calculate(const std::string& path, 79 std::string PrefHashCalculator::Calculate(const std::string& path,
66 const base::Value* value) const { 80 const base::Value* value) const {
67 std::vector<std::string> components; 81 std::vector<std::string> components;
68 if (!device_id_.empty()) 82 if (!device_id_.empty())
69 components.push_back(device_id_); 83 components.push_back(device_id_);
70 components.push_back(path); 84 components.push_back(path);
71 return CalculateFromValueAndComponents(seed_, value, components); 85 return CalculateFromValueAndComponents(seed_, value, components);
72 } 86 }
73 87
74 PrefHashCalculator::ValidationResult PrefHashCalculator::Validate( 88 PrefHashCalculator::ValidationResult PrefHashCalculator::Validate(
75 const std::string& path, 89 const std::string& path,
76 const base::Value* value, 90 const base::Value* value,
77 const std::string& hash) const { 91 const std::string& hash) const {
78 if (hash == Calculate(path, value)) 92 if (hash == Calculate(path, value))
Ryan Sleevi 2014/01/09 20:51:14 SECURITY BUG: Use HMAC::Verify() for this. This is
erikwright (departed) 2014/01/09 21:45:57 Done.
79 return VALID; 93 return VALID;
80 if (hash == CalculateLegacyHash(path, value)) 94 if (hash == CalculateLegacyHash(path, value))
81 return VALID_LEGACY; 95 return VALID_LEGACY;
82 return INVALID; 96 return INVALID;
83 } 97 }
84 98
85 std::string PrefHashCalculator::CalculateLegacyHash( 99 std::string PrefHashCalculator::CalculateLegacyHash(
86 const std::string& path, const base::Value* value) const { 100 const std::string& path, const base::Value* value) const {
87 return CalculateFromValueAndComponents(seed_, 101 return CalculateFromValueAndComponents(seed_,
88 value, 102 value,
89 std::vector<std::string>()); 103 std::vector<std::string>());
90 } 104 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698