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

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

Powered by Google App Engine
This is Rietveld 408576698