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

Side by Side Diff: sync/internal_api/sync_encryption_handler_impl.cc

Issue 10878015: [Sync] Move keystore key handling to SyncEncryptionHandlerImpl (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Change observer registration ordering Created 8 years, 4 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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "sync/internal_api/sync_encryption_handler_impl.h" 5 #include "sync/internal_api/sync_encryption_handler_impl.h"
6 6
7 #include <queue> 7 #include <queue>
8 #include <string> 8 #include <string>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
45 45
46 template <typename T> 46 template <typename T>
47 T* TransactionalHolder<T>::GetMutable( 47 T* TransactionalHolder<T>::GetMutable(
48 syncable::BaseTransaction* const trans) { 48 syncable::BaseTransaction* const trans) {
49 DCHECK_EQ(user_share_->directory.get(), trans->directory()); 49 DCHECK_EQ(user_share_->directory.get(), trans->directory());
50 return obj_; 50 return obj_;
51 } 51 }
52 52
53 SyncEncryptionHandlerImpl::SyncEncryptionHandlerImpl( 53 SyncEncryptionHandlerImpl::SyncEncryptionHandlerImpl(
54 UserShare* user_share, 54 UserShare* user_share,
55 Encryptor* encryptor) 55 Encryptor* encryptor,
56 const std::string& restored_key_for_bootstrapping,
57 const std::string& restored_keystore_key_for_bootstrapping)
56 : weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), 58 : weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
57 user_share_(user_share), 59 user_share_(user_share),
58 cryptographer_unsafe_(encryptor), 60 cryptographer_unsafe_(encryptor),
59 encrypted_types_unsafe_(SensitiveTypes()), 61 encrypted_types_unsafe_(SensitiveTypes()),
60 cryptographer_holder_(user_share_, &cryptographer_unsafe_), 62 cryptographer_holder_(user_share_, &cryptographer_unsafe_),
61 encrypted_types_holder_(user_share_, &encrypted_types_unsafe_), 63 encrypted_types_holder_(user_share_, &encrypted_types_unsafe_),
62 encrypt_everything_(false), 64 encrypt_everything_(false),
63 passphrase_state_(IMPLICIT_PASSPHRASE), 65 passphrase_state_(IMPLICIT_PASSPHRASE),
66 keystore_key_(restored_keystore_key_for_bootstrapping),
64 nigori_overwrite_count_(0) { 67 nigori_overwrite_count_(0) {
68 // We only bootstrap the user provided passphrase. The keystore key is handled
69 // at Init time once we're sure the nigori is downloaded.
70 cryptographer_unsafe_.Bootstrap(restored_key_for_bootstrapping);
65 } 71 }
66 72
67 SyncEncryptionHandlerImpl::~SyncEncryptionHandlerImpl() {} 73 SyncEncryptionHandlerImpl::~SyncEncryptionHandlerImpl() {}
68 74
69 void SyncEncryptionHandlerImpl::AddObserver(Observer* observer) { 75 void SyncEncryptionHandlerImpl::AddObserver(Observer* observer) {
70 DCHECK(thread_checker_.CalledOnValidThread()); 76 DCHECK(thread_checker_.CalledOnValidThread());
71 DCHECK(!observers_.HasObserver(observer)); 77 DCHECK(!observers_.HasObserver(observer));
72 observers_.AddObserver(observer); 78 observers_.AddObserver(observer);
73 } 79 }
74 80
(...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after
423 } 429 }
424 430
425 void SyncEncryptionHandlerImpl::UpdateNigoriFromEncryptedTypes( 431 void SyncEncryptionHandlerImpl::UpdateNigoriFromEncryptedTypes(
426 sync_pb::NigoriSpecifics* nigori, 432 sync_pb::NigoriSpecifics* nigori,
427 syncable::BaseTransaction* const trans) const { 433 syncable::BaseTransaction* const trans) const {
428 syncable::UpdateNigoriFromEncryptedTypes(encrypted_types_holder_.Get(trans), 434 syncable::UpdateNigoriFromEncryptedTypes(encrypted_types_holder_.Get(trans),
429 encrypt_everything_, 435 encrypt_everything_,
430 nigori); 436 nigori);
431 } 437 }
432 438
439 bool SyncEncryptionHandlerImpl::NeedKeystoreKey(
440 syncable::BaseTransaction* const trans) const {
441 return keystore_key_.empty();
442 }
443
444 bool SyncEncryptionHandlerImpl::SetKeystoreKey(
445 const std::string& key,
446 syncable::BaseTransaction* const trans) {
447 if (!keystore_key_.empty() || key.empty())
448 return false;
449 keystore_key_ = key;
450
451 // TODO(zea): trigger migration if necessary.
452
453 DVLOG(1) << "Keystore bootstrap token updated.";
454 FOR_EACH_OBSERVER(SyncEncryptionHandler::Observer, observers_,
455 OnBootstrapTokenUpdated(key,
456 KEYSTORE_BOOTSTRAP_TOKEN));
457 return true;
458 }
459
433 ModelTypeSet SyncEncryptionHandlerImpl::GetEncryptedTypes( 460 ModelTypeSet SyncEncryptionHandlerImpl::GetEncryptedTypes(
434 syncable::BaseTransaction* const trans) const { 461 syncable::BaseTransaction* const trans) const {
435 return encrypted_types_holder_.Get(trans); 462 return encrypted_types_holder_.Get(trans);
436 } 463 }
437 464
438 // This function iterates over all encrypted types. There are many scenarios in 465 // This function iterates over all encrypted types. There are many scenarios in
439 // which data for some or all types is not currently available. In that case, 466 // which data for some or all types is not currently available. In that case,
440 // the lookup of the root node will fail and we will skip encryption for that 467 // the lookup of the root node will fail and we will skip encryption for that
441 // type. 468 // type.
442 void SyncEncryptionHandlerImpl::ReEncryptEverything( 469 void SyncEncryptionHandlerImpl::ReEncryptEverything(
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after
677 observers_, 704 observers_,
678 OnCryptographerStateChanged( 705 OnCryptographerStateChanged(
679 cryptographer_holder_.GetMutable(trans->GetWrappedTrans()))); 706 cryptographer_holder_.GetMutable(trans->GetWrappedTrans())));
680 707
681 // It's possible we need to change the bootstrap token even if we failed to 708 // It's possible we need to change the bootstrap token even if we failed to
682 // set the passphrase (for example if we need to preserve the new GAIA 709 // set the passphrase (for example if we need to preserve the new GAIA
683 // passphrase). 710 // passphrase).
684 if (!bootstrap_token.empty()) { 711 if (!bootstrap_token.empty()) {
685 DVLOG(1) << "Passphrase bootstrap token updated."; 712 DVLOG(1) << "Passphrase bootstrap token updated.";
686 FOR_EACH_OBSERVER(SyncEncryptionHandler::Observer, observers_, 713 FOR_EACH_OBSERVER(SyncEncryptionHandler::Observer, observers_,
687 OnBootstrapTokenUpdated(bootstrap_token)); 714 OnBootstrapTokenUpdated(bootstrap_token,
715 PASSPHRASE_BOOTSTRAP_TOKEN));
688 } 716 }
689 717
690 const Cryptographer& cryptographer = 718 const Cryptographer& cryptographer =
691 cryptographer_holder_.Get(trans->GetWrappedTrans()); 719 cryptographer_holder_.Get(trans->GetWrappedTrans());
692 if (!success) { 720 if (!success) {
693 if (cryptographer.is_ready()) { 721 if (cryptographer.is_ready()) {
694 LOG(ERROR) << "Attempt to change passphrase failed while cryptographer " 722 LOG(ERROR) << "Attempt to change passphrase failed while cryptographer "
695 << "was ready."; 723 << "was ready.";
696 } else if (cryptographer.has_pending_keys()) { 724 } else if (cryptographer.has_pending_keys()) {
697 FOR_EACH_OBSERVER(SyncEncryptionHandler::Observer, observers_, 725 FOR_EACH_OBSERVER(SyncEncryptionHandler::Observer, observers_,
698 OnPassphraseRequired(REASON_DECRYPTION, 726 OnPassphraseRequired(REASON_DECRYPTION,
699 cryptographer.GetPendingKeys())); 727 cryptographer.GetPendingKeys()));
700 } else { 728 } else {
701 FOR_EACH_OBSERVER(SyncEncryptionHandler::Observer, observers_, 729 FOR_EACH_OBSERVER(SyncEncryptionHandler::Observer, observers_,
702 OnPassphraseRequired(REASON_ENCRYPTION, 730 OnPassphraseRequired(REASON_ENCRYPTION,
703 sync_pb::EncryptedData())); 731 sync_pb::EncryptedData()));
704 } 732 }
705 return; 733 return;
706 } 734 }
707 735
708 DCHECK(cryptographer.is_ready()); 736 DCHECK(cryptographer.is_ready());
709 737
738 // TODO(zea): trigger migration if necessary.
739
710 sync_pb::NigoriSpecifics specifics(nigori_node->GetNigoriSpecifics()); 740 sync_pb::NigoriSpecifics specifics(nigori_node->GetNigoriSpecifics());
711 // Does not modify specifics.encrypted() if the original decrypted data was 741 // Does not modify specifics.encrypted() if the original decrypted data was
712 // the same. 742 // the same.
713 if (!cryptographer.GetKeys(specifics.mutable_encrypted())) 743 if (!cryptographer.GetKeys(specifics.mutable_encrypted()))
714 NOTREACHED(); 744 NOTREACHED();
715 if (is_explicit && passphrase_state_ != CUSTOM_PASSPHRASE) { 745 if (is_explicit && passphrase_state_ != CUSTOM_PASSPHRASE) {
716 passphrase_state_ = CUSTOM_PASSPHRASE; 746 passphrase_state_ = CUSTOM_PASSPHRASE;
717 FOR_EACH_OBSERVER(SyncEncryptionHandler::Observer, observers_, 747 FOR_EACH_OBSERVER(SyncEncryptionHandler::Observer, observers_,
718 OnPassphraseStateChanged(passphrase_state_)); 748 OnPassphraseStateChanged(passphrase_state_));
719 } 749 }
(...skipping 16 matching lines...) Expand all
736 ModelTypeSet* encrypted_types = encrypted_types_holder_.GetMutable(trans); 766 ModelTypeSet* encrypted_types = encrypted_types_holder_.GetMutable(trans);
737 if (!encrypted_types->HasAll(new_encrypted_types)) { 767 if (!encrypted_types->HasAll(new_encrypted_types)) {
738 *encrypted_types = new_encrypted_types; 768 *encrypted_types = new_encrypted_types;
739 FOR_EACH_OBSERVER( 769 FOR_EACH_OBSERVER(
740 Observer, observers_, 770 Observer, observers_,
741 OnEncryptedTypesChanged(*encrypted_types, encrypt_everything_)); 771 OnEncryptedTypesChanged(*encrypted_types, encrypt_everything_));
742 } 772 }
743 } 773 }
744 774
745 } // namespace browser_sync 775 } // namespace browser_sync
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698