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

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

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

Powered by Google App Engine
This is Rietveld 408576698