Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(58)

Side by Side Diff: chrome/browser/sync/internal_api/write_node.cc

Issue 8759019: [Sync] Add intelligent re-encryption support. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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 &&
64 old_specifics.has_encrypted()) {
65 generated_specifics.CopyFrom(old_specifics);
66 } else {
67 syncable::AddDefaultExtensionValue(type, &generated_specifics);
68 }
69 // Does not change anything if underlying encrypted blob was already up
70 // to date and encrypted with the default key.
71 if (!cryptographer->EncryptIfDifferent(
72 new_specifics,
73 generated_specifics.mutable_encrypted())) {
61 NOTREACHED() << "Could not encrypt data for node of type " 74 NOTREACHED() << "Could not encrypt data for node of type "
62 << syncable::ModelTypeToString(type); 75 << syncable::ModelTypeToString(type);
63 return false; 76 return false;
64 } 77 }
65 } 78 }
66 79
67 const sync_pb::EntitySpecifics& old_specifics = entry->Get(SPECIFICS); 80 // No need to account for encryption here, EncryptIfDifferent(..) would only
68 if (AreSpecificsEqual(cryptographer, old_specifics, generated_specifics) && 81 // have modified generated_specifics if something changed.
82 if (old_specifics.SerializeAsString() ==
83 generated_specifics.SerializeAsString() &&
69 (entry->Get(syncable::NON_UNIQUE_NAME) == kEncryptedString || 84 (entry->Get(syncable::NON_UNIQUE_NAME) == kEncryptedString ||
70 !generated_specifics.has_encrypted())) { 85 !generated_specifics.has_encrypted())) {
71 // Even if the data is the same but the old specifics are encrypted with an 86 DVLOG(2) << "Specifics of type " << syncable::ModelTypeToString(type)
72 // old key, we should go ahead and re-encrypt with the new key. 87 << " already match, dropping change.";
73 if ((!old_specifics.has_encrypted() && 88 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 } 89 }
83 90
84 if (generated_specifics.has_encrypted()) { 91 if (generated_specifics.has_encrypted()) {
85 // Overwrite the possibly sensitive non-specifics data. 92 // Overwrite the possibly sensitive non-specifics data.
86 entry->Put(syncable::NON_UNIQUE_NAME, kEncryptedString); 93 entry->Put(syncable::NON_UNIQUE_NAME, kEncryptedString);
87 // For bookmarks we actually put bogus data into the unencrypted specifics, 94 // For bookmarks we actually put bogus data into the unencrypted specifics,
88 // else the server will try to do it for us. 95 // else the server will try to do it for us.
89 if (type == syncable::BOOKMARKS) { 96 if (type == syncable::BOOKMARKS) {
90 sync_pb::BookmarkSpecifics* bookmark_specifics = 97 sync_pb::BookmarkSpecifics* bookmark_specifics =
91 generated_specifics.MutableExtension(sync_pb::bookmark); 98 generated_specifics.MutableExtension(sync_pb::bookmark);
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 entity_specifics.MutableExtension(sync_pb::nigori)->CopyFrom(new_value); 184 entity_specifics.MutableExtension(sync_pb::nigori)->CopyFrom(new_value);
178 SetEntitySpecifics(entity_specifics); 185 SetEntitySpecifics(entity_specifics);
179 } 186 }
180 187
181 void WriteNode::SetPasswordSpecifics( 188 void WriteNode::SetPasswordSpecifics(
182 const sync_pb::PasswordSpecificsData& data) { 189 const sync_pb::PasswordSpecificsData& data) {
183 DCHECK_EQ(syncable::PASSWORDS, GetModelType()); 190 DCHECK_EQ(syncable::PASSWORDS, GetModelType());
184 191
185 Cryptographer* cryptographer = GetTransaction()->GetCryptographer(); 192 Cryptographer* cryptographer = GetTransaction()->GetCryptographer();
186 193
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; 194 sync_pb::PasswordSpecifics new_value;
202 if (!cryptographer->Encrypt(data, new_value.mutable_encrypted())) { 195 if (!cryptographer->EncryptIfDifferent(data, new_value.mutable_encrypted())) {
203 NOTREACHED() << "Failed to encrypt password, possibly due to sync node " 196 NOTREACHED() << "Failed to encrypt password, possibly due to sync node "
204 << "corruption"; 197 << "corruption";
205 return; 198 return;
206 } 199 }
207 200
208 sync_pb::EntitySpecifics entity_specifics; 201 sync_pb::EntitySpecifics entity_specifics;
209 entity_specifics.MutableExtension(sync_pb::password)->CopyFrom(new_value); 202 entity_specifics.MutableExtension(sync_pb::password)->CopyFrom(new_value);
210 SetEntitySpecifics(entity_specifics); 203 SetEntitySpecifics(entity_specifics);
211 } 204 }
212 205
(...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after
518 sync_pb::BookmarkSpecifics new_value = GetBookmarkSpecifics(); 511 sync_pb::BookmarkSpecifics new_value = GetBookmarkSpecifics();
519 new_value.set_favicon(bytes.empty() ? NULL : &bytes[0], bytes.size()); 512 new_value.set_favicon(bytes.empty() ? NULL : &bytes[0], bytes.size());
520 SetBookmarkSpecifics(new_value); 513 SetBookmarkSpecifics(new_value);
521 } 514 }
522 515
523 void WriteNode::MarkForSyncing() { 516 void WriteNode::MarkForSyncing() {
524 syncable::MarkForSyncing(entry_); 517 syncable::MarkForSyncing(entry_);
525 } 518 }
526 519
527 } // namespace sync_api 520 } // namespace sync_api
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698