| 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" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "components/gcm_driver/common/gcm_messages.h" | 12 #include "components/gcm_driver/common/gcm_messages.h" |
| 13 #include "components/gcm_driver/crypto/encryption_header_parsers.h" | 13 #include "components/gcm_driver/crypto/encryption_header_parsers.h" |
| 14 #include "components/gcm_driver/crypto/gcm_key_store.h" | 14 #include "components/gcm_driver/crypto/gcm_key_store.h" |
| 15 #include "components/gcm_driver/crypto/gcm_message_cryptographer.h" | 15 #include "components/gcm_driver/crypto/gcm_message_cryptographer.h" |
| 16 #include "components/gcm_driver/crypto/p256_key_util.h" | 16 #include "components/gcm_driver/crypto/p256_key_util.h" |
| 17 #include "components/gcm_driver/crypto/proto/gcm_encryption_data.pb.h" | 17 #include "components/gcm_driver/crypto/proto/gcm_encryption_data.pb.h" |
| 18 | 18 |
| 19 namespace gcm { | 19 namespace gcm { |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 const char kEncryptionProperty[] = "encryption"; | 23 const char kEncryptionProperty[] = "encryption"; |
| 24 const char kEncryptionKeyProperty[] = "encryption_key"; | 24 const char kCryptoKeyProperty[] = "crypto_key"; |
| 25 | 25 |
| 26 // Directory in the GCM Store in which the encryption database will be stored. | 26 // Directory in the GCM Store in which the encryption database will be stored. |
| 27 const base::FilePath::CharType kEncryptionDirectoryName[] = | 27 const base::FilePath::CharType kEncryptionDirectoryName[] = |
| 28 FILE_PATH_LITERAL("Encryption"); | 28 FILE_PATH_LITERAL("Encryption"); |
| 29 | 29 |
| 30 } // namespace | 30 } // namespace |
| 31 | 31 |
| 32 GCMEncryptionProvider::GCMEncryptionProvider() | 32 GCMEncryptionProvider::GCMEncryptionProvider() |
| 33 : weak_ptr_factory_(this) { | 33 : weak_ptr_factory_(this) { |
| 34 } | 34 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 55 void GCMEncryptionProvider::GetPublicKey(const std::string& app_id, | 55 void GCMEncryptionProvider::GetPublicKey(const std::string& app_id, |
| 56 const PublicKeyCallback& callback) { | 56 const PublicKeyCallback& callback) { |
| 57 DCHECK(key_store_); | 57 DCHECK(key_store_); |
| 58 key_store_->GetKeys( | 58 key_store_->GetKeys( |
| 59 app_id, base::Bind(&GCMEncryptionProvider::DidGetPublicKey, | 59 app_id, base::Bind(&GCMEncryptionProvider::DidGetPublicKey, |
| 60 weak_ptr_factory_.GetWeakPtr(), app_id, callback)); | 60 weak_ptr_factory_.GetWeakPtr(), app_id, callback)); |
| 61 } | 61 } |
| 62 | 62 |
| 63 bool GCMEncryptionProvider::IsEncryptedMessage(const IncomingMessage& message) | 63 bool GCMEncryptionProvider::IsEncryptedMessage(const IncomingMessage& message) |
| 64 const { | 64 const { |
| 65 // The Web Push protocol requires the encryption and encryption_key properties | 65 // The Web Push protocol requires the encryption and crypto_key properties to |
| 66 // to be set, and the raw_data field to be populated with the payload. | 66 // be set, and the raw_data field to be populated with the payload. |
| 67 if (message.data.find(kEncryptionProperty) == message.data.end() || | 67 if (message.data.find(kEncryptionProperty) == message.data.end() || |
| 68 message.data.find(kEncryptionKeyProperty) == message.data.end()) | 68 message.data.find(kCryptoKeyProperty) == message.data.end()) |
| 69 return false; | 69 return false; |
| 70 | 70 |
| 71 // TODO(peter): Support decrypting messages that were sent using the existing | |
| 72 // GCM protocol, as opposed to the Web Push protocol. | |
| 73 | |
| 74 return message.raw_data.size() > 0; | 71 return message.raw_data.size() > 0; |
| 75 } | 72 } |
| 76 | 73 |
| 77 void GCMEncryptionProvider::DecryptMessage( | 74 void GCMEncryptionProvider::DecryptMessage( |
| 78 const std::string& app_id, | 75 const std::string& app_id, |
| 79 const IncomingMessage& message, | 76 const IncomingMessage& message, |
| 80 const MessageDecryptedCallback& success_callback, | 77 const MessageDecryptedCallback& success_callback, |
| 81 const DecryptionFailedCallback& failure_callback) { | 78 const DecryptionFailedCallback& failure_callback) { |
| 82 DCHECK(key_store_); | 79 DCHECK(key_store_); |
| 83 | 80 |
| 84 const auto& encryption_header = message.data.find(kEncryptionProperty); | 81 const auto& encryption_header = message.data.find(kEncryptionProperty); |
| 85 const auto& encryption_key_header = message.data.find(kEncryptionKeyProperty); | 82 const auto& crypto_key_header = message.data.find(kCryptoKeyProperty); |
| 86 | 83 |
| 87 // Callers are expected to call IsEncryptedMessage() prior to this method. | 84 // Callers are expected to call IsEncryptedMessage() prior to this method. |
| 88 DCHECK(encryption_header != message.data.end()); | 85 DCHECK(encryption_header != message.data.end()); |
| 89 DCHECK(encryption_key_header != message.data.end()); | 86 DCHECK(crypto_key_header != message.data.end()); |
| 90 | 87 |
| 91 std::vector<EncryptionHeaderValues> encryption_header_values; | 88 std::vector<EncryptionHeaderValues> encryption_header_values; |
| 92 if (!ParseEncryptionHeader(encryption_header->second, | 89 if (!ParseEncryptionHeader(encryption_header->second, |
| 93 &encryption_header_values)) { | 90 &encryption_header_values)) { |
| 94 DLOG(ERROR) << "Unable to parse the value of the Encryption header"; | 91 DLOG(ERROR) << "Unable to parse the value of the Encryption header"; |
| 95 failure_callback.Run(DECRYPTION_FAILURE_INVALID_ENCRYPTION_HEADER); | 92 failure_callback.Run(DECRYPTION_FAILURE_INVALID_ENCRYPTION_HEADER); |
| 96 return; | 93 return; |
| 97 } | 94 } |
| 98 | 95 |
| 99 if (encryption_header_values.size() != 1u || | 96 if (encryption_header_values.size() != 1u || |
| 100 encryption_header_values[0].salt.size() != | 97 encryption_header_values[0].salt.size() != |
| 101 GCMMessageCryptographer::kSaltSize) { | 98 GCMMessageCryptographer::kSaltSize) { |
| 102 DLOG(ERROR) << "Invalid values supplied in the Encryption header"; | 99 DLOG(ERROR) << "Invalid values supplied in the Encryption header"; |
| 103 failure_callback.Run(DECRYPTION_FAILURE_INVALID_ENCRYPTION_HEADER); | 100 failure_callback.Run(DECRYPTION_FAILURE_INVALID_ENCRYPTION_HEADER); |
| 104 return; | 101 return; |
| 105 } | 102 } |
| 106 | 103 |
| 107 std::vector<EncryptionKeyHeaderValues> encryption_key_header_values; | 104 std::vector<CryptoKeyHeaderValues> crypto_key_header_values; |
| 108 if (!ParseEncryptionKeyHeader(encryption_key_header->second, | 105 if (!ParseCryptoKeyHeader(crypto_key_header->second, |
| 109 &encryption_key_header_values)) { | 106 &crypto_key_header_values)) { |
| 110 DLOG(ERROR) << "Unable to parse the value of the Encryption-Key header"; | 107 DLOG(ERROR) << "Unable to parse the value of the Crypto-Key header"; |
| 111 failure_callback.Run(DECRYPTION_FAILURE_INVALID_ENCRYPTION_KEY_HEADER); | 108 failure_callback.Run(DECRYPTION_FAILURE_INVALID_CRYPTO_KEY_HEADER); |
| 112 return; | 109 return; |
| 113 } | 110 } |
| 114 | 111 |
| 115 if (encryption_key_header_values.size() != 1u || | 112 if (crypto_key_header_values.size() != 1u || |
| 116 !encryption_key_header_values[0].dh.size()) { | 113 !crypto_key_header_values[0].dh.size()) { |
| 117 DLOG(ERROR) << "Invalid values supplied in the Encryption-Key header"; | 114 DLOG(ERROR) << "Invalid values supplied in the Crypto-Key header"; |
| 118 failure_callback.Run(DECRYPTION_FAILURE_INVALID_ENCRYPTION_KEY_HEADER); | 115 failure_callback.Run(DECRYPTION_FAILURE_INVALID_CRYPTO_KEY_HEADER); |
| 119 return; | 116 return; |
| 120 } | 117 } |
| 121 | 118 |
| 122 key_store_->GetKeys( | 119 key_store_->GetKeys( |
| 123 app_id, base::Bind(&GCMEncryptionProvider::DecryptMessageWithKey, | 120 app_id, base::Bind(&GCMEncryptionProvider::DecryptMessageWithKey, |
| 124 weak_ptr_factory_.GetWeakPtr(), message, | 121 weak_ptr_factory_.GetWeakPtr(), message, |
| 125 success_callback, failure_callback, | 122 success_callback, failure_callback, |
| 126 encryption_header_values[0].salt, | 123 encryption_header_values[0].salt, |
| 127 encryption_key_header_values[0].dh, | 124 crypto_key_header_values[0].dh, |
| 128 encryption_header_values[0].rs)); | 125 encryption_header_values[0].rs)); |
| 129 } | 126 } |
| 130 | 127 |
| 131 void GCMEncryptionProvider::DidGetPublicKey(const std::string& app_id, | 128 void GCMEncryptionProvider::DidGetPublicKey(const std::string& app_id, |
| 132 const PublicKeyCallback& callback, | 129 const PublicKeyCallback& callback, |
| 133 const KeyPair& pair) { | 130 const KeyPair& pair) { |
| 134 if (!pair.IsInitialized()) { | 131 if (!pair.IsInitialized()) { |
| 135 key_store_->CreateKeys( | 132 key_store_->CreateKeys( |
| 136 app_id, base::Bind(&GCMEncryptionProvider::DidCreatePublicKey, | 133 app_id, base::Bind(&GCMEncryptionProvider::DidCreatePublicKey, |
| 137 weak_ptr_factory_.GetWeakPtr(), callback)); | 134 weak_ptr_factory_.GetWeakPtr(), callback)); |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 decrypted_message.decrypted = true; | 192 decrypted_message.decrypted = true; |
| 196 | 193 |
| 197 // There must be no data associated with the decrypted message at this point, | 194 // There must be no data associated with the decrypted message at this point, |
| 198 // to make sure that we don't end up in an infinite decryption loop. | 195 // to make sure that we don't end up in an infinite decryption loop. |
| 199 DCHECK_EQ(0u, decrypted_message.data.size()); | 196 DCHECK_EQ(0u, decrypted_message.data.size()); |
| 200 | 197 |
| 201 success_callback.Run(decrypted_message); | 198 success_callback.Run(decrypted_message); |
| 202 } | 199 } |
| 203 | 200 |
| 204 } // namespace gcm | 201 } // namespace gcm |
| OLD | NEW |