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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 | 48 |
49 bool Cryptographer::CanDecrypt(const sync_pb::EncryptedData& data) const { | 49 bool Cryptographer::CanDecrypt(const sync_pb::EncryptedData& data) const { |
50 return nigoris_.end() != nigoris_.find(data.key_name()); | 50 return nigoris_.end() != nigoris_.find(data.key_name()); |
51 } | 51 } |
52 | 52 |
53 bool Cryptographer::CanDecryptUsingDefaultKey( | 53 bool Cryptographer::CanDecryptUsingDefaultKey( |
54 const sync_pb::EncryptedData& data) const { | 54 const sync_pb::EncryptedData& data) const { |
55 return default_nigori_ && (data.key_name() == default_nigori_->first); | 55 return default_nigori_ && (data.key_name() == default_nigori_->first); |
56 } | 56 } |
57 | 57 |
58 bool Cryptographer::Encrypt(const ::google::protobuf::MessageLite& message, | 58 bool Cryptographer::Encrypt( |
59 sync_pb::EncryptedData* encrypted) const { | 59 const ::google::protobuf::MessageLite& message, |
60 if (!encrypted || !default_nigori_) { | 60 sync_pb::EncryptedData* encrypted) const { |
| 61 DCHECK(encrypted); |
| 62 if (!default_nigori_) { |
61 LOG(ERROR) << "Cryptographer not ready, failed to encrypt."; | 63 LOG(ERROR) << "Cryptographer not ready, failed to encrypt."; |
62 return false; | 64 return false; |
63 } | 65 } |
64 | 66 |
65 std::string serialized; | 67 std::string serialized; |
66 if (!message.SerializeToString(&serialized)) { | 68 if (!message.SerializeToString(&serialized)) { |
67 LOG(ERROR) << "Message is invalid/missing a required field."; | 69 LOG(ERROR) << "Message is invalid/missing a required field."; |
68 return false; | 70 return false; |
69 } | 71 } |
70 | 72 |
| 73 if (CanDecryptUsingDefaultKey(*encrypted)) { |
| 74 const std::string& original_serialized = DecryptToString(*encrypted); |
| 75 if (original_serialized == serialized) { |
| 76 DVLOG(2) << "Re-encryption unnecessary, encrypted data already matches."; |
| 77 return true; |
| 78 } |
| 79 } |
| 80 |
71 encrypted->set_key_name(default_nigori_->first); | 81 encrypted->set_key_name(default_nigori_->first); |
72 if (!default_nigori_->second->Encrypt(serialized, | 82 if (!default_nigori_->second->Encrypt(serialized, |
73 encrypted->mutable_blob())) { | 83 encrypted->mutable_blob())) { |
74 LOG(ERROR) << "Failed to encrypt data."; | 84 LOG(ERROR) << "Failed to encrypt data."; |
75 return false; | 85 return false; |
76 } | 86 } |
77 return true; | 87 return true; |
78 } | 88 } |
79 | 89 |
80 bool Cryptographer::Decrypt(const sync_pb::EncryptedData& encrypted, | 90 bool Cryptographer::Decrypt(const sync_pb::EncryptedData& encrypted, |
(...skipping 330 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
411 continue; | 421 continue; |
412 } | 422 } |
413 nigoris_[key.name()] = make_linked_ptr(new_nigori.release()); | 423 nigoris_[key.name()] = make_linked_ptr(new_nigori.release()); |
414 } | 424 } |
415 } | 425 } |
416 DCHECK(nigoris_.end() != nigoris_.find(default_key_name)); | 426 DCHECK(nigoris_.end() != nigoris_.find(default_key_name)); |
417 default_nigori_ = &*nigoris_.find(default_key_name); | 427 default_nigori_ = &*nigoris_.find(default_key_name); |
418 } | 428 } |
419 | 429 |
420 } // namespace browser_sync | 430 } // namespace browser_sync |
OLD | NEW |