Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 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 #include "base/base64.h" | 5 #include "base/base64.h" |
| 6 #include "chrome/browser/sync/util/cryptographer.h" | 6 #include "chrome/browser/sync/util/cryptographer.h" |
| 7 #include "chrome/browser/password_manager/encryptor.h" | 7 #include "chrome/browser/password_manager/encryptor.h" |
| 8 | 8 |
| 9 namespace browser_sync { | 9 namespace browser_sync { |
| 10 | 10 |
| 11 const char kNigoriTag[] = "google_chrome_nigori"; | 11 const char kNigoriTag[] = "google_chrome_nigori"; |
| 12 | 12 |
| 13 // We name a particular Nigori instance (ie. a triplet consisting of a hostname, | 13 // We name a particular Nigori instance (ie. a triplet consisting of a hostname, |
| 14 // a username, and a password) by calling Permute on this string. Since the | 14 // a username, and a password) by calling Permute on this string. Since the |
| 15 // output of Permute is always the same for a given triplet, clients will always | 15 // output of Permute is always the same for a given triplet, clients will always |
| 16 // assign the same name to a particular triplet. | 16 // assign the same name to a particular triplet. |
| 17 const char kNigoriKeyName[] = "nigori-key"; | 17 const char kNigoriKeyName[] = "nigori-key"; |
| 18 | 18 |
| 19 Cryptographer::Observer::~Observer() {} | |
| 20 | |
| 19 Cryptographer::Cryptographer() | 21 Cryptographer::Cryptographer() |
| 20 : default_nigori_(NULL), | 22 : default_nigori_(NULL), |
| 21 encrypt_everything_(false) { | 23 encrypt_everything_(false) { |
| 22 syncable::ModelTypeSet sensitive_types = SensitiveTypes(); | 24 syncable::ModelTypeSet sensitive_types = SensitiveTypes(); |
| 23 encrypted_types_.insert(sensitive_types.begin(), sensitive_types.end()); | 25 encrypted_types_.insert(sensitive_types.begin(), sensitive_types.end()); |
| 24 } | 26 } |
| 25 | 27 |
| 26 Cryptographer::~Cryptographer() {} | 28 Cryptographer::~Cryptographer() {} |
| 27 | 29 |
| 30 void Cryptographer::AddObserver(Observer* observer) { | |
| 31 observers_.AddObserver(observer); | |
| 32 } | |
| 33 | |
| 34 void Cryptographer::RemoveObserver(Observer* observer) { | |
| 35 observers_.RemoveObserver(observer); | |
| 36 } | |
| 37 | |
| 28 void Cryptographer::Bootstrap(const std::string& restored_bootstrap_token) { | 38 void Cryptographer::Bootstrap(const std::string& restored_bootstrap_token) { |
| 29 if (is_initialized()) { | 39 if (is_initialized()) { |
| 30 NOTREACHED(); | 40 NOTREACHED(); |
| 31 return; | 41 return; |
| 32 } | 42 } |
| 33 | 43 |
| 34 scoped_ptr<Nigori> nigori(UnpackBootstrapToken(restored_bootstrap_token)); | 44 scoped_ptr<Nigori> nigori(UnpackBootstrapToken(restored_bootstrap_token)); |
| 35 if (nigori.get()) | 45 if (nigori.get()) |
| 36 AddKeyImpl(nigori.release()); | 46 AddKeyImpl(nigori.release()); |
| 37 } | 47 } |
| (...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 305 encrypted_types_.insert(syncable::APP_NOTIFICATIONS); | 315 encrypted_types_.insert(syncable::APP_NOTIFICATIONS); |
| 306 | 316 |
| 307 // Note: the initial version with encryption did not support the | 317 // Note: the initial version with encryption did not support the |
| 308 // encrypt_everything field. If anything more than the sensitive types were | 318 // encrypt_everything field. If anything more than the sensitive types were |
| 309 // encrypted, it meant we were encrypting everything. | 319 // encrypted, it meant we were encrypting everything. |
| 310 syncable::ModelTypeSet sensitive_types = SensitiveTypes(); | 320 syncable::ModelTypeSet sensitive_types = SensitiveTypes(); |
| 311 encrypted_types_.insert(sensitive_types.begin(), sensitive_types.end()); | 321 encrypted_types_.insert(sensitive_types.begin(), sensitive_types.end()); |
| 312 if (!nigori.has_encrypt_everything() && | 322 if (!nigori.has_encrypt_everything() && |
| 313 encrypted_types_.size() > sensitive_types.size()) { | 323 encrypted_types_.size() > sensitive_types.size()) { |
| 314 set_encrypt_everything(); | 324 set_encrypt_everything(); |
| 325 return; | |
| 315 } | 326 } |
| 327 | |
| 328 EmitEncryptedTypesChangeNotification(); | |
|
Nicolas Zea
2011/10/21 14:29:07
Perhaps only emit if the types actually changed? (
akalin
2011/10/22 03:28:38
Done.
| |
| 316 } | 329 } |
| 317 | 330 |
| 318 void Cryptographer::UpdateNigoriFromEncryptedTypes( | 331 void Cryptographer::UpdateNigoriFromEncryptedTypes( |
| 319 sync_pb::NigoriSpecifics* nigori) const { | 332 sync_pb::NigoriSpecifics* nigori) const { |
| 320 nigori->set_encrypt_everything(encrypt_everything_); | 333 nigori->set_encrypt_everything(encrypt_everything_); |
| 321 nigori->set_encrypt_bookmarks( | 334 nigori->set_encrypt_bookmarks( |
| 322 encrypted_types_.count(syncable::BOOKMARKS) > 0); | 335 encrypted_types_.count(syncable::BOOKMARKS) > 0); |
| 323 nigori->set_encrypt_preferences( | 336 nigori->set_encrypt_preferences( |
| 324 encrypted_types_.count(syncable::PREFERENCES) > 0); | 337 encrypted_types_.count(syncable::PREFERENCES) > 0); |
| 325 nigori->set_encrypt_autofill_profile( | 338 nigori->set_encrypt_autofill_profile( |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 336 encrypted_types_.count(syncable::SEARCH_ENGINES) > 0); | 349 encrypted_types_.count(syncable::SEARCH_ENGINES) > 0); |
| 337 nigori->set_encrypt_sessions(encrypted_types_.count(syncable::SESSIONS) > 0); | 350 nigori->set_encrypt_sessions(encrypted_types_.count(syncable::SESSIONS) > 0); |
| 338 nigori->set_encrypt_apps(encrypted_types_.count(syncable::APPS) > 0); | 351 nigori->set_encrypt_apps(encrypted_types_.count(syncable::APPS) > 0); |
| 339 nigori->set_encrypt_app_notifications( | 352 nigori->set_encrypt_app_notifications( |
| 340 encrypted_types_.count(syncable::APP_NOTIFICATIONS) > 0); | 353 encrypted_types_.count(syncable::APP_NOTIFICATIONS) > 0); |
| 341 } | 354 } |
| 342 | 355 |
| 343 void Cryptographer::set_encrypt_everything() { | 356 void Cryptographer::set_encrypt_everything() { |
| 344 encrypt_everything_ = true; | 357 encrypt_everything_ = true; |
| 345 encrypted_types_ = syncable::GetAllRealModelTypes(); | 358 encrypted_types_ = syncable::GetAllRealModelTypes(); |
| 359 EmitEncryptedTypesChangeNotification(); | |
| 346 } | 360 } |
| 347 | 361 |
| 348 bool Cryptographer::encrypt_everything() const { | 362 bool Cryptographer::encrypt_everything() const { |
| 349 return encrypt_everything_; | 363 return encrypt_everything_; |
| 350 } | 364 } |
| 351 | 365 |
| 352 void Cryptographer::SetEncryptedTypes(syncable::ModelTypeSet new_types) { | 366 void Cryptographer::SetEncryptedTypes(syncable::ModelTypeSet new_types) { |
| 353 encrypted_types_.insert(new_types.begin(), new_types.end()); | 367 encrypted_types_.insert(new_types.begin(), new_types.end()); |
| 368 EmitEncryptedTypesChangeNotification(); | |
| 354 } | 369 } |
| 355 | 370 |
| 356 syncable::ModelTypeSet Cryptographer::GetEncryptedTypes() const { | 371 syncable::ModelTypeSet Cryptographer::GetEncryptedTypes() const { |
| 357 return encrypted_types_; | 372 return encrypted_types_; |
| 358 } | 373 } |
| 359 | 374 |
| 375 void Cryptographer::MarkEncryptionComplete() { | |
| 376 EmitEncryptedTypesChangeNotification(); | |
| 377 FOR_EACH_OBSERVER(Observer, observers_, OnEncryptionComplete()); | |
| 378 } | |
| 379 | |
| 380 void Cryptographer::EmitEncryptedTypesChangeNotification() { | |
| 381 FOR_EACH_OBSERVER( | |
| 382 Observer, observers_, | |
| 383 OnEncryptedTypesChanged(encrypted_types_, encrypt_everything_)); | |
| 384 } | |
| 385 | |
| 360 void Cryptographer::InstallKeys(const std::string& default_key_name, | 386 void Cryptographer::InstallKeys(const std::string& default_key_name, |
| 361 const sync_pb::NigoriKeyBag& bag) { | 387 const sync_pb::NigoriKeyBag& bag) { |
| 362 int key_size = bag.key_size(); | 388 int key_size = bag.key_size(); |
| 363 for (int i = 0; i < key_size; ++i) { | 389 for (int i = 0; i < key_size; ++i) { |
| 364 const sync_pb::NigoriKey key = bag.key(i); | 390 const sync_pb::NigoriKey key = bag.key(i); |
| 365 // Only use this key if we don't already know about it. | 391 // Only use this key if we don't already know about it. |
| 366 if (nigoris_.end() == nigoris_.find(key.name())) { | 392 if (nigoris_.end() == nigoris_.find(key.name())) { |
| 367 scoped_ptr<Nigori> new_nigori(new Nigori); | 393 scoped_ptr<Nigori> new_nigori(new Nigori); |
| 368 if (!new_nigori->InitByImport(key.user_key(), | 394 if (!new_nigori->InitByImport(key.user_key(), |
| 369 key.encryption_key(), | 395 key.encryption_key(), |
| 370 key.mac_key())) { | 396 key.mac_key())) { |
| 371 NOTREACHED(); | 397 NOTREACHED(); |
| 372 continue; | 398 continue; |
| 373 } | 399 } |
| 374 nigoris_[key.name()] = make_linked_ptr(new_nigori.release()); | 400 nigoris_[key.name()] = make_linked_ptr(new_nigori.release()); |
| 375 } | 401 } |
| 376 } | 402 } |
| 377 DCHECK(nigoris_.end() != nigoris_.find(default_key_name)); | 403 DCHECK(nigoris_.end() != nigoris_.find(default_key_name)); |
| 378 default_nigori_ = &*nigoris_.find(default_key_name); | 404 default_nigori_ = &*nigoris_.find(default_key_name); |
| 379 } | 405 } |
| 380 | 406 |
| 381 } // namespace browser_sync | 407 } // namespace browser_sync |
| OLD | NEW |