OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "sync/internal_api/sync_encryption_handler_impl.h" | |
6 | |
7 #include <queue> | |
8 #include <string> | |
9 | |
10 #include "base/bind.h" | |
11 #include "base/message_loop.h" | |
12 #include "base/tracked_objects.h" | |
13 #include "base/metrics/histogram.h" | |
14 #include "sync/internal_api/public/read_node.h" | |
15 #include "sync/internal_api/public/read_transaction.h" | |
16 #include "sync/internal_api/public/user_share.h" | |
17 #include "sync/internal_api/public/util/experiments.h" | |
18 #include "sync/internal_api/public/write_node.h" | |
19 #include "sync/internal_api/public/write_transaction.h" | |
20 #include "sync/protocol/encryption.pb.h" | |
21 #include "sync/protocol/nigori_specifics.pb.h" | |
22 #include "sync/syncable/entry.h" | |
23 #include "sync/syncable/nigori_util.h" | |
24 #include "sync/util/cryptographer.h" | |
25 | |
26 namespace syncer { | |
27 | |
28 namespace { | |
29 // The maximum number of times we will automatically overwrite the nigori node | |
30 // because the encryption keys don't match (per chrome instantiation). | |
tim (not reviewing)
2012/08/14 02:32:20
Can you include a link to the bug that led to this
Nicolas Zea
2012/08/14 23:24:51
Done.
| |
31 static const int kNigoriOverwriteLimit = 10; | |
32 } | |
33 | |
34 SyncEncryptionHandlerImpl::SyncEncryptionHandlerImpl( | |
35 UserShare* user_share, | |
36 Cryptographer* cryptographer) | |
37 : weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), | |
38 user_share_(user_share), | |
39 cryptographer_(cryptographer), | |
40 encrypted_types_(SensitiveTypes()), | |
41 encrypt_everything_(false), | |
42 explicit_passphrase_(false), | |
43 nigori_overwrite_count_(0) { | |
44 } | |
45 | |
46 SyncEncryptionHandlerImpl::~SyncEncryptionHandlerImpl() {} | |
47 | |
48 void SyncEncryptionHandlerImpl::AddObserver(Observer* observer) { | |
49 DCHECK(!observers_.HasObserver(observer)); | |
50 observers_.AddObserver(observer); | |
51 } | |
52 | |
53 void SyncEncryptionHandlerImpl::RemoveObserver(Observer* observer) { | |
54 DCHECK(observers_.HasObserver(observer)); | |
55 observers_.RemoveObserver(observer); | |
56 } | |
57 | |
58 void SyncEncryptionHandlerImpl::ReloadNigori() { | |
tim (not reviewing)
2012/08/14 02:32:20
Is InitNigori clearer? Something to get the point
Nicolas Zea
2012/08/14 23:24:51
Went with Init, since otherwise it sounds like it'
| |
59 WriteTransaction trans(FROM_HERE, user_share_); | |
60 WriteNode node(&trans); | |
61 Cryptographer* cryptographer = trans.GetCryptographer(); | |
62 cryptographer_ = cryptographer; | |
63 | |
64 if (node.InitByTagLookup(kNigoriTag) != BaseNode::INIT_OK) | |
65 return; | |
66 if (!ApplyNigoriUpdate(node.GetNigoriSpecifics(), cryptographer)) | |
67 WriteEncryptionStateToNigori(&trans); | |
68 | |
69 FOR_EACH_OBSERVER(SyncEncryptionHandler::Observer, observers_, | |
70 OnCryptographerStateChanged(cryptographer)); | |
71 | |
72 if (cryptographer->is_ready()) | |
73 ReEncryptEverything(&trans); | |
74 } | |
75 | |
76 // Note: this is called from within a syncable transaction, so we need to post | |
77 // tasks if we want to do any work that creates a new sync_api transaction. | |
78 void SyncEncryptionHandlerImpl::UpdateFromNigori( | |
tim (not reviewing)
2012/08/14 02:32:20
Why not make this take a transaction and dcheck on
Nicolas Zea
2012/08/14 23:24:51
Rewrite nigori is necessary to open a new _syncapi
| |
79 const sync_pb::NigoriSpecifics& nigori) { | |
80 if (!ApplyNigoriUpdate(nigori, cryptographer_)) { | |
81 MessageLoop::current()->PostTask( | |
82 FROM_HERE, | |
83 base::Bind(&SyncEncryptionHandlerImpl::RewriteNigori, | |
84 weak_ptr_factory_.GetWeakPtr())); | |
85 } | |
86 | |
87 FOR_EACH_OBSERVER(SyncEncryptionHandler::Observer, observers_, | |
88 OnCryptographerStateChanged(cryptographer_)); | |
89 } | |
90 | |
91 // Note: this is always called via the Cryptographer interface right now, | |
92 // so a transaction is already held. Once we remove that interface, we'll | |
93 // need to enforce holding a transaction when calling this method. | |
94 ModelTypeSet SyncEncryptionHandlerImpl::GetEncryptedTypes() const { | |
95 return encrypted_types_; | |
96 } | |
97 | |
98 void SyncEncryptionHandlerImpl::SetEncryptionPassphrase( | |
tim (not reviewing)
2012/08/14 02:32:20
Was this + SetDecryption pretty much just carried
Nicolas Zea
2012/08/14 23:24:51
Yep, all the same!
| |
99 const std::string& passphrase, | |
100 bool is_explicit) { | |
101 // We do not accept empty passphrases. | |
102 if (passphrase.empty()) { | |
103 NOTREACHED() << "Cannot encrypt with an empty passphrase."; | |
104 return; | |
105 } | |
106 | |
107 // All accesses to the cryptographer are protected by a transaction. | |
108 WriteTransaction trans(FROM_HERE, user_share_); | |
109 Cryptographer* cryptographer = trans.GetCryptographer(); | |
110 KeyParams key_params = {"localhost", "dummy", passphrase}; | |
111 WriteNode node(&trans); | |
112 if (node.InitByTagLookup(kNigoriTag) != BaseNode::INIT_OK) { | |
113 NOTREACHED(); | |
114 return; | |
115 } | |
116 | |
117 bool nigori_has_explicit_passphrase = | |
118 node.GetNigoriSpecifics().using_explicit_passphrase(); | |
119 std::string bootstrap_token; | |
120 sync_pb::EncryptedData pending_keys; | |
121 if (cryptographer->has_pending_keys()) | |
122 pending_keys = cryptographer->GetPendingKeys(); | |
123 bool success = false; | |
124 | |
125 | |
126 // There are six cases to handle here: | |
127 // 1. The user has no pending keys and is setting their current GAIA password | |
128 // as the encryption passphrase. This happens either during first time sync | |
129 // with a clean profile, or after re-authenticating on a profile that was | |
130 // already signed in with the cryptographer ready. | |
131 // 2. The user has no pending keys, and is overwriting an (already provided) | |
132 // implicit passphrase with an explicit (custom) passphrase. | |
133 // 3. The user has pending keys for an explicit passphrase that is somehow set | |
134 // to their current GAIA passphrase. | |
135 // 4. The user has pending keys encrypted with their current GAIA passphrase | |
136 // and the caller passes in the current GAIA passphrase. | |
137 // 5. The user has pending keys encrypted with an older GAIA passphrase | |
138 // and the caller passes in the current GAIA passphrase. | |
139 // 6. The user has previously done encryption with an explicit passphrase. | |
140 // Furthermore, we enforce the fact that the bootstrap encryption token will | |
141 // always be derived from the newest GAIA password if the account is using | |
142 // an implicit passphrase (even if the data is encrypted with an old GAIA | |
143 // password). If the account is using an explicit (custom) passphrase, the | |
144 // bootstrap token will be derived from the most recently provided explicit | |
145 // passphrase (that was able to decrypt the data). | |
146 if (!nigori_has_explicit_passphrase) { | |
147 if (!cryptographer->has_pending_keys()) { | |
148 if (cryptographer->AddKey(key_params)) { | |
149 // Case 1 and 2. We set a new GAIA passphrase when there are no pending | |
150 // keys (1), or overwriting an implicit passphrase with a new explicit | |
151 // one (2) when there are no pending keys. | |
152 DVLOG(1) << "Setting " << (is_explicit ? "explicit" : "implicit" ) | |
153 << " passphrase for encryption."; | |
154 cryptographer->GetBootstrapToken(&bootstrap_token); | |
155 success = true; | |
156 } else { | |
157 NOTREACHED() << "Failed to add key to cryptographer."; | |
158 success = false; | |
159 } | |
160 } else { // cryptographer->has_pending_keys() == true | |
161 if (is_explicit) { | |
162 // This can only happen if the nigori node is updated with a new | |
163 // implicit passphrase while a client is attempting to set a new custom | |
164 // passphrase (race condition). | |
165 DVLOG(1) << "Failing because an implicit passphrase is already set."; | |
166 success = false; | |
167 } else { // is_explicit == false | |
168 if (cryptographer->DecryptPendingKeys(key_params)) { | |
169 // Case 4. We successfully decrypted with the implicit GAIA passphrase | |
170 // passed in. | |
171 DVLOG(1) << "Implicit internal passphrase accepted for decryption."; | |
172 cryptographer->GetBootstrapToken(&bootstrap_token); | |
173 success = true; | |
174 } else { | |
175 // Case 5. Encryption was done with an old GAIA password, but we were | |
176 // provided with the current GAIA password. We need to generate a new | |
177 // bootstrap token to preserve it. We build a temporary cryptographer | |
178 // to allow us to extract these params without polluting our current | |
179 // cryptographer. | |
180 DVLOG(1) << "Implicit internal passphrase failed to decrypt, adding " | |
181 << "anyways as default passphrase and persisting via " | |
182 << "bootstrap token."; | |
183 Cryptographer temp_cryptographer(cryptographer->encryptor()); | |
184 temp_cryptographer.AddKey(key_params); | |
185 temp_cryptographer.GetBootstrapToken(&bootstrap_token); | |
186 // We then set the new passphrase as the default passphrase of the | |
187 // real cryptographer, even though we have pending keys. This is safe, | |
188 // as although Cryptographer::is_initialized() will now be true, | |
189 // is_ready() will remain false due to having pending keys. | |
190 cryptographer->AddKey(key_params); | |
191 success = false; | |
192 } | |
193 } // is_explicit | |
194 } // cryptographer->has_pending_keys() | |
195 } else { // nigori_has_explicit_passphrase == true | |
196 // Case 6. We do not want to override a previously set explicit passphrase, | |
197 // so we return a failure. | |
198 DVLOG(1) << "Failing because an explicit passphrase is already set."; | |
199 success = false; | |
200 } | |
201 | |
202 DVLOG_IF(1, !success) | |
203 << "Failure in SetEncryptionPassphrase; notifying and returning."; | |
204 DVLOG_IF(1, success) | |
205 << "Successfully set encryption passphrase; updating nigori and " | |
206 "reencrypting."; | |
207 | |
208 FinishSetPassphrase( | |
209 success, bootstrap_token, is_explicit, &trans, &node); | |
210 } | |
211 | |
212 void SyncEncryptionHandlerImpl::SetDecryptionPassphrase( | |
213 const std::string& passphrase) { | |
214 // We do not accept empty passphrases. | |
215 if (passphrase.empty()) { | |
216 NOTREACHED() << "Cannot decrypt with an empty passphrase."; | |
217 return; | |
218 } | |
219 | |
220 // All accesses to the cryptographer are protected by a transaction. | |
221 WriteTransaction trans(FROM_HERE, user_share_); | |
222 Cryptographer* cryptographer = trans.GetCryptographer(); | |
223 KeyParams key_params = {"localhost", "dummy", passphrase}; | |
224 WriteNode node(&trans); | |
225 if (node.InitByTagLookup(kNigoriTag) != BaseNode::INIT_OK) { | |
226 NOTREACHED(); | |
227 return; | |
228 } | |
229 | |
230 if (!cryptographer->has_pending_keys()) { | |
231 // Note that this *can* happen in a rare situation where data is | |
232 // re-encrypted on another client while a SetDecryptionPassphrase() call is | |
233 // in-flight on this client. It is rare enough that we choose to do nothing. | |
234 NOTREACHED() << "Attempt to set decryption passphrase failed because there " | |
235 << "were no pending keys."; | |
236 return; | |
237 } | |
238 | |
239 bool nigori_has_explicit_passphrase = | |
240 node.GetNigoriSpecifics().using_explicit_passphrase(); | |
241 std::string bootstrap_token; | |
242 sync_pb::EncryptedData pending_keys; | |
243 pending_keys = cryptographer->GetPendingKeys(); | |
244 bool success = false; | |
245 | |
246 // There are three cases to handle here: | |
247 // 7. We're using the current GAIA password to decrypt the pending keys. This | |
248 // happens when signing in to an account with a previously set implicit | |
249 // passphrase, where the data is already encrypted with the newest GAIA | |
250 // password. | |
251 // 8. The user is providing an old GAIA password to decrypt the pending keys. | |
252 // In this case, the user is using an implicit passphrase, but has changed | |
253 // their password since they last encrypted their data, and therefore | |
254 // their current GAIA password was unable to decrypt the data. This will | |
255 // happen when the user is setting up a new profile with a previously | |
256 // encrypted account (after changing passwords). | |
257 // 9. The user is providing a previously set explicit passphrase to decrypt | |
258 // the pending keys. | |
259 if (!nigori_has_explicit_passphrase) { | |
260 if (cryptographer->is_initialized()) { | |
261 // We only want to change the default encryption key to the pending | |
262 // one if the pending keybag already contains the current default. | |
263 // This covers the case where a different client re-encrypted | |
264 // everything with a newer gaia passphrase (and hence the keybag | |
265 // contains keys from all previously used gaia passphrases). | |
266 // Otherwise, we're in a situation where the pending keys are | |
267 // encrypted with an old gaia passphrase, while the default is the | |
268 // current gaia passphrase. In that case, we preserve the default. | |
269 Cryptographer temp_cryptographer(cryptographer->encryptor()); | |
270 temp_cryptographer.SetPendingKeys(cryptographer->GetPendingKeys()); | |
271 if (temp_cryptographer.DecryptPendingKeys(key_params)) { | |
272 // Check to see if the pending bag of keys contains the current | |
273 // default key. | |
274 sync_pb::EncryptedData encrypted; | |
275 cryptographer->GetKeys(&encrypted); | |
276 if (temp_cryptographer.CanDecrypt(encrypted)) { | |
277 DVLOG(1) << "Implicit user provided passphrase accepted for " | |
278 << "decryption, overwriting default."; | |
279 // Case 7. The pending keybag contains the current default. Go ahead | |
280 // and update the cryptographer, letting the default change. | |
281 cryptographer->DecryptPendingKeys(key_params); | |
282 cryptographer->GetBootstrapToken(&bootstrap_token); | |
283 success = true; | |
284 } else { | |
285 // Case 8. The pending keybag does not contain the current default | |
286 // encryption key. We decrypt the pending keys here, and in | |
287 // FinishSetPassphrase, re-encrypt everything with the current GAIA | |
288 // passphrase instead of the passphrase just provided by the user. | |
289 DVLOG(1) << "Implicit user provided passphrase accepted for " | |
290 << "decryption, restoring implicit internal passphrase " | |
291 << "as default."; | |
292 std::string bootstrap_token_from_current_key; | |
293 cryptographer->GetBootstrapToken( | |
294 &bootstrap_token_from_current_key); | |
295 cryptographer->DecryptPendingKeys(key_params); | |
296 // Overwrite the default from the pending keys. | |
297 cryptographer->AddKeyFromBootstrapToken( | |
298 bootstrap_token_from_current_key); | |
299 success = true; | |
300 } | |
301 } else { // !temp_cryptographer.DecryptPendingKeys(..) | |
302 DVLOG(1) << "Implicit user provided passphrase failed to decrypt."; | |
303 success = false; | |
304 } // temp_cryptographer.DecryptPendingKeys(...) | |
305 } else { // cryptographer->is_initialized() == false | |
306 if (cryptographer->DecryptPendingKeys(key_params)) { | |
307 // This can happpen in two cases: | |
308 // - First time sync on android, where we'll never have a | |
309 // !user_provided passphrase. | |
310 // - This is a restart for a client that lost their bootstrap token. | |
311 // In both cases, we should go ahead and initialize the cryptographer | |
312 // and persist the new bootstrap token. | |
313 // | |
314 // Note: at this point, we cannot distinguish between cases 7 and 8 | |
315 // above. This user provided passphrase could be the current or the | |
316 // old. But, as long as we persist the token, there's nothing more | |
317 // we can do. | |
318 cryptographer->GetBootstrapToken(&bootstrap_token); | |
319 DVLOG(1) << "Implicit user provided passphrase accepted, initializing" | |
320 << " cryptographer."; | |
321 success = true; | |
322 } else { | |
323 DVLOG(1) << "Implicit user provided passphrase failed to decrypt."; | |
324 success = false; | |
325 } | |
326 } // cryptographer->is_initialized() | |
327 } else { // nigori_has_explicit_passphrase == true | |
328 // Case 9. Encryption was done with an explicit passphrase, and we decrypt | |
329 // with the passphrase provided by the user. | |
330 if (cryptographer->DecryptPendingKeys(key_params)) { | |
331 DVLOG(1) << "Explicit passphrase accepted for decryption."; | |
332 cryptographer->GetBootstrapToken(&bootstrap_token); | |
333 success = true; | |
334 } else { | |
335 DVLOG(1) << "Explicit passphrase failed to decrypt."; | |
336 success = false; | |
337 } | |
338 } // nigori_has_explicit_passphrase | |
339 | |
340 DVLOG_IF(1, !success) | |
341 << "Failure in SetDecryptionPassphrase; notifying and returning."; | |
342 DVLOG_IF(1, success) | |
343 << "Successfully set decryption passphrase; updating nigori and " | |
344 "reencrypting."; | |
345 | |
346 FinishSetPassphrase(success, | |
347 bootstrap_token, | |
348 nigori_has_explicit_passphrase, | |
349 &trans, | |
350 &node); | |
351 } | |
352 | |
353 void SyncEncryptionHandlerImpl::EnableEncryptEverything() { | |
354 if (encrypt_everything_) { | |
355 DCHECK(encrypted_types_.Equals(ModelTypeSet::All())); | |
356 return; | |
357 } | |
358 WriteTransaction trans(FROM_HERE, user_share_); | |
359 encrypt_everything_ = true; | |
360 // Change |encrypted_types_| directly to avoid sending more than one | |
361 // notification. | |
362 encrypted_types_ = ModelTypeSet::All(); | |
363 FOR_EACH_OBSERVER( | |
364 Observer, observers_, | |
365 OnEncryptedTypesChanged(encrypted_types_, encrypt_everything_)); | |
366 WriteEncryptionStateToNigori(&trans); | |
367 ReEncryptEverything(&trans); | |
368 } | |
369 | |
370 bool SyncEncryptionHandlerImpl::EncryptEverythingEnabled() const { | |
371 ReadTransaction trans(FROM_HERE, user_share_); | |
372 return encrypt_everything_; | |
373 } | |
374 | |
375 bool SyncEncryptionHandlerImpl::IsUsingExplicitPassphrase() const { | |
376 ReadTransaction trans(FROM_HERE, user_share_); | |
377 return explicit_passphrase_; | |
378 } | |
379 | |
380 // This function iterates over all encrypted types. There are many scenarios in | |
381 // which data for some or all types is not currently available. In that case, | |
382 // the lookup of the root node will fail and we will skip encryption for that | |
383 // type. | |
384 void SyncEncryptionHandlerImpl::ReEncryptEverything( | |
385 WriteTransaction* trans) { | |
386 Cryptographer* cryptographer = trans->GetCryptographer(); | |
387 if (!cryptographer->is_ready()) | |
388 return; | |
389 ModelTypeSet encrypted_types = GetEncryptedTypes(); | |
390 for (ModelTypeSet::Iterator iter = encrypted_types.First(); | |
391 iter.Good(); iter.Inc()) { | |
392 if (iter.Get() == PASSWORDS || iter.Get() == NIGORI) | |
393 continue; // These types handle encryption differently. | |
394 | |
395 ReadNode type_root(trans); | |
396 std::string tag = ModelTypeToRootTag(iter.Get()); | |
397 if (type_root.InitByTagLookup(tag) != BaseNode::INIT_OK) | |
398 continue; // Don't try to reencrypt if the type's data is unavailable. | |
399 | |
400 // Iterate through all children of this datatype. | |
401 std::queue<int64> to_visit; | |
402 int64 child_id = type_root.GetFirstChildId(); | |
403 to_visit.push(child_id); | |
404 while (!to_visit.empty()) { | |
405 child_id = to_visit.front(); | |
406 to_visit.pop(); | |
407 if (child_id == kInvalidId) | |
408 continue; | |
409 | |
410 WriteNode child(trans); | |
411 if (child.InitByIdLookup(child_id) != BaseNode::INIT_OK) { | |
412 NOTREACHED(); | |
413 continue; | |
414 } | |
415 if (child.GetIsFolder()) { | |
416 to_visit.push(child.GetFirstChildId()); | |
417 } | |
418 if (child.GetEntry()->Get(syncable::UNIQUE_SERVER_TAG).empty()) { | |
419 // Rewrite the specifics of the node with encrypted data if necessary | |
420 // (only rewrite the non-unique folders). | |
421 child.ResetFromSpecifics(); | |
422 } | |
423 to_visit.push(child.GetSuccessorId()); | |
424 } | |
425 } | |
426 | |
427 // Passwords are encrypted with their own legacy scheme. Passwords are always | |
428 // encrypted so we don't need to check GetEncryptedTypes() here. | |
429 ReadNode passwords_root(trans); | |
430 std::string passwords_tag = ModelTypeToRootTag(PASSWORDS); | |
431 if (passwords_root.InitByTagLookup(passwords_tag) == | |
432 BaseNode::INIT_OK) { | |
433 int64 child_id = passwords_root.GetFirstChildId(); | |
434 while (child_id != kInvalidId) { | |
435 WriteNode child(trans); | |
436 if (child.InitByIdLookup(child_id) != BaseNode::INIT_OK) { | |
437 NOTREACHED(); | |
438 return; | |
439 } | |
440 child.SetPasswordSpecifics(child.GetPasswordSpecifics()); | |
441 child_id = child.GetSuccessorId(); | |
442 } | |
443 } | |
444 | |
445 // NOTE: We notify from within a transaction. | |
446 FOR_EACH_OBSERVER(SyncEncryptionHandler::Observer, observers_, | |
447 OnEncryptionComplete()); | |
448 } | |
449 | |
450 bool SyncEncryptionHandlerImpl::ApplyNigoriUpdate( | |
451 const sync_pb::NigoriSpecifics& nigori, | |
452 Cryptographer* cryptographer) { | |
453 bool encrypted_types_need_update = !UpdateEncryptedTypesFromNigori(nigori); | |
tim (not reviewing)
2012/08/14 02:32:20
Something feels... weird here. The function on th
Nicolas Zea
2012/08/14 23:24:51
Renamed to nigori_types_need_update (I'd like to k
| |
454 if (nigori.using_explicit_passphrase()) | |
455 explicit_passphrase_ = true; | |
456 | |
457 bool needs_new_keys = false; | |
458 if (!nigori.encrypted().blob().empty()) { | |
459 if (cryptographer->CanDecrypt(nigori.encrypted())) { | |
460 cryptographer->InstallKeys(nigori.encrypted()); | |
461 // We only update the default passphrase if this was a new explicit | |
462 // passphrase. Else, since it was decryptable, it must not have been a new | |
463 // key. | |
464 if (nigori.using_explicit_passphrase()) | |
465 cryptographer->SetDefaultKey(nigori.encrypted().key_name()); | |
466 | |
467 // Check if the cryptographer's keybag is newer than the nigori's | |
468 // keybag. If so, we need to overwrite the nigori node. | |
469 sync_pb::EncryptedData new_keys = nigori.encrypted(); | |
470 if (!cryptographer->GetKeys(&new_keys)) | |
471 NOTREACHED(); | |
472 if (nigori.encrypted().SerializeAsString() != | |
473 new_keys.SerializeAsString()) | |
474 needs_new_keys = true; | |
475 } else { | |
476 cryptographer->SetPendingKeys(nigori.encrypted()); | |
477 } | |
478 } else { | |
479 needs_new_keys = true; | |
480 } | |
481 | |
482 // If we've completed a sync cycle and the cryptographer isn't ready | |
483 // yet, prompt the user for a passphrase. | |
tim (not reviewing)
2012/08/14 02:32:20
"... isn't ready yet or has pending keys, ..."
Nicolas Zea
2012/08/14 23:24:51
Done.
| |
484 if (cryptographer->has_pending_keys()) { | |
485 DVLOG(1) << "OnPassPhraseRequired Sent"; | |
tim (not reviewing)
2012/08/14 02:32:20
nit - Passphrase
Nicolas Zea
2012/08/14 23:24:51
Done.
| |
486 sync_pb::EncryptedData pending_keys = cryptographer->GetPendingKeys(); | |
487 FOR_EACH_OBSERVER(SyncEncryptionHandler::Observer, observers_, | |
488 OnPassphraseRequired(REASON_DECRYPTION, | |
489 pending_keys)); | |
490 } else if (!cryptographer->is_ready()) { | |
491 DVLOG(1) << "OnPassphraseRequired sent because cryptographer is not " | |
492 << "ready"; | |
493 FOR_EACH_OBSERVER(SyncEncryptionHandler::Observer, observers_, | |
494 OnPassphraseRequired(REASON_ENCRYPTION, | |
495 sync_pb::EncryptedData())); | |
496 } | |
497 | |
498 // Check if the current local state is stricter/newer than the nigori state. | |
499 // If so, we need to update the nigori node. | |
tim (not reviewing)
2012/08/14 02:32:20
"... If so, we need to add to the set of encrypted
Nicolas Zea
2012/08/14 23:24:51
Well, it's also the passphrase/keybag info too, no
| |
500 if (nigori.using_explicit_passphrase() != explicit_passphrase_ || | |
501 nigori.encrypt_everything() != encrypt_everything_ || | |
502 encrypted_types_need_update || | |
503 needs_new_keys) | |
tim (not reviewing)
2012/08/14 02:32:20
nit - { } around multi line ifs.
Nicolas Zea
2012/08/14 23:24:51
Done.
| |
504 return false; | |
505 return true; | |
506 } | |
507 | |
508 void SyncEncryptionHandlerImpl::RewriteNigori() { | |
509 WriteTransaction trans(FROM_HERE, user_share_); | |
510 WriteEncryptionStateToNigori(&trans); | |
511 } | |
512 | |
513 void SyncEncryptionHandlerImpl::WriteEncryptionStateToNigori( | |
514 WriteTransaction* trans) { | |
515 WriteNode nigori_node(trans); | |
516 // This can happen in tests that don't have nigori nodes. | |
517 if (!nigori_node.InitByTagLookup(kNigoriTag) == BaseNode::INIT_OK) | |
518 return; | |
519 sync_pb::NigoriSpecifics nigori = nigori_node.GetNigoriSpecifics(); | |
520 Cryptographer* cryptographer = trans->GetCryptographer(); | |
521 if (cryptographer->is_ready() && | |
522 nigori_overwrite_count_ < kNigoriOverwriteLimit) { | |
523 // Does not modify the encrypted blob if the unencrypted data already | |
524 // matches what is about to be written. | |
525 sync_pb::EncryptedData original_keys = nigori.encrypted(); | |
526 if (!cryptographer->GetKeys(nigori.mutable_encrypted())) | |
527 NOTREACHED(); | |
528 | |
529 if (nigori.encrypted().SerializeAsString() != | |
530 original_keys.SerializeAsString()) { | |
531 // We've updated the nigori node's encryption keys. In order to prevent | |
532 // a possible looping of two clients constantly overwriting each other, | |
533 // we limit the absolute number of overwrites per client instantiation. | |
534 nigori_overwrite_count_++; | |
535 UMA_HISTOGRAM_COUNTS("Sync.AutoNigoriOverwrites", | |
536 nigori_overwrite_count_); | |
537 } | |
538 | |
539 // Note: we don't try to set using_explicit_passphrase here since if that | |
540 // is lost the user can always set it again. The main point is to preserve | |
541 // the encryption keys so all data remains decryptable. | |
542 } | |
543 UpdateNigoriFromEncryptedTypes(&nigori); | |
tim (not reviewing)
2012/08/14 02:32:20
Would it be bad to just call the util directly her
Nicolas Zea
2012/08/14 23:24:51
Need to keep it in nigori_util so the fake can use
| |
544 | |
545 // If nothing has changed, this is a no-op. | |
546 nigori_node.SetNigoriSpecifics(nigori); | |
547 } | |
548 | |
549 bool SyncEncryptionHandlerImpl::UpdateEncryptedTypesFromNigori( | |
550 const sync_pb::NigoriSpecifics& nigori) { | |
551 if (nigori.encrypt_everything()) { | |
552 if (!encrypt_everything_) { | |
553 encrypt_everything_ = true; | |
554 encrypted_types_ = ModelTypeSet::All(); | |
555 FOR_EACH_OBSERVER( | |
556 Observer, observers_, | |
557 OnEncryptedTypesChanged(encrypted_types_, encrypt_everything_)); | |
558 } | |
559 DCHECK(encrypted_types_.Equals(ModelTypeSet::All())); | |
560 return true; | |
561 } | |
562 | |
563 ModelTypeSet encrypted_types(SensitiveTypes()); | |
564 if (nigori.encrypt_bookmarks()) | |
565 encrypted_types.Put(BOOKMARKS); | |
566 if (nigori.encrypt_preferences()) | |
567 encrypted_types.Put(PREFERENCES); | |
568 if (nigori.encrypt_autofill_profile()) | |
569 encrypted_types.Put(AUTOFILL_PROFILE); | |
570 if (nigori.encrypt_autofill()) | |
571 encrypted_types.Put(AUTOFILL); | |
572 if (nigori.encrypt_themes()) | |
573 encrypted_types.Put(THEMES); | |
574 if (nigori.encrypt_typed_urls()) | |
575 encrypted_types.Put(TYPED_URLS); | |
576 if (nigori.encrypt_extension_settings()) | |
577 encrypted_types.Put(EXTENSION_SETTINGS); | |
578 if (nigori.encrypt_extensions()) | |
579 encrypted_types.Put(EXTENSIONS); | |
580 if (nigori.encrypt_search_engines()) | |
581 encrypted_types.Put(SEARCH_ENGINES); | |
582 if (nigori.encrypt_sessions()) | |
583 encrypted_types.Put(SESSIONS); | |
584 if (nigori.encrypt_app_settings()) | |
585 encrypted_types.Put(APP_SETTINGS); | |
586 if (nigori.encrypt_apps()) | |
587 encrypted_types.Put(APPS); | |
588 if (nigori.encrypt_app_notifications()) | |
589 encrypted_types.Put(APP_NOTIFICATIONS); | |
tim (not reviewing)
2012/08/14 02:32:20
Should there be a compile-assert here on number of
Nicolas Zea
2012/08/14 23:24:51
Done.
| |
590 | |
591 // Note: the initial version with encryption did not support the | |
592 // encrypt_everything field. If anything more than the sensitive types were | |
tim (not reviewing)
2012/08/14 02:32:20
What version was this, again? At some point I thi
Nicolas Zea
2012/08/14 23:24:51
That was m14 or m15 I believe. That said, since we
| |
593 // encrypted, it meant we were encrypting everything. | |
594 if (!nigori.has_encrypt_everything() && | |
595 !Difference(encrypted_types, SensitiveTypes()).Empty()) { | |
596 if (!encrypt_everything_) { | |
597 encrypt_everything_ = true; | |
598 encrypted_types_ = ModelTypeSet::All(); | |
599 FOR_EACH_OBSERVER( | |
600 Observer, observers_, | |
601 OnEncryptedTypesChanged(encrypted_types_, encrypt_everything_)); | |
602 } | |
603 DCHECK(encrypted_types_.Equals(ModelTypeSet::All())); | |
604 return false; | |
605 } | |
606 | |
607 MergeEncryptedTypes(encrypted_types); | |
608 if (!encrypted_types_.Equals(encrypted_types)) | |
609 return false; | |
tim (not reviewing)
2012/08/14 02:32:20
these 3 lines are equivalent to "return encrypted_
tim (not reviewing)
2012/08/14 02:34:13
Ignore the second part of this comment, I get it n
Nicolas Zea
2012/08/14 23:24:51
Done.
| |
610 return true; | |
611 } | |
612 | |
613 void SyncEncryptionHandlerImpl::UpdateNigoriFromEncryptedTypes( | |
614 sync_pb::NigoriSpecifics* nigori) const { | |
615 syncable::UpdateNigoriFromEncryptedTypes(encrypted_types_, | |
616 encrypt_everything_, | |
617 nigori); | |
618 } | |
619 | |
620 void SyncEncryptionHandlerImpl::FinishSetPassphrase( | |
621 bool success, | |
622 const std::string& bootstrap_token, | |
623 bool is_explicit, | |
624 WriteTransaction* trans, | |
625 WriteNode* nigori_node) { | |
626 Cryptographer* cryptographer = trans->GetCryptographer(); | |
627 FOR_EACH_OBSERVER(SyncEncryptionHandler::Observer, observers_, | |
628 OnCryptographerStateChanged(cryptographer)); | |
629 | |
630 // It's possible we need to change the bootstrap token even if we failed to | |
631 // set the passphrase (for example if we need to preserve the new GAIA | |
632 // passphrase). | |
633 if (!bootstrap_token.empty()) { | |
634 DVLOG(1) << "Bootstrap token updated."; | |
635 FOR_EACH_OBSERVER(SyncEncryptionHandler::Observer, observers_, | |
636 OnBootstrapTokenUpdated(bootstrap_token)); | |
637 } | |
638 | |
639 if (!success) { | |
640 if (cryptographer->is_ready()) { | |
641 LOG(ERROR) << "Attempt to change passphrase failed while cryptographer " | |
642 << "was ready."; | |
643 } else if (cryptographer->has_pending_keys()) { | |
644 FOR_EACH_OBSERVER(SyncEncryptionHandler::Observer, observers_, | |
645 OnPassphraseRequired(REASON_DECRYPTION, | |
646 cryptographer->GetPendingKeys())); | |
647 } else { | |
648 FOR_EACH_OBSERVER(SyncEncryptionHandler::Observer, observers_, | |
649 OnPassphraseRequired(REASON_ENCRYPTION, | |
650 sync_pb::EncryptedData())); | |
651 } | |
652 return; | |
653 } | |
654 | |
655 FOR_EACH_OBSERVER(SyncEncryptionHandler::Observer, observers_, | |
656 OnPassphraseAccepted()); | |
657 DCHECK(cryptographer->is_ready()); | |
658 | |
659 sync_pb::NigoriSpecifics specifics(nigori_node->GetNigoriSpecifics()); | |
660 // Does not modify specifics.encrypted() if the original decrypted data was | |
661 // the same. | |
662 if (!cryptographer->GetKeys(specifics.mutable_encrypted())) { | |
663 NOTREACHED(); | |
664 return; | |
665 } | |
666 explicit_passphrase_ = is_explicit; | |
667 specifics.set_using_explicit_passphrase(is_explicit); | |
668 nigori_node->SetNigoriSpecifics(specifics); | |
669 | |
670 // Does nothing if everything is already encrypted or the cryptographer has | |
671 // pending keys. | |
672 ReEncryptEverything(trans); | |
673 } | |
674 | |
675 void SyncEncryptionHandlerImpl::MergeEncryptedTypes( | |
tim (not reviewing)
2012/08/14 02:32:20
I think in this case it'd be better to have
if (
Nicolas Zea
2012/08/14 23:24:51
Done.
| |
676 ModelTypeSet encrypted_types) { | |
677 if (encrypted_types_.HasAll(encrypted_types)) | |
678 return; | |
679 encrypted_types_ = encrypted_types; | |
680 FOR_EACH_OBSERVER( | |
681 Observer, observers_, | |
682 OnEncryptedTypesChanged(encrypted_types_, encrypt_everything_)); | |
683 } | |
684 | |
685 } // namespace browser_sync | |
OLD | NEW |