Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 #ifndef SYNC_INTERNAL_API_SYNC_ENCRYPTION_HANDLER_IMPL_H_ | |
| 6 #define SYNC_INTERNAL_API_SYNC_ENCRYPTION_HANDLER_IMPL_H_ | |
| 7 | |
| 8 #include <string> | |
| 9 | |
| 10 #include "base/compiler_specific.h" | |
| 11 #include "base/gtest_prod_util.h" | |
| 12 #include "base/memory/scoped_ptr.h" | |
| 13 #include "base/memory/weak_ptr.h" | |
| 14 #include "base/observer_list.h" | |
| 15 #include "sync/internal_api/public/sync_encryption_handler.h" | |
| 16 #include "sync/syncable/nigori_handler.h" | |
| 17 | |
| 18 namespace syncer { | |
| 19 | |
| 20 struct UserShare; | |
| 21 class WriteNode; | |
| 22 class WriteTransaction; | |
| 23 | |
| 24 // TODO(zea): Make this class explicitly non-thread safe and ensure its only | |
| 25 // accessed from the sync thread, with the possible exception of | |
| 26 // GetEncryptedTypes. Need to cache explicit passphrase state on the UI thread. | |
|
tim (not reviewing)
2012/08/14 02:32:20
I think it would be useful to have a class comment
Nicolas Zea
2012/08/14 23:24:51
Done.
| |
| 27 class SyncEncryptionHandlerImpl | |
| 28 : public SyncEncryptionHandler, | |
| 29 public syncable::NigoriHandler { | |
| 30 public: | |
| 31 SyncEncryptionHandlerImpl(UserShare* user_share, | |
| 32 Cryptographer* cryptographer); | |
| 33 virtual ~SyncEncryptionHandlerImpl(); | |
| 34 | |
| 35 // SyncEncryptionHandler implementation. | |
| 36 virtual void AddObserver(Observer* observer) OVERRIDE; | |
| 37 virtual void RemoveObserver(Observer* observer) OVERRIDE; | |
| 38 virtual void ReloadNigori() OVERRIDE; | |
| 39 virtual void SetEncryptionPassphrase(const std::string& passphrase, | |
| 40 bool is_explicit) OVERRIDE; | |
| 41 virtual void SetDecryptionPassphrase(const std::string& passphrase) OVERRIDE; | |
| 42 virtual void EnableEncryptEverything() OVERRIDE; | |
| 43 virtual bool EncryptEverythingEnabled() const OVERRIDE; | |
| 44 virtual bool IsUsingExplicitPassphrase() const OVERRIDE; | |
| 45 | |
| 46 // NigoriHandler implementation. | |
| 47 // Note: all methods are invoked while the caller holds a transaction. | |
| 48 virtual void UpdateFromNigori( | |
| 49 const sync_pb::NigoriSpecifics& nigori) OVERRIDE; | |
| 50 virtual void UpdateNigoriFromEncryptedTypes( | |
| 51 sync_pb::NigoriSpecifics* nigori) const OVERRIDE; | |
| 52 virtual ModelTypeSet GetEncryptedTypes() const OVERRIDE; | |
| 53 | |
| 54 private: | |
| 55 DISALLOW_COPY_AND_ASSIGN(SyncEncryptionHandlerImpl); | |
|
tim (not reviewing)
2012/08/14 02:32:20
nit - put this at the bottom.
Nicolas Zea
2012/08/14 23:24:51
Done.
| |
| 56 FRIEND_TEST_ALL_PREFIXES(SyncEncryptionHandlerImplTest, | |
| 57 NigoriEncryptionTypes); | |
| 58 FRIEND_TEST_ALL_PREFIXES(SyncEncryptionHandlerImplTest, | |
| 59 EncryptEverythingExplicit); | |
| 60 FRIEND_TEST_ALL_PREFIXES(SyncEncryptionHandlerImplTest, | |
| 61 EncryptEverythingImplicit); | |
| 62 FRIEND_TEST_ALL_PREFIXES(SyncEncryptionHandlerImplTest, | |
| 63 UnknownSensitiveTypes); | |
| 64 | |
| 65 // Iterate over all encrypted types ensuring each entry is properly encrypted. | |
| 66 void ReEncryptEverything(WriteTransaction* trans); | |
| 67 | |
| 68 // Apply a nigori update. Updates internal and cryptographer state. | |
| 69 // Returns true on success, false if |nigori| was incompatible, and the | |
| 70 // nigori node must be corrected. | |
| 71 // Note: must be called from within a transaction. | |
| 72 bool ApplyNigoriUpdate(const sync_pb::NigoriSpecifics& nigori, | |
| 73 Cryptographer* cryptographer); | |
| 74 | |
| 75 // Wrapper around WriteEncryptionStateToNigori that creates a new write | |
| 76 // transaction. | |
| 77 void RewriteNigori(); | |
| 78 | |
| 79 // Write the current encryption state into the nigori node. This includes | |
| 80 // the encrypted types/encrypt everything state, as well as the keybag/ | |
| 81 // explicit passphrase state (if the cryptographer is ready). | |
| 82 void WriteEncryptionStateToNigori(WriteTransaction* trans); | |
| 83 | |
| 84 // Updates local encrypted types from |nigori|. | |
| 85 // Returns true if the local set of encrypted types either matched or was | |
| 86 // a subset of that in |nigori|. Returns false if the local state already | |
| 87 // had stricter encryption than |nigori|, and the nigori node needs to be | |
| 88 // updated with the newer encryption state. | |
| 89 // Note: must be called from within a transaction. | |
| 90 bool UpdateEncryptedTypesFromNigori(const sync_pb::NigoriSpecifics& nigori); | |
| 91 | |
| 92 // The final step of SetEncryptionPassphrase and SetDecryptionPassphrase that | |
| 93 // notifies observers of the result of the set passphrase operation, updates | |
| 94 // the nigori node, and does re-encryption. | |
| 95 // |success|: true if the operation was successful and false otherwise. If | |
| 96 // success == false, we send an OnPassphraseRequired notification. | |
| 97 // |bootstrap_token|: used to inform observers if the cryptographer's | |
| 98 // bootstrap token was updated. | |
| 99 // |is_explicit|: used to differentiate between a custom passphrase (true) and | |
| 100 // a GAIA passphrase that is implicitly used for encryption | |
| 101 // (false). | |
| 102 // |trans| and |nigori_node|: used to access data in the cryptographer. | |
| 103 void FinishSetPassphrase(bool success, | |
| 104 const std::string& bootstrap_token, | |
| 105 bool is_explicit, | |
| 106 WriteTransaction* trans, | |
| 107 WriteNode* nigori_node); | |
| 108 | |
| 109 // Merges the given set of encrypted types with the existing set and emits a | |
| 110 // notification if necessary. | |
| 111 // Note: must be called from within a transaction. | |
| 112 void MergeEncryptedTypes(ModelTypeSet encrypted_types); | |
| 113 | |
| 114 base::WeakPtrFactory<SyncEncryptionHandlerImpl> weak_ptr_factory_; | |
| 115 | |
| 116 ObserverList<SyncEncryptionHandler::Observer> observers_; | |
| 117 | |
| 118 // The current user share (for creating transactions). | |
| 119 UserShare* user_share_; | |
| 120 | |
| 121 // TODO(zea): have the sync encryption handler own the cryptographer, and live | |
| 122 // in the directory. | |
| 123 Cryptographer* cryptographer_; | |
| 124 | |
| 125 // The set of types that require encryption. This is accessed on all sync | |
| 126 // datatype threads when we write to a node, so we must hold a transaction | |
| 127 // whenever we touch/read it. | |
| 128 ModelTypeSet encrypted_types_; | |
| 129 | |
| 130 // Sync encryption state. These are only modified and accessed from the sync | |
| 131 // thread. | |
| 132 bool encrypt_everything_; | |
| 133 bool explicit_passphrase_; | |
| 134 | |
| 135 // The number of times we've automatically (i.e. not via SetPassphrase or | |
| 136 // conflict resolver) updated the nigori's encryption keys in this chrome | |
| 137 // instantiation. | |
| 138 int nigori_overwrite_count_; | |
| 139 }; | |
| 140 | |
| 141 } // namespace syncer | |
| 142 | |
| 143 #endif // SYNC_INTERNAL_API_PUBLIC_SYNC_ENCRYPTION_HANDLER_IMPL_H_ | |
| OLD | NEW |