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 // Our cache is now inconsistent. Reject requests until Chrome is restarted. | |
Peter Beverloo
2016/05/05 15:29:35
nit (1): Blank line above
nit (2): Do not mention
johnme
2016/05/05 17:32:46
Done.
| |
145 state_ = State::FAILED; | |
141 callback.Run(KeyPair(), std::string() /* auth_secret */); | 146 callback.Run(KeyPair(), std::string() /* auth_secret */); |
142 return; | 147 return; |
143 } | 148 } |
144 | 149 |
145 key_pairs_[app_id] = pair; | 150 callback.Run(pair, auth_secret); |
146 auth_secrets_[app_id] = auth_secret; | |
147 | |
148 callback.Run(key_pairs_[app_id], auth_secret); | |
149 } | 151 } |
150 | 152 |
151 void GCMKeyStore::RemoveKeys(const std::string& app_id, | 153 void GCMKeyStore::RemoveKeys(const std::string& app_id, |
152 const base::Closure& callback) { | 154 const base::Closure& callback) { |
153 LazyInitialize(base::Bind(&GCMKeyStore::RemoveKeysAfterInitialize, | 155 LazyInitialize(base::Bind(&GCMKeyStore::RemoveKeysAfterInitialize, |
154 weak_factory_.GetWeakPtr(), app_id, callback)); | 156 weak_factory_.GetWeakPtr(), app_id, callback)); |
155 } | 157 } |
156 | 158 |
157 void GCMKeyStore::RemoveKeysAfterInitialize(const std::string& app_id, | 159 void GCMKeyStore::RemoveKeysAfterInitialize(const std::string& app_id, |
158 const base::Closure& callback) { | 160 const base::Closure& callback) { |
159 DCHECK(state_ == State::INITIALIZED || state_ == State::FAILED); | 161 DCHECK(state_ == State::INITIALIZED || state_ == State::FAILED); |
160 const auto iter = key_pairs_.find(app_id); | 162 const auto iter = key_pairs_.find(app_id); |
161 if (iter == key_pairs_.end() || state_ != State::INITIALIZED) { | 163 if (iter == key_pairs_.end() || state_ != State::INITIALIZED) { |
162 callback.Run(); | 164 callback.Run(); |
163 return; | 165 return; |
164 } | 166 } |
165 | 167 |
168 // Clear them immediately from our cache, so subsequent calls to | |
169 // {Get/Create/Remove}Keys don't see them. | |
170 key_pairs_.erase(app_id); | |
171 auth_secrets_.erase(app_id); | |
172 | |
166 using EntryVectorType = | 173 using EntryVectorType = |
167 leveldb_proto::ProtoDatabase<EncryptionData>::KeyEntryVector; | 174 leveldb_proto::ProtoDatabase<EncryptionData>::KeyEntryVector; |
168 | 175 |
169 std::unique_ptr<EntryVectorType> entries_to_save(new EntryVectorType()); | 176 std::unique_ptr<EntryVectorType> entries_to_save(new EntryVectorType()); |
170 std::unique_ptr<std::vector<std::string>> keys_to_remove( | 177 std::unique_ptr<std::vector<std::string>> keys_to_remove( |
171 new std::vector<std::string>(1, app_id)); | 178 new std::vector<std::string>(1, app_id)); |
172 | 179 |
173 database_->UpdateEntries( | 180 database_->UpdateEntries(std::move(entries_to_save), |
174 std::move(entries_to_save), std::move(keys_to_remove), | 181 std::move(keys_to_remove), |
175 base::Bind(&GCMKeyStore::DidRemoveKeys, weak_factory_.GetWeakPtr(), | 182 base::Bind(&GCMKeyStore::DidRemoveKeys, |
176 app_id, callback)); | 183 weak_factory_.GetWeakPtr(), callback)); |
177 } | 184 } |
178 | 185 |
179 void GCMKeyStore::DidRemoveKeys(const std::string& app_id, | 186 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); | 187 UMA_HISTOGRAM_BOOLEAN("GCM.Crypto.RemoveKeySuccessRate", success); |
183 | 188 |
184 if (success) { | 189 if (!success) { |
185 key_pairs_.erase(app_id); | 190 LOG(ERROR) << "Unable to delete a key from the GCM Key Store."; |
186 auth_secrets_.erase(app_id); | 191 // Our cache is now inconsistent. Reject requests until Chrome is restarted. |
187 } else { | 192 state_ = State::FAILED; |
188 DVLOG(1) << "Unable to delete a key from the GCM Key Store."; | |
189 } | 193 } |
190 | 194 |
191 callback.Run(); | 195 callback.Run(); |
192 } | 196 } |
193 | 197 |
194 void GCMKeyStore::LazyInitialize(const base::Closure& done_closure) { | 198 void GCMKeyStore::LazyInitialize(const base::Closure& done_closure) { |
195 if (delayed_task_controller_.CanRunTaskWithoutDelay()) { | 199 if (delayed_task_controller_.CanRunTaskWithoutDelay()) { |
196 done_closure.Run(); | 200 done_closure.Run(); |
197 return; | 201 return; |
198 } | 202 } |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
243 key_pairs_[entry.app_id()] = entry.keys(0); | 247 key_pairs_[entry.app_id()] = entry.keys(0); |
244 auth_secrets_[entry.app_id()] = entry.auth_secret(); | 248 auth_secrets_[entry.app_id()] = entry.auth_secret(); |
245 } | 249 } |
246 | 250 |
247 state_ = State::INITIALIZED; | 251 state_ = State::INITIALIZED; |
248 | 252 |
249 delayed_task_controller_.SetReady(); | 253 delayed_task_controller_.SetReady(); |
250 } | 254 } |
251 | 255 |
252 } // namespace gcm | 256 } // namespace gcm |
OLD | NEW |