| OLD | NEW |
| 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 351 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 362 &trans, | 362 &trans, |
| 363 &node); | 363 &node); |
| 364 } | 364 } |
| 365 | 365 |
| 366 void SyncEncryptionHandlerImpl::EnableEncryptEverything() { | 366 void SyncEncryptionHandlerImpl::EnableEncryptEverything() { |
| 367 DCHECK(thread_checker_.CalledOnValidThread()); | 367 DCHECK(thread_checker_.CalledOnValidThread()); |
| 368 WriteTransaction trans(FROM_HERE, user_share_); | 368 WriteTransaction trans(FROM_HERE, user_share_); |
| 369 ModelTypeSet* encrypted_types = | 369 ModelTypeSet* encrypted_types = |
| 370 &UnlockVaultMutable(trans.GetWrappedTrans())->encrypted_types; | 370 &UnlockVaultMutable(trans.GetWrappedTrans())->encrypted_types; |
| 371 if (encrypt_everything_) { | 371 if (encrypt_everything_) { |
| 372 DCHECK(encrypted_types->Equals(ModelTypeSet::All())); | 372 DCHECK(encrypted_types->Equals(UserTypes())); |
| 373 return; | 373 return; |
| 374 } | 374 } |
| 375 DVLOG(1) << "Enabling encrypt everything."; | 375 DVLOG(1) << "Enabling encrypt everything."; |
| 376 encrypt_everything_ = true; | 376 encrypt_everything_ = true; |
| 377 // Change |encrypted_types_| directly to avoid sending more than one | 377 // Change |encrypted_types_| directly to avoid sending more than one |
| 378 // notification. | 378 // notification. |
| 379 *encrypted_types = ModelTypeSet::All(); | 379 *encrypted_types = UserTypes(); |
| 380 FOR_EACH_OBSERVER( | 380 FOR_EACH_OBSERVER( |
| 381 Observer, observers_, | 381 Observer, observers_, |
| 382 OnEncryptedTypesChanged(*encrypted_types, encrypt_everything_)); | 382 OnEncryptedTypesChanged(*encrypted_types, encrypt_everything_)); |
| 383 WriteEncryptionStateToNigori(&trans); | 383 WriteEncryptionStateToNigori(&trans); |
| 384 if (UnlockVault(trans.GetWrappedTrans()).cryptographer.is_ready()) | 384 if (UnlockVault(trans.GetWrappedTrans()).cryptographer.is_ready()) |
| 385 ReEncryptEverything(&trans); | 385 ReEncryptEverything(&trans); |
| 386 } | 386 } |
| 387 | 387 |
| 388 bool SyncEncryptionHandlerImpl::EncryptEverythingEnabled() const { | 388 bool SyncEncryptionHandlerImpl::EncryptEverythingEnabled() const { |
| 389 DCHECK(thread_checker_.CalledOnValidThread()); | 389 DCHECK(thread_checker_.CalledOnValidThread()); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 443 // which data for some or all types is not currently available. In that case, | 443 // which data for some or all types is not currently available. In that case, |
| 444 // the lookup of the root node will fail and we will skip encryption for that | 444 // the lookup of the root node will fail and we will skip encryption for that |
| 445 // type. | 445 // type. |
| 446 void SyncEncryptionHandlerImpl::ReEncryptEverything( | 446 void SyncEncryptionHandlerImpl::ReEncryptEverything( |
| 447 WriteTransaction* trans) { | 447 WriteTransaction* trans) { |
| 448 DCHECK(thread_checker_.CalledOnValidThread()); | 448 DCHECK(thread_checker_.CalledOnValidThread()); |
| 449 DCHECK(UnlockVault(trans->GetWrappedTrans()).cryptographer.is_ready()); | 449 DCHECK(UnlockVault(trans->GetWrappedTrans()).cryptographer.is_ready()); |
| 450 for (ModelTypeSet::Iterator iter = | 450 for (ModelTypeSet::Iterator iter = |
| 451 UnlockVault(trans->GetWrappedTrans()).encrypted_types.First(); | 451 UnlockVault(trans->GetWrappedTrans()).encrypted_types.First(); |
| 452 iter.Good(); iter.Inc()) { | 452 iter.Good(); iter.Inc()) { |
| 453 if (iter.Get() == PASSWORDS || iter.Get() == NIGORI) | 453 if (iter.Get() == PASSWORDS || IsControlType(iter.Get())) |
| 454 continue; // These types handle encryption differently. | 454 continue; // These types handle encryption differently. |
| 455 | 455 |
| 456 ReadNode type_root(trans); | 456 ReadNode type_root(trans); |
| 457 std::string tag = ModelTypeToRootTag(iter.Get()); | 457 std::string tag = ModelTypeToRootTag(iter.Get()); |
| 458 if (type_root.InitByTagLookup(tag) != BaseNode::INIT_OK) | 458 if (type_root.InitByTagLookup(tag) != BaseNode::INIT_OK) |
| 459 continue; // Don't try to reencrypt if the type's data is unavailable. | 459 continue; // Don't try to reencrypt if the type's data is unavailable. |
| 460 | 460 |
| 461 // Iterate through all children of this datatype. | 461 // Iterate through all children of this datatype. |
| 462 std::queue<int64> to_visit; | 462 std::queue<int64> to_visit; |
| 463 int64 child_id = type_root.GetFirstChildId(); | 463 int64 child_id = type_root.GetFirstChildId(); |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 628 } | 628 } |
| 629 | 629 |
| 630 bool SyncEncryptionHandlerImpl::UpdateEncryptedTypesFromNigori( | 630 bool SyncEncryptionHandlerImpl::UpdateEncryptedTypesFromNigori( |
| 631 const sync_pb::NigoriSpecifics& nigori, | 631 const sync_pb::NigoriSpecifics& nigori, |
| 632 syncable::BaseTransaction* const trans) { | 632 syncable::BaseTransaction* const trans) { |
| 633 DCHECK(thread_checker_.CalledOnValidThread()); | 633 DCHECK(thread_checker_.CalledOnValidThread()); |
| 634 ModelTypeSet* encrypted_types = &UnlockVaultMutable(trans)->encrypted_types; | 634 ModelTypeSet* encrypted_types = &UnlockVaultMutable(trans)->encrypted_types; |
| 635 if (nigori.encrypt_everything()) { | 635 if (nigori.encrypt_everything()) { |
| 636 if (!encrypt_everything_) { | 636 if (!encrypt_everything_) { |
| 637 encrypt_everything_ = true; | 637 encrypt_everything_ = true; |
| 638 *encrypted_types = ModelTypeSet::All(); | 638 *encrypted_types = UserTypes(); |
| 639 DVLOG(1) << "Enabling encrypt everything via nigori node update"; | 639 DVLOG(1) << "Enabling encrypt everything via nigori node update"; |
| 640 FOR_EACH_OBSERVER( | 640 FOR_EACH_OBSERVER( |
| 641 Observer, observers_, | 641 Observer, observers_, |
| 642 OnEncryptedTypesChanged(*encrypted_types, encrypt_everything_)); | 642 OnEncryptedTypesChanged(*encrypted_types, encrypt_everything_)); |
| 643 } | 643 } |
| 644 DCHECK(encrypted_types->Equals(ModelTypeSet::All())); | 644 DCHECK(encrypted_types->Equals(UserTypes())); |
| 645 return true; | 645 return true; |
| 646 } | 646 } |
| 647 | 647 |
| 648 ModelTypeSet nigori_encrypted_types; | 648 ModelTypeSet nigori_encrypted_types; |
| 649 nigori_encrypted_types = syncable::GetEncryptedTypesFromNigori(nigori); | 649 nigori_encrypted_types = syncable::GetEncryptedTypesFromNigori(nigori); |
| 650 nigori_encrypted_types.PutAll(SensitiveTypes()); | 650 nigori_encrypted_types.PutAll(SensitiveTypes()); |
| 651 | 651 |
| 652 // If anything more than the sensitive types were encrypted, and | 652 // If anything more than the sensitive types were encrypted, and |
| 653 // encrypt_everything is not explicitly set to false, we assume it means | 653 // encrypt_everything is not explicitly set to false, we assume it means |
| 654 // a client intended to enable encrypt everything. | 654 // a client intended to enable encrypt everything. |
| 655 if (!nigori.has_encrypt_everything() && | 655 if (!nigori.has_encrypt_everything() && |
| 656 !Difference(nigori_encrypted_types, SensitiveTypes()).Empty()) { | 656 !Difference(nigori_encrypted_types, SensitiveTypes()).Empty()) { |
| 657 if (!encrypt_everything_) { | 657 if (!encrypt_everything_) { |
| 658 encrypt_everything_ = true; | 658 encrypt_everything_ = true; |
| 659 *encrypted_types = ModelTypeSet::All(); | 659 *encrypted_types = UserTypes(); |
| 660 FOR_EACH_OBSERVER( | 660 FOR_EACH_OBSERVER( |
| 661 Observer, observers_, | 661 Observer, observers_, |
| 662 OnEncryptedTypesChanged(*encrypted_types, encrypt_everything_)); | 662 OnEncryptedTypesChanged(*encrypted_types, encrypt_everything_)); |
| 663 } | 663 } |
| 664 DCHECK(encrypted_types->Equals(ModelTypeSet::All())); | 664 DCHECK(encrypted_types->Equals(UserTypes())); |
| 665 return false; | 665 return false; |
| 666 } | 666 } |
| 667 | 667 |
| 668 MergeEncryptedTypes(nigori_encrypted_types, trans); | 668 MergeEncryptedTypes(nigori_encrypted_types, trans); |
| 669 return encrypted_types->Equals(nigori_encrypted_types); | 669 return encrypted_types->Equals(nigori_encrypted_types); |
| 670 } | 670 } |
| 671 | 671 |
| 672 void SyncEncryptionHandlerImpl::FinishSetPassphrase( | 672 void SyncEncryptionHandlerImpl::FinishSetPassphrase( |
| 673 bool success, | 673 bool success, |
| 674 const std::string& bootstrap_token, | 674 const std::string& bootstrap_token, |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 730 OnPassphraseAccepted()); | 730 OnPassphraseAccepted()); |
| 731 | 731 |
| 732 // Does nothing if everything is already encrypted. | 732 // Does nothing if everything is already encrypted. |
| 733 ReEncryptEverything(trans); | 733 ReEncryptEverything(trans); |
| 734 } | 734 } |
| 735 | 735 |
| 736 void SyncEncryptionHandlerImpl::MergeEncryptedTypes( | 736 void SyncEncryptionHandlerImpl::MergeEncryptedTypes( |
| 737 ModelTypeSet new_encrypted_types, | 737 ModelTypeSet new_encrypted_types, |
| 738 syncable::BaseTransaction* const trans) { | 738 syncable::BaseTransaction* const trans) { |
| 739 DCHECK(thread_checker_.CalledOnValidThread()); | 739 DCHECK(thread_checker_.CalledOnValidThread()); |
| 740 |
| 741 // Only UserTypes may be encrypted. |
| 742 DCHECK(UserTypes().HasAll(new_encrypted_types)); |
| 743 |
| 740 ModelTypeSet* encrypted_types = &UnlockVaultMutable(trans)->encrypted_types; | 744 ModelTypeSet* encrypted_types = &UnlockVaultMutable(trans)->encrypted_types; |
| 741 if (!encrypted_types->HasAll(new_encrypted_types)) { | 745 if (!encrypted_types->HasAll(new_encrypted_types)) { |
| 742 *encrypted_types = new_encrypted_types; | 746 *encrypted_types = new_encrypted_types; |
| 743 FOR_EACH_OBSERVER( | 747 FOR_EACH_OBSERVER( |
| 744 Observer, observers_, | 748 Observer, observers_, |
| 745 OnEncryptedTypesChanged(*encrypted_types, encrypt_everything_)); | 749 OnEncryptedTypesChanged(*encrypted_types, encrypt_everything_)); |
| 746 } | 750 } |
| 747 } | 751 } |
| 748 | 752 |
| 749 SyncEncryptionHandlerImpl::Vault* SyncEncryptionHandlerImpl::UnlockVaultMutable( | 753 SyncEncryptionHandlerImpl::Vault* SyncEncryptionHandlerImpl::UnlockVaultMutable( |
| 750 syncable::BaseTransaction* const trans) { | 754 syncable::BaseTransaction* const trans) { |
| 751 DCHECK_EQ(user_share_->directory.get(), trans->directory()); | 755 DCHECK_EQ(user_share_->directory.get(), trans->directory()); |
| 752 return &vault_unsafe_; | 756 return &vault_unsafe_; |
| 753 } | 757 } |
| 754 | 758 |
| 755 const SyncEncryptionHandlerImpl::Vault& SyncEncryptionHandlerImpl::UnlockVault( | 759 const SyncEncryptionHandlerImpl::Vault& SyncEncryptionHandlerImpl::UnlockVault( |
| 756 syncable::BaseTransaction* const trans) const { | 760 syncable::BaseTransaction* const trans) const { |
| 757 DCHECK_EQ(user_share_->directory.get(), trans->directory()); | 761 DCHECK_EQ(user_share_->directory.get(), trans->directory()); |
| 758 return vault_unsafe_; | 762 return vault_unsafe_; |
| 759 } | 763 } |
| 760 | 764 |
| 761 } // namespace browser_sync | 765 } // namespace browser_sync |
| OLD | NEW |