OLD | NEW |
---|---|
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" |
17 #include "sync/internal_api/public/user_share.h" | |
16 #include "sync/syncable/nigori_handler.h" | 18 #include "sync/syncable/nigori_handler.h" |
19 #include "sync/util/cryptographer.h" | |
17 | 20 |
18 namespace syncer { | 21 namespace syncer { |
19 | 22 |
23 class Encryptor; | |
20 struct UserShare; | 24 struct UserShare; |
21 class WriteNode; | 25 class WriteNode; |
22 class WriteTransaction; | 26 class WriteTransaction; |
23 | 27 |
28 // Helper class for ensuring non-thread-safe objects are only accessed | |
29 // while a valid transaction is open. Does not take ownership, so the held | |
30 // object must live longer than the transaction holder. | |
31 template <typename T> | |
32 class TransactionalHolder { | |
tim (not reviewing)
2012/08/23 18:38:10
I don't think this needs to be exposed outside of
Nicolas Zea
2012/08/23 22:49:32
Yeah, I guess they're pretty much the same. Done.
| |
33 public: | |
34 TransactionalHolder(UserShare* user_share, T* obj) | |
35 : user_share_(user_share), | |
36 obj_(obj) {} | |
37 ~TransactionalHolder() {} | |
38 | |
39 // Given an open transaction |trans| that matches the directory in | |
40 // |user_share_|, returns the held object. | |
41 const T& Get(syncable::BaseTransaction* const trans) const; | |
42 T* GetMutable(syncable::BaseTransaction* const trans); | |
43 | |
44 private: | |
45 // The current user share. Used to enforce the transaction belongs to this | |
46 // sync profile. | |
47 const UserShare* user_share_; | |
48 | |
49 // The object being held. Can only be publicly accessed while a transaction | |
50 // is open. | |
51 T* obj_; | |
52 }; | |
53 | |
24 // Sync encryption handler implementation. | 54 // Sync encryption handler implementation. |
25 // | 55 // |
26 // This class acts as the respository of all sync encryption state, and handles | 56 // 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 | 57 // 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 | 58 // the sync side (via NigoriHandler). It is capable of modifying all sync data |
29 // (re-encryption), updating the encrypted types, changing the encryption keys, | 59 // (re-encryption), updating the encrypted types, changing the encryption keys, |
30 // and creating/receiving nigori node updates. | 60 // and creating/receiving nigori node updates. |
31 // | 61 // |
32 // The class should live as long as the directory itself in order to ensure | 62 // The class should live as long as the directory itself in order to ensure |
33 // any data read/written is properly decrypted/encrypted. | 63 // any data read/written is properly decrypted/encrypted. |
34 // | 64 // |
35 // Note: See sync_encryption_handler.h for a description of the chrome visible | 65 // 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 | 66 // methods and what they do, and nigori_handler.h for a description of the |
37 // sync methods. | 67 // sync methods. |
38 // | 68 // 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 | 69 // 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 | 70 class SyncEncryptionHandlerImpl |
43 : public SyncEncryptionHandler, | 71 : public SyncEncryptionHandler, |
44 public syncable::NigoriHandler { | 72 public syncable::NigoriHandler { |
45 public: | 73 public: |
46 SyncEncryptionHandlerImpl(UserShare* user_share, | 74 SyncEncryptionHandlerImpl(UserShare* user_share, |
47 Cryptographer* cryptographer); | 75 Encryptor* encryptor); |
48 virtual ~SyncEncryptionHandlerImpl(); | 76 virtual ~SyncEncryptionHandlerImpl(); |
49 | 77 |
50 // SyncEncryptionHandler implementation. | 78 // SyncEncryptionHandler implementation. |
51 virtual void AddObserver(Observer* observer) OVERRIDE; | 79 virtual void AddObserver(Observer* observer) OVERRIDE; |
52 virtual void RemoveObserver(Observer* observer) OVERRIDE; | 80 virtual void RemoveObserver(Observer* observer) OVERRIDE; |
53 virtual void Init() OVERRIDE; | 81 virtual void Init() OVERRIDE; |
54 virtual void SetEncryptionPassphrase(const std::string& passphrase, | 82 virtual void SetEncryptionPassphrase(const std::string& passphrase, |
55 bool is_explicit) OVERRIDE; | 83 bool is_explicit) OVERRIDE; |
56 virtual void SetDecryptionPassphrase(const std::string& passphrase) OVERRIDE; | 84 virtual void SetDecryptionPassphrase(const std::string& passphrase) OVERRIDE; |
57 virtual void EnableEncryptEverything() OVERRIDE; | 85 virtual void EnableEncryptEverything() OVERRIDE; |
58 virtual bool EncryptEverythingEnabled() const OVERRIDE; | 86 virtual bool EncryptEverythingEnabled() const OVERRIDE; |
87 // Can be called from any thread. | |
88 // TODO(zea): enforce this is only called on sync thread. | |
59 virtual bool IsUsingExplicitPassphrase() const OVERRIDE; | 89 virtual bool IsUsingExplicitPassphrase() const OVERRIDE; |
60 | 90 |
61 // NigoriHandler implementation. | 91 // NigoriHandler implementation. |
62 // Note: all methods are invoked while the caller holds a transaction. | 92 // Note: all methods are invoked while the caller holds a transaction. |
63 virtual void ApplyNigoriUpdate( | 93 virtual void ApplyNigoriUpdate( |
64 const sync_pb::NigoriSpecifics& nigori, | 94 const sync_pb::NigoriSpecifics& nigori, |
65 syncable::BaseTransaction* const trans) OVERRIDE; | 95 syncable::BaseTransaction* const trans) OVERRIDE; |
66 virtual void UpdateNigoriFromEncryptedTypes( | 96 virtual void UpdateNigoriFromEncryptedTypes( |
67 sync_pb::NigoriSpecifics* nigori, | 97 sync_pb::NigoriSpecifics* nigori, |
68 syncable::BaseTransaction* const trans) const OVERRIDE; | 98 syncable::BaseTransaction* const trans) const OVERRIDE; |
69 virtual ModelTypeSet GetEncryptedTypes() const OVERRIDE; | 99 // Can be called from any thread. |
100 virtual ModelTypeSet GetEncryptedTypes( | |
101 syncable::BaseTransaction* const trans) const OVERRIDE; | |
102 | |
103 // Getters. | |
104 Cryptographer* cryptographer_unsafe() { return &cryptographer_unsafe_; } | |
105 ModelTypeSet encrypted_types_unsafe() { return encrypted_types_unsafe_; } | |
70 | 106 |
71 private: | 107 private: |
72 FRIEND_TEST_ALL_PREFIXES(SyncEncryptionHandlerImplTest, | 108 FRIEND_TEST_ALL_PREFIXES(SyncEncryptionHandlerImplTest, |
73 NigoriEncryptionTypes); | 109 NigoriEncryptionTypes); |
74 FRIEND_TEST_ALL_PREFIXES(SyncEncryptionHandlerImplTest, | 110 FRIEND_TEST_ALL_PREFIXES(SyncEncryptionHandlerImplTest, |
75 EncryptEverythingExplicit); | 111 EncryptEverythingExplicit); |
76 FRIEND_TEST_ALL_PREFIXES(SyncEncryptionHandlerImplTest, | 112 FRIEND_TEST_ALL_PREFIXES(SyncEncryptionHandlerImplTest, |
77 EncryptEverythingImplicit); | 113 EncryptEverythingImplicit); |
78 FRIEND_TEST_ALL_PREFIXES(SyncEncryptionHandlerImplTest, | 114 FRIEND_TEST_ALL_PREFIXES(SyncEncryptionHandlerImplTest, |
79 UnknownSensitiveTypes); | 115 UnknownSensitiveTypes); |
(...skipping 16 matching lines...) Expand all Loading... | |
96 // the encrypted types/encrypt everything state, as well as the keybag/ | 132 // the encrypted types/encrypt everything state, as well as the keybag/ |
97 // explicit passphrase state (if the cryptographer is ready). | 133 // explicit passphrase state (if the cryptographer is ready). |
98 void WriteEncryptionStateToNigori(WriteTransaction* trans); | 134 void WriteEncryptionStateToNigori(WriteTransaction* trans); |
99 | 135 |
100 // Updates local encrypted types from |nigori|. | 136 // Updates local encrypted types from |nigori|. |
101 // Returns true if the local set of encrypted types either matched or was | 137 // 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 | 138 // 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 | 139 // had stricter encryption than |nigori|, and the nigori node needs to be |
104 // updated with the newer encryption state. | 140 // updated with the newer encryption state. |
105 // Note: must be called from within a transaction. | 141 // Note: must be called from within a transaction. |
106 bool UpdateEncryptedTypesFromNigori(const sync_pb::NigoriSpecifics& nigori); | 142 bool UpdateEncryptedTypesFromNigori( |
143 const sync_pb::NigoriSpecifics& nigori, | |
144 syncable::BaseTransaction* const trans); | |
107 | 145 |
108 // The final step of SetEncryptionPassphrase and SetDecryptionPassphrase that | 146 // The final step of SetEncryptionPassphrase and SetDecryptionPassphrase that |
109 // notifies observers of the result of the set passphrase operation, updates | 147 // notifies observers of the result of the set passphrase operation, updates |
110 // the nigori node, and does re-encryption. | 148 // the nigori node, and does re-encryption. |
111 // |success|: true if the operation was successful and false otherwise. If | 149 // |success|: true if the operation was successful and false otherwise. If |
112 // success == false, we send an OnPassphraseRequired notification. | 150 // success == false, we send an OnPassphraseRequired notification. |
113 // |bootstrap_token|: used to inform observers if the cryptographer's | 151 // |bootstrap_token|: used to inform observers if the cryptographer's |
114 // bootstrap token was updated. | 152 // bootstrap token was updated. |
115 // |is_explicit|: used to differentiate between a custom passphrase (true) and | 153 // |is_explicit|: used to differentiate between a custom passphrase (true) and |
116 // a GAIA passphrase that is implicitly used for encryption | 154 // a GAIA passphrase that is implicitly used for encryption |
117 // (false). | 155 // (false). |
118 // |trans| and |nigori_node|: used to access data in the cryptographer. | 156 // |trans| and |nigori_node|: used to access data in the cryptographer. |
119 void FinishSetPassphrase(bool success, | 157 void FinishSetPassphrase(bool success, |
120 const std::string& bootstrap_token, | 158 const std::string& bootstrap_token, |
121 bool is_explicit, | 159 bool is_explicit, |
122 WriteTransaction* trans, | 160 WriteTransaction* trans, |
123 WriteNode* nigori_node); | 161 WriteNode* nigori_node); |
124 | 162 |
125 // Merges the given set of encrypted types with the existing set and emits a | 163 // Merges the given set of encrypted types with the existing set and emits a |
126 // notification if necessary. | 164 // notification if necessary. |
127 // Note: must be called from within a transaction. | 165 // Note: must be called from within a transaction. |
128 void MergeEncryptedTypes(ModelTypeSet encrypted_types); | 166 void MergeEncryptedTypes(ModelTypeSet new_encrypted_types, |
167 syncable::BaseTransaction* const trans); | |
168 | |
169 base::ThreadChecker thread_checker_; | |
129 | 170 |
130 base::WeakPtrFactory<SyncEncryptionHandlerImpl> weak_ptr_factory_; | 171 base::WeakPtrFactory<SyncEncryptionHandlerImpl> weak_ptr_factory_; |
131 | 172 |
132 ObserverList<SyncEncryptionHandler::Observer> observers_; | 173 ObserverList<SyncEncryptionHandler::Observer> observers_; |
133 | 174 |
134 // The current user share (for creating transactions). | 175 // The current user share (for creating transactions). |
135 UserShare* user_share_; | 176 UserShare* user_share_; |
136 | 177 |
137 // TODO(zea): have the sync encryption handler own the cryptographer, and live | 178 // Sync encryption state that is accessed from any thread. Must only be |
138 // in the directory. | 179 // accessed while holding a transaction. |
139 Cryptographer* cryptographer_; | 180 // Sync's cryptographer. |
181 Cryptographer cryptographer_unsafe_; | |
182 // The set of types that require encryption. | |
183 ModelTypeSet encrypted_types_unsafe_; | |
140 | 184 |
141 // The set of types that require encryption. This is accessed on all sync | 185 // Helpers to enforce thread safety. |
142 // datatype threads when we write to a node, so we must hold a transaction | 186 // Sync's cryptographer (wrapped within a transactional holder). |
143 // whenever we touch/read it. | 187 TransactionalHolder<Cryptographer> cryptographer_holder_; |
144 ModelTypeSet encrypted_types_; | 188 // Sync's set of encrypted types (wrapped within a transactional holder). |
189 TransactionalHolder<ModelTypeSet> encrypted_types_holder_; | |
145 | 190 |
146 // Sync encryption state. These are only modified and accessed from the sync | 191 // Sync encryption state that is only modified and accessed from the sync |
147 // thread. | 192 // thread. |
193 // Whether all current and future types should be encrypted. | |
148 bool encrypt_everything_; | 194 bool encrypt_everything_; |
195 // Whether the user is using a custom passphrase for encryption. | |
149 bool explicit_passphrase_; | 196 bool explicit_passphrase_; |
150 | 197 |
151 // The number of times we've automatically (i.e. not via SetPassphrase or | 198 // 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 | 199 // conflict resolver) updated the nigori's encryption keys in this chrome |
153 // instantiation. | 200 // instantiation. |
154 int nigori_overwrite_count_; | 201 int nigori_overwrite_count_; |
155 | 202 |
156 DISALLOW_COPY_AND_ASSIGN(SyncEncryptionHandlerImpl); | 203 DISALLOW_COPY_AND_ASSIGN(SyncEncryptionHandlerImpl); |
157 }; | 204 }; |
158 | 205 |
159 } // namespace syncer | 206 } // namespace syncer |
160 | 207 |
161 #endif // SYNC_INTERNAL_API_PUBLIC_SYNC_ENCRYPTION_HANDLER_IMPL_H_ | 208 #endif // SYNC_INTERNAL_API_PUBLIC_SYNC_ENCRYPTION_HANDLER_IMPL_H_ |
OLD | NEW |