1// Copyright 2017 The Chromium Authors. All rights reserved.
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 are
6// met:
7//
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
13// distribution.
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.
17//
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.
29
30#pragma once
31
32#if ENABLE(WEB_AUTHN)
33
34#include "FidoConstants.h"
35#include <wtf/Noncopyable.h>
36#include <wtf/Vector.h>
37
38namespace fido {
39
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.
46class WEBCORE_EXPORT FidoHidPacket {
47 WTF_MAKE_FAST_ALLOCATED;
48 WTF_MAKE_NONCOPYABLE(FidoHidPacket);
49public:
50 FidoHidPacket(Vector<uint8_t>&& data, uint32_t channelId);
51 virtual ~FidoHidPacket() = default;
52
53 virtual Vector<uint8_t> getSerializedData() const = 0;
54 const Vector<uint8_t>& getPacketPayload() const { return m_data; }
55 uint32_t channelId() const { return m_channelId; }
56
57protected:
58 FidoHidPacket();
59
60 Vector<uint8_t> m_data;
61 uint32_t m_channelId = kHidBroadcastChannel;
62};
63
64// FidoHidInitPacket, based on the CTAP specification consists of a header with
65// data that is serialized into a IOBuffer. A channel identifier is allocated by
66// the CTAP device to ensure its system-wide uniqueness. Command identifiers
67// determine the type of message the packet corresponds to. Payload length
68// is the length of the entire message payload, and the data is only the portion
69// of the payload that will fit into the HidInitPacket.
70class WEBCORE_EXPORT FidoHidInitPacket : public FidoHidPacket {
71public:
72 // Creates a packet from the serialized data of an initialization packet. As
73 // this is the first packet, the payload length of the entire message will be
74 // included within the serialized data. Remaining size will be returned to
75 // inform the callee how many additional packets to expect.
76 static std::unique_ptr<FidoHidInitPacket> createFromSerializedData(const Vector<uint8_t>&, size_t* remainingSize);
77
78 FidoHidInitPacket(uint32_t channelId, FidoHidDeviceCommand, Vector<uint8_t>&& data, uint16_t payloadLength);
79
80 Vector<uint8_t> getSerializedData() const final;
81 FidoHidDeviceCommand command() const { return m_command; }
82 uint16_t payloadLength() const { return m_payloadLength; }
83
84private:
85 FidoHidDeviceCommand m_command;
86 uint16_t m_payloadLength;
87};
88
89// FidoHidContinuationPacket, based on the CTAP Specification consists of a
90// header with data that is serialized into an IOBuffer. The channel identifier
91// will be identical to the identifier in all other packets of the message. The
92// packet sequence will be the sequence number of this particular packet, from
93// 0x00 to 0x7f.
94class WEBCORE_EXPORT FidoHidContinuationPacket : public FidoHidPacket {
95public:
96 // Creates a packet from the serialized data of a continuation packet. As an
97 // HidInitPacket would have arrived earlier with the total payload size,
98 // the remaining size should be passed to inform the packet of how much data
99 // to expect.
100 static std::unique_ptr<FidoHidContinuationPacket> createFromSerializedData(const Vector<uint8_t>&, size_t* remainingSize);
101
102 FidoHidContinuationPacket(uint32_t channelId, uint8_t sequence, Vector<uint8_t>&& data);
103
104 Vector<uint8_t> getSerializedData() const final;
105 uint8_t sequence() const { return m_sequence; }
106
107private:
108 uint8_t m_sequence;
109};
110
111} // namespace fido
112
113#endif // ENABLE(WEB_AUTHN)
114