1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/base64.h"
#include "base/memory/raw_ptr.h"
#include "build/build_config.h"
#include "chrome/browser/sync/test/integration/bookmarks_helper.h"
#include "chrome/browser/sync/test/integration/encryption_helper.h"
#include "chrome/browser/sync/test/integration/passwords_helper.h"
#include "chrome/browser/sync/test/integration/sync_engine_stopped_checker.h"
#include "chrome/browser/sync/test/integration/sync_service_impl_harness.h"
#include "chrome/browser/sync/test/integration/sync_test.h"
#include "components/password_manager/core/browser/password_form.h"
#include "components/sync/base/passphrase_enums.h"
#include "components/sync/engine/nigori/key_derivation_params.h"
#include "components/sync/engine/nigori/nigori.h"
#include "components/sync/nigori/cryptographer_impl.h"
#include "components/sync/service/sync_service_impl.h"
#include "components/sync/test/fake_server_nigori_helper.h"
#include "components/sync/test/nigori_test_utils.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/test_launcher.h"
#include "testing/gmock/include/gmock/gmock.h"
namespace {
using bookmarks_helper::AddURL;
using bookmarks_helper::BookmarksTitleChecker;
using bookmarks_helper::CreateBookmarkServerEntity;
using bookmarks_helper::ServerBookmarksEqualityChecker;
using fake_server::FakeServer;
using fake_server::GetServerNigori;
using fake_server::SetNigoriInFakeServer;
using sync_pb::NigoriSpecifics;
using syncer::BuildCustomPassphraseNigoriSpecifics;
using syncer::Cryptographer;
using syncer::DataTypeSet;
using syncer::GetEncryptedBookmarkEntitySpecifics;
using syncer::InitCustomPassphraseCryptographerFromNigori;
using syncer::KeyParamsForTesting;
using syncer::LoopbackServerEntity;
using syncer::PassphraseType;
using syncer::Pbkdf2PassphraseKeyParamsForTesting;
using syncer::ProtoPassphraseInt32ToEnum;
using syncer::ScryptPassphraseKeyParamsForTesting;
using syncer::SyncEngineStoppedChecker;
using testing::ElementsAre;
// Intercepts all bookmark entity names as committed to the FakeServer.
class CommittedBookmarkEntityNameObserver : public FakeServer::Observer {
public:
explicit CommittedBookmarkEntityNameObserver(FakeServer* fake_server)
: fake_server_(fake_server) {
fake_server->AddObserver(this);
}
~CommittedBookmarkEntityNameObserver() override {
fake_server_->RemoveObserver(this);
}
void OnCommit(DataTypeSet committed_data_types) override {
sync_pb::ClientToServerMessage message;
fake_server_->GetLastCommitMessage(&message);
for (const sync_pb::SyncEntity& entity : message.commit().entries()) {
if (syncer::GetDataTypeFromSpecifics(entity.specifics()) ==
syncer::BOOKMARKS) {
committed_names_.insert(entity.name());
}
}
}
const std::set<std::string> GetCommittedEntityNames() const {
return committed_names_;
}
private:
const raw_ptr<FakeServer> fake_server_;
std::set<std::string> committed_names_;
};
// These tests use a gray-box testing approach to verify that the data committed
// to the server is encrypted properly, and that properly-encrypted data from
// the server is successfully decrypted by the client. They also verify that the
// key derivation methods are set, read and handled properly. They do not,
// however, directly ensure that two clients syncing through the same account
// will be able to access each others' data in the presence of a custom
// passphrase. For this, a separate two-client test is be used.
class SingleClientCustomPassphraseSyncTest : public SyncTest {
public:
SingleClientCustomPassphraseSyncTest() : SyncTest(SINGLE_CLIENT) {}
SingleClientCustomPassphraseSyncTest(
const SingleClientCustomPassphraseSyncTest&) = delete;
SingleClientCustomPassphraseSyncTest& operator=(
const SingleClientCustomPassphraseSyncTest&) = delete;
~SingleClientCustomPassphraseSyncTest() override = default;
// Waits until the given set of bookmarks appears on the server, encrypted
// according to the server-side Nigori and with the given passphrase.
bool WaitForEncryptedServerBookmarks(
const std::vector<ServerBookmarksEqualityChecker::ExpectedBookmark>&
expected_bookmarks,
const std::string& passphrase) {
std::unique_ptr<Cryptographer> cryptographer =
CreateCryptographerFromServerNigori(passphrase);
return ServerBookmarksEqualityChecker(expected_bookmarks,
cryptographer.get())
.Wait();
}
bool WaitForUnencryptedServerBookmarks(
const std::vector<ServerBookmarksEqualityChecker::ExpectedBookmark>&
expected_bookmarks) {
return ServerBookmarksEqualityChecker(expected_bookmarks,
/*cryptographer=*/nullptr)
.Wait();
}
bool WaitForNigori(PassphraseType expected_passphrase_type) {
return ServerPassphraseTypeChecker(expected_passphrase_type).Wait();
}
bool WaitForPassphraseRequired() {
return PassphraseRequiredChecker(GetSyncService()).Wait();
}
bool WaitForPassphraseAccepted() {
return PassphraseAcceptedChecker(GetSyncService()).Wait();
}
bool WaitForClientBookmarkWithTitle(const std::u16string title) {
return BookmarksTitleChecker(/*profile_index=*/0, title,
/*expected_count=*/1)
.Wait();
}
syncer::SyncServiceImpl* GetSyncService() {
return SyncTest::GetSyncService(0);
}
// When the cryptographer is initialized with a passphrase, it uses the key
// derivation method and other parameters from the server-side Nigori. Thus,
// checking that the server-side Nigori contains the desired key derivation
// method and checking that the server-side encrypted bookmarks can be
// decrypted using a cryptographer initialized with this function is
// sufficient to determine that a given key derivation method is being
// correctly used for encryption.
std::unique_ptr<Cryptographer> CreateCryptographerFromServerNigori(
const std::string& passphrase) {
NigoriSpecifics nigori;
EXPECT_TRUE(GetServerNigori(GetFakeServer(), &nigori));
EXPECT_EQ(ProtoPassphraseInt32ToEnum(nigori.passphrase_type()),
PassphraseType::kCustomPassphrase);
return InitCustomPassphraseCryptographerFromNigori(nigori, passphrase);
}
void InjectEncryptedServerBookmark(const std::u16string& title,
const GURL& url,
const KeyParamsForTesting& key_params) {
std::unique_ptr<LoopbackServerEntity> server_entity =
CreateBookmarkServerEntity(title, url);
server_entity->SetSpecifics(GetEncryptedBookmarkEntitySpecifics(
server_entity->GetSpecifics().bookmark(), key_params));
GetFakeServer()->InjectEntity(std::move(server_entity));
}
};
IN_PROC_BROWSER_TEST_F(SingleClientCustomPassphraseSyncTest,
ShouldSetNewPassphraseAndCommitEncryptedData) {
const std::u16string title1 = u"Hello world";
const std::u16string title2 = u"Bookmark #2";
const GURL page_url1("https://google.com/");
const GURL page_url2("https://example.com/");
ASSERT_TRUE(SetupSync());
GetSyncService()->GetUserSettings()->SetEncryptionPassphrase("hunter2");
ASSERT_TRUE(AddURL(/*profile=*/0, title1, page_url1));
ASSERT_TRUE(AddURL(/*profile=*/0, title2, page_url2));
ASSERT_TRUE(WaitForNigori(PassphraseType::kCustomPassphrase));
NigoriSpecifics nigori;
EXPECT_TRUE(GetServerNigori(GetFakeServer(), &nigori));
EXPECT_EQ(nigori.custom_passphrase_key_derivation_method(),
sync_pb::NigoriSpecifics::SCRYPT_8192_8_11);
EXPECT_TRUE(WaitForEncryptedServerBookmarks(
{{title1, page_url1}, {title2, page_url2}},
/*passphrase=*/"hunter2"));
}
IN_PROC_BROWSER_TEST_F(SingleClientCustomPassphraseSyncTest,
ShouldDecryptPbkdf2KeyEncryptedData) {
const KeyParamsForTesting kKeyParams =
Pbkdf2PassphraseKeyParamsForTesting("hunter2");
InjectEncryptedServerBookmark(u"PBKDF2-encrypted bookmark",
GURL("http://example.com/doesnt-matter"),
kKeyParams);
SetNigoriInFakeServer(BuildCustomPassphraseNigoriSpecifics(kKeyParams),
GetFakeServer());
ASSERT_TRUE(SetupSync(WAIT_FOR_SYNC_SETUP_TO_COMPLETE));
EXPECT_TRUE(GetSyncService()->GetUserSettings()->SetDecryptionPassphrase(
kKeyParams.password));
EXPECT_TRUE(WaitForPassphraseAccepted());
EXPECT_TRUE(WaitForClientBookmarkWithTitle(u"PBKDF2-encrypted bookmark"));
}
IN_PROC_BROWSER_TEST_F(SingleClientCustomPassphraseSyncTest,
ShouldEncryptDataWithPbkdf2Key) {
const KeyParamsForTesting kKeyParams =
Pbkdf2PassphraseKeyParamsForTesting("hunter2");
SetNigoriInFakeServer(BuildCustomPassphraseNigoriSpecifics(kKeyParams),
GetFakeServer());
ASSERT_TRUE(SetupSync(WAIT_FOR_SYNC_SETUP_TO_COMPLETE));
EXPECT_TRUE(GetSyncService()->GetUserSettings()->SetDecryptionPassphrase(
kKeyParams.password));
EXPECT_TRUE(WaitForPassphraseAccepted());
const std::u16string kTitle = u"Should be encrypted";
const GURL kURL("https://google.com/encrypted");
ASSERT_TRUE(AddURL(/*profile=*/0, kTitle, kURL));
EXPECT_TRUE(WaitForEncryptedServerBookmarks({{kTitle, kURL}},
/*passphrase=*/"hunter2"));
}
IN_PROC_BROWSER_TEST_F(SingleClientCustomPassphraseSyncTest,
ShouldDecryptScryptKeyEncryptedData) {
const KeyParamsForTesting kKeyParams =
ScryptPassphraseKeyParamsForTesting("hunter2");
InjectEncryptedServerBookmark(u"scypt-encrypted bookmark",
GURL("http://example.com/doesnt-matter"),
kKeyParams);
SetNigoriInFakeServer(BuildCustomPassphraseNigoriSpecifics(kKeyParams),
GetFakeServer());
ASSERT_TRUE(SetupSync(WAIT_FOR_SYNC_SETUP_TO_COMPLETE));
EXPECT_TRUE(GetSyncService()->GetUserSettings()->SetDecryptionPassphrase(
kKeyParams.password));
EXPECT_TRUE(WaitForPassphraseAccepted());
EXPECT_TRUE(WaitForClientBookmarkWithTitle(u"scypt-encrypted bookmark"));
}
IN_PROC_BROWSER_TEST_F(SingleClientCustomPassphraseSyncTest,
ShouldEncryptDataWithScryptKey) {
const KeyParamsForTesting kKeyParams =
ScryptPassphraseKeyParamsForTesting("hunter2");
SetNigoriInFakeServer(BuildCustomPassphraseNigoriSpecifics(kKeyParams),
GetFakeServer());
ASSERT_TRUE(SetupSync(WAIT_FOR_SYNC_SETUP_TO_COMPLETE));
EXPECT_TRUE(GetSyncService()->GetUserSettings()->SetDecryptionPassphrase(
kKeyParams.password));
EXPECT_TRUE(WaitForPassphraseAccepted());
const std::u16string kTitle = u"Should be encrypted";
const GURL kURL("https://google.com/encrypted");
ASSERT_TRUE(AddURL(/*profile=*/0, kTitle, kURL));
EXPECT_TRUE(WaitForEncryptedServerBookmarks({{kTitle, kURL}},
/*passphrase=*/"hunter2"));
}
// PRE_* tests aren't supported on Android browser tests.
#if !BUILDFLAG(IS_ANDROID)
// Populates custom passphrase Nigori without keystore keys to the client.
IN_PROC_BROWSER_TEST_F(SingleClientCustomPassphraseSyncTest,
PRE_CanDecryptWithKeystoreKeys) {
const KeyParamsForTesting key_params =
Pbkdf2PassphraseKeyParamsForTesting("hunter2");
SetNigoriInFakeServer(BuildCustomPassphraseNigoriSpecifics(key_params),
GetFakeServer());
ASSERT_TRUE(SetupSync(WAIT_FOR_SYNC_SETUP_TO_COMPLETE));
ASSERT_TRUE(GetSyncService()->GetUserSettings()->SetDecryptionPassphrase(
key_params.password));
ASSERT_TRUE(WaitForPassphraseAccepted());
}
// Client should be able to decrypt with keystore keys, regardless whether they
// were stored in NigoriSpecifics. It's not a normal state, when the server
// stores some data encrypted with keystore keys, but client is able to
// reencrypt the data and recover from this state.
IN_PROC_BROWSER_TEST_F(SingleClientCustomPassphraseSyncTest,
CanDecryptWithKeystoreKeys) {
const password_manager::PasswordForm password_form =
passwords_helper::CreateTestPasswordForm(0);
passwords_helper::InjectKeystoreEncryptedServerPassword(password_form,
GetFakeServer());
ASSERT_TRUE(SetupClients());
EXPECT_TRUE(
PasswordFormsChecker(/*index=*/0, /*expected_forms=*/{password_form})
.Wait());
}
#endif // !BUILDFLAG(IS_ANDROID)
IN_PROC_BROWSER_TEST_F(SingleClientCustomPassphraseSyncTest,
DoesNotLeakUnencryptedData) {
const std::u16string title = u"Should be encrypted";
const GURL page_url("https://google.com/encrypted");
ASSERT_TRUE(SetupClients());
// Create local bookmarks before setting up sync.
CommittedBookmarkEntityNameObserver observer(GetFakeServer());
ASSERT_TRUE(AddURL(/*profile=*/0, title, page_url));
// Mimic custom passphrase being set during initial sync setup.
ASSERT_TRUE(GetClient(0)->SetupSyncWithCustomSettings(
/*user_settings_callback=*/base::BindOnce(
[](syncer::SyncUserSettings* user_settings) {
user_settings->SetEncryptionPassphrase("hunter2");
#if !BUILDFLAG(IS_CHROMEOS)
user_settings->SetInitialSyncFeatureSetupComplete(
syncer::SyncFirstSetupCompleteSource::ADVANCED_FLOW_CONFIRM);
#endif // !BUILDFLAG(IS_CHROMEOS)
})));
ASSERT_TRUE(WaitForNigori(PassphraseType::kCustomPassphrase));
// Ensure that only encrypted bookmarks were committed and that they are
// encrypted using custom passprhase.
EXPECT_TRUE(WaitForEncryptedServerBookmarks({{title, page_url}},
/*passphrase=*/"hunter2"));
EXPECT_THAT(observer.GetCommittedEntityNames(), ElementsAre("encrypted"));
}
IN_PROC_BROWSER_TEST_F(SingleClientCustomPassphraseSyncTest,
ReencryptsDataWhenPassphraseIsSet) {
const std::u16string title = u"Re-encryption is great";
const GURL page_url("https://google.com/re-encrypted");
ASSERT_TRUE(SetupSync());
ASSERT_TRUE(WaitForNigori(PassphraseType::kKeystorePassphrase));
ASSERT_TRUE(AddURL(/*profile=*/0, title, page_url));
const std::vector<ServerBookmarksEqualityChecker::ExpectedBookmark> expected =
{{title, page_url}};
ASSERT_TRUE(WaitForUnencryptedServerBookmarks(expected));
GetSyncService()->GetUserSettings()->SetEncryptionPassphrase("hunter2");
ASSERT_TRUE(WaitForNigori(PassphraseType::kCustomPassphrase));
// If WaitForEncryptedServerBookmarks() succeeds, that means that a
// cryptographer initialized with only the key params was able to decrypt the
// data, so the data must be encrypted using a passphrase-derived key (and not
// e.g. the previous keystore key which was stored in the Nigori keybag),
// because that cryptographer has never seen the server-side Nigori.
EXPECT_TRUE(
WaitForEncryptedServerBookmarks(expected, /*passphrase=*/"hunter2"));
}
// Tests that on receiving CLIENT_DATA_OBSOLETE passphrase is silently restored,
// e.g. user input is not needed.
IN_PROC_BROWSER_TEST_F(
SingleClientCustomPassphraseSyncTest,
ShouldRestorePassphraseOnClientDataObsoleteResponseWhenPassphraseSetByDecryption) {
// Set up sync with custom passphrase.
ASSERT_TRUE(SetupSync());
const KeyParamsForTesting kKeyParams =
ScryptPassphraseKeyParamsForTesting("hunter2");
SetNigoriInFakeServer(BuildCustomPassphraseNigoriSpecifics(kKeyParams),
GetFakeServer());
ASSERT_TRUE(WaitForPassphraseRequired());
ASSERT_TRUE(GetSyncService()->GetUserSettings()->SetDecryptionPassphrase(
kKeyParams.password));
ASSERT_TRUE(WaitForPassphraseAccepted());
// Mimic going through CLIENT_DATA_OBSOLETE state.
GetFakeServer()->TriggerError(sync_pb::SyncEnums::CLIENT_DATA_OBSOLETE);
// Trigger sync by making one more change.
ASSERT_TRUE(AddURL(/*profile=*/0, /*title=*/u"title1",
GURL("https://www.google.com")));
ASSERT_TRUE(SyncEngineStoppedChecker(GetSyncService()).Wait());
GetFakeServer()->TriggerError(sync_pb::SyncEnums::SUCCESS);
ASSERT_TRUE(GetClient(0)->AwaitEngineInitialization());
// Make sure the client is still able to decrypt the data.
EXPECT_TRUE(WaitForPassphraseAccepted());
const std::u16string kEncryptedBookmarkTitle = u"title2";
InjectEncryptedServerBookmark(kEncryptedBookmarkTitle,
GURL("https://www.google.com"), kKeyParams);
EXPECT_TRUE(WaitForClientBookmarkWithTitle(kEncryptedBookmarkTitle));
}
// Similar to the above, but passphrase is obtained by
// SetEncryptionPassphrase(). Regression test for crbug.com/1298062.
IN_PROC_BROWSER_TEST_F(
SingleClientCustomPassphraseSyncTest,
ShouldRestorePassphraseOnClientDataObsoleteResponseWhenPassphraseSetByEncryption) {
// Set up sync with custom passphrase.
ASSERT_TRUE(SetupSync());
const std::string kPassphrase = "hunter2";
// Mimic user just enabled custom passphrase.
GetSyncService()->GetUserSettings()->SetEncryptionPassphrase(kPassphrase);
ASSERT_TRUE(WaitForNigori(PassphraseType::kCustomPassphrase));
// Mimic going through CLIENT_DATA_OBSOLETE state.
GetFakeServer()->TriggerError(sync_pb::SyncEnums::CLIENT_DATA_OBSOLETE);
// Trigger sync by making one more change.
ASSERT_TRUE(AddURL(/*profile=*/0, /*title=*/u"title1",
GURL("https://www.google.com")));
ASSERT_TRUE(SyncEngineStoppedChecker(GetSyncService()).Wait());
GetFakeServer()->TriggerError(sync_pb::SyncEnums::SUCCESS);
ASSERT_TRUE(GetClient(0)->AwaitEngineInitialization());
// Make sure the client is still able to decrypt the data.
EXPECT_TRUE(WaitForPassphraseAccepted());
const std::u16string kEncryptedBookmarkTitle = u"title2";
NigoriSpecifics nigori;
EXPECT_TRUE(GetServerNigori(GetFakeServer(), &nigori));
const KeyParamsForTesting key_params{
syncer::InitCustomPassphraseKeyDerivationParamsFromNigori(nigori),
kPassphrase};
InjectEncryptedServerBookmark(kEncryptedBookmarkTitle,
GURL("https://www.google.com"), key_params);
EXPECT_TRUE(WaitForClientBookmarkWithTitle(kEncryptedBookmarkTitle));
}
} // namespace
|