keyple-card-calypso-crypto-legacysam-cpp-lib - 1.0.2
Component of the Keyple C++ middleware
CommandPsoComputeCertificate.cpp
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (c) 2025 Calypso Networks Association https://calypsonet.org/ *
3 * *
4 * See the NOTICE file(s) distributed with this work for additional *
5 * information regarding copyright ownership. *
6 * *
7 * This program and the accompanying materials are made available under the *
8 * terms of the Eclipse Public License 2.0 which is available at *
9 * http://www.eclipse.org/legal/epl-2.0 *
10 * *
11 * SPDX-License-Identifier: EPL-2.0 *
12 ******************************************************************************/
13
14#include "keyple/card/calypso/crypto/legacysam/CommandPsoComputeCertificate.hpp"
15
16#include <algorithm>
17#include <cstring>
18#include <map>
19#include <memory>
20#include <vector>
21
22#include "keyple/card/calypso/crypto/legacysam/AccessForbiddenException.hpp"
23#include "keyple/card/calypso/crypto/legacysam/CounterOverflowException.hpp"
24#include "keyple/card/calypso/crypto/legacysam/DataAccessException.hpp"
25#include "keyple/card/calypso/crypto/legacysam/IllegalParameterException.hpp"
26#include "keyple/card/calypso/crypto/legacysam/IncorrectInputDataException.hpp"
27#include "keyple/card/calypso/crypto/legacysam/LegacySamConstants.hpp"
28#include "keyple/core/util/ApduUtil.hpp"
29#include "keyple/core/util/HexUtil.hpp"
30#include "keyple/core/util/cpp/Arrays.hpp"
31
32namespace keyple {
33namespace card {
34namespace calypso {
35namespace crypto {
36namespace legacysam {
37
38using keyple::core::util::ApduUtil;
39using keyple::core::util::HexUtil;
40using keyple::core::util::cpp::Arrays;
41
42const std::map<int, const std::shared_ptr<StatusProperties>>
43 CommandPsoComputeCertificate::STATUS_TABLE = [] {
44 std::map<int, const std::shared_ptr<StatusProperties>> m(
45 Command::STATUS_TABLE);
46
47 m.insert(
48 {{0x6700,
49 std::make_shared<StatusProperties>(
50 "Incorrect Lc", typeid(IllegalParameterException))},
51 {0x6900,
52 std::make_shared<StatusProperties>(
53 "An event counter cannot be incremented",
54 typeid(CounterOverflowException))},
55 {0x6985,
56 std::make_shared<StatusProperties>(
57 "Preconditions not satisfied",
58 typeid(AccessForbiddenException))},
59 {0x6A80,
60 std::make_shared<StatusProperties>(
61 "Incorrect value in the incoming data",
62 typeid(IncorrectInputDataException))},
63 {0x6A88,
64 std::make_shared<StatusProperties>(
65 "Unknown incoming data object (incorrect tag)",
66 typeid(DataAccessException))},
67 {0x6B00,
68 std::make_shared<StatusProperties>(
69 "Incorrect P1 or P2", typeid(IllegalParameterException))}});
70 return m;
71 }();
72
73CommandPsoComputeCertificate::CommandPsoComputeCertificate(
74 std::shared_ptr<CommandContextDto> context,
75 std::shared_ptr<LegacyCardCertificateComputationData> data)
76: Command(
77 CommandRef::PSO_COMPUTE_CERTIFICATE,
78 LegacySamConstants::TagInfo::GENERATED_CARD_CERTIFICATE.getTotalLength(),
79 context)
80, mData(
81 std::dynamic_pointer_cast<LegacyCardCertificateComputationDataAdapter>(
82 data))
83{
84 const std::uint8_t cla = context->getTargetSam()->getClassByte();
85 const std::uint8_t inst = getCommandRef().getInstructionByte();
86 const std::uint8_t p1 = 0xEE;
87 const std::uint8_t p2 = 0xAC;
88
89 setApduRequest(
90 std::make_shared<ApduRequestAdapter>(ApduUtil::build(
91 cla, inst, p1, p2, generateCardPublicKeyData(mData), 0xFF)));
92}
93
94std::vector<std::uint8_t>
95CommandPsoComputeCertificate::generateCardPublicKeyData(
96 std::shared_ptr<LegacyCardCertificateComputationDataAdapter> data)
97{
98 /* BER-TLV header */
99 std::vector<std::uint8_t> header
100 = LegacySamConstants::TagInfo::CARD_PUBLIC_KEY_DATA.getHeader();
101
102 /*
103 * Allocate buffer size according to the presence of cardPublicKey, adjust
104 * length byte accordingly
105 */
106 std::vector<std::uint8_t> cardPublicKeyData;
107 if (!data->getCardPublicKey().empty()) {
108 header[2] = 127; // change default length
109 }
110
111 std::copy(
112 header.begin(), header.end(), std::back_inserter(cardPublicKeyData));
113
114 /* AID length */
115 cardPublicKeyData.push_back(
116 static_cast<std::uint8_t>(data->getAid().size()));
117
118 /* AID */
119 const std::vector<std::uint8_t> aid = data->getAid();
120 std::copy(aid.begin(), aid.end(), std::back_inserter(cardPublicKeyData));
121
122 /* AID padding */
123 cardPublicKeyData.resize(
124 cardPublicKeyData.size() + LegacySamConstants::AID_SIZE_MAX
125 - aid.size(),
126 0);
127
128 /* Serial number */
129 const std::vector<uint8_t> serial = data->getSerialNumber();
130 std::copy(
131 serial.begin(), serial.end(), std::back_inserter(cardPublicKeyData));
132
133 /* RFU */
134 cardPublicKeyData.resize(cardPublicKeyData.size() + 4, 0);
135
136 /* Start date */
137 size_t previousSize = cardPublicKeyData.size();
138 cardPublicKeyData.resize(cardPublicKeyData.size() + 4);
139 const uint32_t startDate = data->getStartDateBcd();
140 std::memcpy(cardPublicKeyData.data() + previousSize, &startDate, 4);
141
142 /* End date */
143 previousSize = cardPublicKeyData.size();
144 cardPublicKeyData.resize(cardPublicKeyData.size() + 4);
145 const uint32_t endDate = data->getEndDateBcd();
146 std::memcpy(cardPublicKeyData.data() + previousSize, &endDate, 4);
147
148 /* Rights (RFU) */
149 cardPublicKeyData.resize(cardPublicKeyData.size() + 1, 0);
150
151 /* Startup information */
152 const std::vector<std::uint8_t>& info = data->getStartupInfo();
153 std::copy(info.begin(), info.end(), std::back_inserter(cardPublicKeyData));
154
155 /* RFU */
156 cardPublicKeyData.resize(cardPublicKeyData.size() + 18, 0);
157
158 /* Public key if provided */
159 const std::vector<std::uint8_t>& publicKey = data->getCardPublicKey();
160 if (!publicKey.empty()) {
161 std::copy(
162 publicKey.begin(),
163 publicKey.end(),
164 std::back_inserter(cardPublicKeyData));
165 }
166
167 return cardPublicKeyData;
168}
169
170void
171CommandPsoComputeCertificate::finalizeRequest()
172{
173 /* Nothing to do */
174}
175
176bool
177CommandPsoComputeCertificate::isControlSamRequiredToFinalizeRequest() const
178{
179 return false;
180}
181
182void
183CommandPsoComputeCertificate::parseResponse(
184 std::shared_ptr<ApduResponseApi> apduResponse)
185{
186 setResponseAndCheckStatus(apduResponse);
187 const std::vector<std::uint8_t> dataOut = apduResponse->getDataOut();
188 if (dataOut.size() > 0) {
189 /* Check BER-TLV header */
190 const std::vector<std::uint8_t>& header
191 = LegacySamConstants::TagInfo::GENERATED_CARD_CERTIFICATE
192 .getHeader();
193 for (int i = 0; i < static_cast<int>(header.size()); i++) {
194 if (dataOut[i] != header[i]) {
195 throw DataAccessException(
196 "Inconsistent BER-TLV tag. Expected: "
197 + HexUtil::toHex(header[i])
198 + "h, Actual: " + HexUtil::toHex(dataOut[i]) + "h");
199 }
200 }
201
202 mData->setCertificate(
203 Arrays::copyOfRange(dataOut, header.size(), dataOut.size()));
204 }
205}
206
207const std::map<int, const std::shared_ptr<StatusProperties>>&
208CommandPsoComputeCertificate::getStatusTable() const
209{
210 return STATUS_TABLE;
211}
212
213} /* namespace legacysam */
214} /* namespace crypto */
215} /* namespace calypso */
216} /* namespace card */
217} /* namespace keyple */