fake_secure_message_delegate_unittest.cc 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ash/components/multidevice/fake_secure_message_delegate.h"
  5. #include "base/bind.h"
  6. #include "base/callback.h"
  7. #include "testing/gtest/include/gtest/gtest.h"
  8. namespace ash::multidevice {
  9. namespace {
  10. const char kTestPublicKey[] = "the private key is in another castle";
  11. const char kPayload[] = "500 tons of uranium";
  12. const char kSymmetricKey[] = "hunter2";
  13. const char kPublicMetadata[] = "brought to you by our sponsors";
  14. const char kAssociatedData[] = "save 20% bytes on your nonce insurance";
  15. const char kVerificationKeyId[] = "the one with the red stripes";
  16. const char kDecryptionKeyId[] = "it's in your pocket somewhere";
  17. // Callback for saving the result of GenerateKeys().
  18. void SaveKeyPair(std::string* private_key_out,
  19. std::string* public_key_out,
  20. const std::string& private_key,
  21. const std::string& public_key) {
  22. *private_key_out = private_key;
  23. *public_key_out = public_key;
  24. }
  25. // Callback for saving the result of DeriveKey() and CreateSecureMessage().
  26. void SaveString(std::string* out, const std::string& value) {
  27. *out = value;
  28. }
  29. // Callback for saving the result of UnwrapSecureMessage().
  30. void SaveUnwrapResults(std::string* payload_out,
  31. securemessage::Header* header_out,
  32. bool verified,
  33. const std::string& payload,
  34. const securemessage::Header& header) {
  35. ASSERT_TRUE(verified);
  36. *payload_out = payload;
  37. *header_out = header;
  38. }
  39. // Returns the CreateOptions struct to create the test message.
  40. SecureMessageDelegate::CreateOptions GetCreateOptions(
  41. securemessage::EncScheme encryption_scheme,
  42. securemessage::SigScheme signature_scheme) {
  43. SecureMessageDelegate::CreateOptions create_options;
  44. create_options.encryption_scheme = encryption_scheme;
  45. create_options.signature_scheme = signature_scheme;
  46. create_options.public_metadata = kPublicMetadata;
  47. create_options.associated_data = kAssociatedData;
  48. create_options.verification_key_id = kVerificationKeyId;
  49. create_options.decryption_key_id = kDecryptionKeyId;
  50. return create_options;
  51. }
  52. // Returns the UnwrapOptions struct to unwrap the test message.
  53. SecureMessageDelegate::UnwrapOptions GetUnwrapOptions(
  54. securemessage::EncScheme encryption_scheme,
  55. securemessage::SigScheme signature_scheme) {
  56. SecureMessageDelegate::UnwrapOptions unwrap_options;
  57. unwrap_options.encryption_scheme = encryption_scheme;
  58. unwrap_options.signature_scheme = signature_scheme;
  59. unwrap_options.associated_data = kAssociatedData;
  60. return unwrap_options;
  61. }
  62. void CheckSerializedSecureMessage(
  63. const std::string& serialized_message,
  64. const SecureMessageDelegate::CreateOptions& create_options) {
  65. securemessage::SecureMessage secure_message;
  66. ASSERT_TRUE(secure_message.ParseFromString(serialized_message));
  67. securemessage::HeaderAndBody header_and_body;
  68. ASSERT_TRUE(
  69. header_and_body.ParseFromString(secure_message.header_and_body()));
  70. const securemessage::Header& header = header_and_body.header();
  71. EXPECT_EQ(create_options.signature_scheme, header.signature_scheme());
  72. EXPECT_EQ(create_options.encryption_scheme, header.encryption_scheme());
  73. EXPECT_EQ(create_options.verification_key_id, header.verification_key_id());
  74. EXPECT_EQ(create_options.decryption_key_id, header.decryption_key_id());
  75. EXPECT_EQ(create_options.public_metadata, header.public_metadata());
  76. }
  77. } // namespace
  78. class CryptAuthFakeSecureMessageDelegateTest : public testing::Test {
  79. public:
  80. CryptAuthFakeSecureMessageDelegateTest(
  81. const CryptAuthFakeSecureMessageDelegateTest&) = delete;
  82. CryptAuthFakeSecureMessageDelegateTest& operator=(
  83. const CryptAuthFakeSecureMessageDelegateTest&) = delete;
  84. protected:
  85. CryptAuthFakeSecureMessageDelegateTest() {}
  86. FakeSecureMessageDelegate delegate_;
  87. };
  88. TEST_F(CryptAuthFakeSecureMessageDelegateTest, GenerateKeyPair) {
  89. std::string public_key1, private_key1;
  90. delegate_.GenerateKeyPair(
  91. base::BindOnce(&SaveKeyPair, &public_key1, &private_key1));
  92. EXPECT_NE(private_key1, public_key1);
  93. std::string public_key2, private_key2;
  94. delegate_.GenerateKeyPair(
  95. base::BindOnce(&SaveKeyPair, &public_key2, &private_key2));
  96. EXPECT_NE(private_key2, public_key2);
  97. EXPECT_NE(public_key1, public_key2);
  98. EXPECT_NE(private_key1, private_key2);
  99. delegate_.set_next_public_key(kTestPublicKey);
  100. std::string public_key3, private_key3;
  101. delegate_.GenerateKeyPair(
  102. base::BindOnce(&SaveKeyPair, &public_key3, &private_key3));
  103. EXPECT_EQ(kTestPublicKey, public_key3);
  104. EXPECT_NE(private_key3, public_key3);
  105. EXPECT_NE(public_key1, public_key3);
  106. EXPECT_NE(private_key1, private_key3);
  107. }
  108. TEST_F(CryptAuthFakeSecureMessageDelegateTest, DeriveKey) {
  109. delegate_.set_next_public_key("key_pair_1");
  110. std::string public_key1, private_key1;
  111. delegate_.GenerateKeyPair(
  112. base::BindOnce(&SaveKeyPair, &public_key1, &private_key1));
  113. delegate_.set_next_public_key("key_pair_2");
  114. std::string public_key2, private_key2;
  115. delegate_.GenerateKeyPair(
  116. base::BindOnce(&SaveKeyPair, &public_key2, &private_key2));
  117. std::string symmetric_key1, symmetric_key2;
  118. delegate_.DeriveKey(private_key1, public_key2,
  119. base::BindOnce(&SaveString, &symmetric_key1));
  120. delegate_.DeriveKey(private_key2, public_key1,
  121. base::BindOnce(&SaveString, &symmetric_key2));
  122. EXPECT_EQ(symmetric_key1, symmetric_key2);
  123. }
  124. TEST_F(CryptAuthFakeSecureMessageDelegateTest,
  125. CreateAndUnwrapWithSymmetricKey) {
  126. // Create SecureMessage using symmetric key.
  127. SecureMessageDelegate::CreateOptions create_options =
  128. GetCreateOptions(securemessage::AES_256_CBC, securemessage::HMAC_SHA256);
  129. std::string serialized_message;
  130. delegate_.CreateSecureMessage(
  131. kPayload, kSymmetricKey, create_options,
  132. base::BindOnce(&SaveString, &serialized_message));
  133. CheckSerializedSecureMessage(serialized_message, create_options);
  134. // Unwrap SecureMessage using symmetric key.
  135. SecureMessageDelegate::UnwrapOptions unwrap_options =
  136. GetUnwrapOptions(securemessage::AES_256_CBC, securemessage::HMAC_SHA256);
  137. std::string payload;
  138. securemessage::Header header;
  139. delegate_.UnwrapSecureMessage(
  140. serialized_message, kSymmetricKey, unwrap_options,
  141. base::BindOnce(&SaveUnwrapResults, &payload, &header));
  142. EXPECT_EQ(kPayload, payload);
  143. }
  144. TEST_F(CryptAuthFakeSecureMessageDelegateTest,
  145. CreateAndUnwrapWithAsymmetricKey) {
  146. delegate_.set_next_public_key(kTestPublicKey);
  147. std::string public_key, private_key;
  148. delegate_.GenerateKeyPair(
  149. base::BindOnce(&SaveKeyPair, &public_key, &private_key));
  150. // Create SecureMessage using asymmetric key.
  151. SecureMessageDelegate::CreateOptions create_options =
  152. GetCreateOptions(securemessage::NONE, securemessage::ECDSA_P256_SHA256);
  153. std::string serialized_message;
  154. delegate_.CreateSecureMessage(
  155. kPayload, private_key, create_options,
  156. base::BindOnce(&SaveString, &serialized_message));
  157. CheckSerializedSecureMessage(serialized_message, create_options);
  158. // Unwrap SecureMessage using symmetric key.
  159. SecureMessageDelegate::UnwrapOptions unwrap_options =
  160. GetUnwrapOptions(securemessage::NONE, securemessage::ECDSA_P256_SHA256);
  161. std::string payload;
  162. securemessage::Header header;
  163. delegate_.UnwrapSecureMessage(
  164. serialized_message, public_key, unwrap_options,
  165. base::BindOnce(&SaveUnwrapResults, &payload, &header));
  166. EXPECT_EQ(kPayload, payload);
  167. }
  168. TEST_F(CryptAuthFakeSecureMessageDelegateTest, GetPrivateKeyForPublicKey) {
  169. delegate_.set_next_public_key(kTestPublicKey);
  170. std::string public_key, private_key;
  171. delegate_.GenerateKeyPair(
  172. base::BindOnce(&SaveKeyPair, &public_key, &private_key));
  173. EXPECT_EQ(kTestPublicKey, public_key);
  174. EXPECT_EQ(private_key, delegate_.GetPrivateKeyForPublicKey(kTestPublicKey));
  175. }
  176. } // namespace ash::multidevice