Chromium Code Reviews| 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> | |
| 8 | |
| 9 #include "base/base64.h" | |
| 7 #include "base/bind.h" | 10 #include "base/bind.h" |
| 8 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "components/gcm_driver/common/gcm_messages.h" | |
| 13 #include "components/gcm_driver/crypto/encryption_header_parsers.h" | |
| 9 #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" | |
| 10 #include "components/gcm_driver/crypto/proto/gcm_encryption_data.pb.h" | 16 #include "components/gcm_driver/crypto/proto/gcm_encryption_data.pb.h" |
| 17 #include "crypto/curve25519.h" | |
|
Michael van Ouwerkerk
2015/10/02 14:30:41
I thought we settled on curve P-256?
Peter Beverloo
2015/10/02 15:01:22
Yes, but we'll switch over in M48. (The branch poi
| |
| 11 | 18 |
| 12 namespace gcm { | 19 namespace gcm { |
| 13 | 20 |
| 21 namespace { | |
| 22 | |
| 23 const char kEncryptionProperty[] = "encryption"; | |
| 24 const char kEncryptionKeyProperty[] = "encryption_key"; | |
| 25 | |
| 14 // 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. |
| 15 const base::FilePath::CharType kEncryptionDirectoryName[] = | 27 const base::FilePath::CharType kEncryptionDirectoryName[] = |
| 16 FILE_PATH_LITERAL("Encryption"); | 28 FILE_PATH_LITERAL("Encryption"); |
| 17 | 29 |
| 30 } // namespace | |
| 31 | |
| 18 GCMEncryptionProvider::GCMEncryptionProvider() | 32 GCMEncryptionProvider::GCMEncryptionProvider() |
| 19 : weak_ptr_factory_(this) { | 33 : weak_ptr_factory_(this) { |
| 20 } | 34 } |
| 21 | 35 |
| 22 GCMEncryptionProvider::~GCMEncryptionProvider() { | 36 GCMEncryptionProvider::~GCMEncryptionProvider() { |
| 23 } | 37 } |
| 24 | 38 |
| 25 void GCMEncryptionProvider::Init( | 39 void GCMEncryptionProvider::Init( |
| 26 const base::FilePath& store_path, | 40 const base::FilePath& store_path, |
| 27 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner) { | 41 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner) { |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 39 } | 53 } |
| 40 | 54 |
| 41 void GCMEncryptionProvider::GetPublicKey(const std::string& app_id, | 55 void GCMEncryptionProvider::GetPublicKey(const std::string& app_id, |
| 42 const PublicKeyCallback& callback) { | 56 const PublicKeyCallback& callback) { |
| 43 DCHECK(key_store_); | 57 DCHECK(key_store_); |
| 44 key_store_->GetKeys( | 58 key_store_->GetKeys( |
| 45 app_id, base::Bind(&GCMEncryptionProvider::DidGetPublicKey, | 59 app_id, base::Bind(&GCMEncryptionProvider::DidGetPublicKey, |
| 46 weak_ptr_factory_.GetWeakPtr(), app_id, callback)); | 60 weak_ptr_factory_.GetWeakPtr(), app_id, callback)); |
| 47 } | 61 } |
| 48 | 62 |
| 63 bool GCMEncryptionProvider::IsEncryptedMessage(const IncomingMessage& message) | |
| 64 const { | |
| 65 // The Web Push protocol requires the encryption and encryption_key properties | |
| 66 // to be set, and the raw_data field to be populated with the payload. | |
| 67 if (message.data.find(kEncryptionProperty) == message.data.end() || | |
| 68 message.data.find(kEncryptionKeyProperty) == message.data.end()) | |
| 69 return false; | |
| 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; | |
| 75 } | |
| 76 | |
| 77 void GCMEncryptionProvider::DecryptMessage( | |
| 78 const std::string& app_id, | |
| 79 const IncomingMessage& message, | |
| 80 const MessageDecryptedCallback& success_callback, | |
| 81 const DecryptionFailedCallback& failure_callback) { | |
| 82 DCHECK(key_store_); | |
| 83 | |
| 84 const auto& encryption_header = message.data.find(kEncryptionProperty); | |
| 85 const auto& encryption_key_header = message.data.find(kEncryptionKeyProperty); | |
| 86 | |
| 87 // Callers are expected to call IsEncryptedMessage() prior to this method. | |
|
Michael van Ouwerkerk
2015/10/02 14:30:41
Why not DCHECK(IsEncryptedMessage()) instead of ch
Peter Beverloo
2015/10/02 15:01:21
|message| is a const reference to IncomingMessage,
| |
| 88 DCHECK(encryption_header != message.data.end()); | |
| 89 DCHECK(encryption_key_header != message.data.end()); | |
| 90 | |
| 91 std::vector<EncryptionHeaderValues> encryption_header_values; | |
| 92 if (!ParseEncryptionHeader(encryption_header->second, | |
| 93 &encryption_header_values)) { | |
| 94 DLOG(ERROR) << "Unable to parse the value of the Encryption header"; | |
| 95 failure_callback.Run(DECRYPTION_FAILURE_INVALID_ENCRYPTION_HEADER); | |
| 96 return; | |
| 97 } | |
| 98 | |
| 99 if (encryption_header_values.size() != 1u || | |
| 100 encryption_header_values[0].salt.size() != | |
| 101 GCMMessageCryptographer::kSaltSize) { | |
| 102 DLOG(ERROR) << "Invalid values supplied in the Encryption header"; | |
| 103 failure_callback.Run(DECRYPTION_FAILURE_INVALID_ENCRYPTION_HEADER); | |
| 104 return; | |
| 105 } | |
| 106 | |
| 107 std::vector<EncryptionKeyHeaderValues> encryption_key_header_values; | |
| 108 if (!ParseEncryptionKeyHeader(encryption_key_header->second, | |
| 109 &encryption_key_header_values)) { | |
| 110 DLOG(ERROR) << "Unable to parse the value of the Encryption-Key header"; | |
| 111 failure_callback.Run(DECRYPTION_FAILURE_INVALID_ENCRYPTION_KEY_HEADER); | |
| 112 return; | |
| 113 } | |
| 114 | |
| 115 if (encryption_key_header_values.size() != 1u || | |
| 116 encryption_key_header_values[0].dh.size() != crypto::curve25519::kBytes) { | |
| 117 DLOG(ERROR) << "Invalid values supplied in the Encryption header"; | |
|
Michael van Ouwerkerk
2015/10/02 14:30:41
Encryption-Key
Peter Beverloo
2015/10/02 15:01:22
Done.
| |
| 118 failure_callback.Run(DECRYPTION_FAILURE_INVALID_ENCRYPTION_KEY_HEADER); | |
| 119 return; | |
| 120 } | |
| 121 | |
| 122 key_store_->GetKeys( | |
| 123 app_id, base::Bind(&GCMEncryptionProvider::DecryptMessageWithKey, | |
| 124 weak_ptr_factory_.GetWeakPtr(), message, | |
| 125 success_callback, failure_callback, | |
| 126 encryption_header_values[0].salt, | |
| 127 encryption_key_header_values[0].dh, | |
| 128 encryption_header_values[0].rs)); | |
| 129 } | |
| 130 | |
| 49 void GCMEncryptionProvider::DidGetPublicKey(const std::string& app_id, | 131 void GCMEncryptionProvider::DidGetPublicKey(const std::string& app_id, |
| 50 const PublicKeyCallback& callback, | 132 const PublicKeyCallback& callback, |
| 51 const KeyPair& pair) { | 133 const KeyPair& pair) { |
| 52 if (!pair.IsInitialized()) { | 134 if (!pair.IsInitialized()) { |
| 53 key_store_->CreateKeys( | 135 key_store_->CreateKeys( |
| 54 app_id, base::Bind(&GCMEncryptionProvider::DidCreatePublicKey, | 136 app_id, base::Bind(&GCMEncryptionProvider::DidCreatePublicKey, |
| 55 weak_ptr_factory_.GetWeakPtr(), callback)); | 137 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 56 return; | 138 return; |
| 57 } | 139 } |
| 58 | 140 |
| 59 DCHECK_EQ(KeyPair::ECDH_CURVE_25519, pair.type()); | 141 DCHECK_EQ(KeyPair::ECDH_CURVE_25519, pair.type()); |
| 60 callback.Run(pair.public_key()); | 142 callback.Run(pair.public_key()); |
| 61 } | 143 } |
| 62 | 144 |
| 63 void GCMEncryptionProvider::DidCreatePublicKey( | 145 void GCMEncryptionProvider::DidCreatePublicKey( |
| 64 const PublicKeyCallback& callback, | 146 const PublicKeyCallback& callback, |
| 65 const KeyPair& pair) { | 147 const KeyPair& pair) { |
| 66 if (!pair.IsInitialized()) { | 148 if (!pair.IsInitialized()) { |
| 67 callback.Run(std::string()); | 149 callback.Run(std::string()); |
| 68 return; | 150 return; |
| 69 } | 151 } |
| 70 | 152 |
| 71 DCHECK_EQ(KeyPair::ECDH_CURVE_25519, pair.type()); | 153 DCHECK_EQ(KeyPair::ECDH_CURVE_25519, pair.type()); |
| 72 callback.Run(pair.public_key()); | 154 callback.Run(pair.public_key()); |
| 73 } | 155 } |
| 74 | 156 |
| 157 void GCMEncryptionProvider::DecryptMessageWithKey( | |
| 158 const IncomingMessage& message, | |
| 159 const MessageDecryptedCallback& success_callback, | |
| 160 const DecryptionFailedCallback& failure_callback, | |
| 161 const std::string& salt, | |
| 162 const std::string& dh, | |
| 163 uint64_t rs, | |
| 164 const KeyPair& pair) { | |
| 165 if (!pair.IsInitialized()) { | |
| 166 DLOG(ERROR) << "Unable to retrieve the keys for the incoming message."; | |
| 167 failure_callback.Run(DECRYPTION_FAILURE_NO_KEYS); | |
| 168 return; | |
| 169 } | |
| 170 | |
| 171 DCHECK_EQ(KeyPair::ECDH_CURVE_25519, pair.type()); | |
| 172 | |
| 173 // TODO(peter): Support explicit keys for the decryption that don't rely | |
| 174 // on use of an HKDF. | |
| 175 | |
| 176 uint8_t shared_key[crypto::curve25519::kBytes]; | |
| 177 | |
| 178 // Calculate the shared secret for the message. | |
| 179 crypto::curve25519::ScalarMult( | |
| 180 reinterpret_cast<const unsigned char*>(pair.private_key().data()), | |
| 181 reinterpret_cast<const unsigned char*>(dh.data()), | |
| 182 shared_key); | |
| 183 | |
| 184 base::StringPiece shared_key_string_piece( | |
| 185 reinterpret_cast<char*>(shared_key), crypto::curve25519::kBytes); | |
| 186 | |
| 187 std::string plaintext; | |
| 188 | |
| 189 GCMMessageCryptographer cryptographer; | |
| 190 if (!cryptographer.Decrypt(message.raw_data, shared_key_string_piece, salt, | |
| 191 rs, &plaintext)) { | |
| 192 DLOG(ERROR) << "Unable to decrypt the incoming data."; | |
| 193 failure_callback.Run(DECRYPTION_FAILURE_INVALID_PAYLOAD); | |
| 194 return; | |
| 195 } | |
| 196 | |
| 197 IncomingMessage decrypted_message; | |
| 198 decrypted_message.collapse_key = message.collapse_key; | |
| 199 decrypted_message.sender_id = message.sender_id; | |
| 200 decrypted_message.raw_data.swap(plaintext); | |
| 201 decrypted_message.decrypted = true; | |
| 202 | |
| 203 // There must be no data associated with the decrypted message at this point, | |
| 204 // to make sure that we don't end up in an infinite decryption loop. | |
| 205 DCHECK_EQ(0u, decrypted_message.data.size()); | |
| 206 | |
| 207 success_callback.Run(decrypted_message); | |
| 208 } | |
| 209 | |
| 75 } // namespace gcm | 210 } // namespace gcm |
| OLD | NEW |