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

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: Address comments 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
60 // encrypted. The first time we encrypt a node we start from scratch, hence
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.
59 if (!cryptographer->Encrypt(new_specifics, 71 if (!cryptographer->Encrypt(new_specifics,
60 generated_specifics.mutable_encrypted())) { 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 // It's possible this entry was encrypted but didn't properly overwrite the
68 if (AreSpecificsEqual(cryptographer, old_specifics, generated_specifics) && 80 // non_unique_name (see crbug.com/96314).
69 (entry->Get(syncable::NON_UNIQUE_NAME) == kEncryptedString || 81 bool encrypted_without_overwriting_name = (old_specifics.has_encrypted() &&
70 !generated_specifics.has_encrypted())) { 82 entry->Get(syncable::NON_UNIQUE_NAME) != kEncryptedString);
71 // Even if the data is the same but the old specifics are encrypted with an 83
72 // old key, we should go ahead and re-encrypt with the new key. 84 // If we're encrypted but the name wasn't overwritten properly we still want
73 if ((!old_specifics.has_encrypted() && 85 // to rewrite the entry, irrespective of whether the specifics match.
74 !generated_specifics.has_encrypted()) || 86 if (!encrypted_without_overwriting_name &&
75 cryptographer->CanDecryptUsingDefaultKey(old_specifics.encrypted())) { 87 old_specifics.SerializeAsString() ==
76 DVLOG(2) << "Specifics of type " << syncable::ModelTypeToString(type) 88 generated_specifics.SerializeAsString()) {
77 << " already match, dropping change."; 89 DVLOG(2) << "Specifics of type " << syncable::ModelTypeToString(type)
78 return true; 90 << " already match, dropping change.";
79 } 91 return true;
80 // TODO(zea): Add some way to keep track of how often we're reencrypting
81 // because of a passphrase change.
82 } 92 }
83 93
84 if (generated_specifics.has_encrypted()) { 94 if (generated_specifics.has_encrypted()) {
85 // Overwrite the possibly sensitive non-specifics data. 95 // Overwrite the possibly sensitive non-specifics data.
86 entry->Put(syncable::NON_UNIQUE_NAME, kEncryptedString); 96 entry->Put(syncable::NON_UNIQUE_NAME, kEncryptedString);
87 // For bookmarks we actually put bogus data into the unencrypted specifics, 97 // For bookmarks we actually put bogus data into the unencrypted specifics,
88 // else the server will try to do it for us. 98 // else the server will try to do it for us.
89 if (type == syncable::BOOKMARKS) { 99 if (type == syncable::BOOKMARKS) {
90 sync_pb::BookmarkSpecifics* bookmark_specifics = 100 sync_pb::BookmarkSpecifics* bookmark_specifics =
91 generated_specifics.MutableExtension(sync_pb::bookmark); 101 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); 187 entity_specifics.MutableExtension(sync_pb::nigori)->CopyFrom(new_value);
178 SetEntitySpecifics(entity_specifics); 188 SetEntitySpecifics(entity_specifics);
179 } 189 }
180 190
181 void WriteNode::SetPasswordSpecifics( 191 void WriteNode::SetPasswordSpecifics(
182 const sync_pb::PasswordSpecificsData& data) { 192 const sync_pb::PasswordSpecificsData& data) {
183 DCHECK_EQ(syncable::PASSWORDS, GetModelType()); 193 DCHECK_EQ(syncable::PASSWORDS, GetModelType());
184 194
185 Cryptographer* cryptographer = GetTransaction()->GetCryptographer(); 195 Cryptographer* cryptographer = GetTransaction()->GetCryptographer();
186 196
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; 197 sync_pb::PasswordSpecifics new_value;
202 if (!cryptographer->Encrypt(data, new_value.mutable_encrypted())) { 198 if (!cryptographer->Encrypt(data, new_value.mutable_encrypted())) {
203 NOTREACHED() << "Failed to encrypt password, possibly due to sync node " 199 NOTREACHED() << "Failed to encrypt password, possibly due to sync node "
204 << "corruption"; 200 << "corruption";
205 return; 201 return;
206 } 202 }
207 203
208 sync_pb::EntitySpecifics entity_specifics; 204 sync_pb::EntitySpecifics entity_specifics;
209 entity_specifics.MutableExtension(sync_pb::password)->CopyFrom(new_value); 205 entity_specifics.MutableExtension(sync_pb::password)->CopyFrom(new_value);
210 SetEntitySpecifics(entity_specifics); 206 SetEntitySpecifics(entity_specifics);
(...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after
518 sync_pb::BookmarkSpecifics new_value = GetBookmarkSpecifics(); 514 sync_pb::BookmarkSpecifics new_value = GetBookmarkSpecifics();
519 new_value.set_favicon(bytes.empty() ? NULL : &bytes[0], bytes.size()); 515 new_value.set_favicon(bytes.empty() ? NULL : &bytes[0], bytes.size());
520 SetBookmarkSpecifics(new_value); 516 SetBookmarkSpecifics(new_value);
521 } 517 }
522 518
523 void WriteNode::MarkForSyncing() { 519 void WriteNode::MarkForSyncing() {
524 syncable::MarkForSyncing(entry_); 520 syncable::MarkForSyncing(entry_);
525 } 521 }
526 522
527 } // namespace sync_api 523 } // namespace sync_api
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698