| 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 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 } | 117 } |
| 118 | 118 |
| 119 // IsEncryptedMessage() verifies that both the Encryption and Crypto-Key HTTP | 119 // IsEncryptedMessage() verifies that both the Encryption and Crypto-Key HTTP |
| 120 // headers have been provided for the |message|. | 120 // headers have been provided for the |message|. |
| 121 const auto& encryption_header = message.data.find(kEncryptionProperty); | 121 const auto& encryption_header = message.data.find(kEncryptionProperty); |
| 122 const auto& crypto_key_header = message.data.find(kCryptoKeyProperty); | 122 const auto& crypto_key_header = message.data.find(kCryptoKeyProperty); |
| 123 | 123 |
| 124 DCHECK(encryption_header != message.data.end()); | 124 DCHECK(encryption_header != message.data.end()); |
| 125 DCHECK(crypto_key_header != message.data.end()); | 125 DCHECK(crypto_key_header != message.data.end()); |
| 126 | 126 |
| 127 std::vector<EncryptionHeaderValues> encryption_header_values; | 127 EncryptionHeaderIterator encryption_header_iterator( |
| 128 if (!ParseEncryptionHeader(encryption_header->second, | 128 encryption_header->second.begin(), encryption_header->second.end()); |
| 129 &encryption_header_values)) { | 129 if (!encryption_header_iterator.GetNext()) { |
| 130 DLOG(ERROR) << "Unable to parse the value of the Encryption header"; | 130 DLOG(ERROR) << "Unable to parse the value of the Encryption header"; |
| 131 callback.Run(DECRYPTION_RESULT_INVALID_ENCRYPTION_HEADER, | 131 callback.Run(DECRYPTION_RESULT_INVALID_ENCRYPTION_HEADER, |
| 132 IncomingMessage()); | 132 IncomingMessage()); |
| 133 return; | 133 return; |
| 134 } | 134 } |
| 135 | 135 |
| 136 if (encryption_header_values.size() != 1u || | 136 if (encryption_header_iterator.salt().size() != |
| 137 encryption_header_values[0].salt.size() != | |
| 138 GCMMessageCryptographer::kSaltSize) { | 137 GCMMessageCryptographer::kSaltSize) { |
| 139 DLOG(ERROR) << "Invalid values supplied in the Encryption header"; | 138 DLOG(ERROR) << "Invalid values supplied in the Encryption header"; |
| 140 callback.Run(DECRYPTION_RESULT_INVALID_ENCRYPTION_HEADER, | 139 callback.Run(DECRYPTION_RESULT_INVALID_ENCRYPTION_HEADER, |
| 141 IncomingMessage()); | 140 IncomingMessage()); |
| 142 return; | 141 return; |
| 143 } | 142 } |
| 144 | 143 |
| 145 std::vector<CryptoKeyHeaderValues> crypto_key_header_values; | 144 CryptoKeyHeaderIterator crypto_key_header_iterator( |
| 146 if (!ParseCryptoKeyHeader(crypto_key_header->second, | 145 crypto_key_header->second.begin(), crypto_key_header->second.end()); |
| 147 &crypto_key_header_values)) { | 146 if (!crypto_key_header_iterator.GetNext()) { |
| 148 DLOG(ERROR) << "Unable to parse the value of the Crypto-Key header"; | 147 DLOG(ERROR) << "Unable to parse the value of the Crypto-Key header"; |
| 149 callback.Run(DECRYPTION_RESULT_INVALID_CRYPTO_KEY_HEADER, | 148 callback.Run(DECRYPTION_RESULT_INVALID_CRYPTO_KEY_HEADER, |
| 150 IncomingMessage()); | 149 IncomingMessage()); |
| 151 return; | 150 return; |
| 152 } | 151 } |
| 153 | 152 |
| 154 if (crypto_key_header_values.size() != 1u || | 153 if (crypto_key_header_iterator.dh().empty()) { |
| 155 !crypto_key_header_values[0].dh.size()) { | |
| 156 DLOG(ERROR) << "Invalid values supplied in the Crypto-Key header"; | 154 DLOG(ERROR) << "Invalid values supplied in the Crypto-Key header"; |
| 157 callback.Run(DECRYPTION_RESULT_INVALID_CRYPTO_KEY_HEADER, | 155 callback.Run(DECRYPTION_RESULT_INVALID_CRYPTO_KEY_HEADER, |
| 158 IncomingMessage()); | 156 IncomingMessage()); |
| 159 return; | 157 return; |
| 160 } | 158 } |
| 161 | 159 |
| 162 // Use |fallback_to_empty_authorized_entity|, since this message might have | 160 // Use |fallback_to_empty_authorized_entity|, since this message might have |
| 163 // been sent to either an InstanceID token or a non-InstanceID registration. | 161 // been sent to either an InstanceID token or a non-InstanceID registration. |
| 164 key_store_->GetKeys(app_id, message.sender_id /* authorized_entity */, | 162 key_store_->GetKeys(app_id, message.sender_id /* authorized_entity */, |
| 165 true /* fallback_to_empty_authorized_entity */, | 163 true /* fallback_to_empty_authorized_entity */, |
| 166 base::Bind(&GCMEncryptionProvider::DecryptMessageWithKey, | 164 base::Bind(&GCMEncryptionProvider::DecryptMessageWithKey, |
| 167 weak_ptr_factory_.GetWeakPtr(), message, | 165 weak_ptr_factory_.GetWeakPtr(), message, |
| 168 callback, encryption_header_values[0].salt, | 166 callback, encryption_header_iterator.salt(), |
| 169 crypto_key_header_values[0].dh, | 167 crypto_key_header_iterator.dh(), |
| 170 encryption_header_values[0].rs)); | 168 encryption_header_iterator.rs())); |
| 171 } | 169 } |
| 172 | 170 |
| 173 void GCMEncryptionProvider::DidGetEncryptionInfo( | 171 void GCMEncryptionProvider::DidGetEncryptionInfo( |
| 174 const std::string& app_id, | 172 const std::string& app_id, |
| 175 const std::string& authorized_entity, | 173 const std::string& authorized_entity, |
| 176 const EncryptionInfoCallback& callback, | 174 const EncryptionInfoCallback& callback, |
| 177 const KeyPair& pair, | 175 const KeyPair& pair, |
| 178 const std::string& auth_secret) { | 176 const std::string& auth_secret) { |
| 179 if (!pair.IsInitialized()) { | 177 if (!pair.IsInitialized()) { |
| 180 key_store_->CreateKeys( | 178 key_store_->CreateKeys( |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 244 decrypted_message.decrypted = true; | 242 decrypted_message.decrypted = true; |
| 245 | 243 |
| 246 // There must be no data associated with the decrypted message at this point, | 244 // There must be no data associated with the decrypted message at this point, |
| 247 // to make sure that we don't end up in an infinite decryption loop. | 245 // to make sure that we don't end up in an infinite decryption loop. |
| 248 DCHECK_EQ(0u, decrypted_message.data.size()); | 246 DCHECK_EQ(0u, decrypted_message.data.size()); |
| 249 | 247 |
| 250 callback.Run(DECRYPTION_RESULT_DECRYPTED, decrypted_message); | 248 callback.Run(DECRYPTION_RESULT_DECRYPTED, decrypted_message); |
| 251 } | 249 } |
| 252 | 250 |
| 253 } // namespace gcm | 251 } // namespace gcm |
| OLD | NEW |