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

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

Issue 10824410: [Sync] Refactor passphrase state handling (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix last test 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 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
53 SyncEncryptionHandlerImpl::SyncEncryptionHandlerImpl( 53 SyncEncryptionHandlerImpl::SyncEncryptionHandlerImpl(
54 UserShare* user_share, 54 UserShare* user_share,
55 Encryptor* encryptor) 55 Encryptor* encryptor)
56 : weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), 56 : weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
57 user_share_(user_share), 57 user_share_(user_share),
58 cryptographer_unsafe_(encryptor), 58 cryptographer_unsafe_(encryptor),
59 encrypted_types_unsafe_(SensitiveTypes()), 59 encrypted_types_unsafe_(SensitiveTypes()),
60 cryptographer_holder_(user_share_, &cryptographer_unsafe_), 60 cryptographer_holder_(user_share_, &cryptographer_unsafe_),
61 encrypted_types_holder_(user_share_, &encrypted_types_unsafe_), 61 encrypted_types_holder_(user_share_, &encrypted_types_unsafe_),
62 encrypt_everything_(false), 62 encrypt_everything_(false),
63 explicit_passphrase_(false), 63 passphrase_state_(IMPLICIT_PASSPHRASE),
64 nigori_overwrite_count_(0) { 64 nigori_overwrite_count_(0) {
65 } 65 }
66 66
67 SyncEncryptionHandlerImpl::~SyncEncryptionHandlerImpl() {} 67 SyncEncryptionHandlerImpl::~SyncEncryptionHandlerImpl() {}
68 68
69 void SyncEncryptionHandlerImpl::AddObserver(Observer* observer) { 69 void SyncEncryptionHandlerImpl::AddObserver(Observer* observer) {
70 DCHECK(thread_checker_.CalledOnValidThread()); 70 DCHECK(thread_checker_.CalledOnValidThread());
71 DCHECK(!observers_.HasObserver(observer)); 71 DCHECK(!observers_.HasObserver(observer));
72 observers_.AddObserver(observer); 72 observers_.AddObserver(observer);
73 } 73 }
(...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after
382 WriteEncryptionStateToNigori(&trans); 382 WriteEncryptionStateToNigori(&trans);
383 if (cryptographer_holder_.Get(trans.GetWrappedTrans()).is_ready()) 383 if (cryptographer_holder_.Get(trans.GetWrappedTrans()).is_ready())
384 ReEncryptEverything(&trans); 384 ReEncryptEverything(&trans);
385 } 385 }
386 386
387 bool SyncEncryptionHandlerImpl::EncryptEverythingEnabled() const { 387 bool SyncEncryptionHandlerImpl::EncryptEverythingEnabled() const {
388 DCHECK(thread_checker_.CalledOnValidThread()); 388 DCHECK(thread_checker_.CalledOnValidThread());
389 return encrypt_everything_; 389 return encrypt_everything_;
390 } 390 }
391 391
392 bool SyncEncryptionHandlerImpl::IsUsingExplicitPassphrase() const { 392 PassphraseState SyncEncryptionHandlerImpl::GetPassphraseState() const {
393 // TODO(zea): this is called from the UI thread, so we have to have a 393 DCHECK(thread_checker_.CalledOnValidThread());
394 // transaction while accessing it. Add an OnPassphraseTypeChanged observer 394 return passphrase_state_;
395 // and have the SBH cache the value on the UI thread.
396 ReadTransaction trans(FROM_HERE, user_share_);
397 return explicit_passphrase_;
398 } 395 }
399 396
400 // Note: this is called from within a syncable transaction, so we need to post 397 // Note: this is called from within a syncable transaction, so we need to post
401 // tasks if we want to do any work that creates a new sync_api transaction. 398 // tasks if we want to do any work that creates a new sync_api transaction.
402 void SyncEncryptionHandlerImpl::ApplyNigoriUpdate( 399 void SyncEncryptionHandlerImpl::ApplyNigoriUpdate(
403 const sync_pb::NigoriSpecifics& nigori, 400 const sync_pb::NigoriSpecifics& nigori,
404 syncable::BaseTransaction* const trans) { 401 syncable::BaseTransaction* const trans) {
405 DCHECK(thread_checker_.CalledOnValidThread()); 402 DCHECK(thread_checker_.CalledOnValidThread());
406 DCHECK(trans); 403 DCHECK(trans);
407 if (!ApplyNigoriUpdateImpl(nigori, trans)) { 404 if (!ApplyNigoriUpdateImpl(nigori, trans)) {
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
499 496
500 // NOTE: We notify from within a transaction. 497 // NOTE: We notify from within a transaction.
501 FOR_EACH_OBSERVER(SyncEncryptionHandler::Observer, observers_, 498 FOR_EACH_OBSERVER(SyncEncryptionHandler::Observer, observers_,
502 OnEncryptionComplete()); 499 OnEncryptionComplete());
503 } 500 }
504 501
505 bool SyncEncryptionHandlerImpl::ApplyNigoriUpdateImpl( 502 bool SyncEncryptionHandlerImpl::ApplyNigoriUpdateImpl(
506 const sync_pb::NigoriSpecifics& nigori, 503 const sync_pb::NigoriSpecifics& nigori,
507 syncable::BaseTransaction* const trans) { 504 syncable::BaseTransaction* const trans) {
508 DCHECK(thread_checker_.CalledOnValidThread()); 505 DCHECK(thread_checker_.CalledOnValidThread());
506 DVLOG(1) << "Applying nigori node update.";
509 bool nigori_types_need_update = !UpdateEncryptedTypesFromNigori(nigori, 507 bool nigori_types_need_update = !UpdateEncryptedTypesFromNigori(nigori,
510 trans); 508 trans);
511 if (nigori.using_explicit_passphrase()) 509 if (nigori.using_explicit_passphrase() &&
512 explicit_passphrase_ = true; 510 passphrase_state_ != CUSTOM_PASSPHRASE) {
511 passphrase_state_ = CUSTOM_PASSPHRASE;
512 FOR_EACH_OBSERVER(SyncEncryptionHandler::Observer, observers_,
513 OnPassphraseStateChanged(passphrase_state_));
514 }
513 515
514 Cryptographer* cryptographer = cryptographer_holder_.GetMutable(trans); 516 Cryptographer* cryptographer = cryptographer_holder_.GetMutable(trans);
515 bool nigori_needs_new_keys = false; 517 bool nigori_needs_new_keys = false;
516 if (!nigori.encrypted().blob().empty()) { 518 if (!nigori.encrypted().blob().empty()) {
517 if (cryptographer->CanDecrypt(nigori.encrypted())) { 519 if (cryptographer->CanDecrypt(nigori.encrypted())) {
518 cryptographer->InstallKeys(nigori.encrypted()); 520 cryptographer->InstallKeys(nigori.encrypted());
519 // We only update the default passphrase if this was a new explicit 521 // We only update the default passphrase if this was a new explicit
520 // passphrase. Else, since it was decryptable, it must not have been a new 522 // passphrase. Else, since it was decryptable, it must not have been a new
521 // key. 523 // key.
522 if (nigori.using_explicit_passphrase()) 524 if (nigori.using_explicit_passphrase())
(...skipping 26 matching lines...) Expand all
549 DVLOG(1) << "OnPassphraseRequired sent because cryptographer is not " 551 DVLOG(1) << "OnPassphraseRequired sent because cryptographer is not "
550 << "ready"; 552 << "ready";
551 FOR_EACH_OBSERVER(SyncEncryptionHandler::Observer, observers_, 553 FOR_EACH_OBSERVER(SyncEncryptionHandler::Observer, observers_,
552 OnPassphraseRequired(REASON_ENCRYPTION, 554 OnPassphraseRequired(REASON_ENCRYPTION,
553 sync_pb::EncryptedData())); 555 sync_pb::EncryptedData()));
554 } 556 }
555 557
556 // Check if the current local encryption state is stricter/newer than the 558 // Check if the current local encryption state is stricter/newer than the
557 // nigori state. If so, we need to overwrite the nigori node with the local 559 // nigori state. If so, we need to overwrite the nigori node with the local
558 // state. 560 // state.
559 if (nigori.using_explicit_passphrase() != explicit_passphrase_ || 561 if (nigori.using_explicit_passphrase() !=
562 (passphrase_state_ == CUSTOM_PASSPHRASE) ||
akalin 2012/08/22 20:10:39 might be clearer to have an intermediate variable
Nicolas Zea 2012/08/22 20:18:33 Done.
560 nigori.encrypt_everything() != encrypt_everything_ || 563 nigori.encrypt_everything() != encrypt_everything_ ||
561 nigori_types_need_update || 564 nigori_types_need_update ||
562 nigori_needs_new_keys) { 565 nigori_needs_new_keys) {
563 return false; 566 return false;
564 } 567 }
565 return true; 568 return true;
566 } 569 }
567 570
568 void SyncEncryptionHandlerImpl::RewriteNigori() { 571 void SyncEncryptionHandlerImpl::RewriteNigori() {
569 DVLOG(1) << "Overwriting stale nigori node."; 572 DVLOG(1) << "Overwriting stale nigori node.";
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
688 OnPassphraseRequired(REASON_DECRYPTION, 691 OnPassphraseRequired(REASON_DECRYPTION,
689 cryptographer.GetPendingKeys())); 692 cryptographer.GetPendingKeys()));
690 } else { 693 } else {
691 FOR_EACH_OBSERVER(SyncEncryptionHandler::Observer, observers_, 694 FOR_EACH_OBSERVER(SyncEncryptionHandler::Observer, observers_,
692 OnPassphraseRequired(REASON_ENCRYPTION, 695 OnPassphraseRequired(REASON_ENCRYPTION,
693 sync_pb::EncryptedData())); 696 sync_pb::EncryptedData()));
694 } 697 }
695 return; 698 return;
696 } 699 }
697 700
698 FOR_EACH_OBSERVER(SyncEncryptionHandler::Observer, observers_,
699 OnPassphraseAccepted());
700 DCHECK(cryptographer.is_ready()); 701 DCHECK(cryptographer.is_ready());
701 702
702 sync_pb::NigoriSpecifics specifics(nigori_node->GetNigoriSpecifics()); 703 sync_pb::NigoriSpecifics specifics(nigori_node->GetNigoriSpecifics());
703 // Does not modify specifics.encrypted() if the original decrypted data was 704 // Does not modify specifics.encrypted() if the original decrypted data was
704 // the same. 705 // the same.
705 if (!cryptographer.GetKeys(specifics.mutable_encrypted())) 706 if (!cryptographer.GetKeys(specifics.mutable_encrypted()))
706 NOTREACHED(); 707 NOTREACHED();
707 explicit_passphrase_ = is_explicit; 708 if (is_explicit && passphrase_state_ != CUSTOM_PASSPHRASE) {
709 passphrase_state_ = CUSTOM_PASSPHRASE;
710 FOR_EACH_OBSERVER(SyncEncryptionHandler::Observer, observers_,
711 OnPassphraseStateChanged(passphrase_state_));
712 }
708 specifics.set_using_explicit_passphrase(is_explicit); 713 specifics.set_using_explicit_passphrase(is_explicit);
709 nigori_node->SetNigoriSpecifics(specifics); 714 nigori_node->SetNigoriSpecifics(specifics);
710 715
716 // Must do this after OnPassphraseStateChanged, in order to ensure the PSS
717 // checks the passphrase state after it has been set.
718 FOR_EACH_OBSERVER(SyncEncryptionHandler::Observer, observers_,
719 OnPassphraseAccepted());
720
711 // Does nothing if everything is already encrypted. 721 // Does nothing if everything is already encrypted.
712 ReEncryptEverything(trans); 722 ReEncryptEverything(trans);
713 } 723 }
714 724
715 void SyncEncryptionHandlerImpl::MergeEncryptedTypes( 725 void SyncEncryptionHandlerImpl::MergeEncryptedTypes(
716 ModelTypeSet new_encrypted_types, 726 ModelTypeSet new_encrypted_types,
717 syncable::BaseTransaction* const trans) { 727 syncable::BaseTransaction* const trans) {
718 DCHECK(thread_checker_.CalledOnValidThread()); 728 DCHECK(thread_checker_.CalledOnValidThread());
719 ModelTypeSet* encrypted_types = encrypted_types_holder_.GetMutable(trans); 729 ModelTypeSet* encrypted_types = encrypted_types_holder_.GetMutable(trans);
720 if (!encrypted_types->HasAll(new_encrypted_types)) { 730 if (!encrypted_types->HasAll(new_encrypted_types)) {
721 *encrypted_types = new_encrypted_types; 731 *encrypted_types = new_encrypted_types;
722 FOR_EACH_OBSERVER( 732 FOR_EACH_OBSERVER(
723 Observer, observers_, 733 Observer, observers_,
724 OnEncryptedTypesChanged(*encrypted_types, encrypt_everything_)); 734 OnEncryptedTypesChanged(*encrypted_types, encrypt_everything_));
725 } 735 }
726 } 736 }
727 737
728 } // namespace browser_sync 738 } // namespace browser_sync
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698