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