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). |
| 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 observers_.AddObserver(observer); |
| 50 } |
| 51 |
| 52 void SyncEncryptionHandlerImpl::RemoveObserver(Observer* observer) { |
| 53 observers_.RemoveObserver(observer); |
| 54 } |
| 55 |
| 56 void SyncEncryptionHandlerImpl::ReloadNigori() { |
| 57 WriteTransaction trans(FROM_HERE, user_share_); |
| 58 WriteNode node(&trans); |
| 59 Cryptographer* cryptographer = trans.GetCryptographer(); |
| 60 cryptographer_ = cryptographer; |
| 61 |
| 62 if (node.InitByTagLookup(kNigoriTag) != BaseNode::INIT_OK) |
| 63 return; |
| 64 if (!ApplyNigoriUpdate(node.GetNigoriSpecifics(), cryptographer)) |
| 65 WriteEncryptionStateToNigori(&trans); |
| 66 |
| 67 FOR_EACH_OBSERVER(SyncEncryptionHandler::Observer, observers_, |
| 68 OnCryptographerStateChanged(cryptographer)); |
| 69 |
| 70 if (cryptographer->is_ready()) |
| 71 ReEncryptEverything(&trans); |
| 72 } |
| 73 |
| 74 // Note: this is called from within a syncable transaction, so we need to post |
| 75 // tasks if we want to do any work that creates a new sync_api transaction. |
| 76 void SyncEncryptionHandlerImpl::UpdateFromNigori( |
| 77 const sync_pb::NigoriSpecifics& nigori) { |
| 78 if (!ApplyNigoriUpdate(nigori, cryptographer_)) { |
| 79 MessageLoop::current()->PostTask( |
| 80 FROM_HERE, |
| 81 base::Bind(&SyncEncryptionHandlerImpl::RewriteNigori, |
| 82 weak_ptr_factory_.GetWeakPtr())); |
| 83 } |
| 84 |
| 85 FOR_EACH_OBSERVER(SyncEncryptionHandler::Observer, observers_, |
| 86 OnCryptographerStateChanged(cryptographer_)); |
| 87 } |
| 88 |
| 89 // Note: this is always called via the Cryptographer interface right now, |
| 90 // so a transaction is already held. Once we remove that interface, we'll |
| 91 // need to enforce holding a transaction when calling this method. |
| 92 ModelTypeSet SyncEncryptionHandlerImpl::GetEncryptedTypes() const { |
| 93 return encrypted_types_; |
| 94 } |
| 95 |
| 96 void SyncEncryptionHandlerImpl::SetEncryptionPassphrase( |
| 97 const std::string& passphrase, |
| 98 bool is_explicit) { |
| 99 // We do not accept empty passphrases. |
| 100 if (passphrase.empty()) { |
| 101 NOTREACHED() << "Cannot encrypt with an empty passphrase."; |
| 102 return; |
| 103 } |
| 104 |
| 105 // All accesses to the cryptographer are protected by a transaction. |
| 106 WriteTransaction trans(FROM_HERE, user_share_); |
| 107 Cryptographer* cryptographer = trans.GetCryptographer(); |
| 108 KeyParams key_params = {"localhost", "dummy", passphrase}; |
| 109 WriteNode node(&trans); |
| 110 if (node.InitByTagLookup(kNigoriTag) != BaseNode::INIT_OK) { |
| 111 // TODO(albertb): Plumb an UnrecoverableError all the way back to the PSS. |
| 112 NOTREACHED(); |
| 113 return; |
| 114 } |
| 115 |
| 116 bool nigori_has_explicit_passphrase = |
| 117 node.GetNigoriSpecifics().using_explicit_passphrase(); |
| 118 std::string bootstrap_token; |
| 119 sync_pb::EncryptedData pending_keys; |
| 120 if (cryptographer->has_pending_keys()) |
| 121 pending_keys = cryptographer->GetPendingKeys(); |
| 122 bool success = false; |
| 123 |
| 124 |
| 125 // There are six cases to handle here: |
| 126 // 1. The user has no pending keys and is setting their current GAIA password |
| 127 // as the encryption passphrase. This happens either during first time sync |
| 128 // with a clean profile, or after re-authenticating on a profile that was |
| 129 // already signed in with the cryptographer ready. |
| 130 // 2. The user has no pending keys, and is overwriting an (already provided) |
| 131 // implicit passphrase with an explicit (custom) passphrase. |
| 132 // 3. The user has pending keys for an explicit passphrase that is somehow set |
| 133 // to their current GAIA passphrase. |
| 134 // 4. The user has pending keys encrypted with their current GAIA passphrase |
| 135 // and the caller passes in the current GAIA passphrase. |
| 136 // 5. The user has pending keys encrypted with an older GAIA passphrase |
| 137 // and the caller passes in the current GAIA passphrase. |
| 138 // 6. The user has previously done encryption with an explicit passphrase. |
| 139 // Furthermore, we enforce the fact that the bootstrap encryption token will |
| 140 // always be derived from the newest GAIA password if the account is using |
| 141 // an implicit passphrase (even if the data is encrypted with an old GAIA |
| 142 // password). If the account is using an explicit (custom) passphrase, the |
| 143 // bootstrap token will be derived from the most recently provided explicit |
| 144 // passphrase (that was able to decrypt the data). |
| 145 if (!nigori_has_explicit_passphrase) { |
| 146 if (!cryptographer->has_pending_keys()) { |
| 147 if (cryptographer->AddKey(key_params)) { |
| 148 // Case 1 and 2. We set a new GAIA passphrase when there are no pending |
| 149 // keys (1), or overwriting an implicit passphrase with a new explicit |
| 150 // one (2) when there are no pending keys. |
| 151 DVLOG(1) << "Setting " << (is_explicit ? "explicit" : "implicit" ) |
| 152 << " passphrase for encryption."; |
| 153 cryptographer->GetBootstrapToken(&bootstrap_token); |
| 154 success = true; |
| 155 } else { |
| 156 NOTREACHED() << "Failed to add key to cryptographer."; |
| 157 success = false; |
| 158 } |
| 159 } else { // cryptographer->has_pending_keys() == true |
| 160 if (is_explicit) { |
| 161 // This can only happen if the nigori node is updated with a new |
| 162 // implicit passphrase while a client is attempting to set a new custom |
| 163 // passphrase (race condition). |
| 164 DVLOG(1) << "Failing because an implicit passphrase is already set."; |
| 165 success = false; |
| 166 } else { // is_explicit == false |
| 167 if (cryptographer->DecryptPendingKeys(key_params)) { |
| 168 // Case 4. We successfully decrypted with the implicit GAIA passphrase |
| 169 // passed in. |
| 170 DVLOG(1) << "Implicit internal passphrase accepted for decryption."; |
| 171 cryptographer->GetBootstrapToken(&bootstrap_token); |
| 172 success = true; |
| 173 } else { |
| 174 // Case 5. Encryption was done with an old GAIA password, but we were |
| 175 // provided with the current GAIA password. We need to generate a new |
| 176 // bootstrap token to preserve it. We build a temporary cryptographer |
| 177 // to allow us to extract these params without polluting our current |
| 178 // cryptographer. |
| 179 DVLOG(1) << "Implicit internal passphrase failed to decrypt, adding " |
| 180 << "anyways as default passphrase and persisting via " |
| 181 << "bootstrap token."; |
| 182 Cryptographer temp_cryptographer(cryptographer->encryptor()); |
| 183 temp_cryptographer.AddKey(key_params); |
| 184 temp_cryptographer.GetBootstrapToken(&bootstrap_token); |
| 185 // We then set the new passphrase as the default passphrase of the |
| 186 // real cryptographer, even though we have pending keys. This is safe, |
| 187 // as although Cryptographer::is_initialized() will now be true, |
| 188 // is_ready() will remain false due to having pending keys. |
| 189 cryptographer->AddKey(key_params); |
| 190 success = false; |
| 191 } |
| 192 } // is_explicit |
| 193 } // cryptographer->has_pending_keys() |
| 194 } else { // nigori_has_explicit_passphrase == true |
| 195 // Case 6. We do not want to override a previously set explicit passphrase, |
| 196 // so we return a failure. |
| 197 DVLOG(1) << "Failing because an explicit passphrase is already set."; |
| 198 success = false; |
| 199 } |
| 200 |
| 201 DVLOG_IF(1, !success) |
| 202 << "Failure in SetEncryptionPassphrase; notifying and returning."; |
| 203 DVLOG_IF(1, success) |
| 204 << "Successfully set encryption passphrase; updating nigori and " |
| 205 "reencrypting."; |
| 206 |
| 207 FinishSetPassphrase( |
| 208 success, bootstrap_token, is_explicit, &trans, &node); |
| 209 } |
| 210 |
| 211 void SyncEncryptionHandlerImpl::SetDecryptionPassphrase( |
| 212 const std::string& passphrase) { |
| 213 // We do not accept empty passphrases. |
| 214 if (passphrase.empty()) { |
| 215 NOTREACHED() << "Cannot decrypt with an empty passphrase."; |
| 216 return; |
| 217 } |
| 218 |
| 219 // All accesses to the cryptographer are protected by a transaction. |
| 220 WriteTransaction trans(FROM_HERE, user_share_); |
| 221 Cryptographer* cryptographer = trans.GetCryptographer(); |
| 222 KeyParams key_params = {"localhost", "dummy", passphrase}; |
| 223 WriteNode node(&trans); |
| 224 if (node.InitByTagLookup(kNigoriTag) != BaseNode::INIT_OK) { |
| 225 // TODO(albertb): Plumb an UnrecoverableError all the way back to the PSS. |
| 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); |
| 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. |
| 484 if (cryptographer->has_pending_keys()) { |
| 485 DVLOG(1) << "OnPassPhraseRequired Sent"; |
| 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. |
| 500 if (nigori.using_explicit_passphrase() != explicit_passphrase_ || |
| 501 nigori.encrypt_everything() != encrypt_everything_ || |
| 502 encrypted_types_need_update || |
| 503 needs_new_keys) |
| 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); |
| 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); |
| 590 |
| 591 // Note: the initial version with encryption did not support the |
| 592 // encrypt_everything field. If anything more than the sensitive types were |
| 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; |
| 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( |
| 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 |