1 // Copyright 2017 The Chromium Authors. All rights reserved.
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 are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include "FidoConstants.h"
35 #include <wtf/Noncopyable.h>
36 #include <wtf/Vector.h>
40 // HID Packets are defined by the specification at
41 // https://fidoalliance.org/specs/fido-v2.0-ps-20170927/fido-client-to-authenticator-protocol-v2.0-ps-20170927.html#message-and-packet-structure
42 // Packets are one of two types, initialization packets and continuation
43 // packets. HID Packets have header information and a payload. If a
44 // FidoHidInitPacket cannot store the entire payload, further payload
45 // information is stored in HidContinuationPackets.
46 class WEBCORE_EXPORT FidoHidPacket {
47 WTF_MAKE_NONCOPYABLE(FidoHidPacket);
49 FidoHidPacket(Vector<uint8_t>&& data, uint32_t channelId);
50 virtual ~FidoHidPacket() = default;
52 virtual Vector<uint8_t> getSerializedData() const = 0;
53 const Vector<uint8_t>& getPacketPayload() const { return m_data; }
54 uint32_t channelId() const { return m_channelId; }
59 Vector<uint8_t> m_data;
60 uint32_t m_channelId = kHidBroadcastChannel;
63 // FidoHidInitPacket, based on the CTAP specification consists of a header with
64 // data that is serialized into a IOBuffer. A channel identifier is allocated by
65 // the CTAP device to ensure its system-wide uniqueness. Command identifiers
66 // determine the type of message the packet corresponds to. Payload length
67 // is the length of the entire message payload, and the data is only the portion
68 // of the payload that will fit into the HidInitPacket.
69 class WEBCORE_EXPORT FidoHidInitPacket : public FidoHidPacket {
71 // Creates a packet from the serialized data of an initialization packet. As
72 // this is the first packet, the payload length of the entire message will be
73 // included within the serialized data. Remaining size will be returned to
74 // inform the callee how many additional packets to expect.
75 static std::unique_ptr<FidoHidInitPacket> createFromSerializedData(const Vector<uint8_t>&, size_t* remainingSize);
77 FidoHidInitPacket(uint32_t channelId, FidoHidDeviceCommand, Vector<uint8_t>&& data, uint16_t payloadLength);
79 Vector<uint8_t> getSerializedData() const final;
80 FidoHidDeviceCommand command() const { return m_command; }
81 uint16_t payloadLength() const { return m_payloadLength; }
84 FidoHidDeviceCommand m_command;
85 uint16_t m_payloadLength;
88 // FidoHidContinuationPacket, based on the CTAP Specification consists of a
89 // header with data that is serialized into an IOBuffer. The channel identifier
90 // will be identical to the identifier in all other packets of the message. The
91 // packet sequence will be the sequence number of this particular packet, from
93 class WEBCORE_EXPORT FidoHidContinuationPacket : public FidoHidPacket {
95 // Creates a packet from the serialized data of a continuation packet. As an
96 // HidInitPacket would have arrived earlier with the total payload size,
97 // the remaining size should be passed to inform the packet of how much data
99 static std::unique_ptr<FidoHidContinuationPacket> createFromSerializedData(const Vector<uint8_t>&, size_t* remainingSize);
101 FidoHidContinuationPacket(uint32_t channelId, uint8_t sequence, Vector<uint8_t>&& data);
103 Vector<uint8_t> getSerializedData() const final;
104 uint8_t sequence() const { return m_sequence; }
112 #endif // ENABLE(WEB_AUTHN)