Chromium Code Reviews| 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 { | |
|
bcwhite
2015/01/28 15:34:12
I'm not sure it's worth the trouble to create an a
Mike Lerman
2015/01/28 15:38:38
We'll always need to support older versions. I may
bcwhite
2015/01/28 16:01:35
Not true. The locally saved hash is required only
Mike Lerman
2015/01/28 16:45:55
The way an auditor would reference each encoding's
| |
| 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 }; | |
| 25 // WARNING: Changing these values will make it impossible to do off-line | 47 // WARNING: Changing these values will make it impossible to do off-line |
| 26 // authentication until the next successful on-line authentication. To change | 48 // authentication until the next successful on-line authentication. To change |
| 27 // these safely, change the "encoding" version below and make verification | 49 // these safely, add a new HashEncoding object below and increment |
| 28 // handle multiple values. | 50 // NUM_HASH_ENCODINGS. |
| 29 const char kHash1Encoding = '1'; | 51 const char kHash1Version = '1'; |
| 30 const unsigned kHash1Bits = 256; | 52 const unsigned kHash1Bits = 256; |
| 31 const unsigned kHash1Bytes = kHash1Bits / 8; | 53 const unsigned kHash1Bytes = kHash1Bits / 8; |
| 32 const unsigned kHash1IterationCount = 100000; | 54 const unsigned kHash1IterationCount = 100000; |
| 33 | 55 |
| 56 // Store 13 bits to provide pin-like security (8192 possible values), without | |
| 57 // providing a complete oracle for the user's GAIA password. | |
| 58 const char kHash2Version = '2'; | |
| 59 const unsigned kHash2Bits = 256; | |
| 60 const unsigned kHash2Bytes = kHash2Bits / 8; | |
| 61 const unsigned kHash2IterationCount = 100000; | |
| 62 const unsigned kHash2StoredBits = 13; | |
| 63 const unsigned kHash2StoredBytes = (kHash2StoredBits + 7) / 8; | |
| 64 | |
| 65 const int NUM_HASH_ENCODINGS = 2; | |
| 66 HashEncoding encodings[NUM_HASH_ENCODINGS] = { | |
| 67 HashEncoding( | |
| 68 kHash1Version, kHash1Bits, kHash1Bytes, kHash1IterationCount, 0, 0), | |
| 69 HashEncoding( | |
| 70 kHash2Version, kHash2Bits, kHash2Bytes, kHash2IterationCount, | |
| 71 kHash2StoredBits, kHash2StoredBytes) | |
| 72 }; | |
| 73 | |
| 74 const HashEncoding* GetEncodingForVersion(char version) { | |
| 75 // Note that versions are 1-indexed. | |
| 76 DCHECK(version > '0' && version <= ('0' + NUM_HASH_ENCODINGS)); | |
| 77 return &encodings[(version - '0') - 1]; | |
| 78 } | |
| 79 | |
| 34 std::string CreateSecurePasswordHash(const std::string& salt, | 80 std::string CreateSecurePasswordHash(const std::string& salt, |
| 35 const std::string& password, | 81 const std::string& password, |
| 36 char encoding) { | 82 const HashEncoding& encoding) { |
| 37 DCHECK_EQ(kHash1Bytes, salt.length()); | 83 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(); | 84 base::Time start_time = base::Time::Now(); |
| 41 | 85 |
| 42 // Library call to create secure password hash as SymmetricKey (uses PBKDF2). | 86 // Library call to create secure password hash as SymmetricKey (uses PBKDF2). |
| 43 scoped_ptr<crypto::SymmetricKey> password_key( | 87 scoped_ptr<crypto::SymmetricKey> password_key( |
| 44 crypto::SymmetricKey::DeriveKeyFromPassword( | 88 crypto::SymmetricKey::DeriveKeyFromPassword( |
| 45 crypto::SymmetricKey::AES, | 89 crypto::SymmetricKey::AES, |
| 46 password, salt, | 90 password, salt, |
| 47 kHash1IterationCount, kHash1Bits)); | 91 encoding.iteration_count, encoding.hash_bits)); |
| 48 std::string password_hash; | 92 std::string password_hash; |
| 49 const bool success = password_key->GetRawKey(&password_hash); | 93 const bool success = password_key->GetRawKey(&password_hash); |
| 50 DCHECK(success); | 94 DCHECK(success); |
| 51 DCHECK_EQ(kHash1Bytes, password_hash.length()); | 95 DCHECK_EQ(encoding.hash_bytes, password_hash.length()); |
| 52 | 96 |
| 53 UMA_HISTOGRAM_TIMES("PasswordHash.CreateTime", | 97 UMA_HISTOGRAM_TIMES("PasswordHash.CreateTime", |
| 54 base::Time::Now() - start_time); | 98 base::Time::Now() - start_time); |
| 55 | 99 |
| 100 if (encoding.stored_bits) { | |
| 101 password_hash = chrome::TruncateStringByBits( | |
| 102 password_hash, encoding.stored_bits); | |
| 103 DCHECK_EQ(encoding.stored_bytes, password_hash.length()); | |
| 104 } | |
| 105 DCHECK_EQ(encoding.stored_bytes ? encoding.stored_bytes : encoding.hash_bytes, | |
| 106 password_hash.length()); | |
| 56 return password_hash; | 107 return password_hash; |
| 57 } | 108 } |
| 58 | 109 |
| 59 std::string EncodePasswordHashRecord(const std::string& record, | 110 std::string EncodePasswordHashRecord(const std::string& record, |
| 60 char encoding) { | 111 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). | 112 // Encrypt the hash using the OS account-password protection (if available). |
| 64 std::string encoded; | 113 std::string encoded; |
| 65 const bool success = OSCrypt::EncryptString(record, &encoded); | 114 const bool success = OSCrypt::EncryptString(record, &encoded); |
| 66 DCHECK(success); | 115 DCHECK(success); |
| 67 | 116 |
| 68 // Convert binary record to text for preference database. | 117 // Convert binary record to text for preference database. |
| 69 std::string encoded64; | 118 std::string encoded64; |
| 70 base::Base64Encode(encoded, &encoded64); | 119 base::Base64Encode(encoded, &encoded64); |
| 71 | 120 |
| 72 // Stuff the "encoding" value into the first byte. | 121 // Stuff the "encoding" value into the first byte. |
| 73 encoded64.insert(0, &encoding, sizeof(encoding)); | 122 encoded64.insert(0, &encoding.version, sizeof(encoding.version)); |
| 74 | 123 |
| 75 return encoded64; | 124 return encoded64; |
| 76 } | 125 } |
| 77 | 126 |
| 78 bool DecodePasswordHashRecord(const std::string& encoded, | 127 bool DecodePasswordHashRecord(const std::string& encoded, |
| 79 std::string* decoded, | 128 std::string* decoded, |
| 80 char* encoding) { | 129 char* encoding) { |
| 81 // Extract the "encoding" value from the first byte and validate. | 130 // Extract the "encoding" value from the first byte and validate. |
| 82 if (encoded.length() < 1) | 131 if (encoded.length() < 1) |
| 83 return false; | 132 return false; |
| 84 *encoding = encoded[0]; | 133 *encoding = encoded[0]; |
| 85 if (*encoding != kHash1Encoding) | 134 if (!GetEncodingForVersion(*encoding)) |
| 86 return false; | 135 return false; |
| 87 | 136 |
| 88 // Stored record is base64; convert to binary. | 137 // Stored record is base64; convert to binary. |
| 89 std::string unbase64; | 138 std::string unbase64; |
| 90 if (!base::Base64Decode(encoded.substr(1), &unbase64)) | 139 if (!base::Base64Decode(encoded.substr(1), &unbase64)) |
| 91 return false; | 140 return false; |
| 92 | 141 |
| 93 // Decrypt the record using the OS account-password protection (if available). | 142 // Decrypt the record using the OS account-password protection (if available). |
| 94 return OSCrypt::DecryptString(unbase64, decoded); | 143 return OSCrypt::DecryptString(unbase64, decoded); |
| 95 } | 144 } |
| 96 | 145 |
| 97 size_t GetProfileInfoIndexOfProfile(const Profile* profile) { | 146 size_t GetProfileInfoIndexOfProfile(const Profile* profile) { |
| 98 DCHECK(profile); | 147 DCHECK(profile); |
| 99 | 148 |
| 100 ProfileInfoCache& info = | 149 ProfileInfoCache& info = |
| 101 g_browser_process->profile_manager()->GetProfileInfoCache(); | 150 g_browser_process->profile_manager()->GetProfileInfoCache(); |
| 102 return info.GetIndexOfProfileWithPath(profile->GetPath()); | 151 return info.GetIndexOfProfileWithPath(profile->GetPath()); |
| 103 } | 152 } |
| 104 | 153 |
| 105 } // namespace | 154 } // namespace |
| 106 | 155 |
| 107 namespace chrome { | 156 namespace chrome { |
| 108 | 157 |
| 158 std::string TruncateStringByBits(const std::string& str, | |
| 159 const size_t len_bits) { | |
| 160 if (len_bits % 8 == 0) | |
| 161 return str.substr(0, len_bits / 8); | |
| 162 | |
| 163 // The initial truncation copies whole bytes | |
| 164 int number_bytes = (len_bits + 7) / 8; | |
| 165 std::string truncated_string = str.substr(0, number_bytes); | |
| 166 | |
| 167 // Keep the prescribed number of bits from the last byte. | |
| 168 unsigned last_char_bitmask = (1 << (len_bits % 8)) - 1; | |
| 169 truncated_string[number_bytes - 1] &= last_char_bitmask; | |
| 170 return truncated_string; | |
| 171 } | |
| 172 | |
| 109 void RegisterLocalAuthPrefs(user_prefs::PrefRegistrySyncable* registry) { | 173 void RegisterLocalAuthPrefs(user_prefs::PrefRegistrySyncable* registry) { |
| 110 registry->RegisterStringPref( | 174 registry->RegisterStringPref( |
| 111 prefs::kGoogleServicesPasswordHash, | 175 prefs::kGoogleServicesPasswordHash, |
| 112 std::string(), | 176 std::string(), |
| 113 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 177 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
| 114 } | 178 } |
| 115 | 179 |
| 116 void SetLocalAuthCredentials(size_t info_index, | 180 void SetLocalAuthCredentialsWithEncoding(size_t info_index, |
| 117 const std::string& password) { | 181 const std::string& password, |
| 118 if (info_index == std::string::npos) { | 182 char encoding_version) { |
| 119 NOTREACHED(); | 183 const HashEncoding& encoding = encodings[(encoding_version - '0') - 1]; |
| 120 return; | |
| 121 } | |
| 122 DCHECK(password.length()); | |
| 123 | 184 |
| 124 // Salt should be random data, as long as the hash length, and different with | 185 // Salt should be random data, as long as the hash length, and different with |
| 125 // every save. | 186 // every save. |
| 126 std::string salt_str; | 187 std::string salt_str; |
| 127 crypto::RandBytes(WriteInto(&salt_str, kHash1Bytes + 1), kHash1Bytes); | 188 crypto::RandBytes(WriteInto(&salt_str, encoding.hash_bytes + 1), |
| 128 DCHECK_EQ(kHash1Bytes, salt_str.length()); | 189 encoding.hash_bytes); |
| 190 DCHECK_EQ(encoding.hash_bytes, salt_str.length()); | |
|
jww
2015/01/29 11:44:45
This seems to me like an unnecessary DCHECK. This
Mike Lerman
2015/01/29 16:57:42
Done. I ain't touching crypto:: anything :)
| |
| 129 | 191 |
| 130 // Perform secure hash of password for storage. | 192 // Perform secure hash of password for storage. |
| 131 std::string password_hash = CreateSecurePasswordHash( | 193 std::string password_hash = CreateSecurePasswordHash( |
| 132 salt_str, password, kHash1Encoding); | 194 salt_str, password, encoding); |
| 133 DCHECK_EQ(kHash1Bytes, password_hash.length()); | |
| 134 | 195 |
| 135 // Group all fields into a single record for storage; | 196 // Group all fields into a single record for storage; |
| 136 std::string record; | 197 std::string record; |
| 137 record.append(salt_str); | 198 record.append(salt_str); |
| 138 record.append(password_hash); | 199 record.append(password_hash); |
| 139 | 200 |
| 140 // Encode it and store it. | 201 // Encode it and store it. |
| 141 std::string encoded = EncodePasswordHashRecord(record, kHash1Encoding); | 202 std::string encoded = EncodePasswordHashRecord(record, encoding); |
| 142 ProfileInfoCache& info = | 203 ProfileInfoCache& info = |
| 143 g_browser_process->profile_manager()->GetProfileInfoCache(); | 204 g_browser_process->profile_manager()->GetProfileInfoCache(); |
| 144 info.SetLocalAuthCredentialsOfProfileAtIndex(info_index, encoded); | 205 info.SetLocalAuthCredentialsOfProfileAtIndex(info_index, encoded); |
| 145 } | 206 } |
| 146 | 207 |
| 208 void SetLocalAuthCredentials(size_t info_index, | |
| 209 const std::string& password) { | |
| 210 if (info_index == std::string::npos) { | |
| 211 NOTREACHED(); | |
| 212 return; | |
| 213 } | |
| 214 DCHECK(password.length()); | |
| 215 SetLocalAuthCredentialsWithEncoding( | |
| 216 info_index, password, '0' + NUM_HASH_ENCODINGS); | |
| 217 } | |
| 218 | |
| 147 void SetLocalAuthCredentials(const Profile* profile, | 219 void SetLocalAuthCredentials(const Profile* profile, |
| 148 const std::string& password) { | 220 const std::string& password) { |
| 149 SetLocalAuthCredentials(GetProfileInfoIndexOfProfile(profile), password); | 221 SetLocalAuthCredentials(GetProfileInfoIndexOfProfile(profile), password); |
| 150 } | 222 } |
| 151 | 223 |
| 152 bool ValidateLocalAuthCredentials(size_t info_index, | 224 bool ValidateLocalAuthCredentials(size_t info_index, |
| 153 const std::string& password) { | 225 const std::string& password) { |
| 154 if (info_index == std::string::npos) { | 226 if (info_index == std::string::npos) { |
| 155 NOTREACHED(); | 227 NOTREACHED(); |
| 156 return false; | 228 return false; |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 167 if (encodedhash.length() == 0 && password.length() == 0) | 239 if (encodedhash.length() == 0 && password.length() == 0) |
| 168 return true; | 240 return true; |
| 169 if (!DecodePasswordHashRecord(encodedhash, &record, &encoding)) | 241 if (!DecodePasswordHashRecord(encodedhash, &record, &encoding)) |
| 170 return false; | 242 return false; |
| 171 | 243 |
| 172 std::string password_hash; | 244 std::string password_hash; |
| 173 const char* password_saved; | 245 const char* password_saved; |
| 174 const char* password_check; | 246 const char* password_check; |
| 175 size_t password_length; | 247 size_t password_length; |
| 176 | 248 |
| 177 if (encoding == '1') { | 249 const HashEncoding* hash_encoding = GetEncodingForVersion(encoding); |
| 178 // Validate correct length; extract salt and password hash. | 250 if (!hash_encoding) { |
| 179 if (record.length() != 2 * kHash1Bytes) | 251 // 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; | 252 return false; |
| 189 } | 253 } |
| 190 | 254 |
| 191 return crypto::SecureMemEqual(password_saved, password_check, | 255 // Extract salt. |
| 192 password_length); | 256 std::string salt_str(record.data(), hash_encoding->hash_bytes); |
| 257 // Extract password. | |
| 258 password_saved = record.data() + hash_encoding->hash_bytes; | |
| 259 password_hash = CreateSecurePasswordHash(salt_str, password, *hash_encoding); | |
| 260 password_length = hash_encoding->stored_bytes; | |
| 261 password_check = password_hash.data(); | |
| 262 | |
| 263 bool passwords_match = crypto::SecureMemEqual( | |
| 264 password_saved, password_check, password_length); | |
| 265 | |
| 266 // Update the stored credentials to the latest encoding if necessary. | |
| 267 if (passwords_match && (hash_encoding->version - '0') != NUM_HASH_ENCODINGS) | |
| 268 SetLocalAuthCredentials(info_index, password); | |
| 269 return passwords_match; | |
| 193 } | 270 } |
| 194 | 271 |
| 195 bool ValidateLocalAuthCredentials(const Profile* profile, | 272 bool ValidateLocalAuthCredentials(const Profile* profile, |
| 196 const std::string& password) { | 273 const std::string& password) { |
| 197 return ValidateLocalAuthCredentials(GetProfileInfoIndexOfProfile(profile), | 274 return ValidateLocalAuthCredentials(GetProfileInfoIndexOfProfile(profile), |
| 198 password); | 275 password); |
| 199 } | 276 } |
| 200 | 277 |
| 201 } // namespace chrome | 278 } // namespace chrome |
| OLD | NEW |