Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 COMPONENTS_GCM_DRIVER_CRYPTO_GCM_KEY_STORE_H_ | 5 #ifndef COMPONENTS_GCM_DRIVER_CRYPTO_GCM_KEY_STORE_H_ |
| 6 #define COMPONENTS_GCM_DRIVER_CRYPTO_GCM_KEY_STORE_H_ | 6 #define COMPONENTS_GCM_DRIVER_CRYPTO_GCM_KEY_STORE_H_ |
| 7 | 7 |
| 8 #include <map> | |
| 9 #include <memory> | 8 #include <memory> |
| 10 #include <string> | 9 #include <string> |
| 10 #include <unordered_map> | |
| 11 #include <utility> | |
| 11 #include <vector> | 12 #include <vector> |
| 12 | 13 |
| 13 #include "base/callback_forward.h" | 14 #include "base/callback_forward.h" |
| 14 #include "base/files/file_path.h" | 15 #include "base/files/file_path.h" |
| 15 #include "base/macros.h" | 16 #include "base/macros.h" |
| 16 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
| 17 #include "base/memory/weak_ptr.h" | 18 #include "base/memory/weak_ptr.h" |
| 18 #include "components/gcm_driver/crypto/proto/gcm_encryption_data.pb.h" | 19 #include "components/gcm_driver/crypto/proto/gcm_encryption_data.pb.h" |
| 19 #include "components/gcm_driver/gcm_delayed_task_controller.h" | 20 #include "components/gcm_driver/gcm_delayed_task_controller.h" |
| 20 | 21 |
| 21 namespace base { | 22 namespace base { |
| 22 class SequencedTaskRunner; | 23 class SequencedTaskRunner; |
| 23 } | 24 } |
| 24 | 25 |
| 25 namespace leveldb_proto { | 26 namespace leveldb_proto { |
| 26 template <typename T> | 27 template <typename T> |
| 27 class ProtoDatabase; | 28 class ProtoDatabase; |
| 28 } | 29 } |
| 29 | 30 |
| 30 namespace gcm { | 31 namespace gcm { |
| 31 | 32 |
| 32 // Key storage for use with encrypted messages received from Google Cloud | 33 // Key storage for use with encrypted messages received from Google Cloud |
| 33 // Messaging. It provides the ability to create and store a key-pair for a given | 34 // Messaging. It provides the ability to create and store a key-pair for a given |
| 34 // app id, as well as retrieving and deleting key-pairs. | 35 // app id + authorized entity pair, and to retrieve and delete key-pairs. |
| 35 // | 36 // |
| 36 // This class is backed by a proto database and might end up doing file I/O on | 37 // This class is backed by a proto database and might end up doing file I/O on |
| 37 // a background task runner. For this reason, all public APIs take a callback | 38 // a background task runner. For this reason, all public APIs take a callback |
| 38 // rather than returning the result. Do not rely on the timing of the callbacks. | 39 // rather than returning the result. Do not rely on the timing of the callbacks. |
| 39 class GCMKeyStore { | 40 class GCMKeyStore { |
| 40 public: | 41 public: |
| 41 using KeysCallback = base::Callback<void(const KeyPair& pair, | 42 using KeysCallback = base::Callback<void(const KeyPair& pair, |
| 42 const std::string& auth_secret)>; | 43 const std::string& auth_secret)>; |
| 43 | 44 |
| 44 GCMKeyStore( | 45 GCMKeyStore( |
| 45 const base::FilePath& key_store_path, | 46 const base::FilePath& key_store_path, |
| 46 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner); | 47 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner); |
| 47 ~GCMKeyStore(); | 48 ~GCMKeyStore(); |
| 48 | 49 |
| 49 // Retrieves the public/private key-pair associated with |app_id|, and | 50 // Retrieves the public/private key-pair associated with the |app_id| + |
| 50 // invokes |callback| when they are available, or when an error occurred. | 51 // authorized entity pair, and invokes |callback| when they are available, or |
| 51 void GetKeys(const std::string& app_id, const KeysCallback& callback); | 52 // when an error occurred. |
| 53 // |instance_id_authorized_entity|: pass InstanceID token's authorized_entity | |
| 54 // or "" for legacy GCM registrations. | |
| 55 // |fallback_to_empty_authorized_entity|: if true and the keys are not found, | |
| 56 // will try again with empty authorized | |
| 57 // entity (use this when you're not | |
| 58 // sure if you have an Instance ID). | |
| 59 void GetKeys(const std::string& app_id, | |
| 60 const std::string& instance_id_authorized_entity, | |
| 61 bool fallback_to_empty_authorized_entity, | |
| 62 const KeysCallback& callback); | |
| 52 | 63 |
| 53 // Creates a new public/private key-pair for |app_id|, and invokes | 64 // Creates a new public/private key-pair for the |app_id| + authorized entity |
| 54 // |callback| when they are available, or when an error occurred. | 65 // pair, and invokes |callback| when they are available, or when an error |
| 55 void CreateKeys(const std::string& app_id, const KeysCallback& callback); | 66 // occurred. Simultaneously using the same |app_id| for both a legacy GCM |
| 67 // registration and one or more InstanceID tokens is not supported. | |
| 68 // |instance_id_authorized_entity|: pass InstanceID token's authorized_entity | |
| 69 // or "" for legacy GCM registrations. | |
| 70 void CreateKeys(const std::string& app_id, | |
| 71 const std::string& instance_id_authorized_entity, | |
| 72 const KeysCallback& callback); | |
| 56 | 73 |
| 57 // Removes the keys associated with |app_id|, and invokes |callback| when | 74 // Removes the keys associated with the |app_id| + authorized entity pair, and |
| 58 // the operation has finished. | 75 // invokes |callback| when the operation has finished. |
| 59 void RemoveKeys(const std::string& app_id, const base::Closure& callback); | 76 // |instance_id_authorized_entity|: pass InstanceID token's authorized_entity |
| 77 // or "*" to remove for all InstanceID tokens | |
| 78 // or "" for legacy GCM registrations. | |
| 79 void RemoveKeys(const std::string& app_id, | |
| 80 const std::string& instance_id_authorized_entity, | |
| 81 const base::Closure& callback); | |
| 60 | 82 |
| 61 private: | 83 private: |
| 62 // Initializes the database if necessary, and runs |done_closure| when done. | 84 // Initializes the database if necessary, and runs |done_closure| when done. |
| 63 void LazyInitialize(const base::Closure& done_closure); | 85 void LazyInitialize(const base::Closure& done_closure); |
| 64 | 86 |
| 65 void DidInitialize(bool success); | 87 void DidInitialize(bool success); |
| 66 void DidLoadKeys(bool success, | 88 void DidLoadKeys(bool success, |
| 67 std::unique_ptr<std::vector<EncryptionData>> entries); | 89 std::unique_ptr<std::vector<EncryptionData>> entries); |
| 68 | 90 |
| 69 void DidStoreKeys(const KeyPair& pair, | 91 void DidStoreKeys(const KeyPair& pair, |
| 70 const std::string& auth_secret, | 92 const std::string& auth_secret, |
| 71 const KeysCallback& callback, | 93 const KeysCallback& callback, |
| 72 bool success); | 94 bool success); |
| 73 | 95 |
| 74 void DidRemoveKeys(const base::Closure& callback, bool success); | 96 void DidRemoveKeys(const base::Closure& callback, bool success); |
| 75 | 97 |
| 76 // Private implementations of the API that will be executed when the database | 98 // Private implementations of the API that will be executed when the database |
| 77 // has either been successfully loaded, or failed to load. | 99 // has either been successfully loaded, or failed to load. |
| 78 | 100 |
| 79 void GetKeysAfterInitialize(const std::string& app_id, | 101 void GetKeysAfterInitialize(const std::string& app_id, |
| 102 const std::string& instance_id_authorized_entity, | |
| 103 bool fallback_to_empty_authorized_entity, | |
| 80 const KeysCallback& callback); | 104 const KeysCallback& callback); |
| 81 void CreateKeysAfterInitialize(const std::string& app_id, | 105 void CreateKeysAfterInitialize( |
| 82 const KeysCallback& callback); | 106 const std::string& app_id, |
| 83 void RemoveKeysAfterInitialize(const std::string& app_id, | 107 const std::string& instance_id_authorized_entity, |
| 84 const base::Closure& callback); | 108 const KeysCallback& callback); |
| 109 void RemoveKeysAfterInitialize( | |
| 110 const std::string& app_id, | |
| 111 const std::string& instance_id_authorized_entity, | |
| 112 const base::Closure& callback); | |
| 85 | 113 |
| 86 // Path in which the key store database will be saved. | 114 // Path in which the key store database will be saved. |
| 87 base::FilePath key_store_path_; | 115 base::FilePath key_store_path_; |
| 88 | 116 |
| 89 // Blocking task runner which the database will do I/O operations on. | 117 // Blocking task runner which the database will do I/O operations on. |
| 90 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; | 118 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; |
| 91 | 119 |
| 92 // Instance of the ProtoDatabase backing the key store. | 120 // Instance of the ProtoDatabase backing the key store. |
| 93 std::unique_ptr<leveldb_proto::ProtoDatabase<EncryptionData>> database_; | 121 std::unique_ptr<leveldb_proto::ProtoDatabase<EncryptionData>> database_; |
| 94 | 122 |
| 95 enum class State; | 123 enum class State; |
| 96 | 124 |
| 97 // The current state of the database. It has to be initialized before use. | 125 // The current state of the database. It has to be initialized before use. |
| 98 State state_; | 126 State state_; |
| 99 | 127 |
| 100 // Controller for tasks that should be executed once the key store has | 128 // Controller for tasks that should be executed once the key store has |
| 101 // finished initializing. | 129 // finished initializing. |
| 102 GCMDelayedTaskController delayed_task_controller_; | 130 GCMDelayedTaskController delayed_task_controller_; |
| 103 | 131 |
| 104 // Mapping of an app id to the loaded key pair and authentication secrets. | 132 // Nested map from app_id to a map from instance_id_authorized_entity to the |
| 105 // TODO(peter): Switch these to std::unordered_map<> once allowed. | 133 // loaded key pair and authentication secrets. |
| 106 std::map<std::string, KeyPair> key_pairs_; | 134 using KeyPairAndAuthSecret = std::pair<KeyPair, std::string>; |
| 107 std::map<std::string, std::string> auth_secrets_; | 135 std::unordered_map<std::string, |
|
Peter Beverloo
2016/05/09 14:10:10
Why can't we use a multimap from |app_id| to a tup
johnme
2016/05/09 18:15:55
I considered that, but I'm not convinced a multima
| |
| 136 std::unordered_map<std::string, KeyPairAndAuthSecret>> | |
| 137 key_data_; | |
| 108 | 138 |
| 109 base::WeakPtrFactory<GCMKeyStore> weak_factory_; | 139 base::WeakPtrFactory<GCMKeyStore> weak_factory_; |
| 110 | 140 |
| 111 DISALLOW_COPY_AND_ASSIGN(GCMKeyStore); | 141 DISALLOW_COPY_AND_ASSIGN(GCMKeyStore); |
| 112 }; | 142 }; |
| 113 | 143 |
| 114 } // namespace gcm | 144 } // namespace gcm |
| 115 | 145 |
| 116 #endif // COMPONENTS_GCM_DRIVER_CRYPTO_GCM_KEY_STORE_H_ | 146 #endif // COMPONENTS_GCM_DRIVER_CRYPTO_GCM_KEY_STORE_H_ |
| OLD | NEW |