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 <algorithm> | 5 #include <algorithm> |
6 | 6 |
7 #include "base/base64.h" | 7 #include "base/base64.h" |
8 #include "chrome/browser/sync/util/cryptographer.h" | 8 #include "chrome/browser/sync/util/cryptographer.h" |
9 #include "chrome/browser/password_manager/encryptor.h" | 9 #include "chrome/browser/password_manager/encryptor.h" |
10 | 10 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
50 | 50 |
51 bool Cryptographer::CanDecrypt(const sync_pb::EncryptedData& data) const { | 51 bool Cryptographer::CanDecrypt(const sync_pb::EncryptedData& data) const { |
52 return nigoris_.end() != nigoris_.find(data.key_name()); | 52 return nigoris_.end() != nigoris_.find(data.key_name()); |
53 } | 53 } |
54 | 54 |
55 bool Cryptographer::CanDecryptUsingDefaultKey( | 55 bool Cryptographer::CanDecryptUsingDefaultKey( |
56 const sync_pb::EncryptedData& data) const { | 56 const sync_pb::EncryptedData& data) const { |
57 return default_nigori_ && (data.key_name() == default_nigori_->first); | 57 return default_nigori_ && (data.key_name() == default_nigori_->first); |
58 } | 58 } |
59 | 59 |
60 bool Cryptographer::Encrypt(const ::google::protobuf::MessageLite& message, | 60 bool Cryptographer::Encrypt( |
61 sync_pb::EncryptedData* encrypted) const { | 61 const ::google::protobuf::MessageLite& message, |
62 if (!encrypted || !default_nigori_) { | 62 sync_pb::EncryptedData* encrypted) const { |
63 DCHECK(encrypted); | |
64 if (!default_nigori_) { | |
63 LOG(ERROR) << "Cryptographer not ready, failed to encrypt."; | 65 LOG(ERROR) << "Cryptographer not ready, failed to encrypt."; |
64 return false; | 66 return false; |
65 } | 67 } |
66 | 68 |
67 std::string serialized; | 69 std::string serialized; |
68 if (!message.SerializeToString(&serialized)) { | 70 if (!message.SerializeToString(&serialized)) { |
69 LOG(ERROR) << "Message is invalid/missing a required field."; | 71 LOG(ERROR) << "Message is invalid/missing a required field."; |
70 return false; | 72 return false; |
71 } | 73 } |
72 | 74 |
75 if (CanDecryptUsingDefaultKey(*encrypted)) { | |
76 const std::string& original_serialized = DecryptToString(*encrypted); | |
77 if (original_serialized == serialized) { | |
78 DVLOG(2) << "Re-encryption unnecessary, encrypted data already matches."; | |
79 return true; | |
80 } | |
81 } | |
82 | |
83 return EncryptImpl(serialized, default_nigori_, encrypted); | |
akalin
2011/12/12 22:05:22
do we still need the Encrypt/EncryptImpl divide?
Nicolas Zea
2011/12/13 00:43:30
Done.
| |
84 } | |
85 | |
86 bool Cryptographer::EncryptImpl(const std::string& serialized, | |
87 const NigoriMap::value_type* nigori, | |
88 sync_pb::EncryptedData* encrypted) const { | |
73 encrypted->set_key_name(default_nigori_->first); | 89 encrypted->set_key_name(default_nigori_->first); |
74 if (!default_nigori_->second->Encrypt(serialized, | 90 if (!nigori->second->Encrypt(serialized, |
75 encrypted->mutable_blob())) { | 91 encrypted->mutable_blob())) { |
76 LOG(ERROR) << "Failed to encrypt data."; | 92 LOG(ERROR) << "Failed to encrypt data."; |
77 return false; | 93 return false; |
78 } | 94 } |
79 return true; | 95 return true; |
80 } | 96 } |
81 | 97 |
82 bool Cryptographer::Decrypt(const sync_pb::EncryptedData& encrypted, | 98 bool Cryptographer::Decrypt(const sync_pb::EncryptedData& encrypted, |
83 ::google::protobuf::MessageLite* message) const { | 99 ::google::protobuf::MessageLite* message) const { |
84 DCHECK(message); | 100 DCHECK(message); |
85 std::string plaintext = DecryptToString(encrypted); | 101 std::string plaintext = DecryptToString(encrypted); |
(...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
414 continue; | 430 continue; |
415 } | 431 } |
416 nigoris_[key.name()] = make_linked_ptr(new_nigori.release()); | 432 nigoris_[key.name()] = make_linked_ptr(new_nigori.release()); |
417 } | 433 } |
418 } | 434 } |
419 DCHECK(nigoris_.end() != nigoris_.find(default_key_name)); | 435 DCHECK(nigoris_.end() != nigoris_.find(default_key_name)); |
420 default_nigori_ = &*nigoris_.find(default_key_name); | 436 default_nigori_ = &*nigoris_.find(default_key_name); |
421 } | 437 } |
422 | 438 |
423 } // namespace browser_sync | 439 } // namespace browser_sync |
OLD | NEW |