File: apdu_command.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (97 lines) | stat: -rw-r--r-- 3,406 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_APDU_APDU_COMMAND_H_
#define COMPONENTS_APDU_APDU_COMMAND_H_

#include <cinttypes>
#include <optional>
#include <utility>
#include <vector>

#include "base/component_export.h"
#include "base/containers/span.h"
#include "base/gtest_prod_util.h"

namespace apdu {

// APDU commands are defined as part of ISO 7816-4. Commands can be serialized
// into either short length encodings, where the maximum data length is 256
// bytes, or an extended length encoding, where the maximum data length is 65536
// bytes. This class implements only the extended length encoding. Serialized
// commands consist of a CLA byte, denoting the class of instruction, an INS
// byte, denoting the instruction code, P1 and P2, each one byte denoting
// instruction parameters, a length field (Lc), a data field of length Lc, and
// a maximum expected response length (Le).
class COMPONENT_EXPORT(APDU) ApduCommand {
 public:
  // Constructs an APDU command from the serialized message data.
  static std::optional<ApduCommand> CreateFromMessage(
      base::span<const uint8_t> message);

  ApduCommand();
  ApduCommand(uint8_t cla,
              uint8_t ins,
              uint8_t p1,
              uint8_t p2,
              size_t response_length,
              std::vector<uint8_t> data);
  ApduCommand(ApduCommand&& that);
  ApduCommand& operator=(ApduCommand&& that);

  ApduCommand(const ApduCommand&) = delete;
  ApduCommand& operator=(const ApduCommand&) = delete;

  ~ApduCommand();

  // Returns serialized message data.
  std::vector<uint8_t> GetEncodedCommand() const;

  void set_cla(uint8_t cla) { cla_ = cla; }
  void set_ins(uint8_t ins) { ins_ = ins; }
  void set_p1(uint8_t p1) { p1_ = p1; }
  void set_p2(uint8_t p2) { p2_ = p2; }
  void set_data(std::vector<uint8_t> data) { data_ = std::move(data); }
  void set_response_length(size_t response_length) {
    response_length_ = response_length;
  }

  uint8_t cla() const { return cla_; }
  uint8_t ins() const { return ins_; }
  uint8_t p1() const { return p1_; }
  uint8_t p2() const { return p2_; }
  size_t response_length() const { return response_length_; }
  const std::vector<uint8_t>& data() const { return data_; }

  static constexpr size_t kApduMaxResponseLength = 65536;

 private:
  FRIEND_TEST_ALL_PREFIXES(ApduTest, TestDeserializeBasic);
  FRIEND_TEST_ALL_PREFIXES(ApduTest, TestDeserializeComplex);
  FRIEND_TEST_ALL_PREFIXES(ApduTest, TestSerializeEdgeCases);

  static constexpr size_t kApduMinHeader = 4;
  static constexpr size_t kApduMaxHeader = 7;
  static constexpr size_t kApduCommandDataOffset = 7;
  static constexpr size_t kApduCommandLengthOffset = 5;

  // As defined in ISO7816-4, extended length APDU request data is limited to
  // 16 bits in length with a maximum value of 65535. Response data length is
  // also limited to 16 bits in length with a value of 0x0000 corresponding to
  // a length of 65536.
  static constexpr size_t kApduMaxDataLength = 65535;
  static constexpr size_t kApduMaxLength =
      kApduMaxDataLength + kApduMaxHeader + 2;

  uint8_t cla_ = 0;
  uint8_t ins_ = 0;
  uint8_t p1_ = 0;
  uint8_t p2_ = 0;
  size_t response_length_ = 0;
  std::vector<uint8_t> data_;
};

}  // namespace apdu

#endif  // COMPONENTS_APDU_APDU_COMMAND_H_