| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #include "base/base64.h" | 5 #include "base/base64.h" |
| 6 #include "chrome/browser/sync/util/cryptographer.h" | 6 #include "chrome/browser/sync/util/cryptographer.h" |
| 7 #include "chrome/browser/password_manager/encryptor.h" | 7 #include "chrome/browser/password_manager/encryptor.h" |
| 8 | 8 |
| 9 namespace browser_sync { | 9 namespace browser_sync { |
| 10 | 10 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 return nigoris_.end() != nigoris_.find(data.key_name()); | 37 return nigoris_.end() != nigoris_.find(data.key_name()); |
| 38 } | 38 } |
| 39 | 39 |
| 40 bool Cryptographer::CanDecryptUsingDefaultKey( | 40 bool Cryptographer::CanDecryptUsingDefaultKey( |
| 41 const sync_pb::EncryptedData& data) const { | 41 const sync_pb::EncryptedData& data) const { |
| 42 return default_nigori_ && (data.key_name() == default_nigori_->first); | 42 return default_nigori_ && (data.key_name() == default_nigori_->first); |
| 43 } | 43 } |
| 44 | 44 |
| 45 bool Cryptographer::Encrypt(const ::google::protobuf::MessageLite& message, | 45 bool Cryptographer::Encrypt(const ::google::protobuf::MessageLite& message, |
| 46 sync_pb::EncryptedData* encrypted) const { | 46 sync_pb::EncryptedData* encrypted) const { |
| 47 DCHECK(encrypted); | 47 if (!encrypted || !default_nigori_) { |
| 48 DCHECK(default_nigori_); | 48 LOG(ERROR) << "Cryptographer not ready, failed to encrypt."; |
| 49 return false; |
| 50 } |
| 49 | 51 |
| 50 std::string serialized; | 52 std::string serialized; |
| 51 if (!message.SerializeToString(&serialized)) { | 53 if (!message.SerializeToString(&serialized)) { |
| 52 NOTREACHED(); // |message| is invalid/missing a required field. | 54 LOG(ERROR) << "Message is invalid/missing a required field."; |
| 53 return false; | 55 return false; |
| 54 } | 56 } |
| 55 | 57 |
| 56 encrypted->set_key_name(default_nigori_->first); | 58 encrypted->set_key_name(default_nigori_->first); |
| 57 if (!default_nigori_->second->Encrypt(serialized, | 59 if (!default_nigori_->second->Encrypt(serialized, |
| 58 encrypted->mutable_blob())) { | 60 encrypted->mutable_blob())) { |
| 59 NOTREACHED(); // Encrypt should not fail. | 61 LOG(ERROR) << "Failed to encrypt data."; |
| 60 return false; | 62 return false; |
| 61 } | 63 } |
| 62 return true; | 64 return true; |
| 63 } | 65 } |
| 64 | 66 |
| 65 bool Cryptographer::Decrypt(const sync_pb::EncryptedData& encrypted, | 67 bool Cryptographer::Decrypt(const sync_pb::EncryptedData& encrypted, |
| 66 ::google::protobuf::MessageLite* message) const { | 68 ::google::protobuf::MessageLite* message) const { |
| 67 DCHECK(message); | 69 DCHECK(message); |
| 68 std::string plaintext = DecryptToString(encrypted); | 70 std::string plaintext = DecryptToString(encrypted); |
| 69 return message->ParseFromString(plaintext); | 71 return message->ParseFromString(plaintext); |
| (...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 299 continue; | 301 continue; |
| 300 } | 302 } |
| 301 nigoris_[key.name()] = make_linked_ptr(new_nigori.release()); | 303 nigoris_[key.name()] = make_linked_ptr(new_nigori.release()); |
| 302 } | 304 } |
| 303 } | 305 } |
| 304 DCHECK(nigoris_.end() != nigoris_.find(default_key_name)); | 306 DCHECK(nigoris_.end() != nigoris_.find(default_key_name)); |
| 305 default_nigori_ = &*nigoris_.find(default_key_name); | 307 default_nigori_ = &*nigoris_.find(default_key_name); |
| 306 } | 308 } |
| 307 | 309 |
| 308 } // namespace browser_sync | 310 } // namespace browser_sync |
| OLD | NEW |