| 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_UTIL_CRYPTOGRAPHER_H_ | 5 #ifndef SYNC_UTIL_CRYPTOGRAPHER_H_ |
| 6 #define SYNC_UTIL_CRYPTOGRAPHER_H_ | 6 #define SYNC_UTIL_CRYPTOGRAPHER_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/gtest_prod_util.h" | 11 #include "base/gtest_prod_util.h" |
| 12 #include "base/memory/linked_ptr.h" | 12 #include "base/memory/linked_ptr.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/observer_list.h" | |
| 15 #include "sync/internal_api/public/base/model_type.h" | 14 #include "sync/internal_api/public/base/model_type.h" |
| 16 #include "sync/protocol/encryption.pb.h" | 15 #include "sync/protocol/encryption.pb.h" |
| 17 #include "sync/protocol/nigori_specifics.pb.h" | |
| 18 #include "sync/util/nigori.h" | 16 #include "sync/util/nigori.h" |
| 19 | 17 |
| 18 namespace browser_sync { |
| 19 class NigoriChangeProcessor; |
| 20 } |
| 21 |
| 22 namespace sync_pb { |
| 23 class NigoriKeyBag; |
| 24 class NigoriSpecifics; |
| 25 } |
| 26 |
| 20 namespace syncer { | 27 namespace syncer { |
| 21 | 28 |
| 22 class Encryptor; | 29 class Encryptor; |
| 30 class SyncEncryptionHandler; |
| 23 | 31 |
| 24 extern const char kNigoriTag[]; | 32 extern const char kNigoriTag[]; |
| 25 | 33 |
| 26 // The parameters used to initialize a Nigori instance. | 34 // The parameters used to initialize a Nigori instance. |
| 27 struct KeyParams { | 35 struct KeyParams { |
| 28 std::string hostname; | 36 std::string hostname; |
| 29 std::string username; | 37 std::string username; |
| 30 std::string password; | 38 std::string password; |
| 31 }; | 39 }; |
| 32 | 40 |
| 33 // This class manages the Nigori objects used to encrypt and decrypt sensitive | 41 // This class manages the Nigori objects used to encrypt and decrypt sensitive |
| 34 // sync data (eg. passwords). Each Nigori object knows how to handle data | 42 // sync data (eg. passwords). Each Nigori object knows how to handle data |
| 35 // protected with a particular passphrase. | 43 // protected with a particular passphrase. |
| 36 // | 44 // |
| 37 // Whenever an update to the Nigori sync node is received from the server, | 45 // Whenever an update to the Nigori sync node is received from the server, |
| 38 // SetPendingKeys should be called with the encrypted contents of that node. | 46 // SetPendingKeys should be called with the encrypted contents of that node. |
| 39 // Most likely, an updated Nigori node means that a new passphrase has been set | 47 // Most likely, an updated Nigori node means that a new passphrase has been set |
| 40 // and that future node updates won't be decryptable. To remedy this, the user | 48 // and that future node updates won't be decryptable. To remedy this, the user |
| 41 // should be prompted for the new passphrase and DecryptPendingKeys be called. | 49 // should be prompted for the new passphrase and DecryptPendingKeys be called. |
| 42 // | 50 // |
| 43 // Whenever a update to an encrypted node is received from the server, | 51 // Whenever a update to an encrypted node is received from the server, |
| 44 // CanDecrypt should be used to verify whether the Cryptographer can decrypt | 52 // CanDecrypt should be used to verify whether the Cryptographer can decrypt |
| 45 // that node. If it cannot, then the application of that update should be | 53 // that node. If it cannot, then the application of that update should be |
| 46 // delayed until after it can be decrypted. | 54 // delayed until after it can be decrypted. |
| 47 class Cryptographer { | 55 class Cryptographer { |
| 48 public: | 56 public: |
| 49 // All Observer methods are done synchronously, so they're called | |
| 50 // under a transaction (since all Cryptographer operations are done | |
| 51 // under a transaction). | |
| 52 class Observer { | |
| 53 public: | |
| 54 // Called when the set of encrypted types or the encrypt | |
| 55 // everything flag has been changed. Note that this doesn't | |
| 56 // necessarily mean that encryption has completed for the given | |
| 57 // types. | |
| 58 // | |
| 59 // |encrypted_types| will always be a superset of | |
| 60 // SensitiveTypes(). If |encrypt_everything| is true, | |
| 61 // |encrypted_types| will be the set of all known types. | |
| 62 // | |
| 63 // Until this function is called, observers can assume that the | |
| 64 // set of encrypted types is SensitiveTypes() and that the encrypt | |
| 65 // everything flag is false. | |
| 66 virtual void OnEncryptedTypesChanged( | |
| 67 ModelTypeSet encrypted_types, | |
| 68 bool encrypt_everything) = 0; | |
| 69 | |
| 70 protected: | |
| 71 virtual ~Observer(); | |
| 72 }; | |
| 73 | |
| 74 // Does not take ownership of |encryptor|. | 57 // Does not take ownership of |encryptor|. |
| 75 explicit Cryptographer(Encryptor* encryptor); | 58 explicit Cryptographer(Encryptor* encryptor); |
| 76 ~Cryptographer(); | 59 virtual ~Cryptographer(); |
| 77 | 60 |
| 78 // When update on cryptographer is called this enum tells if the | 61 // TODO(zea): refactor so that Cryptographer doesn't need any connection |
| 79 // cryptographer was succesfully able to update using the nigori node or if | 62 // to the SyncEncryptionHandler. |
| 80 // it needs a key to decrypt the nigori node. | 63 void SetSyncEncryptionHandlerDelegate(SyncEncryptionHandler* delegate); |
| 81 enum UpdateResult { | |
| 82 SUCCESS, | |
| 83 NEEDS_PASSPHRASE | |
| 84 }; | |
| 85 | 64 |
| 86 // Manage observers. | 65 // SyncEncryptionProvider delegator methods (passes through to delegate). |
| 87 void AddObserver(Observer* observer); | 66 void UpdateFromNigori(const sync_pb::NigoriSpecifics& nigori) ; |
| 88 void RemoveObserver(Observer* observer); | 67 ModelTypeSet GetEncryptedTypes() const; |
| 68 void UpdateNigoriFromEncryptedTypes(sync_pb::NigoriSpecifics* nigori) const; |
| 89 | 69 |
| 90 // |restored_bootstrap_token| can be provided via this method to bootstrap | 70 // |restored_bootstrap_token| can be provided via this method to bootstrap |
| 91 // Cryptographer instance into the ready state (is_ready will be true). | 71 // Cryptographer instance into the ready state (is_ready will be true). |
| 92 // It must be a string that was previously built by the | 72 // It must be a string that was previously built by the |
| 93 // GetSerializedBootstrapToken function. It is possible that the token is no | 73 // GetSerializedBootstrapToken function. It is possible that the token is no |
| 94 // longer valid (due to server key change), in which case the normal | 74 // longer valid (due to server key change), in which case the normal |
| 95 // decryption code paths will fail and the user will need to provide a new | 75 // decryption code paths will fail and the user will need to provide a new |
| 96 // passphrase. | 76 // passphrase. |
| 97 // It is an error to call this if is_ready() == true, though it is fair to | 77 // It is an error to call this if is_ready() == true, though it is fair to |
| 98 // never call Bootstrap at all. | 78 // never call Bootstrap at all. |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 bool has_pending_keys() const { return NULL != pending_keys_.get(); } | 149 bool has_pending_keys() const { return NULL != pending_keys_.get(); } |
| 170 | 150 |
| 171 // Obtain a token that can be provided on construction to a future | 151 // Obtain a token that can be provided on construction to a future |
| 172 // Cryptographer instance to bootstrap itself. Returns false if such a token | 152 // Cryptographer instance to bootstrap itself. Returns false if such a token |
| 173 // can't be created (i.e. if this Cryptograhper doesn't have valid keys). | 153 // can't be created (i.e. if this Cryptograhper doesn't have valid keys). |
| 174 bool GetBootstrapToken(std::string* token) const; | 154 bool GetBootstrapToken(std::string* token) const; |
| 175 | 155 |
| 176 // Obtain the bootstrap token based on the keystore encryption key. | 156 // Obtain the bootstrap token based on the keystore encryption key. |
| 177 bool GetKeystoreKeyBootstrapToken(std::string* token) const; | 157 bool GetKeystoreKeyBootstrapToken(std::string* token) const; |
| 178 | 158 |
| 179 // Update the cryptographer based on the contents of the nigori specifics. | |
| 180 // This updates both the encryption keys and the set of encrypted types. | |
| 181 // Returns NEEDS_PASSPHRASE if was unable to decrypt the pending keys, | |
| 182 // SUCCESS otherwise. | |
| 183 // Note: will not change the default key. If the nigori's keybag | |
| 184 // is decryptable, all keys are added to the local keybag and the current | |
| 185 // default is preserved. If the nigori's keybag is not decryptable, it is | |
| 186 // stored in the |pending_keys_|. | |
| 187 UpdateResult Update(const sync_pb::NigoriSpecifics& nigori); | |
| 188 | |
| 189 // Set the keystore-derived nigori from the provided key. | 159 // Set the keystore-derived nigori from the provided key. |
| 190 // Returns true if we succesfully create the keystore derived nigori from the | 160 // Returns true if we succesfully create the keystore derived nigori from the |
| 191 // provided key, false otherwise. | 161 // provided key, false otherwise. |
| 192 bool SetKeystoreKey(const std::string& keystore_key); | 162 bool SetKeystoreKey(const std::string& keystore_key); |
| 193 | 163 |
| 194 // Returns true if we currently have a keystore-derived nigori, false | 164 // Returns true if we currently have a keystore-derived nigori, false |
| 195 // otherwise. | 165 // otherwise. |
| 196 bool HasKeystoreKey() const; | 166 bool HasKeystoreKey() const; |
| 197 | 167 |
| 198 // The set of types that are always encrypted. | 168 Encryptor* encryptor() const { return encryptor_; } |
| 199 static ModelTypeSet SensitiveTypes(); | |
| 200 | |
| 201 // Reset our set of encrypted types based on the contents of the nigori | |
| 202 // specifics. | |
| 203 void UpdateEncryptedTypesFromNigori(const sync_pb::NigoriSpecifics& nigori); | |
| 204 | |
| 205 // Update the nigori to reflect the current set of encrypted types. | |
| 206 void UpdateNigoriFromEncryptedTypes(sync_pb::NigoriSpecifics* nigori) const; | |
| 207 | |
| 208 // Setter/getter for whether all current and future datatypes should | |
| 209 // be encrypted. Once set you cannot unset without reading from a | |
| 210 // new nigori node. set_encrypt_everything() emits a notification | |
| 211 // the first time it's called. | |
| 212 void set_encrypt_everything(); | |
| 213 bool encrypt_everything() const; | |
| 214 | |
| 215 // Return the set of encrypted types. | |
| 216 ModelTypeSet GetEncryptedTypes() const; | |
| 217 | |
| 218 // Forwards to MergeEncryptedTypes. | |
| 219 void MergeEncryptedTypesForTest(ModelTypeSet encrypted_types); | |
| 220 | 169 |
| 221 private: | 170 private: |
| 222 FRIEND_TEST_ALL_PREFIXES(SyncCryptographerTest, PackUnpack); | 171 FRIEND_TEST_ALL_PREFIXES(SyncCryptographerTest, PackUnpack); |
| 172 friend class SyncEncryptionHandlerImpl; |
| 173 friend class FakeSyncEncryptionHandler; |
| 174 |
| 223 typedef std::map<std::string, linked_ptr<const Nigori> > NigoriMap; | 175 typedef std::map<std::string, linked_ptr<const Nigori> > NigoriMap; |
| 224 | 176 |
| 225 // Merges the given set of encrypted types with the existing set and emits a | |
| 226 // notification if necessary. | |
| 227 void MergeEncryptedTypes(ModelTypeSet encrypted_types); | |
| 228 | |
| 229 void EmitEncryptedTypesChangedNotification(); | |
| 230 | |
| 231 // Decrypts |encrypted| and uses its contents to initialize Nigori instances. | 177 // Decrypts |encrypted| and uses its contents to initialize Nigori instances. |
| 232 // Returns true unless decryption of |encrypted| fails. The caller is | 178 // Returns true unless decryption of |encrypted| fails. The caller is |
| 233 // responsible for checking that CanDecrypt(encrypted) == true. | 179 // responsible for checking that CanDecrypt(encrypted) == true. |
| 234 // Does not update the default nigori. | 180 // Does not update the default nigori. |
| 235 void InstallKeys(const sync_pb::EncryptedData& encrypted); | 181 void InstallKeys(const sync_pb::EncryptedData& encrypted); |
| 236 | 182 |
| 183 // Sets the default key to the nigori with name |key_name|. |key_name| must |
| 184 // correspond to a nigori that has already been installed into the keybag. |
| 185 void SetDefaultKey(std::string key_name); |
| 186 |
| 237 // Helper method to instantiate Nigori instances for each set of key | 187 // Helper method to instantiate Nigori instances for each set of key |
| 238 // parameters in |bag|. | 188 // parameters in |bag|. |
| 239 // Does not update the default nigori. | 189 // Does not update the default nigori. |
| 240 void InstallKeyBag(const sync_pb::NigoriKeyBag& bag); | 190 void InstallKeyBag(const sync_pb::NigoriKeyBag& bag); |
| 241 | 191 |
| 242 // Helper method to add a nigori as either the new default nigori or the new | 192 // Helper method to add a nigori as either the new default nigori or the new |
| 243 // keystore nigori. | 193 // keystore nigori. |
| 244 bool AddKeyImpl(Nigori* nigori, bool is_keystore_key); | 194 bool AddKeyImpl(Nigori* nigori, bool is_keystore_key); |
| 245 | 195 |
| 246 // Functions to serialize + encrypt a Nigori object in an opaque format for | 196 // Functions to serialize + encrypt a Nigori object in an opaque format for |
| 247 // persistence by sync infrastructure. | 197 // persistence by sync infrastructure. |
| 248 bool PackBootstrapToken(const Nigori* nigori, std::string* pack_into) const; | 198 bool PackBootstrapToken(const Nigori* nigori, std::string* pack_into) const; |
| 249 Nigori* UnpackBootstrapToken(const std::string& token) const; | 199 Nigori* UnpackBootstrapToken(const std::string& token) const; |
| 250 | 200 |
| 251 Encryptor* const encryptor_; | 201 Encryptor* const encryptor_; |
| 252 | 202 |
| 253 ObserverList<Observer> observers_; | |
| 254 | |
| 255 NigoriMap nigoris_; // The Nigoris we know about, mapped by key name. | 203 NigoriMap nigoris_; // The Nigoris we know about, mapped by key name. |
| 256 NigoriMap::value_type* default_nigori_; // The Nigori used for encryption. | 204 NigoriMap::value_type* default_nigori_; // The Nigori used for encryption. |
| 257 NigoriMap::value_type* keystore_nigori_; // Nigori generated from keystore. | 205 NigoriMap::value_type* keystore_nigori_; // Nigori generated from keystore. |
| 258 | 206 |
| 259 scoped_ptr<sync_pb::EncryptedData> pending_keys_; | 207 scoped_ptr<sync_pb::EncryptedData> pending_keys_; |
| 260 | 208 |
| 261 ModelTypeSet encrypted_types_; | 209 // The actual sync encryption provider. |
| 262 bool encrypt_everything_; | 210 SyncEncryptionHandler* sync_encryption_delegate_; |
| 263 | 211 |
| 264 DISALLOW_COPY_AND_ASSIGN(Cryptographer); | 212 DISALLOW_COPY_AND_ASSIGN(Cryptographer); |
| 265 }; | 213 }; |
| 266 | 214 |
| 267 } // namespace syncer | 215 } // namespace syncer |
| 268 | 216 |
| 269 #endif // SYNC_UTIL_CRYPTOGRAPHER_H_ | 217 #endif // SYNC_UTIL_CRYPTOGRAPHER_H_ |
| OLD | NEW |