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 "chrome/browser/sync/internal_api/write_node.h" | 5 #include "chrome/browser/sync/internal_api/write_node.h" |
6 | 6 |
7 #include "base/json/json_writer.h" | 7 #include "base/json/json_writer.h" |
8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
9 #include "base/values.h" | 9 #include "base/values.h" |
10 #include "chrome/browser/sync/engine/nigori_util.h" | 10 #include "chrome/browser/sync/engine/nigori_util.h" |
(...skipping 20 matching lines...) Expand all Loading... | |
31 namespace sync_api { | 31 namespace sync_api { |
32 | 32 |
33 static const char kDefaultNameForNewNodes[] = " "; | 33 static const char kDefaultNameForNewNodes[] = " "; |
34 | 34 |
35 bool WriteNode::UpdateEntryWithEncryption( | 35 bool WriteNode::UpdateEntryWithEncryption( |
36 browser_sync::Cryptographer* cryptographer, | 36 browser_sync::Cryptographer* cryptographer, |
37 const sync_pb::EntitySpecifics& new_specifics, | 37 const sync_pb::EntitySpecifics& new_specifics, |
38 syncable::MutableEntry* entry) { | 38 syncable::MutableEntry* entry) { |
39 syncable::ModelType type = syncable::GetModelTypeFromSpecifics(new_specifics); | 39 syncable::ModelType type = syncable::GetModelTypeFromSpecifics(new_specifics); |
40 DCHECK_GE(type, syncable::FIRST_REAL_MODEL_TYPE); | 40 DCHECK_GE(type, syncable::FIRST_REAL_MODEL_TYPE); |
41 const sync_pb::EntitySpecifics& old_specifics = entry->Get(SPECIFICS); | |
41 syncable::ModelTypeSet encrypted_types = cryptographer->GetEncryptedTypes(); | 42 syncable::ModelTypeSet encrypted_types = cryptographer->GetEncryptedTypes(); |
42 sync_pb::EntitySpecifics generated_specifics; | 43 sync_pb::EntitySpecifics generated_specifics; |
43 if (!SpecificsNeedsEncryption(encrypted_types, new_specifics) || | 44 if (!SpecificsNeedsEncryption(encrypted_types, new_specifics) || |
44 !cryptographer->is_initialized()) { | 45 !cryptographer->is_initialized()) { |
45 // No encryption required or we are unable to encrypt. | 46 // No encryption required or we are unable to encrypt. |
46 generated_specifics.CopyFrom(new_specifics); | 47 generated_specifics.CopyFrom(new_specifics); |
47 } else { | 48 } else { |
48 // Encrypt new_specifics into generated_specifics. | 49 // Encrypt new_specifics into generated_specifics. |
49 if (VLOG_IS_ON(2)) { | 50 if (VLOG_IS_ON(2)) { |
50 scoped_ptr<DictionaryValue> value(entry->ToValue()); | 51 scoped_ptr<DictionaryValue> value(entry->ToValue()); |
51 std::string info; | 52 std::string info; |
52 base::JSONWriter::Write(value.get(), true, &info); | 53 base::JSONWriter::Write(value.get(), true, &info); |
53 DVLOG(2) << "Encrypting specifics of type " | 54 DVLOG(2) << "Encrypting specifics of type " |
54 << syncable::ModelTypeToString(type) | 55 << syncable::ModelTypeToString(type) |
55 << " with content: " | 56 << " with content: " |
56 << info; | 57 << info; |
57 } | 58 } |
58 syncable::AddDefaultExtensionValue(type, &generated_specifics); | 59 // Only copy over the old specifics if it is of the right type and already |
59 if (!cryptographer->Encrypt(new_specifics, | 60 // encrypted. The first time we encrypt a node we start from scratch, hence |
60 generated_specifics.mutable_encrypted())) { | 61 // removing all the unencrypted data, but from then on we only want to |
62 // update the node if the data changes or the encryption key changes. | |
63 if (syncable::GetModelTypeFromSpecifics(old_specifics) == type && | |
akalin
2011/12/06 18:28:02
use braces for complex if-statement
Nicolas Zea
2011/12/06 20:45:36
Done.
| |
64 old_specifics.has_encrypted()) | |
65 generated_specifics.CopyFrom(old_specifics); | |
66 else | |
67 syncable::AddDefaultExtensionValue(type, &generated_specifics); | |
68 // Does not change anything if underlying encrypted blob was already up | |
69 // to date and encrypted with the default key. | |
70 if (!cryptographer->EncryptIfDifferent( | |
71 new_specifics, | |
72 generated_specifics.mutable_encrypted())) { | |
61 NOTREACHED() << "Could not encrypt data for node of type " | 73 NOTREACHED() << "Could not encrypt data for node of type " |
62 << syncable::ModelTypeToString(type); | 74 << syncable::ModelTypeToString(type); |
63 return false; | 75 return false; |
64 } | 76 } |
65 } | 77 } |
66 | 78 |
67 const sync_pb::EntitySpecifics& old_specifics = entry->Get(SPECIFICS); | 79 // No need to account for encryption here, EncryptIfDifferent(..) would only |
68 if (AreSpecificsEqual(cryptographer, old_specifics, generated_specifics) && | 80 // have modified generated_specifics if something changed. |
81 if (old_specifics.SerializeAsString() == | |
82 generated_specifics.SerializeAsString() && | |
69 (entry->Get(syncable::NON_UNIQUE_NAME) == kEncryptedString || | 83 (entry->Get(syncable::NON_UNIQUE_NAME) == kEncryptedString || |
akalin
2011/12/06 18:28:02
Hmm do we need the 2nd && clause of this if statem
Nicolas Zea
2011/12/06 20:45:36
We only want to enforce the non_unique_name if we'
akalin
2011/12/09 23:52:42
So, if I understand this correctly, if it wasn't f
Nicolas Zea
2011/12/12 20:12:26
Done.
| |
70 !generated_specifics.has_encrypted())) { | 84 !generated_specifics.has_encrypted())) { |
71 // Even if the data is the same but the old specifics are encrypted with an | 85 DVLOG(2) << "Specifics of type " << syncable::ModelTypeToString(type) |
72 // old key, we should go ahead and re-encrypt with the new key. | 86 << " already match, dropping change."; |
73 if ((!old_specifics.has_encrypted() && | 87 return true; |
74 !generated_specifics.has_encrypted()) || | |
75 cryptographer->CanDecryptUsingDefaultKey(old_specifics.encrypted())) { | |
76 DVLOG(2) << "Specifics of type " << syncable::ModelTypeToString(type) | |
77 << " already match, dropping change."; | |
78 return true; | |
79 } | |
80 // TODO(zea): Add some way to keep track of how often we're reencrypting | |
81 // because of a passphrase change. | |
82 } | 88 } |
83 | 89 |
84 if (generated_specifics.has_encrypted()) { | 90 if (generated_specifics.has_encrypted()) { |
85 // Overwrite the possibly sensitive non-specifics data. | 91 // Overwrite the possibly sensitive non-specifics data. |
86 entry->Put(syncable::NON_UNIQUE_NAME, kEncryptedString); | 92 entry->Put(syncable::NON_UNIQUE_NAME, kEncryptedString); |
87 // For bookmarks we actually put bogus data into the unencrypted specifics, | 93 // For bookmarks we actually put bogus data into the unencrypted specifics, |
88 // else the server will try to do it for us. | 94 // else the server will try to do it for us. |
89 if (type == syncable::BOOKMARKS) { | 95 if (type == syncable::BOOKMARKS) { |
90 sync_pb::BookmarkSpecifics* bookmark_specifics = | 96 sync_pb::BookmarkSpecifics* bookmark_specifics = |
91 generated_specifics.MutableExtension(sync_pb::bookmark); | 97 generated_specifics.MutableExtension(sync_pb::bookmark); |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
177 entity_specifics.MutableExtension(sync_pb::nigori)->CopyFrom(new_value); | 183 entity_specifics.MutableExtension(sync_pb::nigori)->CopyFrom(new_value); |
178 SetEntitySpecifics(entity_specifics); | 184 SetEntitySpecifics(entity_specifics); |
179 } | 185 } |
180 | 186 |
181 void WriteNode::SetPasswordSpecifics( | 187 void WriteNode::SetPasswordSpecifics( |
182 const sync_pb::PasswordSpecificsData& data) { | 188 const sync_pb::PasswordSpecificsData& data) { |
183 DCHECK_EQ(syncable::PASSWORDS, GetModelType()); | 189 DCHECK_EQ(syncable::PASSWORDS, GetModelType()); |
184 | 190 |
185 Cryptographer* cryptographer = GetTransaction()->GetCryptographer(); | 191 Cryptographer* cryptographer = GetTransaction()->GetCryptographer(); |
186 | 192 |
187 // Idempotency check to prevent unnecessary syncing: if the plaintexts match | |
188 // and the old ciphertext is encrypted with the most current key, there's | |
189 // nothing to do here. Because each encryption is seeded with a different | |
190 // random value, checking for equivalence post-encryption doesn't suffice. | |
191 const sync_pb::EncryptedData& old_ciphertext = | |
192 GetEntry()->Get(SPECIFICS).GetExtension(sync_pb::password).encrypted(); | |
193 scoped_ptr<sync_pb::PasswordSpecificsData> old_plaintext( | |
194 DecryptPasswordSpecifics(GetEntry()->Get(SPECIFICS), cryptographer)); | |
195 if (old_plaintext.get() && | |
196 old_plaintext->SerializeAsString() == data.SerializeAsString() && | |
197 cryptographer->CanDecryptUsingDefaultKey(old_ciphertext)) { | |
198 return; | |
199 } | |
200 | |
201 sync_pb::PasswordSpecifics new_value; | 193 sync_pb::PasswordSpecifics new_value; |
202 if (!cryptographer->Encrypt(data, new_value.mutable_encrypted())) { | 194 if (!cryptographer->EncryptIfDifferent(data, new_value.mutable_encrypted())) { |
203 NOTREACHED() << "Failed to encrypt password, possibly due to sync node " | 195 NOTREACHED() << "Failed to encrypt password, possibly due to sync node " |
204 << "corruption"; | 196 << "corruption"; |
205 return; | 197 return; |
206 } | 198 } |
207 | 199 |
208 sync_pb::EntitySpecifics entity_specifics; | 200 sync_pb::EntitySpecifics entity_specifics; |
209 entity_specifics.MutableExtension(sync_pb::password)->CopyFrom(new_value); | 201 entity_specifics.MutableExtension(sync_pb::password)->CopyFrom(new_value); |
210 SetEntitySpecifics(entity_specifics); | 202 SetEntitySpecifics(entity_specifics); |
211 } | 203 } |
212 | 204 |
(...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
518 sync_pb::BookmarkSpecifics new_value = GetBookmarkSpecifics(); | 510 sync_pb::BookmarkSpecifics new_value = GetBookmarkSpecifics(); |
519 new_value.set_favicon(bytes.empty() ? NULL : &bytes[0], bytes.size()); | 511 new_value.set_favicon(bytes.empty() ? NULL : &bytes[0], bytes.size()); |
520 SetBookmarkSpecifics(new_value); | 512 SetBookmarkSpecifics(new_value); |
521 } | 513 } |
522 | 514 |
523 void WriteNode::MarkForSyncing() { | 515 void WriteNode::MarkForSyncing() { |
524 syncable::MarkForSyncing(entry_); | 516 syncable::MarkForSyncing(entry_); |
525 } | 517 } |
526 | 518 |
527 } // namespace sync_api | 519 } // namespace sync_api |
OLD | NEW |