piperc | 7929e07 | 2017-01-20 01:04:49 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Jun Choi | 3545f50 | 2018-03-14 01:30:00 | [diff] [blame] | 5 | #ifndef COMPONENTS_APDU_APDU_COMMAND_H_ |
| 6 | #define COMPONENTS_APDU_APDU_COMMAND_H_ |
piperc | 7929e07 | 2017-01-20 01:04:49 | [diff] [blame] | 7 | |
piperc | 6de0082 | 2017-03-22 16:37:32 | [diff] [blame] | 8 | #include <cinttypes> |
Jun Choi | b49986d | 2018-03-13 20:01:44 | [diff] [blame] | 9 | #include <utility> |
piperc | 7929e07 | 2017-01-20 01:04:49 | [diff] [blame] | 10 | #include <vector> |
| 11 | |
Balazs Engedy | d531bf4 | 2018-03-12 22:51:42 | [diff] [blame] | 12 | #include "base/component_export.h" |
Jun Choi | 3545f50 | 2018-03-14 01:30:00 | [diff] [blame] | 13 | #include "base/containers/span.h" |
piperc | 7929e07 | 2017-01-20 01:04:49 | [diff] [blame] | 14 | #include "base/gtest_prod_util.h" |
Jun Choi | 3545f50 | 2018-03-14 01:30:00 | [diff] [blame] | 15 | #include "base/macros.h" |
| 16 | #include "base/optional.h" |
piperc | 7929e07 | 2017-01-20 01:04:49 | [diff] [blame] | 17 | |
Jun Choi | 3545f50 | 2018-03-14 01:30:00 | [diff] [blame] | 18 | namespace apdu { |
piperc | 7929e07 | 2017-01-20 01:04:49 | [diff] [blame] | 19 | |
| 20 | // APDU commands are defined as part of ISO 7816-4. Commands can be serialized |
Pavel Kalinnikov | 0f59e1b | 2017-09-08 20:31:17 | [diff] [blame] | 21 | // into either short length encodings, where the maximum data length is 256 |
piperc | 7929e07 | 2017-01-20 01:04:49 | [diff] [blame] | 22 | // bytes, or an extended length encoding, where the maximum data length is 65536 |
| 23 | // bytes. This class implements only the extended length encoding. Serialized |
| 24 | // commands consist of a CLA byte, denoting the class of instruction, an INS |
| 25 | // byte, denoting the instruction code, P1 and P2, each one byte denoting |
| 26 | // instruction parameters, a length field (Lc), a data field of length Lc, and |
| 27 | // a maximum expected response length (Le). |
Jun Choi | 3545f50 | 2018-03-14 01:30:00 | [diff] [blame] | 28 | class COMPONENT_EXPORT(APDU) ApduCommand { |
piperc | 7929e07 | 2017-01-20 01:04:49 | [diff] [blame] | 29 | public: |
Jun Choi | b49986d | 2018-03-13 20:01:44 | [diff] [blame] | 30 | // Constructs an APDU command from the serialized message data. |
Jun Choi | 3545f50 | 2018-03-14 01:30:00 | [diff] [blame] | 31 | static base::Optional<ApduCommand> CreateFromMessage( |
| 32 | base::span<const uint8_t> message); |
Jun Choi | b49986d | 2018-03-13 20:01:44 | [diff] [blame] | 33 | |
Jun Choi | 3545f50 | 2018-03-14 01:30:00 | [diff] [blame] | 34 | ApduCommand(); |
| 35 | ApduCommand(uint8_t cla, |
| 36 | uint8_t ins, |
| 37 | uint8_t p1, |
| 38 | uint8_t p2, |
| 39 | size_t response_length, |
| 40 | std::vector<uint8_t> data); |
| 41 | ApduCommand(ApduCommand&& that); |
| 42 | ApduCommand& operator=(ApduCommand&& that); |
| 43 | ~ApduCommand(); |
piperc | 6de0082 | 2017-03-22 16:37:32 | [diff] [blame] | 44 | |
Pavel Kalinnikov | 0f59e1b | 2017-09-08 20:31:17 | [diff] [blame] | 45 | // Returns serialized message data. |
piperc | 7929e07 | 2017-01-20 01:04:49 | [diff] [blame] | 46 | std::vector<uint8_t> GetEncodedCommand() const; |
Jun Choi | b49986d | 2018-03-13 20:01:44 | [diff] [blame] | 47 | |
piperc | 7929e07 | 2017-01-20 01:04:49 | [diff] [blame] | 48 | void set_cla(uint8_t cla) { cla_ = cla; } |
| 49 | void set_ins(uint8_t ins) { ins_ = ins; } |
| 50 | void set_p1(uint8_t p1) { p1_ = p1; } |
| 51 | void set_p2(uint8_t p2) { p2_ = p2; } |
Jun Choi | b49986d | 2018-03-13 20:01:44 | [diff] [blame] | 52 | void set_data(std::vector<uint8_t> data) { data_ = std::move(data); } |
piperc | 7929e07 | 2017-01-20 01:04:49 | [diff] [blame] | 53 | void set_response_length(size_t response_length) { |
| 54 | response_length_ = response_length; |
| 55 | } |
Jun Choi | ef10f86 | 2017-11-09 01:04:33 | [diff] [blame] | 56 | |
Jun Choi | b49986d | 2018-03-13 20:01:44 | [diff] [blame] | 57 | uint8_t cla() const { return cla_; } |
| 58 | uint8_t ins() const { return ins_; } |
| 59 | uint8_t p1() const { return p1_; } |
| 60 | uint8_t p2() const { return p2_; } |
| 61 | size_t response_length() const { return response_length_; } |
| 62 | const std::vector<uint8_t>& data() const { return data_; } |
piperc | 7929e07 | 2017-01-20 01:04:49 | [diff] [blame] | 63 | |
Adam Langley | 1d08c09 | 2018-03-27 09:16:37 | [diff] [blame] | 64 | static constexpr size_t kApduMaxResponseLength = 65536; |
| 65 | |
piperc | 7929e07 | 2017-01-20 01:04:49 | [diff] [blame] | 66 | private: |
Jun Choi | 3545f50 | 2018-03-14 01:30:00 | [diff] [blame] | 67 | FRIEND_TEST_ALL_PREFIXES(ApduTest, TestDeserializeBasic); |
| 68 | FRIEND_TEST_ALL_PREFIXES(ApduTest, TestDeserializeComplex); |
| 69 | FRIEND_TEST_ALL_PREFIXES(ApduTest, TestSerializeEdgeCases); |
Arnar Birgisson | edc8fe4 | 2018-03-13 00:52:26 | [diff] [blame] | 70 | |
piperc | 7929e07 | 2017-01-20 01:04:49 | [diff] [blame] | 71 | static constexpr size_t kApduMinHeader = 4; |
| 72 | static constexpr size_t kApduMaxHeader = 7; |
Jun Choi | 3545f50 | 2018-03-14 01:30:00 | [diff] [blame] | 73 | static constexpr size_t kApduCommandDataOffset = 7; |
| 74 | static constexpr size_t kApduCommandLengthOffset = 5; |
| 75 | |
piperc | 7929e07 | 2017-01-20 01:04:49 | [diff] [blame] | 76 | // As defined in ISO7816-4, extended length APDU request data is limited to |
| 77 | // 16 bits in length with a maximum value of 65535. Response data length is |
| 78 | // also limited to 16 bits in length with a value of 0x0000 corresponding to |
Pavel Kalinnikov | 0f59e1b | 2017-09-08 20:31:17 | [diff] [blame] | 79 | // a length of 65536. |
piperc | 7929e07 | 2017-01-20 01:04:49 | [diff] [blame] | 80 | static constexpr size_t kApduMaxDataLength = 65535; |
piperc | 7929e07 | 2017-01-20 01:04:49 | [diff] [blame] | 81 | static constexpr size_t kApduMaxLength = |
| 82 | kApduMaxDataLength + kApduMaxHeader + 2; |
Pavel Kalinnikov | 0f59e1b | 2017-09-08 20:31:17 | [diff] [blame] | 83 | |
Jun Choi | 3545f50 | 2018-03-14 01:30:00 | [diff] [blame] | 84 | uint8_t cla_ = 0; |
| 85 | uint8_t ins_ = 0; |
| 86 | uint8_t p1_ = 0; |
| 87 | uint8_t p2_ = 0; |
| 88 | size_t response_length_ = 0; |
piperc | 7929e07 | 2017-01-20 01:04:49 | [diff] [blame] | 89 | std::vector<uint8_t> data_; |
Jun Choi | 3545f50 | 2018-03-14 01:30:00 | [diff] [blame] | 90 | |
| 91 | DISALLOW_COPY_AND_ASSIGN(ApduCommand); |
piperc | 7929e07 | 2017-01-20 01:04:49 | [diff] [blame] | 92 | }; |
Pavel Kalinnikov | 0f59e1b | 2017-09-08 20:31:17 | [diff] [blame] | 93 | |
Jun Choi | 3545f50 | 2018-03-14 01:30:00 | [diff] [blame] | 94 | } // namespace apdu |
piperc | 7929e07 | 2017-01-20 01:04:49 | [diff] [blame] | 95 | |
Jun Choi | 3545f50 | 2018-03-14 01:30:00 | [diff] [blame] | 96 | #endif // COMPONENTS_APDU_APDU_COMMAND_H_ |