Keyple Card Calypso C++ Library 2.2.5.6
Reference Terminal Reader API for C++
AbstractApduCommand.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 information regarding *
5 * copyright ownership. *
6 * *
7 * This program and the accompanying materials are made available under the terms of the Eclipse *
8 * Public License 2.0 which is available at http://www.eclipse.org/legal/epl-2.0 *
9 * *
10 * SPDX-License-Identifier: EPL-2.0 *
11 **************************************************************************************************/
12
13#include "AbstractApduCommand.h"
14
15#include <typeinfo>
16
17/* Keyple Core Util */
18#include "StringUtils.h"
19
20/* Keyple Card Calypso */
27#include "CardPinException.h"
34
35#include "CalypsoCardCommand.h"
45
46namespace keyple {
47namespace card {
48namespace calypso {
49
50using namespace keyple::core::util::cpp;
51
53
54/* STATUS PROPERTIES ---------------------------------------------------------------------------- */
55
56StatusProperties::StatusProperties(const std::string& information)
57: mInformation(information), mSuccessful(true), mExceptionClass(typeid(nullptr)) {}
58
60 const std::string& information, const std::type_info& exceptionClass)
61: mInformation(information),
62 mSuccessful(exceptionClass == typeid(nullptr)),
63 mExceptionClass(exceptionClass) {}
64
65const std::string& StatusProperties::getInformation() const
66{
67 return mInformation;
68}
69
71{
72 return mSuccessful;
73}
74
75const std::type_info& StatusProperties::getExceptionClass() const
76{
77 return mExceptionClass;
78}
79
80/* ABSTRACT APDU COMMAND ------------------------------------------------------------------------ */
81
82const std::map<const int, const std::shared_ptr<StatusProperties>>
84 {0x9000, std::make_shared<StatusProperties>("Success")},
85};
86
87AbstractApduCommand::AbstractApduCommand(const CardCommand& commandRef, const int expectedResponseLength)
88: mCommandRef(commandRef), mExpectedResponseLength(expectedResponseLength), mName(commandRef.getName()) {}
89
90void AbstractApduCommand::addSubName(const std::string& subName)
91{
92 mName.append("-").append(subName);
93 mApduRequest->setInfo(mName);
94}
95
97{
98 return mCommandRef;
99}
100
101const std::string& AbstractApduCommand::getName() const
102{
103 return mName;
104}
105
106void AbstractApduCommand::setExpectedResponseLength(const int expectedResponseLength)
107{
108 mExpectedResponseLength = expectedResponseLength;
109}
110
111void AbstractApduCommand::setApduRequest(const std::shared_ptr<ApduRequestAdapter> apduRequest)
112{
113 mApduRequest = apduRequest;
114 mApduRequest->setInfo(mName);
115}
116
117const std::shared_ptr<ApduRequestAdapter> AbstractApduCommand::getApduRequest() const
118{
119 return mApduRequest;
120}
121
122void AbstractApduCommand::parseApduResponse(const std::shared_ptr<ApduResponseApi> apduResponse)
123{
124 mApduResponse = apduResponse;
125
126 checkStatus();
127}
128
129const std::shared_ptr<ApduResponseApi> AbstractApduCommand::getApduResponse() const
130{
131 return mApduResponse;
132}
133
134const std::map<const int, const std::shared_ptr<StatusProperties>>&
136{
137 return STATUS_TABLE;
138}
139
140const std::shared_ptr<StatusProperties> AbstractApduCommand::getStatusWordProperties() const
141{
142 const std::map<const int, const std::shared_ptr<StatusProperties>>& table = getStatusTable();
143
144 const auto it = getStatusTable().find(mApduResponse->getStatusWord());
145
146 return it != table.end() ? it->second : nullptr;
147}
148
150{
151 const std::shared_ptr<StatusProperties> props = getStatusWordProperties();
152
153 return props != nullptr &&
154 props->isSuccessful() &&
155 /* CL-CSS-RESPLE.1 */
156 (mExpectedResponseLength == -1 || static_cast<int>(mApduResponse->getDataOut().size()) == mExpectedResponseLength);
157}
158
159void AbstractApduCommand::checkStatus()
160{
161 const std::shared_ptr<StatusProperties> props = getStatusWordProperties();
162 if (props != nullptr && props->isSuccessful()) {
163
164 /* SW is successful, then check the response length (CL-CSS-RESPLE.1) */
165 if (mExpectedResponseLength != -1 && static_cast<int>(mApduResponse->getDataOut().size()) != mExpectedResponseLength) {
166
167 /*
168 * Throw the exception
169 *
170 * C++: the buildCommandException() mechanism does not work as all exceptions are casted
171 * into a generic type that prevents try/catch blocks from catching derived type
172 * exceptions.
173 * Copy/pasted the function content here.
174 */
175 try {
176
177 /* Try with Card Command first */
178 (void)dynamic_cast<const CalypsoCardCommand&>(getCommandRef());
181 StringUtils::format("Incorrect APDU response length (expected: %d, " \
182 "actual: %d)",
183 mExpectedResponseLength,
184 mApduResponse->getDataOut().size()));
185
186 throw static_cast<const CardUnexpectedResponseLengthException&>(ex);
187
188 } catch (const std::bad_cast& e) {
189
190 /* Assume it's Sam Command then */
191 CalypsoApduCommandException ex =
193 StringUtils::format("Incorrect APDU response length (expected: %d, " \
194 "actual: %d)",
195 mExpectedResponseLength,
196 mApduResponse->getDataOut().size()));
197
198 throw static_cast<const CalypsoSamUnexpectedResponseLengthException&>(ex);
199 }
200 }
201
202 /* SW and response length are correct */
203 return;
204 }
205
206 /* Status word is not referenced, or not successful */
207
208 /* Exception class */
209 const std::type_info& exceptionClass = props != nullptr ? props->getExceptionClass()
210 : typeid(nullptr);
211
212 /* Message */
213 const std::string message = props != nullptr ? props->getInformation() : "Unknown status";
214
215 /*
216 * Throw the exception
217 *
218 * C++: the buildCommandException() mechanism does not work as all exceptions are casted into
219 * a generic type that prevents try/catch blocks from catching derived type exceptions.
220 * Copy/pasted the function content here.
221 */
222 //throw buildCommandException(exceptionClass, message);
223
224 try {
225
226 /* Try with Card Command first */
227 const auto& command = dynamic_cast<const CalypsoCardCommand&>(getCommandRef());
228 const auto statusWord = std::make_shared<int>(getApduResponse()->getStatusWord());
229
230 if (exceptionClass == typeid(CardAccessForbiddenException)) {
231
232 throw CardAccessForbiddenException(message, command, statusWord);
233
234 } else if (exceptionClass == typeid(CardDataAccessException)) {
235
236 throw CardDataAccessException(message, command, statusWord);
237
238 } else if (exceptionClass == typeid(CardDataOutOfBoundsException)) {
239
240 throw CardDataOutOfBoundsException(message, command, statusWord);
241
242 } else if (exceptionClass == typeid(CardIllegalArgumentException)) {
243
244 throw CardIllegalArgumentException(message, command);
245
246 } else if (exceptionClass == typeid(CardIllegalParameterException)) {
247
248 throw CardIllegalParameterException(message, command, statusWord);
249
250 } else if (exceptionClass == typeid(CardPinException)) {
251
252 throw CardPinException(message, command, statusWord);
253
254 } else if (exceptionClass == typeid(CardSecurityContextException)) {
255
256 throw CardSecurityContextException(message, command, statusWord);
257
258 } else if (exceptionClass == typeid(CardSecurityDataException)) {
259
260 throw CardSecurityDataException(message, command, statusWord);
261
262 } else if (exceptionClass == typeid(CardSessionBufferOverflowException)) {
263
264 throw CardSessionBufferOverflowException(message, command, statusWord);
265
266 } else if (exceptionClass == typeid(CardTerminatedException)) {
267
268 throw CardTerminatedException(message, command, statusWord);
269
270 } else {
271
272 throw CardUnknownStatusException(message, command, statusWord);
273 }
274
275 } catch (const std::bad_cast& e) {
276
277 /* It's a Sam Command */
278 const auto& command = dynamic_cast<const CalypsoSamCommand&>(getCommandRef());
279 const auto statusWord = std::make_shared<int>(getApduResponse()->getStatusWord());
280
281 if (exceptionClass == typeid(CalypsoSamAccessForbiddenException)) {
282
283 throw CalypsoSamAccessForbiddenException(message, command, statusWord);
284
285 } else if (exceptionClass == typeid(CalypsoSamCounterOverflowException)) {
286
287 throw CalypsoSamCounterOverflowException(message, command, statusWord);
288
289 } else if (exceptionClass == typeid(CalypsoSamDataAccessException)) {
290
291 throw CalypsoSamDataAccessException(message, command, statusWord);
292
293 } else if (exceptionClass == typeid(CalypsoSamIllegalArgumentException)) {
294
295 throw CalypsoSamIllegalArgumentException(message, command);
296
297 } else if (exceptionClass == typeid(CalypsoSamIllegalParameterException)) {
298
299 throw CalypsoSamIllegalParameterException(message, command, statusWord);
300
301 } else if (exceptionClass == typeid(CalypsoSamIncorrectInputDataException)) {
302
303 throw CalypsoSamIncorrectInputDataException(message, command, statusWord);
304
305 } else if (exceptionClass == typeid(CalypsoSamSecurityDataException)) {
306
307 throw CalypsoSamSecurityDataException(message, command, statusWord);
308
309 } else {
310
311 throw CalypsoSamUnknownStatusException(message, command, statusWord);
312 }
313 }
314}
315
317{
318 const std::shared_ptr<StatusProperties> props = getStatusWordProperties();
319
320 return props != nullptr ? props->getInformation() : "";
321}
322
323}
324}
325}
virtual const std::string & getName() const final
static const std::map< const int, const std::shared_ptr< StatusProperties > > STATUS_TABLE
virtual void addSubName(const std::string &subName) final
virtual const std::map< const int, const std::shared_ptr< StatusProperties > > & getStatusTable() const
virtual const std::string getStatusInformation() const final
virtual const std::shared_ptr< ApduResponseApi > getApduResponse() const final
virtual const std::shared_ptr< ApduRequestAdapter > getApduRequest() const final
virtual const CardCommand & getCommandRef() const
virtual void setApduRequest(const std::shared_ptr< ApduRequestAdapter > apduRequest) final
virtual const CalypsoApduCommandException buildUnexpectedResponseLengthException(const std::string &message) const =0
AbstractApduCommand(const CardCommand &commandRef, const int expectedResponseLength)
virtual void setExpectedResponseLength(const int expectedResponseLength) final
virtual void parseApduResponse(const std::shared_ptr< ApduResponseApi > apduResponse)
AbstractApduCommand::StatusProperties StatusProperties