| 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, |
| 51 void GetKeys(const std::string& app_id, const KeysCallback& callback); | 52 // or when an error occurred. |authorized_entity| should be the InstanceID |
| 53 // token's authorized entity, or "" for non-InstanceID GCM registrations. If |
| 54 // |fallback_to_empty_authorized_entity| is true and the keys are not found, |
| 55 // GetKeys will try again with an empty authorized entity; this can be used |
| 56 // when it's not known whether or not the |app_id| is for an InstanceID. |
| 57 void GetKeys(const std::string& app_id, |
| 58 const std::string& authorized_entity, |
| 59 bool fallback_to_empty_authorized_entity, |
| 60 const KeysCallback& callback); |
| 52 | 61 |
| 53 // Creates a new public/private key-pair for |app_id|, and invokes | 62 // Creates a new public/private key-pair for the |app_id| + |
| 54 // |callback| when they are available, or when an error occurred. | 63 // |authorized_entity| pair, and invokes |callback| when they are available, |
| 55 void CreateKeys(const std::string& app_id, const KeysCallback& callback); | 64 // or when an error occurred. |authorized_entity| should be the InstanceID |
| 65 // token's authorized entity, or "" for non-InstanceID GCM registrations. |
| 66 // Simultaneously using the same |app_id| for both a non-InstanceID GCM |
| 67 // registration and one or more InstanceID tokens is not supported. |
| 68 void CreateKeys(const std::string& app_id, |
| 69 const std::string& authorized_entity, |
| 70 const KeysCallback& callback); |
| 56 | 71 |
| 57 // Removes the keys associated with |app_id|, and invokes |callback| when | 72 // Removes the keys associated with the |app_id| + |authorized_entity| pair, |
| 58 // the operation has finished. | 73 // and invokes |callback| when the operation has finished. |authorized_entity| |
| 59 void RemoveKeys(const std::string& app_id, const base::Closure& callback); | 74 // should be the InstanceID token's authorized entity, or "*" to remove for |
| 75 // all InstanceID tokens, or "" for non-InstanceID GCM registrations. |
| 76 void RemoveKeys(const std::string& app_id, |
| 77 const std::string& authorized_entity, |
| 78 const base::Closure& callback); |
| 60 | 79 |
| 61 private: | 80 private: |
| 62 // Initializes the database if necessary, and runs |done_closure| when done. | 81 // Initializes the database if necessary, and runs |done_closure| when done. |
| 63 void LazyInitialize(const base::Closure& done_closure); | 82 void LazyInitialize(const base::Closure& done_closure); |
| 64 | 83 |
| 65 void DidInitialize(bool success); | 84 void DidInitialize(bool success); |
| 66 void DidLoadKeys(bool success, | 85 void DidLoadKeys(bool success, |
| 67 std::unique_ptr<std::vector<EncryptionData>> entries); | 86 std::unique_ptr<std::vector<EncryptionData>> entries); |
| 68 | 87 |
| 69 void DidStoreKeys(const KeyPair& pair, | 88 void DidStoreKeys(const KeyPair& pair, |
| 70 const std::string& auth_secret, | 89 const std::string& auth_secret, |
| 71 const KeysCallback& callback, | 90 const KeysCallback& callback, |
| 72 bool success); | 91 bool success); |
| 73 | 92 |
| 74 void DidRemoveKeys(const base::Closure& callback, bool success); | 93 void DidRemoveKeys(const base::Closure& callback, bool success); |
| 75 | 94 |
| 76 // Private implementations of the API that will be executed when the database | 95 // Private implementations of the API that will be executed when the database |
| 77 // has either been successfully loaded, or failed to load. | 96 // has either been successfully loaded, or failed to load. |
| 78 | 97 |
| 79 void GetKeysAfterInitialize(const std::string& app_id, | 98 void GetKeysAfterInitialize(const std::string& app_id, |
| 99 const std::string& authorized_entity, |
| 100 bool fallback_to_empty_authorized_entity, |
| 80 const KeysCallback& callback); | 101 const KeysCallback& callback); |
| 81 void CreateKeysAfterInitialize(const std::string& app_id, | 102 void CreateKeysAfterInitialize(const std::string& app_id, |
| 103 const std::string& authorized_entity, |
| 82 const KeysCallback& callback); | 104 const KeysCallback& callback); |
| 83 void RemoveKeysAfterInitialize(const std::string& app_id, | 105 void RemoveKeysAfterInitialize(const std::string& app_id, |
| 106 const std::string& authorized_entity, |
| 84 const base::Closure& callback); | 107 const base::Closure& callback); |
| 85 | 108 |
| 86 // Path in which the key store database will be saved. | 109 // Path in which the key store database will be saved. |
| 87 base::FilePath key_store_path_; | 110 base::FilePath key_store_path_; |
| 88 | 111 |
| 89 // Blocking task runner which the database will do I/O operations on. | 112 // Blocking task runner which the database will do I/O operations on. |
| 90 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; | 113 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; |
| 91 | 114 |
| 92 // Instance of the ProtoDatabase backing the key store. | 115 // Instance of the ProtoDatabase backing the key store. |
| 93 std::unique_ptr<leveldb_proto::ProtoDatabase<EncryptionData>> database_; | 116 std::unique_ptr<leveldb_proto::ProtoDatabase<EncryptionData>> database_; |
| 94 | 117 |
| 95 enum class State; | 118 enum class State; |
| 96 | 119 |
| 97 // The current state of the database. It has to be initialized before use. | 120 // The current state of the database. It has to be initialized before use. |
| 98 State state_; | 121 State state_; |
| 99 | 122 |
| 100 // Controller for tasks that should be executed once the key store has | 123 // Controller for tasks that should be executed once the key store has |
| 101 // finished initializing. | 124 // finished initializing. |
| 102 GCMDelayedTaskController delayed_task_controller_; | 125 GCMDelayedTaskController delayed_task_controller_; |
| 103 | 126 |
| 104 // Mapping of an app id to the loaded key pair and authentication secrets. | 127 // Nested map from app_id to a map from authorized_entity to the loaded key |
| 105 // TODO(peter): Switch these to std::unordered_map<> once allowed. | 128 // pair and authentication secrets. |
| 106 std::map<std::string, KeyPair> key_pairs_; | 129 using KeyPairAndAuthSecret = std::pair<KeyPair, std::string>; |
| 107 std::map<std::string, std::string> auth_secrets_; | 130 std::unordered_map<std::string, |
| 131 std::unordered_map<std::string, KeyPairAndAuthSecret>> |
| 132 key_data_; |
| 108 | 133 |
| 109 base::WeakPtrFactory<GCMKeyStore> weak_factory_; | 134 base::WeakPtrFactory<GCMKeyStore> weak_factory_; |
| 110 | 135 |
| 111 DISALLOW_COPY_AND_ASSIGN(GCMKeyStore); | 136 DISALLOW_COPY_AND_ASSIGN(GCMKeyStore); |
| 112 }; | 137 }; |
| 113 | 138 |
| 114 } // namespace gcm | 139 } // namespace gcm |
| 115 | 140 |
| 116 #endif // COMPONENTS_GCM_DRIVER_CRYPTO_GCM_KEY_STORE_H_ | 141 #endif // COMPONENTS_GCM_DRIVER_CRYPTO_GCM_KEY_STORE_H_ |
| OLD | NEW |