1/*
2 * Copyright (C) 2018 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "MockHidConnection.h"
28
29#if ENABLE(WEB_AUTHN) && PLATFORM(MAC)
30
31#include <WebCore/AuthenticatorGetInfoResponse.h>
32#include <WebCore/CBORReader.h>
33#include <WebCore/FidoConstants.h>
34#include <WebCore/WebAuthenticationConstants.h>
35#include <wtf/BlockPtr.h>
36#include <wtf/CryptographicallyRandomNumber.h>
37#include <wtf/RunLoop.h>
38#include <wtf/text/Base64.h>
39
40namespace WebKit {
41using Mock = MockWebAuthenticationConfiguration::Hid;
42using namespace WebCore;
43using namespace cbor;
44using namespace fido;
45
46namespace MockHidConnectionInternal {
47// https://fidoalliance.org/specs/fido-v2.0-ps-20170927/fido-client-to-authenticator-protocol-v2.0-ps-20170927.html#mandatory-commands
48const size_t CtapChannelIdSize = 4;
49const uint8_t CtapKeepAliveStatusProcessing = 1;
50// https://fidoalliance.org/specs/fido-v2.0-ps-20170927/fido-client-to-authenticator-protocol-v2.0-ps-20170927.html#commands
51const int64_t CtapMakeCredentialRequestOptionsKey = 7;
52const int64_t CtapGetAssertionRequestOptionsKey = 5;
53}
54
55MockHidConnection::MockHidConnection(IOHIDDeviceRef device, const MockWebAuthenticationConfiguration& configuration)
56 : HidConnection(device)
57 , m_configuration(configuration)
58{
59}
60
61void MockHidConnection::initialize()
62{
63 m_initialized = true;
64}
65
66void MockHidConnection::terminate()
67{
68 m_terminated = true;
69}
70
71void MockHidConnection::send(Vector<uint8_t>&& data, DataSentCallback&& callback)
72{
73 ASSERT(m_initialized);
74 auto task = makeBlockPtr([weakThis = makeWeakPtr(*this), data = WTFMove(data), callback = WTFMove(callback)]() mutable {
75 ASSERT(!RunLoop::isMain());
76 RunLoop::main().dispatch([weakThis, data = WTFMove(data), callback = WTFMove(callback)]() mutable {
77 if (!weakThis) {
78 callback(DataSent::No);
79 return;
80 }
81
82 weakThis->assembleRequest(WTFMove(data));
83
84 auto sent = DataSent::Yes;
85 if (weakThis->stagesMatch() && weakThis->m_configuration.hid->error == Mock::Error::DataNotSent)
86 sent = DataSent::No;
87 callback(sent);
88 });
89 });
90 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), task.get());
91}
92
93void MockHidConnection::registerDataReceivedCallbackInternal()
94{
95 if (stagesMatch() && m_configuration.hid->error == Mock::Error::EmptyReport) {
96 receiveReport({ });
97 shouldContinueFeedReports();
98 return;
99 }
100 if (!m_configuration.hid->fastDataArrival)
101 feedReports();
102}
103
104void MockHidConnection::assembleRequest(Vector<uint8_t>&& data)
105{
106 if (!m_requestMessage) {
107 m_requestMessage = FidoHidMessage::createFromSerializedData(data);
108 ASSERT(m_requestMessage);
109 } else {
110 auto status = m_requestMessage->addContinuationPacket(data);
111 ASSERT_UNUSED(status, status);
112 }
113
114 if (m_requestMessage->messageComplete())
115 parseRequest();
116}
117
118void MockHidConnection::parseRequest()
119{
120 using namespace MockHidConnectionInternal;
121
122 ASSERT(m_requestMessage);
123 // Set stages.
124 if (m_requestMessage->cmd() == FidoHidDeviceCommand::kInit) {
125 auto previousSubStage = m_subStage;
126 m_subStage = Mock::SubStage::Init;
127 if (previousSubStage == Mock::SubStage::Msg)
128 m_stage = Mock::Stage::Request;
129 }
130 if (m_requestMessage->cmd() == FidoHidDeviceCommand::kCbor || m_requestMessage->cmd() == FidoHidDeviceCommand::kMsg)
131 m_subStage = Mock::SubStage::Msg;
132
133 if (m_stage == Mock::Stage::Request && m_subStage == Mock::SubStage::Msg) {
134 // Make sure we issue different msg cmd for CTAP and U2F.
135 if (m_configuration.hid->canDowngrade && !m_configuration.hid->isU2f)
136 m_configuration.hid->isU2f = m_requestMessage->cmd() == FidoHidDeviceCommand::kMsg;
137 ASSERT(m_configuration.hid->isU2f ^ (m_requestMessage->cmd() != FidoHidDeviceCommand::kMsg));
138
139 // Set options.
140 if (m_requestMessage->cmd() == FidoHidDeviceCommand::kCbor) {
141 m_requireResidentKey = false;
142 m_requireUserVerification = false;
143
144 auto payload = m_requestMessage->getMessagePayload();
145 ASSERT(payload.size());
146 auto cmd = static_cast<CtapRequestCommand>(payload[0]);
147 payload.remove(0);
148 auto requestMap = CBORReader::read(payload);
149 ASSERT(requestMap);
150
151 if (cmd == CtapRequestCommand::kAuthenticatorMakeCredential) {
152 auto it = requestMap->getMap().find(CBORValue(CtapMakeCredentialRequestOptionsKey)); // Find options.
153 if (it != requestMap->getMap().end()) {
154 auto& optionMap = it->second.getMap();
155
156 auto itr = optionMap.find(CBORValue(kResidentKeyMapKey));
157 if (itr != optionMap.end())
158 m_requireResidentKey = itr->second.getBool();
159
160 itr = optionMap.find(CBORValue(kUserVerificationMapKey));
161 if (itr != optionMap.end())
162 m_requireUserVerification = itr->second.getBool();
163 }
164 }
165
166 if (cmd == CtapRequestCommand::kAuthenticatorGetAssertion) {
167 auto it = requestMap->getMap().find(CBORValue(CtapGetAssertionRequestOptionsKey)); // Find options.
168 if (it != requestMap->getMap().end()) {
169 auto& optionMap = it->second.getMap();
170 auto itr = optionMap.find(CBORValue(kUserVerificationMapKey));
171 if (itr != optionMap.end())
172 m_requireUserVerification = itr->second.getBool();
173 }
174 }
175 }
176 }
177
178 // Store nonce.
179 if (m_subStage == Mock::SubStage::Init) {
180 m_nonce = m_requestMessage->getMessagePayload();
181 ASSERT(m_nonce.size() == kHidInitNonceLength);
182 }
183
184 m_currentChannel = m_requestMessage->channelId();
185 m_requestMessage = WTF::nullopt;
186 if (m_configuration.hid->fastDataArrival)
187 feedReports();
188}
189
190void MockHidConnection::feedReports()
191{
192 using namespace MockHidConnectionInternal;
193
194 if (m_subStage == Mock::SubStage::Init) {
195 Vector<uint8_t> payload;
196 payload.reserveInitialCapacity(kHidInitResponseSize);
197 payload.appendVector(m_nonce);
198 size_t writePosition = payload.size();
199 if (stagesMatch() && m_configuration.hid->error == Mock::Error::WrongNonce)
200 payload[0]--;
201 payload.grow(kHidInitResponseSize);
202 cryptographicallyRandomValues(payload.data() + writePosition, CtapChannelIdSize);
203 auto channel = kHidBroadcastChannel;
204 if (stagesMatch() && m_configuration.hid->error == Mock::Error::WrongChannelId)
205 channel--;
206 FidoHidInitPacket initPacket(channel, FidoHidDeviceCommand::kInit, WTFMove(payload), payload.size());
207 receiveReport(initPacket.getSerializedData());
208 shouldContinueFeedReports();
209 return;
210 }
211
212 Optional<FidoHidMessage> message;
213 if (m_stage == Mock::Stage::Info && m_subStage == Mock::SubStage::Msg) {
214 Vector<uint8_t> infoData;
215 if (m_configuration.hid->canDowngrade)
216 infoData = encodeAsCBOR(AuthenticatorGetInfoResponse({ ProtocolVersion::kCtap, ProtocolVersion::kU2f }, Vector<uint8_t>(aaguidLength, 0u)));
217 else
218 infoData = encodeAsCBOR(AuthenticatorGetInfoResponse({ ProtocolVersion::kCtap }, Vector<uint8_t>(aaguidLength, 0u)));
219 infoData.insert(0, static_cast<uint8_t>(CtapDeviceResponseCode::kSuccess)); // Prepend status code.
220 if (stagesMatch() && m_configuration.hid->error == Mock::Error::WrongChannelId)
221 message = FidoHidMessage::create(m_currentChannel - 1, FidoHidDeviceCommand::kCbor, infoData);
222 else {
223 if (!m_configuration.hid->isU2f)
224 message = FidoHidMessage::create(m_currentChannel, FidoHidDeviceCommand::kCbor, infoData);
225 else
226 message = FidoHidMessage::create(m_currentChannel, FidoHidDeviceCommand::kError, { static_cast<uint8_t>(CtapDeviceResponseCode::kCtap1ErrInvalidCommand) });
227 }
228 }
229
230 if (m_stage == Mock::Stage::Request && m_subStage == Mock::SubStage::Msg) {
231 if (m_configuration.hid->keepAlive) {
232 m_configuration.hid->keepAlive = false;
233 FidoHidInitPacket initPacket(m_currentChannel, FidoHidDeviceCommand::kKeepAlive, { CtapKeepAliveStatusProcessing }, 1);
234 receiveReport(initPacket.getSerializedData());
235 continueFeedReports();
236 return;
237 }
238 if (stagesMatch() && m_configuration.hid->error == Mock::Error::UnsupportedOptions && (m_requireResidentKey || m_requireUserVerification))
239 message = FidoHidMessage::create(m_currentChannel, FidoHidDeviceCommand::kCbor, { static_cast<uint8_t>(CtapDeviceResponseCode::kCtap2ErrUnsupportedOption) });
240 else {
241 Vector<uint8_t> payload;
242 ASSERT(!m_configuration.hid->payloadBase64.isEmpty());
243 auto status = base64Decode(m_configuration.hid->payloadBase64[0], payload);
244 m_configuration.hid->payloadBase64.remove(0);
245 ASSERT_UNUSED(status, status);
246 if (!m_configuration.hid->isU2f)
247 message = FidoHidMessage::create(m_currentChannel, FidoHidDeviceCommand::kCbor, payload);
248 else
249 message = FidoHidMessage::create(m_currentChannel, FidoHidDeviceCommand::kMsg, payload);
250 }
251 }
252
253 ASSERT(message);
254 bool isFirst = true;
255 while (message->numPackets()) {
256 auto report = message->popNextPacket();
257 if (!isFirst && stagesMatch() && m_configuration.hid->error == Mock::Error::WrongChannelId)
258 report = FidoHidContinuationPacket(m_currentChannel - 1, 0, { }).getSerializedData();
259 // Packets are feed asynchronously to mimic actual data transmission.
260 RunLoop::main().dispatch([report = WTFMove(report), weakThis = makeWeakPtr(*this)]() mutable {
261 if (!weakThis)
262 return;
263 weakThis->receiveReport(WTFMove(report));
264 });
265 isFirst = false;
266 }
267}
268
269bool MockHidConnection::stagesMatch() const
270{
271 return m_configuration.hid->stage == m_stage && m_configuration.hid->subStage == m_subStage;
272}
273
274void MockHidConnection::shouldContinueFeedReports()
275{
276 if (!m_configuration.hid->continueAfterErrorData)
277 return;
278 m_configuration.hid->continueAfterErrorData = false;
279 m_configuration.hid->error = Mock::Error::Success;
280 continueFeedReports();
281}
282
283void MockHidConnection::continueFeedReports()
284{
285 // Send actual response for the next run.
286 RunLoop::main().dispatch([weakThis = makeWeakPtr(*this)]() mutable {
287 if (!weakThis)
288 return;
289 weakThis->feedReports();
290 });
291}
292
293} // namespace WebKit
294
295#endif // ENABLE(WEB_AUTHN) && PLATFORM(MAC)
296