| 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_PUBLIC_SYNC_ENCRYPTION_HANDLER_H |
| 6 #define SYNC_INTERNAL_API_PUBLIC_SYNC_ENCRYPTION_HANDLER_H |
| 7 |
| 8 #include <string> |
| 9 |
| 10 #include "sync/internal_api/public/base/model_type.h" |
| 11 |
| 12 namespace sync_pb { |
| 13 class EncryptedData; |
| 14 class NigoriSpecifics; |
| 15 } |
| 16 |
| 17 namespace syncer { |
| 18 |
| 19 class Cryptographer; |
| 20 |
| 21 // Reasons due to which Cryptographer might require a passphrase. |
| 22 enum PassphraseRequiredReason { |
| 23 REASON_PASSPHRASE_NOT_REQUIRED = 0, // Initial value. |
| 24 REASON_ENCRYPTION = 1, // The cryptographer requires a |
| 25 // passphrase for its first attempt at |
| 26 // encryption. Happens only during |
| 27 // migration or upgrade. |
| 28 REASON_DECRYPTION = 2, // The cryptographer requires a |
| 29 // passphrase for its first attempt at |
| 30 // decryption. |
| 31 }; |
| 32 |
| 33 // Sync's encryption handler. Handles tracking encrypted types, ensuring the |
| 34 // cryptographer encrypts with the proper key and has the most recent keybag, |
| 35 // and keeps the nigori node up to date. |
| 36 // |
| 37 // TODO(zea): This class should not need to be entirely thread safe. Refactor |
| 38 // how datatypes/sync gets the set of encrypted types and pull that out |
| 39 // into a thread safe class, then make this class explicitly non-thread safe |
| 40 // and ensure its only accessed from the sync thread. As it stands, |
| 41 // GetEncryptedTypes is called from all datatype threads via the cryptographer |
| 42 // cryptographer. |
| 43 class SyncEncryptionHandler { |
| 44 public: |
| 45 // All Observer methods are done synchronously and are assumed to be called |
| 46 // from within a transaction and on the sync thread. |
| 47 class Observer { |
| 48 public: |
| 49 Observer(); |
| 50 |
| 51 // Called when user interaction is required to obtain a valid passphrase. |
| 52 // - If the passphrase is required for encryption, |reason| will be |
| 53 // REASON_ENCRYPTION. |
| 54 // - If the passphrase is required for the decryption of data that has |
| 55 // already been encrypted, |reason| will be REASON_DECRYPTION. |
| 56 // - If the passphrase is required because decryption failed, and a new |
| 57 // passphrase is required, |reason| will be REASON_SET_PASSPHRASE_FAILED. |
| 58 // |
| 59 // |pending_keys| is a copy of the cryptographer's pending keys, that may be |
| 60 // cached by the frontend for subsequent use by the UI. |
| 61 virtual void OnPassphraseRequired( |
| 62 PassphraseRequiredReason reason, |
| 63 const sync_pb::EncryptedData& pending_keys) = 0; |
| 64 // Called when the passphrase provided by the user has been accepted and is |
| 65 // now used to encrypt sync data. |
| 66 |
| 67 virtual void OnPassphraseAccepted() = 0; |
| 68 // |bootstrap_token| is an opaque base64 encoded representation of the key |
| 69 // generated by the current passphrase, and is provided to the observer for |
| 70 // persistence purposes and use in a future initialization of sync (e.g. |
| 71 // after restart). The boostrap token will always be derived from the most |
| 72 // recent GAIA password (for accounts with implicit passphrases), even if |
| 73 // the data is still encrypted with an older GAIA password. For accounts |
| 74 // with explicit passphrases, it will be the most recently seen custom |
| 75 // passphrase. |
| 76 virtual void OnBootstrapTokenUpdated( |
| 77 const std::string& bootstrap_token) = 0; |
| 78 |
| 79 // Called when the set of encrypted types or the encrypt |
| 80 // everything flag has been changed. Note that encryption isn't |
| 81 // complete until the OnEncryptionComplete() notification has been |
| 82 // sent (see below). |
| 83 // |
| 84 // |encrypted_types| will always be a superset of |
| 85 // Cryptographer::SensitiveTypes(). If |encrypt_everything| is |
| 86 // true, |encrypted_types| will be the set of all known types. |
| 87 // |
| 88 // Until this function is called, observers can assume that the |
| 89 // set of encrypted types is Cryptographer::SensitiveTypes() and |
| 90 // that the encrypt everything flag is false. |
| 91 // |
| 92 // Called from within a transaction. |
| 93 virtual void OnEncryptedTypesChanged( |
| 94 ModelTypeSet encrypted_types, |
| 95 bool encrypt_everything) = 0; |
| 96 |
| 97 // Called after we finish encrypting the current set of encrypted |
| 98 // types. |
| 99 // |
| 100 // Called from within a transaction. |
| 101 virtual void OnEncryptionComplete() = 0; |
| 102 |
| 103 // The cryptographer has been updated. Listeners should check that their |
| 104 // own state matches the cryptographer. |
| 105 // Used primarily for debugging. |
| 106 virtual void OnCryptographerStateChanged(Cryptographer* cryptographer) = 0; |
| 107 |
| 108 protected: |
| 109 virtual ~Observer(); |
| 110 }; |
| 111 |
| 112 SyncEncryptionHandler(); |
| 113 virtual ~SyncEncryptionHandler(); |
| 114 |
| 115 // Add/Remove SyncEncryptionHandler::Observer's. |
| 116 // Must be called from sync thread. |
| 117 virtual void AddObserver(Observer* observer) = 0; |
| 118 virtual void RemoveObserver(Observer* observer) = 0; |
| 119 |
| 120 // Reads the nigori node, updates internal state as needed, and, if a |
| 121 // stale/incompatible nigori node is detected, overwrites the existing |
| 122 // nigori node. Upon completion, if the cryptographer is still ready |
| 123 // attempts to re-encrypt all sync data. |
| 124 // Note: This method is expensive (it iterates through all encrypted types), |
| 125 // so should only be used sparingly (e.g. on startup). |
| 126 virtual void ReloadNigori() = 0; |
| 127 |
| 128 // Apply a nigori node update, updating the internal encryption state |
| 129 // accordingly. |
| 130 // TODO(zea): remove this in favor of using the ChangeProcessor interface. |
| 131 virtual void UpdateFromNigori(const sync_pb::NigoriSpecifics& nigori) = 0; |
| 132 |
| 133 // Returns the set of currently encrypted types. |
| 134 // Note: caller must be holding a transaction. See TODO above about |
| 135 // refactoring how this is called. |
| 136 virtual ModelTypeSet GetEncryptedTypes() const = 0; |
| 137 |
| 138 // Store the current encrypt everything/encrypted types state into |nigori|. |
| 139 // TODO(zea): remove this in favor of using a ResolveConflict method via |
| 140 // the ChangeProcessor interface. |
| 141 virtual void UpdateNigoriFromEncryptedTypes( |
| 142 sync_pb::NigoriSpecifics* nigori) const = 0; |
| 143 |
| 144 // Attempts to re-encrypt encrypted data types using the passphrase provided. |
| 145 // Notifies observers of the result of the operation via OnPassphraseAccepted |
| 146 // or OnPassphraseRequired, updates the nigori node, and does re-encryption as |
| 147 // appropriate. If an explicit password has been set previously, we drop |
| 148 // subsequent requests to set a passphrase. If the cryptographer has pending |
| 149 // keys, and a new implicit passphrase is provided, we try decrypting the |
| 150 // pending keys with it, and if that fails, we cache the passphrase for |
| 151 // re-encryption once the pending keys are decrypted. |
| 152 virtual void SetEncryptionPassphrase(const std::string& passphrase, |
| 153 bool is_explicit) = 0; |
| 154 |
| 155 // Provides a passphrase for decrypting the user's existing sync data. |
| 156 // Notifies observers of the result of the operation via OnPassphraseAccepted |
| 157 // or OnPassphraseRequired, updates the nigori node, and does re-encryption as |
| 158 // appropriate if there is a previously cached encryption passphrase. It is an |
| 159 // error to call this when we don't have pending keys. |
| 160 virtual void SetDecryptionPassphrase(const std::string& passphrase) = 0; |
| 161 |
| 162 // Enables encryption of all datatypes. |
| 163 virtual void EnableEncryptEverything() = 0; |
| 164 |
| 165 // Whether encryption of all datatypes is enabled. If false, only sensitive |
| 166 // types are encrypted. |
| 167 virtual bool EncryptEverythingEnabled() const = 0; |
| 168 |
| 169 virtual bool IsUsingExplicitPassphrase() const = 0; |
| 170 |
| 171 // The set of types that are always encrypted. |
| 172 static ModelTypeSet SensitiveTypes(); |
| 173 }; |
| 174 |
| 175 } // namespace syncer |
| 176 |
| 177 #endif // SYNC_INTERNAL_API_PUBLIC_SYNC_ENCRYPTION_HANDLER_H |
| OLD | NEW |