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

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: Address comments 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,
46 void(ModelTypeSet, bool)); // NOLINT 49 void(ModelTypeSet, bool)); // NOLINT
47 MOCK_METHOD0(OnEncryptionComplete, void()); // NOLINT 50 MOCK_METHOD0(OnEncryptionComplete, void()); // NOLINT
48 MOCK_METHOD1(OnCryptographerStateChanged, void(Cryptographer*)); // NOLINT 51 MOCK_METHOD1(OnCryptographerStateChanged, void(Cryptographer*)); // NOLINT
49 MOCK_METHOD1(OnPassphraseStateChanged, void(PassphraseState)); // NOLINT 52 MOCK_METHOD1(OnPassphraseTypeChanged, void(PassphraseType)); // NOLINT
50 }; 53 };
51 54
52 } // namespace 55 } // namespace
53 56
54 class SyncEncryptionHandlerImplTest : public ::testing::Test { 57 class SyncEncryptionHandlerImplTest : public ::testing::Test {
55 public: 58 public:
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(PassphraseType passphrase_type,
121 const std::string& passphrase) {
122 VerifyMigratedNigoriWithTimestamp(0, passphrase_type, passphrase);
123 }
124
125 void VerifyMigratedNigoriWithTimestamp(
126 int64 migration_time,
127 PassphraseType passphrase_type,
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_type == CUSTOM_PASSPHRASE ||
139 passphrase_type == FROZEN_IMPLICIT_PASSPHRASE) {
140 EXPECT_TRUE(nigori.encrypt_everything());
141 EXPECT_TRUE(nigori.keystore_decryptor().blob().empty());
142 if (passphrase_type == CUSTOM_PASSPHRASE) {
143 EXPECT_EQ(sync_pb::NigoriSpecifics::CUSTOM_PASSPHRASE,
144 nigori.passphrase_type());
145 } else {
146 EXPECT_EQ(sync_pb::NigoriSpecifics::FROZEN_IMPLICIT_PASSPHRASE,
147 nigori.passphrase_type());
148 }
149 } else {
150 EXPECT_FALSE(nigori.encrypt_everything());
151 EXPECT_FALSE(nigori.keystore_decryptor().blob().empty());
152 EXPECT_EQ(sync_pb::NigoriSpecifics::KEYSTORE_PASSPHRASE,
153 nigori.passphrase_type());
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_decryptor()));
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 GetKeystoreDecryptor only updates the keystore decryptor if it
492 // wasn't already set properly. Otherwise, the decryptor should remain the
493 // same.
494 TEST_F(SyncEncryptionHandlerImplTest, GetKeystoreDecryptor) {
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()->GetKeystoreDecryptor(
502 other_cryptographer,
503 kKeystoreKey,
504 &encrypted));
505 std::string serialized = encrypted.SerializeAsString();
506 EXPECT_TRUE(encryption_handler()->GetKeystoreDecryptor(
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 OnPassphraseTypeChanged(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()->GetPassphraseType());
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 OnPassphraseTypeChanged(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()->GetPassphraseType());
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 OnPassphraseTypeChanged(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()->GetPassphraseType());
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 OnPassphraseTypeChanged(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()->GetPassphraseType());
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 OnPassphraseTypeChanged(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()->GetPassphraseType());
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 OnPassphraseTypeChanged(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()->GetPassphraseType());
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 decryptor to decrypt the keybag.
793 TEST_F(SyncEncryptionHandlerImplTest, ReceiveMigratedNigoriKeystorePass) {
794 const char kCurKey[] = "cur";
795 sync_pb::EncryptedData keystore_decryptor;
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()->GetKeystoreDecryptor(
801 other_cryptographer,
802 kKeystoreKey,
803 &keystore_decryptor));
804 EXPECT_FALSE(encryption_handler()->MigratedToKeystore());
805 EXPECT_FALSE(GetCryptographer()->is_ready());
806 EXPECT_NE(encryption_handler()->GetPassphraseType(), KEYSTORE_PASSPHRASE);
807
808 // Now build a nigori node with the generated keystore decryptor and
809 // initialize the encryption handler with it. The cryptographer should be
810 // initialized 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_decryptor()->CopyFrom(keystore_decryptor);
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_type(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 OnPassphraseTypeChanged(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()->GetPassphraseType(), 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 OnPassphraseTypeChanged(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_type(
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()->GetPassphraseType());
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 OnPassphraseTypeChanged(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_type(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()->GetPassphraseType());
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_type(sync_pb::NigoriSpecifics::CUSTOM_PASSPHRASE);
1036 nigori.set_encrypt_everything(true);
1037 nigori_node.SetNigoriSpecifics(nigori);
1038 }
1039
1040 EXPECT_CALL(*observer(),
1041 OnPassphraseTypeChanged(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()->GetPassphraseType(), 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()->GetPassphraseType(), 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_type(sync_pb::NigoriSpecifics::CUSTOM_PASSPHRASE);
1113 nigori.set_encrypt_everything(true);
1114 nigori_node.SetNigoriSpecifics(nigori);
1115 }
1116
1117 EXPECT_CALL(*observer(),
1118 OnPassphraseTypeChanged(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()->GetPassphraseType(), 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 encryption_handler()->GetKeystoreDecryptor(
1152 other_cryptographer,
1153 kKeystoreKey,
1154 nigori.mutable_keystore_decryptor());
1155 other_cryptographer.GetKeys(nigori.mutable_encryption_keybag());
1156 nigori.set_frozen_keybag(true);
1157 nigori.set_encrypt_everything(false);
1158 nigori.set_passphrase_type(sync_pb::NigoriSpecifics::KEYSTORE_PASSPHRASE);
1159 nigori.set_keystore_migration_time(1);
1160 encryption_handler()->ApplyNigoriUpdate(nigori, trans.GetWrappedTrans());
1161 nigori_node.SetNigoriSpecifics(nigori);
1162 }
1163 PumpLoop();
1164
1165 // Verify we're still migrated and have proper encryption state.
1166 EXPECT_TRUE(encryption_handler()->MigratedToKeystore());
1167 EXPECT_TRUE(GetCryptographer()->is_ready());
1168 EXPECT_EQ(encryption_handler()->GetPassphraseType(), CUSTOM_PASSPHRASE);
1169 EXPECT_TRUE(encryption_handler()->EncryptEverythingEnabled());
1170 VerifyMigratedNigoriWithTimestamp(1, CUSTOM_PASSPHRASE, kCurKey);
1171 }
1172
1173 // Test that if we receive the keystore key after receiving a migrated nigori
1174 // node, we properly use the keystore decryptor to decrypt the keybag.
1175 TEST_F(SyncEncryptionHandlerImplTest, SetKeystoreAfterReceivingMigratedNigori) {
1176 const char kCurKey[] = "cur";
1177 sync_pb::EncryptedData keystore_decryptor;
1178 Cryptographer other_cryptographer(GetCryptographer()->encryptor());
1179 KeyParams cur_key = {"localhost", "dummy", kCurKey};
1180 other_cryptographer.AddKey(cur_key);
1181 EXPECT_TRUE(other_cryptographer.is_ready());
1182 EXPECT_TRUE(encryption_handler()->GetKeystoreDecryptor(
1183 other_cryptographer,
1184 kKeystoreKey,
1185 &keystore_decryptor));
1186 EXPECT_FALSE(encryption_handler()->MigratedToKeystore());
1187 EXPECT_FALSE(GetCryptographer()->is_ready());
1188 EXPECT_NE(encryption_handler()->GetPassphraseType(), KEYSTORE_PASSPHRASE);
1189
1190 // Now build a nigori node with the generated keystore decryptor and
1191 // initialize the encryption handler with it. The cryptographer should be
1192 // initialized properly to decrypt both kCurKey and kKeystoreKey.
1193 {
1194 WriteTransaction trans(FROM_HERE, user_share());
1195 WriteNode nigori_node(&trans);
1196 ASSERT_EQ(nigori_node.InitByTagLookup(kNigoriTag), BaseNode::INIT_OK);
1197 sync_pb::NigoriSpecifics nigori;
1198 nigori.mutable_keystore_decryptor()->CopyFrom(keystore_decryptor);
1199 other_cryptographer.GetKeys(nigori.mutable_encryption_keybag());
1200 nigori.set_frozen_keybag(true);
1201 nigori.set_keystore_migration_time(1);
1202 nigori.set_passphrase_type(sync_pb::NigoriSpecifics::KEYSTORE_PASSPHRASE);
1203
1204 EXPECT_CALL(*observer(),
1205 OnPassphraseTypeChanged(KEYSTORE_PASSPHRASE));
1206 EXPECT_CALL(*observer(),
1207 OnCryptographerStateChanged(_));
1208 EXPECT_CALL(*observer(),
1209 OnPassphraseRequired(_, _));
1210 encryption_handler()->ApplyNigoriUpdate(nigori, trans.GetWrappedTrans());
1211 nigori_node.SetNigoriSpecifics(nigori);
1212 }
1213 // Run any tasks posted via AppplyNigoriUpdate.
1214 PumpLoop();
1215 EXPECT_TRUE(encryption_handler()->MigratedToKeystore());
1216 EXPECT_TRUE(GetCryptographer()->has_pending_keys());
1217 EXPECT_EQ(encryption_handler()->GetPassphraseType(), KEYSTORE_PASSPHRASE);
1218 EXPECT_FALSE(encryption_handler()->EncryptEverythingEnabled());
1219 Mock::VerifyAndClearExpectations(observer());
1220
1221 EXPECT_CALL(*observer(),
1222 OnCryptographerStateChanged(_));
1223 EXPECT_CALL(*observer(),
1224 OnBootstrapTokenUpdated(_, PASSPHRASE_BOOTSTRAP_TOKEN));
1225 {
1226 EXPECT_CALL(*observer(),
1227 OnBootstrapTokenUpdated(_, KEYSTORE_BOOTSTRAP_TOKEN));
1228 ReadTransaction trans(FROM_HERE, user_share());
1229 encryption_handler()->SetKeystoreKey(kKeystoreKey, trans.GetWrappedTrans());
1230 }
1231 PumpLoop();
1232 EXPECT_TRUE(encryption_handler()->MigratedToKeystore());
1233 EXPECT_TRUE(GetCryptographer()->is_ready());
1234 EXPECT_EQ(encryption_handler()->GetPassphraseType(), KEYSTORE_PASSPHRASE);
1235 EXPECT_FALSE(encryption_handler()->EncryptEverythingEnabled());
1236 VerifyMigratedNigoriWithTimestamp(1, KEYSTORE_PASSPHRASE, kCurKey);
1237
1238 // Check that the cryptographer still encrypts with the current key.
1239 sync_pb::EncryptedData current_encrypted;
1240 other_cryptographer.EncryptString("string", &current_encrypted);
1241 EXPECT_TRUE(GetCryptographer()->CanDecryptUsingDefaultKey(current_encrypted));
1242
1243 // Check that the cryptographer can decrypt keystore key based encryption.
1244 Cryptographer keystore_cryptographer(GetCryptographer()->encryptor());
1245 KeyParams keystore_key = {"localhost", "dummy", kKeystoreKey};
1246 keystore_cryptographer.AddKey(keystore_key);
1247 sync_pb::EncryptedData keystore_encrypted;
1248 keystore_cryptographer.EncryptString("string", &keystore_encrypted);
1249 EXPECT_TRUE(GetCryptographer()->CanDecrypt(keystore_encrypted));
1250 }
1251
1252 // Test that after receiving a migrated nigori and decrypting it using the
1253 // keystore key, we can then switch to a custom passphrase. The nigori should
1254 // remain migrated and encrypt everything should be enabled.
1255 TEST_F(SyncEncryptionHandlerImplTest, SetCustomPassAfterMigration) {
1256 const char kOldKey[] = "old";
1257 sync_pb::EncryptedData keystore_decryptor;
1258 Cryptographer other_cryptographer(GetCryptographer()->encryptor());
1259 KeyParams cur_key = {"localhost", "dummy", kOldKey};
1260 other_cryptographer.AddKey(cur_key);
1261 EXPECT_TRUE(other_cryptographer.is_ready());
1262 EXPECT_TRUE(encryption_handler()->GetKeystoreDecryptor(
1263 other_cryptographer,
1264 kKeystoreKey,
1265 &keystore_decryptor));
1266
1267 // Build a nigori node with the generated keystore decryptor and
1268 // initialize the encryption handler with it. The cryptographer should be
1269 // initialized properly to decrypt both kOldKey and kKeystoreKey.
1270 {
1271 WriteTransaction trans(FROM_HERE, user_share());
1272 WriteNode nigori_node(&trans);
1273 ASSERT_EQ(nigori_node.InitByTagLookup(kNigoriTag), BaseNode::INIT_OK);
1274 sync_pb::NigoriSpecifics nigori;
1275 nigori.mutable_keystore_decryptor()->CopyFrom(keystore_decryptor);
1276 other_cryptographer.GetKeys(nigori.mutable_encryption_keybag());
1277 nigori.set_frozen_keybag(true);
1278 nigori.set_keystore_migration_time(1);
1279 nigori.set_passphrase_type(sync_pb::NigoriSpecifics::KEYSTORE_PASSPHRASE);
1280 nigori_node.SetNigoriSpecifics(nigori);
1281 EXPECT_CALL(*observer(),
1282 OnBootstrapTokenUpdated(_, KEYSTORE_BOOTSTRAP_TOKEN));
1283 encryption_handler()->SetKeystoreKey(kKeystoreKey, trans.GetWrappedTrans());
1284 }
1285
1286 EXPECT_CALL(*observer(),
1287 OnPassphraseTypeChanged(KEYSTORE_PASSPHRASE));
1288 EXPECT_CALL(*observer(),
1289 OnCryptographerStateChanged(_)).Times(2);
1290 EXPECT_CALL(*observer(),
1291 OnEncryptedTypesChanged(_, false));
1292 EXPECT_CALL(*observer(),
1293 OnBootstrapTokenUpdated(_, PASSPHRASE_BOOTSTRAP_TOKEN));
1294 EXPECT_CALL(*observer(),
1295 OnEncryptionComplete());
1296 encryption_handler()->Init();
1297 EXPECT_TRUE(encryption_handler()->MigratedToKeystore());
1298 EXPECT_TRUE(GetCryptographer()->is_ready());
1299 EXPECT_EQ(encryption_handler()->GetPassphraseType(), KEYSTORE_PASSPHRASE);
1300 EXPECT_FALSE(encryption_handler()->EncryptEverythingEnabled());
1301 Mock::VerifyAndClearExpectations(observer());
1302
1303 const char kNewKey[] = "new_key";
1304 EXPECT_CALL(*observer(),
1305 OnCryptographerStateChanged(_));
1306 EXPECT_CALL(*observer(),
1307 OnPassphraseTypeChanged(CUSTOM_PASSPHRASE));
1308 EXPECT_CALL(*observer(),
1309 OnBootstrapTokenUpdated(_, PASSPHRASE_BOOTSTRAP_TOKEN));
1310 EXPECT_CALL(*observer(),
1311 OnPassphraseAccepted());
1312 EXPECT_CALL(*observer(),
1313 OnEncryptedTypesChanged(_, true));
1314 EXPECT_CALL(*observer(),
1315 OnEncryptionComplete()).Times(2);
1316 encryption_handler()->SetEncryptionPassphrase(kNewKey, true);
1317 EXPECT_TRUE(encryption_handler()->MigratedToKeystore());
1318 EXPECT_TRUE(GetCryptographer()->is_ready());
1319 EXPECT_EQ(encryption_handler()->GetPassphraseType(), CUSTOM_PASSPHRASE);
1320 EXPECT_TRUE(encryption_handler()->EncryptEverythingEnabled());
1321 VerifyMigratedNigoriWithTimestamp(1, CUSTOM_PASSPHRASE, kNewKey);
1322
1323 // Check that the cryptographer can decrypt the old key.
1324 sync_pb::EncryptedData old_encrypted;
1325 other_cryptographer.EncryptString("string", &old_encrypted);
1326 EXPECT_TRUE(GetCryptographer()->CanDecrypt(old_encrypted));
1327
1328 // Check that the cryptographer can decrypt keystore key based encryption.
1329 Cryptographer keystore_cryptographer(GetCryptographer()->encryptor());
1330 KeyParams keystore_key = {"localhost", "dummy", kKeystoreKey};
1331 keystore_cryptographer.AddKey(keystore_key);
1332 sync_pb::EncryptedData keystore_encrypted;
1333 keystore_cryptographer.EncryptString("string", &keystore_encrypted);
1334 EXPECT_TRUE(GetCryptographer()->CanDecrypt(keystore_encrypted));
1335
1336 // Check the the cryptographer is encrypting with the new key.
1337 KeyParams new_key = {"localhost", "dummy", kNewKey};
1338 Cryptographer new_cryptographer(GetCryptographer()->encryptor());
1339 new_cryptographer.AddKey(new_key);
1340 sync_pb::EncryptedData new_encrypted;
1341 new_cryptographer.EncryptString("string", &new_encrypted);
1342 EXPECT_TRUE(GetCryptographer()->CanDecryptUsingDefaultKey(new_encrypted));
1343 }
1344
1345 // Test that if a client without a keystore key (e.g. one without keystore
1346 // encryption enabled) receives a migrated nigori and then attempts to set a
1347 // custom passphrase, it also enables encrypt everything. The nigori node
1348 // should remain migrated.
1349 TEST_F(SyncEncryptionHandlerImplTest,
1350 SetCustomPassAfterMigrationNoKeystoreKey) {
1351 const char kOldKey[] = "old";
1352 sync_pb::EncryptedData keystore_decryptor;
1353 Cryptographer other_cryptographer(GetCryptographer()->encryptor());
1354 KeyParams cur_key = {"localhost", "dummy", kOldKey};
1355 other_cryptographer.AddKey(cur_key);
1356 KeyParams keystore_key = {"localhost", "dummy", kKeystoreKey};
1357 other_cryptographer.AddNonDefaultKey(keystore_key);
1358 EXPECT_TRUE(other_cryptographer.is_ready());
1359 EXPECT_TRUE(encryption_handler()->GetKeystoreDecryptor(
1360 other_cryptographer,
1361 kKeystoreKey,
1362 &keystore_decryptor));
1363
1364 // Build a nigori node with the generated keystore decryptor and
1365 // initialize the encryption handler with it. The cryptographer will have
1366 // pending keys until we provide the decryption passphrase.
1367 {
1368 WriteTransaction trans(FROM_HERE, user_share());
1369 WriteNode nigori_node(&trans);
1370 ASSERT_EQ(nigori_node.InitByTagLookup(kNigoriTag), BaseNode::INIT_OK);
1371 sync_pb::NigoriSpecifics nigori;
1372 nigori.mutable_keystore_decryptor()->CopyFrom(keystore_decryptor);
1373 other_cryptographer.GetKeys(nigori.mutable_encryption_keybag());
1374 nigori.set_frozen_keybag(true);
1375 nigori.set_keystore_migration_time(1);
1376 nigori.set_passphrase_type(sync_pb::NigoriSpecifics::KEYSTORE_PASSPHRASE);
1377 nigori_node.SetNigoriSpecifics(nigori);
1378 }
1379
1380 EXPECT_CALL(*observer(),
1381 OnPassphraseRequired(_, _));
1382 EXPECT_CALL(*observer(),
1383 OnPassphraseTypeChanged(KEYSTORE_PASSPHRASE));
1384 EXPECT_CALL(*observer(),
1385 OnCryptographerStateChanged(_));
1386 EXPECT_CALL(*observer(),
1387 OnEncryptedTypesChanged(_, false));
1388 encryption_handler()->Init();
1389 EXPECT_TRUE(encryption_handler()->MigratedToKeystore());
1390 EXPECT_TRUE(GetCryptographer()->has_pending_keys());
1391 EXPECT_EQ(encryption_handler()->GetPassphraseType(), KEYSTORE_PASSPHRASE);
1392 EXPECT_FALSE(encryption_handler()->EncryptEverythingEnabled());
1393 Mock::VerifyAndClearExpectations(observer());
1394
1395 EXPECT_CALL(*observer(),
1396 OnPassphraseAccepted());
1397 EXPECT_CALL(*observer(),
1398 OnCryptographerStateChanged(_));
1399 EXPECT_CALL(*observer(),
1400 OnBootstrapTokenUpdated(_, PASSPHRASE_BOOTSTRAP_TOKEN));
1401 EXPECT_CALL(*observer(),
1402 OnEncryptionComplete());
1403 encryption_handler()->SetDecryptionPassphrase(kOldKey);
1404 EXPECT_TRUE(GetCryptographer()->is_ready());
1405 EXPECT_FALSE(encryption_handler()->EncryptEverythingEnabled());
1406 Mock::VerifyAndClearExpectations(observer());
1407
1408 const char kNewKey[] = "new_key";
1409 EXPECT_CALL(*observer(),
1410 OnCryptographerStateChanged(_));
1411 EXPECT_CALL(*observer(),
1412 OnPassphraseTypeChanged(CUSTOM_PASSPHRASE));
1413 EXPECT_CALL(*observer(),
1414 OnBootstrapTokenUpdated(_, PASSPHRASE_BOOTSTRAP_TOKEN));
1415 EXPECT_CALL(*observer(),
1416 OnPassphraseAccepted());
1417 EXPECT_CALL(*observer(),
1418 OnEncryptedTypesChanged(_, true));
1419 EXPECT_CALL(*observer(),
1420 OnEncryptionComplete()).Times(2);
1421 encryption_handler()->SetEncryptionPassphrase(kNewKey, true);
1422 EXPECT_TRUE(encryption_handler()->MigratedToKeystore());
1423 EXPECT_TRUE(GetCryptographer()->is_ready());
1424 EXPECT_EQ(encryption_handler()->GetPassphraseType(), CUSTOM_PASSPHRASE);
1425 EXPECT_TRUE(encryption_handler()->EncryptEverythingEnabled());
1426 VerifyMigratedNigoriWithTimestamp(1, CUSTOM_PASSPHRASE, kNewKey);
1427
1428 // Check that the cryptographer can decrypt the old key.
1429 sync_pb::EncryptedData old_encrypted;
1430 other_cryptographer.EncryptString("string", &old_encrypted);
1431 EXPECT_TRUE(GetCryptographer()->CanDecrypt(old_encrypted));
1432
1433 // Check that the cryptographer can still decrypt keystore key based
1434 // encryption (should have been extracted from the encryption keybag).
1435 Cryptographer keystore_cryptographer(GetCryptographer()->encryptor());
1436 keystore_cryptographer.AddKey(keystore_key);
1437 sync_pb::EncryptedData keystore_encrypted;
1438 keystore_cryptographer.EncryptString("string", &keystore_encrypted);
1439 EXPECT_TRUE(GetCryptographer()->CanDecrypt(keystore_encrypted));
1440
1441 // Check the the cryptographer is encrypting with the new key.
1442 KeyParams new_key = {"localhost", "dummy", kNewKey};
1443 Cryptographer new_cryptographer(GetCryptographer()->encryptor());
1444 new_cryptographer.AddKey(new_key);
1445 sync_pb::EncryptedData new_encrypted;
1446 new_cryptographer.EncryptString("string", &new_encrypted);
1447 EXPECT_TRUE(GetCryptographer()->CanDecryptUsingDefaultKey(new_encrypted));
1448 }
1449
1450 // Test that if a client without a keystore key (e.g. one without keystore
1451 // encryption enabled) receives a migrated nigori and then attempts to set a
1452 // new implicit passphrase, we do not modify the nigori node (the implicit
1453 // passphrase is dropped).
1454 TEST_F(SyncEncryptionHandlerImplTest,
1455 SetImplicitPassAfterMigrationNoKeystoreKey) {
1456 const char kOldKey[] = "old";
1457 sync_pb::EncryptedData keystore_decryptor;
1458 Cryptographer other_cryptographer(GetCryptographer()->encryptor());
1459 KeyParams cur_key = {"localhost", "dummy", kOldKey};
1460 other_cryptographer.AddKey(cur_key);
1461 KeyParams keystore_key = {"localhost", "dummy", kKeystoreKey};
1462 other_cryptographer.AddNonDefaultKey(keystore_key);
1463 EXPECT_TRUE(other_cryptographer.is_ready());
1464 EXPECT_TRUE(encryption_handler()->GetKeystoreDecryptor(
1465 other_cryptographer,
1466 kKeystoreKey,
1467 &keystore_decryptor));
1468
1469 // Build a nigori node with the generated keystore decryptor and
1470 // initialize the encryption handler with it. The cryptographer will have
1471 // pending keys until we provide the decryption passphrase.
1472 {
1473 WriteTransaction trans(FROM_HERE, user_share());
1474 WriteNode nigori_node(&trans);
1475 ASSERT_EQ(nigori_node.InitByTagLookup(kNigoriTag), BaseNode::INIT_OK);
1476 sync_pb::NigoriSpecifics nigori;
1477 nigori.mutable_keystore_decryptor()->CopyFrom(keystore_decryptor);
1478 other_cryptographer.GetKeys(nigori.mutable_encryption_keybag());
1479 nigori.set_frozen_keybag(true);
1480 nigori.set_keystore_migration_time(1);
1481 nigori.set_passphrase_type(sync_pb::NigoriSpecifics::KEYSTORE_PASSPHRASE);
1482 nigori_node.SetNigoriSpecifics(nigori);
1483 }
1484
1485 EXPECT_CALL(*observer(),
1486 OnPassphraseRequired(_, _));
1487 EXPECT_CALL(*observer(),
1488 OnPassphraseTypeChanged(KEYSTORE_PASSPHRASE));
1489 EXPECT_CALL(*observer(),
1490 OnCryptographerStateChanged(_));
1491 EXPECT_CALL(*observer(),
1492 OnEncryptedTypesChanged(_, false));
1493 encryption_handler()->Init();
1494 EXPECT_TRUE(encryption_handler()->MigratedToKeystore());
1495 EXPECT_TRUE(GetCryptographer()->has_pending_keys());
1496 EXPECT_EQ(encryption_handler()->GetPassphraseType(), KEYSTORE_PASSPHRASE);
1497 EXPECT_FALSE(encryption_handler()->EncryptEverythingEnabled());
1498 Mock::VerifyAndClearExpectations(observer());
1499
1500 EXPECT_CALL(*observer(),
1501 OnPassphraseAccepted());
1502 EXPECT_CALL(*observer(),
1503 OnCryptographerStateChanged(_));
1504 EXPECT_CALL(*observer(),
1505 OnBootstrapTokenUpdated(_, PASSPHRASE_BOOTSTRAP_TOKEN));
1506 EXPECT_CALL(*observer(),
1507 OnEncryptionComplete());
1508 encryption_handler()->SetDecryptionPassphrase(kOldKey);
1509 EXPECT_TRUE(GetCryptographer()->is_ready());
1510 EXPECT_FALSE(encryption_handler()->EncryptEverythingEnabled());
1511 Mock::VerifyAndClearExpectations(observer());
1512
1513 // Should get dropped on the floor silently.
1514 const char kNewKey[] = "new_key";
1515 encryption_handler()->SetEncryptionPassphrase(kNewKey, false);
1516 EXPECT_TRUE(encryption_handler()->MigratedToKeystore());
1517 EXPECT_TRUE(GetCryptographer()->is_ready());
1518 EXPECT_EQ(encryption_handler()->GetPassphraseType(), KEYSTORE_PASSPHRASE);
1519 EXPECT_FALSE(encryption_handler()->EncryptEverythingEnabled());
1520 VerifyMigratedNigoriWithTimestamp(1, KEYSTORE_PASSPHRASE, kOldKey);
1521
1522 // Check that the cryptographer can decrypt the old key.
1523 sync_pb::EncryptedData old_encrypted;
1524 other_cryptographer.EncryptString("string", &old_encrypted);
1525 EXPECT_TRUE(GetCryptographer()->CanDecryptUsingDefaultKey(old_encrypted));
1526
1527 // Check that the cryptographer can still decrypt keystore key based
1528 // encryption (due to extracting the keystore key from the encryption keybag).
1529 Cryptographer keystore_cryptographer(GetCryptographer()->encryptor());
1530 keystore_cryptographer.AddKey(keystore_key);
1531 sync_pb::EncryptedData keystore_encrypted;
1532 keystore_cryptographer.EncryptString("string", &keystore_encrypted);
1533 EXPECT_TRUE(GetCryptographer()->CanDecrypt(keystore_encrypted));
1534
1535 // Check the the cryptographer does not have the new key.
1536 KeyParams new_key = {"localhost", "dummy", kNewKey};
1537 Cryptographer new_cryptographer(GetCryptographer()->encryptor());
1538 new_cryptographer.AddKey(new_key);
1539 sync_pb::EncryptedData new_encrypted;
1540 new_cryptographer.EncryptString("string", &new_encrypted);
1541 EXPECT_FALSE(GetCryptographer()->CanDecryptUsingDefaultKey(new_encrypted));
1542 }
1543
1544 // Test that if a client without a keystore key (e.g. one without keystore
1545 // encryption enabled) receives a migrated nigori in keystore passphrase state
1546 // and then attempts to enable encrypt everything, we switch to a custom
1547 // passphrase. The nigori should remain migrated.
1548 TEST_F(SyncEncryptionHandlerImplTest,
1549 MigrateOnEncryptEverythingKeystorePassphrase) {
1550 const char kCurKey[] = "cur";
1551 sync_pb::EncryptedData keystore_decryptor;
1552 Cryptographer other_cryptographer(GetCryptographer()->encryptor());
1553 KeyParams cur_key = {"localhost", "dummy", kCurKey};
1554 other_cryptographer.AddKey(cur_key);
1555 KeyParams keystore_key = {"localhost", "dummy", kKeystoreKey};
1556 other_cryptographer.AddNonDefaultKey(keystore_key);
1557 EXPECT_TRUE(other_cryptographer.is_ready());
1558 EXPECT_TRUE(encryption_handler()->GetKeystoreDecryptor(
1559 other_cryptographer,
1560 kKeystoreKey,
1561 &keystore_decryptor));
1562
1563 // Build a nigori node with the generated keystore decryptor and
1564 // initialize the encryption handler with it. The cryptographer will have
1565 // pending keys until we provide the decryption passphrase.
1566 {
1567 WriteTransaction trans(FROM_HERE, user_share());
1568 WriteNode nigori_node(&trans);
1569 ASSERT_EQ(nigori_node.InitByTagLookup(kNigoriTag), BaseNode::INIT_OK);
1570 sync_pb::NigoriSpecifics nigori;
1571 nigori.mutable_keystore_decryptor()->CopyFrom(keystore_decryptor);
1572 other_cryptographer.GetKeys(nigori.mutable_encryption_keybag());
1573 nigori.set_frozen_keybag(true);
1574 nigori.set_keystore_migration_time(1);
1575 nigori.set_passphrase_type(sync_pb::NigoriSpecifics::KEYSTORE_PASSPHRASE);
1576 nigori_node.SetNigoriSpecifics(nigori);
1577 }
1578 EXPECT_CALL(*observer(),
1579 OnPassphraseRequired(_, _));
1580 EXPECT_CALL(*observer(),
1581 OnPassphraseTypeChanged(KEYSTORE_PASSPHRASE));
1582 EXPECT_CALL(*observer(),
1583 OnCryptographerStateChanged(_));
1584 EXPECT_CALL(*observer(),
1585 OnEncryptedTypesChanged(_, false));
1586 encryption_handler()->Init();
1587 EXPECT_TRUE(encryption_handler()->MigratedToKeystore());
1588 EXPECT_TRUE(GetCryptographer()->has_pending_keys());
1589 EXPECT_EQ(encryption_handler()->GetPassphraseType(), KEYSTORE_PASSPHRASE);
1590 EXPECT_FALSE(encryption_handler()->EncryptEverythingEnabled());
1591 Mock::VerifyAndClearExpectations(observer());
1592
1593 EXPECT_CALL(*observer(),
1594 OnPassphraseAccepted());
1595 EXPECT_CALL(*observer(),
1596 OnCryptographerStateChanged(_));
1597 EXPECT_CALL(*observer(),
1598 OnBootstrapTokenUpdated(_, PASSPHRASE_BOOTSTRAP_TOKEN));
1599 EXPECT_CALL(*observer(),
1600 OnEncryptionComplete());
1601 encryption_handler()->SetDecryptionPassphrase(kCurKey);
1602 Mock::VerifyAndClearExpectations(observer());
1603
1604 EXPECT_CALL(*observer(),
1605 OnPassphraseTypeChanged(FROZEN_IMPLICIT_PASSPHRASE));
1606 EXPECT_CALL(*observer(),
1607 OnEncryptionComplete());
1608 EXPECT_CALL(*observer(),
1609 OnEncryptedTypesChanged(_, true));
1610 encryption_handler()->EnableEncryptEverything();
1611 Mock::VerifyAndClearExpectations(observer());
1612
1613 EXPECT_TRUE(encryption_handler()->MigratedToKeystore());
1614 EXPECT_TRUE(GetCryptographer()->is_ready());
1615 EXPECT_EQ(FROZEN_IMPLICIT_PASSPHRASE,
1616 encryption_handler()->GetPassphraseType());
1617 EXPECT_TRUE(encryption_handler()->EncryptEverythingEnabled());
1618 VerifyMigratedNigoriWithTimestamp(1, FROZEN_IMPLICIT_PASSPHRASE, kCurKey);
1619
1620 // Check that the cryptographer is encrypting using the frozen current key.
1621 sync_pb::EncryptedData current_encrypted;
1622 other_cryptographer.EncryptString("string", &current_encrypted);
1623 EXPECT_TRUE(GetCryptographer()->CanDecryptUsingDefaultKey(current_encrypted));
1624
1625 // Check that the cryptographer can still decrypt keystore key based
1626 // encryption (due to extracting the keystore key from the encryption keybag).
1627 Cryptographer keystore_cryptographer(GetCryptographer()->encryptor());
1628 keystore_cryptographer.AddKey(keystore_key);
1629 sync_pb::EncryptedData keystore_encrypted;
1630 keystore_cryptographer.EncryptString("string", &keystore_encrypted);
1631 EXPECT_TRUE(GetCryptographer()->CanDecrypt(keystore_encrypted));
1632 }
1633
440 } // namespace syncer 1634 } // namespace syncer
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698