| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // This class isn't pretty. It's just a step better than globals, which is what | 5 // This class isn't pretty. It's just a step better than globals, which is what |
| 6 // these were previously. | 6 // these were previously. |
| 7 | 7 |
| 8 #include "chrome/browser/sync/util/user_settings.h" | 8 #include "chrome/browser/sync/util/user_settings.h" |
| 9 | 9 |
| 10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
| 11 | 11 |
| 12 #if defined(OS_WIN) | 12 #if defined(OS_WIN) |
| 13 #include <windows.h> | 13 #include <windows.h> |
| 14 #endif | 14 #endif |
| 15 | 15 |
| 16 #include <limits> | 16 #include <limits> |
| 17 #include <string> | 17 #include <string> |
| 18 #include <vector> | 18 #include <vector> |
| 19 | 19 |
| 20 #include "base/file_util.h" | 20 #include "base/file_util.h" |
| 21 #include "base/md5.h" |
| 22 #include "base/rand_util.h" |
| 21 #include "base/string_util.h" | 23 #include "base/string_util.h" |
| 22 #include "chrome/browser/sync/syncable/directory_manager.h" // For migration. | 24 #include "chrome/browser/sync/syncable/directory_manager.h" // For migration. |
| 23 #include "chrome/browser/sync/util/crypto_helpers.h" | |
| 24 #include "chrome/browser/sync/util/data_encryption.h" | 25 #include "chrome/browser/sync/util/data_encryption.h" |
| 26 #include "chrome/common/random.h" |
| 25 #include "chrome/common/sqlite_utils.h" | 27 #include "chrome/common/sqlite_utils.h" |
| 26 | 28 |
| 27 using std::numeric_limits; | 29 using std::numeric_limits; |
| 28 using std::string; | 30 using std::string; |
| 29 using std::vector; | 31 using std::vector; |
| 30 | 32 |
| 31 using syncable::DirectoryManager; | 33 using syncable::DirectoryManager; |
| 32 | 34 |
| 33 namespace browser_sync { | 35 namespace browser_sync { |
| 34 | 36 |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 } | 161 } |
| 160 | 162 |
| 161 static void MakeClientIDTable(sqlite3* const dbhandle) { | 163 static void MakeClientIDTable(sqlite3* const dbhandle) { |
| 162 // Stores a single client ID value that can be used as the client id, if | 164 // Stores a single client ID value that can be used as the client id, if |
| 163 // there's not another such ID provided on the install. | 165 // there's not another such ID provided on the install. |
| 164 ExecOrDie(dbhandle, "CREATE TABLE client_id (id) "); | 166 ExecOrDie(dbhandle, "CREATE TABLE client_id (id) "); |
| 165 { | 167 { |
| 166 SQLStatement statement; | 168 SQLStatement statement; |
| 167 statement.prepare(dbhandle, | 169 statement.prepare(dbhandle, |
| 168 "INSERT INTO client_id values ( ? )"); | 170 "INSERT INTO client_id values ( ? )"); |
| 169 statement.bind_string(0, Generate128BitRandomHexString()); | 171 statement.bind_string(0, Generate128BitRandomBase64String()); |
| 170 if (SQLITE_DONE != statement.step()) { | 172 if (SQLITE_DONE != statement.step()) { |
| 171 LOG(FATAL) << "INSERT INTO client_id\n" << sqlite3_errmsg(dbhandle); | 173 LOG(FATAL) << "INSERT INTO client_id\n" << sqlite3_errmsg(dbhandle); |
| 172 } | 174 } |
| 173 } | 175 } |
| 174 } | 176 } |
| 175 | 177 |
| 176 bool UserSettings::Init(const FilePath& settings_path) { | 178 bool UserSettings::Init(const FilePath& settings_path) { |
| 177 { // Scope the handle. | 179 { // Scope the handle. |
| 178 ScopedDBHandle dbhandle(this); | 180 ScopedDBHandle dbhandle(this); |
| 179 if (dbhandle_) | 181 if (dbhandle_) |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 UserSettings::~UserSettings() { | 265 UserSettings::~UserSettings() { |
| 264 if (dbhandle_) | 266 if (dbhandle_) |
| 265 sqlite3_close(dbhandle_); | 267 sqlite3_close(dbhandle_); |
| 266 } | 268 } |
| 267 | 269 |
| 268 const int32 kInvalidHash = 0xFFFFFFFF; | 270 const int32 kInvalidHash = 0xFFFFFFFF; |
| 269 | 271 |
| 270 // We use 10 bits of data from the MD5 digest as the hash. | 272 // We use 10 bits of data from the MD5 digest as the hash. |
| 271 const int32 kHashMask = 0x3FF; | 273 const int32 kHashMask = 0x3FF; |
| 272 | 274 |
| 273 int32 GetHashFromDigest(const vector<uint8>& digest) { | 275 int32 GetHashFromDigest(MD5Digest& digest) { |
| 274 int32 hash = 0; | 276 int32 hash = 0; |
| 275 int32 mask = kHashMask; | 277 int32 mask = kHashMask; |
| 276 for (vector<uint8>::const_iterator i = digest.begin(); i != digest.end(); | 278 for (size_t i = 0; i < sizeof(digest.a); ++i) { |
| 277 ++i) { | |
| 278 hash = hash << 8; | 279 hash = hash << 8; |
| 279 hash = hash | (*i & kHashMask); | 280 hash = hash | (digest.a[i] & kHashMask); |
| 280 mask = mask >> 8; | 281 mask = mask >> 8; |
| 281 if (0 == mask) | 282 if (0 == mask) |
| 282 break; | 283 break; |
| 283 } | 284 } |
| 284 return hash; | 285 return hash; |
| 285 } | 286 } |
| 286 | 287 |
| 287 void UserSettings::StoreEmailForSignin(const string& signin, | 288 void UserSettings::StoreEmailForSignin(const string& signin, |
| 288 const string& primary_email) { | 289 const string& primary_email) { |
| 289 ScopedDBHandle dbhandle(this); | 290 ScopedDBHandle dbhandle(this); |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 344 return true; | 345 return true; |
| 345 } | 346 } |
| 346 } | 347 } |
| 347 return false; | 348 return false; |
| 348 } | 349 } |
| 349 | 350 |
| 350 void UserSettings::StoreHashedPassword(const string& email, | 351 void UserSettings::StoreHashedPassword(const string& email, |
| 351 const string& password) { | 352 const string& password) { |
| 352 // Save one-way hashed password: | 353 // Save one-way hashed password: |
| 353 char binary_salt[kSaltSize]; | 354 char binary_salt[kSaltSize]; |
| 354 GetRandomBytes(binary_salt, sizeof(binary_salt)); | 355 base::RandBytes(binary_salt, sizeof(binary_salt)); |
| 355 | 356 |
| 356 const string salt = APEncode(string(binary_salt, sizeof(binary_salt))); | 357 const string salt = APEncode(string(binary_salt, sizeof(binary_salt))); |
| 357 MD5Calculator md5; | 358 MD5Context md5_context; |
| 358 md5.AddData(salt.data(), salt.size()); | 359 MD5Init(&md5_context); |
| 359 md5.AddData(password.data(), password.size()); | 360 MD5Update(&md5_context, salt.data(), salt.size()); |
| 361 MD5Update(&md5_context, password.data(), password.size()); |
| 362 MD5Digest md5_digest; |
| 363 MD5Final(&md5_digest, &md5_context); |
| 364 |
| 360 ScopedDBHandle dbhandle(this); | 365 ScopedDBHandle dbhandle(this); |
| 361 SQLTransaction transaction(dbhandle.get()); | 366 SQLTransaction transaction(dbhandle.get()); |
| 362 transaction.BeginExclusive(); | 367 transaction.BeginExclusive(); |
| 363 { | 368 { |
| 364 SQLStatement statement; | 369 SQLStatement statement; |
| 365 statement.prepare(dbhandle.get(), | 370 statement.prepare(dbhandle.get(), |
| 366 "INSERT INTO settings(email, key, value)" | 371 "INSERT INTO settings(email, key, value)" |
| 367 " values ( ?, ?, ? )"); | 372 " values ( ?, ?, ? )"); |
| 368 statement.bind_string(0, email); | 373 statement.bind_string(0, email); |
| 369 statement.bind_string(1, PASSWORD_HASH); | 374 statement.bind_string(1, PASSWORD_HASH); |
| 370 statement.bind_int(2, GetHashFromDigest(md5.GetDigest())); | 375 statement.bind_int(2, GetHashFromDigest(md5_digest)); |
| 371 if (SQLITE_DONE != statement.step()) { | 376 if (SQLITE_DONE != statement.step()) { |
| 372 LOG(FATAL) << sqlite3_errmsg(dbhandle.get()); | 377 LOG(FATAL) << sqlite3_errmsg(dbhandle.get()); |
| 373 } | 378 } |
| 374 } | 379 } |
| 375 { | 380 { |
| 376 SQLStatement statement; | 381 SQLStatement statement; |
| 377 statement.prepare(dbhandle.get(), | 382 statement.prepare(dbhandle.get(), |
| 378 "INSERT INTO settings(email, key, value)" | 383 "INSERT INTO settings(email, key, value)" |
| 379 " values ( ?, ?, ? )"); | 384 " values ( ?, ?, ? )"); |
| 380 statement.bind_string(0, email); | 385 statement.bind_string(0, email); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 406 string key(query.column_string(0)); | 411 string key(query.column_string(0)); |
| 407 if (key == SALT) | 412 if (key == SALT) |
| 408 salt = query.column_string(1); | 413 salt = query.column_string(1); |
| 409 else | 414 else |
| 410 hash = query.column_int(1); | 415 hash = query.column_int(1); |
| 411 query_result = query.step(); | 416 query_result = query.step(); |
| 412 } | 417 } |
| 413 CHECK(SQLITE_DONE == query_result); | 418 CHECK(SQLITE_DONE == query_result); |
| 414 if (salt.empty() || hash == kInvalidHash) | 419 if (salt.empty() || hash == kInvalidHash) |
| 415 return false; | 420 return false; |
| 416 MD5Calculator md5; | 421 MD5Context md5_context; |
| 417 md5.AddData(salt.data(), salt.size()); | 422 MD5Init(&md5_context); |
| 418 md5.AddData(password.data(), password.size()); | 423 MD5Update(&md5_context, salt.data(), salt.size()); |
| 419 return hash == GetHashFromDigest(md5.GetDigest()); | 424 MD5Update(&md5_context, password.data(), password.size()); |
| 425 MD5Digest md5_digest; |
| 426 MD5Final(&md5_digest, &md5_context); |
| 427 return hash == GetHashFromDigest(md5_digest); |
| 420 } | 428 } |
| 421 | 429 |
| 422 void UserSettings::SwitchUser(const string& username) { | 430 void UserSettings::SwitchUser(const string& username) { |
| 423 { | 431 { |
| 424 base::AutoLock lock(mutex_); | 432 base::AutoLock lock(mutex_); |
| 425 email_ = username; | 433 email_ = username; |
| 426 } | 434 } |
| 427 } | 435 } |
| 428 | 436 |
| 429 string UserSettings::GetClientId() { | 437 string UserSettings::GetClientId() { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 448 query.prepare(dbhandle.get(), "SELECT email FROM cookies"); | 456 query.prepare(dbhandle.get(), "SELECT email FROM cookies"); |
| 449 if (SQLITE_ROW == query.step()) { | 457 if (SQLITE_ROW == query.step()) { |
| 450 *username = query.column_string(0); | 458 *username = query.column_string(0); |
| 451 return true; | 459 return true; |
| 452 } else { | 460 } else { |
| 453 return false; | 461 return false; |
| 454 } | 462 } |
| 455 } | 463 } |
| 456 | 464 |
| 457 } // namespace browser_sync | 465 } // namespace browser_sync |
| OLD | NEW |