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 <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <sstream> | 9 #include <sstream> |
10 #include <string> | 10 #include <string> |
11 | 11 |
12 #include "base/base64url.h" | 12 #include "base/base64url.h" |
13 #include "base/bind.h" | 13 #include "base/bind.h" |
| 14 #include "base/bind_helpers.h" |
14 #include "base/files/scoped_temp_dir.h" | 15 #include "base/files/scoped_temp_dir.h" |
15 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
16 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
17 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
18 #include "base/strings/string_piece.h" | 19 #include "base/strings/string_piece.h" |
19 #include "base/strings/string_util.h" | 20 #include "base/strings/string_util.h" |
20 #include "base/test/histogram_tester.h" | 21 #include "base/test/histogram_tester.h" |
21 #include "components/gcm_driver/common/gcm_messages.h" | 22 #include "components/gcm_driver/common/gcm_messages.h" |
22 #include "components/gcm_driver/crypto/gcm_key_store.h" | 23 #include "components/gcm_driver/crypto/gcm_key_store.h" |
23 #include "components/gcm_driver/crypto/gcm_message_cryptographer.h" | 24 #include "components/gcm_driver/crypto/gcm_message_cryptographer.h" |
24 #include "components/gcm_driver/crypto/p256_key_util.h" | 25 #include "components/gcm_driver/crypto/p256_key_util.h" |
25 #include "crypto/random.h" | 26 #include "crypto/random.h" |
26 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
27 | 28 |
28 namespace gcm { | 29 namespace gcm { |
29 namespace { | 30 namespace { |
30 | 31 |
31 const char kExampleAppId[] = "my-app-id"; | 32 const char kExampleAppId[] = "my-app-id"; |
| 33 const char kExampleAuthorizedEntity[] = "my-sender-id"; |
32 const char kExampleMessage[] = "Hello, world, this is the GCM Driver!"; | 34 const char kExampleMessage[] = "Hello, world, this is the GCM Driver!"; |
33 | 35 |
34 const char kValidEncryptionHeader[] = | 36 const char kValidEncryptionHeader[] = |
35 "keyid=foo;salt=MTIzNDU2Nzg5MDEyMzQ1Ng;rs=1024"; | 37 "keyid=foo;salt=MTIzNDU2Nzg5MDEyMzQ1Ng;rs=1024"; |
36 const char kInvalidEncryptionHeader[] = "keyid"; | 38 const char kInvalidEncryptionHeader[] = "keyid"; |
37 | 39 |
38 const char kValidCryptoKeyHeader[] = | 40 const char kValidCryptoKeyHeader[] = |
39 "keyid=foo;dh=BL_UGhfudEkXMUd4U4-D4nP5KHxKjQHsW6j88ybbehXM7fqi1OMFefDUEi0eJ" | 41 "keyid=foo;dh=BL_UGhfudEkXMUd4U4-D4nP5KHxKjQHsW6j88ybbehXM7fqi1OMFefDUEi0eJ" |
40 "vsKfyVBWYkQjH-lSPJKxjAyslg"; | 42 "vsKfyVBWYkQjH-lSPJKxjAyslg"; |
41 const char kInvalidCryptoKeyHeader[] = "keyid"; | 43 const char kInvalidCryptoKeyHeader[] = "keyid"; |
(...skipping 20 matching lines...) Expand all Loading... |
62 | 64 |
63 // To be used as a callback for GCMEncryptionProvider::GetEncryptionInfo(). | 65 // To be used as a callback for GCMEncryptionProvider::GetEncryptionInfo(). |
64 void DidGetEncryptionInfo(std::string* p256dh_out, | 66 void DidGetEncryptionInfo(std::string* p256dh_out, |
65 std::string* auth_secret_out, | 67 std::string* auth_secret_out, |
66 const std::string& p256dh, | 68 const std::string& p256dh, |
67 const std::string& auth_secret) { | 69 const std::string& auth_secret) { |
68 *p256dh_out = p256dh; | 70 *p256dh_out = p256dh; |
69 *auth_secret_out = auth_secret; | 71 *auth_secret_out = auth_secret; |
70 } | 72 } |
71 | 73 |
72 // To be used as a callback for GCMKeyStore::CreateKeys(). | 74 // To be used as a callback for GCMKeyStore::{GetKeys,CreateKeys}. |
73 void DidCreateKeys(KeyPair* pair_out, std::string* auth_secret_out, | 75 void HandleKeysCallback(KeyPair* pair_out, |
74 const KeyPair& pair, const std::string& auth_secret) { | 76 std::string* auth_secret_out, |
| 77 const KeyPair& pair, |
| 78 const std::string& auth_secret) { |
75 *pair_out = pair; | 79 *pair_out = pair; |
76 *auth_secret_out = auth_secret; | 80 *auth_secret_out = auth_secret; |
77 } | 81 } |
78 | 82 |
79 protected: | 83 protected: |
80 // Decrypts the |message| and then synchronously waits until either the | 84 // Decrypts the |message| and then synchronously waits until either the |
81 // success or failure callbacks has been invoked. | 85 // success or failure callbacks has been invoked. |
82 void Decrypt(const IncomingMessage& message) { | 86 void Decrypt(const IncomingMessage& message) { |
83 encryption_provider_->DecryptMessage( | 87 encryption_provider_->DecryptMessage( |
84 kExampleAppId, message, | 88 kExampleAppId, message, |
85 base::Bind(&GCMEncryptionProviderTest::DidDecryptMessage, | 89 base::Bind(&GCMEncryptionProviderTest::DidDecryptMessage, |
86 base::Unretained(this))); | 90 base::Unretained(this))); |
87 | 91 |
88 // The encryption keys will be read asynchronously. | 92 // The encryption keys will be read asynchronously. |
89 base::RunLoop().RunUntilIdle(); | 93 base::RunLoop().RunUntilIdle(); |
90 } | 94 } |
91 | 95 |
| 96 // Checks that the underlying key store has a key for the |kExampleAppId| + |
| 97 // authorized entity pair if and only if |should_have_key| is true. Must wrap |
| 98 // with ASSERT/EXPECT_NO_FATAL_FAILURE. |
| 99 void CheckHasKey(const std::string& authorized_entity, bool should_have_key) { |
| 100 KeyPair pair; |
| 101 std::string auth_secret; |
| 102 encryption_provider()->key_store_->GetKeys( |
| 103 kExampleAppId, authorized_entity, |
| 104 false /* fallback_to_empty_authorized_entity */, |
| 105 base::Bind(&GCMEncryptionProviderTest::HandleKeysCallback, |
| 106 base::Unretained(this), &pair, &auth_secret)); |
| 107 |
| 108 base::RunLoop().RunUntilIdle(); |
| 109 |
| 110 if (should_have_key) { |
| 111 ASSERT_GT(pair.public_key().size(), 0u); |
| 112 ASSERT_GT(pair.private_key().size(), 0u); |
| 113 ASSERT_GT(auth_secret.size(), 0u); |
| 114 } else { |
| 115 ASSERT_EQ(0u, pair.public_key().size()); |
| 116 ASSERT_EQ(0u, pair.private_key().size()); |
| 117 ASSERT_EQ(0u, auth_secret.size()); |
| 118 } |
| 119 } |
| 120 |
92 // Returns the result of the previous decryption operation. | 121 // Returns the result of the previous decryption operation. |
93 GCMEncryptionProvider::DecryptionResult decryption_result() { | 122 GCMEncryptionProvider::DecryptionResult decryption_result() { |
94 return decryption_result_; | 123 return decryption_result_; |
95 } | 124 } |
96 | 125 |
97 // Returns the message resulting from the previous decryption operation. | 126 // Returns the message resulting from the previous decryption operation. |
98 const IncomingMessage& decrypted_message() { return decrypted_message_; } | 127 const IncomingMessage& decrypted_message() { return decrypted_message_; } |
99 | 128 |
100 GCMEncryptionProvider* encryption_provider() { | 129 GCMEncryptionProvider* encryption_provider() { |
101 return encryption_provider_.get(); | 130 return encryption_provider_.get(); |
102 } | 131 } |
103 | 132 |
| 133 // Performs a full round-trip test of the encryption feature. Must wrap this |
| 134 // in ASSERT_NO_FATAL_FAILURE. |
| 135 void TestEncryptionRoundTrip(const std::string& app_id, |
| 136 const std::string& authorized_entity); |
| 137 |
104 private: | 138 private: |
105 void DidDecryptMessage(GCMEncryptionProvider::DecryptionResult result, | 139 void DidDecryptMessage(GCMEncryptionProvider::DecryptionResult result, |
106 const IncomingMessage& message) { | 140 const IncomingMessage& message) { |
107 decryption_result_ = result; | 141 decryption_result_ = result; |
108 decrypted_message_ = message; | 142 decrypted_message_ = message; |
109 } | 143 } |
110 | 144 |
111 base::MessageLoop message_loop_; | 145 base::MessageLoop message_loop_; |
112 base::ScopedTempDir scoped_temp_dir_; | 146 base::ScopedTempDir scoped_temp_dir_; |
113 base::HistogramTester histogram_tester_; | 147 base::HistogramTester histogram_tester_; |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 message.data["encryption"] = kValidEncryptionHeader; | 234 message.data["encryption"] = kValidEncryptionHeader; |
201 message.data["crypto-key"] = kValidCryptoKeyHeader; | 235 message.data["crypto-key"] = kValidCryptoKeyHeader; |
202 message.raw_data = "foo"; | 236 message.raw_data = "foo"; |
203 | 237 |
204 ASSERT_NO_FATAL_FAILURE(Decrypt(message)); | 238 ASSERT_NO_FATAL_FAILURE(Decrypt(message)); |
205 EXPECT_EQ(GCMEncryptionProvider::DECRYPTION_RESULT_NO_KEYS, | 239 EXPECT_EQ(GCMEncryptionProvider::DECRYPTION_RESULT_NO_KEYS, |
206 decryption_result()); | 240 decryption_result()); |
207 | 241 |
208 std::string public_key, auth_secret; | 242 std::string public_key, auth_secret; |
209 encryption_provider()->GetEncryptionInfo( | 243 encryption_provider()->GetEncryptionInfo( |
210 kExampleAppId, | 244 kExampleAppId, "" /* empty authorized entity for non-InstanceID */, |
211 base::Bind(&GCMEncryptionProviderTest::DidGetEncryptionInfo, | 245 base::Bind(&GCMEncryptionProviderTest::DidGetEncryptionInfo, |
212 base::Unretained(this), &public_key, &auth_secret)); | 246 base::Unretained(this), &public_key, &auth_secret)); |
213 | 247 |
214 // Getting (or creating) the public key will be done asynchronously. | 248 // Getting (or creating) the public key will be done asynchronously. |
215 base::RunLoop().RunUntilIdle(); | 249 base::RunLoop().RunUntilIdle(); |
216 | 250 |
217 ASSERT_GT(public_key.size(), 0u); | 251 ASSERT_GT(public_key.size(), 0u); |
218 ASSERT_GT(auth_secret.size(), 0u); | 252 ASSERT_GT(auth_secret.size(), 0u); |
219 | 253 |
220 ASSERT_NO_FATAL_FAILURE(Decrypt(message)); | 254 ASSERT_NO_FATAL_FAILURE(Decrypt(message)); |
221 EXPECT_NE(GCMEncryptionProvider::DECRYPTION_RESULT_NO_KEYS, | 255 EXPECT_NE(GCMEncryptionProvider::DECRYPTION_RESULT_NO_KEYS, |
222 decryption_result()); | 256 decryption_result()); |
223 } | 257 } |
224 | 258 |
225 TEST_F(GCMEncryptionProviderTest, EncryptionRoundTrip) { | 259 TEST_F(GCMEncryptionProviderTest, VerifiesKeyRemovalGCMRegistration) { |
| 260 // Removing encryption info for an InstanceID token shouldn't affect a |
| 261 // non-InstanceID GCM registration. |
| 262 |
| 263 // Non-InstanceID callers pass an empty string for authorized_entity. |
| 264 std::string authorized_entity_gcm = ""; |
| 265 std::string authorized_entity_1 = kExampleAuthorizedEntity + std::string("1"); |
| 266 std::string authorized_entity_2 = kExampleAuthorizedEntity + std::string("2"); |
| 267 |
| 268 // Should create encryption info. |
| 269 std::string public_key, auth_secret; |
| 270 encryption_provider()->GetEncryptionInfo( |
| 271 kExampleAppId, authorized_entity_gcm, |
| 272 base::Bind(&GCMEncryptionProviderTest::DidGetEncryptionInfo, |
| 273 base::Unretained(this), &public_key, &auth_secret)); |
| 274 |
| 275 base::RunLoop().RunUntilIdle(); |
| 276 |
| 277 // Should get encryption info created above. |
| 278 std::string read_public_key, read_auth_secret; |
| 279 encryption_provider()->GetEncryptionInfo( |
| 280 kExampleAppId, authorized_entity_gcm, |
| 281 base::Bind(&GCMEncryptionProviderTest::DidGetEncryptionInfo, |
| 282 base::Unretained(this), &read_public_key, &read_auth_secret)); |
| 283 |
| 284 base::RunLoop().RunUntilIdle(); |
| 285 |
| 286 EXPECT_GT(public_key.size(), 0u); |
| 287 EXPECT_GT(auth_secret.size(), 0u); |
| 288 EXPECT_EQ(public_key, read_public_key); |
| 289 EXPECT_EQ(auth_secret, read_auth_secret); |
| 290 |
| 291 ASSERT_NO_FATAL_FAILURE(CheckHasKey(authorized_entity_gcm, true)); |
| 292 |
| 293 encryption_provider()->RemoveEncryptionInfo( |
| 294 kExampleAppId, authorized_entity_1, base::Bind(&base::DoNothing)); |
| 295 |
| 296 base::RunLoop().RunUntilIdle(); |
| 297 |
| 298 ASSERT_NO_FATAL_FAILURE(CheckHasKey(authorized_entity_gcm, true)); |
| 299 |
| 300 encryption_provider()->RemoveEncryptionInfo(kExampleAppId, "*", |
| 301 base::Bind(&base::DoNothing)); |
| 302 |
| 303 base::RunLoop().RunUntilIdle(); |
| 304 |
| 305 ASSERT_NO_FATAL_FAILURE(CheckHasKey(authorized_entity_gcm, true)); |
| 306 |
| 307 encryption_provider()->RemoveEncryptionInfo( |
| 308 kExampleAppId, authorized_entity_gcm, base::Bind(&base::DoNothing)); |
| 309 |
| 310 base::RunLoop().RunUntilIdle(); |
| 311 |
| 312 ASSERT_NO_FATAL_FAILURE(CheckHasKey(authorized_entity_gcm, false)); |
| 313 } |
| 314 |
| 315 TEST_F(GCMEncryptionProviderTest, VerifiesKeyRemovalInstanceIDToken) { |
| 316 // Removing encryption info for a non-InstanceID GCM registration shouldn't |
| 317 // affect an InstanceID token. |
| 318 |
| 319 // Non-InstanceID callers pass an empty string for authorized_entity. |
| 320 std::string authorized_entity_gcm = ""; |
| 321 std::string authorized_entity_1 = kExampleAuthorizedEntity + std::string("1"); |
| 322 std::string authorized_entity_2 = kExampleAuthorizedEntity + std::string("2"); |
| 323 |
| 324 std::string public_key_1, auth_secret_1; |
| 325 encryption_provider()->GetEncryptionInfo( |
| 326 kExampleAppId, authorized_entity_1, |
| 327 base::Bind(&GCMEncryptionProviderTest::DidGetEncryptionInfo, |
| 328 base::Unretained(this), &public_key_1, &auth_secret_1)); |
| 329 |
| 330 base::RunLoop().RunUntilIdle(); |
| 331 |
| 332 EXPECT_GT(public_key_1.size(), 0u); |
| 333 EXPECT_GT(auth_secret_1.size(), 0u); |
| 334 |
| 335 ASSERT_NO_FATAL_FAILURE(CheckHasKey(authorized_entity_1, true)); |
| 336 ASSERT_NO_FATAL_FAILURE(CheckHasKey(authorized_entity_2, false)); |
| 337 |
| 338 std::string public_key_2, auth_secret_2; |
| 339 encryption_provider()->GetEncryptionInfo( |
| 340 kExampleAppId, authorized_entity_2, |
| 341 base::Bind(&GCMEncryptionProviderTest::DidGetEncryptionInfo, |
| 342 base::Unretained(this), &public_key_2, &auth_secret_2)); |
| 343 |
| 344 base::RunLoop().RunUntilIdle(); |
| 345 |
| 346 EXPECT_GT(public_key_2.size(), 0u); |
| 347 EXPECT_GT(auth_secret_2.size(), 0u); |
| 348 EXPECT_NE(public_key_1, public_key_2); |
| 349 EXPECT_NE(auth_secret_1, auth_secret_2); |
| 350 |
| 351 ASSERT_NO_FATAL_FAILURE(CheckHasKey(authorized_entity_1, true)); |
| 352 ASSERT_NO_FATAL_FAILURE(CheckHasKey(authorized_entity_2, true)); |
| 353 |
| 354 std::string read_public_key_1, read_auth_secret_1; |
| 355 encryption_provider()->GetEncryptionInfo( |
| 356 kExampleAppId, authorized_entity_1, |
| 357 base::Bind(&GCMEncryptionProviderTest::DidGetEncryptionInfo, |
| 358 base::Unretained(this), &read_public_key_1, |
| 359 &read_auth_secret_1)); |
| 360 |
| 361 base::RunLoop().RunUntilIdle(); |
| 362 |
| 363 // Should have returned existing info for authorized_entity_1. |
| 364 EXPECT_EQ(public_key_1, read_public_key_1); |
| 365 EXPECT_EQ(auth_secret_1, read_auth_secret_1); |
| 366 |
| 367 encryption_provider()->RemoveEncryptionInfo( |
| 368 kExampleAppId, authorized_entity_gcm, base::Bind(&base::DoNothing)); |
| 369 |
| 370 base::RunLoop().RunUntilIdle(); |
| 371 |
| 372 ASSERT_NO_FATAL_FAILURE(CheckHasKey(authorized_entity_1, true)); |
| 373 ASSERT_NO_FATAL_FAILURE(CheckHasKey(authorized_entity_2, true)); |
| 374 |
| 375 encryption_provider()->RemoveEncryptionInfo( |
| 376 kExampleAppId, authorized_entity_1, base::Bind(&base::DoNothing)); |
| 377 |
| 378 base::RunLoop().RunUntilIdle(); |
| 379 |
| 380 ASSERT_NO_FATAL_FAILURE(CheckHasKey(authorized_entity_1, false)); |
| 381 ASSERT_NO_FATAL_FAILURE(CheckHasKey(authorized_entity_2, true)); |
| 382 |
| 383 std::string public_key_1_refreshed, auth_secret_1_refreshed; |
| 384 encryption_provider()->GetEncryptionInfo( |
| 385 kExampleAppId, authorized_entity_1, |
| 386 base::Bind(&GCMEncryptionProviderTest::DidGetEncryptionInfo, |
| 387 base::Unretained(this), &public_key_1_refreshed, |
| 388 &auth_secret_1_refreshed)); |
| 389 |
| 390 base::RunLoop().RunUntilIdle(); |
| 391 |
| 392 // Since the info was removed, GetEncryptionInfo should have created new info. |
| 393 EXPECT_GT(public_key_1_refreshed.size(), 0u); |
| 394 EXPECT_GT(auth_secret_1_refreshed.size(), 0u); |
| 395 EXPECT_NE(public_key_1, public_key_1_refreshed); |
| 396 EXPECT_NE(auth_secret_1, auth_secret_1_refreshed); |
| 397 EXPECT_NE(public_key_2, public_key_1_refreshed); |
| 398 EXPECT_NE(auth_secret_2, auth_secret_1_refreshed); |
| 399 |
| 400 ASSERT_NO_FATAL_FAILURE(CheckHasKey(authorized_entity_1, true)); |
| 401 ASSERT_NO_FATAL_FAILURE(CheckHasKey(authorized_entity_2, true)); |
| 402 |
| 403 encryption_provider()->RemoveEncryptionInfo(kExampleAppId, "*", |
| 404 base::Bind(&base::DoNothing)); |
| 405 |
| 406 base::RunLoop().RunUntilIdle(); |
| 407 |
| 408 ASSERT_NO_FATAL_FAILURE(CheckHasKey(authorized_entity_1, false)); |
| 409 ASSERT_NO_FATAL_FAILURE(CheckHasKey(authorized_entity_2, false)); |
| 410 } |
| 411 |
| 412 void GCMEncryptionProviderTest::TestEncryptionRoundTrip( |
| 413 const std::string& app_id, |
| 414 const std::string& authorized_entity) { |
226 // Performs a full round-trip of the encryption feature, including getting a | 415 // Performs a full round-trip of the encryption feature, including getting a |
227 // public/private key-pair and performing the cryptographic operations. This | 416 // public/private key-pair and performing the cryptographic operations. This |
228 // is more of an integration test than a unit test. | 417 // is more of an integration test than a unit test. |
229 | 418 |
230 KeyPair pair, server_pair; | 419 KeyPair pair, server_pair; |
231 std::string auth_secret, server_authentication; | 420 std::string auth_secret, server_authentication; |
232 | 421 |
233 // Retrieve the public/private key-pair immediately from the key store, given | 422 // Retrieve the public/private key-pair immediately from the key store, given |
234 // that the GCMEncryptionProvider will only share the public key with users. | 423 // that the GCMEncryptionProvider will only share the public key with users. |
235 // Also create a second pair, which will act as the server's keys. | 424 // Also create a second pair, which will act as the server's keys. |
236 encryption_provider()->key_store_->CreateKeys( | 425 encryption_provider()->key_store_->CreateKeys( |
237 kExampleAppId, | 426 app_id, authorized_entity, |
238 base::Bind(&GCMEncryptionProviderTest::DidCreateKeys, | 427 base::Bind(&GCMEncryptionProviderTest::HandleKeysCallback, |
239 base::Unretained(this), &pair, &auth_secret)); | 428 base::Unretained(this), &pair, &auth_secret)); |
240 | 429 |
241 encryption_provider()->key_store_->CreateKeys( | 430 encryption_provider()->key_store_->CreateKeys( |
242 std::string(kExampleAppId) + "-server", | 431 "server-" + app_id, authorized_entity, |
243 base::Bind(&GCMEncryptionProviderTest::DidCreateKeys, | 432 base::Bind(&GCMEncryptionProviderTest::HandleKeysCallback, |
244 base::Unretained(this), &server_pair, &server_authentication)); | 433 base::Unretained(this), &server_pair, &server_authentication)); |
245 | 434 |
246 // Creating the public keys will be done asynchronously. | 435 // Creating the public keys will be done asynchronously. |
247 base::RunLoop().RunUntilIdle(); | 436 base::RunLoop().RunUntilIdle(); |
248 | 437 |
249 ASSERT_GT(pair.public_key().size(), 0u); | 438 ASSERT_GT(pair.public_key().size(), 0u); |
250 ASSERT_GT(server_pair.public_key().size(), 0u); | 439 ASSERT_GT(server_pair.public_key().size(), 0u); |
251 | 440 |
252 ASSERT_GT(pair.private_key().size(), 0u); | 441 ASSERT_GT(pair.private_key().size(), 0u); |
253 ASSERT_GT(server_pair.private_key().size(), 0u); | 442 ASSERT_GT(server_pair.private_key().size(), 0u); |
254 | 443 |
255 std::string salt; | 444 std::string salt; |
256 | 445 |
257 // Creates a cryptographically secure salt of |salt_size| octets in size, and | 446 // Creates a cryptographically secure salt of |salt_size| octets in size, and |
258 // calculate the shared secret for the message. | 447 // calculate the shared secret for the message. |
259 crypto::RandBytes(base::WriteInto(&salt, 16 + 1), 16); | 448 crypto::RandBytes(base::WriteInto(&salt, 16 + 1), 16); |
260 | 449 |
261 std::string shared_secret; | 450 std::string shared_secret; |
262 ASSERT_TRUE(ComputeSharedP256Secret( | 451 ASSERT_TRUE(ComputeSharedP256Secret( |
263 pair.private_key(), pair.public_key_x509(), server_pair.public_key(), | 452 pair.private_key(), pair.public_key_x509(), server_pair.public_key(), |
264 &shared_secret)); | 453 &shared_secret)); |
265 | 454 |
266 IncomingMessage message; | 455 IncomingMessage message; |
267 size_t record_size; | 456 size_t record_size; |
268 | 457 |
| 458 message.sender_id = kExampleAuthorizedEntity; |
| 459 |
269 // Encrypts the |kExampleMessage| using the generated shared key and the | 460 // Encrypts the |kExampleMessage| using the generated shared key and the |
270 // random |salt|, storing the result in |record_size| and the message. | 461 // random |salt|, storing the result in |record_size| and the message. |
271 GCMMessageCryptographer cryptographer( | 462 GCMMessageCryptographer cryptographer( |
272 GCMMessageCryptographer::Label::P256, pair.public_key(), | 463 GCMMessageCryptographer::Label::P256, pair.public_key(), |
273 server_pair.public_key(), auth_secret); | 464 server_pair.public_key(), auth_secret); |
274 | 465 |
275 ASSERT_TRUE(cryptographer.Encrypt(kExampleMessage, shared_secret, salt, | 466 ASSERT_TRUE(cryptographer.Encrypt(kExampleMessage, shared_secret, salt, |
276 &record_size, &message.raw_data)); | 467 &record_size, &message.raw_data)); |
277 | 468 |
278 std::string encoded_salt, encoded_key; | 469 std::string encoded_salt, encoded_key; |
(...skipping 16 matching lines...) Expand all Loading... |
295 | 486 |
296 // Decrypt the message, and expect everything to go wonderfully well. | 487 // Decrypt the message, and expect everything to go wonderfully well. |
297 ASSERT_NO_FATAL_FAILURE(Decrypt(message)); | 488 ASSERT_NO_FATAL_FAILURE(Decrypt(message)); |
298 ASSERT_EQ(GCMEncryptionProvider::DECRYPTION_RESULT_DECRYPTED, | 489 ASSERT_EQ(GCMEncryptionProvider::DECRYPTION_RESULT_DECRYPTED, |
299 decryption_result()); | 490 decryption_result()); |
300 | 491 |
301 EXPECT_TRUE(decrypted_message().decrypted); | 492 EXPECT_TRUE(decrypted_message().decrypted); |
302 EXPECT_EQ(kExampleMessage, decrypted_message().raw_data); | 493 EXPECT_EQ(kExampleMessage, decrypted_message().raw_data); |
303 } | 494 } |
304 | 495 |
| 496 TEST_F(GCMEncryptionProviderTest, EncryptionRoundTripGCMRegistration) { |
| 497 // GCMEncryptionProvider::DecryptMessage should succeed when the message was |
| 498 // sent to a non-InstanceID GCM registration (empty authorized_entity). |
| 499 ASSERT_NO_FATAL_FAILURE(TestEncryptionRoundTrip( |
| 500 kExampleAppId, "" /* empty authorized entity for non-InstanceID */)); |
| 501 } |
| 502 |
| 503 TEST_F(GCMEncryptionProviderTest, EncryptionRoundTripInstanceIDToken) { |
| 504 // GCMEncryptionProvider::DecryptMessage should succeed when the message was |
| 505 // sent to an InstanceID token (non-empty authorized_entity). |
| 506 ASSERT_NO_FATAL_FAILURE( |
| 507 TestEncryptionRoundTrip(kExampleAppId, kExampleAuthorizedEntity)); |
| 508 } |
| 509 |
305 } // namespace gcm | 510 } // namespace gcm |
OLD | NEW |