Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / prefs / pref_hash_calculator.cc
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.
4
5 #include "chrome/browser/prefs/pref_hash_calculator.h"
6
7 #include <vector>
8
9 #include "base/json/json_string_value_serializer.h"
10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.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"
16
17 namespace {
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
45 // Renders |value| as a string. |value| may be NULL, in which case the result
46 // is an empty string. This method can be expensive and its result should be
47 // re-used rather than recomputed where possible.
48 std::string ValueAsString(const base::Value* value) {
49   // Dictionary values may contain empty lists and sub-dictionaries. Make a
50   // deep copy with those removed to make the hash more stable.
51   const base::DictionaryValue* dict_value;
52   scoped_ptr<base::DictionaryValue> canonical_dict_value;
53   if (value && value->GetAsDictionary(&dict_value)) {
54     canonical_dict_value.reset(dict_value->DeepCopyWithoutEmptyChildren());
55     value = canonical_dict_value.get();
56   }
57
58   std::string value_as_string;
59   if (value) {
60     JSONStringValueSerializer serializer(&value_as_string);
61     serializer.Serialize(*value);
62   }
63
64   return value_as_string;
65 }
66
67 // Common helper for all hash algorithms.
68 std::string GetMessageFromValueAndComponents(
69     const std::string& value_as_string,
70     const std::vector<std::string>& extra_components) {
71   return JoinString(extra_components, "") + value_as_string;
72 }
73
74
75 // Generates a device ID based on the input device ID. The derived device ID has
76 // no useful properties beyond those of the input device ID except that it is
77 // consistent with previous implementations.
78 std::string GenerateDeviceIdLikePrefMetricsServiceDid(
79     const std::string& original_device_id) {
80   if (original_device_id.empty())
81     return std::string();
82   return StringToLowerASCII(
83       GetDigestString(original_device_id, "PrefMetricsService"));
84 }
85
86 // Verifies a hash using a deprecated hash algorithm. For validating old
87 // hashes during migration.
88 bool VerifyLegacyHash(const std::string& seed,
89                       const std::string& value_as_string,
90                       const std::string& digest_string) {
91   return VerifyDigestString(
92       seed,
93       GetMessageFromValueAndComponents(value_as_string,
94                                        std::vector<std::string>()),
95       digest_string);
96 }
97
98 }  // namespace
99
100 PrefHashCalculator::PrefHashCalculator(const std::string& seed,
101                                        const std::string& device_id)
102     : seed_(seed),
103       device_id_(GenerateDeviceIdLikePrefMetricsServiceDid(device_id)) {}
104
105 std::string PrefHashCalculator::Calculate(const std::string& path,
106                                           const base::Value* value) const {
107   return GetDigestString(seed_, GetMessage(path, ValueAsString(value)));
108 }
109
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(path, value_as_string),
116                          digest_string)) {
117     return VALID;
118   }
119   if (VerifyLegacyHash(seed_, value_as_string, digest_string))
120     return VALID_LEGACY;
121   return INVALID;
122 }
123
124 std::string PrefHashCalculator::GetMessage(
125     const std::string& path,
126     const std::string& value_as_string) const {
127   std::vector<std::string> components;
128   if (!device_id_.empty())
129     components.push_back(device_id_);
130   components.push_back(path);
131   return GetMessageFromValueAndComponents(value_as_string, components);
132 }