| 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 <wtf/Noncopyable.h> | 
|---|
| 35 | #include <wtf/Vector.h> | 
|---|
| 36 |  | 
|---|
| 37 | namespace apdu { | 
|---|
| 38 |  | 
|---|
| 39 | // APDU commands are defined as part of ISO 7816-4. Commands can be serialized | 
|---|
| 40 | // into either short length encodings, where the maximum data length is 256 | 
|---|
| 41 | // bytes, or an extended length encoding, where the maximum data length is 65536 | 
|---|
| 42 | // bytes. This class implements only the extended length encoding. Serialized | 
|---|
| 43 | // commands consist of a CLA byte, denoting the class of instruction, an INS | 
|---|
| 44 | // byte, denoting the instruction code, P1 and P2, each one byte denoting | 
|---|
| 45 | // instruction parameters, a length field (Lc), a data field of length Lc, and | 
|---|
| 46 | // a maximum expected response length (Le). | 
|---|
| 47 | class WEBCORE_EXPORT ApduCommand { | 
|---|
| 48 | WTF_MAKE_NONCOPYABLE(ApduCommand); | 
|---|
| 49 | public: | 
|---|
| 50 | // Constructs an APDU command from the serialized message data. | 
|---|
| 51 | static Optional<ApduCommand> createFromMessage(const Vector<uint8_t>&); | 
|---|
| 52 |  | 
|---|
| 53 | ApduCommand() = default; | 
|---|
| 54 | ApduCommand( | 
|---|
| 55 | uint8_t cla, | 
|---|
| 56 | uint8_t ins, | 
|---|
| 57 | uint8_t p1, | 
|---|
| 58 | uint8_t p2, | 
|---|
| 59 | size_t responseLength, | 
|---|
| 60 | Vector<uint8_t>&& data); | 
|---|
| 61 | ApduCommand(ApduCommand&&) = default; | 
|---|
| 62 | ApduCommand& operator=(ApduCommand&&) = default; | 
|---|
| 63 |  | 
|---|
| 64 | // Returns serialized message data. | 
|---|
| 65 | Vector<uint8_t> getEncodedCommand() const; | 
|---|
| 66 |  | 
|---|
| 67 | void setCla(uint8_t cla) { m_cla = cla; } | 
|---|
| 68 | void setIns(uint8_t ins) { m_ins = ins; } | 
|---|
| 69 | void setP1(uint8_t p1) { m_p1 = p1; } | 
|---|
| 70 | void setP2(uint8_t p2) { m_p2 = p2; } | 
|---|
| 71 | void setData(Vector<uint8_t>&& data) { m_data = WTFMove(data); } | 
|---|
| 72 | void setResponseLength(size_t responseLength) { m_responseLength = responseLength; } | 
|---|
| 73 |  | 
|---|
| 74 | uint8_t cla() const { return m_cla; } | 
|---|
| 75 | uint8_t ins() const { return m_ins; } | 
|---|
| 76 | uint8_t p1() const { return m_p1; } | 
|---|
| 77 | uint8_t p2() const { return m_p2; } | 
|---|
| 78 | size_t responseLength() const { return m_responseLength; } | 
|---|
| 79 | const Vector<uint8_t>& data() const { return m_data; } | 
|---|
| 80 |  | 
|---|
| 81 | static constexpr size_t kApduMaxResponseLength = 65536; | 
|---|
| 82 |  | 
|---|
| 83 | static constexpr size_t kApduMinHeader = 4; | 
|---|
| 84 | static constexpr size_t kApduMaxHeader = 7; | 
|---|
| 85 | static constexpr size_t kApduCommandDataOffset = 7; | 
|---|
| 86 | static constexpr size_t kApduCommandLengthOffset = 5; | 
|---|
| 87 |  | 
|---|
| 88 | // As defined in ISO7816-4, extended length APDU request data is limited to | 
|---|
| 89 | // 16 bits in length with a maximum value of 65535. Response data length is | 
|---|
| 90 | // also limited to 16 bits in length with a value of 0x0000 corresponding to | 
|---|
| 91 | // a length of 65536. | 
|---|
| 92 | static constexpr size_t kApduMaxDataLength = 65535; | 
|---|
| 93 | static constexpr size_t kApduMaxLength = kApduMaxDataLength + kApduMaxHeader + 2; | 
|---|
| 94 |  | 
|---|
| 95 | private: | 
|---|
| 96 | uint8_t m_cla { 0 }; | 
|---|
| 97 | uint8_t m_ins { 0 }; | 
|---|
| 98 | uint8_t m_p1 { 0 }; | 
|---|
| 99 | uint8_t m_p2 { 0 }; | 
|---|
| 100 | size_t m_responseLength { 0 }; | 
|---|
| 101 | Vector<uint8_t> m_data; | 
|---|
| 102 | }; | 
|---|
| 103 |  | 
|---|
| 104 | } // namespace apdu | 
|---|
| 105 |  | 
|---|
| 106 | #endif // ENABLE(WEB_AUTHN) | 
|---|
| 107 |  | 
|---|