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