2 * Copyright (C) 2018 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "CtapHidDriver.h"
29 #if ENABLE(WEB_AUTHN) && PLATFORM(MAC)
31 #include <WebCore/FidoConstants.h>
32 #include <wtf/CryptographicallyRandomNumber.h>
33 #include <wtf/RunLoop.h>
34 #include <wtf/Vector.h>
35 #include <wtf/text/Base64.h>
40 CtapHidDriver::Worker::Worker(UniqueRef<HidConnection>&& connection)
41 : m_connection(WTFMove(connection))
43 m_connection->initialize();
46 CtapHidDriver::Worker::~Worker()
48 m_connection->terminate();
51 void CtapHidDriver::Worker::transact(fido::FidoHidMessage&& requestMessage, MessageCallback&& callback)
53 ASSERT(m_state == State::Idle);
54 m_state = State::Write;
55 m_requestMessage = WTFMove(requestMessage);
56 m_responseMessage.reset();
57 m_callback = WTFMove(callback);
59 // HidConnection could hold data from other applications, and thereofore invalidate it before each transaction.
60 m_connection->invalidateCache();
61 m_connection->send(m_requestMessage->popNextPacket(), [weakThis = makeWeakPtr(*this)](HidConnection::DataSent sent) mutable {
62 ASSERT(RunLoop::isMain());
65 weakThis->write(sent);
69 void CtapHidDriver::Worker::write(HidConnection::DataSent sent)
71 ASSERT(m_state == State::Write);
72 if (sent != HidConnection::DataSent::Yes) {
73 returnMessage(std::nullopt);
77 if (!m_requestMessage->numPackets()) {
78 m_state = State::Read;
79 m_connection->registerDataReceivedCallback([weakThis = makeWeakPtr(*this)](Vector<uint8_t>&& data) mutable {
80 ASSERT(RunLoop::isMain());
88 m_connection->send(m_requestMessage->popNextPacket(), [weakThis = makeWeakPtr(*this)](HidConnection::DataSent sent) mutable {
89 ASSERT(RunLoop::isMain());
92 weakThis->write(sent);
96 void CtapHidDriver::Worker::read(const Vector<uint8_t>& data)
98 ASSERT(m_state == State::Read);
99 if (!m_responseMessage) {
100 m_responseMessage = FidoHidMessage::createFromSerializedData(data);
101 // The first few reports could be for other applications, and therefore ignore those.
102 if (!m_responseMessage || m_responseMessage->channelId() != m_requestMessage->channelId()) {
103 LOG_ERROR("Couldn't parse a hid init packet: %s", m_responseMessage ? "wrong channel id." : "bad data.");
104 m_responseMessage.reset();
108 if (!m_responseMessage->addContinuationPacket(data)) {
109 LOG_ERROR("Couldn't parse a hid continuation packet.");
110 returnMessage(std::nullopt);
115 if (m_responseMessage->messageComplete()) {
116 // A KeepAlive cmd could be sent between a request and a response to indicate that
117 // the authenticator is waiting for user consent. Keep listening for the response.
118 if (m_responseMessage->cmd() == FidoHidDeviceCommand::kKeepAlive) {
119 m_responseMessage.reset();
122 returnMessage(WTFMove(m_responseMessage));
127 void CtapHidDriver::Worker::returnMessage(std::optional<fido::FidoHidMessage>&& message)
129 m_state = State::Idle;
130 m_connection->unregisterDataReceivedCallback();
131 m_callback(WTFMove(message));
134 CtapHidDriver::CtapHidDriver(UniqueRef<HidConnection>&& connection)
135 : m_worker(makeUniqueRef<Worker>(WTFMove(connection)))
136 , m_nonce(kHidInitNonceLength)
140 void CtapHidDriver::transact(Vector<uint8_t>&& data, ResponseCallback&& callback)
142 ASSERT(m_state == State::Idle);
143 m_state = State::AllocateChannel;
144 m_channelId = kHidBroadcastChannel;
145 m_requestData = WTFMove(data);
146 m_responseCallback = WTFMove(callback);
148 // Allocate a channel.
149 ASSERT(m_nonce.size() == kHidInitNonceLength);
150 cryptographicallyRandomValues(m_nonce.data(), m_nonce.size());
151 auto initCommand = FidoHidMessage::create(m_channelId, FidoHidDeviceCommand::kInit, m_nonce);
153 m_worker->transact(WTFMove(*initCommand), [weakThis = makeWeakPtr(*this)](std::optional<FidoHidMessage>&& response) mutable {
154 ASSERT(RunLoop::isMain());
157 weakThis->continueAfterChannelAllocated(WTFMove(response));
161 void CtapHidDriver::continueAfterChannelAllocated(std::optional<FidoHidMessage>&& message)
163 ASSERT(m_state == State::AllocateChannel);
168 ASSERT(message->channelId() == m_channelId);
170 auto payload = message->getMessagePayload();
171 ASSERT(payload.size() == kHidInitResponseSize);
172 // Restart the transaction in the next run loop when nonce mismatches.
173 if (memcmp(payload.data(), m_nonce.data(), m_nonce.size())) {
174 m_state = State::Idle;
175 RunLoop::main().dispatch([weakThis = makeWeakPtr(*this), data = WTFMove(m_requestData), callback = WTFMove(m_responseCallback)]() mutable {
178 weakThis->transact(WTFMove(data), WTFMove(callback));
183 m_state = State::Ready;
184 auto index = kHidInitNonceLength;
185 m_channelId = static_cast<uint32_t>(payload[index++]) << 24;
186 m_channelId |= static_cast<uint32_t>(payload[index++]) << 16;
187 m_channelId |= static_cast<uint32_t>(payload[index++]) << 8;
188 m_channelId |= static_cast<uint32_t>(payload[index]);
189 // FIXME(191534): Check the reset of the payload.
190 auto cmd = FidoHidMessage::create(m_channelId, FidoHidDeviceCommand::kCbor, m_requestData);
192 m_worker->transact(WTFMove(*cmd), [weakThis = makeWeakPtr(*this)](std::optional<FidoHidMessage>&& response) mutable {
193 ASSERT(RunLoop::isMain());
196 weakThis->continueAfterResponseReceived(WTFMove(response));
200 void CtapHidDriver::continueAfterResponseReceived(std::optional<fido::FidoHidMessage>&& message)
202 ASSERT(m_state == State::Ready);
203 ASSERT(!message || message->channelId() == m_channelId);
204 returnResponse(message ? message->getMessagePayload() : Vector<uint8_t>());
207 void CtapHidDriver::returnResponse(Vector<uint8_t>&& response)
209 // Reset state before calling the response callback to avoid being deleted.
210 m_state = State::Idle;
211 m_responseCallback(WTFMove(response));
214 } // namespace WebKit
216 #endif // ENABLE(WEB_AUTHN) && PLATFORM(MAC)