Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / signin / local_auth.cc
blob6846784a936aeeac55ce1285eb98a224e7a69197
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(prefs::kGoogleServicesPasswordHash,
179 std::string());
182 void LocalAuth::SetLocalAuthCredentialsWithEncoding(size_t info_index,
183 const std::string& password,
184 char encoding_version) {
185 const HashEncoding& encoding = encodings[(encoding_version - '0') - 1];
187 // Salt should be random data, as long as the hash length, and different with
188 // every save.
189 std::string salt_str;
190 crypto::RandBytes(base::WriteInto(&salt_str, encoding.hash_bytes + 1),
191 encoding.hash_bytes);
193 // Perform secure hash of password for storage.
194 std::string password_hash = CreateSecurePasswordHash(
195 salt_str, password, encoding);
197 // Group all fields into a single record for storage;
198 std::string record;
199 record.append(salt_str);
200 record.append(password_hash);
202 // Encode it and store it.
203 std::string encoded = EncodePasswordHashRecord(record, encoding);
204 ProfileInfoCache& info =
205 g_browser_process->profile_manager()->GetProfileInfoCache();
206 info.SetLocalAuthCredentialsOfProfileAtIndex(info_index, encoded);
209 void LocalAuth::SetLocalAuthCredentials(size_t info_index,
210 const std::string& password) {
211 if (info_index == std::string::npos) {
212 NOTREACHED();
213 return;
215 DCHECK(password.length());
216 SetLocalAuthCredentialsWithEncoding(
217 info_index, password, '0' + NUM_HASH_ENCODINGS);
220 void LocalAuth::SetLocalAuthCredentials(const Profile* profile,
221 const std::string& password) {
222 SetLocalAuthCredentials(GetProfileInfoIndexOfProfile(profile), password);
225 bool LocalAuth::ValidateLocalAuthCredentials(size_t info_index,
226 const std::string& password) {
227 if (info_index == std::string::npos) {
228 NOTREACHED();
229 return false;
232 std::string record;
233 char encoding;
235 ProfileInfoCache& info =
236 g_browser_process->profile_manager()->GetProfileInfoCache();
238 std::string encodedhash =
239 info.GetLocalAuthCredentialsOfProfileAtIndex(info_index);
240 if (encodedhash.length() == 0 && password.length() == 0)
241 return true;
242 if (!DecodePasswordHashRecord(encodedhash, &record, &encoding))
243 return false;
245 std::string password_hash;
246 const char* password_saved;
247 const char* password_check;
248 size_t password_length;
250 const HashEncoding* hash_encoding = GetEncodingForVersion(encoding);
251 if (!hash_encoding) {
252 // Unknown encoding.
253 return false;
256 // Extract salt.
257 std::string salt_str(record.data(), hash_encoding->hash_bytes);
258 // Extract password.
259 password_saved = record.data() + hash_encoding->hash_bytes;
260 password_hash = CreateSecurePasswordHash(salt_str, password, *hash_encoding);
261 password_length = hash_encoding->stored_bytes;
262 password_check = password_hash.data();
264 bool passwords_match = crypto::SecureMemEqual(
265 password_saved, password_check, password_length);
267 // Update the stored credentials to the latest encoding if necessary.
268 if (passwords_match && (hash_encoding->version - '0') != NUM_HASH_ENCODINGS)
269 SetLocalAuthCredentials(info_index, password);
270 return passwords_match;
273 bool LocalAuth::ValidateLocalAuthCredentials(const Profile* profile,
274 const std::string& password) {
275 return ValidateLocalAuthCredentials(GetProfileInfoIndexOfProfile(profile),
276 password);