1 // Copyright 2017 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_APDU_APDU_COMMAND_H_
6 #define COMPONENTS_APDU_APDU_COMMAND_H_
12 #include "base/component_export.h"
13 #include "base/containers/span.h"
14 #include "base/gtest_prod_util.h"
15 #include "third_party/abseil-cpp/absl/types/optional.h"
19 // APDU commands are defined as part of ISO 7816-4. Commands can be serialized
20 // into either short length encodings, where the maximum data length is 256
21 // bytes, or an extended length encoding, where the maximum data length is 65536
22 // bytes. This class implements only the extended length encoding. Serialized
23 // commands consist of a CLA byte, denoting the class of instruction, an INS
24 // byte, denoting the instruction code, P1 and P2, each one byte denoting
25 // instruction parameters, a length field (Lc), a data field of length Lc, and
26 // a maximum expected response length (Le).
27 class COMPONENT_EXPORT(APDU) ApduCommand {
29 // Constructs an APDU command from the serialized message data.
30 static absl::optional<ApduCommand> CreateFromMessage(
31 base::span<const uint8_t> message);
34 ApduCommand(uint8_t cla,
38 size_t response_length,
39 std::vector<uint8_t> data);
40 ApduCommand(ApduCommand&& that);
41 ApduCommand& operator=(ApduCommand&& that);
43 ApduCommand(const ApduCommand&) = delete;
44 ApduCommand& operator=(const ApduCommand&) = delete;
48 // Returns serialized message data.
49 std::vector<uint8_t> GetEncodedCommand() const;
51 void set_cla(uint8_t cla) { cla_ = cla; }
52 void set_ins(uint8_t ins) { ins_ = ins; }
53 void set_p1(uint8_t p1) { p1_ = p1; }
54 void set_p2(uint8_t p2) { p2_ = p2; }
55 void set_data(std::vector<uint8_t> data) { data_ = std::move(data); }
56 void set_response_length(size_t response_length) {
57 response_length_ = response_length;
60 uint8_t cla() const { return cla_; }
61 uint8_t ins() const { return ins_; }
62 uint8_t p1() const { return p1_; }
63 uint8_t p2() const { return p2_; }
64 size_t response_length() const { return response_length_; }
65 const std::vector<uint8_t>& data() const { return data_; }
67 static constexpr size_t kApduMaxResponseLength = 65536;
70 FRIEND_TEST_ALL_PREFIXES(ApduTest, TestDeserializeBasic);
71 FRIEND_TEST_ALL_PREFIXES(ApduTest, TestDeserializeComplex);
72 FRIEND_TEST_ALL_PREFIXES(ApduTest, TestSerializeEdgeCases);
74 static constexpr size_t kApduMinHeader = 4;
75 static constexpr size_t kApduMaxHeader = 7;
76 static constexpr size_t kApduCommandDataOffset = 7;
77 static constexpr size_t kApduCommandLengthOffset = 5;
79 // As defined in ISO7816-4, extended length APDU request data is limited to
80 // 16 bits in length with a maximum value of 65535. Response data length is
81 // also limited to 16 bits in length with a value of 0x0000 corresponding to
83 static constexpr size_t kApduMaxDataLength = 65535;
84 static constexpr size_t kApduMaxLength =
85 kApduMaxDataLength + kApduMaxHeader + 2;
91 size_t response_length_ = 0;
92 std::vector<uint8_t> data_;
97 #endif // COMPONENTS_APDU_APDU_COMMAND_H_