Commit dc9b6d57 authored by Maksim Moskvitin's avatar Maksim Moskvitin Committed by Commit Bot

Remove DirectoryCryptographer usages from encryption_helper

DirectoryCryptographer usages are replaced with CryptographerImpl.
KeyParams moved to encryption_helper.h.

Bug: 1061045
Change-Id: I4674ea9b0d5c162b672269622d8361bdfdf25279
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2100933
Commit-Queue: Maksim Moskvitin <mmoskvitin@google.com>
Commit-Queue: Mikel Astiz <mastiz@chromium.org>
Auto-Submit: Maksim Moskvitin <mmoskvitin@google.com>
Reviewed-by: default avatarMikel Astiz <mastiz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#749754}
parent d1337e97
...@@ -13,6 +13,8 @@ ...@@ -13,6 +13,8 @@
#include "components/sync/driver/profile_sync_service.h" #include "components/sync/driver/profile_sync_service.h"
#include "components/sync/driver/sync_client.h" #include "components/sync/driver/sync_client.h"
#include "components/sync/engine/sync_engine_switches.h" #include "components/sync/engine/sync_engine_switches.h"
#include "components/sync/nigori/cryptographer_impl.h"
#include "components/sync/nigori/nigori_key_bag.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
namespace encryption_helper { namespace encryption_helper {
...@@ -33,36 +35,46 @@ std::unique_ptr<syncer::Cryptographer> ...@@ -33,36 +35,46 @@ std::unique_ptr<syncer::Cryptographer>
InitCustomPassphraseCryptographerFromNigori( InitCustomPassphraseCryptographerFromNigori(
const sync_pb::NigoriSpecifics& nigori, const sync_pb::NigoriSpecifics& nigori,
const std::string& passphrase) { const std::string& passphrase) {
auto cryptographer = std::make_unique<syncer::DirectoryCryptographer>(); std::unique_ptr<syncer::CryptographerImpl> cryptographer;
sync_pb::EncryptedData keybag = nigori.encryption_keybag(); sync_pb::EncryptedData keybag = nigori.encryption_keybag();
cryptographer->SetPendingKeys(keybag);
std::string decoded_salt; std::string decoded_salt;
switch (syncer::ProtoKeyDerivationMethodToEnum( switch (syncer::ProtoKeyDerivationMethodToEnum(
nigori.custom_passphrase_key_derivation_method())) { nigori.custom_passphrase_key_derivation_method())) {
case syncer::KeyDerivationMethod::PBKDF2_HMAC_SHA1_1003: case syncer::KeyDerivationMethod::PBKDF2_HMAC_SHA1_1003:
EXPECT_TRUE(cryptographer->DecryptPendingKeys( cryptographer =
{syncer::KeyDerivationParams::CreateForPbkdf2(), passphrase})); syncer::CryptographerImpl::FromSingleKeyForTesting(passphrase);
break; break;
case syncer::KeyDerivationMethod::SCRYPT_8192_8_11: case syncer::KeyDerivationMethod::SCRYPT_8192_8_11:
EXPECT_TRUE(base::Base64Decode( EXPECT_TRUE(base::Base64Decode(
nigori.custom_passphrase_key_derivation_salt(), &decoded_salt)); nigori.custom_passphrase_key_derivation_salt(), &decoded_salt));
EXPECT_TRUE(cryptographer->DecryptPendingKeys( cryptographer = syncer::CryptographerImpl::FromSingleKeyForTesting(
{syncer::KeyDerivationParams::CreateForScrypt(decoded_salt), passphrase,
passphrase})); syncer::KeyDerivationParams::CreateForScrypt(decoded_salt));
break; break;
case syncer::KeyDerivationMethod::UNSUPPORTED: case syncer::KeyDerivationMethod::UNSUPPORTED:
// This test cannot pass since we wouldn't know how to decrypt data // This test cannot pass since we wouldn't know how to decrypt data
// encrypted using an unsupported method. // encrypted using an unsupported method.
ADD_FAILURE() << "Unsupported key derivation method encountered: " ADD_FAILURE() << "Unsupported key derivation method encountered: "
<< nigori.custom_passphrase_key_derivation_method(); << nigori.custom_passphrase_key_derivation_method();
return syncer::CryptographerImpl::CreateEmpty();
} }
std::string decrypted_keys_str;
EXPECT_TRUE(cryptographer->DecryptToString(nigori.encryption_keybag(),
&decrypted_keys_str));
sync_pb::NigoriKeyBag decrypted_keys;
EXPECT_TRUE(decrypted_keys.ParseFromString(decrypted_keys_str));
syncer::NigoriKeyBag key_bag =
syncer::NigoriKeyBag::CreateFromProto(decrypted_keys);
cryptographer->EmplaceKeysFrom(key_bag);
return cryptographer; return cryptographer;
} }
sync_pb::NigoriSpecifics CreateCustomPassphraseNigori( sync_pb::NigoriSpecifics CreateCustomPassphraseNigori(const KeyParams& params) {
const syncer::KeyParams& params) {
syncer::KeyDerivationMethod method = params.derivation_params.method(); syncer::KeyDerivationMethod method = params.derivation_params.method();
sync_pb::NigoriSpecifics nigori; sync_pb::NigoriSpecifics nigori;
...@@ -100,27 +112,26 @@ sync_pb::NigoriSpecifics CreateCustomPassphraseNigori( ...@@ -100,27 +112,26 @@ sync_pb::NigoriSpecifics CreateCustomPassphraseNigori(
// keybag using a key derived from that passphrase). However, in some migrated // keybag using a key derived from that passphrase). However, in some migrated
// states, the keybag might also additionally contain an old, pre-migration // states, the keybag might also additionally contain an old, pre-migration
// key. // key.
syncer::DirectoryCryptographer cryptographer; auto cryptographer = syncer::CryptographerImpl::FromSingleKeyForTesting(
bool add_key_result = cryptographer.AddKey(params); params.password, params.derivation_params);
DCHECK(add_key_result); sync_pb::CryptographerData proto = cryptographer->ToProto();
bool get_keys_result = DCHECK(cryptographer->Encrypt(proto.key_bag(),
cryptographer.GetKeys(nigori.mutable_encryption_keybag()); nigori.mutable_encryption_keybag()));
DCHECK(get_keys_result);
return nigori; return nigori;
} }
sync_pb::EntitySpecifics GetEncryptedBookmarkEntitySpecifics( sync_pb::EntitySpecifics GetEncryptedBookmarkEntitySpecifics(
const sync_pb::BookmarkSpecifics& bookmark_specifics, const sync_pb::BookmarkSpecifics& bookmark_specifics,
const syncer::KeyParams& key_params) { const KeyParams& key_params) {
sync_pb::EntitySpecifics new_specifics; sync_pb::EntitySpecifics new_specifics;
sync_pb::EntitySpecifics wrapped_entity_specifics; sync_pb::EntitySpecifics wrapped_entity_specifics;
*wrapped_entity_specifics.mutable_bookmark() = bookmark_specifics; *wrapped_entity_specifics.mutable_bookmark() = bookmark_specifics;
syncer::DirectoryCryptographer cryptographer; auto cryptographer = syncer::CryptographerImpl::FromSingleKeyForTesting(
bool add_key_result = cryptographer.AddKey(key_params); key_params.password, key_params.derivation_params);
DCHECK(add_key_result);
bool encrypt_result = cryptographer.Encrypt( bool encrypt_result = cryptographer->Encrypt(
wrapped_entity_specifics, new_specifics.mutable_encrypted()); wrapped_entity_specifics, new_specifics.mutable_encrypted());
DCHECK(encrypt_result); DCHECK(encrypt_result);
......
...@@ -12,12 +12,21 @@ ...@@ -12,12 +12,21 @@
#include "chrome/browser/sync/test/integration/single_client_status_change_checker.h" #include "chrome/browser/sync/test/integration/single_client_status_change_checker.h"
#include "chrome/browser/sync/test/integration/status_change_checker.h" #include "chrome/browser/sync/test/integration/status_change_checker.h"
#include "components/sync/driver/trusted_vault_client.h" #include "components/sync/driver/trusted_vault_client.h"
#include "components/sync/nigori/nigori.h"
#include "components/sync/protocol/nigori_specifics.pb.h" #include "components/sync/protocol/nigori_specifics.pb.h"
#include "components/sync/syncable/directory_cryptographer.h"
#include "components/sync/test/fake_server/fake_server.h" #include "components/sync/test/fake_server/fake_server.h"
namespace syncer {
class Cryptographer;
} // namespace syncer
namespace encryption_helper { namespace encryption_helper {
struct KeyParams {
syncer::KeyDerivationParams derivation_params;
std::string password;
};
// Given a |fake_server|, fetches its Nigori node and writes it to the // Given a |fake_server|, fetches its Nigori node and writes it to the
// proto pointed to by |nigori|. Returns false if the server does not contain // proto pointed to by |nigori|. Returns false if the server does not contain
// exactly one Nigori node. // exactly one Nigori node.
...@@ -42,12 +51,11 @@ InitCustomPassphraseCryptographerFromNigori( ...@@ -42,12 +51,11 @@ InitCustomPassphraseCryptographerFromNigori(
// provided BookmarkSpecifics and encrypted using the given |key_params|. // provided BookmarkSpecifics and encrypted using the given |key_params|.
sync_pb::EntitySpecifics GetEncryptedBookmarkEntitySpecifics( sync_pb::EntitySpecifics GetEncryptedBookmarkEntitySpecifics(
const sync_pb::BookmarkSpecifics& specifics, const sync_pb::BookmarkSpecifics& specifics,
const syncer::KeyParams& key_params); const KeyParams& key_params);
// Creates a NigoriSpecifics that describes encryption using a custom passphrase // Creates a NigoriSpecifics that describes encryption using a custom passphrase
// with the given key parameters. // with the given key parameters.
sync_pb::NigoriSpecifics CreateCustomPassphraseNigori( sync_pb::NigoriSpecifics CreateCustomPassphraseNigori(const KeyParams& params);
const syncer::KeyParams& params);
} // namespace encryption_helper } // namespace encryption_helper
......
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
#include "components/sync/driver/profile_sync_service.h" #include "components/sync/driver/profile_sync_service.h"
#include "components/sync/driver/sync_driver_switches.h" #include "components/sync/driver/sync_driver_switches.h"
#include "components/sync/engine/sync_engine_switches.h" #include "components/sync/engine/sync_engine_switches.h"
#include "components/sync/nigori/cryptographer.h" #include "components/sync/nigori/cryptographer_impl.h"
#include "content/public/test/test_launcher.h" #include "content/public/test/test_launcher.h"
#include "crypto/ec_private_key.h" #include "crypto/ec_private_key.h"
#include "testing/gmock/include/gmock/gmock.h" #include "testing/gmock/include/gmock/gmock.h"
...@@ -26,6 +26,7 @@ using encryption_helper::CreateCustomPassphraseNigori; ...@@ -26,6 +26,7 @@ using encryption_helper::CreateCustomPassphraseNigori;
using encryption_helper::GetEncryptedBookmarkEntitySpecifics; using encryption_helper::GetEncryptedBookmarkEntitySpecifics;
using encryption_helper::GetServerNigori; using encryption_helper::GetServerNigori;
using encryption_helper::InitCustomPassphraseCryptographerFromNigori; using encryption_helper::InitCustomPassphraseCryptographerFromNigori;
using encryption_helper::KeyParams;
using encryption_helper::SetNigoriInFakeServer; using encryption_helper::SetNigoriInFakeServer;
using fake_server::FakeServer; using fake_server::FakeServer;
using sync_pb::EncryptedData; using sync_pb::EncryptedData;
...@@ -33,7 +34,6 @@ using sync_pb::NigoriSpecifics; ...@@ -33,7 +34,6 @@ using sync_pb::NigoriSpecifics;
using sync_pb::SyncEntity; using sync_pb::SyncEntity;
using syncer::Cryptographer; using syncer::Cryptographer;
using syncer::KeyDerivationParams; using syncer::KeyDerivationParams;
using syncer::KeyParams;
using syncer::LoopbackServerEntity; using syncer::LoopbackServerEntity;
using syncer::ModelType; using syncer::ModelType;
using syncer::ModelTypeSet; using syncer::ModelTypeSet;
...@@ -107,7 +107,8 @@ class SingleClientCustomPassphraseSyncTest : public SyncTest { ...@@ -107,7 +107,8 @@ class SingleClientCustomPassphraseSyncTest : public SyncTest {
const std::vector<ServerBookmarksEqualityChecker::ExpectedBookmark>& const std::vector<ServerBookmarksEqualityChecker::ExpectedBookmark>&
expected_bookmarks, expected_bookmarks,
const KeyParams& key_params) { const KeyParams& key_params) {
auto cryptographer = CreateCryptographerWithKeyParams(key_params); auto cryptographer = syncer::CryptographerImpl::FromSingleKeyForTesting(
key_params.password, key_params.derivation_params);
return ServerBookmarksEqualityChecker(GetSyncService(), GetFakeServer(), return ServerBookmarksEqualityChecker(GetSyncService(), GetFakeServer(),
expected_bookmarks, expected_bookmarks,
cryptographer.get()) cryptographer.get())
...@@ -160,16 +161,6 @@ class SingleClientCustomPassphraseSyncTest : public SyncTest { ...@@ -160,16 +161,6 @@ class SingleClientCustomPassphraseSyncTest : public SyncTest {
return InitCustomPassphraseCryptographerFromNigori(nigori, passphrase); return InitCustomPassphraseCryptographerFromNigori(nigori, passphrase);
} }
// A cryptographer initialized with the given KeyParams has not "seen" the
// server-side Nigori, and so any data decryptable by such a cryptographer
// does not depend on external info.
std::unique_ptr<Cryptographer> CreateCryptographerWithKeyParams(
const KeyParams& key_params) {
auto cryptographer = std::make_unique<syncer::DirectoryCryptographer>();
cryptographer->AddKey(key_params);
return cryptographer;
}
void InjectEncryptedServerBookmark(const std::string& title, void InjectEncryptedServerBookmark(const std::string& title,
const GURL& url, const GURL& url,
const KeyParams& key_params) { const KeyParams& key_params) {
......
...@@ -37,15 +37,11 @@ ...@@ -37,15 +37,11 @@
namespace { namespace {
using encryption_helper::GetServerNigori; using encryption_helper::GetServerNigori;
using encryption_helper::KeyParams;
using encryption_helper::SetNigoriInFakeServer; using encryption_helper::SetNigoriInFakeServer;
using testing::NotNull; using testing::NotNull;
using testing::SizeIs; using testing::SizeIs;
struct KeyParams {
syncer::KeyDerivationParams derivation_params;
std::string password;
};
MATCHER_P(IsDataEncryptedWith, key_params, "") { MATCHER_P(IsDataEncryptedWith, key_params, "") {
const sync_pb::EncryptedData& encrypted_data = arg; const sync_pb::EncryptedData& encrypted_data = arg;
std::unique_ptr<syncer::Nigori> nigori = syncer::Nigori::CreateByDerivation( std::unique_ptr<syncer::Nigori> nigori = syncer::Nigori::CreateByDerivation(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment