| 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 #include "components/gcm_driver/crypto/gcm_key_store.h" | 5 #include "components/gcm_driver/crypto/gcm_key_store.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 EncryptionData encryption_data; | 106 EncryptionData encryption_data; |
| 107 encryption_data.set_app_id(app_id); | 107 encryption_data.set_app_id(app_id); |
| 108 encryption_data.set_auth_secret(auth_secret); | 108 encryption_data.set_auth_secret(auth_secret); |
| 109 | 109 |
| 110 KeyPair* pair = encryption_data.add_keys(); | 110 KeyPair* pair = encryption_data.add_keys(); |
| 111 pair->set_type(KeyPair::ECDH_P256); | 111 pair->set_type(KeyPair::ECDH_P256); |
| 112 pair->set_private_key(private_key); | 112 pair->set_private_key(private_key); |
| 113 pair->set_public_key_x509(public_key_x509); | 113 pair->set_public_key_x509(public_key_x509); |
| 114 pair->set_public_key(public_key); | 114 pair->set_public_key(public_key); |
| 115 | 115 |
| 116 // Write them immediately to our cache, so subsequent calls to |
| 117 // {Get/Create/Remove}Keys can see them. |
| 118 key_pairs_[app_id] = *pair; |
| 119 auth_secrets_[app_id] = auth_secret; |
| 120 |
| 116 using EntryVectorType = | 121 using EntryVectorType = |
| 117 leveldb_proto::ProtoDatabase<EncryptionData>::KeyEntryVector; | 122 leveldb_proto::ProtoDatabase<EncryptionData>::KeyEntryVector; |
| 118 | 123 |
| 119 std::unique_ptr<EntryVectorType> entries_to_save(new EntryVectorType()); | 124 std::unique_ptr<EntryVectorType> entries_to_save(new EntryVectorType()); |
| 120 std::unique_ptr<std::vector<std::string>> keys_to_remove( | 125 std::unique_ptr<std::vector<std::string>> keys_to_remove( |
| 121 new std::vector<std::string>()); | 126 new std::vector<std::string>()); |
| 122 | 127 |
| 123 entries_to_save->push_back(std::make_pair(app_id, encryption_data)); | 128 entries_to_save->push_back(std::make_pair(app_id, encryption_data)); |
| 124 | 129 |
| 125 database_->UpdateEntries( | 130 database_->UpdateEntries( |
| 126 std::move(entries_to_save), std::move(keys_to_remove), | 131 std::move(entries_to_save), std::move(keys_to_remove), |
| 127 base::Bind(&GCMKeyStore::DidStoreKeys, weak_factory_.GetWeakPtr(), app_id, | 132 base::Bind(&GCMKeyStore::DidStoreKeys, weak_factory_.GetWeakPtr(), *pair, |
| 128 *pair, auth_secret, callback)); | 133 auth_secret, callback)); |
| 129 } | 134 } |
| 130 | 135 |
| 131 void GCMKeyStore::DidStoreKeys(const std::string& app_id, | 136 void GCMKeyStore::DidStoreKeys(const KeyPair& pair, |
| 132 const KeyPair& pair, | |
| 133 const std::string& auth_secret, | 137 const std::string& auth_secret, |
| 134 const KeysCallback& callback, | 138 const KeysCallback& callback, |
| 135 bool success) { | 139 bool success) { |
| 136 UMA_HISTOGRAM_BOOLEAN("GCM.Crypto.CreateKeySuccessRate", success); | 140 UMA_HISTOGRAM_BOOLEAN("GCM.Crypto.CreateKeySuccessRate", success); |
| 137 DCHECK_EQ(0u, key_pairs_.count(app_id)); | |
| 138 | 141 |
| 139 if (!success) { | 142 if (!success) { |
| 140 DVLOG(1) << "Unable to store the created key in the GCM Key Store."; | 143 LOG(ERROR) << "Unable to store the created key in the GCM Key Store."; |
| 144 |
| 145 // Our cache is now inconsistent. Reject requests until restarted. |
| 146 state_ = State::FAILED; |
| 147 |
| 141 callback.Run(KeyPair(), std::string() /* auth_secret */); | 148 callback.Run(KeyPair(), std::string() /* auth_secret */); |
| 142 return; | 149 return; |
| 143 } | 150 } |
| 144 | 151 |
| 145 key_pairs_[app_id] = pair; | 152 callback.Run(pair, auth_secret); |
| 146 auth_secrets_[app_id] = auth_secret; | |
| 147 | |
| 148 callback.Run(key_pairs_[app_id], auth_secret); | |
| 149 } | 153 } |
| 150 | 154 |
| 151 void GCMKeyStore::RemoveKeys(const std::string& app_id, | 155 void GCMKeyStore::RemoveKeys(const std::string& app_id, |
| 152 const base::Closure& callback) { | 156 const base::Closure& callback) { |
| 153 LazyInitialize(base::Bind(&GCMKeyStore::RemoveKeysAfterInitialize, | 157 LazyInitialize(base::Bind(&GCMKeyStore::RemoveKeysAfterInitialize, |
| 154 weak_factory_.GetWeakPtr(), app_id, callback)); | 158 weak_factory_.GetWeakPtr(), app_id, callback)); |
| 155 } | 159 } |
| 156 | 160 |
| 157 void GCMKeyStore::RemoveKeysAfterInitialize(const std::string& app_id, | 161 void GCMKeyStore::RemoveKeysAfterInitialize(const std::string& app_id, |
| 158 const base::Closure& callback) { | 162 const base::Closure& callback) { |
| 159 DCHECK(state_ == State::INITIALIZED || state_ == State::FAILED); | 163 DCHECK(state_ == State::INITIALIZED || state_ == State::FAILED); |
| 160 const auto iter = key_pairs_.find(app_id); | 164 const auto iter = key_pairs_.find(app_id); |
| 161 if (iter == key_pairs_.end() || state_ != State::INITIALIZED) { | 165 if (iter == key_pairs_.end() || state_ != State::INITIALIZED) { |
| 162 callback.Run(); | 166 callback.Run(); |
| 163 return; | 167 return; |
| 164 } | 168 } |
| 165 | 169 |
| 170 // Clear them immediately from our cache, so subsequent calls to |
| 171 // {Get/Create/Remove}Keys don't see them. |
| 172 key_pairs_.erase(app_id); |
| 173 auth_secrets_.erase(app_id); |
| 174 |
| 166 using EntryVectorType = | 175 using EntryVectorType = |
| 167 leveldb_proto::ProtoDatabase<EncryptionData>::KeyEntryVector; | 176 leveldb_proto::ProtoDatabase<EncryptionData>::KeyEntryVector; |
| 168 | 177 |
| 169 std::unique_ptr<EntryVectorType> entries_to_save(new EntryVectorType()); | 178 std::unique_ptr<EntryVectorType> entries_to_save(new EntryVectorType()); |
| 170 std::unique_ptr<std::vector<std::string>> keys_to_remove( | 179 std::unique_ptr<std::vector<std::string>> keys_to_remove( |
| 171 new std::vector<std::string>(1, app_id)); | 180 new std::vector<std::string>(1, app_id)); |
| 172 | 181 |
| 173 database_->UpdateEntries( | 182 database_->UpdateEntries(std::move(entries_to_save), |
| 174 std::move(entries_to_save), std::move(keys_to_remove), | 183 std::move(keys_to_remove), |
| 175 base::Bind(&GCMKeyStore::DidRemoveKeys, weak_factory_.GetWeakPtr(), | 184 base::Bind(&GCMKeyStore::DidRemoveKeys, |
| 176 app_id, callback)); | 185 weak_factory_.GetWeakPtr(), callback)); |
| 177 } | 186 } |
| 178 | 187 |
| 179 void GCMKeyStore::DidRemoveKeys(const std::string& app_id, | 188 void GCMKeyStore::DidRemoveKeys(const base::Closure& callback, bool success) { |
| 180 const base::Closure& callback, | |
| 181 bool success) { | |
| 182 UMA_HISTOGRAM_BOOLEAN("GCM.Crypto.RemoveKeySuccessRate", success); | 189 UMA_HISTOGRAM_BOOLEAN("GCM.Crypto.RemoveKeySuccessRate", success); |
| 183 | 190 |
| 184 if (success) { | 191 if (!success) { |
| 185 key_pairs_.erase(app_id); | 192 LOG(ERROR) << "Unable to delete a key from the GCM Key Store."; |
| 186 auth_secrets_.erase(app_id); | 193 |
| 187 } else { | 194 // Our cache is now inconsistent. Reject requests until restarted. |
| 188 DVLOG(1) << "Unable to delete a key from the GCM Key Store."; | 195 state_ = State::FAILED; |
| 189 } | 196 } |
| 190 | 197 |
| 191 callback.Run(); | 198 callback.Run(); |
| 192 } | 199 } |
| 193 | 200 |
| 194 void GCMKeyStore::LazyInitialize(const base::Closure& done_closure) { | 201 void GCMKeyStore::LazyInitialize(const base::Closure& done_closure) { |
| 195 if (delayed_task_controller_.CanRunTaskWithoutDelay()) { | 202 if (delayed_task_controller_.CanRunTaskWithoutDelay()) { |
| 196 done_closure.Run(); | 203 done_closure.Run(); |
| 197 return; | 204 return; |
| 198 } | 205 } |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 key_pairs_[entry.app_id()] = entry.keys(0); | 250 key_pairs_[entry.app_id()] = entry.keys(0); |
| 244 auth_secrets_[entry.app_id()] = entry.auth_secret(); | 251 auth_secrets_[entry.app_id()] = entry.auth_secret(); |
| 245 } | 252 } |
| 246 | 253 |
| 247 state_ = State::INITIALIZED; | 254 state_ = State::INITIALIZED; |
| 248 | 255 |
| 249 delayed_task_controller_.SetReady(); | 256 delayed_task_controller_.SetReady(); |
| 250 } | 257 } |
| 251 | 258 |
| 252 } // namespace gcm | 259 } // namespace gcm |
| OLD | NEW |