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 | #pragma once |
27 | |
28 | #if ENABLE(WEB_AUTHN) && PLATFORM(MAC) |
29 | |
30 | #include "HidConnection.h" |
31 | #include <WebCore/FidoConstants.h> |
32 | #include <WebCore/FidoHidMessage.h> |
33 | #include <wtf/CompletionHandler.h> |
34 | #include <wtf/Forward.h> |
35 | #include <wtf/Noncopyable.h> |
36 | #include <wtf/UniqueRef.h> |
37 | #include <wtf/WeakPtr.h> |
38 | |
39 | namespace WebKit { |
40 | |
41 | // The following implements the CTAP HID protocol: |
42 | // https://fidoalliance.org/specs/fido-v2.0-ps-20170927/fido-client-to-authenticator-protocol-v2.0-ps-20170927.html#usb |
43 | // FSM: Idle => AllocateChannel => Ready |
44 | class CtapHidDriver : public CanMakeWeakPtr<CtapHidDriver> { |
45 | WTF_MAKE_FAST_ALLOCATED; |
46 | WTF_MAKE_NONCOPYABLE(CtapHidDriver); |
47 | public: |
48 | using ResponseCallback = Function<void(Vector<uint8_t>&&)>; |
49 | |
50 | enum class State : uint8_t { |
51 | Idle, |
52 | AllocateChannel, |
53 | Ready, |
54 | // FIXME(191528) |
55 | Busy |
56 | }; |
57 | |
58 | explicit CtapHidDriver(UniqueRef<HidConnection>&&); |
59 | |
60 | void setProtocol(fido::ProtocolVersion protocol) { m_protocol = protocol; } |
61 | void transact(Vector<uint8_t>&& data, ResponseCallback&&); |
62 | |
63 | private: |
64 | // Worker is the helper that maintains the transaction. |
65 | // https://fidoalliance.org/specs/fido-v2.0-ps-20170927/fido-client-to-authenticator-protocol-v2.0-ps-20170927.html#arbitration |
66 | // FSM: Idle => Write => Read. |
67 | class Worker : public CanMakeWeakPtr<Worker> { |
68 | WTF_MAKE_FAST_ALLOCATED; |
69 | WTF_MAKE_NONCOPYABLE(Worker); |
70 | public: |
71 | using MessageCallback = Function<void(Optional<fido::FidoHidMessage>&&)>; |
72 | |
73 | enum class State : uint8_t { |
74 | Idle, |
75 | Write, |
76 | Read |
77 | }; |
78 | |
79 | explicit Worker(UniqueRef<HidConnection>&&); |
80 | ~Worker(); |
81 | |
82 | void transact(fido::FidoHidMessage&&, MessageCallback&&); |
83 | |
84 | private: |
85 | void write(HidConnection::DataSent); |
86 | void read(const Vector<uint8_t>&); |
87 | void returnMessage(Optional<fido::FidoHidMessage>&&); |
88 | |
89 | UniqueRef<HidConnection> m_connection; |
90 | State m_state { State::Idle }; |
91 | Optional<fido::FidoHidMessage> m_requestMessage; |
92 | Optional<fido::FidoHidMessage> m_responseMessage; |
93 | MessageCallback m_callback; |
94 | }; |
95 | |
96 | void continueAfterChannelAllocated(Optional<fido::FidoHidMessage>&&); |
97 | void continueAfterResponseReceived(Optional<fido::FidoHidMessage>&&); |
98 | void returnResponse(Vector<uint8_t>&&); |
99 | |
100 | UniqueRef<Worker> m_worker; |
101 | State m_state { State::Idle }; |
102 | uint32_t m_channelId { fido::kHidBroadcastChannel }; |
103 | // One request at a time. |
104 | Vector<uint8_t> m_requestData; |
105 | ResponseCallback m_responseCallback; |
106 | Vector<uint8_t> m_nonce; |
107 | fido::ProtocolVersion m_protocol { fido::ProtocolVersion::kCtap }; |
108 | }; |
109 | |
110 | } // namespace WebKit |
111 | |
112 | #endif // ENABLE(WEB_AUTHN) && PLATFORM(MAC) |
113 | |