nigori_sync_bridge_impl_unittest.cc 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/sync/nigori/nigori_sync_bridge_impl.h"
  5. #include <utility>
  6. #include "base/base64.h"
  7. #include "base/bind.h"
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/strings/string_util.h"
  10. #include "base/test/scoped_feature_list.h"
  11. #include "components/os_crypt/os_crypt_mocker.h"
  12. #include "components/sync/base/time.h"
  13. #include "components/sync/engine/nigori/key_derivation_params.h"
  14. #include "components/sync/nigori/keystore_keys_cryptographer.h"
  15. #include "components/sync/nigori/nigori_state.h"
  16. #include "components/sync/nigori/nigori_storage.h"
  17. #include "components/sync/nigori/nigori_test_utils.h"
  18. #include "components/sync/protocol/entity_data.h"
  19. #include "testing/gmock/include/gmock/gmock.h"
  20. #include "testing/gtest/include/gtest/gtest.h"
  21. namespace syncer {
  22. namespace {
  23. using testing::_;
  24. using testing::Eq;
  25. using testing::Ne;
  26. using testing::Not;
  27. using testing::NotNull;
  28. using testing::Return;
  29. const char kNigoriKeyName[] = "nigori-key";
  30. NigoriMetadataBatch CreateDummyNigoriMetadataBatch(
  31. const std::string& progress_marker_token,
  32. int64_t entity_metadata_sequence_number);
  33. MATCHER(NullTime, "") {
  34. return arg.is_null();
  35. }
  36. MATCHER_P(HasDefaultKeyDerivedFrom, key_params, "") {
  37. const Cryptographer& cryptographer = arg;
  38. std::unique_ptr<Nigori> expected_default_nigori = Nigori::CreateByDerivation(
  39. key_params.derivation_params, key_params.password);
  40. std::string expected_default_key_name;
  41. EXPECT_TRUE(expected_default_nigori->Permute(
  42. Nigori::Type::Password, kNigoriKeyName, &expected_default_key_name));
  43. return cryptographer.GetDefaultEncryptionKeyName() ==
  44. expected_default_key_name;
  45. }
  46. MATCHER(HasKeystoreNigori, "") {
  47. const std::unique_ptr<EntityData>& entity_data = arg;
  48. if (!entity_data || !entity_data->specifics.has_nigori()) {
  49. return false;
  50. }
  51. const sync_pb::NigoriSpecifics& specifics = entity_data->specifics.nigori();
  52. if (specifics.passphrase_type() !=
  53. sync_pb::NigoriSpecifics::KEYSTORE_PASSPHRASE) {
  54. return false;
  55. }
  56. return !specifics.encryption_keybag().blob().empty() &&
  57. !specifics.keystore_decryptor_token().blob().empty() &&
  58. specifics.keybag_is_frozen() &&
  59. specifics.has_keystore_migration_time();
  60. }
  61. MATCHER(HasCustomPassphraseNigori, "") {
  62. const std::unique_ptr<EntityData>& entity_data = arg;
  63. if (!entity_data || !entity_data->specifics.has_nigori()) {
  64. return false;
  65. }
  66. const sync_pb::NigoriSpecifics& specifics = entity_data->specifics.nigori();
  67. return specifics.passphrase_type() ==
  68. sync_pb::NigoriSpecifics::CUSTOM_PASSPHRASE &&
  69. !specifics.encryption_keybag().blob().empty() &&
  70. !specifics.has_keystore_decryptor_token() &&
  71. specifics.encrypt_everything() && specifics.keybag_is_frozen() &&
  72. specifics.has_custom_passphrase_time() &&
  73. specifics.has_custom_passphrase_key_derivation_method();
  74. }
  75. MATCHER_P(CanDecryptWith, key_params, "") {
  76. const Cryptographer& cryptographer = arg;
  77. std::unique_ptr<Nigori> nigori = Nigori::CreateByDerivation(
  78. key_params.derivation_params, key_params.password);
  79. std::string nigori_name;
  80. EXPECT_TRUE(
  81. nigori->Permute(Nigori::Type::Password, kNigoriKeyName, &nigori_name));
  82. const std::string unencrypted = "test";
  83. sync_pb::EncryptedData encrypted;
  84. encrypted.set_key_name(nigori_name);
  85. EXPECT_TRUE(nigori->Encrypt(unencrypted, encrypted.mutable_blob()));
  86. if (!cryptographer.CanDecrypt(encrypted)) {
  87. return false;
  88. }
  89. std::string decrypted;
  90. if (!cryptographer.DecryptToString(encrypted, &decrypted)) {
  91. return false;
  92. }
  93. return decrypted == unencrypted;
  94. }
  95. MATCHER_P(EncryptedDataEq, expected, "") {
  96. const sync_pb::EncryptedData& given = arg;
  97. return given.key_name() == expected.key_name() &&
  98. given.blob() == expected.blob();
  99. }
  100. MATCHER_P3(EncryptedDataEqAfterDecryption,
  101. expected,
  102. password,
  103. derivation_params,
  104. "") {
  105. const sync_pb::EncryptedData& given = arg;
  106. std::unique_ptr<CryptographerImpl> cryptographer =
  107. CryptographerImpl::FromSingleKeyForTesting(password, derivation_params);
  108. std::string decrypted_given;
  109. EXPECT_TRUE(cryptographer->DecryptToString(given, &decrypted_given));
  110. std::string decrypted_expected;
  111. EXPECT_TRUE(cryptographer->DecryptToString(expected, &decrypted_expected));
  112. return decrypted_given == decrypted_expected;
  113. }
  114. MATCHER_P2(IsDummyNigoriMetadataBatchWithTokenAndSequenceNumber,
  115. expected_token,
  116. expected_sequence_number,
  117. "") {
  118. const NigoriMetadataBatch& given = arg;
  119. NigoriMetadataBatch expected =
  120. CreateDummyNigoriMetadataBatch(expected_token, expected_sequence_number);
  121. if (given.model_type_state.SerializeAsString() !=
  122. expected.model_type_state.SerializeAsString()) {
  123. return false;
  124. }
  125. if (!given.entity_metadata.has_value()) {
  126. return !expected.entity_metadata.has_value();
  127. }
  128. return given.entity_metadata->SerializeAsString() ==
  129. expected.entity_metadata->SerializeAsString();
  130. }
  131. NigoriMetadataBatch CreateDummyNigoriMetadataBatch(
  132. const std::string& progress_marker_token,
  133. int64_t entity_metadata_sequence_number) {
  134. NigoriMetadataBatch metadata_batch;
  135. metadata_batch.model_type_state.mutable_progress_marker()->set_token(
  136. progress_marker_token);
  137. metadata_batch.entity_metadata = sync_pb::EntityMetadata::default_instance();
  138. metadata_batch.entity_metadata->set_sequence_number(
  139. entity_metadata_sequence_number);
  140. return metadata_batch;
  141. }
  142. std::unique_ptr<Nigori> MakeNigoriKey(const KeyParamsForTesting& key_params) {
  143. return Nigori::CreateByDerivation(key_params.derivation_params,
  144. key_params.password);
  145. }
  146. KeyDerivationParams MakeCustomPassphraseKeyDerivationParams() {
  147. return KeyDerivationParams::CreateForScrypt("salt");
  148. }
  149. class MockNigoriLocalChangeProcessor : public NigoriLocalChangeProcessor {
  150. public:
  151. MockNigoriLocalChangeProcessor() = default;
  152. ~MockNigoriLocalChangeProcessor() override = default;
  153. MOCK_METHOD(void,
  154. ModelReadyToSync,
  155. (NigoriSyncBridge*, NigoriMetadataBatch),
  156. (override));
  157. MOCK_METHOD(void, Put, (std::unique_ptr<EntityData>), (override));
  158. MOCK_METHOD(bool, IsEntityUnsynced, (), (override));
  159. MOCK_METHOD(NigoriMetadataBatch, GetMetadata, (), (override));
  160. MOCK_METHOD(void, ReportError, (const ModelError&), (override));
  161. MOCK_METHOD(base::WeakPtr<ModelTypeControllerDelegate>,
  162. GetControllerDelegate,
  163. (),
  164. (override));
  165. MOCK_METHOD(bool, IsTrackingMetadata, (), (override));
  166. };
  167. class MockObserver : public SyncEncryptionHandler::Observer {
  168. public:
  169. MockObserver() = default;
  170. ~MockObserver() override = default;
  171. MOCK_METHOD(void,
  172. OnPassphraseRequired,
  173. (const KeyDerivationParams&, const sync_pb::EncryptedData&),
  174. (override));
  175. MOCK_METHOD(void, OnPassphraseAccepted, (), (override));
  176. MOCK_METHOD(void, OnTrustedVaultKeyRequired, (), (override));
  177. MOCK_METHOD(void, OnTrustedVaultKeyAccepted, (), (override));
  178. MOCK_METHOD(void, OnEncryptedTypesChanged, (ModelTypeSet, bool), (override));
  179. MOCK_METHOD(void,
  180. OnCryptographerStateChanged,
  181. (Cryptographer*, bool has_pending_keys),
  182. (override));
  183. MOCK_METHOD(void,
  184. OnPassphraseTypeChanged,
  185. (PassphraseType, base::Time),
  186. (override));
  187. };
  188. class MockNigoriStorage : public NigoriStorage {
  189. public:
  190. MockNigoriStorage() = default;
  191. ~MockNigoriStorage() override = default;
  192. MOCK_METHOD(void, StoreData, (const sync_pb::NigoriLocalData&), (override));
  193. MOCK_METHOD(absl::optional<sync_pb::NigoriLocalData>,
  194. RestoreData,
  195. (),
  196. (override));
  197. MOCK_METHOD(void, ClearData, (), (override));
  198. };
  199. class NigoriSyncBridgeImplTest : public testing::Test {
  200. protected:
  201. NigoriSyncBridgeImplTest() {
  202. auto processor =
  203. std::make_unique<testing::NiceMock<MockNigoriLocalChangeProcessor>>();
  204. ON_CALL(*processor, IsTrackingMetadata()).WillByDefault(Return(true));
  205. processor_ = processor.get();
  206. auto storage = std::make_unique<testing::NiceMock<MockNigoriStorage>>();
  207. storage_ = storage.get();
  208. bridge_ = std::make_unique<NigoriSyncBridgeImpl>(std::move(processor),
  209. std::move(storage));
  210. bridge_->AddObserver(&observer_);
  211. }
  212. ~NigoriSyncBridgeImplTest() override { bridge_->RemoveObserver(&observer_); }
  213. void SetUp() override { OSCryptMocker::SetUp(); }
  214. void TearDown() override { OSCryptMocker::TearDown(); }
  215. NigoriSyncBridgeImpl* bridge() { return bridge_.get(); }
  216. MockNigoriLocalChangeProcessor* processor() { return processor_; }
  217. MockObserver* observer() { return &observer_; }
  218. MockNigoriStorage* storage() { return storage_; }
  219. Cryptographer* cryptographer() { return bridge_->GetCryptographer(); }
  220. const std::vector<uint8_t> kRawKeystoreKey = {0, 1, 2, 3, 4};
  221. const std::vector<uint8_t> kTrustedVaultKey = {2, 3, 4, 5, 6};
  222. private:
  223. std::unique_ptr<NigoriSyncBridgeImpl> bridge_;
  224. // Ownership transferred to |bridge_|.
  225. raw_ptr<testing::NiceMock<MockNigoriLocalChangeProcessor>> processor_;
  226. raw_ptr<testing::NiceMock<MockNigoriStorage>> storage_;
  227. testing::NiceMock<MockObserver> observer_;
  228. };
  229. class NigoriSyncBridgeImplTestWithOptionalScryptDerivation
  230. : public NigoriSyncBridgeImplTest,
  231. public testing::WithParamInterface<bool> {
  232. public:
  233. NigoriSyncBridgeImplTestWithOptionalScryptDerivation()
  234. : key_params_(GetParam()
  235. ? ScryptPassphraseKeyParamsForTesting("passphrase")
  236. : Pbkdf2PassphraseKeyParamsForTesting("passphrase")) {}
  237. const KeyParamsForTesting& GetCustomPassphraseKeyParams() const {
  238. return key_params_;
  239. }
  240. private:
  241. const KeyParamsForTesting key_params_;
  242. };
  243. class NigoriSyncBridgeImplPersistenceTest : public testing::Test {
  244. protected:
  245. NigoriSyncBridgeImplPersistenceTest() = default;
  246. ~NigoriSyncBridgeImplPersistenceTest() override = default;
  247. void SetUp() override { OSCryptMocker::SetUp(); }
  248. void TearDown() override { OSCryptMocker::TearDown(); }
  249. };
  250. // During initialization bridge should expose encrypted types via observers
  251. // notification.
  252. TEST_F(NigoriSyncBridgeImplTest, ShouldNotifyObserversOnInit) {
  253. // TODO(crbug.com/922900): once persistence is supported for Nigori, this
  254. // test should be extended to verify whole encryption state.
  255. EXPECT_CALL(*observer(),
  256. OnEncryptedTypesChanged(AlwaysEncryptedUserTypes(),
  257. /*encrypt_everything=*/false));
  258. bridge()->NotifyInitialStateToObservers();
  259. }
  260. // Tests that bridge support Nigori with IMPLICIT_PASSPHRASE.
  261. TEST_F(NigoriSyncBridgeImplTest, ShouldAcceptKeysFromImplicitPassphraseNigori) {
  262. const KeyParamsForTesting kKeyParams =
  263. Pbkdf2PassphraseKeyParamsForTesting("password");
  264. std::unique_ptr<CryptographerImpl> temp_cryptographer =
  265. CryptographerImpl::FromSingleKeyForTesting(kKeyParams.password,
  266. kKeyParams.derivation_params);
  267. EntityData entity_data;
  268. *entity_data.specifics.mutable_nigori() =
  269. sync_pb::NigoriSpecifics::default_instance();
  270. ASSERT_TRUE(temp_cryptographer->Encrypt(
  271. temp_cryptographer->ToProto().key_bag(),
  272. entity_data.specifics.mutable_nigori()->mutable_encryption_keybag()));
  273. EXPECT_CALL(*observer(), OnCryptographerStateChanged(
  274. NotNull(), /*has_pending_keys=*/true));
  275. EXPECT_CALL(
  276. *observer(),
  277. OnPassphraseRequired(
  278. /*key_derivation_params=*/KeyDerivationParams::CreateForPbkdf2(),
  279. /*pending_keys=*/
  280. EncryptedDataEq(entity_data.specifics.nigori().encryption_keybag())));
  281. EXPECT_THAT(bridge()->MergeSyncData(std::move(entity_data)),
  282. Eq(absl::nullopt));
  283. testing::InSequence seq;
  284. EXPECT_CALL(*observer(), OnCryptographerStateChanged(
  285. NotNull(), /*has_pending_keys=*/false));
  286. EXPECT_CALL(*observer(), OnPassphraseAccepted());
  287. bridge()->SetExplicitPassphraseDecryptionKey(MakeNigoriKey(kKeyParams));
  288. EXPECT_THAT(*cryptographer(), CanDecryptWith(kKeyParams));
  289. EXPECT_THAT(*cryptographer(), HasDefaultKeyDerivedFrom(kKeyParams));
  290. }
  291. // Simplest case of keystore Nigori: we have only one keystore key and no old
  292. // keys. This keystore key is encrypted in both encryption_keybag and
  293. // keystore_decryptor_token. Client receives such Nigori if initialization of
  294. // Nigori node was done after keystore was introduced and no key rotations
  295. // happened.
  296. TEST_F(NigoriSyncBridgeImplTest,
  297. ShouldAcceptKeysFromKeystoreNigoriAndNotifyObservers) {
  298. const KeyParamsForTesting kKeystoreKeyParams =
  299. KeystoreKeyParamsForTesting(kRawKeystoreKey);
  300. EntityData entity_data;
  301. *entity_data.specifics.mutable_nigori() = BuildKeystoreNigoriSpecifics(
  302. /*keybag_keys_params=*/{kKeystoreKeyParams},
  303. /*keystore_decryptor_params=*/kKeystoreKeyParams,
  304. /*keystore_key_params=*/kKeystoreKeyParams);
  305. EXPECT_CALL(*observer(), OnPassphraseRequired).Times(0);
  306. EXPECT_CALL(*observer(), OnTrustedVaultKeyRequired()).Times(0);
  307. EXPECT_TRUE(bridge()->SetKeystoreKeys({kRawKeystoreKey}));
  308. // The current implementation issues a redundant notification.
  309. EXPECT_CALL(*observer(), OnCryptographerStateChanged(
  310. NotNull(), /*has_pending_keys=*/false))
  311. .Times(2);
  312. EXPECT_THAT(bridge()->MergeSyncData(std::move(entity_data)),
  313. Eq(absl::nullopt));
  314. bridge()->NotifyInitialStateToObservers();
  315. EXPECT_THAT(bridge()->GetKeystoreMigrationTime(), Not(NullTime()));
  316. EXPECT_THAT(*cryptographer(), CanDecryptWith(kKeystoreKeyParams));
  317. EXPECT_THAT(*cryptographer(), HasDefaultKeyDerivedFrom(kKeystoreKeyParams));
  318. }
  319. // Tests that client can properly process remote updates with rotated keystore
  320. // nigori. Cryptographer should be able to decrypt any data encrypted with any
  321. // keystore key and use current keystore key as default key.
  322. TEST_F(NigoriSyncBridgeImplTest, ShouldAcceptKeysFromRotatedKeystoreNigori) {
  323. const std::vector<uint8_t> kRawOldKey = {5, 6, 7, 8};
  324. const KeyParamsForTesting kOldKeyParams =
  325. KeystoreKeyParamsForTesting(kRawOldKey);
  326. const std::vector<uint8_t> kRawCurrentKey{kRawKeystoreKey};
  327. const KeyParamsForTesting kCurrentKeyParams =
  328. KeystoreKeyParamsForTesting(kRawCurrentKey);
  329. EntityData entity_data;
  330. *entity_data.specifics.mutable_nigori() = BuildKeystoreNigoriSpecifics(
  331. /*keybag_keys_params=*/{kOldKeyParams, kCurrentKeyParams},
  332. /*keystore_decryptor_params=*/kCurrentKeyParams,
  333. /*keystore_key_params=*/kCurrentKeyParams);
  334. EXPECT_TRUE(bridge()->SetKeystoreKeys({kRawOldKey, kRawCurrentKey}));
  335. EXPECT_THAT(bridge()->MergeSyncData(std::move(entity_data)),
  336. Eq(absl::nullopt));
  337. EXPECT_THAT(*cryptographer(), CanDecryptWith(kOldKeyParams));
  338. EXPECT_THAT(*cryptographer(), CanDecryptWith(kCurrentKeyParams));
  339. EXPECT_THAT(*cryptographer(), HasDefaultKeyDerivedFrom(kCurrentKeyParams));
  340. }
  341. // In the backward compatible mode keystore Nigori's keystore_decryptor_token
  342. // isn't a kestore key, however keystore_decryptor_token itself should be
  343. // encrypted with the keystore key.
  344. TEST_F(NigoriSyncBridgeImplTest,
  345. ShouldAcceptKeysFromBackwardCompatibleKeystoreNigori) {
  346. const KeyParamsForTesting kGaiaKeyParams =
  347. Pbkdf2PassphraseKeyParamsForTesting("gaia_key");
  348. const KeyParamsForTesting kKeystoreKeyParams =
  349. KeystoreKeyParamsForTesting(kRawKeystoreKey);
  350. EntityData entity_data;
  351. *entity_data.specifics.mutable_nigori() = BuildKeystoreNigoriSpecifics(
  352. /*keybag_keys_params=*/{kGaiaKeyParams, kKeystoreKeyParams},
  353. /*keystore_decryptor_params=*/kGaiaKeyParams,
  354. /*keystore_key_params=*/kKeystoreKeyParams);
  355. EXPECT_TRUE(bridge()->SetKeystoreKeys({kRawKeystoreKey}));
  356. EXPECT_THAT(bridge()->MergeSyncData(std::move(entity_data)),
  357. Eq(absl::nullopt));
  358. EXPECT_THAT(*cryptographer(), CanDecryptWith(kGaiaKeyParams));
  359. EXPECT_THAT(*cryptographer(), CanDecryptWith(kKeystoreKeyParams));
  360. EXPECT_THAT(*cryptographer(), HasDefaultKeyDerivedFrom(kGaiaKeyParams));
  361. }
  362. // Tests that we can successfully use old keys from encryption_keybag in
  363. // backward compatible mode.
  364. TEST_F(NigoriSyncBridgeImplTest,
  365. ShouldAcceptOldKeysFromBackwardCompatibleKeystoreNigori) {
  366. // |kOldKeyParams| is needed to ensure we was able to decrypt
  367. // encryption_keybag - there is no way to add key derived from
  368. // |kOldKeyParams| to cryptographer without decrypting encryption_keybag.
  369. const KeyParamsForTesting kOldKeyParams =
  370. Pbkdf2PassphraseKeyParamsForTesting("old_key");
  371. const KeyParamsForTesting kCurrentKeyParams =
  372. Pbkdf2PassphraseKeyParamsForTesting("current_key");
  373. const KeyParamsForTesting kKeystoreKeyParams =
  374. KeystoreKeyParamsForTesting(kRawKeystoreKey);
  375. const std::vector<KeyParamsForTesting> kAllKeyParams = {
  376. kOldKeyParams, kCurrentKeyParams, kKeystoreKeyParams};
  377. EntityData entity_data;
  378. *entity_data.specifics.mutable_nigori() = BuildKeystoreNigoriSpecifics(
  379. /*keybag_keys_params=*/kAllKeyParams,
  380. /*keystore_decryptor_params=*/kCurrentKeyParams,
  381. /*keystore_key_params=*/kKeystoreKeyParams);
  382. EXPECT_TRUE(bridge()->SetKeystoreKeys({kRawKeystoreKey}));
  383. EXPECT_THAT(bridge()->MergeSyncData(std::move(entity_data)),
  384. Eq(absl::nullopt));
  385. for (const KeyParamsForTesting& key_params : kAllKeyParams) {
  386. EXPECT_THAT(*cryptographer(), CanDecryptWith(key_params));
  387. }
  388. EXPECT_THAT(*cryptographer(), HasDefaultKeyDerivedFrom(kCurrentKeyParams));
  389. }
  390. // Tests that we build keystore Nigori, put it to processor, initialize the
  391. // cryptographer and expose a valid entity through GetData(), when the default
  392. // Nigori is received.
  393. TEST_F(NigoriSyncBridgeImplTest,
  394. ShouldPutAndMakeCryptographerReadyOnDefaultNigori) {
  395. const KeyParamsForTesting kKeystoreKeyParams =
  396. KeystoreKeyParamsForTesting(kRawKeystoreKey);
  397. EntityData default_entity_data;
  398. *default_entity_data.specifics.mutable_nigori() =
  399. sync_pb::NigoriSpecifics::default_instance();
  400. EXPECT_TRUE(bridge()->SetKeystoreKeys({kRawKeystoreKey}));
  401. // We don't verify entire NigoriSpecifics here, because it requires too
  402. // complex matcher (NigoriSpecifics is not determenistic).
  403. // Calling MergeSyncData() triggers a commit cycle but doesn't immediately
  404. // expose the new state, until the commit completes.
  405. EXPECT_CALL(*processor(), Put(HasKeystoreNigori()));
  406. EXPECT_THAT(bridge()->MergeSyncData(std::move(default_entity_data)),
  407. Eq(absl::nullopt));
  408. EXPECT_THAT(bridge()->GetData(), HasKeystoreNigori());
  409. EXPECT_THAT(bridge()->ApplySyncChanges(absl::nullopt), Eq(absl::nullopt));
  410. EXPECT_THAT(bridge()->GetData(), HasKeystoreNigori());
  411. EXPECT_THAT(bridge()->GetKeystoreMigrationTime(), Not(NullTime()));
  412. EXPECT_EQ(PassphraseType::kKeystorePassphrase, bridge()->GetPassphraseType());
  413. EXPECT_THAT(*cryptographer(), CanDecryptWith(kKeystoreKeyParams));
  414. EXPECT_THAT(*cryptographer(), HasDefaultKeyDerivedFrom(kKeystoreKeyParams));
  415. }
  416. // Tests that upon receiving Nigori corrupted due to absence of
  417. // |encryption_keybag|, bridge respect its passphrase type and doesn't attempt
  418. // to trigger keystore initialization.
  419. TEST_F(NigoriSyncBridgeImplTest,
  420. ShouldNotTriggerKeystoreInitializationForCorruptedCustomPassphrase) {
  421. const KeyParamsForTesting kKeystoreKeyParams =
  422. KeystoreKeyParamsForTesting(kRawKeystoreKey);
  423. EntityData entity_data;
  424. *entity_data.specifics.mutable_nigori() =
  425. sync_pb::NigoriSpecifics::default_instance();
  426. entity_data.specifics.mutable_nigori()->set_passphrase_type(
  427. sync_pb::NigoriSpecifics::CUSTOM_PASSPHRASE);
  428. EXPECT_TRUE(bridge()->SetKeystoreKeys({kRawKeystoreKey}));
  429. // There should be no commits.
  430. EXPECT_CALL(*processor(), Put).Times(0);
  431. // Model error should be reported, because there is no |encryption_keybag|.
  432. EXPECT_THAT(bridge()->MergeSyncData(std::move(entity_data)),
  433. Ne(absl::nullopt));
  434. }
  435. TEST_F(NigoriSyncBridgeImplTest, ShouldRotateKeystoreKey) {
  436. const std::vector<uint8_t> kRawKeystoreKey1{kRawKeystoreKey};
  437. const KeyParamsForTesting kKeystoreKeyParams1 =
  438. KeystoreKeyParamsForTesting(kRawKeystoreKey1);
  439. sync_pb::NigoriSpecifics not_rotated_specifics = BuildKeystoreNigoriSpecifics(
  440. /*keybag_keys_params=*/{kKeystoreKeyParams1},
  441. /*keystore_decryptor_params=*/kKeystoreKeyParams1,
  442. /*keystore_key_params=*/kKeystoreKeyParams1);
  443. EntityData entity_data;
  444. *entity_data.specifics.mutable_nigori() = not_rotated_specifics;
  445. ASSERT_TRUE(bridge()->SetKeystoreKeys({kRawKeystoreKey1}));
  446. ASSERT_THAT(bridge()->MergeSyncData(std::move(entity_data)),
  447. Eq(absl::nullopt));
  448. const std::vector<uint8_t> kRawKeystoreKey2 = {5, 6, 7, 8};
  449. const KeyParamsForTesting kKeystoreKeyParams2 =
  450. KeystoreKeyParamsForTesting(kRawKeystoreKey2);
  451. // Emulate server and client behavior: server sends both keystore keys and
  452. // |not_rotated_specifics| with changed metadata. Client have already seen
  453. // this specifics, but should pass it to the bridge, because bridge also
  454. // issues a commit, which conflicts with |not_rotated_specifics|.
  455. // Ensure bridge issues a commit right after SetKeystoreKeys() call, because
  456. // otherwise there is no conflict and ApplySyncChanges() will be called with
  457. // empty |data|.
  458. EXPECT_CALL(*processor(), Put(HasKeystoreNigori()));
  459. EXPECT_TRUE(bridge()->SetKeystoreKeys({kRawKeystoreKey1, kRawKeystoreKey2}));
  460. // Populate new remote specifics to bridge, which is actually still
  461. // |not_rotated_specifics|.
  462. *entity_data.specifics.mutable_nigori() = not_rotated_specifics;
  463. EXPECT_CALL(*processor(), Put(HasKeystoreNigori()));
  464. EXPECT_THAT(bridge()->ApplySyncChanges(std::move(entity_data)),
  465. Eq(absl::nullopt));
  466. // Mimic commit completion.
  467. EXPECT_CALL(*observer(), OnCryptographerStateChanged(
  468. NotNull(), /*has_pending_keys=*/false));
  469. EXPECT_THAT(bridge()->ApplySyncChanges(absl::nullopt), Eq(absl::nullopt));
  470. EXPECT_THAT(bridge()->GetData(), HasKeystoreNigori());
  471. EXPECT_THAT(*cryptographer(), CanDecryptWith(kKeystoreKeyParams1));
  472. EXPECT_THAT(*cryptographer(), CanDecryptWith(kKeystoreKeyParams2));
  473. EXPECT_THAT(*cryptographer(), HasDefaultKeyDerivedFrom(kKeystoreKeyParams2));
  474. }
  475. // This test emulates late arrival of keystore keys, so neither
  476. // |keystore_decryptor_token| or |encryption_keybag| could be decrypted at the
  477. // moment NigoriSpecifics arrived. They should be decrypted right after
  478. // keystore keys arrival.
  479. TEST_F(NigoriSyncBridgeImplTest, ShouldDecryptPendingKeysInKeystoreMode) {
  480. const KeyParamsForTesting kKeystoreKeyParams =
  481. KeystoreKeyParamsForTesting(kRawKeystoreKey);
  482. EntityData entity_data;
  483. *entity_data.specifics.mutable_nigori() = BuildKeystoreNigoriSpecifics(
  484. /*keybag_keys_params=*/{kKeystoreKeyParams},
  485. /*keystore_decryptor_params=*/kKeystoreKeyParams,
  486. /*keystore_key_params=*/kKeystoreKeyParams);
  487. EXPECT_CALL(*observer(), OnCryptographerStateChanged(
  488. NotNull(), /*has_pending_keys=*/true));
  489. EXPECT_CALL(
  490. *observer(),
  491. OnPassphraseRequired(
  492. /*key_derivation_params=*/KeyDerivationParams::CreateForPbkdf2(),
  493. /*pending_keys=*/
  494. EncryptedDataEq(entity_data.specifics.nigori().encryption_keybag())));
  495. EXPECT_THAT(bridge()->MergeSyncData(std::move(entity_data)),
  496. Eq(absl::nullopt));
  497. EXPECT_FALSE(cryptographer()->CanEncrypt());
  498. testing::InSequence seq;
  499. EXPECT_CALL(*observer(), OnCryptographerStateChanged(
  500. NotNull(), /*has_pending_keys=*/false));
  501. EXPECT_CALL(*observer(), OnPassphraseAccepted());
  502. EXPECT_TRUE(bridge()->SetKeystoreKeys({kRawKeystoreKey}));
  503. EXPECT_THAT(*cryptographer(), CanDecryptWith(kKeystoreKeyParams));
  504. EXPECT_THAT(*cryptographer(), HasDefaultKeyDerivedFrom(kKeystoreKeyParams));
  505. }
  506. // This test emulates late arrival of keystore keys in backward-compatible
  507. // keystore mode, so neither |keystore_decryptor_token| or |encryption_keybag|
  508. // could be decrypted at the moment NigoriSpecifics arrived. Since default key
  509. // is derived from legacy implicit passphrase, pending keys should be decrypted
  510. // once passphrase passed to SetExplicitPassphraseDecryptionKey().
  511. // SetKeystoreKeys() intentionally not called in this test, to not allow
  512. // decryption with |keystore_decryptor_token|.
  513. TEST_F(NigoriSyncBridgeImplTest,
  514. ShouldDecryptPendingKeysWithPassphraseInKeystoreMode) {
  515. const KeyParamsForTesting kKeystoreKeyParams =
  516. KeystoreKeyParamsForTesting(kRawKeystoreKey);
  517. const KeyParamsForTesting kPassphraseKeyParams =
  518. Pbkdf2PassphraseKeyParamsForTesting("passphrase");
  519. EntityData entity_data;
  520. *entity_data.specifics.mutable_nigori() = BuildKeystoreNigoriSpecifics(
  521. /*keybag_keys_params=*/{kKeystoreKeyParams, kPassphraseKeyParams},
  522. /*keystore_decryptor_params=*/kPassphraseKeyParams,
  523. /*keystore_key_params=*/kKeystoreKeyParams);
  524. ASSERT_THAT(bridge()->MergeSyncData(std::move(entity_data)),
  525. Eq(absl::nullopt));
  526. testing::InSequence seq;
  527. EXPECT_CALL(*observer(), OnCryptographerStateChanged(
  528. NotNull(), /*has_pending_keys=*/false));
  529. EXPECT_CALL(*observer(), OnPassphraseAccepted());
  530. bridge()->SetExplicitPassphraseDecryptionKey(
  531. MakeNigoriKey(kPassphraseKeyParams));
  532. EXPECT_THAT(*cryptographer(), CanDecryptWith(kKeystoreKeyParams));
  533. EXPECT_THAT(*cryptographer(), CanDecryptWith(kPassphraseKeyParams));
  534. EXPECT_THAT(*cryptographer(), HasDefaultKeyDerivedFrom(kPassphraseKeyParams));
  535. // Regression part of the test, SetKeystoreKeys() call in this scenario used
  536. // to cause the crash (see crbug.com/1042203).
  537. EXPECT_TRUE(bridge()->SetKeystoreKeys({kRawKeystoreKey}));
  538. EXPECT_FALSE(bridge()->NeedKeystoreKey());
  539. }
  540. // Tests that unsuccessful attempt of |pending_keys| decryption ends up in
  541. // additional OnPassphraseRequired() call. This is allowed because of possible
  542. // change of |pending_keys| in keystore mode or due to transition from keystore
  543. // to custom passphrase.
  544. TEST_F(NigoriSyncBridgeImplTest,
  545. ShouldNotifyWhenDecryptionWithPassphraseFailed) {
  546. const KeyParamsForTesting kKeystoreKeyParams =
  547. KeystoreKeyParamsForTesting(kRawKeystoreKey);
  548. EntityData entity_data;
  549. *entity_data.specifics.mutable_nigori() = BuildKeystoreNigoriSpecifics(
  550. /*keybag_keys_params=*/{kKeystoreKeyParams},
  551. /*keystore_decryptor_params=*/kKeystoreKeyParams,
  552. /*keystore_key_params=*/kKeystoreKeyParams);
  553. sync_pb::EncryptedData expected_pending_keys =
  554. entity_data.specifics.nigori().encryption_keybag();
  555. ASSERT_THAT(bridge()->MergeSyncData(std::move(entity_data)),
  556. Eq(absl::nullopt));
  557. EXPECT_CALL(
  558. *observer(),
  559. OnPassphraseRequired(
  560. /*key_derivation_params=*/KeyDerivationParams::CreateForPbkdf2(),
  561. /*pending_keys=*/
  562. EncryptedDataEq(expected_pending_keys)));
  563. bridge()->SetExplicitPassphraseDecryptionKey(
  564. MakeNigoriKey(Pbkdf2PassphraseKeyParamsForTesting("wrong_passphrase")));
  565. EXPECT_THAT(bridge()->GetCryptographerImplForTesting().KeyBagSizeForTesting(),
  566. Eq(size_t(0)));
  567. }
  568. // Tests that attempt to SetEncryptionPassphrase() has no effect (at least
  569. // that bridge's Nigori is still keystore one) if it was called, while bridge
  570. // has pending keys in keystore mode.
  571. TEST_F(NigoriSyncBridgeImplTest,
  572. ShouldNotSetEncryptionPassphraseWithPendingKeys) {
  573. const KeyParamsForTesting kKeystoreKeyParams =
  574. KeystoreKeyParamsForTesting(kRawKeystoreKey);
  575. EntityData entity_data;
  576. *entity_data.specifics.mutable_nigori() = BuildKeystoreNigoriSpecifics(
  577. /*keybag_keys_params=*/{kKeystoreKeyParams},
  578. /*keystore_decryptor_params=*/kKeystoreKeyParams,
  579. /*keystore_key_params=*/kKeystoreKeyParams);
  580. ASSERT_THAT(bridge()->MergeSyncData(std::move(entity_data)),
  581. Eq(absl::nullopt));
  582. bridge()->SetEncryptionPassphrase("passphrase",
  583. MakeCustomPassphraseKeyDerivationParams());
  584. bridge()->SetKeystoreKeys({kRawKeystoreKey});
  585. // TODO(crbug.com/922900): revisit expectations once conflict resolution is
  586. // implemented. They might be not properly working with deferred state
  587. // change.
  588. EXPECT_THAT(bridge()->GetData(), HasKeystoreNigori());
  589. EXPECT_THAT(*cryptographer(), CanDecryptWith(kKeystoreKeyParams));
  590. EXPECT_THAT(*cryptographer(), HasDefaultKeyDerivedFrom(kKeystoreKeyParams));
  591. }
  592. // Tests that we can perform initial sync with custom passphrase Nigori.
  593. // We should notify observers about encryption state changes and cryptographer
  594. // shouldn't be ready (by having pending keys) until user provides the
  595. // passphrase.
  596. TEST_F(NigoriSyncBridgeImplTest,
  597. ShouldNotifyWhenSyncedWithCustomPassphraseNigori) {
  598. EntityData entity_data;
  599. *entity_data.specifics.mutable_nigori() =
  600. BuildCustomPassphraseNigoriSpecifics(
  601. Pbkdf2PassphraseKeyParamsForTesting("passphrase"));
  602. EXPECT_CALL(*observer(), OnTrustedVaultKeyRequired()).Times(0);
  603. ASSERT_TRUE(bridge()->SetKeystoreKeys({kRawKeystoreKey}));
  604. // The current implementation issues redundant notifications.
  605. EXPECT_CALL(*observer(), OnEncryptedTypesChanged(
  606. /*encrypted_types=*/EncryptableUserTypes(),
  607. /*encrypt_everything=*/true))
  608. .Times(2);
  609. EXPECT_CALL(*observer(),
  610. OnCryptographerStateChanged(NotNull(), /*has_pending_keys=*/true))
  611. .Times(2);
  612. EXPECT_CALL(*observer(),
  613. OnPassphraseTypeChanged(PassphraseType::kCustomPassphrase,
  614. Not(NullTime())))
  615. .Times(2);
  616. EXPECT_THAT(bridge()->MergeSyncData(std::move(entity_data)),
  617. Eq(absl::nullopt));
  618. bridge()->NotifyInitialStateToObservers();
  619. EXPECT_TRUE(bridge()->HasPendingKeysForTesting());
  620. }
  621. // Tests that we can process remote update with custom passphrase Nigori, while
  622. // we already have keystore Nigori locally.
  623. // We should notify observers about encryption state changes and cryptographer
  624. // shouldn't be ready (by having pending keys) until user provides the
  625. // passphrase.
  626. TEST_F(NigoriSyncBridgeImplTest, ShouldTransitToCustomPassphrase) {
  627. EntityData default_entity_data;
  628. *default_entity_data.specifics.mutable_nigori() =
  629. sync_pb::NigoriSpecifics::default_instance();
  630. ASSERT_TRUE(bridge()->SetKeystoreKeys({kRawKeystoreKey}));
  631. // Note: passing default Nigori to MergeSyncData() leads to instantiation of
  632. // keystore Nigori.
  633. ASSERT_THAT(bridge()->MergeSyncData(std::move(default_entity_data)),
  634. Eq(absl::nullopt));
  635. EntityData new_entity_data;
  636. *new_entity_data.specifics.mutable_nigori() =
  637. BuildCustomPassphraseNigoriSpecifics(
  638. Pbkdf2PassphraseKeyParamsForTesting("passphrase"));
  639. EXPECT_CALL(*observer(), OnEncryptedTypesChanged(
  640. /*encrypted_types=*/EncryptableUserTypes(),
  641. /*encrypt_everything=*/true));
  642. EXPECT_CALL(*observer(), OnCryptographerStateChanged(
  643. NotNull(), /*has_pending_keys=*/true));
  644. EXPECT_CALL(*observer(),
  645. OnPassphraseTypeChanged(PassphraseType::kCustomPassphrase,
  646. Not(NullTime())));
  647. EXPECT_THAT(bridge()->ApplySyncChanges(std::move(new_entity_data)),
  648. Eq(absl::nullopt));
  649. EXPECT_TRUE(bridge()->HasPendingKeysForTesting());
  650. }
  651. // Tests that bridge doesn't try to overwrite unknown passphrase type and
  652. // report ModelError unless it received default Nigori node (which is
  653. // determined by the size of encryption_keybag). It's a requirement because
  654. // receiving unknown passphrase type might mean that some newer client switched
  655. // to the new passphrase type.
  656. TEST_F(NigoriSyncBridgeImplTest, ShouldFailOnUnknownPassprase) {
  657. EntityData entity_data;
  658. *entity_data.specifics.mutable_nigori() =
  659. sync_pb::NigoriSpecifics::default_instance();
  660. entity_data.specifics.mutable_nigori()->mutable_encryption_keybag()->set_blob(
  661. "data");
  662. entity_data.specifics.mutable_nigori()->set_passphrase_type(
  663. sync_pb::NigoriSpecifics::TRUSTED_VAULT_PASSPHRASE + 1);
  664. ASSERT_TRUE(bridge()->SetKeystoreKeys({kRawKeystoreKey}));
  665. EXPECT_CALL(*processor(), Put).Times(0);
  666. EXPECT_THAT(bridge()->MergeSyncData(std::move(entity_data)),
  667. Ne(absl::nullopt));
  668. }
  669. // Test emulates remote update in custom passphrase mode, which contains
  670. // |encryption_keybag| encrypted with known key, but without this key inside
  671. // the |encryption_keybag|. This is a protocol violation and bridge should
  672. // return ModelError on such updates.
  673. TEST_F(NigoriSyncBridgeImplTest,
  674. ShouldFailOnCustomPassphraseUpdateWithMissingKeybagDecryptionKey) {
  675. const KeyParamsForTesting kOldKeyParams =
  676. Pbkdf2PassphraseKeyParamsForTesting("old_key");
  677. const KeyParamsForTesting kPassphraseKeyParams =
  678. Pbkdf2PassphraseKeyParamsForTesting("passphrase");
  679. sync_pb::NigoriSpecifics specifics =
  680. BuildCustomPassphraseNigoriSpecifics(kPassphraseKeyParams, kOldKeyParams);
  681. EntityData entity_data;
  682. *entity_data.specifics.mutable_nigori() = specifics;
  683. ASSERT_THAT(bridge()->MergeSyncData(std::move(entity_data)),
  684. Eq(absl::nullopt));
  685. bridge()->SetExplicitPassphraseDecryptionKey(
  686. MakeNigoriKey(kPassphraseKeyParams));
  687. // Emulate |encryption_keybag| corruption: it will contain only key derived
  688. // from |kOldKeyParams|, but will be encrypted with key derived from
  689. // |kPassphraseKeyParams|.
  690. std::unique_ptr<CryptographerImpl> passphrase_cryptographer =
  691. CryptographerImpl::FromSingleKeyForTesting(
  692. kPassphraseKeyParams.password,
  693. kPassphraseKeyParams.derivation_params);
  694. NigoriKeyBag old_key_key_bag = NigoriKeyBag::CreateEmpty();
  695. old_key_key_bag.AddKey(Nigori::CreateByDerivation(
  696. kOldKeyParams.derivation_params, kOldKeyParams.password));
  697. ASSERT_TRUE(passphrase_cryptographer->Encrypt(
  698. old_key_key_bag.ToProto(), specifics.mutable_encryption_keybag()));
  699. EntityData corrupted_entity_data;
  700. *entity_data.specifics.mutable_nigori() = specifics;
  701. EXPECT_THAT(bridge()->ApplySyncChanges(std::move(entity_data)),
  702. Ne(absl::nullopt));
  703. }
  704. // Tests that bridge reports error when receiving corrupted NigoriSpecifics
  705. // if decryption happens in SetKeystoreKeys().
  706. TEST_F(NigoriSyncBridgeImplTest, ShouldFailOnInvalidKeystoreDecryption) {
  707. const KeyParamsForTesting kKeystoreKeyParams =
  708. KeystoreKeyParamsForTesting(kRawKeystoreKey);
  709. // Don't populate |kKeystoreKeyParams| in |keybag_keys_params|, so encryption
  710. // keybag isn't valid. Put fake key params in |keybage_keys_params|, because
  711. // they must be non-empty.
  712. EntityData entity_data;
  713. *entity_data.specifics.mutable_nigori() = BuildKeystoreNigoriSpecifics(
  714. /*keybag_keys_params=*/{Pbkdf2PassphraseKeyParamsForTesting("fake_key")},
  715. /*keystore_decryptor_params=*/kKeystoreKeyParams,
  716. /*keystore_key_params=*/kKeystoreKeyParams);
  717. // Call SetKeystoreKeys() after MergeSyncData() to trigger decryption upon
  718. // receiving keystore keys.
  719. ASSERT_THAT(bridge()->MergeSyncData(std::move(entity_data)),
  720. Eq(absl::nullopt));
  721. EXPECT_CALL(*processor(), ReportError);
  722. EXPECT_FALSE(bridge()->SetKeystoreKeys({kRawKeystoreKey}));
  723. }
  724. TEST_F(NigoriSyncBridgeImplTest, ShouldClearDataWhenSyncDisabled) {
  725. const KeyParamsForTesting kKeystoreKeyParams =
  726. KeystoreKeyParamsForTesting(kRawKeystoreKey);
  727. EntityData entity_data;
  728. *entity_data.specifics.mutable_nigori() = BuildKeystoreNigoriSpecifics(
  729. /*keybag_keys_params=*/{kKeystoreKeyParams},
  730. /*keystore_decryptor_params=*/kKeystoreKeyParams,
  731. /*keystore_key_params=*/kKeystoreKeyParams);
  732. ASSERT_TRUE(bridge()->SetKeystoreKeys({kRawKeystoreKey}));
  733. ASSERT_THAT(bridge()->MergeSyncData(std::move(entity_data)),
  734. Eq(absl::nullopt));
  735. ASSERT_TRUE(cryptographer()->CanEncrypt());
  736. EXPECT_CALL(*storage(), ClearData);
  737. bridge()->ApplyDisableSyncChanges();
  738. EXPECT_FALSE(cryptographer()->CanEncrypt());
  739. }
  740. // Tests decryption logic for explicit passphrase. In order to check that we're
  741. // able to decrypt the data encrypted with old key (i.e. keystore keys or old
  742. // GAIA passphrase) we add one extra key to the encryption keybag.
  743. TEST_P(NigoriSyncBridgeImplTestWithOptionalScryptDerivation,
  744. ShouldDecryptWithCustomPassphraseAndUpdateDefaultKey) {
  745. const KeyParamsForTesting kOldKeyParams =
  746. Pbkdf2PassphraseKeyParamsForTesting("old_key");
  747. const KeyParamsForTesting& passphrase_key_params =
  748. GetCustomPassphraseKeyParams();
  749. EntityData entity_data;
  750. *entity_data.specifics.mutable_nigori() =
  751. BuildCustomPassphraseNigoriSpecifics(passphrase_key_params,
  752. kOldKeyParams);
  753. ASSERT_TRUE(bridge()->SetKeystoreKeys({kRawKeystoreKey}));
  754. EXPECT_CALL(
  755. *observer(),
  756. OnPassphraseRequired(
  757. /*key_derivation_params=*/passphrase_key_params.derivation_params,
  758. /*pending_keys=*/
  759. EncryptedDataEq(entity_data.specifics.nigori().encryption_keybag())));
  760. ASSERT_THAT(bridge()->MergeSyncData(std::move(entity_data)),
  761. Eq(absl::nullopt));
  762. testing::InSequence seq;
  763. EXPECT_CALL(*observer(), OnCryptographerStateChanged(
  764. NotNull(), /*has_pending_keys=*/false));
  765. EXPECT_CALL(*observer(), OnPassphraseAccepted());
  766. bridge()->SetExplicitPassphraseDecryptionKey(
  767. MakeNigoriKey(passphrase_key_params));
  768. EXPECT_THAT(*cryptographer(), CanDecryptWith(kOldKeyParams));
  769. EXPECT_THAT(*cryptographer(), CanDecryptWith(passphrase_key_params));
  770. EXPECT_THAT(*cryptographer(),
  771. HasDefaultKeyDerivedFrom(passphrase_key_params));
  772. }
  773. INSTANTIATE_TEST_SUITE_P(Scrypt,
  774. NigoriSyncBridgeImplTestWithOptionalScryptDerivation,
  775. testing::Values(false, true));
  776. // Tests custom passphrase setup logic. Initially Nigori node will be
  777. // initialized with keystore Nigori due to sync with default Nigori. After
  778. // SetEncryptionPassphrase() call observers should be notified about state
  779. // changes, custom passphrase Nigori should be put into the processor and
  780. // exposed through GetData(), cryptographer should encrypt data with custom
  781. // passphrase.
  782. TEST_F(NigoriSyncBridgeImplTest,
  783. ShouldPutAndNotifyObserversWhenSetEncryptionPassphrase) {
  784. const std::string kCustomPassphrase = "passphrase";
  785. EntityData default_entity_data;
  786. *default_entity_data.specifics.mutable_nigori() =
  787. sync_pb::NigoriSpecifics::default_instance();
  788. ASSERT_TRUE(bridge()->SetKeystoreKeys({kRawKeystoreKey}));
  789. ASSERT_THAT(bridge()->MergeSyncData(std::move(default_entity_data)),
  790. Eq(absl::nullopt));
  791. ASSERT_THAT(bridge()->GetData(), Not(HasCustomPassphraseNigori()));
  792. EXPECT_THAT(bridge()->ApplySyncChanges(absl::nullopt), Eq(absl::nullopt));
  793. // Calling SetEncryptionPassphrase() triggers a commit cycle but doesn't
  794. // immediately expose the new state, until the commit completes.
  795. EXPECT_CALL(*processor(), Put(HasCustomPassphraseNigori()));
  796. bridge()->SetEncryptionPassphrase(kCustomPassphrase,
  797. MakeCustomPassphraseKeyDerivationParams());
  798. EXPECT_THAT(bridge()->GetData(), HasCustomPassphraseNigori());
  799. // Mimic commit completion.
  800. testing::InSequence seq;
  801. EXPECT_CALL(*observer(),
  802. OnPassphraseTypeChanged(PassphraseType::kCustomPassphrase,
  803. /*passphrase_time=*/Not(NullTime())));
  804. EXPECT_CALL(*observer(), OnCryptographerStateChanged(
  805. NotNull(), /*has_pending_keys=*/false));
  806. EXPECT_CALL(*observer(), OnEncryptedTypesChanged(
  807. /*encrypted_types=*/EncryptableUserTypes(),
  808. /*encrypt_everything=*/true));
  809. EXPECT_CALL(*observer(), OnPassphraseAccepted());
  810. EXPECT_THAT(bridge()->ApplySyncChanges(absl::nullopt), Eq(absl::nullopt));
  811. EXPECT_THAT(bridge()->GetData(), HasCustomPassphraseNigori());
  812. const KeyParamsForTesting passphrase_key_params = {
  813. bridge()->GetCustomPassphraseKeyDerivationParamsForTesting(),
  814. kCustomPassphrase};
  815. EXPECT_THAT(*cryptographer(), CanDecryptWith(passphrase_key_params));
  816. EXPECT_THAT(*cryptographer(),
  817. HasDefaultKeyDerivedFrom(passphrase_key_params));
  818. }
  819. // Tests that pending local change with setting custom passphrase is applied,
  820. // when there was a conflicting remote update and remote update is respected.
  821. TEST_F(NigoriSyncBridgeImplTest,
  822. ShouldSetCustomPassphraseAfterConflictingUpdates) {
  823. // Start with simple keystore Nigori.
  824. const std::vector<uint8_t> kRawKeystoreKey1{kRawKeystoreKey};
  825. const KeyParamsForTesting kKeystoreKeyParams1 =
  826. KeystoreKeyParamsForTesting(kRawKeystoreKey1);
  827. EntityData simple_keystore_entity_data;
  828. *simple_keystore_entity_data.specifics.mutable_nigori() =
  829. BuildKeystoreNigoriSpecifics(
  830. /*keybag_keys_params=*/{kKeystoreKeyParams1},
  831. /*keystore_decryptor_params=*/kKeystoreKeyParams1,
  832. /*keystore_key_params=*/kKeystoreKeyParams1);
  833. bridge()->SetKeystoreKeys({kRawKeystoreKey1});
  834. ASSERT_THAT(bridge()->MergeSyncData(std::move(simple_keystore_entity_data)),
  835. Eq(absl::nullopt));
  836. // Set up custom passphrase locally, but don't emulate commit completion.
  837. const std::string kCustomPassphrase = "custom_passphrase";
  838. bridge()->SetEncryptionPassphrase(kCustomPassphrase,
  839. MakeCustomPassphraseKeyDerivationParams());
  840. // Emulate conflict with rotated keystore Nigori.
  841. const std::vector<uint8_t> kRawKeystoreKey2 = {5, 6, 7, 8};
  842. const KeyParamsForTesting kKeystoreKeyParams2 =
  843. KeystoreKeyParamsForTesting(kRawKeystoreKey2);
  844. EntityData rotated_keystore_entity_data;
  845. *rotated_keystore_entity_data.specifics.mutable_nigori() =
  846. BuildKeystoreNigoriSpecifics(
  847. /*keybag_keys_params=*/{kKeystoreKeyParams1, kKeystoreKeyParams2},
  848. /*keystore_decryptor_params=*/kKeystoreKeyParams2,
  849. /*keystore_key_params=*/kKeystoreKeyParams2);
  850. bridge()->SetKeystoreKeys({kRawKeystoreKey1, kRawKeystoreKey2});
  851. // Verify that custom passphrase is set on top of
  852. // |rotated_keystore_entity_data|.
  853. EXPECT_CALL(*processor(), Put(HasCustomPassphraseNigori()));
  854. EXPECT_THAT(
  855. bridge()->ApplySyncChanges(std::move(rotated_keystore_entity_data)),
  856. Eq(absl::nullopt));
  857. EXPECT_THAT(bridge()->GetData(), HasCustomPassphraseNigori());
  858. // Mimic commit completion.
  859. testing::InSequence seq;
  860. EXPECT_CALL(*observer(),
  861. OnPassphraseTypeChanged(PassphraseType::kCustomPassphrase,
  862. /*passphrase_time=*/Not(NullTime())));
  863. EXPECT_CALL(*observer(), OnCryptographerStateChanged(
  864. NotNull(), /*has_pending_keys=*/false));
  865. EXPECT_CALL(*observer(), OnEncryptedTypesChanged(
  866. /*encrypted_types=*/EncryptableUserTypes(),
  867. /*encrypt_everything=*/true));
  868. EXPECT_CALL(*observer(), OnPassphraseAccepted());
  869. EXPECT_THAT(bridge()->ApplySyncChanges(absl::nullopt), Eq(absl::nullopt));
  870. EXPECT_THAT(bridge()->GetData(), HasCustomPassphraseNigori());
  871. const KeyParamsForTesting passphrase_key_params = {
  872. bridge()->GetCustomPassphraseKeyDerivationParamsForTesting(),
  873. kCustomPassphrase};
  874. EXPECT_THAT(*cryptographer(), CanDecryptWith(kKeystoreKeyParams1));
  875. EXPECT_THAT(*cryptographer(), CanDecryptWith(kKeystoreKeyParams2));
  876. EXPECT_THAT(*cryptographer(), CanDecryptWith(passphrase_key_params));
  877. EXPECT_THAT(*cryptographer(),
  878. HasDefaultKeyDerivedFrom(passphrase_key_params));
  879. }
  880. // Tests that SetEncryptionPassphrase() call doesn't lead to custom passphrase
  881. // change in case we already have one.
  882. TEST_F(NigoriSyncBridgeImplTest, ShouldNotAllowCustomPassphraseChange) {
  883. EntityData entity_data;
  884. *entity_data.specifics.mutable_nigori() =
  885. BuildCustomPassphraseNigoriSpecifics(
  886. Pbkdf2PassphraseKeyParamsForTesting("passphrase"));
  887. ASSERT_TRUE(bridge()->SetKeystoreKeys({kRawKeystoreKey}));
  888. ASSERT_THAT(bridge()->MergeSyncData(std::move(entity_data)),
  889. Eq(absl::nullopt));
  890. EXPECT_CALL(*observer(), OnPassphraseAccepted()).Times(0);
  891. bridge()->SetEncryptionPassphrase("new_passphrase",
  892. MakeCustomPassphraseKeyDerivationParams());
  893. }
  894. TEST_F(NigoriSyncBridgeImplPersistenceTest, ShouldRestoreKeystoreNigori) {
  895. // Emulate storing on disc.
  896. auto storage1 = std::make_unique<testing::NiceMock<MockNigoriStorage>>();
  897. sync_pb::NigoriLocalData nigori_local_data;
  898. ON_CALL(*storage1, StoreData)
  899. .WillByDefault(testing::SaveArg<0>(&nigori_local_data));
  900. // Provide some metadata to verify that we store it.
  901. auto processor1 =
  902. std::make_unique<testing::NiceMock<MockNigoriLocalChangeProcessor>>();
  903. const std::string kDummyProgressMarkerToken = "dummy_token";
  904. const int64_t kDummySequenceNumber = 100;
  905. ON_CALL(*processor1, GetMetadata()).WillByDefault([&] {
  906. return CreateDummyNigoriMetadataBatch(kDummyProgressMarkerToken,
  907. kDummySequenceNumber);
  908. });
  909. auto bridge1 = std::make_unique<NigoriSyncBridgeImpl>(std::move(processor1),
  910. std::move(storage1));
  911. // Perform initial sync with simple keystore Nigori.
  912. const std::vector<uint8_t> kRawKeystoreKey = {0, 1, 2, 3, 4};
  913. const KeyParamsForTesting kKeystoreKeyParams =
  914. KeystoreKeyParamsForTesting(kRawKeystoreKey);
  915. EntityData entity_data;
  916. *entity_data.specifics.mutable_nigori() = BuildKeystoreNigoriSpecifics(
  917. /*keybag_keys_params=*/{kKeystoreKeyParams},
  918. /*keystore_decryptor_params=*/kKeystoreKeyParams,
  919. /*keystore_key_params=*/kKeystoreKeyParams);
  920. ASSERT_TRUE(bridge1->SetKeystoreKeys({kRawKeystoreKey}));
  921. ASSERT_THAT(bridge1->MergeSyncData(std::move(entity_data)),
  922. Eq(absl::nullopt));
  923. // At this point |nigori_local_data| must be initialized with metadata
  924. // provided by CreateDummyNigoriMetadataBatch() and data should represent
  925. // the simple keystore Nigori.
  926. // Create secondary storage which will return |nigori_local_data| on
  927. // RestoreData() call.
  928. auto storage2 = std::make_unique<testing::NiceMock<MockNigoriStorage>>();
  929. ON_CALL(*storage2, RestoreData()).WillByDefault(Return(nigori_local_data));
  930. // Create secondary processor, which should expect ModelReadyToSync() call
  931. // with previously stored metadata.
  932. auto processor2 =
  933. std::make_unique<testing::NiceMock<MockNigoriLocalChangeProcessor>>();
  934. EXPECT_CALL(
  935. *processor2,
  936. ModelReadyToSync(NotNull(),
  937. IsDummyNigoriMetadataBatchWithTokenAndSequenceNumber(
  938. kDummyProgressMarkerToken, kDummySequenceNumber)));
  939. auto bridge2 = std::make_unique<NigoriSyncBridgeImpl>(std::move(processor2),
  940. std::move(storage2));
  941. // Verify that we restored Cryptographer state.
  942. EXPECT_THAT(*bridge2->GetCryptographer(), CanDecryptWith(kKeystoreKeyParams));
  943. EXPECT_THAT(*bridge2->GetCryptographer(),
  944. HasDefaultKeyDerivedFrom(kKeystoreKeyParams));
  945. }
  946. // Commit with keystore Nigori initialization might be not completed before
  947. // the browser restart. This test emulates loading non-initialized Nigori
  948. // after restart and expects that bridge will trigger initialization after
  949. // loading.
  950. TEST_F(NigoriSyncBridgeImplPersistenceTest,
  951. ShouldInitializeKeystoreNigoriWhenLoadedFromStorage) {
  952. const KeyParamsForTesting kKeystoreKeyParams =
  953. KeystoreKeyParamsForTesting({1, 2, 3});
  954. NigoriState unitialized_state_with_keystore_keys;
  955. unitialized_state_with_keystore_keys.keystore_keys_cryptographer =
  956. KeystoreKeysCryptographer::FromKeystoreKeys(
  957. {kKeystoreKeyParams.password});
  958. sync_pb::NigoriLocalData nigori_local_data;
  959. *nigori_local_data.mutable_nigori_model() =
  960. unitialized_state_with_keystore_keys.ToLocalProto();
  961. auto storage = std::make_unique<testing::NiceMock<MockNigoriStorage>>();
  962. ON_CALL(*storage, RestoreData()).WillByDefault(Return(nigori_local_data));
  963. auto processor =
  964. std::make_unique<testing::NiceMock<MockNigoriLocalChangeProcessor>>();
  965. ON_CALL(*processor, IsTrackingMetadata()).WillByDefault(Return(true));
  966. MockNigoriLocalChangeProcessor* not_owned_processor = processor.get();
  967. // Calling bridge constructor triggers a commit cycle but doesn't immediately
  968. // expose the new state, until the commit completes.
  969. EXPECT_CALL(*not_owned_processor, Put(HasKeystoreNigori()));
  970. auto bridge = std::make_unique<NigoriSyncBridgeImpl>(std::move(processor),
  971. std::move(storage));
  972. EXPECT_THAT(bridge->GetData(), HasKeystoreNigori());
  973. // Emulate commit completeness.
  974. EXPECT_THAT(bridge->ApplySyncChanges(absl::nullopt), Eq(absl::nullopt));
  975. EXPECT_THAT(bridge->GetData(), HasKeystoreNigori());
  976. EXPECT_THAT(bridge->GetKeystoreMigrationTime(), Not(NullTime()));
  977. EXPECT_EQ(PassphraseType::kKeystorePassphrase, bridge->GetPassphraseType());
  978. EXPECT_THAT(*bridge->GetCryptographer(), CanDecryptWith(kKeystoreKeyParams));
  979. EXPECT_THAT(*bridge->GetCryptographer(),
  980. HasDefaultKeyDerivedFrom(kKeystoreKeyParams));
  981. }
  982. // Tests the initial sync with a trusted vault Nigori. Observers should be
  983. // notified about encryption state changes and cryptographer shouldn't be ready
  984. // (by having pending keys) until the passphrase is received by means other than
  985. // the sync protocol.
  986. TEST_F(NigoriSyncBridgeImplTest,
  987. ShouldRequireUserActionIfInitiallyUsingTrustedVault) {
  988. EntityData entity_data;
  989. *entity_data.specifics.mutable_nigori() =
  990. BuildTrustedVaultNigoriSpecifics({kTrustedVaultKey});
  991. EXPECT_CALL(*observer(), OnPassphraseRequired).Times(0);
  992. ASSERT_TRUE(bridge()->SetKeystoreKeys({kRawKeystoreKey}));
  993. // The current implementation issues redundant notifications.
  994. EXPECT_CALL(*observer(),
  995. OnCryptographerStateChanged(NotNull(), /*has_pending_keys=*/true))
  996. .Times(2);
  997. EXPECT_CALL(*observer(),
  998. OnPassphraseTypeChanged(PassphraseType::kTrustedVaultPassphrase,
  999. NullTime()))
  1000. .Times(2);
  1001. EXPECT_CALL(*observer(), OnTrustedVaultKeyRequired()).Times(2);
  1002. EXPECT_THAT(bridge()->MergeSyncData(std::move(entity_data)),
  1003. Eq(absl::nullopt));
  1004. bridge()->NotifyInitialStateToObservers();
  1005. EXPECT_THAT(bridge()->GetPassphraseType(),
  1006. Eq(PassphraseType::kTrustedVaultPassphrase));
  1007. EXPECT_THAT(bridge()->GetEncryptedTypes(), Eq(AlwaysEncryptedUserTypes()));
  1008. EXPECT_TRUE(bridge()->HasPendingKeysForTesting());
  1009. EXPECT_CALL(*observer(), OnTrustedVaultKeyAccepted());
  1010. EXPECT_CALL(*observer(), OnCryptographerStateChanged(
  1011. NotNull(), /*has_pending_keys=*/false));
  1012. bridge()->AddTrustedVaultDecryptionKeys({kTrustedVaultKey});
  1013. EXPECT_FALSE(bridge()->HasPendingKeysForTesting());
  1014. }
  1015. // Tests the processing of a remote incremental update that transitions from
  1016. // keystore to trusted vault passphrase, which requires receiving the new
  1017. // passphrase by means other than the sync protocol.
  1018. TEST_F(NigoriSyncBridgeImplTest,
  1019. ShouldProcessRemoteTransitionFromKeystoreToTrustedVault) {
  1020. EntityData default_entity_data;
  1021. *default_entity_data.specifics.mutable_nigori() =
  1022. sync_pb::NigoriSpecifics::default_instance();
  1023. EXPECT_CALL(*observer(), OnPassphraseRequired).Times(0);
  1024. ASSERT_TRUE(bridge()->SetKeystoreKeys({kRawKeystoreKey}));
  1025. // Note: passing default Nigori to MergeSyncData() leads to instantiation of
  1026. // keystore Nigori.
  1027. ASSERT_THAT(bridge()->MergeSyncData(std::move(default_entity_data)),
  1028. Eq(absl::nullopt));
  1029. bridge()->NotifyInitialStateToObservers();
  1030. ASSERT_FALSE(bridge()->GetTrustedVaultDebugInfo().has_migration_time());
  1031. ASSERT_FALSE(bridge()->GetTrustedVaultDebugInfo().has_key_version());
  1032. EntityData new_entity_data;
  1033. *new_entity_data.specifics.mutable_nigori() =
  1034. BuildTrustedVaultNigoriSpecifics({kTrustedVaultKey});
  1035. EXPECT_CALL(*observer(), OnEncryptedTypesChanged).Times(0);
  1036. EXPECT_CALL(*observer(), OnCryptographerStateChanged(
  1037. NotNull(), /*has_pending_keys=*/true));
  1038. EXPECT_CALL(*observer(),
  1039. OnPassphraseTypeChanged(PassphraseType::kTrustedVaultPassphrase,
  1040. NullTime()));
  1041. EXPECT_CALL(*observer(), OnTrustedVaultKeyRequired());
  1042. EXPECT_THAT(bridge()->ApplySyncChanges(std::move(new_entity_data)),
  1043. Eq(absl::nullopt));
  1044. EXPECT_THAT(bridge()->GetPassphraseType(),
  1045. Eq(PassphraseType::kTrustedVaultPassphrase));
  1046. EXPECT_THAT(bridge()->GetEncryptedTypes(), Eq(AlwaysEncryptedUserTypes()));
  1047. EXPECT_TRUE(bridge()->HasPendingKeysForTesting());
  1048. EXPECT_CALL(*observer(), OnTrustedVaultKeyAccepted());
  1049. EXPECT_CALL(*observer(), OnCryptographerStateChanged(
  1050. NotNull(), /*has_pending_keys=*/false));
  1051. bridge()->AddTrustedVaultDecryptionKeys({kTrustedVaultKey});
  1052. EXPECT_FALSE(bridge()->HasPendingKeysForTesting());
  1053. EXPECT_TRUE(bridge()->GetTrustedVaultDebugInfo().has_migration_time());
  1054. EXPECT_TRUE(bridge()->GetTrustedVaultDebugInfo().has_key_version());
  1055. }
  1056. // Tests the processing of a remote incremental update that rotates the trusted
  1057. // vault passphrase.
  1058. TEST_F(NigoriSyncBridgeImplTest,
  1059. ShouldProcessRemoteKeyRotationForTrustedVault) {
  1060. const std::vector<uint8_t> kRotatedTrustedVaultKey = {7, 8, 9, 10};
  1061. EXPECT_CALL(*observer(), OnPassphraseRequired).Times(0);
  1062. EntityData entity_data;
  1063. *entity_data.specifics.mutable_nigori() =
  1064. BuildTrustedVaultNigoriSpecifics({kTrustedVaultKey});
  1065. ASSERT_TRUE(bridge()->SetKeystoreKeys({kRawKeystoreKey}));
  1066. ASSERT_THAT(bridge()->MergeSyncData(std::move(entity_data)),
  1067. Eq(absl::nullopt));
  1068. bridge()->NotifyInitialStateToObservers();
  1069. ASSERT_TRUE(bridge()->HasPendingKeysForTesting());
  1070. bridge()->AddTrustedVaultDecryptionKeys({kTrustedVaultKey});
  1071. ASSERT_FALSE(bridge()->HasPendingKeysForTesting());
  1072. ASSERT_THAT(bridge()->GetPassphraseType(),
  1073. Eq(PassphraseType::kTrustedVaultPassphrase));
  1074. // Mimic remote key rotation.
  1075. EntityData new_entity_data;
  1076. *new_entity_data.specifics.mutable_nigori() =
  1077. BuildTrustedVaultNigoriSpecifics(
  1078. {kTrustedVaultKey, kRotatedTrustedVaultKey});
  1079. EXPECT_CALL(*observer(), OnEncryptedTypesChanged).Times(0);
  1080. EXPECT_CALL(*observer(), OnPassphraseTypeChanged).Times(0);
  1081. EXPECT_CALL(*observer(), OnCryptographerStateChanged(
  1082. NotNull(), /*has_pending_keys=*/true));
  1083. EXPECT_CALL(*observer(), OnTrustedVaultKeyRequired());
  1084. EXPECT_THAT(bridge()->ApplySyncChanges(std::move(new_entity_data)),
  1085. Eq(absl::nullopt));
  1086. EXPECT_TRUE(bridge()->HasPendingKeysForTesting());
  1087. EXPECT_CALL(*observer(), OnTrustedVaultKeyAccepted());
  1088. EXPECT_CALL(*observer(), OnCryptographerStateChanged(
  1089. NotNull(), /*has_pending_keys=*/false));
  1090. bridge()->AddTrustedVaultDecryptionKeys({kRotatedTrustedVaultKey});
  1091. EXPECT_FALSE(bridge()->HasPendingKeysForTesting());
  1092. }
  1093. // Tests transitioning locally from trusted vault passphrase to custom
  1094. // passphrase.
  1095. TEST_F(NigoriSyncBridgeImplTest,
  1096. ShouldTransitionLocallyFromTrustedVaultToCustomPassphrase) {
  1097. const std::string kCustomPassphrase = "custom_passphrase";
  1098. EntityData entity_data;
  1099. *entity_data.specifics.mutable_nigori() =
  1100. BuildTrustedVaultNigoriSpecifics({kTrustedVaultKey});
  1101. ASSERT_TRUE(bridge()->SetKeystoreKeys({kRawKeystoreKey}));
  1102. ASSERT_THAT(bridge()->MergeSyncData(std::move(entity_data)),
  1103. Eq(absl::nullopt));
  1104. bridge()->NotifyInitialStateToObservers();
  1105. ASSERT_TRUE(bridge()->HasPendingKeysForTesting());
  1106. bridge()->AddTrustedVaultDecryptionKeys({kTrustedVaultKey});
  1107. ASSERT_FALSE(bridge()->HasPendingKeysForTesting());
  1108. ASSERT_THAT(bridge()->GetPassphraseType(),
  1109. Eq(PassphraseType::kTrustedVaultPassphrase));
  1110. ASSERT_THAT(bridge()->GetData(), Not(HasCustomPassphraseNigori()));
  1111. // Calling SetEncryptionPassphrase() triggers a commit cycle but doesn't
  1112. // immediately expose the new state, until the commit completes.
  1113. EXPECT_CALL(*processor(), Put(HasCustomPassphraseNigori()));
  1114. bridge()->SetEncryptionPassphrase(kCustomPassphrase,
  1115. MakeCustomPassphraseKeyDerivationParams());
  1116. EXPECT_THAT(bridge()->GetData(), HasCustomPassphraseNigori());
  1117. // Mimic commit completion.
  1118. testing::InSequence seq;
  1119. EXPECT_CALL(*observer(),
  1120. OnPassphraseTypeChanged(PassphraseType::kCustomPassphrase,
  1121. /*passphrase_time=*/Not(NullTime())));
  1122. EXPECT_CALL(*observer(), OnCryptographerStateChanged(
  1123. NotNull(), /*has_pending_keys=*/false));
  1124. EXPECT_CALL(*observer(), OnEncryptedTypesChanged(
  1125. /*encrypted_types=*/EncryptableUserTypes(),
  1126. /*encrypt_everything=*/true));
  1127. EXPECT_CALL(*observer(), OnPassphraseAccepted());
  1128. EXPECT_THAT(bridge()->ApplySyncChanges(absl::nullopt), Eq(absl::nullopt));
  1129. EXPECT_THAT(bridge()->GetData(), HasCustomPassphraseNigori());
  1130. }
  1131. // Tests processing of remote incremental update that transits from trusted
  1132. // vault to keystore passphrase.
  1133. TEST_F(NigoriSyncBridgeImplTest,
  1134. ShouldProcessRemoteTransitionFromTrustedVaultToKeystore) {
  1135. EntityData entity_data;
  1136. *entity_data.specifics.mutable_nigori() =
  1137. BuildTrustedVaultNigoriSpecifics({kTrustedVaultKey});
  1138. ASSERT_TRUE(bridge()->SetKeystoreKeys({kRawKeystoreKey}));
  1139. ASSERT_THAT(bridge()->MergeSyncData(std::move(entity_data)),
  1140. Eq(absl::nullopt));
  1141. bridge()->NotifyInitialStateToObservers();
  1142. ASSERT_TRUE(bridge()->HasPendingKeysForTesting());
  1143. bridge()->AddTrustedVaultDecryptionKeys({kTrustedVaultKey});
  1144. ASSERT_FALSE(bridge()->HasPendingKeysForTesting());
  1145. ASSERT_THAT(bridge()->GetPassphraseType(),
  1146. Eq(PassphraseType::kTrustedVaultPassphrase));
  1147. ASSERT_THAT(bridge()->GetData(), Not(HasCustomPassphraseNigori()));
  1148. const KeyParamsForTesting kTrustedVaultKeyParams =
  1149. TrustedVaultKeyParamsForTesting(kTrustedVaultKey);
  1150. const KeyParamsForTesting kKeystoreKeyParams =
  1151. KeystoreKeyParamsForTesting(kRawKeystoreKey);
  1152. EntityData new_entity_data;
  1153. *new_entity_data.specifics.mutable_nigori() = BuildKeystoreNigoriSpecifics(
  1154. /*keybag_keys_params=*/{kTrustedVaultKeyParams, kKeystoreKeyParams},
  1155. /*keystore_decryptor_params=*/kKeystoreKeyParams,
  1156. /*keystore_key_params=*/kKeystoreKeyParams);
  1157. EXPECT_CALL(*observer(), OnEncryptedTypesChanged).Times(0);
  1158. EXPECT_CALL(*observer(), OnCryptographerStateChanged(
  1159. NotNull(), /*has_pending_keys=*/false));
  1160. EXPECT_CALL(
  1161. *observer(),
  1162. OnPassphraseTypeChanged(PassphraseType::kKeystorePassphrase, NullTime()));
  1163. EXPECT_THAT(bridge()->ApplySyncChanges(std::move(new_entity_data)),
  1164. Eq(absl::nullopt));
  1165. EXPECT_THAT(bridge()->GetPassphraseType(),
  1166. Eq(PassphraseType::kKeystorePassphrase));
  1167. EXPECT_THAT(bridge()->GetEncryptedTypes(), Eq(AlwaysEncryptedUserTypes()));
  1168. EXPECT_FALSE(bridge()->HasPendingKeysForTesting());
  1169. EXPECT_THAT(*cryptographer(), CanDecryptWith(kTrustedVaultKeyParams));
  1170. EXPECT_THAT(*cryptographer(), CanDecryptWith(kKeystoreKeyParams));
  1171. EXPECT_THAT(*cryptographer(), HasDefaultKeyDerivedFrom(kKeystoreKeyParams));
  1172. }
  1173. // Tests processing of remote incremental update that transits from trusted
  1174. // vault to custom passphrase.
  1175. TEST_F(NigoriSyncBridgeImplTest,
  1176. ShouldProcessRemoteTransitionFromTrustedVaultToCustomPassphrase) {
  1177. EntityData entity_data;
  1178. *entity_data.specifics.mutable_nigori() =
  1179. BuildTrustedVaultNigoriSpecifics({kTrustedVaultKey});
  1180. ASSERT_TRUE(bridge()->SetKeystoreKeys({kRawKeystoreKey}));
  1181. ASSERT_THAT(bridge()->MergeSyncData(std::move(entity_data)),
  1182. Eq(absl::nullopt));
  1183. bridge()->NotifyInitialStateToObservers();
  1184. ASSERT_TRUE(bridge()->HasPendingKeysForTesting());
  1185. bridge()->AddTrustedVaultDecryptionKeys({kTrustedVaultKey});
  1186. ASSERT_FALSE(bridge()->HasPendingKeysForTesting());
  1187. ASSERT_THAT(bridge()->GetPassphraseType(),
  1188. Eq(PassphraseType::kTrustedVaultPassphrase));
  1189. ASSERT_THAT(bridge()->GetData(), Not(HasCustomPassphraseNigori()));
  1190. const KeyParamsForTesting kTrustedVaultKeyParams =
  1191. TrustedVaultKeyParamsForTesting(kTrustedVaultKey);
  1192. const KeyParamsForTesting kCustomPassphraseKeyParams =
  1193. Pbkdf2PassphraseKeyParamsForTesting("custom_passphrase");
  1194. EntityData new_entity_data;
  1195. *new_entity_data.specifics.mutable_nigori() =
  1196. BuildCustomPassphraseNigoriSpecifics(
  1197. kCustomPassphraseKeyParams,
  1198. /*old_key_params=*/kTrustedVaultKeyParams);
  1199. EXPECT_CALL(*observer(), OnEncryptedTypesChanged(
  1200. /*encrypted_types=*/EncryptableUserTypes(),
  1201. /*encrypt_everything=*/true));
  1202. EXPECT_CALL(*observer(), OnCryptographerStateChanged(
  1203. NotNull(), /*has_pending_keys=*/true));
  1204. EXPECT_CALL(*observer(),
  1205. OnPassphraseTypeChanged(PassphraseType::kCustomPassphrase,
  1206. Not(NullTime())));
  1207. EXPECT_THAT(bridge()->ApplySyncChanges(std::move(new_entity_data)),
  1208. Eq(absl::nullopt));
  1209. EXPECT_TRUE(bridge()->HasPendingKeysForTesting());
  1210. EXPECT_CALL(*observer(), OnCryptographerStateChanged(
  1211. NotNull(), /*has_pending_keys=*/false));
  1212. EXPECT_CALL(*observer(), OnPassphraseAccepted());
  1213. bridge()->SetExplicitPassphraseDecryptionKey(
  1214. MakeNigoriKey(kCustomPassphraseKeyParams));
  1215. EXPECT_THAT(*cryptographer(), CanDecryptWith(kTrustedVaultKeyParams));
  1216. EXPECT_THAT(*cryptographer(), CanDecryptWith(kCustomPassphraseKeyParams));
  1217. EXPECT_THAT(*cryptographer(),
  1218. HasDefaultKeyDerivedFrom(kCustomPassphraseKeyParams));
  1219. }
  1220. // Tests processing of remote incremental update that transits from trusted
  1221. // vault to keystore passphrase, which doesn't contain trusted vault key. The
  1222. // bridge should report model error.
  1223. TEST_F(NigoriSyncBridgeImplTest,
  1224. ShouldFailOnInvalidRemoteTransitionFromTrustedVaultToKeystore) {
  1225. EntityData entity_data;
  1226. *entity_data.specifics.mutable_nigori() =
  1227. BuildTrustedVaultNigoriSpecifics({kTrustedVaultKey});
  1228. ASSERT_TRUE(bridge()->SetKeystoreKeys({kRawKeystoreKey}));
  1229. ASSERT_THAT(bridge()->MergeSyncData(std::move(entity_data)),
  1230. Eq(absl::nullopt));
  1231. bridge()->NotifyInitialStateToObservers();
  1232. ASSERT_TRUE(bridge()->HasPendingKeysForTesting());
  1233. bridge()->AddTrustedVaultDecryptionKeys({kTrustedVaultKey});
  1234. ASSERT_FALSE(bridge()->HasPendingKeysForTesting());
  1235. ASSERT_THAT(bridge()->GetPassphraseType(),
  1236. Eq(PassphraseType::kTrustedVaultPassphrase));
  1237. ASSERT_THAT(bridge()->GetData(), Not(HasCustomPassphraseNigori()));
  1238. const KeyParamsForTesting kKeystoreKeyParams =
  1239. KeystoreKeyParamsForTesting(kRawKeystoreKey);
  1240. // Don't populate kTrustedVaultKey into |new_entity_data|.
  1241. EntityData new_entity_data;
  1242. *new_entity_data.specifics.mutable_nigori() = BuildKeystoreNigoriSpecifics(
  1243. /*keybag_keys_params=*/{kKeystoreKeyParams},
  1244. /*keystore_decryptor_params=*/kKeystoreKeyParams,
  1245. /*keystore_key_params=*/kKeystoreKeyParams);
  1246. EXPECT_THAT(bridge()->ApplySyncChanges(std::move(new_entity_data)),
  1247. Ne(absl::nullopt));
  1248. }
  1249. // Tests processing of remote incremental update that transits from trusted
  1250. // vault to custom passphrase, which doesn't contain trusted vault key. The
  1251. // bridge should report model error.
  1252. TEST_F(NigoriSyncBridgeImplTest,
  1253. ShouldFailOnInvalidRemoteTransitionFromTrustedVaultToCustomPassphrase) {
  1254. EntityData entity_data;
  1255. *entity_data.specifics.mutable_nigori() =
  1256. BuildTrustedVaultNigoriSpecifics({kTrustedVaultKey});
  1257. ASSERT_TRUE(bridge()->SetKeystoreKeys({kRawKeystoreKey}));
  1258. ASSERT_THAT(bridge()->MergeSyncData(std::move(entity_data)),
  1259. Eq(absl::nullopt));
  1260. bridge()->NotifyInitialStateToObservers();
  1261. ASSERT_TRUE(bridge()->HasPendingKeysForTesting());
  1262. bridge()->AddTrustedVaultDecryptionKeys({kTrustedVaultKey});
  1263. ASSERT_FALSE(bridge()->HasPendingKeysForTesting());
  1264. ASSERT_THAT(bridge()->GetPassphraseType(),
  1265. Eq(PassphraseType::kTrustedVaultPassphrase));
  1266. ASSERT_THAT(bridge()->GetData(), Not(HasCustomPassphraseNigori()));
  1267. const KeyParamsForTesting kCustomPassphraseKeyParams =
  1268. Pbkdf2PassphraseKeyParamsForTesting("custom_passphrase");
  1269. // Don't populate kTrustedVaultKey into |new_entity_data|.
  1270. EntityData new_entity_data;
  1271. *new_entity_data.specifics.mutable_nigori() =
  1272. BuildCustomPassphraseNigoriSpecifics(kCustomPassphraseKeyParams);
  1273. // The bridge doesn't know whether update is valid until decryption, expect
  1274. // processing as a normal update.
  1275. EXPECT_CALL(*observer(), OnEncryptedTypesChanged(
  1276. /*encrypted_types=*/EncryptableUserTypes(),
  1277. /*encrypt_everything=*/true));
  1278. EXPECT_CALL(*observer(), OnCryptographerStateChanged(
  1279. NotNull(), /*has_pending_keys=*/true));
  1280. EXPECT_CALL(*observer(),
  1281. OnPassphraseTypeChanged(PassphraseType::kCustomPassphrase,
  1282. Not(NullTime())));
  1283. EXPECT_THAT(bridge()->ApplySyncChanges(std::move(new_entity_data)),
  1284. Eq(absl::nullopt));
  1285. EXPECT_TRUE(bridge()->HasPendingKeysForTesting());
  1286. // Once decryption passphrase is provided, bridge should ReportError().
  1287. EXPECT_CALL(*processor(), ReportError);
  1288. bridge()->SetExplicitPassphraseDecryptionKey(
  1289. MakeNigoriKey(kCustomPassphraseKeyParams));
  1290. }
  1291. // Tests processing of remote incremental update that transits from trusted
  1292. // vault to custom passphrase, which doesn't contain trusted vault key. Mimics
  1293. // browser restart in between of receiving the remote update and providing
  1294. // custom passphrase. The bridge should report model error.
  1295. TEST_F(NigoriSyncBridgeImplPersistenceTest,
  1296. ShouldFailOnInvalidRemoteTransitionFromTrustedVaultAfterRestart) {
  1297. // Emulate storing on disc.
  1298. auto storage1 = std::make_unique<testing::NiceMock<MockNigoriStorage>>();
  1299. sync_pb::NigoriLocalData nigori_local_data;
  1300. ON_CALL(*storage1, StoreData)
  1301. .WillByDefault(testing::SaveArg<0>(&nigori_local_data));
  1302. auto bridge1 = std::make_unique<NigoriSyncBridgeImpl>(
  1303. std::make_unique<testing::NiceMock<MockNigoriLocalChangeProcessor>>(),
  1304. std::move(storage1));
  1305. // Perform initial sync with trusted vault passphrase.
  1306. const std::vector<uint8_t> kTrustedVaultKey = {2, 3, 4, 5, 6};
  1307. EntityData entity_data;
  1308. *entity_data.specifics.mutable_nigori() =
  1309. BuildTrustedVaultNigoriSpecifics({kTrustedVaultKey});
  1310. const std::vector<uint8_t> kRawKeystoreKey = {0, 1, 2, 3, 4};
  1311. ASSERT_TRUE(bridge1->SetKeystoreKeys({kRawKeystoreKey}));
  1312. ASSERT_THAT(bridge1->MergeSyncData(std::move(entity_data)),
  1313. Eq(absl::nullopt));
  1314. bridge1->NotifyInitialStateToObservers();
  1315. ASSERT_TRUE(bridge1->HasPendingKeysForTesting());
  1316. bridge1->AddTrustedVaultDecryptionKeys({kTrustedVaultKey});
  1317. ASSERT_FALSE(bridge1->HasPendingKeysForTesting());
  1318. ASSERT_THAT(bridge1->GetPassphraseType(),
  1319. Eq(PassphraseType::kTrustedVaultPassphrase));
  1320. ASSERT_THAT(bridge1->GetData(), Not(HasCustomPassphraseNigori()));
  1321. // Mimic invalid remote update with custom passphrase.
  1322. const KeyParamsForTesting kCustomPassphraseKeyParams =
  1323. Pbkdf2PassphraseKeyParamsForTesting("custom_passphrase");
  1324. // Don't populate kTrustedVaultKeyParams into |new_entity_data|.
  1325. EntityData new_entity_data;
  1326. *new_entity_data.specifics.mutable_nigori() =
  1327. BuildCustomPassphraseNigoriSpecifics(kCustomPassphraseKeyParams);
  1328. // The bridge doesn't know whether update is valid until decryption, expect
  1329. // processing as a normal update.
  1330. ASSERT_THAT(bridge1->ApplySyncChanges(std::move(new_entity_data)),
  1331. Eq(absl::nullopt));
  1332. // Create secondary storage which will return |nigori_local_data| on
  1333. // RestoreData() call.
  1334. auto storage2 = std::make_unique<testing::NiceMock<MockNigoriStorage>>();
  1335. ON_CALL(*storage2, RestoreData()).WillByDefault(Return(nigori_local_data));
  1336. // Create secondary processor.
  1337. auto processor2 =
  1338. std::make_unique<testing::NiceMock<MockNigoriLocalChangeProcessor>>();
  1339. // Once decryption passphrase is provided, bridge should ReportError().
  1340. EXPECT_CALL(*processor2, ReportError);
  1341. auto bridge2 = std::make_unique<NigoriSyncBridgeImpl>(std::move(processor2),
  1342. std::move(storage2));
  1343. bridge2->SetExplicitPassphraseDecryptionKey(
  1344. MakeNigoriKey(kCustomPassphraseKeyParams));
  1345. }
  1346. TEST_F(NigoriSyncBridgeImplTest,
  1347. ShouldNotAddDecryptionKeysToTrustedVaultCryptographer) {
  1348. const std::vector<uint8_t> kTrustedVaultKey1{kTrustedVaultKey};
  1349. const std::vector<uint8_t> kTrustedVaultKey2 = {3, 4, 5, 6};
  1350. EntityData entity_data;
  1351. *entity_data.specifics.mutable_nigori() =
  1352. BuildTrustedVaultNigoriSpecifics({kTrustedVaultKey1});
  1353. ASSERT_TRUE(bridge()->SetKeystoreKeys({kRawKeystoreKey}));
  1354. EXPECT_THAT(bridge()->MergeSyncData(std::move(entity_data)),
  1355. Eq(absl::nullopt));
  1356. bridge()->NotifyInitialStateToObservers();
  1357. ASSERT_THAT(bridge()->GetPassphraseType(),
  1358. Eq(PassphraseType::kTrustedVaultPassphrase));
  1359. ASSERT_TRUE(bridge()->HasPendingKeysForTesting());
  1360. // Note that |kTrustedVaultKey2| was not part of Nigori specifics.
  1361. bridge()->AddTrustedVaultDecryptionKeys(
  1362. {kTrustedVaultKey1, kTrustedVaultKey2});
  1363. ASSERT_FALSE(bridge()->HasPendingKeysForTesting());
  1364. const CryptographerImpl& cryptographer =
  1365. bridge()->GetCryptographerImplForTesting();
  1366. ASSERT_THAT(
  1367. cryptographer,
  1368. CanDecryptWith(TrustedVaultKeyParamsForTesting(kTrustedVaultKey1)));
  1369. EXPECT_THAT(
  1370. cryptographer,
  1371. Not(CanDecryptWith(TrustedVaultKeyParamsForTesting(kTrustedVaultKey2))));
  1372. EXPECT_THAT(cryptographer.KeyBagSizeForTesting(), Eq(size_t(1)));
  1373. }
  1374. // Tests that upon startup bridge migrates the Nigori from backward compatible
  1375. // keystore mode to full keystore mode.
  1376. TEST_F(NigoriSyncBridgeImplPersistenceTest, ShouldCompleteKeystoreMigration) {
  1377. // Emulate storing on disc.
  1378. auto storage1 = std::make_unique<testing::NiceMock<MockNigoriStorage>>();
  1379. sync_pb::NigoriLocalData nigori_local_data;
  1380. ON_CALL(*storage1, StoreData)
  1381. .WillByDefault(testing::SaveArg<0>(&nigori_local_data));
  1382. auto bridge1 = std::make_unique<NigoriSyncBridgeImpl>(
  1383. std::make_unique<testing::NiceMock<MockNigoriLocalChangeProcessor>>(),
  1384. std::move(storage1));
  1385. // Perform initial sync with backward compatible keystore Nigori.
  1386. const std::vector<uint8_t> kRawKeystoreKey = {0, 1, 2, 3, 4};
  1387. const KeyParamsForTesting kKeystoreKeyParams =
  1388. KeystoreKeyParamsForTesting(kRawKeystoreKey);
  1389. const KeyParamsForTesting kPassphraseKeyParams =
  1390. Pbkdf2PassphraseKeyParamsForTesting("passphrase");
  1391. EntityData entity_data;
  1392. *entity_data.specifics.mutable_nigori() = BuildKeystoreNigoriSpecifics(
  1393. /*keybag_keys_params=*/{kKeystoreKeyParams, kPassphraseKeyParams},
  1394. /*keystore_decryptor_params=*/kPassphraseKeyParams,
  1395. /*keystore_key_params=*/kKeystoreKeyParams);
  1396. ASSERT_TRUE(bridge1->SetKeystoreKeys({kRawKeystoreKey}));
  1397. ASSERT_THAT(bridge1->MergeSyncData(std::move(entity_data)),
  1398. Eq(absl::nullopt));
  1399. // Mimic the browser restart.
  1400. auto storage2 = std::make_unique<testing::NiceMock<MockNigoriStorage>>();
  1401. ON_CALL(*storage2, RestoreData()).WillByDefault(Return(nigori_local_data));
  1402. auto processor2 =
  1403. std::make_unique<testing::NiceMock<MockNigoriLocalChangeProcessor>>();
  1404. ON_CALL(*processor2, IsTrackingMetadata()).WillByDefault(Return(true));
  1405. // Upon startup bridge should issue a commit with full keystore Nigori.
  1406. EXPECT_CALL(*processor2, Put(HasKeystoreNigori()));
  1407. auto bridge2 = std::make_unique<NigoriSyncBridgeImpl>(std::move(processor2),
  1408. std::move(storage2));
  1409. // Mimic commit completion.
  1410. EXPECT_THAT(bridge2->ApplySyncChanges(absl::nullopt), Eq(absl::nullopt));
  1411. EXPECT_THAT(bridge2->GetData(), HasKeystoreNigori());
  1412. // Ensure the cryptographer corresponds to full keystore Nigori.
  1413. EXPECT_THAT(*bridge2->GetCryptographer(), CanDecryptWith(kKeystoreKeyParams));
  1414. EXPECT_THAT(*bridge2->GetCryptographer(),
  1415. CanDecryptWith(kPassphraseKeyParams));
  1416. EXPECT_THAT(*bridge2->GetCryptographer(),
  1417. HasDefaultKeyDerivedFrom(kKeystoreKeyParams));
  1418. }
  1419. // Tests that upon startup bridge adds keystore keys into cryptographer, so it
  1420. // can later decrypt the data using them.
  1421. TEST_F(NigoriSyncBridgeImplPersistenceTest,
  1422. ShouldDecryptWithKeystoreKeysAfterRestart) {
  1423. // Emulate storing on disc.
  1424. auto storage1 = std::make_unique<testing::NiceMock<MockNigoriStorage>>();
  1425. sync_pb::NigoriLocalData nigori_local_data;
  1426. ON_CALL(*storage1, StoreData)
  1427. .WillByDefault(testing::SaveArg<0>(&nigori_local_data));
  1428. auto bridge1 = std::make_unique<NigoriSyncBridgeImpl>(
  1429. std::make_unique<testing::NiceMock<MockNigoriLocalChangeProcessor>>(),
  1430. std::move(storage1));
  1431. // Perform initial sync with custom passphrase Nigori without keystore keys.
  1432. const std::vector<uint8_t> kRawKeystoreKey = {0, 1, 2, 3, 4};
  1433. const KeyParamsForTesting kPassphraseKeyParams =
  1434. Pbkdf2PassphraseKeyParamsForTesting("passphrase");
  1435. EntityData entity_data;
  1436. *entity_data.specifics.mutable_nigori() =
  1437. BuildCustomPassphraseNigoriSpecifics(kPassphraseKeyParams);
  1438. ASSERT_TRUE(bridge1->SetKeystoreKeys({kRawKeystoreKey}));
  1439. ASSERT_THAT(bridge1->MergeSyncData(std::move(entity_data)),
  1440. Eq(absl::nullopt));
  1441. bridge1->SetExplicitPassphraseDecryptionKey(
  1442. MakeNigoriKey(kPassphraseKeyParams));
  1443. // Mimic the browser restart.
  1444. auto storage2 = std::make_unique<testing::NiceMock<MockNigoriStorage>>();
  1445. ON_CALL(*storage2, RestoreData()).WillByDefault(Return(nigori_local_data));
  1446. auto processor2 =
  1447. std::make_unique<testing::NiceMock<MockNigoriLocalChangeProcessor>>();
  1448. ON_CALL(*processor2, IsTrackingMetadata()).WillByDefault(Return(true));
  1449. // No commits should be issued.
  1450. EXPECT_CALL(*processor2, Put).Times(0);
  1451. auto bridge2 = std::make_unique<NigoriSyncBridgeImpl>(std::move(processor2),
  1452. std::move(storage2));
  1453. EXPECT_THAT(*bridge2->GetCryptographer(),
  1454. CanDecryptWith(KeystoreKeyParamsForTesting(kRawKeystoreKey)));
  1455. EXPECT_THAT(*bridge2->GetCryptographer(),
  1456. CanDecryptWith(kPassphraseKeyParams));
  1457. EXPECT_THAT(*bridge2->GetCryptographer(),
  1458. HasDefaultKeyDerivedFrom(kPassphraseKeyParams));
  1459. }
  1460. } // namespace
  1461. } // namespace syncer