| 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_encryption_provider.h" | 5 #include "components/gcm_driver/crypto/gcm_encryption_provider.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/base64.h" | 9 #include "base/base64.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 // be created in memory rather than on-disk. | 70 // be created in memory rather than on-disk. |
| 71 if (!store_path.empty()) | 71 if (!store_path.empty()) |
| 72 encryption_store_path = store_path.Append(kEncryptionDirectoryName); | 72 encryption_store_path = store_path.Append(kEncryptionDirectoryName); |
| 73 | 73 |
| 74 key_store_.reset( | 74 key_store_.reset( |
| 75 new GCMKeyStore(encryption_store_path, blocking_task_runner)); | 75 new GCMKeyStore(encryption_store_path, blocking_task_runner)); |
| 76 } | 76 } |
| 77 | 77 |
| 78 void GCMEncryptionProvider::GetEncryptionInfo( | 78 void GCMEncryptionProvider::GetEncryptionInfo( |
| 79 const std::string& app_id, | 79 const std::string& app_id, |
| 80 const std::string& authorized_entity, |
| 80 const EncryptionInfoCallback& callback) { | 81 const EncryptionInfoCallback& callback) { |
| 81 DCHECK(key_store_); | 82 DCHECK(key_store_); |
| 82 key_store_->GetKeys( | 83 key_store_->GetKeys(app_id, authorized_entity, |
| 83 app_id, base::Bind(&GCMEncryptionProvider::DidGetEncryptionInfo, | 84 false /* fallback_to_empty_authorized_entity */, |
| 84 weak_ptr_factory_.GetWeakPtr(), app_id, callback)); | 85 base::Bind(&GCMEncryptionProvider::DidGetEncryptionInfo, |
| 86 weak_ptr_factory_.GetWeakPtr(), app_id, |
| 87 authorized_entity, callback)); |
| 85 } | 88 } |
| 86 | 89 |
| 87 void GCMEncryptionProvider::RemoveEncryptionInfo( | 90 void GCMEncryptionProvider::RemoveEncryptionInfo( |
| 88 const std::string& app_id, | 91 const std::string& app_id, |
| 92 const std::string& authorized_entity, |
| 89 const base::Closure& callback) { | 93 const base::Closure& callback) { |
| 90 DCHECK(key_store_); | 94 DCHECK(key_store_); |
| 91 key_store_->RemoveKeys(app_id, callback); | 95 key_store_->RemoveKeys(app_id, authorized_entity, callback); |
| 92 } | 96 } |
| 93 | 97 |
| 94 bool GCMEncryptionProvider::IsEncryptedMessage(const IncomingMessage& message) | 98 bool GCMEncryptionProvider::IsEncryptedMessage(const IncomingMessage& message) |
| 95 const { | 99 const { |
| 96 // The Web Push protocol requires the encryption and crypto-key properties to | 100 // The Web Push protocol requires the encryption and crypto-key properties to |
| 97 // be set, and the raw_data field to be populated with the payload. | 101 // be set, and the raw_data field to be populated with the payload. |
| 98 if (message.data.find(kEncryptionProperty) == message.data.end() || | 102 if (message.data.find(kEncryptionProperty) == message.data.end() || |
| 99 message.data.find(kCryptoKeyProperty) == message.data.end()) | 103 message.data.find(kCryptoKeyProperty) == message.data.end()) |
| 100 return false; | 104 return false; |
| 101 | 105 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 } | 152 } |
| 149 | 153 |
| 150 if (crypto_key_header_values.size() != 1u || | 154 if (crypto_key_header_values.size() != 1u || |
| 151 !crypto_key_header_values[0].dh.size()) { | 155 !crypto_key_header_values[0].dh.size()) { |
| 152 DLOG(ERROR) << "Invalid values supplied in the Crypto-Key header"; | 156 DLOG(ERROR) << "Invalid values supplied in the Crypto-Key header"; |
| 153 callback.Run(DECRYPTION_RESULT_INVALID_CRYPTO_KEY_HEADER, | 157 callback.Run(DECRYPTION_RESULT_INVALID_CRYPTO_KEY_HEADER, |
| 154 IncomingMessage()); | 158 IncomingMessage()); |
| 155 return; | 159 return; |
| 156 } | 160 } |
| 157 | 161 |
| 158 key_store_->GetKeys( | 162 // Use |fallback_to_empty_authorized_entity|, since this message might have |
| 159 app_id, base::Bind(&GCMEncryptionProvider::DecryptMessageWithKey, | 163 // been sent to either an InstanceID token or a non-InstanceID registration. |
| 160 weak_ptr_factory_.GetWeakPtr(), message, | 164 key_store_->GetKeys(app_id, message.sender_id /* authorized_entity */, |
| 161 callback, encryption_header_values[0].salt, | 165 true /* fallback_to_empty_authorized_entity */, |
| 162 crypto_key_header_values[0].dh, | 166 base::Bind(&GCMEncryptionProvider::DecryptMessageWithKey, |
| 163 encryption_header_values[0].rs)); | 167 weak_ptr_factory_.GetWeakPtr(), message, |
| 168 callback, encryption_header_values[0].salt, |
| 169 crypto_key_header_values[0].dh, |
| 170 encryption_header_values[0].rs)); |
| 164 } | 171 } |
| 165 | 172 |
| 166 void GCMEncryptionProvider::DidGetEncryptionInfo( | 173 void GCMEncryptionProvider::DidGetEncryptionInfo( |
| 167 const std::string& app_id, | 174 const std::string& app_id, |
| 175 const std::string& authorized_entity, |
| 168 const EncryptionInfoCallback& callback, | 176 const EncryptionInfoCallback& callback, |
| 169 const KeyPair& pair, | 177 const KeyPair& pair, |
| 170 const std::string& auth_secret) { | 178 const std::string& auth_secret) { |
| 171 if (!pair.IsInitialized()) { | 179 if (!pair.IsInitialized()) { |
| 172 key_store_->CreateKeys( | 180 key_store_->CreateKeys( |
| 173 app_id, base::Bind(&GCMEncryptionProvider::DidCreateEncryptionInfo, | 181 app_id, authorized_entity, |
| 174 weak_ptr_factory_.GetWeakPtr(), callback)); | 182 base::Bind(&GCMEncryptionProvider::DidCreateEncryptionInfo, |
| 183 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 175 return; | 184 return; |
| 176 } | 185 } |
| 177 | 186 |
| 178 DCHECK_EQ(KeyPair::ECDH_P256, pair.type()); | 187 DCHECK_EQ(KeyPair::ECDH_P256, pair.type()); |
| 179 callback.Run(pair.public_key(), auth_secret); | 188 callback.Run(pair.public_key(), auth_secret); |
| 180 } | 189 } |
| 181 | 190 |
| 182 void GCMEncryptionProvider::DidCreateEncryptionInfo( | 191 void GCMEncryptionProvider::DidCreateEncryptionInfo( |
| 183 const EncryptionInfoCallback& callback, | 192 const EncryptionInfoCallback& callback, |
| 184 const KeyPair& pair, | 193 const KeyPair& pair, |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 235 decrypted_message.decrypted = true; | 244 decrypted_message.decrypted = true; |
| 236 | 245 |
| 237 // There must be no data associated with the decrypted message at this point, | 246 // There must be no data associated with the decrypted message at this point, |
| 238 // to make sure that we don't end up in an infinite decryption loop. | 247 // to make sure that we don't end up in an infinite decryption loop. |
| 239 DCHECK_EQ(0u, decrypted_message.data.size()); | 248 DCHECK_EQ(0u, decrypted_message.data.size()); |
| 240 | 249 |
| 241 callback.Run(DECRYPTION_RESULT_DECRYPTED, decrypted_message); | 250 callback.Run(DECRYPTION_RESULT_DECRYPTED, decrypted_message); |
| 242 } | 251 } |
| 243 | 252 |
| 244 } // namespace gcm | 253 } // namespace gcm |
| OLD | NEW |