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

Side by Side Diff: sync/internal_api/sync_encryption_handler_impl.h

Issue 10844005: [Sync] Refactor GetEncryptedTypes usage. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase + add dcheck 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 #ifndef SYNC_INTERNAL_API_SYNC_ENCRYPTION_HANDLER_IMPL_H_ 5 #ifndef SYNC_INTERNAL_API_SYNC_ENCRYPTION_HANDLER_IMPL_H_
6 #define SYNC_INTERNAL_API_SYNC_ENCRYPTION_HANDLER_IMPL_H_ 6 #define SYNC_INTERNAL_API_SYNC_ENCRYPTION_HANDLER_IMPL_H_
7 7
8 #include <string> 8 #include <string>
9 9
10 #include "base/compiler_specific.h" 10 #include "base/compiler_specific.h"
11 #include "base/gtest_prod_util.h" 11 #include "base/gtest_prod_util.h"
12 #include "base/threading/thread_checker.h"
12 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h" 14 #include "base/memory/weak_ptr.h"
14 #include "base/observer_list.h" 15 #include "base/observer_list.h"
15 #include "sync/internal_api/public/sync_encryption_handler.h" 16 #include "sync/internal_api/public/sync_encryption_handler.h"
16 #include "sync/syncable/nigori_handler.h" 17 #include "sync/syncable/nigori_handler.h"
18 #include "sync/util/cryptographer.h"
17 19
18 namespace syncer { 20 namespace syncer {
19 21
22 class Encryptor;
20 struct UserShare; 23 struct UserShare;
21 class WriteNode; 24 class WriteNode;
22 class WriteTransaction; 25 class WriteTransaction;
23 26
24 // Sync encryption handler implementation. 27 // Sync encryption handler implementation.
25 // 28 //
26 // This class acts as the respository of all sync encryption state, and handles 29 // This class acts as the respository of all sync encryption state, and handles
27 // encryption related changes/queries coming from both the chrome side and 30 // encryption related changes/queries coming from both the chrome side and
28 // the sync side (via NigoriHandler). It is capable of modifying all sync data 31 // the sync side (via NigoriHandler). It is capable of modifying all sync data
29 // (re-encryption), updating the encrypted types, changing the encryption keys, 32 // (re-encryption), updating the encrypted types, changing the encryption keys,
30 // and creating/receiving nigori node updates. 33 // and creating/receiving nigori node updates.
31 // 34 //
32 // The class should live as long as the directory itself in order to ensure 35 // The class should live as long as the directory itself in order to ensure
33 // any data read/written is properly decrypted/encrypted. 36 // any data read/written is properly decrypted/encrypted.
34 // 37 //
35 // Note: See sync_encryption_handler.h for a description of the chrome visible 38 // Note: See sync_encryption_handler.h for a description of the chrome visible
36 // methods and what they do, and nigori_handler.h for a description of the 39 // methods and what they do, and nigori_handler.h for a description of the
37 // sync methods. 40 // sync methods.
38 // 41 // All methods are non-thread-safe and should only be called from the sync
39 // TODO(zea): Make this class explicitly non-thread safe and ensure its only 42 // thread unless explicitly noted otherwise.
40 // accessed from the sync thread, with the possible exception of
41 // GetEncryptedTypes. Need to cache explicit passphrase state on the UI thread.
42 class SyncEncryptionHandlerImpl 43 class SyncEncryptionHandlerImpl
43 : public SyncEncryptionHandler, 44 : public SyncEncryptionHandler,
44 public syncable::NigoriHandler { 45 public syncable::NigoriHandler {
45 public: 46 public:
46 SyncEncryptionHandlerImpl(UserShare* user_share, 47 SyncEncryptionHandlerImpl(UserShare* user_share,
47 Cryptographer* cryptographer); 48 Encryptor* encryptor);
48 virtual ~SyncEncryptionHandlerImpl(); 49 virtual ~SyncEncryptionHandlerImpl();
49 50
50 // SyncEncryptionHandler implementation. 51 // SyncEncryptionHandler implementation.
51 virtual void AddObserver(Observer* observer) OVERRIDE; 52 virtual void AddObserver(Observer* observer) OVERRIDE;
52 virtual void RemoveObserver(Observer* observer) OVERRIDE; 53 virtual void RemoveObserver(Observer* observer) OVERRIDE;
53 virtual void Init() OVERRIDE; 54 virtual void Init() OVERRIDE;
54 virtual void SetEncryptionPassphrase(const std::string& passphrase, 55 virtual void SetEncryptionPassphrase(const std::string& passphrase,
55 bool is_explicit) OVERRIDE; 56 bool is_explicit) OVERRIDE;
56 virtual void SetDecryptionPassphrase(const std::string& passphrase) OVERRIDE; 57 virtual void SetDecryptionPassphrase(const std::string& passphrase) OVERRIDE;
57 virtual void EnableEncryptEverything() OVERRIDE; 58 virtual void EnableEncryptEverything() OVERRIDE;
58 virtual bool EncryptEverythingEnabled() const OVERRIDE; 59 virtual bool EncryptEverythingEnabled() const OVERRIDE;
60 // Can be called from any thread.
61 // TODO(zea): enforce this is only called on sync thread.
59 virtual bool IsUsingExplicitPassphrase() const OVERRIDE; 62 virtual bool IsUsingExplicitPassphrase() const OVERRIDE;
60 63
61 // NigoriHandler implementation. 64 // NigoriHandler implementation.
62 // Note: all methods are invoked while the caller holds a transaction. 65 // Note: all methods are invoked while the caller holds a transaction.
63 virtual void ApplyNigoriUpdate( 66 virtual void ApplyNigoriUpdate(
64 const sync_pb::NigoriSpecifics& nigori, 67 const sync_pb::NigoriSpecifics& nigori,
65 syncable::BaseTransaction* const trans) OVERRIDE; 68 syncable::BaseTransaction* const trans) OVERRIDE;
66 virtual void UpdateNigoriFromEncryptedTypes( 69 virtual void UpdateNigoriFromEncryptedTypes(
67 sync_pb::NigoriSpecifics* nigori, 70 sync_pb::NigoriSpecifics* nigori,
68 syncable::BaseTransaction* const trans) const OVERRIDE; 71 syncable::BaseTransaction* const trans) const OVERRIDE;
69 virtual ModelTypeSet GetEncryptedTypes() const OVERRIDE; 72 // Can be called from any thread.
73 virtual ModelTypeSet GetEncryptedTypes(
74 syncable::BaseTransaction* const trans) const OVERRIDE;
75
76 // Unsafe getters. Use only if sync is not up and running and there is no risk
77 // of other threads calling this.
78 Cryptographer* GetCryptographerUnsafe();
79 ModelTypeSet GetEncryptedTypesUnsafe();
70 80
71 private: 81 private:
72 FRIEND_TEST_ALL_PREFIXES(SyncEncryptionHandlerImplTest, 82 FRIEND_TEST_ALL_PREFIXES(SyncEncryptionHandlerImplTest,
73 NigoriEncryptionTypes); 83 NigoriEncryptionTypes);
74 FRIEND_TEST_ALL_PREFIXES(SyncEncryptionHandlerImplTest, 84 FRIEND_TEST_ALL_PREFIXES(SyncEncryptionHandlerImplTest,
75 EncryptEverythingExplicit); 85 EncryptEverythingExplicit);
76 FRIEND_TEST_ALL_PREFIXES(SyncEncryptionHandlerImplTest, 86 FRIEND_TEST_ALL_PREFIXES(SyncEncryptionHandlerImplTest,
77 EncryptEverythingImplicit); 87 EncryptEverythingImplicit);
78 FRIEND_TEST_ALL_PREFIXES(SyncEncryptionHandlerImplTest, 88 FRIEND_TEST_ALL_PREFIXES(SyncEncryptionHandlerImplTest,
79 UnknownSensitiveTypes); 89 UnknownSensitiveTypes);
80 90
91 // Container for members that require thread safety protection. All members
92 // that can be accessed from more than one thread should be held here and
93 // accessed via UnlockVault(..) and UnlockVaultMutable(..), which enforce
94 // that a transaction is held.
95 struct Vault {
96 Vault(Encryptor* encryptor, ModelTypeSet encrypted_types);
97 ~Vault();
98
99 // Sync's cryptographer. Used for encrypting and decrypting sync data.
100 Cryptographer cryptographer;
101 // The set of types that require encryption.
102 ModelTypeSet encrypted_types;
103
104 private:
105 DISALLOW_COPY_AND_ASSIGN(Vault);
106 };
107
81 // Iterate over all encrypted types ensuring each entry is properly encrypted. 108 // Iterate over all encrypted types ensuring each entry is properly encrypted.
82 void ReEncryptEverything(WriteTransaction* trans); 109 void ReEncryptEverything(WriteTransaction* trans);
83 110
84 // Apply a nigori update. Updates internal and cryptographer state. 111 // Apply a nigori update. Updates internal and cryptographer state.
85 // Returns true on success, false if |nigori| was incompatible, and the 112 // Returns true on success, false if |nigori| was incompatible, and the
86 // nigori node must be corrected. 113 // nigori node must be corrected.
87 // Note: must be called from within a transaction. 114 // Note: must be called from within a transaction.
88 bool ApplyNigoriUpdateImpl(const sync_pb::NigoriSpecifics& nigori, 115 bool ApplyNigoriUpdateImpl(const sync_pb::NigoriSpecifics& nigori,
89 syncable::BaseTransaction* const trans); 116 syncable::BaseTransaction* const trans);
90 117
91 // Wrapper around WriteEncryptionStateToNigori that creates a new write 118 // Wrapper around WriteEncryptionStateToNigori that creates a new write
92 // transaction. 119 // transaction.
93 void RewriteNigori(); 120 void RewriteNigori();
94 121
95 // Write the current encryption state into the nigori node. This includes 122 // Write the current encryption state into the nigori node. This includes
96 // the encrypted types/encrypt everything state, as well as the keybag/ 123 // the encrypted types/encrypt everything state, as well as the keybag/
97 // explicit passphrase state (if the cryptographer is ready). 124 // explicit passphrase state (if the cryptographer is ready).
98 void WriteEncryptionStateToNigori(WriteTransaction* trans); 125 void WriteEncryptionStateToNigori(WriteTransaction* trans);
99 126
100 // Updates local encrypted types from |nigori|. 127 // Updates local encrypted types from |nigori|.
101 // Returns true if the local set of encrypted types either matched or was 128 // Returns true if the local set of encrypted types either matched or was
102 // a subset of that in |nigori|. Returns false if the local state already 129 // a subset of that in |nigori|. Returns false if the local state already
103 // had stricter encryption than |nigori|, and the nigori node needs to be 130 // had stricter encryption than |nigori|, and the nigori node needs to be
104 // updated with the newer encryption state. 131 // updated with the newer encryption state.
105 // Note: must be called from within a transaction. 132 // Note: must be called from within a transaction.
106 bool UpdateEncryptedTypesFromNigori(const sync_pb::NigoriSpecifics& nigori); 133 bool UpdateEncryptedTypesFromNigori(
134 const sync_pb::NigoriSpecifics& nigori,
135 syncable::BaseTransaction* const trans);
107 136
108 // The final step of SetEncryptionPassphrase and SetDecryptionPassphrase that 137 // The final step of SetEncryptionPassphrase and SetDecryptionPassphrase that
109 // notifies observers of the result of the set passphrase operation, updates 138 // notifies observers of the result of the set passphrase operation, updates
110 // the nigori node, and does re-encryption. 139 // the nigori node, and does re-encryption.
111 // |success|: true if the operation was successful and false otherwise. If 140 // |success|: true if the operation was successful and false otherwise. If
112 // success == false, we send an OnPassphraseRequired notification. 141 // success == false, we send an OnPassphraseRequired notification.
113 // |bootstrap_token|: used to inform observers if the cryptographer's 142 // |bootstrap_token|: used to inform observers if the cryptographer's
114 // bootstrap token was updated. 143 // bootstrap token was updated.
115 // |is_explicit|: used to differentiate between a custom passphrase (true) and 144 // |is_explicit|: used to differentiate between a custom passphrase (true) and
116 // a GAIA passphrase that is implicitly used for encryption 145 // a GAIA passphrase that is implicitly used for encryption
117 // (false). 146 // (false).
118 // |trans| and |nigori_node|: used to access data in the cryptographer. 147 // |trans| and |nigori_node|: used to access data in the cryptographer.
119 void FinishSetPassphrase(bool success, 148 void FinishSetPassphrase(bool success,
120 const std::string& bootstrap_token, 149 const std::string& bootstrap_token,
121 bool is_explicit, 150 bool is_explicit,
122 WriteTransaction* trans, 151 WriteTransaction* trans,
123 WriteNode* nigori_node); 152 WriteNode* nigori_node);
124 153
125 // Merges the given set of encrypted types with the existing set and emits a 154 // Merges the given set of encrypted types with the existing set and emits a
126 // notification if necessary. 155 // notification if necessary.
127 // Note: must be called from within a transaction. 156 // Note: must be called from within a transaction.
128 void MergeEncryptedTypes(ModelTypeSet encrypted_types); 157 void MergeEncryptedTypes(ModelTypeSet new_encrypted_types,
158 syncable::BaseTransaction* const trans);
159
160 // Helper methods for ensuring transactions are held when accessing
161 // |vault_unsafe_|.
162 Vault* UnlockVaultMutable(syncable::BaseTransaction* const trans);
163 const Vault& UnlockVault(syncable::BaseTransaction* const trans) const;
164
165 base::ThreadChecker thread_checker_;
129 166
130 base::WeakPtrFactory<SyncEncryptionHandlerImpl> weak_ptr_factory_; 167 base::WeakPtrFactory<SyncEncryptionHandlerImpl> weak_ptr_factory_;
131 168
132 ObserverList<SyncEncryptionHandler::Observer> observers_; 169 ObserverList<SyncEncryptionHandler::Observer> observers_;
133 170
134 // The current user share (for creating transactions). 171 // The current user share (for creating transactions).
135 UserShare* user_share_; 172 UserShare* user_share_;
136 173
137 // TODO(zea): have the sync encryption handler own the cryptographer, and live 174 // Container for all data that can be accessed from multiple threads. Do not
138 // in the directory. 175 // access this object directly. Instead access it via UnlockVault(..) and
139 Cryptographer* cryptographer_; 176 // UnlockVaultMutable(..).
177 Vault vault_unsafe_;
140 178
141 // The set of types that require encryption. This is accessed on all sync 179 // Sync encryption state that is only modified and accessed from the sync
142 // datatype threads when we write to a node, so we must hold a transaction
143 // whenever we touch/read it.
144 ModelTypeSet encrypted_types_;
145
146 // Sync encryption state. These are only modified and accessed from the sync
147 // thread. 180 // thread.
181 // Whether all current and future types should be encrypted.
148 bool encrypt_everything_; 182 bool encrypt_everything_;
183 // Whether the user is using a custom passphrase for encryption.
149 bool explicit_passphrase_; 184 bool explicit_passphrase_;
150 185
151 // The number of times we've automatically (i.e. not via SetPassphrase or 186 // The number of times we've automatically (i.e. not via SetPassphrase or
152 // conflict resolver) updated the nigori's encryption keys in this chrome 187 // conflict resolver) updated the nigori's encryption keys in this chrome
153 // instantiation. 188 // instantiation.
154 int nigori_overwrite_count_; 189 int nigori_overwrite_count_;
155 190
156 DISALLOW_COPY_AND_ASSIGN(SyncEncryptionHandlerImpl); 191 DISALLOW_COPY_AND_ASSIGN(SyncEncryptionHandlerImpl);
157 }; 192 };
158 193
159 } // namespace syncer 194 } // namespace syncer
160 195
161 #endif // SYNC_INTERNAL_API_PUBLIC_SYNC_ENCRYPTION_HANDLER_IMPL_H_ 196 #endif // SYNC_INTERNAL_API_PUBLIC_SYNC_ENCRYPTION_HANDLER_IMPL_H_
OLDNEW
« no previous file with comments | « sync/internal_api/public/test/test_user_share.h ('k') | sync/internal_api/sync_encryption_handler_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698