File: apdu_command.cc

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 (140 lines) | stat: -rw-r--r-- 4,544 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// 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.

#include "components/apdu/apdu_command.h"

#include "base/check_op.h"

namespace apdu {

namespace {

// APDU command data length is 2 bytes encoded in big endian order.
uint16_t ParseMessageLength(base::span<const uint8_t> message, size_t offset) {
  DCHECK_GE(message.size(), offset + 2);
  return (message[offset] << 8) | message[offset + 1];
}

}  // namespace

std::optional<ApduCommand> ApduCommand::CreateFromMessage(
    base::span<const uint8_t> message) {
  if (message.size() < kApduMinHeader || message.size() > kApduMaxLength)
    return std::nullopt;

  uint8_t cla = message[0];
  uint8_t ins = message[1];
  uint8_t p1 = message[2];
  uint8_t p2 = message[3];

  size_t response_length = 0;
  std::vector<uint8_t> data;

  switch (message.size()) {
    // No data present; no expected response.
    case kApduMinHeader:
      break;
    // Invalid encoding sizes.
    case kApduMinHeader + 1:
    case kApduMinHeader + 2:
      return std::nullopt;
    // No data present; response expected.
    case kApduMinHeader + 3:
      // Fifth byte must be 0.
      if (message[4] != 0) {
        return std::nullopt;
      }
      response_length = ParseMessageLength(message, kApduCommandLengthOffset);
      // Special case where response length of 0x0000 corresponds to 65536
      // as defined in ISO7816-4.
      if (response_length == 0) {
        response_length = kApduMaxResponseLength;
      }
      break;
    default:
      // Fifth byte must be 0.
      if (message[4] != 0) {
        return std::nullopt;
      }
      auto data_length = ParseMessageLength(message, kApduCommandLengthOffset);

      if (message.size() == data_length + kApduCommandDataOffset) {
        // No response expected.
        data.insert(data.end(), message.begin() + kApduCommandDataOffset,
                    message.end());
      } else if (message.size() == data_length + kApduCommandDataOffset + 2) {
        // Maximum response size is stored in final 2 bytes.
        data.insert(data.end(), message.begin() + kApduCommandDataOffset,
                    message.end() - 2);
        auto response_length_offset = kApduCommandDataOffset + data_length;
        response_length = ParseMessageLength(message, response_length_offset);
        // Special case where response length of 0x0000 corresponds to 65536
        // as defined in ISO7816-4.
        if (response_length == 0) {
          response_length = kApduMaxResponseLength;
        }
      } else {
        return std::nullopt;
      }
      break;
  }

  return ApduCommand(cla, ins, p1, p2, response_length, std::move(data));
}

ApduCommand::ApduCommand() = default;

ApduCommand::ApduCommand(uint8_t cla,
                         uint8_t ins,
                         uint8_t p1,
                         uint8_t p2,
                         size_t response_length,
                         std::vector<uint8_t> data)
    : cla_(cla),
      ins_(ins),
      p1_(p1),
      p2_(p2),
      response_length_(response_length),
      data_(std::move(data)) {}

ApduCommand::ApduCommand(ApduCommand&& that) = default;

ApduCommand& ApduCommand::operator=(ApduCommand&& that) = default;

ApduCommand::~ApduCommand() = default;

std::vector<uint8_t> ApduCommand::GetEncodedCommand() const {
  std::vector<uint8_t> encoded = {cla_, ins_, p1_, p2_};

  // If data exists, request size (Lc) is encoded in 3 bytes, with the first
  // byte always being null, and the other two bytes being a big-endian
  // representation of the request size. If data length is 0, response size (Le)
  // will be prepended with a null byte.
  if (!data_.empty()) {
    size_t data_length = data_.size();

    encoded.push_back(0x0);
    if (data_length > kApduMaxDataLength) {
      data_length = kApduMaxDataLength;
    }
    encoded.push_back((data_length >> 8) & 0xff);
    encoded.push_back(data_length & 0xff);
    encoded.insert(encoded.end(), data_.begin(), data_.begin() + data_length);
  } else if (response_length_ > 0) {
    encoded.push_back(0x0);
  }

  if (response_length_ > 0) {
    size_t response_length = response_length_;
    if (response_length > kApduMaxResponseLength) {
      response_length = kApduMaxResponseLength;
    }
    // A zero value represents a response length of 65,536 bytes.
    encoded.push_back((response_length >> 8) & 0xff);
    encoded.push_back(response_length & 0xff);
  }
  return encoded;
}

}  // namespace apdu