| 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 "base/base64url.h" | 5 #include "base/base64url.h" |
| 6 #include "base/bind.h" | 6 #include "base/bind.h" |
| 7 #include "base/macros.h" | 7 #include "base/macros.h" |
| 8 #include "components/cryptauth/device_to_device_initiator_operations.h" | 8 #include "components/cryptauth/device_to_device_initiator_operations.h" |
| 9 #include "components/cryptauth/device_to_device_responder_operations.h" | 9 #include "components/cryptauth/device_to_device_responder_operations.h" |
| 10 #include "components/cryptauth/fake_secure_message_delegate.h" | 10 #include "components/cryptauth/fake_secure_message_delegate.h" |
| 11 #include "components/cryptauth/session_keys.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 13 |
| 13 namespace cryptauth { | 14 namespace cryptauth { |
| 14 | 15 |
| 15 namespace { | 16 namespace { |
| 16 | 17 |
| 17 // The initiator's session public key in base64url form. Note that this is | 18 // The initiator's session public key in base64url form. Note that this is |
| 18 // actually a serialized proto. | 19 // actually a serialized proto. |
| 19 const char kInitiatorSessionPublicKeyBase64[] = | 20 const char kInitiatorSessionPublicKeyBase64[] = |
| 20 "CAESRQogOlH8DgPMQu7eAt-b6yoTXcazG8mAl6SPC5Ds-LTULIcSIQDZDMqsoYRO4tNMej1FB" | 21 "CAESRQogOlH8DgPMQu7eAt-b6yoTXcazG8mAl6SPC5Ds-LTULIcSIQDZDMqsoYRO4tNMej1FB" |
| (...skipping 24 matching lines...) Expand all Loading... |
| 45 // ValidateHelloMessage operations, saving both the outcome and the returned | 46 // ValidateHelloMessage operations, saving both the outcome and the returned |
| 46 // key. | 47 // key. |
| 47 void SaveValidationResultWithKey(bool* out_success, | 48 void SaveValidationResultWithKey(bool* out_success, |
| 48 std::string* out_key, | 49 std::string* out_key, |
| 49 bool success, | 50 bool success, |
| 50 const std::string& key) { | 51 const std::string& key) { |
| 51 *out_success = success; | 52 *out_success = success; |
| 52 *out_key = key; | 53 *out_key = key; |
| 53 } | 54 } |
| 54 | 55 |
| 56 void SaveValidationResultWithSessionKeys(bool* out_success, |
| 57 SessionKeys* out_keys, |
| 58 bool success, |
| 59 const SessionKeys& keys) { |
| 60 *out_success = success; |
| 61 *out_keys = keys; |
| 62 } |
| 63 |
| 55 } // namespace | 64 } // namespace |
| 56 | 65 |
| 57 class ProximityAuthDeviceToDeviceOperationsTest : public testing::Test { | 66 class ProximityAuthDeviceToDeviceOperationsTest : public testing::Test { |
| 58 protected: | 67 protected: |
| 59 ProximityAuthDeviceToDeviceOperationsTest() {} | 68 ProximityAuthDeviceToDeviceOperationsTest() {} |
| 60 ~ProximityAuthDeviceToDeviceOperationsTest() override {} | 69 ~ProximityAuthDeviceToDeviceOperationsTest() override {} |
| 61 | 70 |
| 62 void SetUp() override { | 71 void SetUp() override { |
| 63 ASSERT_TRUE( | 72 ASSERT_TRUE( |
| 64 base::Base64UrlDecode(kInitiatorSessionPublicKeyBase64, | 73 base::Base64UrlDecode(kInitiatorSessionPublicKeyBase64, |
| 65 base::Base64UrlDecodePolicy::REQUIRE_PADDING, | 74 base::Base64UrlDecodePolicy::REQUIRE_PADDING, |
| 66 &local_session_public_key_)); | 75 &local_session_public_key_)); |
| 67 local_session_private_key_ = | 76 local_session_private_key_ = |
| 68 secure_message_delegate_.GetPrivateKeyForPublicKey( | 77 secure_message_delegate_.GetPrivateKeyForPublicKey( |
| 69 local_session_public_key_); | 78 local_session_public_key_); |
| 70 | 79 |
| 71 ASSERT_TRUE( | 80 ASSERT_TRUE( |
| 72 base::Base64UrlDecode(kResponderSessionPublicKeyBase64, | 81 base::Base64UrlDecode(kResponderSessionPublicKeyBase64, |
| 73 base::Base64UrlDecodePolicy::REQUIRE_PADDING, | 82 base::Base64UrlDecodePolicy::REQUIRE_PADDING, |
| 74 &remote_session_public_key_)); | 83 &remote_session_public_key_)); |
| 75 remote_session_private_key_ = | 84 remote_session_private_key_ = |
| 76 secure_message_delegate_.GetPrivateKeyForPublicKey( | 85 secure_message_delegate_.GetPrivateKeyForPublicKey( |
| 77 remote_session_public_key_); | 86 remote_session_public_key_); |
| 78 | 87 |
| 79 // Note: FakeSecureMessageDelegate functions are synchronous. | 88 // Note: FakeSecureMessageDelegate functions are synchronous. |
| 80 secure_message_delegate_.DeriveKey( | 89 secure_message_delegate_.DeriveKey( |
| 81 local_session_private_key_, remote_session_public_key_, | 90 local_session_private_key_, remote_session_public_key_, |
| 82 base::Bind(&SaveMessageResult, &session_symmetric_key_)); | 91 base::Bind(&SaveMessageResult, &session_symmetric_key_)); |
| 92 session_keys_ = SessionKeys(session_symmetric_key_); |
| 83 | 93 |
| 84 persistent_symmetric_key_ = "persistent symmetric key"; | 94 persistent_symmetric_key_ = "persistent symmetric key"; |
| 85 } | 95 } |
| 86 | 96 |
| 87 // Creates the initator's [Hello] message. | 97 // Creates the initator's [Hello] message. |
| 88 std::string CreateHelloMessage() { | 98 std::string CreateHelloMessage() { |
| 89 std::string hello_message; | 99 std::string hello_message; |
| 90 DeviceToDeviceInitiatorOperations::CreateHelloMessage( | 100 DeviceToDeviceInitiatorOperations::CreateHelloMessage( |
| 91 local_session_public_key_, persistent_symmetric_key_, | 101 local_session_public_key_, persistent_symmetric_key_, |
| 92 &secure_message_delegate_, | 102 &secure_message_delegate_, |
| (...skipping 16 matching lines...) Expand all Loading... |
| 109 base::Bind(&SaveMessageResult, &remote_auth_message)); | 119 base::Bind(&SaveMessageResult, &remote_auth_message)); |
| 110 EXPECT_FALSE(remote_auth_message.empty()); | 120 EXPECT_FALSE(remote_auth_message.empty()); |
| 111 return remote_auth_message; | 121 return remote_auth_message; |
| 112 } | 122 } |
| 113 | 123 |
| 114 // Creates the initiator's [Initiator Auth] message. | 124 // Creates the initiator's [Initiator Auth] message. |
| 115 std::string CreateInitiatorAuthMessage( | 125 std::string CreateInitiatorAuthMessage( |
| 116 const std::string& remote_auth_message) { | 126 const std::string& remote_auth_message) { |
| 117 std::string local_auth_message; | 127 std::string local_auth_message; |
| 118 DeviceToDeviceInitiatorOperations::CreateInitiatorAuthMessage( | 128 DeviceToDeviceInitiatorOperations::CreateInitiatorAuthMessage( |
| 119 session_symmetric_key_, persistent_symmetric_key_, remote_auth_message, | 129 session_keys_, persistent_symmetric_key_, remote_auth_message, |
| 120 &secure_message_delegate_, | 130 &secure_message_delegate_, |
| 121 base::Bind(&SaveMessageResult, &local_auth_message)); | 131 base::Bind(&SaveMessageResult, &local_auth_message)); |
| 122 EXPECT_FALSE(local_auth_message.empty()); | 132 EXPECT_FALSE(local_auth_message.empty()); |
| 123 return local_auth_message; | 133 return local_auth_message; |
| 124 } | 134 } |
| 125 | 135 |
| 126 FakeSecureMessageDelegate secure_message_delegate_; | 136 FakeSecureMessageDelegate secure_message_delegate_; |
| 127 | 137 |
| 128 std::string persistent_symmetric_key_; | 138 std::string persistent_symmetric_key_; |
| 129 std::string local_session_public_key_; | 139 std::string local_session_public_key_; |
| 130 std::string local_session_private_key_; | 140 std::string local_session_private_key_; |
| 131 std::string remote_session_public_key_; | 141 std::string remote_session_public_key_; |
| 132 std::string remote_session_private_key_; | 142 std::string remote_session_private_key_; |
| 133 std::string session_symmetric_key_; | 143 std::string session_symmetric_key_; |
| 144 SessionKeys session_keys_; |
| 134 | 145 |
| 135 DISALLOW_COPY_AND_ASSIGN(ProximityAuthDeviceToDeviceOperationsTest); | 146 DISALLOW_COPY_AND_ASSIGN(ProximityAuthDeviceToDeviceOperationsTest); |
| 136 }; | 147 }; |
| 137 | 148 |
| 138 TEST_F(ProximityAuthDeviceToDeviceOperationsTest, | 149 TEST_F(ProximityAuthDeviceToDeviceOperationsTest, |
| 139 ValidateHelloMessage_Success) { | 150 ValidateHelloMessage_Success) { |
| 140 bool validation_success = false; | 151 bool validation_success = false; |
| 141 std::string hello_public_key; | 152 std::string hello_public_key; |
| 142 DeviceToDeviceResponderOperations::ValidateHelloMessage( | 153 DeviceToDeviceResponderOperations::ValidateHelloMessage( |
| 143 CreateHelloMessage(), persistent_symmetric_key_, | 154 CreateHelloMessage(), persistent_symmetric_key_, |
| (...skipping 18 matching lines...) Expand all Loading... |
| 162 EXPECT_FALSE(validation_success); | 173 EXPECT_FALSE(validation_success); |
| 163 EXPECT_TRUE(hello_public_key.empty()); | 174 EXPECT_TRUE(hello_public_key.empty()); |
| 164 } | 175 } |
| 165 | 176 |
| 166 TEST_F(ProximityAuthDeviceToDeviceOperationsTest, | 177 TEST_F(ProximityAuthDeviceToDeviceOperationsTest, |
| 167 ValidateResponderAuthMessage_Success) { | 178 ValidateResponderAuthMessage_Success) { |
| 168 std::string hello_message = CreateHelloMessage(); | 179 std::string hello_message = CreateHelloMessage(); |
| 169 std::string remote_auth_message = CreateResponderAuthMessage(hello_message); | 180 std::string remote_auth_message = CreateResponderAuthMessage(hello_message); |
| 170 | 181 |
| 171 bool validation_success = false; | 182 bool validation_success = false; |
| 172 std::string session_symmetric_key; | 183 SessionKeys session_keys; |
| 173 DeviceToDeviceInitiatorOperations::ValidateResponderAuthMessage( | 184 DeviceToDeviceInitiatorOperations::ValidateResponderAuthMessage( |
| 174 remote_auth_message, kResponderPersistentPublicKey, | 185 remote_auth_message, kResponderPersistentPublicKey, |
| 175 persistent_symmetric_key_, local_session_private_key_, hello_message, | 186 persistent_symmetric_key_, local_session_private_key_, hello_message, |
| 176 &secure_message_delegate_, | 187 &secure_message_delegate_, |
| 177 base::Bind(&SaveValidationResultWithKey, &validation_success, | 188 base::Bind(&SaveValidationResultWithSessionKeys, &validation_success, |
| 178 &session_symmetric_key)); | 189 &session_keys)); |
| 179 | 190 |
| 180 EXPECT_TRUE(validation_success); | 191 EXPECT_TRUE(validation_success); |
| 181 EXPECT_EQ(session_symmetric_key_, session_symmetric_key); | 192 EXPECT_EQ(session_keys_.initiator_encode_key(), |
| 193 session_keys.initiator_encode_key()); |
| 194 EXPECT_EQ(session_keys_.responder_encode_key(), |
| 195 session_keys.responder_encode_key()); |
| 182 } | 196 } |
| 183 | 197 |
| 184 TEST_F(ProximityAuthDeviceToDeviceOperationsTest, | 198 TEST_F(ProximityAuthDeviceToDeviceOperationsTest, |
| 185 ValidateResponderAuthMessage_InvalidHelloMessage) { | 199 ValidateResponderAuthMessage_InvalidHelloMessage) { |
| 186 std::string hello_message = CreateHelloMessage(); | 200 std::string hello_message = CreateHelloMessage(); |
| 187 std::string remote_auth_message = CreateResponderAuthMessage(hello_message); | 201 std::string remote_auth_message = CreateResponderAuthMessage(hello_message); |
| 188 | 202 |
| 189 bool validation_success = true; | 203 bool validation_success = true; |
| 190 std::string session_symmetric_key = "non empty"; | 204 SessionKeys session_keys("non empty"); |
| 191 DeviceToDeviceInitiatorOperations::ValidateResponderAuthMessage( | 205 DeviceToDeviceInitiatorOperations::ValidateResponderAuthMessage( |
| 192 remote_auth_message, kResponderPersistentPublicKey, | 206 remote_auth_message, kResponderPersistentPublicKey, |
| 193 persistent_symmetric_key_, local_session_private_key_, | 207 persistent_symmetric_key_, local_session_private_key_, |
| 194 "invalid hello message", &secure_message_delegate_, | 208 "invalid hello message", &secure_message_delegate_, |
| 195 base::Bind(&SaveValidationResultWithKey, &validation_success, | 209 base::Bind(&SaveValidationResultWithSessionKeys, &validation_success, |
| 196 &session_symmetric_key)); | 210 &session_keys)); |
| 197 | 211 |
| 198 EXPECT_FALSE(validation_success); | 212 EXPECT_FALSE(validation_success); |
| 199 EXPECT_TRUE(session_symmetric_key.empty()); | 213 EXPECT_TRUE(session_keys.initiator_encode_key().empty()); |
| 214 EXPECT_TRUE(session_keys.responder_encode_key().empty()); |
| 200 } | 215 } |
| 201 | 216 |
| 202 TEST_F(ProximityAuthDeviceToDeviceOperationsTest, | 217 TEST_F(ProximityAuthDeviceToDeviceOperationsTest, |
| 203 ValidateResponderAuthMessage_InvalidPSK) { | 218 ValidateResponderAuthMessage_InvalidPSK) { |
| 204 std::string hello_message = CreateHelloMessage(); | 219 std::string hello_message = CreateHelloMessage(); |
| 205 std::string remote_auth_message = CreateResponderAuthMessage(hello_message); | 220 std::string remote_auth_message = CreateResponderAuthMessage(hello_message); |
| 206 | 221 |
| 207 bool validation_success = true; | 222 bool validation_success = true; |
| 208 std::string session_symmetric_key = "non empty"; | 223 SessionKeys session_keys("non empty"); |
| 209 DeviceToDeviceInitiatorOperations::ValidateResponderAuthMessage( | 224 DeviceToDeviceInitiatorOperations::ValidateResponderAuthMessage( |
| 210 remote_auth_message, kResponderPersistentPublicKey, | 225 remote_auth_message, kResponderPersistentPublicKey, |
| 211 "invalid persistent symmetric key", local_session_private_key_, | 226 "invalid persistent symmetric key", local_session_private_key_, |
| 212 hello_message, &secure_message_delegate_, | 227 hello_message, &secure_message_delegate_, |
| 213 base::Bind(&SaveValidationResultWithKey, &validation_success, | 228 base::Bind(&SaveValidationResultWithSessionKeys, &validation_success, |
| 214 &session_symmetric_key)); | 229 &session_keys)); |
| 215 | 230 |
| 216 EXPECT_FALSE(validation_success); | 231 EXPECT_FALSE(validation_success); |
| 217 EXPECT_TRUE(session_symmetric_key.empty()); | 232 EXPECT_TRUE(session_keys.initiator_encode_key().empty()); |
| 233 EXPECT_TRUE(session_keys.responder_encode_key().empty()); |
| 218 } | 234 } |
| 219 | 235 |
| 220 TEST_F(ProximityAuthDeviceToDeviceOperationsTest, | 236 TEST_F(ProximityAuthDeviceToDeviceOperationsTest, |
| 221 ValidateInitiatorAuthMessage_Success) { | 237 ValidateInitiatorAuthMessage_Success) { |
| 222 std::string hello_message = CreateHelloMessage(); | 238 std::string hello_message = CreateHelloMessage(); |
| 223 std::string remote_auth_message = CreateResponderAuthMessage(hello_message); | 239 std::string remote_auth_message = CreateResponderAuthMessage(hello_message); |
| 224 std::string local_auth_message = | 240 std::string local_auth_message = |
| 225 CreateInitiatorAuthMessage(remote_auth_message); | 241 CreateInitiatorAuthMessage(remote_auth_message); |
| 226 | 242 |
| 227 bool validation_success = false; | 243 bool validation_success = false; |
| 228 DeviceToDeviceResponderOperations::ValidateInitiatorAuthMessage( | 244 DeviceToDeviceResponderOperations::ValidateInitiatorAuthMessage( |
| 229 local_auth_message, session_symmetric_key_, persistent_symmetric_key_, | 245 local_auth_message, session_keys_, persistent_symmetric_key_, |
| 230 remote_auth_message, &secure_message_delegate_, | 246 remote_auth_message, &secure_message_delegate_, |
| 231 base::Bind(&SaveValidationResult, &validation_success)); | 247 base::Bind(&SaveValidationResult, &validation_success)); |
| 232 | 248 |
| 233 EXPECT_TRUE(validation_success); | 249 EXPECT_TRUE(validation_success); |
| 234 } | 250 } |
| 235 | 251 |
| 236 TEST_F(ProximityAuthDeviceToDeviceOperationsTest, | 252 TEST_F(ProximityAuthDeviceToDeviceOperationsTest, |
| 237 ValidateInitiatorAuthMessage_InvalidRemoteAuth) { | 253 ValidateInitiatorAuthMessage_InvalidRemoteAuth) { |
| 238 std::string hello_message = CreateHelloMessage(); | 254 std::string hello_message = CreateHelloMessage(); |
| 239 std::string remote_auth_message = CreateResponderAuthMessage(hello_message); | 255 std::string remote_auth_message = CreateResponderAuthMessage(hello_message); |
| 240 std::string local_auth_message = | 256 std::string local_auth_message = |
| 241 CreateInitiatorAuthMessage(remote_auth_message); | 257 CreateInitiatorAuthMessage(remote_auth_message); |
| 242 | 258 |
| 243 bool validation_success = true; | 259 bool validation_success = true; |
| 244 DeviceToDeviceResponderOperations::ValidateInitiatorAuthMessage( | 260 DeviceToDeviceResponderOperations::ValidateInitiatorAuthMessage( |
| 245 local_auth_message, session_symmetric_key_, persistent_symmetric_key_, | 261 local_auth_message, session_keys_, persistent_symmetric_key_, |
| 246 "invalid remote auth", &secure_message_delegate_, | 262 "invalid remote auth", &secure_message_delegate_, |
| 247 base::Bind(&SaveValidationResult, &validation_success)); | 263 base::Bind(&SaveValidationResult, &validation_success)); |
| 248 | 264 |
| 249 EXPECT_FALSE(validation_success); | 265 EXPECT_FALSE(validation_success); |
| 250 } | 266 } |
| 251 | 267 |
| 252 TEST_F(ProximityAuthDeviceToDeviceOperationsTest, | 268 TEST_F(ProximityAuthDeviceToDeviceOperationsTest, |
| 253 ValidateInitiatorAuthMessage_InvalidPSK) { | 269 ValidateInitiatorAuthMessage_InvalidPSK) { |
| 254 std::string hello_message = CreateHelloMessage(); | 270 std::string hello_message = CreateHelloMessage(); |
| 255 std::string remote_auth_message = CreateResponderAuthMessage(hello_message); | 271 std::string remote_auth_message = CreateResponderAuthMessage(hello_message); |
| 256 std::string local_auth_message = | 272 std::string local_auth_message = |
| 257 CreateInitiatorAuthMessage(remote_auth_message); | 273 CreateInitiatorAuthMessage(remote_auth_message); |
| 258 | 274 |
| 259 bool validation_success = true; | 275 bool validation_success = true; |
| 260 DeviceToDeviceResponderOperations::ValidateInitiatorAuthMessage( | 276 DeviceToDeviceResponderOperations::ValidateInitiatorAuthMessage( |
| 261 local_auth_message, session_symmetric_key_, | 277 local_auth_message, session_keys_, "invalid persistent symmetric key", |
| 262 "invalid persistent symmetric key", remote_auth_message, | 278 remote_auth_message, &secure_message_delegate_, |
| 263 &secure_message_delegate_, | |
| 264 base::Bind(&SaveValidationResult, &validation_success)); | 279 base::Bind(&SaveValidationResult, &validation_success)); |
| 265 | 280 |
| 266 EXPECT_FALSE(validation_success); | 281 EXPECT_FALSE(validation_success); |
| 267 } | 282 } |
| 268 | 283 |
| 269 } // namespace cryptauth | 284 } // namespace cryptauth |
| OLD | NEW |