| Index: sync/util/cryptographer.cc
|
| diff --git a/sync/util/cryptographer.cc b/sync/util/cryptographer.cc
|
| index 202480dd112c5c3c5650f64a4afdadd5ef86ecf6..ab66b68bc0837e5df6a19eacb9478ed386372c7a 100644
|
| --- a/sync/util/cryptographer.cc
|
| +++ b/sync/util/cryptographer.cc
|
| @@ -7,6 +7,7 @@
|
| #include <algorithm>
|
|
|
| #include "base/base64.h"
|
| +#include "base/basictypes.h"
|
| #include "base/logging.h"
|
| #include "sync/protocol/nigori_specifics.pb.h"
|
| #include "sync/util/encryptor.h"
|
| @@ -37,7 +38,7 @@ void Cryptographer::Bootstrap(const std::string& restored_bootstrap_token) {
|
|
|
| scoped_ptr<Nigori> nigori(UnpackBootstrapToken(restored_bootstrap_token));
|
| if (nigori.get())
|
| - AddKeyImpl(nigori.Pass());
|
| + AddKeyImpl(nigori.Pass(), true);
|
| }
|
|
|
| bool Cryptographer::CanDecrypt(const sync_pb::EncryptedData& data) const {
|
| @@ -58,12 +59,6 @@ bool Cryptographer::Encrypt(
|
| LOG(ERROR) << "Cryptographer not ready, failed to encrypt.";
|
| return false;
|
| }
|
| - NigoriMap::const_iterator default_nigori =
|
| - nigoris_.find(default_nigori_name_);
|
| - if (default_nigori == nigoris_.end()) {
|
| - LOG(ERROR) << "Corrupt default key.";
|
| - return false;
|
| - }
|
|
|
| std::string serialized;
|
| if (!message.SerializeToString(&serialized)) {
|
| @@ -71,6 +66,12 @@ bool Cryptographer::Encrypt(
|
| return false;
|
| }
|
|
|
| + return EncryptString(serialized, encrypted);
|
| +}
|
| +
|
| +bool Cryptographer::EncryptString(
|
| + const std::string& serialized,
|
| + sync_pb::EncryptedData* encrypted) const {
|
| if (CanDecryptUsingDefaultKey(*encrypted)) {
|
| const std::string& original_serialized = DecryptToString(*encrypted);
|
| if (original_serialized == serialized) {
|
| @@ -79,6 +80,13 @@ bool Cryptographer::Encrypt(
|
| }
|
| }
|
|
|
| + NigoriMap::const_iterator default_nigori =
|
| + nigoris_.find(default_nigori_name_);
|
| + if (default_nigori == nigoris_.end()) {
|
| + LOG(ERROR) << "Corrupt default key.";
|
| + return false;
|
| + }
|
| +
|
| encrypted->set_key_name(default_nigori_name_);
|
| if (!default_nigori->second->Encrypt(serialized,
|
| encrypted->mutable_blob())) {
|
| @@ -140,7 +148,20 @@ bool Cryptographer::AddKey(const KeyParams& params) {
|
| NOTREACHED(); // Invalid username or password.
|
| return false;
|
| }
|
| - return AddKeyImpl(nigori.Pass());
|
| + return AddKeyImpl(nigori.Pass(), true);
|
| +}
|
| +
|
| +bool Cryptographer::AddNonDefaultKey(const KeyParams& params) {
|
| + DCHECK(is_initialized());
|
| + // Create the new Nigori and add it to the keybag.
|
| + scoped_ptr<Nigori> nigori(new Nigori);
|
| + if (!nigori->InitByDerivation(params.hostname,
|
| + params.username,
|
| + params.password)) {
|
| + NOTREACHED(); // Invalid username or password.
|
| + return false;
|
| + }
|
| + return AddKeyImpl(nigori.Pass(), false);
|
| }
|
|
|
| bool Cryptographer::AddKeyFromBootstrapToken(
|
| @@ -149,17 +170,31 @@ bool Cryptographer::AddKeyFromBootstrapToken(
|
| scoped_ptr<Nigori> nigori(UnpackBootstrapToken(restored_bootstrap_token));
|
| if (!nigori.get())
|
| return false;
|
| - return AddKeyImpl(nigori.Pass());
|
| + return AddKeyImpl(nigori.Pass(), true);
|
| }
|
|
|
| -bool Cryptographer::AddKeyImpl(scoped_ptr<Nigori> initialized_nigori) {
|
| +bool Cryptographer::AddKeyImpl(scoped_ptr<Nigori> initialized_nigori,
|
| + bool set_as_default) {
|
| std::string name;
|
| if (!initialized_nigori->Permute(Nigori::Password, kNigoriKeyName, &name)) {
|
| NOTREACHED();
|
| return false;
|
| }
|
| +
|
| nigoris_[name] = make_linked_ptr(initialized_nigori.release());
|
| - default_nigori_name_ = name;
|
| +
|
| + // Check if the key we just added can decrypt the pending keys and add them
|
| + // too if so.
|
| + if (pending_keys_.get() && CanDecrypt(*pending_keys_)) {
|
| + sync_pb::NigoriKeyBag pending_bag;
|
| + Decrypt(*pending_keys_, &pending_bag);
|
| + InstallKeyBag(pending_bag);
|
| + SetDefaultKey(pending_keys_->key_name());
|
| + pending_keys_.reset();
|
| + }
|
| +
|
| + // The just-added key takes priority over the pending keys as default.
|
| + if (set_as_default) SetDefaultKey(name);
|
| return true;
|
| }
|
|
|
| @@ -307,4 +342,25 @@ void Cryptographer::InstallKeyBag(const sync_pb::NigoriKeyBag& bag) {
|
| }
|
| }
|
|
|
| +bool Cryptographer::KeybagIsStale(
|
| + const sync_pb::EncryptedData& encrypted_bag) const {
|
| + if (!is_ready())
|
| + return false;
|
| + if (encrypted_bag.blob().empty())
|
| + return true;
|
| + if (!CanDecrypt(encrypted_bag))
|
| + return false;
|
| + if (!CanDecryptUsingDefaultKey(encrypted_bag))
|
| + return true;
|
| + sync_pb::NigoriKeyBag bag;
|
| + if (!Decrypt(encrypted_bag, &bag)) {
|
| + LOG(ERROR) << "Failed to decrypt keybag for stale check. "
|
| + << "Assuming keybag is corrupted.";
|
| + return true;
|
| + }
|
| + if (static_cast<size_t>(bag.key_size()) < nigoris_.size())
|
| + return true;
|
| + return false;
|
| +}
|
| +
|
| } // namespace syncer
|
|
|