Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1306)

Side by Side Diff: chrome/browser/sync/util/user_settings.cc

Issue 6873156: Move crypto_helpers from sync to base (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Follow agl review Created 9 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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
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
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 MD5Digest md5_digest;
359 md5.AddData(password.data(), password.size()); 360 MD5Init(&md5_context);
361 MD5Update(&md5_context, salt.data(), salt.size());
362 MD5Update(&md5_context, password.data(), password.size());
363 MD5Final(&md5_digest, &md5_context);
brettw 2011/05/03 17:36:53 I'd put a blank line after this to help separate i
360 ScopedDBHandle dbhandle(this); 364 ScopedDBHandle dbhandle(this);
361 SQLTransaction transaction(dbhandle.get()); 365 SQLTransaction transaction(dbhandle.get());
362 transaction.BeginExclusive(); 366 transaction.BeginExclusive();
363 { 367 {
364 SQLStatement statement; 368 SQLStatement statement;
365 statement.prepare(dbhandle.get(), 369 statement.prepare(dbhandle.get(),
366 "INSERT INTO settings(email, key, value)" 370 "INSERT INTO settings(email, key, value)"
367 " values ( ?, ?, ? )"); 371 " values ( ?, ?, ? )");
368 statement.bind_string(0, email); 372 statement.bind_string(0, email);
369 statement.bind_string(1, PASSWORD_HASH); 373 statement.bind_string(1, PASSWORD_HASH);
370 statement.bind_int(2, GetHashFromDigest(md5.GetDigest())); 374 statement.bind_int(2, GetHashFromDigest(md5_digest));
371 if (SQLITE_DONE != statement.step()) { 375 if (SQLITE_DONE != statement.step()) {
372 LOG(FATAL) << sqlite3_errmsg(dbhandle.get()); 376 LOG(FATAL) << sqlite3_errmsg(dbhandle.get());
373 } 377 }
374 } 378 }
375 { 379 {
376 SQLStatement statement; 380 SQLStatement statement;
377 statement.prepare(dbhandle.get(), 381 statement.prepare(dbhandle.get(),
378 "INSERT INTO settings(email, key, value)" 382 "INSERT INTO settings(email, key, value)"
379 " values ( ?, ?, ? )"); 383 " values ( ?, ?, ? )");
380 statement.bind_string(0, email); 384 statement.bind_string(0, email);
(...skipping 25 matching lines...) Expand all
406 string key(query.column_string(0)); 410 string key(query.column_string(0));
407 if (key == SALT) 411 if (key == SALT)
408 salt = query.column_string(1); 412 salt = query.column_string(1);
409 else 413 else
410 hash = query.column_int(1); 414 hash = query.column_int(1);
411 query_result = query.step(); 415 query_result = query.step();
412 } 416 }
413 CHECK(SQLITE_DONE == query_result); 417 CHECK(SQLITE_DONE == query_result);
414 if (salt.empty() || hash == kInvalidHash) 418 if (salt.empty() || hash == kInvalidHash)
415 return false; 419 return false;
416 MD5Calculator md5; 420 MD5Context md5_context;
417 md5.AddData(salt.data(), salt.size()); 421 MD5Digest md5_digest;
418 md5.AddData(password.data(), password.size()); 422 MD5Init(&md5_context);
419 return hash == GetHashFromDigest(md5.GetDigest()); 423 MD5Update(&md5_context, salt.data(), salt.size());
424 MD5Update(&md5_context, password.data(), password.size());
425 MD5Final(&md5_digest, &md5_context);
426 return hash == GetHashFromDigest(md5_digest);
420 } 427 }
421 428
422 void UserSettings::SwitchUser(const string& username) { 429 void UserSettings::SwitchUser(const string& username) {
423 { 430 {
424 base::AutoLock lock(mutex_); 431 base::AutoLock lock(mutex_);
425 email_ = username; 432 email_ = username;
426 } 433 }
427 } 434 }
428 435
429 string UserSettings::GetClientId() { 436 string UserSettings::GetClientId() {
(...skipping 18 matching lines...) Expand all
448 query.prepare(dbhandle.get(), "SELECT email FROM cookies"); 455 query.prepare(dbhandle.get(), "SELECT email FROM cookies");
449 if (SQLITE_ROW == query.step()) { 456 if (SQLITE_ROW == query.step()) {
450 *username = query.column_string(0); 457 *username = query.column_string(0);
451 return true; 458 return true;
452 } else { 459 } else {
453 return false; 460 return false;
454 } 461 }
455 } 462 }
456 463
457 } // namespace browser_sync 464 } // namespace browser_sync
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698