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

Side by Side Diff: components/sync/driver/sync_service_crypto.h

Issue 2663783002: [Sync] Split encryption state and logic out of PSS and SBHI. (Closed)
Patch Set: Tweak comment. Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 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
5 #ifndef COMPONENTS_SYNC_DRIVER_SYNC_SERVICE_CRYPTO_H_
6 #define COMPONENTS_SYNC_DRIVER_SYNC_SERVICE_CRYPTO_H_
7
8 #include <memory>
9 #include <string>
10
11 #include "base/callback.h"
12 #include "base/memory/weak_ptr.h"
13 #include "components/sync/base/model_type.h"
14 #include "components/sync/engine/sync_encryption_handler.h"
15 #include "components/sync/engine/sync_engine.h"
16
17 namespace syncer {
18
19 class DataTypeManager;
20 class SyncPrefs;
21
22 // This class functions as mostly independent component of SyncServiceBase that
23 // handles things related to encryption, including holding lots of state and
24 // encryption communications with the sync thread.
25 class SyncServiceCrypto : public SyncEncryptionHandler::Observer {
26 public:
27 SyncServiceCrypto(const base::Closure& notify_observers,
28 const base::Callback<ModelTypeSet()>& get_preferred_types,
29 SyncPrefs* sync_prefs);
30 ~SyncServiceCrypto() override;
31
32 // See the SyncService header.
33 base::Time GetExplicitPassphraseTime() const;
34 bool IsUsingSecondaryPassphrase() const;
35 void EnableEncryptEverything();
36 bool IsEncryptEverythingEnabled() const;
37 void SetEncryptionPassphrase(const std::string& passphrase, bool is_explicit);
38 bool SetDecryptionPassphrase(const std::string& passphrase);
39
40 // Returns the actual passphrase type being used for encryption.
41 PassphraseType GetPassphraseType() const;
42
43 // Returns true if encrypting all the sync data is allowed. If this method
44 // returns false, EnableEncryptEverything() should not be called.
45 bool IsEncryptEverythingAllowed() const;
46
47 // Sets whether encrypting all the sync data is allowed or not.
48 void SetEncryptEverythingAllowed(bool allowed);
49
50 // Returns the current set of encrypted data types.
51 ModelTypeSet GetEncryptedDataTypes() const;
52
53 // SyncEncryptionHandler::Observer implementation.
54 void OnPassphraseRequired(
55 PassphraseRequiredReason reason,
56 const sync_pb::EncryptedData& pending_keys) override;
57 void OnPassphraseAccepted() override;
58 void OnBootstrapTokenUpdated(const std::string& bootstrap_token,
59 BootstrapTokenType type) override;
60 void OnEncryptedTypesChanged(ModelTypeSet encrypted_types,
61 bool encrypt_everything) override;
62 void OnEncryptionComplete() override;
63 void OnCryptographerStateChanged(Cryptographer* cryptographer) override;
64 void OnPassphraseTypeChanged(PassphraseType type,
65 base::Time passphrase_time) override;
66 void OnLocalSetPassphraseEncryption(
67 const SyncEncryptionHandler::NigoriState& nigori_state) override;
68
69 // Calls data type manager to start catch up configure.
70 void BeginConfigureCatchUpBeforeClear();
71
72 // Used to provide the engine and DTM when the engine is initialized.
73 void SetSyncEngine(SyncEngine* engine) { engine_ = engine; }
74 void SetDataTypeManager(DataTypeManager* dtm) { data_type_manager_ = dtm; }
75
76 // Creates a proxy observer object that will post calls to this thread.
77 std::unique_ptr<SyncEncryptionHandler::Observer> GetEncryptionObserverProxy();
78
79 // Takes the previously saved nigori state; null if there isn't any.
80 std::unique_ptr<SyncEncryptionHandler::NigoriState> TakeSavedNigoriState();
81
82 // Sets the cached passphrase.
83 void CachePassphrase(const std::string& passphrase) {
84 cached_passphrase_ = passphrase;
85 }
86
87 // During initial signin, ProfileSyncService caches the user's signin
88 // passphrase so it can be used to encrypt/decrypt data after sync starts up.
89 // This routine is invoked once the engine has started up to use the
90 // cached passphrase and clear it out when it is done.
91 void ConsumeCachedPassphraseIfPossible();
92
93 PassphraseRequiredReason passphrase_required_reason() const {
94 return passphrase_required_reason_;
95 }
96 const std::string& cached_passphrase() { return cached_passphrase_; }
97 bool encryption_pending() { return encryption_pending_; }
98
99 private:
100 // Checks if |passphrase| can be used to decrypt the cryptographer's pending
101 // keys that were cached during NotifyPassphraseRequired. Returns true if
102 // decryption was successful. Returns false otherwise. Must be called with a
103 // non-empty pending keys cache.
104 bool CheckPassphraseAgainstCachedPendingKeys(
105 const std::string& passphrase) const;
106
107 // Calls SyncServiceBase::NotifyObservers(). Never null.
108 const base::Closure notify_observers_;
109
110 // Calls SyncService::GetPreferredDataTypes(). Never null.
111 const base::Callback<ModelTypeSet()> get_preferred_types_;
112
113 // A pointer to the sync prefs. Never null and guaranteed to outlive us.
114 SyncPrefs* const sync_prefs_;
115
116 // These are only not-null when the engine is initialized.
117 SyncEngine* engine_ = nullptr;
118 DataTypeManager* data_type_manager_ = nullptr;
119
120 // Was the last SYNC_PASSPHRASE_REQUIRED notification sent because it
121 // was required for encryption, decryption with a cached passphrase, or
122 // because a new passphrase is required?
123 PassphraseRequiredReason passphrase_required_reason_ =
124 REASON_PASSPHRASE_NOT_REQUIRED;
125
126 // Sometimes we need to temporarily hold on to a passphrase because we don't
127 // yet have a engine to send it to. This happens during initialization as
128 // we don't StartUp until we have a valid token, which happens after valid
129 // credentials were provided.
130 std::string cached_passphrase_;
131
132 // The current set of encrypted types. Always a superset of
133 // Cryptographer::SensitiveTypes().
134 ModelTypeSet encrypted_types_ = SyncEncryptionHandler::SensitiveTypes();
135
136 // Whether encrypting everything is allowed.
137 bool encrypt_everything_allowed_ = true;
138
139 // Whether we want to encrypt everything.
140 bool encrypt_everything_ = false;
141
142 // Whether we're waiting for an attempt to encryption all sync data to
143 // complete. We track this at this layer in order to allow the user to cancel
144 // if they e.g. don't remember their explicit passphrase.
145 bool encryption_pending_ = false;
146
147 // Nigori state after user switching to custom passphrase, saved until
148 // transition steps complete. It will be injected into new engine after sync
149 // restart.
150 std::unique_ptr<SyncEncryptionHandler::NigoriState> saved_nigori_state_;
151
152 // We cache the cryptographer's pending keys whenever NotifyPassphraseRequired
153 // is called. This way, before the UI calls SetDecryptionPassphrase on the
154 // syncer, it can avoid the overhead of an asynchronous decryption call and
155 // give the user immediate feedback about the passphrase entered by first
156 // trying to decrypt the cached pending keys on the UI thread. Note that
157 // SetDecryptionPassphrase can still fail after the cached pending keys are
158 // successfully decrypted if the pending keys have changed since the time they
159 // were cached.
160 sync_pb::EncryptedData cached_pending_keys_;
161
162 // The state of the passphrase required to decrypt the bag of encryption keys
163 // in the nigori node. Updated whenever a new nigori node arrives or the user
164 // manually changes their passphrase state. Cached so we can synchronously
165 // check it from the UI thread.
166 PassphraseType cached_passphrase_type_ = PassphraseType::IMPLICIT_PASSPHRASE;
167
168 // If an explicit passphrase is in use, the time at which the passphrase was
169 // first set (if available).
170 base::Time cached_explicit_passphrase_time_;
171
172 base::ThreadChecker thread_checker_;
173 base::WeakPtrFactory<SyncServiceCrypto> weak_factory_;
174
175 DISALLOW_COPY_AND_ASSIGN(SyncServiceCrypto);
176 };
177
178 } // namespace syncer
179
180 #endif // COMPONENTS_SYNC_DRIVER_SYNC_SERVICE_CRYPTO_H_
OLDNEW
« no previous file with comments | « components/sync/driver/sync_service_base.cc ('k') | components/sync/driver/sync_service_crypto.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698