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

Side by Side Diff: sync/engine/apply_control_data_updates.cc

Issue 10832286: sync: Introduce control data types (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Respond to Tim's comments Created 8 years, 3 months 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
(Empty)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "sync/engine/apply_control_data_updates.h"
6
7 #include "base/metrics/histogram.h"
8 #include "sync/engine/conflict_resolver.h"
9 #include "sync/engine/conflict_util.h"
10 #include "sync/engine/syncer_util.h"
11 #include "sync/syncable/directory.h"
12 #include "sync/syncable/mutable_entry.h"
13 #include "sync/syncable/nigori_util.h"
14 #include "sync/syncable/write_transaction.h"
15 #include "sync/util/cryptographer.h"
16
17 namespace syncer {
18
19 using syncable::GET_BY_SERVER_TAG;
20 using syncable::IS_UNAPPLIED_UPDATE;
21 using syncable::IS_UNSYNCED;
22 using syncable::SERVER_SPECIFICS;
23 using syncable::SPECIFICS;
24 using syncable::SYNCER;
25
26 void ApplyControlDataUpdates(syncable::Directory* dir) {
27 syncable::WriteTransaction trans(FROM_HERE, SYNCER, dir);
28
29 if (ApplyNigoriUpdates(&trans, dir->GetCryptographer(&trans))) {
30 dir->set_initial_sync_ended_for_type(NIGORI, true);
31 }
32 }
33
34 // Update the cryptographer with the server's nigori node.
35 //
36 // If we have a locally modified nigori node, we merge them manually. This
37 // handles the case where two clients both set a different passphrase. The
38 // second client to attempt to commit will go into a state of having pending
39 // keys, unioned the set of encrypted types, and eventually re-encrypt
40 // everything with the passphrase of the first client and commit the set of
41 // merged encryption keys. Until the second client provides the pending
42 // passphrase, the cryptographer will preserve the encryption keys based on the
43 // local passphrase, while the nigori node will preserve the server encryption
44 // keys.
45 bool ApplyNigoriUpdates(syncable::WriteTransaction* trans,
46 Cryptographer* cryptographer) {
47 syncable::MutableEntry nigori_node(trans, GET_BY_SERVER_TAG,
48 ModelTypeToRootTag(NIGORI));
49
50 // Mainly for unit tests. We should have a Nigori node by this point.
51 if (!nigori_node.good()) {
52 return false;
53 }
54
55 if (!nigori_node.Get(IS_UNAPPLIED_UPDATE)) {
56 return true;
57 }
58
59 const sync_pb::NigoriSpecifics& nigori =
60 nigori_node.Get(SERVER_SPECIFICS).nigori();
61 cryptographer->ApplyNigoriUpdate(nigori, trans);
62
63 // Make sure any unsynced changes are properly encrypted as necessary.
64 // We only perform this if the cryptographer is ready. If not, these are
65 // re-encrypted at SetDecryptionPassphrase time (via ReEncryptEverything).
66 // This logic covers the case where the nigori update marked new datatypes
67 // for encryption, but didn't change the passphrase.
68 if (cryptographer->is_ready()) {
69 // Note that we don't bother to encrypt any data for which IS_UNSYNCED
70 // == false here. The machine that turned on encryption should know about
71 // and re-encrypt all synced data. It's possible it could get interrupted
72 // during this process, but we currently reencrypt everything at startup
73 // as well, so as soon as a client is restarted with this datatype marked
74 // for encryption, all the data should be updated as necessary.
75
76 // If this fails, something is wrong with the cryptographer, but there's
77 // nothing we can do about it here.
78 DVLOG(1) << "Received new nigori, encrypting unsynced changes.";
79 syncable::ProcessUnsyncedChangesForEncryption(trans, cryptographer);
80 }
81
82 if (!nigori_node.Get(IS_UNSYNCED)) { // Update only.
83 UpdateLocalDataFromServerData(trans, &nigori_node);
84 } else { // Conflict.
85 // Create a new set of specifics based on the server specifics (which
86 // preserves their encryption keys).
87 sync_pb::EntitySpecifics specifics = nigori_node.Get(SERVER_SPECIFICS);
88 sync_pb::NigoriSpecifics* server_nigori = specifics.mutable_nigori();
89 // Store the merged set of encrypted types (cryptographer->Update(..) will
90 // have merged the local types already).
91 cryptographer->UpdateNigoriFromEncryptedTypes(server_nigori, trans);
92 // The cryptographer has the both the local and remote encryption keys
93 // (added at cryptographer->Update(..) time).
94 // If the cryptographer is ready, then it already merged both sets of keys
95 // and we can store them back in. In that case, the remote key was already
96 // part of the local keybag, so we preserve the local key as the default
97 // (including whether it's an explicit key).
98 // If the cryptographer is not ready, then the user will have to provide
99 // the passphrase to decrypt the pending keys. When they do so, the
100 // SetDecryptionPassphrase code will act based on whether the server
101 // update has an explicit passphrase or not.
102 // - If the server had an explicit passphrase, that explicit passphrase
103 // will be preserved as the default encryption key.
104 // - If the server did not have an explicit passphrase, we assume the
105 // local passphrase is the most up to date and preserve the local
106 // default encryption key marked as an implicit passphrase.
107 // This works fine except for the case where we had locally set an
108 // explicit passphrase. In that case the nigori node will have the default
109 // key based on the local explicit passphassphrase, but will not have it
110 // marked as explicit. To fix this we'd have to track whether we have a
111 // explicit passphrase or not separate from the nigori, which would
112 // introduce even more complexity, so we leave it up to the user to reset
113 // that passphrase as an explicit one via settings. The goal here is to
114 // ensure both sets of encryption keys are preserved.
115 if (cryptographer->is_ready()) {
116 cryptographer->GetKeys(server_nigori->mutable_encrypted());
117 server_nigori->set_using_explicit_passphrase(
118 nigori_node.Get(SPECIFICS).nigori().using_explicit_passphrase());
119 }
120 nigori_node.Put(SPECIFICS, specifics);
121 DVLOG(1) << "Resolving simple conflict, merging nigori nodes: "
122 << nigori_node;
123
124 OverwriteServerChanges(&nigori_node);
125
126 UMA_HISTOGRAM_ENUMERATION("Sync.ResolveSimpleConflict",
127 ConflictResolver::NIGORI_MERGE,
128 ConflictResolver::CONFLICT_RESOLUTION_SIZE);
129 }
130
131 return true;
132 }
133
134 } // namespace syncer
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698