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/signin/local_auth.h" | 5 #include "chrome/browser/signin/local_auth.h" |
6 | 6 |
7 #include "base/base64.h" | 7 #include "base/base64.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
11 #include "base/prefs/pref_service.h" | 11 #include "base/prefs/pref_service.h" |
12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
13 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
14 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
15 #include "chrome/browser/profiles/profile_manager.h" | 15 #include "chrome/browser/profiles/profile_manager.h" |
16 #include "chrome/common/pref_names.h" | 16 #include "chrome/common/pref_names.h" |
17 #include "components/os_crypt/os_crypt.h" | 17 #include "components/os_crypt/os_crypt.h" |
18 #include "components/pref_registry/pref_registry_syncable.h" | 18 #include "components/pref_registry/pref_registry_syncable.h" |
19 #include "crypto/random.h" | 19 #include "crypto/random.h" |
20 #include "crypto/secure_util.h" | 20 #include "crypto/secure_util.h" |
21 #include "crypto/symmetric_key.h" | 21 #include "crypto/symmetric_key.h" |
22 | 22 |
23 namespace { | 23 namespace { |
24 | 24 |
| 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; |
| 32 |
| 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) {} |
| 46 }; |
| 47 |
25 // WARNING: Changing these values will make it impossible to do off-line | 48 // WARNING: Changing these values will make it impossible to do off-line |
26 // authentication until the next successful on-line authentication. To change | 49 // authentication until the next successful on-line authentication. To change |
27 // these safely, change the "encoding" version below and make verification | 50 // these safely, add a new HashEncoding object below and increment |
28 // handle multiple values. | 51 // NUM_HASH_ENCODINGS. |
29 const char kHash1Encoding = '1'; | 52 const char kHash1Version = '1'; |
30 const unsigned kHash1Bits = 256; | 53 const unsigned kHash1Bits = 256; |
31 const unsigned kHash1Bytes = kHash1Bits / 8; | 54 const unsigned kHash1Bytes = kHash1Bits / 8; |
32 const unsigned kHash1IterationCount = 100000; | 55 const unsigned kHash1IterationCount = 100000; |
33 | 56 |
| 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; |
| 65 |
| 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) |
| 73 }; |
| 74 |
| 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]; |
| 79 } |
| 80 |
| 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); |
| 85 |
| 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); |
| 89 |
| 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; |
| 94 } |
| 95 |
34 std::string CreateSecurePasswordHash(const std::string& salt, | 96 std::string CreateSecurePasswordHash(const std::string& salt, |
35 const std::string& password, | 97 const std::string& password, |
36 char encoding) { | 98 const HashEncoding& encoding) { |
37 DCHECK_EQ(kHash1Bytes, salt.length()); | 99 DCHECK_EQ(encoding.hash_bytes, salt.length()); |
38 DCHECK_EQ(kHash1Encoding, encoding); // Currently support only one method. | |
39 | |
40 base::Time start_time = base::Time::Now(); | 100 base::Time start_time = base::Time::Now(); |
41 | 101 |
42 // Library call to create secure password hash as SymmetricKey (uses PBKDF2). | 102 // Library call to create secure password hash as SymmetricKey (uses PBKDF2). |
43 scoped_ptr<crypto::SymmetricKey> password_key( | 103 scoped_ptr<crypto::SymmetricKey> password_key( |
44 crypto::SymmetricKey::DeriveKeyFromPassword( | 104 crypto::SymmetricKey::DeriveKeyFromPassword( |
45 crypto::SymmetricKey::AES, | 105 crypto::SymmetricKey::AES, |
46 password, salt, | 106 password, salt, |
47 kHash1IterationCount, kHash1Bits)); | 107 encoding.iteration_count, encoding.hash_bits)); |
48 std::string password_hash; | 108 std::string password_hash; |
49 const bool success = password_key->GetRawKey(&password_hash); | 109 const bool success = password_key->GetRawKey(&password_hash); |
50 DCHECK(success); | 110 DCHECK(success); |
51 DCHECK_EQ(kHash1Bytes, password_hash.length()); | 111 DCHECK_EQ(encoding.hash_bytes, password_hash.length()); |
52 | 112 |
53 UMA_HISTOGRAM_TIMES("PasswordHash.CreateTime", | 113 UMA_HISTOGRAM_TIMES("PasswordHash.CreateTime", |
54 base::Time::Now() - start_time); | 114 base::Time::Now() - start_time); |
55 | 115 |
| 116 if (encoding.stored_bits) { |
| 117 password_hash = TruncateStringByBits(password_hash, encoding.stored_bits); |
| 118 DCHECK_EQ(encoding.stored_bytes, password_hash.length()); |
| 119 } |
| 120 DCHECK_EQ(encoding.stored_bytes ? encoding.stored_bytes : encoding.hash_bytes, |
| 121 password_hash.length()); |
56 return password_hash; | 122 return password_hash; |
57 } | 123 } |
58 | 124 |
59 std::string EncodePasswordHashRecord(const std::string& record, | 125 std::string EncodePasswordHashRecord(const std::string& record, |
60 char encoding) { | 126 const HashEncoding& encoding) { |
61 DCHECK_EQ(kHash1Encoding, encoding); // Currently support only one method. | |
62 | |
63 // Encrypt the hash using the OS account-password protection (if available). | 127 // Encrypt the hash using the OS account-password protection (if available). |
64 std::string encoded; | 128 std::string encoded; |
65 const bool success = OSCrypt::EncryptString(record, &encoded); | 129 const bool success = OSCrypt::EncryptString(record, &encoded); |
66 DCHECK(success); | 130 DCHECK(success); |
67 | 131 |
68 // Convert binary record to text for preference database. | 132 // Convert binary record to text for preference database. |
69 std::string encoded64; | 133 std::string encoded64; |
70 base::Base64Encode(encoded, &encoded64); | 134 base::Base64Encode(encoded, &encoded64); |
71 | 135 |
72 // Stuff the "encoding" value into the first byte. | 136 // Stuff the "encoding" value into the first byte. |
73 encoded64.insert(0, &encoding, sizeof(encoding)); | 137 encoded64.insert(0, &encoding.version, sizeof(encoding.version)); |
74 | 138 |
75 return encoded64; | 139 return encoded64; |
76 } | 140 } |
77 | 141 |
78 bool DecodePasswordHashRecord(const std::string& encoded, | 142 bool DecodePasswordHashRecord(const std::string& encoded, |
79 std::string* decoded, | 143 std::string* decoded, |
80 char* encoding) { | 144 char* encoding) { |
81 // Extract the "encoding" value from the first byte and validate. | 145 // Extract the "encoding" value from the first byte and validate. |
82 if (encoded.length() < 1) | 146 if (encoded.length() < 1) |
83 return false; | 147 return false; |
84 *encoding = encoded[0]; | 148 *encoding = encoded[0]; |
85 if (*encoding != kHash1Encoding) | 149 if (!GetEncodingForVersion(*encoding)) |
86 return false; | 150 return false; |
87 | 151 |
88 // Stored record is base64; convert to binary. | 152 // Stored record is base64; convert to binary. |
89 std::string unbase64; | 153 std::string unbase64; |
90 if (!base::Base64Decode(encoded.substr(1), &unbase64)) | 154 if (!base::Base64Decode(encoded.substr(1), &unbase64)) |
91 return false; | 155 return false; |
92 | 156 |
93 // Decrypt the record using the OS account-password protection (if available). | 157 // Decrypt the record using the OS account-password protection (if available). |
94 return OSCrypt::DecryptString(unbase64, decoded); | 158 return OSCrypt::DecryptString(unbase64, decoded); |
95 } | 159 } |
96 | 160 |
97 size_t GetProfileInfoIndexOfProfile(const Profile* profile) { | 161 size_t GetProfileInfoIndexOfProfile(const Profile* profile) { |
98 DCHECK(profile); | 162 DCHECK(profile); |
99 | 163 |
100 ProfileInfoCache& info = | 164 ProfileInfoCache& info = |
101 g_browser_process->profile_manager()->GetProfileInfoCache(); | 165 g_browser_process->profile_manager()->GetProfileInfoCache(); |
102 return info.GetIndexOfProfileWithPath(profile->GetPath()); | 166 return info.GetIndexOfProfileWithPath(profile->GetPath()); |
103 } | 167 } |
104 | 168 |
105 } // namespace | 169 } // namespace |
106 | 170 |
107 namespace chrome { | 171 std::string LocalAuth::TruncateStringByBits(const std::string& str, |
| 172 const size_t len_bits) { |
| 173 return ::TruncateStringByBits(str, len_bits); |
| 174 } |
108 | 175 |
109 void RegisterLocalAuthPrefs(user_prefs::PrefRegistrySyncable* registry) { | 176 void LocalAuth::RegisterLocalAuthPrefs( |
| 177 user_prefs::PrefRegistrySyncable* registry) { |
110 registry->RegisterStringPref( | 178 registry->RegisterStringPref( |
111 prefs::kGoogleServicesPasswordHash, | 179 prefs::kGoogleServicesPasswordHash, |
112 std::string(), | 180 std::string(), |
113 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 181 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
114 } | 182 } |
115 | 183 |
116 void SetLocalAuthCredentials(size_t info_index, | 184 void LocalAuth::SetLocalAuthCredentialsWithEncoding(size_t info_index, |
117 const std::string& password) { | 185 const std::string& password, |
118 if (info_index == std::string::npos) { | 186 char encoding_version) { |
119 NOTREACHED(); | 187 const HashEncoding& encoding = encodings[(encoding_version - '0') - 1]; |
120 return; | |
121 } | |
122 DCHECK(password.length()); | |
123 | 188 |
124 // Salt should be random data, as long as the hash length, and different with | 189 // Salt should be random data, as long as the hash length, and different with |
125 // every save. | 190 // every save. |
126 std::string salt_str; | 191 std::string salt_str; |
127 crypto::RandBytes(WriteInto(&salt_str, kHash1Bytes + 1), kHash1Bytes); | 192 crypto::RandBytes(WriteInto(&salt_str, encoding.hash_bytes + 1), |
128 DCHECK_EQ(kHash1Bytes, salt_str.length()); | 193 encoding.hash_bytes); |
129 | 194 |
130 // Perform secure hash of password for storage. | 195 // Perform secure hash of password for storage. |
131 std::string password_hash = CreateSecurePasswordHash( | 196 std::string password_hash = CreateSecurePasswordHash( |
132 salt_str, password, kHash1Encoding); | 197 salt_str, password, encoding); |
133 DCHECK_EQ(kHash1Bytes, password_hash.length()); | |
134 | 198 |
135 // Group all fields into a single record for storage; | 199 // Group all fields into a single record for storage; |
136 std::string record; | 200 std::string record; |
137 record.append(salt_str); | 201 record.append(salt_str); |
138 record.append(password_hash); | 202 record.append(password_hash); |
139 | 203 |
140 // Encode it and store it. | 204 // Encode it and store it. |
141 std::string encoded = EncodePasswordHashRecord(record, kHash1Encoding); | 205 std::string encoded = EncodePasswordHashRecord(record, encoding); |
142 ProfileInfoCache& info = | 206 ProfileInfoCache& info = |
143 g_browser_process->profile_manager()->GetProfileInfoCache(); | 207 g_browser_process->profile_manager()->GetProfileInfoCache(); |
144 info.SetLocalAuthCredentialsOfProfileAtIndex(info_index, encoded); | 208 info.SetLocalAuthCredentialsOfProfileAtIndex(info_index, encoded); |
145 } | 209 } |
146 | 210 |
147 void SetLocalAuthCredentials(const Profile* profile, | 211 void LocalAuth::SetLocalAuthCredentials(size_t info_index, |
148 const std::string& password) { | 212 const std::string& password) { |
| 213 if (info_index == std::string::npos) { |
| 214 NOTREACHED(); |
| 215 return; |
| 216 } |
| 217 DCHECK(password.length()); |
| 218 SetLocalAuthCredentialsWithEncoding( |
| 219 info_index, password, '0' + NUM_HASH_ENCODINGS); |
| 220 } |
| 221 |
| 222 void LocalAuth::SetLocalAuthCredentials(const Profile* profile, |
| 223 const std::string& password) { |
149 SetLocalAuthCredentials(GetProfileInfoIndexOfProfile(profile), password); | 224 SetLocalAuthCredentials(GetProfileInfoIndexOfProfile(profile), password); |
150 } | 225 } |
151 | 226 |
152 bool ValidateLocalAuthCredentials(size_t info_index, | 227 bool LocalAuth::ValidateLocalAuthCredentials(size_t info_index, |
153 const std::string& password) { | 228 const std::string& password) { |
154 if (info_index == std::string::npos) { | 229 if (info_index == std::string::npos) { |
155 NOTREACHED(); | 230 NOTREACHED(); |
156 return false; | 231 return false; |
157 } | 232 } |
158 | 233 |
159 std::string record; | 234 std::string record; |
160 char encoding; | 235 char encoding; |
161 | 236 |
162 ProfileInfoCache& info = | 237 ProfileInfoCache& info = |
163 g_browser_process->profile_manager()->GetProfileInfoCache(); | 238 g_browser_process->profile_manager()->GetProfileInfoCache(); |
164 | 239 |
165 std::string encodedhash = | 240 std::string encodedhash = |
166 info.GetLocalAuthCredentialsOfProfileAtIndex(info_index); | 241 info.GetLocalAuthCredentialsOfProfileAtIndex(info_index); |
167 if (encodedhash.length() == 0 && password.length() == 0) | 242 if (encodedhash.length() == 0 && password.length() == 0) |
168 return true; | 243 return true; |
169 if (!DecodePasswordHashRecord(encodedhash, &record, &encoding)) | 244 if (!DecodePasswordHashRecord(encodedhash, &record, &encoding)) |
170 return false; | 245 return false; |
171 | 246 |
172 std::string password_hash; | 247 std::string password_hash; |
173 const char* password_saved; | 248 const char* password_saved; |
174 const char* password_check; | 249 const char* password_check; |
175 size_t password_length; | 250 size_t password_length; |
176 | 251 |
177 if (encoding == '1') { | 252 const HashEncoding* hash_encoding = GetEncodingForVersion(encoding); |
178 // Validate correct length; extract salt and password hash. | 253 if (!hash_encoding) { |
179 if (record.length() != 2 * kHash1Bytes) | 254 // Unknown encoding. |
180 return false; | |
181 std::string salt_str(record.data(), kHash1Bytes); | |
182 password_saved = record.data() + kHash1Bytes; | |
183 password_hash = CreateSecurePasswordHash(salt_str, password, encoding); | |
184 password_check = password_hash.data(); | |
185 password_length = kHash1Bytes; | |
186 } else { | |
187 // unknown encoding | |
188 return false; | 255 return false; |
189 } | 256 } |
190 | 257 |
191 return crypto::SecureMemEqual(password_saved, password_check, | 258 // Extract salt. |
192 password_length); | 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(); |
| 265 |
| 266 bool passwords_match = crypto::SecureMemEqual( |
| 267 password_saved, password_check, password_length); |
| 268 |
| 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; |
193 } | 273 } |
194 | 274 |
195 bool ValidateLocalAuthCredentials(const Profile* profile, | 275 bool LocalAuth::ValidateLocalAuthCredentials(const Profile* profile, |
196 const std::string& password) { | 276 const std::string& password) { |
197 return ValidateLocalAuthCredentials(GetProfileInfoIndexOfProfile(profile), | 277 return ValidateLocalAuthCredentials(GetProfileInfoIndexOfProfile(profile), |
198 password); | 278 password); |
199 } | 279 } |
200 | |
201 } // namespace chrome | |
OLD | NEW |