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

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

Issue 10916036: [Sync] Implement keystore migration support. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Self review Created 8 years, 3 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 <string> 7 #include <string>
8 8
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop.h" 10 #include "base/message_loop.h"
11 #include "base/tracked_objects.h" 11 #include "base/tracked_objects.h"
12 #include "sync/internal_api/public/base/model_type_test_util.h" 12 #include "sync/internal_api/public/base/model_type_test_util.h"
13 #include "sync/internal_api/public/read_node.h" 13 #include "sync/internal_api/public/read_node.h"
14 #include "sync/internal_api/public/read_transaction.h" 14 #include "sync/internal_api/public/read_transaction.h"
15 #include "sync/internal_api/public/write_node.h"
15 #include "sync/internal_api/public/write_transaction.h" 16 #include "sync/internal_api/public/write_transaction.h"
16 #include "sync/internal_api/public/test/test_user_share.h" 17 #include "sync/internal_api/public/test/test_user_share.h"
17 #include "sync/protocol/nigori_specifics.pb.h" 18 #include "sync/protocol/nigori_specifics.pb.h"
18 #include "sync/protocol/sync.pb.h" 19 #include "sync/protocol/sync.pb.h"
19 #include "sync/syncable/entry.h" 20 #include "sync/syncable/entry.h"
20 #include "sync/syncable/mutable_entry.h" 21 #include "sync/syncable/mutable_entry.h"
21 #include "sync/syncable/write_transaction.h" 22 #include "sync/syncable/write_transaction.h"
22 #include "sync/test/engine/test_id_factory.h" 23 #include "sync/test/engine/test_id_factory.h"
23 #include "sync/test/fake_encryptor.h" 24 #include "sync/test/fake_encryptor.h"
24 #include "sync/util/cryptographer.h" 25 #include "sync/util/cryptographer.h"
25 #include "testing/gmock/include/gmock/gmock.h" 26 #include "testing/gmock/include/gmock/gmock.h"
26 #include "testing/gtest/include/gtest/gtest.h" 27 #include "testing/gtest/include/gtest/gtest.h"
27 28
28 namespace syncer { 29 namespace syncer {
29 30
30 namespace { 31 namespace {
31 32
32 using ::testing::_; 33 using ::testing::_;
33 using ::testing::Mock; 34 using ::testing::Mock;
34 using ::testing::StrictMock; 35 using ::testing::StrictMock;
35 36
37 static const char kKeystoreKey[] = "keystore_key";
38
36 class SyncEncryptionHandlerObserverMock 39 class SyncEncryptionHandlerObserverMock
37 : public SyncEncryptionHandler::Observer { 40 : public SyncEncryptionHandler::Observer {
38 public: 41 public:
39 MOCK_METHOD2(OnPassphraseRequired, 42 MOCK_METHOD2(OnPassphraseRequired,
40 void(PassphraseRequiredReason, 43 void(PassphraseRequiredReason,
41 const sync_pb::EncryptedData&)); // NOLINT 44 const sync_pb::EncryptedData&)); // NOLINT
42 MOCK_METHOD0(OnPassphraseAccepted, void()); // NOLINT 45 MOCK_METHOD0(OnPassphraseAccepted, void()); // NOLINT
43 MOCK_METHOD2(OnBootstrapTokenUpdated, 46 MOCK_METHOD2(OnBootstrapTokenUpdated,
44 void(const std::string&, BootstrapTokenType type)); // NOLINT 47 void(const std::string&, BootstrapTokenType type)); // NOLINT
45 MOCK_METHOD2(OnEncryptedTypesChanged, 48 MOCK_METHOD2(OnEncryptedTypesChanged,
(...skipping 10 matching lines...) Expand all
56 SyncEncryptionHandlerImplTest() {} 59 SyncEncryptionHandlerImplTest() {}
57 virtual ~SyncEncryptionHandlerImplTest() {} 60 virtual ~SyncEncryptionHandlerImplTest() {}
58 61
59 virtual void SetUp() { 62 virtual void SetUp() {
60 test_user_share_.SetUp(); 63 test_user_share_.SetUp();
61 SetUpEncryption(); 64 SetUpEncryption();
62 CreateRootForType(NIGORI); 65 CreateRootForType(NIGORI);
63 } 66 }
64 67
65 virtual void TearDown() { 68 virtual void TearDown() {
69 PumpLoop();
66 test_user_share_.TearDown(); 70 test_user_share_.TearDown();
67 } 71 }
68 72
69 protected: 73 protected:
70 void SetUpEncryption() { 74 void SetUpEncryption() {
71 encryption_handler_.reset( 75 encryption_handler_.reset(
72 new SyncEncryptionHandlerImpl(user_share(), 76 new SyncEncryptionHandlerImpl(user_share(),
73 &encryptor_, 77 &encryptor_,
74 "", "" /* bootstrap tokens */)); 78 "", "" /* bootstrap tokens */));
75 encryption_handler_->AddObserver(&observer_); 79 encryption_handler_->AddObserver(&observer_);
(...skipping 30 matching lines...) Expand all
106 // Getters for tests. 110 // Getters for tests.
107 UserShare* user_share() { return test_user_share_.user_share(); } 111 UserShare* user_share() { return test_user_share_.user_share(); }
108 SyncEncryptionHandlerImpl* encryption_handler() { 112 SyncEncryptionHandlerImpl* encryption_handler() {
109 return encryption_handler_.get(); 113 return encryption_handler_.get();
110 } 114 }
111 SyncEncryptionHandlerObserverMock* observer() { return &observer_; } 115 SyncEncryptionHandlerObserverMock* observer() { return &observer_; }
112 Cryptographer* GetCryptographer() { 116 Cryptographer* GetCryptographer() {
113 return encryption_handler_->GetCryptographerUnsafe(); 117 return encryption_handler_->GetCryptographerUnsafe();
114 } 118 }
115 119
120 void VerifyMigratedNigori(PassphraseState passphrase_state,
121 const std::string& passphrase) {
122 VerifyMigratedNigoriWithTimestamp(0, passphrase_state, passphrase);
123 }
124
125 void VerifyMigratedNigoriWithTimestamp(
126 int64 migration_time,
127 PassphraseState passphrase_state,
128 const std::string& passphrase) {
129 ReadTransaction trans(FROM_HERE, user_share());
130 ReadNode nigori_node(&trans);
131 ASSERT_EQ(nigori_node.InitByTagLookup(kNigoriTag), BaseNode::INIT_OK);
132 const sync_pb::NigoriSpecifics& nigori = nigori_node.GetNigoriSpecifics();
133 if (migration_time > 0)
134 EXPECT_EQ(migration_time, nigori.keystore_migration_time());
135 else
136 EXPECT_TRUE(nigori.has_keystore_migration_time());
137 EXPECT_TRUE(nigori.frozen_keybag());
138 if (passphrase_state == CUSTOM_PASSPHRASE ||
139 passphrase_state == FROZEN_IMPLICIT_PASSPHRASE) {
140 EXPECT_TRUE(nigori.encrypt_everything());
141 EXPECT_TRUE(nigori.keystore_bootstrap().blob().empty());
142 if (passphrase_state == CUSTOM_PASSPHRASE) {
143 EXPECT_EQ(sync_pb::NigoriSpecifics::CUSTOM_PASSPHRASE,
144 nigori.passphrase_state());
145 } else {
146 EXPECT_EQ(sync_pb::NigoriSpecifics::FROZEN_IMPLICIT_PASSPHRASE,
147 nigori.passphrase_state());
148 }
149 } else {
150 EXPECT_FALSE(nigori.encrypt_everything());
151 EXPECT_FALSE(nigori.keystore_bootstrap().blob().empty());
152 EXPECT_EQ(sync_pb::NigoriSpecifics::KEYSTORE_PASSPHRASE,
153 nigori.passphrase_state());
154 Cryptographer keystore_cryptographer(&encryptor_);
155 KeyParams params = {"localhost", "dummy", kKeystoreKey};
156 keystore_cryptographer.AddKey(params);
157 EXPECT_TRUE(keystore_cryptographer.CanDecryptUsingDefaultKey(
158 nigori.keystore_bootstrap()));
159 }
160
161 Cryptographer temp_cryptographer(&encryptor_);
162 KeyParams params = {"localhost", "dummy", passphrase};
163 temp_cryptographer.AddKey(params);
164 EXPECT_TRUE(temp_cryptographer.CanDecryptUsingDefaultKey(
165 nigori.encryption_keybag()));
166 }
167
116 protected: 168 protected:
117 TestUserShare test_user_share_; 169 TestUserShare test_user_share_;
118 FakeEncryptor encryptor_; 170 FakeEncryptor encryptor_;
119 scoped_ptr<SyncEncryptionHandlerImpl> encryption_handler_; 171 scoped_ptr<SyncEncryptionHandlerImpl> encryption_handler_;
120 StrictMock<SyncEncryptionHandlerObserverMock> observer_; 172 StrictMock<SyncEncryptionHandlerObserverMock> observer_;
121 TestIdFactory ids_; 173 TestIdFactory ids_;
122 MessageLoop message_loop_; 174 MessageLoop message_loop_;
123 }; 175 };
124 176
125 // Verify that the encrypted types are being written to and read from the 177 // Verify that the encrypted types are being written to and read from the
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after
352 404
353 // Set up the current encryption state (containing both keys and encrypt 405 // Set up the current encryption state (containing both keys and encrypt
354 // everything). 406 // everything).
355 sync_pb::NigoriSpecifics current_nigori_specifics; 407 sync_pb::NigoriSpecifics current_nigori_specifics;
356 GetCryptographer()->AddKey(old_key); 408 GetCryptographer()->AddKey(old_key);
357 GetCryptographer()->AddKey(current_key); 409 GetCryptographer()->AddKey(current_key);
358 GetCryptographer()->Encrypt( 410 GetCryptographer()->Encrypt(
359 our_encrypted_specifics, 411 our_encrypted_specifics,
360 our_encrypted_specifics.mutable_encrypted()); 412 our_encrypted_specifics.mutable_encrypted());
361 GetCryptographer()->GetKeys( 413 GetCryptographer()->GetKeys(
362 current_nigori_specifics.mutable_encrypted()); 414 current_nigori_specifics.mutable_encryption_keybag());
363 current_nigori_specifics.set_encrypt_everything(true); 415 current_nigori_specifics.set_encrypt_everything(true);
364 416
365 EXPECT_CALL(*observer(), OnCryptographerStateChanged(_)); 417 EXPECT_CALL(*observer(), OnCryptographerStateChanged(_));
366 EXPECT_CALL(*observer(), OnEncryptedTypesChanged( 418 EXPECT_CALL(*observer(), OnEncryptedTypesChanged(
367 HasModelTypes(ModelTypeSet::All()), true)); 419 HasModelTypes(ModelTypeSet::All()), true));
368 { 420 {
369 // Update the encryption handler. 421 // Update the encryption handler.
370 WriteTransaction trans(FROM_HERE, user_share()); 422 WriteTransaction trans(FROM_HERE, user_share());
371 encryption_handler()->ApplyNigoriUpdate( 423 encryption_handler()->ApplyNigoriUpdate(
372 current_nigori_specifics, 424 current_nigori_specifics,
373 trans.GetWrappedTrans()); 425 trans.GetWrappedTrans());
374 } 426 }
375 Mock::VerifyAndClearExpectations(observer()); 427 Mock::VerifyAndClearExpectations(observer());
376 428
377 // Now set up the old nigori specifics and apply it on top. 429 // Now set up the old nigori specifics and apply it on top.
378 // Has an old set of keys, and no encrypted types. 430 // Has an old set of keys, and no encrypted types.
379 sync_pb::NigoriSpecifics old_nigori; 431 sync_pb::NigoriSpecifics old_nigori;
380 other_cryptographer.GetKeys(old_nigori.mutable_encrypted()); 432 other_cryptographer.GetKeys(old_nigori.mutable_encryption_keybag());
381 433
382 EXPECT_CALL(*observer(), OnCryptographerStateChanged(_)); 434 EXPECT_CALL(*observer(), OnCryptographerStateChanged(_));
383 { 435 {
384 // Update the encryption handler. 436 // Update the encryption handler.
385 WriteTransaction trans(FROM_HERE, user_share()); 437 WriteTransaction trans(FROM_HERE, user_share());
386 encryption_handler()->ApplyNigoriUpdate( 438 encryption_handler()->ApplyNigoriUpdate(
387 old_nigori, 439 old_nigori,
388 trans.GetWrappedTrans()); 440 trans.GetWrappedTrans());
389 } 441 }
390 EXPECT_TRUE(GetCryptographer()->is_ready()); 442 EXPECT_TRUE(GetCryptographer()->is_ready());
391 EXPECT_FALSE(GetCryptographer()->has_pending_keys()); 443 EXPECT_FALSE(GetCryptographer()->has_pending_keys());
392 444
393 // Encryption handler should have posted a task to overwrite the old 445 // Encryption handler should have posted a task to overwrite the old
394 // specifics. 446 // specifics.
395 PumpLoop(); 447 PumpLoop();
396 448
397 { 449 {
398 // The cryptographer should be able to decrypt both sets of keys and still 450 // The cryptographer should be able to decrypt both sets of keys and still
399 // be encrypting with the newest, and the encrypted types should be the 451 // be encrypting with the newest, and the encrypted types should be the
400 // most recent. 452 // most recent.
401 // In addition, the nigori node should match the current encryption state. 453 // In addition, the nigori node should match the current encryption state.
402 ReadTransaction trans(FROM_HERE, user_share()); 454 ReadTransaction trans(FROM_HERE, user_share());
403 ReadNode nigori_node(&trans); 455 ReadNode nigori_node(&trans);
404 ASSERT_EQ(nigori_node.InitByTagLookup(ModelTypeToRootTag(NIGORI)), 456 ASSERT_EQ(nigori_node.InitByTagLookup(kNigoriTag), BaseNode::INIT_OK);
405 BaseNode::INIT_OK);
406 const sync_pb::NigoriSpecifics& nigori = nigori_node.GetNigoriSpecifics(); 457 const sync_pb::NigoriSpecifics& nigori = nigori_node.GetNigoriSpecifics();
407 EXPECT_TRUE(GetCryptographer()->CanDecryptUsingDefaultKey( 458 EXPECT_TRUE(GetCryptographer()->CanDecryptUsingDefaultKey(
408 our_encrypted_specifics.encrypted())); 459 our_encrypted_specifics.encrypted()));
409 EXPECT_TRUE(GetCryptographer()->CanDecrypt( 460 EXPECT_TRUE(GetCryptographer()->CanDecrypt(
410 other_encrypted_specifics.encrypted())); 461 other_encrypted_specifics.encrypted()));
411 EXPECT_TRUE(GetCryptographer()->CanDecrypt(nigori.encrypted())); 462 EXPECT_TRUE(GetCryptographer()->CanDecrypt(nigori.encryption_keybag()));
412 EXPECT_TRUE(nigori.encrypt_everything()); 463 EXPECT_TRUE(nigori.encrypt_everything());
413 EXPECT_TRUE( 464 EXPECT_TRUE(
414 GetCryptographer()->CanDecryptUsingDefaultKey(nigori.encrypted())); 465 GetCryptographer()->CanDecryptUsingDefaultKey(
466 nigori.encryption_keybag()));
415 } 467 }
416 EXPECT_TRUE(encryption_handler()->EncryptEverythingEnabled()); 468 EXPECT_TRUE(encryption_handler()->EncryptEverythingEnabled());
417 } 469 }
418 470
419 // Ensure setting the keystore key works, updates the bootstrap token, and 471 // Ensure setting the keystore key works, updates the bootstrap token, and
420 // doesn't modify the cryptographer. 472 // doesn't modify the cryptographer.
421 TEST_F(SyncEncryptionHandlerImplTest, SetKeystoreUpdatedBoostrapToken) { 473 TEST_F(SyncEncryptionHandlerImplTest, SetKeystoreUpdatedBoostrapToken) {
422 WriteTransaction trans(FROM_HERE, user_share()); 474 WriteTransaction trans(FROM_HERE, user_share());
423 EXPECT_CALL(*observer(), OnBootstrapTokenUpdated(_, _)).Times(0); 475 EXPECT_CALL(*observer(), OnBootstrapTokenUpdated(_, _)).Times(0);
424 EXPECT_FALSE(GetCryptographer()->is_initialized()); 476 EXPECT_FALSE(GetCryptographer()->is_initialized());
425 EXPECT_TRUE(encryption_handler()->NeedKeystoreKey(trans.GetWrappedTrans())); 477 EXPECT_TRUE(encryption_handler()->NeedKeystoreKey(trans.GetWrappedTrans()));
426 EXPECT_FALSE(encryption_handler()->SetKeystoreKey("", 478 EXPECT_FALSE(encryption_handler()->SetKeystoreKey("",
427 trans.GetWrappedTrans())); 479 trans.GetWrappedTrans()));
428 EXPECT_TRUE(encryption_handler()->NeedKeystoreKey(trans.GetWrappedTrans())); 480 EXPECT_TRUE(encryption_handler()->NeedKeystoreKey(trans.GetWrappedTrans()));
429 Mock::VerifyAndClearExpectations(observer()); 481 Mock::VerifyAndClearExpectations(observer());
430 482
431 const char kValidKey[] = "keystore_key"; 483 EXPECT_CALL(*observer(),
432 EXPECT_CALL(*observer(), 484 OnBootstrapTokenUpdated(kKeystoreKey, KEYSTORE_BOOTSTRAP_TOKEN));
433 OnBootstrapTokenUpdated(kValidKey, KEYSTORE_BOOTSTRAP_TOKEN)); 485 EXPECT_TRUE(encryption_handler()->SetKeystoreKey(kKeystoreKey,
434 EXPECT_TRUE(encryption_handler()->SetKeystoreKey(kValidKey,
435 trans.GetWrappedTrans())); 486 trans.GetWrappedTrans()));
436 EXPECT_FALSE(encryption_handler()->NeedKeystoreKey(trans.GetWrappedTrans())); 487 EXPECT_FALSE(encryption_handler()->NeedKeystoreKey(trans.GetWrappedTrans()));
437 EXPECT_FALSE(GetCryptographer()->is_initialized()); 488 EXPECT_FALSE(GetCryptographer()->is_initialized());
438 } 489 }
439 490
491 // Ensure GetKeystoreBootstrap only updates the keystore bootstrap if it
492 // wasn't already set properly. Otherwise, the bootstrap should remain the
493 // same.
494 TEST_F(SyncEncryptionHandlerImplTest, GetKeystoreBootstrap) {
495 const char kCurKey[] = "cur";
496 sync_pb::EncryptedData encrypted;
497 Cryptographer other_cryptographer(GetCryptographer()->encryptor());
498 KeyParams cur_key = {"localhost", "dummy", kCurKey};
499 other_cryptographer.AddKey(cur_key);
500 EXPECT_TRUE(other_cryptographer.is_ready());
501 EXPECT_TRUE(encryption_handler()->GetKeystoreBootstrapKey(
502 other_cryptographer,
503 kKeystoreKey,
504 &encrypted));
505 std::string serialized = encrypted.SerializeAsString();
506 EXPECT_TRUE(encryption_handler()->GetKeystoreBootstrapKey(
507 other_cryptographer,
508 kKeystoreKey,
509 &encrypted));
510 EXPECT_EQ(serialized, encrypted.SerializeAsString());
511 }
512
513 // Test that we don't attempt to migrate while an implicit passphrase is pending
514 // and that once we do decrypt pending keys we migrate the nigori. Once
515 // migrated, we should be in keystore passphrase state.
516 TEST_F(SyncEncryptionHandlerImplTest, MigrateOnDecryptImplicitPass) {
517 const char kOtherKey[] = "other";
518 {
519 EXPECT_CALL(*observer(),
520 OnBootstrapTokenUpdated(_, KEYSTORE_BOOTSTRAP_TOKEN));
521 ReadTransaction trans(FROM_HERE, user_share());
522 encryption_handler()->SetKeystoreKey(kKeystoreKey, trans.GetWrappedTrans());
523 Mock::VerifyAndClearExpectations(observer());
524 }
525 EXPECT_FALSE(encryption_handler()->MigratedToKeystore());
526
527 {
528 WriteTransaction trans(FROM_HERE, user_share());
529 WriteNode nigori_node(&trans);
530 ASSERT_EQ(nigori_node.InitByTagLookup(kNigoriTag), BaseNode::INIT_OK);
531 Cryptographer other_cryptographer(GetCryptographer()->encryptor());
532 KeyParams other_key = {"localhost", "dummy", kOtherKey};
533 other_cryptographer.AddKey(other_key);
534
535 sync_pb::NigoriSpecifics nigori;
536 other_cryptographer.GetKeys(nigori.mutable_encryption_keybag());
537 nigori.set_frozen_keybag(false);
538 nigori.set_encrypt_everything(false);
539 EXPECT_CALL(*observer(),
540 OnCryptographerStateChanged(_));
541 EXPECT_CALL(*observer(),
542 OnPassphraseRequired(_, _));
543 encryption_handler()->ApplyNigoriUpdate(nigori, trans.GetWrappedTrans());
544 nigori_node.SetNigoriSpecifics(nigori);
545 }
546 // Run any tasks posted via AppplyNigoriUpdate.
547 PumpLoop();
548 EXPECT_FALSE(encryption_handler()->MigratedToKeystore());
549 Mock::VerifyAndClearExpectations(observer());
550
551 EXPECT_CALL(*observer(),
552 OnCryptographerStateChanged(_));
553 EXPECT_CALL(*observer(),
554 OnPassphraseAccepted());
555 EXPECT_CALL(*observer(),
556 OnPassphraseStateChanged(KEYSTORE_PASSPHRASE));
557 EXPECT_CALL(*observer(),
558 OnBootstrapTokenUpdated(_, PASSPHRASE_BOOTSTRAP_TOKEN));
559 EXPECT_CALL(*observer(),
560 OnEncryptionComplete());
561 EXPECT_FALSE(encryption_handler()->MigratedToKeystore());
562 encryption_handler()->SetDecryptionPassphrase(kOtherKey);
563 EXPECT_TRUE(encryption_handler()->MigratedToKeystore());
564 EXPECT_EQ(KEYSTORE_PASSPHRASE, encryption_handler()->GetPassphraseState());
565 VerifyMigratedNigori(KEYSTORE_PASSPHRASE, kOtherKey);
566 }
567
568 // Test that we don't attempt to migrate while a custom passphrase is pending,
569 // and that once we do decrypt pending keys we migrate the nigori. Once
570 // migrated, we should be in custom passphrase state with encrypt everything.
571 TEST_F(SyncEncryptionHandlerImplTest, MigrateOnDecryptCustomPass) {
572 const char kOtherKey[] = "other";
573 {
574 EXPECT_CALL(*observer(),
575 OnBootstrapTokenUpdated(_, KEYSTORE_BOOTSTRAP_TOKEN));
576 ReadTransaction trans(FROM_HERE, user_share());
577 encryption_handler()->SetKeystoreKey(kKeystoreKey, trans.GetWrappedTrans());
578 Mock::VerifyAndClearExpectations(observer());
579 }
580 EXPECT_FALSE(encryption_handler()->MigratedToKeystore());
581
582 {
583 WriteTransaction trans(FROM_HERE, user_share());
584 WriteNode nigori_node(&trans);
585 ASSERT_EQ(nigori_node.InitByTagLookup(kNigoriTag), BaseNode::INIT_OK);
586 Cryptographer other_cryptographer(GetCryptographer()->encryptor());
587 KeyParams other_key = {"localhost", "dummy", kOtherKey};
588 other_cryptographer.AddKey(other_key);
589
590 sync_pb::NigoriSpecifics nigori;
591 other_cryptographer.GetKeys(nigori.mutable_encryption_keybag());
592 nigori.set_frozen_keybag(true);
593 nigori.set_encrypt_everything(false);
594 EXPECT_CALL(*observer(),
595 OnCryptographerStateChanged(_));
596 EXPECT_CALL(*observer(),
597 OnPassphraseRequired(_, _));
598 EXPECT_CALL(*observer(),
599 OnPassphraseStateChanged(CUSTOM_PASSPHRASE));
600 encryption_handler()->ApplyNigoriUpdate(nigori, trans.GetWrappedTrans());
601 nigori_node.SetNigoriSpecifics(nigori);
602 }
603 // Run any tasks posted via AppplyNigoriUpdate.
604 PumpLoop();
605 EXPECT_FALSE(encryption_handler()->MigratedToKeystore());
606 Mock::VerifyAndClearExpectations(observer());
607
608 EXPECT_CALL(*observer(),
609 OnCryptographerStateChanged(_));
610 EXPECT_CALL(*observer(),
611 OnPassphraseAccepted());
612 EXPECT_CALL(*observer(),
613 OnBootstrapTokenUpdated(_, PASSPHRASE_BOOTSTRAP_TOKEN));
614 EXPECT_CALL(*observer(),
615 OnEncryptedTypesChanged(_, true));
616 EXPECT_CALL(*observer(),
617 OnEncryptionComplete()).Times(2);
618 EXPECT_FALSE(encryption_handler()->MigratedToKeystore());
619 encryption_handler()->SetDecryptionPassphrase(kOtherKey);
620 EXPECT_TRUE(encryption_handler()->MigratedToKeystore());
621 EXPECT_EQ(CUSTOM_PASSPHRASE, encryption_handler()->GetPassphraseState());
622 VerifyMigratedNigori(CUSTOM_PASSPHRASE, kOtherKey);
623 }
624
625 // Test that we trigger a migration when we set the keystore key, had an
626 // implicit passphrase, and did not have encrypt everything. We should switch
627 // to KEYSTORE_PASSPHRASE.
628 TEST_F(SyncEncryptionHandlerImplTest, MigrateOnKeystoreKeyAvailableImplicit) {
629 const char kCurKey[] = "cur";
630 KeyParams current_key = {"localhost", "dummy", kCurKey};
631 GetCryptographer()->AddKey(current_key);
632 EXPECT_CALL(*observer(),
633 OnCryptographerStateChanged(_));
634 EXPECT_CALL(*observer(),
635 OnEncryptedTypesChanged(_, false));
636 EXPECT_CALL(*observer(),
637 OnEncryptionComplete());
638 encryption_handler()->Init();
639 Mock::VerifyAndClearExpectations(observer());
640
641 {
642 ReadTransaction trans(FROM_HERE, user_share());
643 // Once we provide a keystore key, we should perform the migration.
644 EXPECT_CALL(*observer(),
645 OnBootstrapTokenUpdated(_, KEYSTORE_BOOTSTRAP_TOKEN));
646 encryption_handler()->SetKeystoreKey(kKeystoreKey, trans.GetWrappedTrans());
647 }
648 EXPECT_CALL(*observer(),
649 OnPassphraseStateChanged(KEYSTORE_PASSPHRASE));
650 // The actual migration gets posted, so run all pending tasks.
651 PumpLoop();
652 EXPECT_TRUE(encryption_handler()->MigratedToKeystore());
653 EXPECT_EQ(KEYSTORE_PASSPHRASE,
654 encryption_handler()->GetPassphraseState());
655 EXPECT_FALSE(encryption_handler()->EncryptEverythingEnabled());
656 VerifyMigratedNigori(KEYSTORE_PASSPHRASE, kCurKey);
657 }
658
659 // Test that we trigger a migration when we set the keystore key, had an
660 // implicit passphrase, and encrypt everything enabled. We should switch to
661 // FROZEN_IMPLICIT_PASSPHRASE.
662 TEST_F(SyncEncryptionHandlerImplTest,
663 MigrateOnKeystoreKeyAvailableFrozenImplicit) {
664 const char kCurKey[] = "cur";
665 KeyParams current_key = {"localhost", "dummy", kCurKey};
666 GetCryptographer()->AddKey(current_key);
667 EXPECT_CALL(*observer(),
668 OnCryptographerStateChanged(_));
669 EXPECT_CALL(*observer(),
670 OnEncryptedTypesChanged(_, false));
671 EXPECT_CALL(*observer(),
672 OnEncryptionComplete());
673 encryption_handler()->Init();
674 Mock::VerifyAndClearExpectations(observer());
675
676 EXPECT_CALL(*observer(),
677 OnEncryptedTypesChanged(_, true));
678 EXPECT_CALL(*observer(),
679 OnEncryptionComplete());
680 encryption_handler()->EnableEncryptEverything();
681
682 {
683 ReadTransaction trans(FROM_HERE, user_share());
684 // Once we provide a keystore key, we should perform the migration.
685 EXPECT_CALL(*observer(),
686 OnBootstrapTokenUpdated(_, KEYSTORE_BOOTSTRAP_TOKEN));
687 encryption_handler()->SetKeystoreKey(kKeystoreKey, trans.GetWrappedTrans());
688 }
689 EXPECT_CALL(*observer(),
690 OnPassphraseStateChanged(FROZEN_IMPLICIT_PASSPHRASE));
691 // The actual migration gets posted, so run all pending tasks.
692 PumpLoop();
693 EXPECT_TRUE(encryption_handler()->MigratedToKeystore());
694 EXPECT_EQ(FROZEN_IMPLICIT_PASSPHRASE,
695 encryption_handler()->GetPassphraseState());
696 EXPECT_TRUE(encryption_handler()->EncryptEverythingEnabled());
697 VerifyMigratedNigori(FROZEN_IMPLICIT_PASSPHRASE, kCurKey);
698 }
699
700 // Test that we trigger a migration when we set the keystore key, had a
701 // custom passphrase, and encrypt everything enabled. The passphrase state
702 // should remain as CUSTOM_PASSPHRASE, and encrypt everything stay the same.
703 TEST_F(SyncEncryptionHandlerImplTest,
704 MigrateOnKeystoreKeyAvailableCustomWithEncryption) {
705 const char kCurKey[] = "cur";
706 EXPECT_CALL(*observer(),
707 OnCryptographerStateChanged(_)).Times(2);
708 EXPECT_CALL(*observer(),
709 OnPassphraseRequired(_, _));
710 EXPECT_CALL(*observer(),
711 OnPassphraseAccepted());
712 EXPECT_CALL(*observer(),
713 OnEncryptedTypesChanged(_, false));
714 EXPECT_CALL(*observer(),
715 OnEncryptionComplete());
716 EXPECT_CALL(*observer(),
717 OnPassphraseStateChanged(CUSTOM_PASSPHRASE));
718 EXPECT_CALL(*observer(),
719 OnBootstrapTokenUpdated(_, PASSPHRASE_BOOTSTRAP_TOKEN));
720 encryption_handler()->Init();
721 encryption_handler()->SetEncryptionPassphrase(kCurKey, true);
722 Mock::VerifyAndClearExpectations(observer());
723
724 EXPECT_CALL(*observer(),
725 OnEncryptedTypesChanged(_, true));
726 EXPECT_CALL(*observer(),
727 OnEncryptionComplete());
728 encryption_handler()->EnableEncryptEverything();
729 Mock::VerifyAndClearExpectations(observer());
730
731 {
732 ReadTransaction trans(FROM_HERE, user_share());
733 // Once we provide a keystore key, we should perform the migration.
734 EXPECT_CALL(*observer(),
735 OnBootstrapTokenUpdated(_, KEYSTORE_BOOTSTRAP_TOKEN));
736 encryption_handler()->SetKeystoreKey(kKeystoreKey, trans.GetWrappedTrans());
737 }
738 // The actual migration gets posted, so run all pending tasks.
739 PumpLoop();
740 EXPECT_TRUE(encryption_handler()->MigratedToKeystore());
741 EXPECT_EQ(CUSTOM_PASSPHRASE,
742 encryption_handler()->GetPassphraseState());
743 EXPECT_TRUE(encryption_handler()->EncryptEverythingEnabled());
744 VerifyMigratedNigori(CUSTOM_PASSPHRASE, kCurKey);
745 }
746
747 // Test that we trigger a migration when we set the keystore key, had a
748 // custom passphrase, and did not have encrypt everything. The passphrase state
749 // should remain as CUSTOM_PASSPHRASE, and encrypt everything should be enabled.
750 TEST_F(SyncEncryptionHandlerImplTest,
751 MigrateOnKeystoreKeyAvailableCustomNoEncryption) {
752 const char kCurKey[] = "cur";
753 EXPECT_CALL(*observer(),
754 OnCryptographerStateChanged(_)).Times(2);
755 EXPECT_CALL(*observer(),
756 OnPassphraseRequired(_, _));
757 EXPECT_CALL(*observer(),
758 OnPassphraseAccepted());
759 EXPECT_CALL(*observer(),
760 OnEncryptedTypesChanged(_, false));
761 EXPECT_CALL(*observer(),
762 OnEncryptionComplete());
763 EXPECT_CALL(*observer(),
764 OnPassphraseStateChanged(CUSTOM_PASSPHRASE));
765 EXPECT_CALL(*observer(),
766 OnBootstrapTokenUpdated(_, PASSPHRASE_BOOTSTRAP_TOKEN));
767 encryption_handler()->Init();
768 encryption_handler()->SetEncryptionPassphrase(kCurKey, true);
769 Mock::VerifyAndClearExpectations(observer());
770
771 {
772 ReadTransaction trans(FROM_HERE, user_share());
773 // Once we provide a keystore key, we should perform the migration.
774 EXPECT_CALL(*observer(),
775 OnBootstrapTokenUpdated(_, KEYSTORE_BOOTSTRAP_TOKEN));
776 encryption_handler()->SetKeystoreKey(kKeystoreKey, trans.GetWrappedTrans());
777 }
778 EXPECT_CALL(*observer(),
779 OnEncryptedTypesChanged(_, true));
780 EXPECT_CALL(*observer(),
781 OnEncryptionComplete());
782 // The actual migration gets posted, so run all pending tasks.
783 PumpLoop();
784 EXPECT_TRUE(encryption_handler()->MigratedToKeystore());
785 EXPECT_EQ(CUSTOM_PASSPHRASE,
786 encryption_handler()->GetPassphraseState());
787 EXPECT_TRUE(encryption_handler()->EncryptEverythingEnabled());
788 VerifyMigratedNigori(CUSTOM_PASSPHRASE, kCurKey);
789 }
790
791 // Test that we can handle receiving a migrated nigori node in the
792 // KEYSTORE_PASS state, and use the keystore bootstrap to decrypt the keybag.
793 TEST_F(SyncEncryptionHandlerImplTest, ReceiveMigratedNigoriKeystorePass) {
794 const char kCurKey[] = "cur";
795 sync_pb::EncryptedData encrypted;
796 Cryptographer other_cryptographer(GetCryptographer()->encryptor());
797 KeyParams cur_key = {"localhost", "dummy", kCurKey};
798 other_cryptographer.AddKey(cur_key);
799 EXPECT_TRUE(other_cryptographer.is_ready());
800 EXPECT_TRUE(encryption_handler()->GetKeystoreBootstrapKey(
801 other_cryptographer,
802 kKeystoreKey,
803 &encrypted));
804 EXPECT_FALSE(encryption_handler()->MigratedToKeystore());
805 EXPECT_FALSE(GetCryptographer()->is_ready());
806 EXPECT_NE(encryption_handler()->GetPassphraseState(), KEYSTORE_PASSPHRASE);
807
808 // Now build a nigori node with the generated keystore bootstrap token and
809 // initialize the encryption handler with it. The cryptographer should be
810 // bootstrapped properly to decrypt both kCurKey and kKeystoreKey.
811 {
812 WriteTransaction trans(FROM_HERE, user_share());
813 WriteNode nigori_node(&trans);
814 ASSERT_EQ(nigori_node.InitByTagLookup(kNigoriTag), BaseNode::INIT_OK);
815 sync_pb::NigoriSpecifics nigori;
816 nigori.mutable_keystore_bootstrap()->CopyFrom(encrypted);
817 other_cryptographer.GetKeys(nigori.mutable_encryption_keybag());
818 nigori.set_frozen_keybag(true);
819 nigori.set_keystore_migration_time(1);
820 nigori.set_passphrase_state(sync_pb::NigoriSpecifics::KEYSTORE_PASSPHRASE);
821
822 EXPECT_CALL(*observer(),
823 OnBootstrapTokenUpdated(_, PASSPHRASE_BOOTSTRAP_TOKEN));
824 EXPECT_CALL(*observer(),
825 OnBootstrapTokenUpdated(_, KEYSTORE_BOOTSTRAP_TOKEN));
826 EXPECT_CALL(*observer(),
827 OnPassphraseStateChanged(KEYSTORE_PASSPHRASE));
828 EXPECT_CALL(*observer(),
829 OnCryptographerStateChanged(_)).Times(2);
830 encryption_handler()->SetKeystoreKey(kKeystoreKey, trans.GetWrappedTrans());
831 encryption_handler()->ApplyNigoriUpdate(nigori, trans.GetWrappedTrans());
832 nigori_node.SetNigoriSpecifics(nigori);
833 }
834 // Run any tasks posted via AppplyNigoriUpdate.
835 PumpLoop();
836 Mock::VerifyAndClearExpectations(observer());
837
838 EXPECT_TRUE(encryption_handler()->MigratedToKeystore());
839 EXPECT_TRUE(GetCryptographer()->is_ready());
840 EXPECT_EQ(encryption_handler()->GetPassphraseState(), KEYSTORE_PASSPHRASE);
841 EXPECT_FALSE(encryption_handler()->EncryptEverythingEnabled());
842 VerifyMigratedNigoriWithTimestamp(1, KEYSTORE_PASSPHRASE, kCurKey);
843
844 // Check that the cryptographer still encrypts with the current key.
845 sync_pb::EncryptedData current_encrypted;
846 other_cryptographer.EncryptString("string", &current_encrypted);
847 EXPECT_TRUE(GetCryptographer()->CanDecryptUsingDefaultKey(current_encrypted));
848
849 // Check that the cryptographer can decrypt keystore key based encryption.
850 Cryptographer keystore_cryptographer(GetCryptographer()->encryptor());
851 KeyParams keystore_key = {"localhost", "dummy", kKeystoreKey};
852 keystore_cryptographer.AddKey(keystore_key);
853 sync_pb::EncryptedData keystore_encrypted;
854 keystore_cryptographer.EncryptString("string", &keystore_encrypted);
855 EXPECT_TRUE(GetCryptographer()->CanDecrypt(keystore_encrypted));
856 }
857
858 // Test that we handle receiving migrated nigori's with
859 // FROZEN_IMPLICIT_PASSPHRASE state. We should be in a pending key state until
860 // we supply the pending frozen implicit passphrase key.
861 TEST_F(SyncEncryptionHandlerImplTest, ReceiveMigratedNigoriFrozenImplicitPass) {
862 const char kCurKey[] = "cur";
863 sync_pb::EncryptedData encrypted;
864 Cryptographer other_cryptographer(GetCryptographer()->encryptor());
865 KeyParams cur_key = {"localhost", "dummy", kCurKey};
866 other_cryptographer.AddKey(cur_key);
867 EXPECT_FALSE(encryption_handler()->MigratedToKeystore());
868
869 {
870 EXPECT_CALL(*observer(),
871 OnBootstrapTokenUpdated(_, KEYSTORE_BOOTSTRAP_TOKEN));
872 ReadTransaction trans(FROM_HERE, user_share());
873 encryption_handler()->SetKeystoreKey(kKeystoreKey, trans.GetWrappedTrans());
874 }
875 EXPECT_FALSE(encryption_handler()->MigratedToKeystore());
876
877 {
878 EXPECT_CALL(*observer(),
879 OnPassphraseStateChanged(FROZEN_IMPLICIT_PASSPHRASE));
880 EXPECT_CALL(*observer(),
881 OnPassphraseRequired(_, _));
882 EXPECT_CALL(*observer(),
883 OnCryptographerStateChanged(_));
884 EXPECT_CALL(*observer(),
885 OnEncryptedTypesChanged(_, true));
886 WriteTransaction trans(FROM_HERE, user_share());
887 WriteNode nigori_node(&trans);
888 ASSERT_EQ(nigori_node.InitByTagLookup(kNigoriTag), BaseNode::INIT_OK);
889 sync_pb::NigoriSpecifics nigori;
890 nigori.set_frozen_keybag(true);
891 nigori.set_passphrase_state(
892 sync_pb::NigoriSpecifics::FROZEN_IMPLICIT_PASSPHRASE);
893 nigori.set_keystore_migration_time(1);
894 nigori.set_encrypt_everything(true);
895 other_cryptographer.GetKeys(nigori.mutable_encryption_keybag());
896 encryption_handler()->ApplyNigoriUpdate(nigori, trans.GetWrappedTrans());
897 nigori_node.SetNigoriSpecifics(nigori);
898 }
899 // Run any tasks posted via AppplyNigoriUpdate.
900 PumpLoop();
901 Mock::VerifyAndClearExpectations(observer());
902
903 EXPECT_TRUE(encryption_handler()->MigratedToKeystore());
904 EXPECT_EQ(FROZEN_IMPLICIT_PASSPHRASE,
905 encryption_handler()->GetPassphraseState());
906 EXPECT_TRUE(GetCryptographer()->has_pending_keys());
907 EXPECT_TRUE(encryption_handler()->EncryptEverythingEnabled());
908
909 EXPECT_CALL(*observer(),
910 OnBootstrapTokenUpdated(_, PASSPHRASE_BOOTSTRAP_TOKEN));
911 EXPECT_CALL(*observer(),
912 OnCryptographerStateChanged(_));
913 EXPECT_CALL(*observer(),
914 OnEncryptionComplete());
915 EXPECT_CALL(*observer(),
916 OnPassphraseAccepted());
917 encryption_handler()->SetDecryptionPassphrase(kCurKey);
918 EXPECT_TRUE(encryption_handler()->MigratedToKeystore());
919 EXPECT_TRUE(GetCryptographer()->is_ready());
920 VerifyMigratedNigoriWithTimestamp(1, FROZEN_IMPLICIT_PASSPHRASE, kCurKey);
921
922 // Check that the cryptographer still encrypts with the current key.
923 sync_pb::EncryptedData current_encrypted;
924 other_cryptographer.EncryptString("string", &current_encrypted);
925 EXPECT_TRUE(GetCryptographer()->CanDecryptUsingDefaultKey(current_encrypted));
926
927 // Check that the cryptographer can decrypt keystore key based encryption.
928 Cryptographer keystore_cryptographer(GetCryptographer()->encryptor());
929 KeyParams keystore_key = {"localhost", "dummy", kKeystoreKey};
930 keystore_cryptographer.AddKey(keystore_key);
931 sync_pb::EncryptedData keystore_encrypted;
932 keystore_cryptographer.EncryptString("string", &keystore_encrypted);
933 EXPECT_TRUE(GetCryptographer()->CanDecrypt(keystore_encrypted));
934 }
935
936 // Test that we handle receiving migrated nigori's with
937 // CUSTOM_PASSPHRASE state. We should be in a pending key state until we
938 // provide the custom passphrase key.
939 TEST_F(SyncEncryptionHandlerImplTest, ReceiveMigratedNigoriCustomPass) {
940 const char kKeystoreKey[] = "keystore_key";
941 const char kCurKey[] = "cur";
942 sync_pb::EncryptedData encrypted;
943 Cryptographer other_cryptographer(GetCryptographer()->encryptor());
944 KeyParams cur_key = {"localhost", "dummy", kCurKey};
945 other_cryptographer.AddKey(cur_key);
946 EXPECT_FALSE(encryption_handler()->MigratedToKeystore());
947
948 {
949 EXPECT_CALL(*observer(),
950 OnBootstrapTokenUpdated(_, KEYSTORE_BOOTSTRAP_TOKEN));
951 ReadTransaction trans(FROM_HERE, user_share());
952 encryption_handler()->SetKeystoreKey(kKeystoreKey,
953 trans.GetWrappedTrans());
954 }
955 EXPECT_FALSE(encryption_handler()->MigratedToKeystore());
956
957 {
958 EXPECT_CALL(*observer(),
959 OnPassphraseStateChanged(CUSTOM_PASSPHRASE));
960 EXPECT_CALL(*observer(),
961 OnPassphraseRequired(_, _));
962 EXPECT_CALL(*observer(),
963 OnCryptographerStateChanged(_));
964 EXPECT_CALL(*observer(),
965 OnEncryptedTypesChanged(_, true));
966 WriteTransaction trans(FROM_HERE, user_share());
967 WriteNode nigori_node(&trans);
968 ASSERT_EQ(nigori_node.InitByTagLookup(kNigoriTag), BaseNode::INIT_OK);
969 sync_pb::NigoriSpecifics nigori;
970 nigori.set_frozen_keybag(true);
971 nigori.set_passphrase_state(sync_pb::NigoriSpecifics::CUSTOM_PASSPHRASE);
972 nigori.set_keystore_migration_time(1);
973 nigori.set_encrypt_everything(true);
974 other_cryptographer.GetKeys(nigori.mutable_encryption_keybag());
975 encryption_handler()->ApplyNigoriUpdate(nigori, trans.GetWrappedTrans());
976 nigori_node.SetNigoriSpecifics(nigori);
977 }
978 // Run any tasks posted via AppplyNigoriUpdate.
979 PumpLoop();
980 Mock::VerifyAndClearExpectations(observer());
981
982 EXPECT_TRUE(encryption_handler()->MigratedToKeystore());
983 EXPECT_EQ(CUSTOM_PASSPHRASE, encryption_handler()->GetPassphraseState());
984 EXPECT_TRUE(GetCryptographer()->has_pending_keys());
985 EXPECT_TRUE(encryption_handler()->EncryptEverythingEnabled());
986
987 EXPECT_CALL(*observer(),
988 OnBootstrapTokenUpdated(_, PASSPHRASE_BOOTSTRAP_TOKEN));
989 EXPECT_CALL(*observer(),
990 OnCryptographerStateChanged(_));
991 EXPECT_CALL(*observer(),
992 OnEncryptionComplete());
993 EXPECT_CALL(*observer(),
994 OnPassphraseAccepted());
995 encryption_handler()->SetDecryptionPassphrase(kCurKey);
996 EXPECT_TRUE(encryption_handler()->MigratedToKeystore());
997 EXPECT_TRUE(GetCryptographer()->is_ready());
998 VerifyMigratedNigoriWithTimestamp(1, CUSTOM_PASSPHRASE, kCurKey);
999
1000 // Check that the cryptographer still encrypts with the current key.
1001 sync_pb::EncryptedData current_encrypted;
1002 other_cryptographer.EncryptString("string", &current_encrypted);
1003 EXPECT_TRUE(GetCryptographer()->CanDecryptUsingDefaultKey(current_encrypted));
1004
1005 // Check that the cryptographer can decrypt keystore key based encryption.
1006 Cryptographer keystore_cryptographer(GetCryptographer()->encryptor());
1007 KeyParams keystore_key = {"localhost", "dummy", kKeystoreKey};
1008 keystore_cryptographer.AddKey(keystore_key);
1009 sync_pb::EncryptedData keystore_encrypted;
1010 keystore_cryptographer.EncryptString("string", &keystore_encrypted);
1011 EXPECT_TRUE(GetCryptographer()->CanDecrypt(keystore_encrypted));
1012 }
1013
1014 // Test that if we have a migrated nigori with a custom passphrase, then receive
1015 // and old implicit passphrase nigori, we properly overwrite it with the current
1016 // state.
1017 TEST_F(SyncEncryptionHandlerImplTest, ReceiveUnmigratedNigoriAfterMigration) {
1018 const char kOldKey[] = "old";
1019 const char kCurKey[] = "cur";
1020 sync_pb::EncryptedData encrypted;
1021 KeyParams old_key = {"localhost", "dummy", kOldKey};
1022 KeyParams cur_key = {"localhost", "dummy", kCurKey};
1023 GetCryptographer()->AddKey(old_key);
1024 GetCryptographer()->AddKey(cur_key);
1025
1026 // Build a migrated nigori with full encryption.
1027 {
1028 WriteTransaction trans(FROM_HERE, user_share());
1029 WriteNode nigori_node(&trans);
1030 ASSERT_EQ(nigori_node.InitByTagLookup(kNigoriTag), BaseNode::INIT_OK);
1031 sync_pb::NigoriSpecifics nigori;
1032 GetCryptographer()->GetKeys(nigori.mutable_encryption_keybag());
1033 nigori.set_frozen_keybag(true);
1034 nigori.set_keystore_migration_time(1);
1035 nigori.set_passphrase_state(sync_pb::NigoriSpecifics::CUSTOM_PASSPHRASE);
1036 nigori.set_encrypt_everything(true);
1037 nigori_node.SetNigoriSpecifics(nigori);
1038 }
1039
1040 EXPECT_CALL(*observer(),
1041 OnPassphraseStateChanged(CUSTOM_PASSPHRASE));
1042 EXPECT_CALL(*observer(),
1043 OnCryptographerStateChanged(_));
1044 EXPECT_CALL(*observer(),
1045 OnEncryptedTypesChanged(_, true)).Times(2);
1046 EXPECT_CALL(*observer(),
1047 OnEncryptionComplete());
1048 encryption_handler()->Init();
1049 EXPECT_TRUE(encryption_handler()->MigratedToKeystore());
1050 EXPECT_TRUE(GetCryptographer()->is_ready());
1051 EXPECT_EQ(encryption_handler()->GetPassphraseState(), CUSTOM_PASSPHRASE);
1052 EXPECT_TRUE(encryption_handler()->EncryptEverythingEnabled());
1053 VerifyMigratedNigoriWithTimestamp(1, CUSTOM_PASSPHRASE, kCurKey);
1054
1055 {
1056 EXPECT_CALL(*observer(),
1057 OnBootstrapTokenUpdated(_, KEYSTORE_BOOTSTRAP_TOKEN));
1058 ReadTransaction trans(FROM_HERE, user_share());
1059 encryption_handler()->SetKeystoreKey(kKeystoreKey, trans.GetWrappedTrans());
1060 }
1061 Mock::VerifyAndClearExpectations(observer());
1062
1063 // Now build an old unmigrated nigori node with old encrypted types. We should
1064 // properly overwrite it with the migrated + encrypt everything state.
1065 EXPECT_CALL(*observer(),
1066 OnCryptographerStateChanged(_));
1067 {
1068 Cryptographer other_cryptographer(GetCryptographer()->encryptor());
1069 other_cryptographer.AddKey(old_key);
1070 WriteTransaction trans(FROM_HERE, user_share());
1071 WriteNode nigori_node(&trans);
1072 ASSERT_EQ(nigori_node.InitByTagLookup(kNigoriTag), BaseNode::INIT_OK);
1073 sync_pb::NigoriSpecifics nigori;
1074 other_cryptographer.GetKeys(nigori.mutable_encryption_keybag());
1075 nigori.set_frozen_keybag(false);
1076 nigori.set_encrypt_everything(false);
1077 encryption_handler()->ApplyNigoriUpdate(nigori, trans.GetWrappedTrans());
1078 nigori_node.SetNigoriSpecifics(nigori);
1079 }
1080 PumpLoop();
1081
1082 // Verify we're still migrated and have proper encryption state.
1083 EXPECT_TRUE(encryption_handler()->MigratedToKeystore());
1084 EXPECT_TRUE(GetCryptographer()->is_ready());
1085 EXPECT_EQ(encryption_handler()->GetPassphraseState(), CUSTOM_PASSPHRASE);
1086 EXPECT_TRUE(encryption_handler()->EncryptEverythingEnabled());
1087 VerifyMigratedNigoriWithTimestamp(1, CUSTOM_PASSPHRASE, kCurKey);
1088 }
1089
1090 // Test that if we have a migrated nigori with a custom passphrase, then receive
1091 // a migrated nigori with a keystore passphrase, we properly overwrite it with
1092 // the current state.
1093 TEST_F(SyncEncryptionHandlerImplTest, ReceiveOldMigratedNigori) {
1094 const char kOldKey[] = "old";
1095 const char kCurKey[] = "cur";
1096 sync_pb::EncryptedData encrypted;
1097 KeyParams old_key = {"localhost", "dummy", kOldKey};
1098 KeyParams cur_key = {"localhost", "dummy", kCurKey};
1099 KeyParams keystore_key = {"localhost", "dummy", kKeystoreKey};
1100 GetCryptographer()->AddKey(old_key);
1101 GetCryptographer()->AddKey(cur_key);
1102
1103 // Build a migrated nigori with full encryption.
1104 {
1105 WriteTransaction trans(FROM_HERE, user_share());
1106 WriteNode nigori_node(&trans);
1107 ASSERT_EQ(nigori_node.InitByTagLookup(kNigoriTag), BaseNode::INIT_OK);
1108 sync_pb::NigoriSpecifics nigori;
1109 GetCryptographer()->GetKeys(nigori.mutable_encryption_keybag());
1110 nigori.set_frozen_keybag(true);
1111 nigori.set_keystore_migration_time(1);
1112 nigori.set_passphrase_state(sync_pb::NigoriSpecifics::CUSTOM_PASSPHRASE);
1113 nigori.set_encrypt_everything(true);
1114 nigori_node.SetNigoriSpecifics(nigori);
1115 }
1116
1117 EXPECT_CALL(*observer(),
1118 OnPassphraseStateChanged(CUSTOM_PASSPHRASE));
1119 EXPECT_CALL(*observer(),
1120 OnCryptographerStateChanged(_));
1121 EXPECT_CALL(*observer(),
1122 OnEncryptedTypesChanged(_, true)).Times(2);
1123 EXPECT_CALL(*observer(),
1124 OnEncryptionComplete());
1125 encryption_handler()->Init();
1126 EXPECT_TRUE(encryption_handler()->MigratedToKeystore());
1127 EXPECT_TRUE(GetCryptographer()->is_ready());
1128 EXPECT_EQ(encryption_handler()->GetPassphraseState(), CUSTOM_PASSPHRASE);
1129 EXPECT_TRUE(encryption_handler()->EncryptEverythingEnabled());
1130 VerifyMigratedNigoriWithTimestamp(1, CUSTOM_PASSPHRASE, kCurKey);
1131
1132 {
1133 EXPECT_CALL(*observer(),
1134 OnBootstrapTokenUpdated(_, KEYSTORE_BOOTSTRAP_TOKEN));
1135 ReadTransaction trans(FROM_HERE, user_share());
1136 encryption_handler()->SetKeystoreKey(kKeystoreKey, trans.GetWrappedTrans());
1137 }
1138 Mock::VerifyAndClearExpectations(observer());
1139
1140 // Now build an old keystore nigori node with old encrypted types. We should
1141 // properly overwrite it with the migrated + encrypt everything state.
1142 EXPECT_CALL(*observer(),
1143 OnCryptographerStateChanged(_));
1144 {
1145 WriteTransaction trans(FROM_HERE, user_share());
1146 WriteNode nigori_node(&trans);
1147 ASSERT_EQ(nigori_node.InitByTagLookup(kNigoriTag), BaseNode::INIT_OK);
1148 sync_pb::NigoriSpecifics nigori;
1149 Cryptographer other_cryptographer(GetCryptographer()->encryptor());
1150 other_cryptographer.AddKey(old_key);
1151 std::string keystore_bootstrap;
1152 other_cryptographer.GetBootstrapToken(&keystore_bootstrap);
1153 other_cryptographer.GetKeys(nigori.mutable_encryption_keybag());
1154 other_cryptographer.AddKey(keystore_key);
1155 other_cryptographer.EncryptString(keystore_bootstrap,
1156 nigori.mutable_keystore_bootstrap());
1157 nigori.set_frozen_keybag(true);
1158 nigori.set_encrypt_everything(false);
1159 nigori.set_passphrase_state(sync_pb::NigoriSpecifics::KEYSTORE_PASSPHRASE);
1160 nigori.set_keystore_migration_time(1);
1161 encryption_handler()->ApplyNigoriUpdate(nigori, trans.GetWrappedTrans());
1162 nigori_node.SetNigoriSpecifics(nigori);
1163 }
1164 PumpLoop();
1165
1166 // Verify we're still migrated and have proper encryption state.
1167 EXPECT_TRUE(encryption_handler()->MigratedToKeystore());
1168 EXPECT_TRUE(GetCryptographer()->is_ready());
1169 EXPECT_EQ(encryption_handler()->GetPassphraseState(), CUSTOM_PASSPHRASE);
1170 EXPECT_TRUE(encryption_handler()->EncryptEverythingEnabled());
1171 VerifyMigratedNigoriWithTimestamp(1, CUSTOM_PASSPHRASE, kCurKey);
1172 }
1173
1174 // Test that if we receive the keystore key after receiving a migrated nigori
1175 // node, we properly use the keystore bootstrap to decrypt the keybag.
1176 TEST_F(SyncEncryptionHandlerImplTest, SetKeystoreAfterReceivingMigratedNigori) {
1177 const char kCurKey[] = "cur";
1178 sync_pb::EncryptedData encrypted;
1179 Cryptographer other_cryptographer(GetCryptographer()->encryptor());
1180 KeyParams cur_key = {"localhost", "dummy", kCurKey};
1181 other_cryptographer.AddKey(cur_key);
1182 EXPECT_TRUE(other_cryptographer.is_ready());
1183 EXPECT_TRUE(encryption_handler()->GetKeystoreBootstrapKey(
1184 other_cryptographer,
1185 kKeystoreKey,
1186 &encrypted));
1187 EXPECT_FALSE(encryption_handler()->MigratedToKeystore());
1188 EXPECT_FALSE(GetCryptographer()->is_ready());
1189 EXPECT_NE(encryption_handler()->GetPassphraseState(), KEYSTORE_PASSPHRASE);
1190
1191 // Now build a nigori node with the generated keystore bootstrap token and
1192 // initialize the encryption handler with it. The cryptographer should be
1193 // bootstrapped properly to decrypt both kCurKey and kKeystoreKey.
1194 {
1195 WriteTransaction trans(FROM_HERE, user_share());
1196 WriteNode nigori_node(&trans);
1197 ASSERT_EQ(nigori_node.InitByTagLookup(kNigoriTag), BaseNode::INIT_OK);
1198 sync_pb::NigoriSpecifics nigori;
1199 nigori.mutable_keystore_bootstrap()->CopyFrom(encrypted);
1200 other_cryptographer.GetKeys(nigori.mutable_encryption_keybag());
1201 nigori.set_frozen_keybag(true);
1202 nigori.set_keystore_migration_time(1);
1203 nigori.set_passphrase_state(sync_pb::NigoriSpecifics::KEYSTORE_PASSPHRASE);
1204
1205 EXPECT_CALL(*observer(),
1206 OnPassphraseStateChanged(KEYSTORE_PASSPHRASE));
1207 EXPECT_CALL(*observer(),
1208 OnCryptographerStateChanged(_));
1209 EXPECT_CALL(*observer(),
1210 OnPassphraseRequired(_, _));
1211 encryption_handler()->ApplyNigoriUpdate(nigori, trans.GetWrappedTrans());
1212 nigori_node.SetNigoriSpecifics(nigori);
1213 }
1214 // Run any tasks posted via AppplyNigoriUpdate.
1215 PumpLoop();
1216 EXPECT_TRUE(encryption_handler()->MigratedToKeystore());
1217 EXPECT_TRUE(GetCryptographer()->has_pending_keys());
1218 EXPECT_EQ(encryption_handler()->GetPassphraseState(), KEYSTORE_PASSPHRASE);
1219 EXPECT_FALSE(encryption_handler()->EncryptEverythingEnabled());
1220 Mock::VerifyAndClearExpectations(observer());
1221
1222 EXPECT_CALL(*observer(),
1223 OnCryptographerStateChanged(_));
1224 EXPECT_CALL(*observer(),
1225 OnBootstrapTokenUpdated(_, PASSPHRASE_BOOTSTRAP_TOKEN));
1226 {
1227 EXPECT_CALL(*observer(),
1228 OnBootstrapTokenUpdated(_, KEYSTORE_BOOTSTRAP_TOKEN));
1229 ReadTransaction trans(FROM_HERE, user_share());
1230 encryption_handler()->SetKeystoreKey(kKeystoreKey, trans.GetWrappedTrans());
1231 }
1232 PumpLoop();
1233 EXPECT_TRUE(encryption_handler()->MigratedToKeystore());
1234 EXPECT_TRUE(GetCryptographer()->is_ready());
1235 EXPECT_EQ(encryption_handler()->GetPassphraseState(), KEYSTORE_PASSPHRASE);
1236 EXPECT_FALSE(encryption_handler()->EncryptEverythingEnabled());
1237 VerifyMigratedNigoriWithTimestamp(1, KEYSTORE_PASSPHRASE, kCurKey);
1238
1239 // Check that the cryptographer still encrypts with the current key.
1240 sync_pb::EncryptedData current_encrypted;
1241 other_cryptographer.EncryptString("string", &current_encrypted);
1242 EXPECT_TRUE(GetCryptographer()->CanDecryptUsingDefaultKey(current_encrypted));
1243
1244 // Check that the cryptographer can decrypt keystore key based encryption.
1245 Cryptographer keystore_cryptographer(GetCryptographer()->encryptor());
1246 KeyParams keystore_key = {"localhost", "dummy", kKeystoreKey};
1247 keystore_cryptographer.AddKey(keystore_key);
1248 sync_pb::EncryptedData keystore_encrypted;
1249 keystore_cryptographer.EncryptString("string", &keystore_encrypted);
1250 EXPECT_TRUE(GetCryptographer()->CanDecrypt(keystore_encrypted));
1251 }
1252
1253 // Test that after receiving a migrated nigori and decrypting it using the
1254 // keystore key, we can then switch to a custom passphrase. The nigori should
1255 // remain migrated and encrypt everything should be enabled.
1256 TEST_F(SyncEncryptionHandlerImplTest, SetCustomPassAfterMigration) {
1257 const char kOldKey[] = "old";
1258 sync_pb::EncryptedData encrypted;
1259 Cryptographer other_cryptographer(GetCryptographer()->encryptor());
1260 KeyParams cur_key = {"localhost", "dummy", kOldKey};
1261 other_cryptographer.AddKey(cur_key);
1262 EXPECT_TRUE(other_cryptographer.is_ready());
1263 EXPECT_TRUE(encryption_handler()->GetKeystoreBootstrapKey(
1264 other_cryptographer,
1265 kKeystoreKey,
1266 &encrypted));
1267
1268 // Build a nigori node with the generated keystore bootstrap token and
1269 // initialize the encryption handler with it. The cryptographer should be
1270 // bootstrapped properly to decrypt both kOldKey and kKeystoreKey.
1271 {
1272 WriteTransaction trans(FROM_HERE, user_share());
1273 WriteNode nigori_node(&trans);
1274 ASSERT_EQ(nigori_node.InitByTagLookup(kNigoriTag), BaseNode::INIT_OK);
1275 sync_pb::NigoriSpecifics nigori;
1276 nigori.mutable_keystore_bootstrap()->CopyFrom(encrypted);
1277 other_cryptographer.GetKeys(nigori.mutable_encryption_keybag());
1278 nigori.set_frozen_keybag(true);
1279 nigori.set_keystore_migration_time(1);
1280 nigori.set_passphrase_state(sync_pb::NigoriSpecifics::KEYSTORE_PASSPHRASE);
1281 nigori_node.SetNigoriSpecifics(nigori);
1282 EXPECT_CALL(*observer(),
1283 OnBootstrapTokenUpdated(_, KEYSTORE_BOOTSTRAP_TOKEN));
1284 encryption_handler()->SetKeystoreKey(kKeystoreKey, trans.GetWrappedTrans());
1285 }
1286
1287 EXPECT_CALL(*observer(),
1288 OnPassphraseStateChanged(KEYSTORE_PASSPHRASE));
1289 EXPECT_CALL(*observer(),
1290 OnCryptographerStateChanged(_)).Times(2);
1291 EXPECT_CALL(*observer(),
1292 OnEncryptedTypesChanged(_, false));
1293 EXPECT_CALL(*observer(),
1294 OnBootstrapTokenUpdated(_, PASSPHRASE_BOOTSTRAP_TOKEN));
1295 EXPECT_CALL(*observer(),
1296 OnEncryptionComplete());
1297 encryption_handler()->Init();
1298 EXPECT_TRUE(encryption_handler()->MigratedToKeystore());
1299 EXPECT_TRUE(GetCryptographer()->is_ready());
1300 EXPECT_EQ(encryption_handler()->GetPassphraseState(), KEYSTORE_PASSPHRASE);
1301 EXPECT_FALSE(encryption_handler()->EncryptEverythingEnabled());
1302 Mock::VerifyAndClearExpectations(observer());
1303
1304 const char kNewKey[] = "new_key";
1305 EXPECT_CALL(*observer(),
1306 OnCryptographerStateChanged(_));
1307 EXPECT_CALL(*observer(),
1308 OnPassphraseStateChanged(CUSTOM_PASSPHRASE));
1309 EXPECT_CALL(*observer(),
1310 OnBootstrapTokenUpdated(_, PASSPHRASE_BOOTSTRAP_TOKEN));
1311 EXPECT_CALL(*observer(),
1312 OnPassphraseAccepted());
1313 EXPECT_CALL(*observer(),
1314 OnEncryptedTypesChanged(_, true));
1315 EXPECT_CALL(*observer(),
1316 OnEncryptionComplete()).Times(2);
1317 encryption_handler()->SetEncryptionPassphrase(kNewKey, true);
1318 EXPECT_TRUE(encryption_handler()->MigratedToKeystore());
1319 EXPECT_TRUE(GetCryptographer()->is_ready());
1320 EXPECT_EQ(encryption_handler()->GetPassphraseState(), CUSTOM_PASSPHRASE);
1321 EXPECT_TRUE(encryption_handler()->EncryptEverythingEnabled());
1322 VerifyMigratedNigoriWithTimestamp(1, CUSTOM_PASSPHRASE, kNewKey);
1323
1324 // Check that the cryptographer can decrypt the old key.
1325 sync_pb::EncryptedData old_encrypted;
1326 other_cryptographer.EncryptString("string", &old_encrypted);
1327 EXPECT_TRUE(GetCryptographer()->CanDecrypt(old_encrypted));
1328
1329 // Check that the cryptographer can decrypt keystore key based encryption.
1330 Cryptographer keystore_cryptographer(GetCryptographer()->encryptor());
1331 KeyParams keystore_key = {"localhost", "dummy", kKeystoreKey};
1332 keystore_cryptographer.AddKey(keystore_key);
1333 sync_pb::EncryptedData keystore_encrypted;
1334 keystore_cryptographer.EncryptString("string", &keystore_encrypted);
1335 EXPECT_TRUE(GetCryptographer()->CanDecrypt(keystore_encrypted));
1336
1337 // Check the the cryptographer is encrypting with the new key.
1338 KeyParams new_key = {"localhost", "dummy", kNewKey};
1339 Cryptographer new_cryptographer(GetCryptographer()->encryptor());
1340 new_cryptographer.AddKey(new_key);
1341 sync_pb::EncryptedData new_encrypted;
1342 new_cryptographer.EncryptString("string", &new_encrypted);
1343 EXPECT_TRUE(GetCryptographer()->CanDecryptUsingDefaultKey(new_encrypted));
1344 }
1345
1346 // Test that if a client without a keystore key (e.g. one without keystore
1347 // encryption enabled) receives a migrated nigori and then attempts to set a
1348 // custom passphrase, it also enables encrypt everything. The nigori node
1349 // should remain migrated.
1350 TEST_F(SyncEncryptionHandlerImplTest,
1351 SetCustomPassAfterMigrationNoKeystoreKey) {
1352 const char kOldKey[] = "old";
1353 sync_pb::EncryptedData encrypted;
1354 Cryptographer other_cryptographer(GetCryptographer()->encryptor());
1355 KeyParams cur_key = {"localhost", "dummy", kOldKey};
1356 other_cryptographer.AddKey(cur_key);
1357 KeyParams keystore_key = {"localhost", "dummy", kKeystoreKey};
1358 other_cryptographer.AddNonDefaultKey(keystore_key);
1359 EXPECT_TRUE(other_cryptographer.is_ready());
1360 EXPECT_TRUE(encryption_handler()->GetKeystoreBootstrapKey(
1361 other_cryptographer,
1362 kKeystoreKey,
1363 &encrypted));
1364
1365 // Build a nigori node with the generated keystore bootstrap token and
1366 // initialize the encryption handler with it. The cryptographer will have
1367 // pending keys until we provide the decryption passphrase.
1368 {
1369 WriteTransaction trans(FROM_HERE, user_share());
1370 WriteNode nigori_node(&trans);
1371 ASSERT_EQ(nigori_node.InitByTagLookup(kNigoriTag), BaseNode::INIT_OK);
1372 sync_pb::NigoriSpecifics nigori;
1373 nigori.mutable_keystore_bootstrap()->CopyFrom(encrypted);
1374 other_cryptographer.GetKeys(nigori.mutable_encryption_keybag());
1375 nigori.set_frozen_keybag(true);
1376 nigori.set_keystore_migration_time(1);
1377 nigori.set_passphrase_state(sync_pb::NigoriSpecifics::KEYSTORE_PASSPHRASE);
1378 nigori_node.SetNigoriSpecifics(nigori);
1379 }
1380
1381 EXPECT_CALL(*observer(),
1382 OnPassphraseRequired(_, _));
1383 EXPECT_CALL(*observer(),
1384 OnPassphraseStateChanged(KEYSTORE_PASSPHRASE));
1385 EXPECT_CALL(*observer(),
1386 OnCryptographerStateChanged(_));
1387 EXPECT_CALL(*observer(),
1388 OnEncryptedTypesChanged(_, false));
1389 encryption_handler()->Init();
1390 EXPECT_TRUE(encryption_handler()->MigratedToKeystore());
1391 EXPECT_TRUE(GetCryptographer()->has_pending_keys());
1392 EXPECT_EQ(encryption_handler()->GetPassphraseState(), KEYSTORE_PASSPHRASE);
1393 EXPECT_FALSE(encryption_handler()->EncryptEverythingEnabled());
1394 Mock::VerifyAndClearExpectations(observer());
1395
1396 EXPECT_CALL(*observer(),
1397 OnPassphraseAccepted());
1398 EXPECT_CALL(*observer(),
1399 OnCryptographerStateChanged(_));
1400 EXPECT_CALL(*observer(),
1401 OnBootstrapTokenUpdated(_, PASSPHRASE_BOOTSTRAP_TOKEN));
1402 EXPECT_CALL(*observer(),
1403 OnEncryptionComplete());
1404 encryption_handler()->SetDecryptionPassphrase(kOldKey);
1405 EXPECT_TRUE(GetCryptographer()->is_ready());
1406 EXPECT_FALSE(encryption_handler()->EncryptEverythingEnabled());
1407 Mock::VerifyAndClearExpectations(observer());
1408
1409 const char kNewKey[] = "new_key";
1410 EXPECT_CALL(*observer(),
1411 OnCryptographerStateChanged(_));
1412 EXPECT_CALL(*observer(),
1413 OnPassphraseStateChanged(CUSTOM_PASSPHRASE));
1414 EXPECT_CALL(*observer(),
1415 OnBootstrapTokenUpdated(_, PASSPHRASE_BOOTSTRAP_TOKEN));
1416 EXPECT_CALL(*observer(),
1417 OnPassphraseAccepted());
1418 EXPECT_CALL(*observer(),
1419 OnEncryptedTypesChanged(_, true));
1420 EXPECT_CALL(*observer(),
1421 OnEncryptionComplete()).Times(2);
1422 encryption_handler()->SetEncryptionPassphrase(kNewKey, true);
1423 EXPECT_TRUE(encryption_handler()->MigratedToKeystore());
1424 EXPECT_TRUE(GetCryptographer()->is_ready());
1425 EXPECT_EQ(encryption_handler()->GetPassphraseState(), CUSTOM_PASSPHRASE);
1426 EXPECT_TRUE(encryption_handler()->EncryptEverythingEnabled());
1427 VerifyMigratedNigoriWithTimestamp(1, CUSTOM_PASSPHRASE, kNewKey);
1428
1429 // Check that the cryptographer can decrypt the old key.
1430 sync_pb::EncryptedData old_encrypted;
1431 other_cryptographer.EncryptString("string", &old_encrypted);
1432 EXPECT_TRUE(GetCryptographer()->CanDecrypt(old_encrypted));
1433
1434 // Check that the cryptographer can still decrypt keystore key based
1435 // encryption (should have been extracted from the encryption keybag).
1436 Cryptographer keystore_cryptographer(GetCryptographer()->encryptor());
1437 keystore_cryptographer.AddKey(keystore_key);
1438 sync_pb::EncryptedData keystore_encrypted;
1439 keystore_cryptographer.EncryptString("string", &keystore_encrypted);
1440 EXPECT_TRUE(GetCryptographer()->CanDecrypt(keystore_encrypted));
1441
1442 // Check the the cryptographer is encrypting with the new key.
1443 KeyParams new_key = {"localhost", "dummy", kNewKey};
1444 Cryptographer new_cryptographer(GetCryptographer()->encryptor());
1445 new_cryptographer.AddKey(new_key);
1446 sync_pb::EncryptedData new_encrypted;
1447 new_cryptographer.EncryptString("string", &new_encrypted);
1448 EXPECT_TRUE(GetCryptographer()->CanDecryptUsingDefaultKey(new_encrypted));
1449 }
1450
1451 // Test that if a client without a keystore key (e.g. one without keystore
1452 // encryption enabled) receives a migrated nigori and then attempts to set a
1453 // new implicit passphrase, we do not modify the nigori node (the implicit
1454 // passphrase is dropped).
1455 TEST_F(SyncEncryptionHandlerImplTest,
1456 SetImplicitPassAfterMigrationNoKeystoreKey) {
1457 const char kOldKey[] = "old";
1458 sync_pb::EncryptedData encrypted;
1459 Cryptographer other_cryptographer(GetCryptographer()->encryptor());
1460 KeyParams cur_key = {"localhost", "dummy", kOldKey};
1461 other_cryptographer.AddKey(cur_key);
1462 KeyParams keystore_key = {"localhost", "dummy", kKeystoreKey};
1463 other_cryptographer.AddNonDefaultKey(keystore_key);
1464 EXPECT_TRUE(other_cryptographer.is_ready());
1465 EXPECT_TRUE(encryption_handler()->GetKeystoreBootstrapKey(
1466 other_cryptographer,
1467 kKeystoreKey,
1468 &encrypted));
1469
1470 // Build a nigori node with the generated keystore bootstrap token and
1471 // initialize the encryption handler with it. The cryptographer will have
1472 // pending keys until we provide the decryption passphrase.
1473 {
1474 WriteTransaction trans(FROM_HERE, user_share());
1475 WriteNode nigori_node(&trans);
1476 ASSERT_EQ(nigori_node.InitByTagLookup(kNigoriTag), BaseNode::INIT_OK);
1477 sync_pb::NigoriSpecifics nigori;
1478 nigori.mutable_keystore_bootstrap()->CopyFrom(encrypted);
1479 other_cryptographer.GetKeys(nigori.mutable_encryption_keybag());
1480 nigori.set_frozen_keybag(true);
1481 nigori.set_keystore_migration_time(1);
1482 nigori.set_passphrase_state(sync_pb::NigoriSpecifics::KEYSTORE_PASSPHRASE);
1483 nigori_node.SetNigoriSpecifics(nigori);
1484 }
1485
1486 EXPECT_CALL(*observer(),
1487 OnPassphraseRequired(_, _));
1488 EXPECT_CALL(*observer(),
1489 OnPassphraseStateChanged(KEYSTORE_PASSPHRASE));
1490 EXPECT_CALL(*observer(),
1491 OnCryptographerStateChanged(_));
1492 EXPECT_CALL(*observer(),
1493 OnEncryptedTypesChanged(_, false));
1494 encryption_handler()->Init();
1495 EXPECT_TRUE(encryption_handler()->MigratedToKeystore());
1496 EXPECT_TRUE(GetCryptographer()->has_pending_keys());
1497 EXPECT_EQ(encryption_handler()->GetPassphraseState(), KEYSTORE_PASSPHRASE);
1498 EXPECT_FALSE(encryption_handler()->EncryptEverythingEnabled());
1499 Mock::VerifyAndClearExpectations(observer());
1500
1501 EXPECT_CALL(*observer(),
1502 OnPassphraseAccepted());
1503 EXPECT_CALL(*observer(),
1504 OnCryptographerStateChanged(_));
1505 EXPECT_CALL(*observer(),
1506 OnBootstrapTokenUpdated(_, PASSPHRASE_BOOTSTRAP_TOKEN));
1507 EXPECT_CALL(*observer(),
1508 OnEncryptionComplete());
1509 encryption_handler()->SetDecryptionPassphrase(kOldKey);
1510 EXPECT_TRUE(GetCryptographer()->is_ready());
1511 EXPECT_FALSE(encryption_handler()->EncryptEverythingEnabled());
1512 Mock::VerifyAndClearExpectations(observer());
1513
1514 // Should get dropped on the floor silently.
1515 const char kNewKey[] = "new_key";
1516 encryption_handler()->SetEncryptionPassphrase(kNewKey, false);
1517 EXPECT_TRUE(encryption_handler()->MigratedToKeystore());
1518 EXPECT_TRUE(GetCryptographer()->is_ready());
1519 EXPECT_EQ(encryption_handler()->GetPassphraseState(), KEYSTORE_PASSPHRASE);
1520 EXPECT_FALSE(encryption_handler()->EncryptEverythingEnabled());
1521 VerifyMigratedNigoriWithTimestamp(1, KEYSTORE_PASSPHRASE, kOldKey);
1522
1523 // Check that the cryptographer can decrypt the old key.
1524 sync_pb::EncryptedData old_encrypted;
1525 other_cryptographer.EncryptString("string", &old_encrypted);
1526 EXPECT_TRUE(GetCryptographer()->CanDecryptUsingDefaultKey(old_encrypted));
1527
1528 // Check that the cryptographer can still decrypt keystore key based
1529 // encryption (due to extracting the keystore key from the encryption keybag).
1530 Cryptographer keystore_cryptographer(GetCryptographer()->encryptor());
1531 keystore_cryptographer.AddKey(keystore_key);
1532 sync_pb::EncryptedData keystore_encrypted;
1533 keystore_cryptographer.EncryptString("string", &keystore_encrypted);
1534 EXPECT_TRUE(GetCryptographer()->CanDecrypt(keystore_encrypted));
1535
1536 // Check the the cryptographer does not have the new key.
1537 KeyParams new_key = {"localhost", "dummy", kNewKey};
1538 Cryptographer new_cryptographer(GetCryptographer()->encryptor());
1539 new_cryptographer.AddKey(new_key);
1540 sync_pb::EncryptedData new_encrypted;
1541 new_cryptographer.EncryptString("string", &new_encrypted);
1542 EXPECT_FALSE(GetCryptographer()->CanDecryptUsingDefaultKey(new_encrypted));
1543 }
1544
1545 // Test that if a client without a keystore key (e.g. one without keystore
1546 // encryption enabled) receives a migrated nigori in keystore passphrase state
1547 // and then attempts to enable encrypt everything, we switch to a custom
1548 // passphrase. The nigori should remain migrated.
1549 TEST_F(SyncEncryptionHandlerImplTest,
1550 MigrateOnEncryptEverythingKeystorePassphrase) {
1551 const char kCurKey[] = "cur";
1552 sync_pb::EncryptedData encrypted;
1553 Cryptographer other_cryptographer(GetCryptographer()->encryptor());
1554 KeyParams cur_key = {"localhost", "dummy", kCurKey};
1555 other_cryptographer.AddKey(cur_key);
1556 KeyParams keystore_key = {"localhost", "dummy", kKeystoreKey};
1557 other_cryptographer.AddNonDefaultKey(keystore_key);
1558 EXPECT_TRUE(other_cryptographer.is_ready());
1559 EXPECT_TRUE(encryption_handler()->GetKeystoreBootstrapKey(
1560 other_cryptographer,
1561 kKeystoreKey,
1562 &encrypted));
1563
1564 // Build a nigori node with the generated keystore bootstrap token and
1565 // initialize the encryption handler with it. The cryptographer will have
1566 // pending keys until we provide the decryption passphrase.
1567 {
1568 WriteTransaction trans(FROM_HERE, user_share());
1569 WriteNode nigori_node(&trans);
1570 ASSERT_EQ(nigori_node.InitByTagLookup(kNigoriTag), BaseNode::INIT_OK);
1571 sync_pb::NigoriSpecifics nigori;
1572 nigori.mutable_keystore_bootstrap()->CopyFrom(encrypted);
1573 other_cryptographer.GetKeys(nigori.mutable_encryption_keybag());
1574 nigori.set_frozen_keybag(true);
1575 nigori.set_keystore_migration_time(1);
1576 nigori.set_passphrase_state(sync_pb::NigoriSpecifics::KEYSTORE_PASSPHRASE);
1577 nigori_node.SetNigoriSpecifics(nigori);
1578 }
1579 EXPECT_CALL(*observer(),
1580 OnPassphraseRequired(_, _));
1581 EXPECT_CALL(*observer(),
1582 OnPassphraseStateChanged(KEYSTORE_PASSPHRASE));
1583 EXPECT_CALL(*observer(),
1584 OnCryptographerStateChanged(_));
1585 EXPECT_CALL(*observer(),
1586 OnEncryptedTypesChanged(_, false));
1587 encryption_handler()->Init();
1588 EXPECT_TRUE(encryption_handler()->MigratedToKeystore());
1589 EXPECT_TRUE(GetCryptographer()->has_pending_keys());
1590 EXPECT_EQ(encryption_handler()->GetPassphraseState(), KEYSTORE_PASSPHRASE);
1591 EXPECT_FALSE(encryption_handler()->EncryptEverythingEnabled());
1592 Mock::VerifyAndClearExpectations(observer());
1593
1594 EXPECT_CALL(*observer(),
1595 OnPassphraseAccepted());
1596 EXPECT_CALL(*observer(),
1597 OnCryptographerStateChanged(_));
1598 EXPECT_CALL(*observer(),
1599 OnBootstrapTokenUpdated(_, PASSPHRASE_BOOTSTRAP_TOKEN));
1600 EXPECT_CALL(*observer(),
1601 OnEncryptionComplete());
1602 encryption_handler()->SetDecryptionPassphrase(kCurKey);
1603 Mock::VerifyAndClearExpectations(observer());
1604
1605 EXPECT_CALL(*observer(),
1606 OnPassphraseStateChanged(FROZEN_IMPLICIT_PASSPHRASE));
1607 EXPECT_CALL(*observer(),
1608 OnEncryptionComplete());
1609 EXPECT_CALL(*observer(),
1610 OnEncryptedTypesChanged(_, true));
1611 encryption_handler()->EnableEncryptEverything();
1612 Mock::VerifyAndClearExpectations(observer());
1613
1614 EXPECT_TRUE(encryption_handler()->MigratedToKeystore());
1615 EXPECT_TRUE(GetCryptographer()->is_ready());
1616 EXPECT_EQ(FROZEN_IMPLICIT_PASSPHRASE,
1617 encryption_handler()->GetPassphraseState());
1618 EXPECT_TRUE(encryption_handler()->EncryptEverythingEnabled());
1619 VerifyMigratedNigoriWithTimestamp(1, FROZEN_IMPLICIT_PASSPHRASE, kCurKey);
1620
1621 // Check that the cryptographer is encrypting using the frozen current key.
1622 sync_pb::EncryptedData current_encrypted;
1623 other_cryptographer.EncryptString("string", &current_encrypted);
1624 EXPECT_TRUE(GetCryptographer()->CanDecryptUsingDefaultKey(current_encrypted));
1625
1626 // Check that the cryptographer can still decrypt keystore key based
1627 // encryption (due to extracting the keystore key from the encryption keybag).
1628 Cryptographer keystore_cryptographer(GetCryptographer()->encryptor());
1629 keystore_cryptographer.AddKey(keystore_key);
1630 sync_pb::EncryptedData keystore_encrypted;
1631 keystore_cryptographer.EncryptString("string", &keystore_encrypted);
1632 EXPECT_TRUE(GetCryptographer()->CanDecrypt(keystore_encrypted));
1633 }
1634
440 } // namespace syncer 1635 } // namespace syncer
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698