keyple-card-calypso-crypto-legacysam-cpp-lib - 1.0.2
Component of the Keyple C++ middleware
Command.cpp
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (c) 2023 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/Command.hpp"
15
16#include <map>
17#include <memory>
18#include <string>
19#include <utility>
20
21#include "keyple/card/calypso/crypto/legacysam/AccessForbiddenException.hpp"
22#include "keyple/card/calypso/crypto/legacysam/CommandException.hpp"
23#include "keyple/card/calypso/crypto/legacysam/CommandExecutor.hpp"
24#include "keyple/card/calypso/crypto/legacysam/CounterOverflowException.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/SecurityContextException.hpp"
28#include "keyple/card/calypso/crypto/legacysam/SecurityDataException.hpp"
29#include "keyple/card/calypso/crypto/legacysam/UnexpectedResponseLengthException.hpp"
30#include "keyple/card/calypso/crypto/legacysam/UnknownStatusException.hpp"
31#include "keyple/core/util/HexUtil.hpp"
32
33namespace keyple {
34namespace card {
35namespace calypso {
36namespace crypto {
37namespace legacysam {
38
39using keyple::core::util::HexUtil;
40
42
43const std::map<int, const std::shared_ptr<StatusProperties>>
44 Command::STATUS_TABLE = [] {
45 std::map<int, const std::shared_ptr<StatusProperties>> m;
46
47 m.insert(
48 {{0x9000, std::make_shared<StatusProperties>("Success")},
49 {0x6200,
50 std::make_shared<StatusProperties>(
51 "No information given", typeid(CommandException))},
52 {0x6281,
53 std::make_shared<StatusProperties>(
54 "Part of returned data may be corrupted",
55 typeid(CommandException))},
56 {0x6282,
57 std::make_shared<StatusProperties>(
58 "End of file/record reached before reading Ne bytes",
59 typeid(CommandException))},
60 {0x6300,
61 std::make_shared<StatusProperties>(
62 "Authentication failed", typeid(SecurityDataException))},
63 {0x6310,
64 std::make_shared<StatusProperties>(
65 "Authentication failed, 10 retries left",
66 typeid(SecurityDataException))},
67 {0x6400,
68 std::make_shared<StatusProperties>(
69 "State of non-volatile memory unchanged",
70 typeid(CommandException))},
71 {0x6581,
72 std::make_shared<StatusProperties>(
73 "Memory failure", typeid(CommandException))},
74 {0x6600,
75 std::make_shared<StatusProperties>(
76 "No information given", typeid(CommandException))},
77 {0x6700,
78 std::make_shared<StatusProperties>(
79 "Wrong length", typeid(IllegalParameterException))},
80 {0x6800,
81 std::make_shared<StatusProperties>(
82 "No information given", typeid(CommandException))},
83 {0x6881,
84 std::make_shared<StatusProperties>(
85 "Logical channel not supported",
86 typeid(IllegalParameterException))},
87 {0x6882,
88 std::make_shared<StatusProperties>(
89 "Secure messaging not supported",
90 typeid(IllegalParameterException))},
91 {0x6900,
92 std::make_shared<StatusProperties>(
93 "No information given", typeid(CommandException))},
94 {0x6981,
95 std::make_shared<StatusProperties>(
96 "Command incompatible with file structure",
97 typeid(IllegalParameterException))},
98 {0x6982,
99 std::make_shared<StatusProperties>(
100 "Security status not satisfied",
101 typeid(SecurityDataException))},
102 {0x6983,
103 std::make_shared<StatusProperties>(
104 "Authentication method blocked",
105 typeid(SecurityDataException))},
106 {0x6984,
107 std::make_shared<StatusProperties>(
108 "Reference data not usable", typeid(SecurityDataException))},
109 {0x6985,
110 std::make_shared<StatusProperties>(
111 "Conditions of use not satisfied",
112 typeid(AccessForbiddenException))},
113 {0x6986,
114 std::make_shared<StatusProperties>(
115 "Command not allowed (no current EF)",
116 typeid(AccessForbiddenException))},
117 {0x6A00,
118 std::make_shared<StatusProperties>(
119 "Wrong P1/P2", typeid(IllegalParameterException))},
120 {0x6A80,
121 std::make_shared<StatusProperties>(
122 "Incorrect parameters in data field",
123 typeid(IllegalParameterException))},
124 {0x6A81,
125 std::make_shared<StatusProperties>(
126 "Function not supported", typeid(IllegalParameterException))},
127 {0x6A82,
128 std::make_shared<StatusProperties>(
129 "File not found", typeid(IllegalParameterException))},
130 {0x6A83,
131 std::make_shared<StatusProperties>(
132 "Record not found", typeid(IllegalParameterException))},
133 {0x6A84,
134 std::make_shared<StatusProperties>(
135 "Not enough memory space",
136 typeid(IllegalParameterException))},
137 {0x6A86,
138 std::make_shared<StatusProperties>(
139 "Incorrect parameters P1-P2",
140 typeid(IllegalParameterException))},
141 {0x6B00,
142 std::make_shared<StatusProperties>(
143 "Wrong P1/P2", typeid(IllegalParameterException))},
144 {0x6C00,
145 std::make_shared<StatusProperties>(
146 "Wrong Le field", typeid(IllegalParameterException))},
147 {0x6D00,
148 std::make_shared<StatusProperties>(
149 "Instruction code not supported or invalid",
150 typeid(UnknownStatusException))},
151 {0x6E00,
152 std::make_shared<StatusProperties>(
153 "Class not supported", typeid(UnknownStatusException))},
154 {0x6F00,
155 std::make_shared<StatusProperties>(
156 "No precise diagnosis", typeid(UnknownStatusException))}});
157
158 return m;
159 }();
160
161Command::Command(
162 CommandRef commandRef,
163 int le,
164 std::shared_ptr<DtoAdapters::CommandContextDto> context)
165: mCommandRef(commandRef)
166, mLe(le)
167, mContext(std::move(context))
168{
169}
170
171void
172Command::addSubName(const std::string& subName)
173{
174 mName += " - " + subName;
175
176 mApduRequest->setInfo(mName);
177}
178
179CommandRef
180Command::getCommandRef() const
181{
182 return mCommandRef;
183}
184
185std::shared_ptr<ApduRequestAdapter>
186Command::getApduRequest() const
187{
188 return mApduRequest;
189}
190
191std::shared_ptr<CommandContextDto>
192Command::getContext() const
193{
194 return mContext;
195}
196
197void
198Command::addControlSamCommand(std::shared_ptr<Command> command)
199{
200 mControlSamCommands.push_back(std::move(command));
201}
202
203void
204Command::processControlSamCommand()
205{
206 try {
207 CommandExecutor::processCommands(
208 mControlSamCommands,
209 mContext->getControlSamReader(),
210 ChannelControl::KEEP_OPEN);
211
212 } catch (const std::exception&) {
213 throw;
214 }
215
216 /* Finally */
217 mControlSamCommands.clear();
218}
219
220void
221Command::setApduRequest(std::shared_ptr<ApduRequestAdapter> apduRequest)
222{
223 mApduRequest = std::move(apduRequest);
224}
225
226std::shared_ptr<StatusProperties>
227Command::getStatusWordProperties()
228{
229 if (mApduResponse == nullptr) {
230 throw std::exception();
231 }
232
233 const auto it = getStatusTable().find(mApduResponse->getStatusWord());
234
235 return (it != getStatusTable().end()) ? it->second : nullptr;
236}
237
238void
239Command::setResponseAndCheckStatus(
240 std::shared_ptr<ApduResponseApi> apduResponse)
241{
242 mApduResponse = apduResponse;
243 checkStatus();
244}
245
246void
247Command::checkStatus()
248{
249 const auto props = getStatusWordProperties();
250 if (props != nullptr && props->isSuccessful()) {
251 /* SW is successful, then check the response length (CL-CSS-RESPLE.1) */
252 if (mLe != 0
253 && mLe != static_cast<int>(mApduResponse->getDataOut().size())) {
254 throw UnexpectedResponseLengthException(
255 "Incorrect APDU response length. Expected: "
256 + std::to_string(mLe) + ", Actual: "
257 + std::to_string(mApduResponse->getDataOut().size()));
258 }
259
260 /* SW and response length are correct. */
261 return;
262 }
263
264 /* Status word is not referenced, or not successful. */
265
266 /* Exception class */
267 const std::type_info& exceptionClass
268 = props != nullptr ? props->getExceptionClass() : typeid(nullptr);
269
270 /* Message */
271 const std::string message
272 = props != nullptr ? props->getInformation() : "Unknown status";
273
274 /* Throw the exception */
275 throwCommandException(exceptionClass, message);
276}
277
278std::shared_ptr<ApduResponseApi>
279Command::getApduResponse() const
280{
281 return mApduResponse;
282}
283
284const std::map<int, const std::shared_ptr<StatusProperties>>&
285Command::getStatusTable() const
286{
287 return STATUS_TABLE;
288}
289
290void
291Command::throwCommandException(
292 const std::type_info& exceptionClass, const std::string& message)
293{
294 if (exceptionClass == typeid(AccessForbiddenException)) {
295 throw AccessForbiddenException(message);
296 } else if (exceptionClass == typeid(CounterOverflowException)) {
297 throw CounterOverflowException(message);
298 } else if (exceptionClass == typeid(DataAccessException)) {
299 throw DataAccessException(message);
300 } else if (exceptionClass == typeid(IllegalParameterException)) {
301 throw IllegalParameterException(message);
302 } else if (exceptionClass == typeid(IncorrectInputDataException)) {
303 throw IncorrectInputDataException(message);
304 } else if (exceptionClass == typeid(SecurityDataException)) {
305 throw SecurityDataException(message);
306 } else if (exceptionClass == typeid(SecurityContextException)) {
307 throw SecurityContextException(message);
308 } else {
309 throw UnknownStatusException(message);
310 }
311}
312
313Command::StatusProperties::StatusProperties(const std::string& information)
314: mInformation(information)
315, mSuccessful(true)
316, mExceptionClass(typeid(nullptr))
317{
318}
319
321 const std::string& information, const std::type_info& exceptionClass)
322: mInformation(information)
323, mSuccessful(exceptionClass == typeid(nullptr))
324, mExceptionClass(exceptionClass)
325{
326}
327
328const std::string&
329Command::StatusProperties::getInformation() const
330{
331 return mInformation;
332}
333
334bool
335Command::StatusProperties::isSuccessful() const
336{
337 return mSuccessful;
338}
339
340const std::type_info&
341Command::StatusProperties::getExceptionClass() const
342{
343 return mExceptionClass;
344}
345
346} /* namespace legacysam */
347} /* namespace crypto */
348} /* namespace calypso */
349} /* namespace card */
350} /* namespace keyple */
Command::StatusProperties StatusProperties
Definition: Command.cpp:41