1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "sync/test/fake_sync_encryption_handler.h"
7 #include "sync/protocol/nigori_specifics.pb.h"
8 #include "sync/syncable/nigori_util.h"
12 FakeSyncEncryptionHandler::FakeSyncEncryptionHandler()
13 : encrypted_types_(SensitiveTypes()),
14 encrypt_everything_(false),
15 passphrase_type_(IMPLICIT_PASSPHRASE
),
16 cryptographer_(&encryptor_
) {
18 FakeSyncEncryptionHandler::~FakeSyncEncryptionHandler() {}
20 void FakeSyncEncryptionHandler::Init() {
21 // Set up a basic cryptographer.
22 KeyParams keystore_params
= {"localhost", "dummy", "keystore_key"};
23 cryptographer_
.AddKey(keystore_params
);
26 void FakeSyncEncryptionHandler::ApplyNigoriUpdate(
27 const sync_pb::NigoriSpecifics
& nigori
,
28 syncable::BaseTransaction
* const trans
) {
29 if (nigori
.encrypt_everything())
30 EnableEncryptEverything();
31 if (nigori
.keybag_is_frozen())
32 passphrase_type_
= CUSTOM_PASSPHRASE
;
34 // TODO(zea): consider adding fake support for migration.
35 if (cryptographer_
.CanDecrypt(nigori
.encryption_keybag()))
36 cryptographer_
.InstallKeys(nigori
.encryption_keybag());
37 else if (nigori
.has_encryption_keybag())
38 cryptographer_
.SetPendingKeys(nigori
.encryption_keybag());
40 if (cryptographer_
.has_pending_keys()) {
41 DVLOG(1) << "OnPassPhraseRequired Sent";
42 sync_pb::EncryptedData pending_keys
= cryptographer_
.GetPendingKeys();
43 FOR_EACH_OBSERVER(SyncEncryptionHandler::Observer
, observers_
,
44 OnPassphraseRequired(REASON_DECRYPTION
,
46 } else if (!cryptographer_
.is_ready()) {
47 DVLOG(1) << "OnPassphraseRequired sent because cryptographer is not "
49 FOR_EACH_OBSERVER(SyncEncryptionHandler::Observer
, observers_
,
50 OnPassphraseRequired(REASON_ENCRYPTION
,
51 sync_pb::EncryptedData()));
55 void FakeSyncEncryptionHandler::UpdateNigoriFromEncryptedTypes(
56 sync_pb::NigoriSpecifics
* nigori
,
57 syncable::BaseTransaction
* const trans
) const {
58 syncable::UpdateNigoriFromEncryptedTypes(encrypted_types_
,
63 bool FakeSyncEncryptionHandler::NeedKeystoreKey(
64 syncable::BaseTransaction
* const trans
) const {
65 return keystore_key_
.empty();
68 bool FakeSyncEncryptionHandler::SetKeystoreKeys(
69 const google::protobuf::RepeatedPtrField
<google::protobuf::string
>& keys
,
70 syncable::BaseTransaction
* const trans
) {
73 std::string new_key
= keys
.Get(keys
.size()-1);
76 keystore_key_
= new_key
;
79 DVLOG(1) << "Keystore bootstrap token updated.";
80 FOR_EACH_OBSERVER(SyncEncryptionHandler::Observer
, observers_
,
81 OnBootstrapTokenUpdated(keystore_key_
,
82 KEYSTORE_BOOTSTRAP_TOKEN
));
86 ModelTypeSet
FakeSyncEncryptionHandler::GetEncryptedTypes(
87 syncable::BaseTransaction
* const trans
) const {
88 return encrypted_types_
;
91 void FakeSyncEncryptionHandler::AddObserver(Observer
* observer
) {
92 observers_
.AddObserver(observer
);
95 void FakeSyncEncryptionHandler::RemoveObserver(Observer
* observer
) {
96 observers_
.RemoveObserver(observer
);
99 void FakeSyncEncryptionHandler::SetEncryptionPassphrase(
100 const std::string
& passphrase
,
103 passphrase_type_
= CUSTOM_PASSPHRASE
;
106 void FakeSyncEncryptionHandler::SetDecryptionPassphrase(
107 const std::string
& passphrase
) {
111 void FakeSyncEncryptionHandler::EnableEncryptEverything() {
112 if (encrypt_everything_
)
114 encrypt_everything_
= true;
115 encrypted_types_
= ModelTypeSet::All();
117 Observer
, observers_
,
118 OnEncryptedTypesChanged(encrypted_types_
, encrypt_everything_
));
121 bool FakeSyncEncryptionHandler::EncryptEverythingEnabled() const {
122 return encrypt_everything_
;
125 PassphraseType
FakeSyncEncryptionHandler::GetPassphraseType() const {
126 return passphrase_type_
;
129 } // namespace syncer