OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef SYNC_UTIL_CRYPTOGRAPHER_H_ | 5 #ifndef SYNC_UTIL_CRYPTOGRAPHER_H_ |
6 #define SYNC_UTIL_CRYPTOGRAPHER_H_ | 6 #define SYNC_UTIL_CRYPTOGRAPHER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/gtest_prod_util.h" | 11 #include "base/gtest_prod_util.h" |
12 #include "base/memory/linked_ptr.h" | 12 #include "base/memory/linked_ptr.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "base/observer_list.h" | |
15 #include "sync/internal_api/public/base/model_type.h" | 14 #include "sync/internal_api/public/base/model_type.h" |
16 #include "sync/protocol/encryption.pb.h" | 15 #include "sync/protocol/encryption.pb.h" |
17 #include "sync/protocol/nigori_specifics.pb.h" | |
18 #include "sync/util/nigori.h" | 16 #include "sync/util/nigori.h" |
19 | 17 |
18 namespace sync_pb { | |
19 class NigoriKeyBag; | |
20 class NigoriSpecifics; | |
21 } | |
22 | |
20 namespace syncer { | 23 namespace syncer { |
21 | 24 |
22 class Encryptor; | 25 class Encryptor; |
26 class SyncEncryptionHandler; | |
23 | 27 |
24 extern const char kNigoriTag[]; | 28 extern const char kNigoriTag[]; |
25 | 29 |
26 // The parameters used to initialize a Nigori instance. | 30 // The parameters used to initialize a Nigori instance. |
27 struct KeyParams { | 31 struct KeyParams { |
28 std::string hostname; | 32 std::string hostname; |
29 std::string username; | 33 std::string username; |
30 std::string password; | 34 std::string password; |
31 }; | 35 }; |
32 | 36 |
33 // This class manages the Nigori objects used to encrypt and decrypt sensitive | 37 // This class manages the Nigori objects used to encrypt and decrypt sensitive |
34 // sync data (eg. passwords). Each Nigori object knows how to handle data | 38 // sync data (eg. passwords). Each Nigori object knows how to handle data |
35 // protected with a particular passphrase. | 39 // protected with a particular passphrase. |
36 // | 40 // |
37 // Whenever an update to the Nigori sync node is received from the server, | 41 // Whenever an update to the Nigori sync node is received from the server, |
38 // SetPendingKeys should be called with the encrypted contents of that node. | 42 // SetPendingKeys should be called with the encrypted contents of that node. |
39 // Most likely, an updated Nigori node means that a new passphrase has been set | 43 // Most likely, an updated Nigori node means that a new passphrase has been set |
40 // and that future node updates won't be decryptable. To remedy this, the user | 44 // and that future node updates won't be decryptable. To remedy this, the user |
41 // should be prompted for the new passphrase and DecryptPendingKeys be called. | 45 // should be prompted for the new passphrase and DecryptPendingKeys be called. |
42 // | 46 // |
43 // Whenever a update to an encrypted node is received from the server, | 47 // Whenever a update to an encrypted node is received from the server, |
44 // CanDecrypt should be used to verify whether the Cryptographer can decrypt | 48 // CanDecrypt should be used to verify whether the Cryptographer can decrypt |
45 // that node. If it cannot, then the application of that update should be | 49 // that node. If it cannot, then the application of that update should be |
46 // delayed until after it can be decrypted. | 50 // delayed until after it can be decrypted. |
47 class Cryptographer { | 51 class Cryptographer { |
48 public: | 52 public: |
49 // All Observer methods are done synchronously, so they're called | |
50 // under a transaction (since all Cryptographer operations are done | |
51 // under a transaction). | |
52 class Observer { | |
53 public: | |
54 // Called when the set of encrypted types or the encrypt | |
55 // everything flag has been changed. Note that this doesn't | |
56 // necessarily mean that encryption has completed for the given | |
57 // types. | |
58 // | |
59 // |encrypted_types| will always be a superset of | |
60 // SensitiveTypes(). If |encrypt_everything| is true, | |
61 // |encrypted_types| will be the set of all known types. | |
62 // | |
63 // Until this function is called, observers can assume that the | |
64 // set of encrypted types is SensitiveTypes() and that the encrypt | |
65 // everything flag is false. | |
66 virtual void OnEncryptedTypesChanged( | |
67 ModelTypeSet encrypted_types, | |
68 bool encrypt_everything) = 0; | |
69 | |
70 protected: | |
71 virtual ~Observer(); | |
72 }; | |
73 | |
74 // Does not take ownership of |encryptor|. | 53 // Does not take ownership of |encryptor|. |
75 explicit Cryptographer(Encryptor* encryptor); | 54 explicit Cryptographer(Encryptor* encryptor); |
76 ~Cryptographer(); | 55 ~Cryptographer(); |
77 | 56 |
78 // When update on cryptographer is called this enum tells if the | 57 // TODO(zea): refactor so that Cryptographer doesn't need any connection |
79 // cryptographer was succesfully able to update using the nigori node or if | 58 // to the SyncEncryptionHandler. |
80 // it needs a key to decrypt the nigori node. | 59 void SetSyncEncryptionHandlerDelegate(SyncEncryptionHandler* delegate); |
tim (not reviewing)
2012/08/13 20:01:00
As discussed offline, it looks like the set of thi
Nicolas Zea
2012/08/13 22:56:38
Done.
| |
81 enum UpdateResult { | |
82 SUCCESS, | |
83 NEEDS_PASSPHRASE | |
84 }; | |
85 | 60 |
86 // Manage observers. | 61 // SyncEncryptionProvider delegator methods (passes through to delegate). |
87 void AddObserver(Observer* observer); | 62 void UpdateFromNigori(const sync_pb::NigoriSpecifics& nigori) ; |
88 void RemoveObserver(Observer* observer); | 63 ModelTypeSet GetEncryptedTypes() const; |
64 void UpdateNigoriFromEncryptedTypes(sync_pb::NigoriSpecifics* nigori) const; | |
89 | 65 |
90 // |restored_bootstrap_token| can be provided via this method to bootstrap | 66 // |restored_bootstrap_token| can be provided via this method to bootstrap |
91 // Cryptographer instance into the ready state (is_ready will be true). | 67 // Cryptographer instance into the ready state (is_ready will be true). |
92 // It must be a string that was previously built by the | 68 // It must be a string that was previously built by the |
93 // GetSerializedBootstrapToken function. It is possible that the token is no | 69 // GetSerializedBootstrapToken function. It is possible that the token is no |
94 // longer valid (due to server key change), in which case the normal | 70 // longer valid (due to server key change), in which case the normal |
95 // decryption code paths will fail and the user will need to provide a new | 71 // decryption code paths will fail and the user will need to provide a new |
96 // passphrase. | 72 // passphrase. |
97 // It is an error to call this if is_ready() == true, though it is fair to | 73 // It is an error to call this if is_ready() == true, though it is fair to |
98 // never call Bootstrap at all. | 74 // never call Bootstrap at all. |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
135 // Creates a new Nigori instance using |params|. If successful, |params| will | 111 // Creates a new Nigori instance using |params|. If successful, |params| will |
136 // become the default encryption key and be used for all future calls to | 112 // become the default encryption key and be used for all future calls to |
137 // Encrypt. | 113 // Encrypt. |
138 bool AddKey(const KeyParams& params); | 114 bool AddKey(const KeyParams& params); |
139 | 115 |
140 // Same as AddKey(..), but builds the new Nigori from a previously persisted | 116 // Same as AddKey(..), but builds the new Nigori from a previously persisted |
141 // bootstrap token. This can be useful when consuming a bootstrap token | 117 // bootstrap token. This can be useful when consuming a bootstrap token |
142 // with a cryptographer that has already been initialized. | 118 // with a cryptographer that has already been initialized. |
143 bool AddKeyFromBootstrapToken(const std::string restored_bootstrap_token); | 119 bool AddKeyFromBootstrapToken(const std::string restored_bootstrap_token); |
144 | 120 |
121 // Decrypts |encrypted| and uses its contents to initialize Nigori instances. | |
122 // Returns true unless decryption of |encrypted| fails. The caller is | |
123 // responsible for checking that CanDecrypt(encrypted) == true. | |
124 // Does not update the default nigori. | |
125 void InstallKeys(const sync_pb::EncryptedData& encrypted); | |
126 | |
127 | |
145 // Makes a local copy of |encrypted| to later be decrypted by | 128 // Makes a local copy of |encrypted| to later be decrypted by |
146 // DecryptPendingKeys. This should only be used if CanDecrypt(encrypted) == | 129 // DecryptPendingKeys. This should only be used if CanDecrypt(encrypted) == |
147 // false. | 130 // false. |
148 void SetPendingKeys(const sync_pb::EncryptedData& encrypted); | 131 void SetPendingKeys(const sync_pb::EncryptedData& encrypted); |
149 | 132 |
150 // Makes |pending_keys_| available to callers that may want to cache its | 133 // Makes |pending_keys_| available to callers that may want to cache its |
151 // value for later use on the UI thread. It is illegal to call this if the | 134 // value for later use on the UI thread. It is illegal to call this if the |
152 // cryptographer has no pending keys. Like other calls that access the | 135 // cryptographer has no pending keys. Like other calls that access the |
153 // cryptographer, this method must be called from within a transaction. | 136 // cryptographer, this method must be called from within a transaction. |
154 const sync_pb::EncryptedData& GetPendingKeys() const; | 137 const sync_pb::EncryptedData& GetPendingKeys() const; |
155 | 138 |
156 // Attempts to decrypt the set of keys that was copied in the previous call to | 139 // Attempts to decrypt the set of keys that was copied in the previous call to |
157 // SetPendingKeys using |params|. Returns true if the pending keys were | 140 // SetPendingKeys using |params|. Returns true if the pending keys were |
158 // successfully decrypted and installed. If successful, the default key | 141 // successfully decrypted and installed. If successful, the default key |
159 // is updated. | 142 // is updated. |
160 bool DecryptPendingKeys(const KeyParams& params); | 143 bool DecryptPendingKeys(const KeyParams& params); |
161 | 144 |
145 // Sets the default key to the nigori with name |key_name|. |key_name| must | |
146 // correspond to a nigori that has already been installed into the keybag. | |
147 void SetDefaultKey(std::string key_name); | |
148 | |
162 bool is_initialized() const { return !nigoris_.empty() && default_nigori_; } | 149 bool is_initialized() const { return !nigoris_.empty() && default_nigori_; } |
163 | 150 |
164 // Returns whether this Cryptographer is ready to encrypt and decrypt data. | 151 // Returns whether this Cryptographer is ready to encrypt and decrypt data. |
165 bool is_ready() const { return is_initialized() && | 152 bool is_ready() const { return is_initialized() && |
166 has_pending_keys() == false; } | 153 has_pending_keys() == false; } |
167 | 154 |
168 // Returns whether there is a pending set of keys that needs to be decrypted. | 155 // Returns whether there is a pending set of keys that needs to be decrypted. |
169 bool has_pending_keys() const { return NULL != pending_keys_.get(); } | 156 bool has_pending_keys() const { return NULL != pending_keys_.get(); } |
170 | 157 |
171 // Obtain a token that can be provided on construction to a future | 158 // Obtain a token that can be provided on construction to a future |
172 // Cryptographer instance to bootstrap itself. Returns false if such a token | 159 // Cryptographer instance to bootstrap itself. Returns false if such a token |
173 // can't be created (i.e. if this Cryptograhper doesn't have valid keys). | 160 // can't be created (i.e. if this Cryptograhper doesn't have valid keys). |
174 bool GetBootstrapToken(std::string* token) const; | 161 bool GetBootstrapToken(std::string* token) const; |
175 | 162 |
176 // Obtain the bootstrap token based on the keystore encryption key. | 163 // Obtain the bootstrap token based on the keystore encryption key. |
177 bool GetKeystoreKeyBootstrapToken(std::string* token) const; | 164 bool GetKeystoreKeyBootstrapToken(std::string* token) const; |
178 | 165 |
179 // Update the cryptographer based on the contents of the nigori specifics. | |
180 // This updates both the encryption keys and the set of encrypted types. | |
181 // Returns NEEDS_PASSPHRASE if was unable to decrypt the pending keys, | |
182 // SUCCESS otherwise. | |
183 // Note: will not change the default key. If the nigori's keybag | |
184 // is decryptable, all keys are added to the local keybag and the current | |
185 // default is preserved. If the nigori's keybag is not decryptable, it is | |
186 // stored in the |pending_keys_|. | |
187 UpdateResult Update(const sync_pb::NigoriSpecifics& nigori); | |
188 | |
189 // Set the keystore-derived nigori from the provided key. | 166 // Set the keystore-derived nigori from the provided key. |
190 // Returns true if we succesfully create the keystore derived nigori from the | 167 // Returns true if we succesfully create the keystore derived nigori from the |
191 // provided key, false otherwise. | 168 // provided key, false otherwise. |
192 bool SetKeystoreKey(const std::string& keystore_key); | 169 bool SetKeystoreKey(const std::string& keystore_key); |
193 | 170 |
194 // Returns true if we currently have a keystore-derived nigori, false | 171 // Returns true if we currently have a keystore-derived nigori, false |
195 // otherwise. | 172 // otherwise. |
196 bool HasKeystoreKey() const; | 173 bool HasKeystoreKey() const; |
197 | 174 |
198 // The set of types that are always encrypted. | 175 Encryptor* encryptor() const { return encryptor_; } |
199 static ModelTypeSet SensitiveTypes(); | |
200 | |
201 // Reset our set of encrypted types based on the contents of the nigori | |
202 // specifics. | |
203 void UpdateEncryptedTypesFromNigori(const sync_pb::NigoriSpecifics& nigori); | |
204 | |
205 // Update the nigori to reflect the current set of encrypted types. | |
206 void UpdateNigoriFromEncryptedTypes(sync_pb::NigoriSpecifics* nigori) const; | |
207 | |
208 // Setter/getter for whether all current and future datatypes should | |
209 // be encrypted. Once set you cannot unset without reading from a | |
210 // new nigori node. set_encrypt_everything() emits a notification | |
211 // the first time it's called. | |
212 void set_encrypt_everything(); | |
213 bool encrypt_everything() const; | |
214 | |
215 // Return the set of encrypted types. | |
216 ModelTypeSet GetEncryptedTypes() const; | |
217 | |
218 // Forwards to MergeEncryptedTypes. | |
219 void MergeEncryptedTypesForTest(ModelTypeSet encrypted_types); | |
220 | 176 |
221 private: | 177 private: |
222 FRIEND_TEST_ALL_PREFIXES(SyncCryptographerTest, PackUnpack); | 178 FRIEND_TEST_ALL_PREFIXES(SyncCryptographerTest, PackUnpack); |
179 | |
223 typedef std::map<std::string, linked_ptr<const Nigori> > NigoriMap; | 180 typedef std::map<std::string, linked_ptr<const Nigori> > NigoriMap; |
224 | 181 |
225 // Merges the given set of encrypted types with the existing set and emits a | |
226 // notification if necessary. | |
227 void MergeEncryptedTypes(ModelTypeSet encrypted_types); | |
228 | |
229 void EmitEncryptedTypesChangedNotification(); | |
230 | |
231 // Decrypts |encrypted| and uses its contents to initialize Nigori instances. | |
232 // Returns true unless decryption of |encrypted| fails. The caller is | |
233 // responsible for checking that CanDecrypt(encrypted) == true. | |
234 // Does not update the default nigori. | |
235 void InstallKeys(const sync_pb::EncryptedData& encrypted); | |
236 | |
237 // Helper method to instantiate Nigori instances for each set of key | 182 // Helper method to instantiate Nigori instances for each set of key |
238 // parameters in |bag|. | 183 // parameters in |bag|. |
239 // Does not update the default nigori. | 184 // Does not update the default nigori. |
240 void InstallKeyBag(const sync_pb::NigoriKeyBag& bag); | 185 void InstallKeyBag(const sync_pb::NigoriKeyBag& bag); |
241 | 186 |
242 // Helper method to add a nigori as either the new default nigori or the new | 187 // Helper method to add a nigori as either the new default nigori or the new |
243 // keystore nigori. | 188 // keystore nigori. |
244 bool AddKeyImpl(Nigori* nigori, bool is_keystore_key); | 189 bool AddKeyImpl(Nigori* nigori, bool is_keystore_key); |
245 | 190 |
246 // Functions to serialize + encrypt a Nigori object in an opaque format for | 191 // Functions to serialize + encrypt a Nigori object in an opaque format for |
247 // persistence by sync infrastructure. | 192 // persistence by sync infrastructure. |
248 bool PackBootstrapToken(const Nigori* nigori, std::string* pack_into) const; | 193 bool PackBootstrapToken(const Nigori* nigori, std::string* pack_into) const; |
249 Nigori* UnpackBootstrapToken(const std::string& token) const; | 194 Nigori* UnpackBootstrapToken(const std::string& token) const; |
250 | 195 |
251 Encryptor* const encryptor_; | 196 Encryptor* const encryptor_; |
252 | 197 |
253 ObserverList<Observer> observers_; | |
254 | |
255 NigoriMap nigoris_; // The Nigoris we know about, mapped by key name. | 198 NigoriMap nigoris_; // The Nigoris we know about, mapped by key name. |
256 NigoriMap::value_type* default_nigori_; // The Nigori used for encryption. | 199 NigoriMap::value_type* default_nigori_; // The Nigori used for encryption. |
257 NigoriMap::value_type* keystore_nigori_; // Nigori generated from keystore. | 200 NigoriMap::value_type* keystore_nigori_; // Nigori generated from keystore. |
258 | 201 |
259 scoped_ptr<sync_pb::EncryptedData> pending_keys_; | 202 scoped_ptr<sync_pb::EncryptedData> pending_keys_; |
260 | 203 |
261 ModelTypeSet encrypted_types_; | 204 // The sync encryption provider. Necessary until we decouple the encrypted |
262 bool encrypt_everything_; | 205 // types from the cryptographer. |
206 SyncEncryptionHandler* sync_encryption_delegate_; | |
263 | 207 |
264 DISALLOW_COPY_AND_ASSIGN(Cryptographer); | 208 DISALLOW_COPY_AND_ASSIGN(Cryptographer); |
265 }; | 209 }; |
266 | 210 |
267 } // namespace syncer | 211 } // namespace syncer |
268 | 212 |
269 #endif // SYNC_UTIL_CRYPTOGRAPHER_H_ | 213 #endif // SYNC_UTIL_CRYPTOGRAPHER_H_ |
OLD | NEW |