Jun Choi | 4fc8b781 | 2018-04-05 07:39:07 | [diff] [blame] | 1 | // Copyright 2018 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 | |
| 5 | #include <memory> |
| 6 | #include <utility> |
| 7 | |
Jun Choi | 0e56e5dd | 2018-06-08 21:27:34 | [diff] [blame] | 8 | #include "base/test/scoped_feature_list.h" |
Jun Choi | 4fc8b781 | 2018-04-05 07:39:07 | [diff] [blame] | 9 | #include "base/test/scoped_task_environment.h" |
Jun Choi | 0e56e5dd | 2018-06-08 21:27:34 | [diff] [blame] | 10 | #include "device/base/features.h" |
Jun Choi | 4fc8b781 | 2018-04-05 07:39:07 | [diff] [blame] | 11 | #include "device/fido/authenticator_get_assertion_response.h" |
| 12 | #include "device/fido/ctap_get_assertion_request.h" |
| 13 | #include "device/fido/fake_fido_discovery.h" |
| 14 | #include "device/fido/fido_constants.h" |
Jun Choi | 19b944e9 | 2018-04-23 20:20:20 | [diff] [blame] | 15 | #include "device/fido/fido_parsing_utils.h" |
Jun Choi | 22af8b37 | 2018-04-09 04:29:18 | [diff] [blame] | 16 | #include "device/fido/fido_test_data.h" |
Jun Choi | b60937e | 2018-04-12 17:02:38 | [diff] [blame] | 17 | #include "device/fido/fido_transport_protocol.h" |
Jun Choi | 4fc8b781 | 2018-04-05 07:39:07 | [diff] [blame] | 18 | #include "device/fido/get_assertion_request_handler.h" |
| 19 | #include "device/fido/mock_fido_device.h" |
| 20 | #include "device/fido/test_callback_receiver.h" |
Jun Choi | 4fc8b781 | 2018-04-05 07:39:07 | [diff] [blame] | 21 | #include "testing/gmock/include/gmock/gmock.h" |
| 22 | #include "testing/gtest/include/gtest/gtest.h" |
| 23 | |
| 24 | namespace device { |
| 25 | |
| 26 | namespace { |
| 27 | |
Jun Choi | 1beb4c2 | 2018-08-16 03:04:38 | [diff] [blame^] | 28 | using TestGetAssertionRequestCallback = test::StatusAndValuesCallbackReceiver< |
Jun Choi | f7ab0df | 2018-04-05 21:48:16 | [diff] [blame] | 29 | FidoReturnCode, |
Jun Choi | 1beb4c2 | 2018-08-16 03:04:38 | [diff] [blame^] | 30 | base::Optional<AuthenticatorGetAssertionResponse>, |
| 31 | FidoTransportProtocol>; |
Jun Choi | 4fc8b781 | 2018-04-05 07:39:07 | [diff] [blame] | 32 | |
| 33 | } // namespace |
| 34 | |
| 35 | class FidoGetAssertionHandlerTest : public ::testing::Test { |
| 36 | public: |
| 37 | void ForgeNextHidDiscovery() { |
| 38 | discovery_ = scoped_fake_discovery_factory_.ForgeNextHidDiscovery(); |
| 39 | } |
| 40 | |
Martin Kreichgauer | 8987fd0 | 2018-07-20 22:42:03 | [diff] [blame] | 41 | std::unique_ptr<GetAssertionRequestHandler> CreateGetAssertionHandlerU2f() { |
Martin Kreichgauer | 2b75372 | 2018-07-17 01:06:01 | [diff] [blame] | 42 | CtapGetAssertionRequest request(test_data::kRelyingPartyId, |
| 43 | test_data::kClientDataHash); |
| 44 | request.SetAllowList( |
Jan Wilken Doerrie | 726e197e | 2018-05-14 12:53:25 | [diff] [blame] | 45 | {{CredentialType::kPublicKey, |
Jun Choi | 0e56e5dd | 2018-06-08 21:27:34 | [diff] [blame] | 46 | fido_parsing_utils::Materialize(test_data::kU2fSignKeyHandle)}}); |
Martin Kreichgauer | 8987fd0 | 2018-07-20 22:42:03 | [diff] [blame] | 47 | return CreateGetAssertionHandlerWithRequest(std::move(request)); |
| 48 | } |
Jun Choi | 4fc8b781 | 2018-04-05 07:39:07 | [diff] [blame] | 49 | |
Martin Kreichgauer | 8987fd0 | 2018-07-20 22:42:03 | [diff] [blame] | 50 | std::unique_ptr<GetAssertionRequestHandler> CreateGetAssertionHandlerCtap() { |
| 51 | CtapGetAssertionRequest request(test_data::kRelyingPartyId, |
| 52 | test_data::kClientDataHash); |
| 53 | request.SetAllowList({{CredentialType::kPublicKey, |
| 54 | fido_parsing_utils::Materialize( |
| 55 | test_data::kTestGetAssertionCredentialId)}}); |
Martin Kreichgauer | 2b75372 | 2018-07-17 01:06:01 | [diff] [blame] | 56 | return CreateGetAssertionHandlerWithRequest(std::move(request)); |
| 57 | } |
| 58 | |
| 59 | std::unique_ptr<GetAssertionRequestHandler> |
| 60 | CreateGetAssertionHandlerWithRequest(CtapGetAssertionRequest request) { |
| 61 | ForgeNextHidDiscovery(); |
| 62 | |
Jun Choi | 4fc8b781 | 2018-04-05 07:39:07 | [diff] [blame] | 63 | return std::make_unique<GetAssertionRequestHandler>( |
| 64 | nullptr /* connector */, |
Jun Choi | b60937e | 2018-04-12 17:02:38 | [diff] [blame] | 65 | base::flat_set<FidoTransportProtocol>( |
| 66 | {FidoTransportProtocol::kUsbHumanInterfaceDevice}), |
Martin Kreichgauer | 2b75372 | 2018-07-17 01:06:01 | [diff] [blame] | 67 | std::move(request), get_assertion_cb_.callback()); |
Jun Choi | 4fc8b781 | 2018-04-05 07:39:07 | [diff] [blame] | 68 | } |
| 69 | |
Jun Choi | d19453d | 2018-06-21 23:16:39 | [diff] [blame] | 70 | void InitFeatureListAndDisableCtapFlag() { |
| 71 | scoped_feature_list_.InitAndDisableFeature(kNewCtap2Device); |
Jun Choi | 0e56e5dd | 2018-06-08 21:27:34 | [diff] [blame] | 72 | } |
Jun Choi | 4fc8b781 | 2018-04-05 07:39:07 | [diff] [blame] | 73 | |
Jun Choi | 0e56e5dd | 2018-06-08 21:27:34 | [diff] [blame] | 74 | test::FakeFidoDiscovery* discovery() const { return discovery_; } |
Jun Choi | 4fc8b781 | 2018-04-05 07:39:07 | [diff] [blame] | 75 | TestGetAssertionRequestCallback& get_assertion_callback() { |
| 76 | return get_assertion_cb_; |
| 77 | } |
| 78 | |
| 79 | protected: |
| 80 | base::test::ScopedTaskEnvironment scoped_task_environment_{ |
| 81 | base::test::ScopedTaskEnvironment::MainThreadType::MOCK_TIME}; |
Jun Choi | 0e56e5dd | 2018-06-08 21:27:34 | [diff] [blame] | 82 | base::test::ScopedFeatureList scoped_feature_list_; |
Jun Choi | 4fc8b781 | 2018-04-05 07:39:07 | [diff] [blame] | 83 | test::ScopedFakeFidoDiscoveryFactory scoped_fake_discovery_factory_; |
| 84 | test::FakeFidoDiscovery* discovery_; |
| 85 | TestGetAssertionRequestCallback get_assertion_cb_; |
| 86 | }; |
| 87 | |
Martin Kreichgauer | 8987fd0 | 2018-07-20 22:42:03 | [diff] [blame] | 88 | TEST_F(FidoGetAssertionHandlerTest, CtapRequestOnSingleDevice) { |
| 89 | auto request_handler = CreateGetAssertionHandlerCtap(); |
Jun Choi | 4fc8b781 | 2018-04-05 07:39:07 | [diff] [blame] | 90 | discovery()->WaitForCallToStartAndSimulateSuccess(); |
Martin Kreichgauer | 8987fd0 | 2018-07-20 22:42:03 | [diff] [blame] | 91 | auto device = MockFidoDevice::MakeCtapWithGetInfoExpectation(); |
Jun Choi | 4fc8b781 | 2018-04-05 07:39:07 | [diff] [blame] | 92 | device->ExpectCtap2CommandAndRespondWith( |
| 93 | CtapRequestCommand::kAuthenticatorGetAssertion, |
| 94 | test_data::kTestGetAssertionResponse); |
| 95 | |
| 96 | discovery()->AddDevice(std::move(device)); |
| 97 | get_assertion_callback().WaitForCallback(); |
| 98 | |
| 99 | EXPECT_EQ(FidoReturnCode::kSuccess, get_assertion_callback().status()); |
Jun Choi | 1beb4c2 | 2018-08-16 03:04:38 | [diff] [blame^] | 100 | EXPECT_TRUE(get_assertion_callback().value<0>()); |
Jun Choi | 4fc8b781 | 2018-04-05 07:39:07 | [diff] [blame] | 101 | EXPECT_TRUE(request_handler->is_complete()); |
| 102 | } |
| 103 | |
Jun Choi | 0e56e5dd | 2018-06-08 21:27:34 | [diff] [blame] | 104 | // Test a scenario where the connected authenticator is a U2F device. |
| 105 | TEST_F(FidoGetAssertionHandlerTest, TestU2fSign) { |
Martin Kreichgauer | 8987fd0 | 2018-07-20 22:42:03 | [diff] [blame] | 106 | auto request_handler = CreateGetAssertionHandlerU2f(); |
Jun Choi | 4fc8b781 | 2018-04-05 07:39:07 | [diff] [blame] | 107 | discovery()->WaitForCallToStartAndSimulateSuccess(); |
| 108 | |
Martin Kreichgauer | 8987fd0 | 2018-07-20 22:42:03 | [diff] [blame] | 109 | auto device = MockFidoDevice::MakeU2fWithGetInfoExpectation(); |
Jun Choi | 0e56e5dd | 2018-06-08 21:27:34 | [diff] [blame] | 110 | device->ExpectRequestAndRespondWith( |
| 111 | test_data::kU2fCheckOnlySignCommandApdu, |
| 112 | test_data::kApduEncodedNoErrorSignResponse); |
| 113 | device->ExpectRequestAndRespondWith( |
| 114 | test_data::kU2fSignCommandApdu, |
| 115 | test_data::kApduEncodedNoErrorSignResponse); |
Jun Choi | 4fc8b781 | 2018-04-05 07:39:07 | [diff] [blame] | 116 | |
| 117 | discovery()->AddDevice(std::move(device)); |
| 118 | scoped_task_environment_.FastForwardUntilNoTasksRemain(); |
Jun Choi | 0e56e5dd | 2018-06-08 21:27:34 | [diff] [blame] | 119 | EXPECT_EQ(FidoReturnCode::kSuccess, get_assertion_callback().status()); |
Jun Choi | 1beb4c2 | 2018-08-16 03:04:38 | [diff] [blame^] | 120 | EXPECT_TRUE(get_assertion_callback().value<0>()); |
Jun Choi | 0e56e5dd | 2018-06-08 21:27:34 | [diff] [blame] | 121 | EXPECT_TRUE(request_handler->is_complete()); |
| 122 | } |
| 123 | |
| 124 | // Test a scenario where the connected authenticator is a U2F device and |
| 125 | // "WebAuthenticationCtap2" flag is not enabled. |
| 126 | TEST_F(FidoGetAssertionHandlerTest, TestU2fSignWithoutCtapFlag) { |
Jun Choi | d19453d | 2018-06-21 23:16:39 | [diff] [blame] | 127 | InitFeatureListAndDisableCtapFlag(); |
Martin Kreichgauer | 8987fd0 | 2018-07-20 22:42:03 | [diff] [blame] | 128 | auto request_handler = CreateGetAssertionHandlerU2f(); |
Jun Choi | 0e56e5dd | 2018-06-08 21:27:34 | [diff] [blame] | 129 | discovery()->WaitForCallToStartAndSimulateSuccess(); |
| 130 | |
| 131 | auto device = std::make_unique<MockFidoDevice>(); |
| 132 | EXPECT_CALL(*device, GetId()).WillRepeatedly(testing::Return("device0")); |
| 133 | device->ExpectRequestAndRespondWith( |
| 134 | test_data::kU2fCheckOnlySignCommandApdu, |
| 135 | test_data::kApduEncodedNoErrorSignResponse); |
| 136 | device->ExpectRequestAndRespondWith( |
| 137 | test_data::kU2fSignCommandApdu, |
| 138 | test_data::kApduEncodedNoErrorSignResponse); |
| 139 | |
| 140 | discovery()->AddDevice(std::move(device)); |
| 141 | scoped_task_environment_.FastForwardUntilNoTasksRemain(); |
| 142 | EXPECT_EQ(FidoReturnCode::kSuccess, get_assertion_callback().status()); |
Jun Choi | 1beb4c2 | 2018-08-16 03:04:38 | [diff] [blame^] | 143 | EXPECT_TRUE(get_assertion_callback().value<0>()); |
Jun Choi | 0e56e5dd | 2018-06-08 21:27:34 | [diff] [blame] | 144 | EXPECT_TRUE(request_handler->is_complete()); |
Jun Choi | 4fc8b781 | 2018-04-05 07:39:07 | [diff] [blame] | 145 | } |
| 146 | |
Martin Kreichgauer | 2b75372 | 2018-07-17 01:06:01 | [diff] [blame] | 147 | TEST_F(FidoGetAssertionHandlerTest, TestIncompatibleUserVerificationSetting) { |
| 148 | auto request = CtapGetAssertionRequest(test_data::kRelyingPartyId, |
| 149 | test_data::kClientDataHash); |
| 150 | request.SetUserVerification(UserVerificationRequirement::kRequired); |
| 151 | auto request_handler = |
| 152 | CreateGetAssertionHandlerWithRequest(std::move(request)); |
| 153 | discovery()->WaitForCallToStartAndSimulateSuccess(); |
| 154 | |
Martin Kreichgauer | 8987fd0 | 2018-07-20 22:42:03 | [diff] [blame] | 155 | auto device = MockFidoDevice::MakeCtapWithGetInfoExpectation( |
Martin Kreichgauer | 2b75372 | 2018-07-17 01:06:01 | [diff] [blame] | 156 | test_data::kTestGetInfoResponseWithoutUvSupport); |
| 157 | |
| 158 | discovery()->AddDevice(std::move(device)); |
| 159 | |
| 160 | scoped_task_environment_.FastForwardUntilNoTasksRemain(); |
| 161 | EXPECT_FALSE(get_assertion_callback().was_called()); |
| 162 | } |
| 163 | |
| 164 | TEST_F(FidoGetAssertionHandlerTest, |
| 165 | TestU2fSignRequestWithUserVerificationRequired) { |
| 166 | auto request = CtapGetAssertionRequest(test_data::kRelyingPartyId, |
| 167 | test_data::kClientDataHash); |
| 168 | request.SetAllowList( |
| 169 | {{CredentialType::kPublicKey, |
| 170 | fido_parsing_utils::Materialize(test_data::kU2fSignKeyHandle)}}); |
| 171 | request.SetUserVerification(UserVerificationRequirement::kRequired); |
| 172 | auto request_handler = |
| 173 | CreateGetAssertionHandlerWithRequest(std::move(request)); |
| 174 | discovery()->WaitForCallToStartAndSimulateSuccess(); |
| 175 | |
Martin Kreichgauer | 8987fd0 | 2018-07-20 22:42:03 | [diff] [blame] | 176 | auto device = MockFidoDevice::MakeU2fWithGetInfoExpectation(); |
| 177 | discovery()->AddDevice(std::move(device)); |
| 178 | |
| 179 | scoped_task_environment_.FastForwardUntilNoTasksRemain(); |
| 180 | EXPECT_FALSE(get_assertion_callback().was_called()); |
| 181 | } |
| 182 | |
| 183 | TEST_F(FidoGetAssertionHandlerTest, IncorrectRpIdHash) { |
| 184 | auto request_handler = |
| 185 | CreateGetAssertionHandlerWithRequest(CtapGetAssertionRequest( |
| 186 | test_data::kRelyingPartyId, test_data::kClientDataHash)); |
| 187 | discovery()->WaitForCallToStartAndSimulateSuccess(); |
| 188 | auto device = MockFidoDevice::MakeCtapWithGetInfoExpectation(); |
Martin Kreichgauer | 2b75372 | 2018-07-17 01:06:01 | [diff] [blame] | 189 | device->ExpectCtap2CommandAndRespondWith( |
Martin Kreichgauer | 8987fd0 | 2018-07-20 22:42:03 | [diff] [blame] | 190 | CtapRequestCommand::kAuthenticatorGetAssertion, |
| 191 | test_data::kTestGetAssertionResponseWithIncorrectRpIdHash); |
| 192 | |
| 193 | discovery()->AddDevice(std::move(device)); |
| 194 | |
| 195 | scoped_task_environment_.FastForwardUntilNoTasksRemain(); |
| 196 | EXPECT_FALSE(get_assertion_callback().was_called()); |
| 197 | } |
| 198 | |
| 199 | // Tests a scenario where the authenticator responds with credential ID that |
| 200 | // is not included in the allowed list. |
| 201 | TEST_F(FidoGetAssertionHandlerTest, InvalidCredential) { |
| 202 | CtapGetAssertionRequest request(test_data::kRelyingPartyId, |
| 203 | test_data::kClientDataHash); |
| 204 | request.SetAllowList( |
| 205 | {{CredentialType::kPublicKey, |
| 206 | fido_parsing_utils::Materialize(test_data::kKeyHandleAlpha)}}); |
| 207 | auto request_handler = |
| 208 | CreateGetAssertionHandlerWithRequest(std::move(request)); |
| 209 | discovery()->WaitForCallToStartAndSimulateSuccess(); |
| 210 | // Resident Keys must be disabled, otherwise allow list check is skipped. |
| 211 | auto device = MockFidoDevice::MakeCtapWithGetInfoExpectation( |
| 212 | test_data::kTestGetInfoResponseWithoutResidentKeySupport); |
| 213 | device->ExpectCtap2CommandAndRespondWith( |
| 214 | CtapRequestCommand::kAuthenticatorGetAssertion, |
| 215 | test_data::kTestGetAssertionResponse); |
| 216 | |
| 217 | discovery()->AddDevice(std::move(device)); |
| 218 | |
| 219 | scoped_task_environment_.FastForwardUntilNoTasksRemain(); |
| 220 | EXPECT_FALSE(get_assertion_callback().was_called()); |
| 221 | } |
| 222 | |
| 223 | // Tests a scenario where authenticator responds without user entity in its |
| 224 | // response but client is expecting a resident key credential. |
| 225 | TEST_F(FidoGetAssertionHandlerTest, IncorrectUserEntity) { |
| 226 | // Use a GetAssertion request with an empty allow list. |
| 227 | auto request_handler = |
| 228 | CreateGetAssertionHandlerWithRequest(CtapGetAssertionRequest( |
| 229 | test_data::kRelyingPartyId, test_data::kClientDataHash)); |
| 230 | discovery()->WaitForCallToStartAndSimulateSuccess(); |
| 231 | auto device = MockFidoDevice::MakeCtapWithGetInfoExpectation(); |
| 232 | device->ExpectCtap2CommandAndRespondWith( |
| 233 | CtapRequestCommand::kAuthenticatorGetAssertion, |
| 234 | test_data::kTestGetAssertionResponse); |
Martin Kreichgauer | 2b75372 | 2018-07-17 01:06:01 | [diff] [blame] | 235 | |
| 236 | discovery()->AddDevice(std::move(device)); |
| 237 | |
| 238 | scoped_task_environment_.FastForwardUntilNoTasksRemain(); |
| 239 | EXPECT_FALSE(get_assertion_callback().was_called()); |
| 240 | } |
| 241 | |
Jun Choi | e4aee4ff3 | 2018-08-13 19:35:07 | [diff] [blame] | 242 | // Tests a scenario where authenticator of incorrect transport type was used to |
| 243 | // conduct CTAP GetAssertion call. |
| 244 | TEST_F(FidoGetAssertionHandlerTest, IncorrectTransportType) { |
| 245 | // GetAssertion request that expects GetAssertion call for credential |
| 246 | // |CredentialType::kPublicKey| to be signed with Cable authenticator. |
| 247 | CtapGetAssertionRequest request(test_data::kRelyingPartyId, |
| 248 | test_data::kClientDataHash); |
| 249 | request.SetAllowList( |
| 250 | {{CredentialType::kPublicKey, |
| 251 | fido_parsing_utils::Materialize( |
| 252 | test_data::kTestGetAssertionCredentialId), |
| 253 | {FidoTransportProtocol::kCloudAssistedBluetoothLowEnergy}}}); |
| 254 | auto request_handler = |
| 255 | CreateGetAssertionHandlerWithRequest(std::move(request)); |
| 256 | discovery()->WaitForCallToStartAndSimulateSuccess(); |
| 257 | auto device = MockFidoDevice::MakeCtapWithGetInfoExpectation(); |
| 258 | // Since transport type of |device| is different from what the relying party |
| 259 | // defined in |request| above, this request should fail. |
| 260 | device->SetDeviceTransport(FidoTransportProtocol::kUsbHumanInterfaceDevice); |
| 261 | device->ExpectCtap2CommandAndRespondWith( |
| 262 | CtapRequestCommand::kAuthenticatorGetAssertion, |
| 263 | test_data::kTestGetAssertionResponse); |
| 264 | |
| 265 | discovery()->AddDevice(std::move(device)); |
| 266 | |
| 267 | scoped_task_environment_.FastForwardUntilNoTasksRemain(); |
| 268 | EXPECT_FALSE(get_assertion_callback().was_called()); |
| 269 | } |
| 270 | |
Jun Choi | 4fc8b781 | 2018-04-05 07:39:07 | [diff] [blame] | 271 | } // namespace device |