base::Time multiplicative operator overloading
[chromium-blink-merge.git] / chrome / browser / signin / local_auth.cc
blob9b20c3228769ff2066b178e335e551d31a43061e
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 "chrome/browser/signin/local_auth.h"
7 #include "base/base64.h"
8 #include "base/logging.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/metrics/histogram.h"
11 #include "base/prefs/pref_service.h"
12 #include "base/strings/string_util.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/profiles/profile_manager.h"
16 #include "chrome/common/pref_names.h"
17 #include "components/os_crypt/os_crypt.h"
18 #include "components/pref_registry/pref_registry_syncable.h"
19 #include "crypto/random.h"
20 #include "crypto/secure_util.h"
21 #include "crypto/symmetric_key.h"
23 namespace {
25 struct HashEncoding {
26 char version;
27 unsigned hash_bits;
28 unsigned hash_bytes;
29 unsigned iteration_count;
30 unsigned stored_bits;
31 unsigned stored_bytes;
33 public:
34 HashEncoding(char version,
35 unsigned hash_bits,
36 unsigned hash_bytes,
37 unsigned iteration_count,
38 unsigned stored_bits,
39 unsigned stored_bytes) :
40 version(version),
41 hash_bits(hash_bits),
42 hash_bytes(hash_bytes),
43 iteration_count(iteration_count),
44 stored_bits(stored_bits),
45 stored_bytes(stored_bytes) {}
48 // WARNING: Changing these values will make it impossible to do off-line
49 // authentication until the next successful on-line authentication. To change
50 // these safely, add a new HashEncoding object below and increment
51 // NUM_HASH_ENCODINGS.
52 const char kHash1Version = '1';
53 const unsigned kHash1Bits = 256;
54 const unsigned kHash1Bytes = kHash1Bits / 8;
55 const unsigned kHash1IterationCount = 100000;
57 // Store 13 bits to provide pin-like security (8192 possible values), without
58 // providing a complete oracle for the user's GAIA password.
59 const char kHash2Version = '2';
60 const unsigned kHash2Bits = 256;
61 const unsigned kHash2Bytes = kHash2Bits / 8;
62 const unsigned kHash2IterationCount = 100000;
63 const unsigned kHash2StoredBits = 13;
64 const unsigned kHash2StoredBytes = (kHash2StoredBits + 7) / 8;
66 const int NUM_HASH_ENCODINGS = 2;
67 HashEncoding encodings[NUM_HASH_ENCODINGS] = {
68 HashEncoding(
69 kHash1Version, kHash1Bits, kHash1Bytes, kHash1IterationCount, 0, 0),
70 HashEncoding(
71 kHash2Version, kHash2Bits, kHash2Bytes, kHash2IterationCount,
72 kHash2StoredBits, kHash2StoredBytes)
75 const HashEncoding* GetEncodingForVersion(char version) {
76 // Note that versions are 1-indexed.
77 DCHECK(version > '0' && version <= ('0' + NUM_HASH_ENCODINGS));
78 return &encodings[(version - '0') - 1];
81 std::string TruncateStringByBits(const std::string& str,
82 const size_t len_bits) {
83 if (len_bits % 8 == 0)
84 return str.substr(0, len_bits / 8);
86 // The initial truncation copies whole bytes
87 int number_bytes = (len_bits + 7) / 8;
88 std::string truncated_string = str.substr(0, number_bytes);
90 // Keep the prescribed number of bits from the last byte.
91 unsigned last_char_bitmask = (1 << (len_bits % 8)) - 1;
92 truncated_string[number_bytes - 1] &= last_char_bitmask;
93 return truncated_string;
96 std::string CreateSecurePasswordHash(const std::string& salt,
97 const std::string& password,
98 const HashEncoding& encoding) {
99 DCHECK_EQ(encoding.hash_bytes, salt.length());
100 base::Time start_time = base::Time::Now();
102 // Library call to create secure password hash as SymmetricKey (uses PBKDF2).
103 scoped_ptr<crypto::SymmetricKey> password_key(
104 crypto::SymmetricKey::DeriveKeyFromPassword(
105 crypto::SymmetricKey::AES,
106 password, salt,
107 encoding.iteration_count, encoding.hash_bits));
108 std::string password_hash;
109 const bool success = password_key->GetRawKey(&password_hash);
110 DCHECK(success);
111 DCHECK_EQ(encoding.hash_bytes, password_hash.length());
113 UMA_HISTOGRAM_TIMES("PasswordHash.CreateTime",
114 base::Time::Now() - start_time);
116 if (encoding.stored_bits) {
117 password_hash = TruncateStringByBits(password_hash, encoding.stored_bits);
118 DCHECK_EQ(encoding.stored_bytes, password_hash.length());
120 DCHECK_EQ(encoding.stored_bytes ? encoding.stored_bytes : encoding.hash_bytes,
121 password_hash.length());
122 return password_hash;
125 std::string EncodePasswordHashRecord(const std::string& record,
126 const HashEncoding& encoding) {
127 // Encrypt the hash using the OS account-password protection (if available).
128 std::string encoded;
129 const bool success = OSCrypt::EncryptString(record, &encoded);
130 DCHECK(success);
132 // Convert binary record to text for preference database.
133 std::string encoded64;
134 base::Base64Encode(encoded, &encoded64);
136 // Stuff the "encoding" value into the first byte.
137 encoded64.insert(0, &encoding.version, sizeof(encoding.version));
139 return encoded64;
142 bool DecodePasswordHashRecord(const std::string& encoded,
143 std::string* decoded,
144 char* encoding) {
145 // Extract the "encoding" value from the first byte and validate.
146 if (encoded.length() < 1)
147 return false;
148 *encoding = encoded[0];
149 if (!GetEncodingForVersion(*encoding))
150 return false;
152 // Stored record is base64; convert to binary.
153 std::string unbase64;
154 if (!base::Base64Decode(encoded.substr(1), &unbase64))
155 return false;
157 // Decrypt the record using the OS account-password protection (if available).
158 return OSCrypt::DecryptString(unbase64, decoded);
161 size_t GetProfileInfoIndexOfProfile(const Profile* profile) {
162 DCHECK(profile);
164 ProfileInfoCache& info =
165 g_browser_process->profile_manager()->GetProfileInfoCache();
166 return info.GetIndexOfProfileWithPath(profile->GetPath());
169 } // namespace
171 std::string LocalAuth::TruncateStringByBits(const std::string& str,
172 const size_t len_bits) {
173 return ::TruncateStringByBits(str, len_bits);
176 void LocalAuth::RegisterLocalAuthPrefs(
177 user_prefs::PrefRegistrySyncable* registry) {
178 registry->RegisterStringPref(
179 prefs::kGoogleServicesPasswordHash,
180 std::string(),
181 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
184 void LocalAuth::SetLocalAuthCredentialsWithEncoding(size_t info_index,
185 const std::string& password,
186 char encoding_version) {
187 const HashEncoding& encoding = encodings[(encoding_version - '0') - 1];
189 // Salt should be random data, as long as the hash length, and different with
190 // every save.
191 std::string salt_str;
192 crypto::RandBytes(WriteInto(&salt_str, encoding.hash_bytes + 1),
193 encoding.hash_bytes);
195 // Perform secure hash of password for storage.
196 std::string password_hash = CreateSecurePasswordHash(
197 salt_str, password, encoding);
199 // Group all fields into a single record for storage;
200 std::string record;
201 record.append(salt_str);
202 record.append(password_hash);
204 // Encode it and store it.
205 std::string encoded = EncodePasswordHashRecord(record, encoding);
206 ProfileInfoCache& info =
207 g_browser_process->profile_manager()->GetProfileInfoCache();
208 info.SetLocalAuthCredentialsOfProfileAtIndex(info_index, encoded);
211 void LocalAuth::SetLocalAuthCredentials(size_t info_index,
212 const std::string& password) {
213 if (info_index == std::string::npos) {
214 NOTREACHED();
215 return;
217 DCHECK(password.length());
218 SetLocalAuthCredentialsWithEncoding(
219 info_index, password, '0' + NUM_HASH_ENCODINGS);
222 void LocalAuth::SetLocalAuthCredentials(const Profile* profile,
223 const std::string& password) {
224 SetLocalAuthCredentials(GetProfileInfoIndexOfProfile(profile), password);
227 bool LocalAuth::ValidateLocalAuthCredentials(size_t info_index,
228 const std::string& password) {
229 if (info_index == std::string::npos) {
230 NOTREACHED();
231 return false;
234 std::string record;
235 char encoding;
237 ProfileInfoCache& info =
238 g_browser_process->profile_manager()->GetProfileInfoCache();
240 std::string encodedhash =
241 info.GetLocalAuthCredentialsOfProfileAtIndex(info_index);
242 if (encodedhash.length() == 0 && password.length() == 0)
243 return true;
244 if (!DecodePasswordHashRecord(encodedhash, &record, &encoding))
245 return false;
247 std::string password_hash;
248 const char* password_saved;
249 const char* password_check;
250 size_t password_length;
252 const HashEncoding* hash_encoding = GetEncodingForVersion(encoding);
253 if (!hash_encoding) {
254 // Unknown encoding.
255 return false;
258 // Extract salt.
259 std::string salt_str(record.data(), hash_encoding->hash_bytes);
260 // Extract password.
261 password_saved = record.data() + hash_encoding->hash_bytes;
262 password_hash = CreateSecurePasswordHash(salt_str, password, *hash_encoding);
263 password_length = hash_encoding->stored_bytes;
264 password_check = password_hash.data();
266 bool passwords_match = crypto::SecureMemEqual(
267 password_saved, password_check, password_length);
269 // Update the stored credentials to the latest encoding if necessary.
270 if (passwords_match && (hash_encoding->version - '0') != NUM_HASH_ENCODINGS)
271 SetLocalAuthCredentials(info_index, password);
272 return passwords_match;
275 bool LocalAuth::ValidateLocalAuthCredentials(const Profile* profile,
276 const std::string& password) {
277 return ValidateLocalAuthCredentials(GetProfileInfoIndexOfProfile(profile),
278 password);