14#include "keyple/card/calypso/crypto/legacysam/Command.hpp"
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"
39using keyple::core::util::HexUtil;
43const std::map<int, const std::shared_ptr<StatusProperties>>
44 Command::STATUS_TABLE = [] {
45 std::map<int, const std::shared_ptr<StatusProperties>> m;
48 {{0x9000, std::make_shared<StatusProperties>(
"Success")},
50 std::make_shared<StatusProperties>(
51 "No information given",
typeid(CommandException))},
53 std::make_shared<StatusProperties>(
54 "Part of returned data may be corrupted",
55 typeid(CommandException))},
57 std::make_shared<StatusProperties>(
58 "End of file/record reached before reading Ne bytes",
59 typeid(CommandException))},
61 std::make_shared<StatusProperties>(
62 "Authentication failed",
typeid(SecurityDataException))},
64 std::make_shared<StatusProperties>(
65 "Authentication failed, 10 retries left",
66 typeid(SecurityDataException))},
68 std::make_shared<StatusProperties>(
69 "State of non-volatile memory unchanged",
70 typeid(CommandException))},
72 std::make_shared<StatusProperties>(
73 "Memory failure",
typeid(CommandException))},
75 std::make_shared<StatusProperties>(
76 "No information given",
typeid(CommandException))},
78 std::make_shared<StatusProperties>(
79 "Wrong length",
typeid(IllegalParameterException))},
81 std::make_shared<StatusProperties>(
82 "No information given",
typeid(CommandException))},
84 std::make_shared<StatusProperties>(
85 "Logical channel not supported",
86 typeid(IllegalParameterException))},
88 std::make_shared<StatusProperties>(
89 "Secure messaging not supported",
90 typeid(IllegalParameterException))},
92 std::make_shared<StatusProperties>(
93 "No information given",
typeid(CommandException))},
95 std::make_shared<StatusProperties>(
96 "Command incompatible with file structure",
97 typeid(IllegalParameterException))},
99 std::make_shared<StatusProperties>(
100 "Security status not satisfied",
101 typeid(SecurityDataException))},
103 std::make_shared<StatusProperties>(
104 "Authentication method blocked",
105 typeid(SecurityDataException))},
107 std::make_shared<StatusProperties>(
108 "Reference data not usable",
typeid(SecurityDataException))},
110 std::make_shared<StatusProperties>(
111 "Conditions of use not satisfied",
112 typeid(AccessForbiddenException))},
114 std::make_shared<StatusProperties>(
115 "Command not allowed (no current EF)",
116 typeid(AccessForbiddenException))},
118 std::make_shared<StatusProperties>(
119 "Wrong P1/P2",
typeid(IllegalParameterException))},
121 std::make_shared<StatusProperties>(
122 "Incorrect parameters in data field",
123 typeid(IllegalParameterException))},
125 std::make_shared<StatusProperties>(
126 "Function not supported",
typeid(IllegalParameterException))},
128 std::make_shared<StatusProperties>(
129 "File not found",
typeid(IllegalParameterException))},
131 std::make_shared<StatusProperties>(
132 "Record not found",
typeid(IllegalParameterException))},
134 std::make_shared<StatusProperties>(
135 "Not enough memory space",
136 typeid(IllegalParameterException))},
138 std::make_shared<StatusProperties>(
139 "Incorrect parameters P1-P2",
140 typeid(IllegalParameterException))},
142 std::make_shared<StatusProperties>(
143 "Wrong P1/P2",
typeid(IllegalParameterException))},
145 std::make_shared<StatusProperties>(
146 "Wrong Le field",
typeid(IllegalParameterException))},
148 std::make_shared<StatusProperties>(
149 "Instruction code not supported or invalid",
150 typeid(UnknownStatusException))},
152 std::make_shared<StatusProperties>(
153 "Class not supported",
typeid(UnknownStatusException))},
155 std::make_shared<StatusProperties>(
156 "No precise diagnosis",
typeid(UnknownStatusException))}});
162 CommandRef commandRef,
164 std::shared_ptr<DtoAdapters::CommandContextDto> context)
165: mCommandRef(commandRef)
167, mContext(std::move(context))
172Command::addSubName(
const std::string& subName)
174 mName +=
" - " + subName;
176 mApduRequest->setInfo(mName);
180Command::getCommandRef()
const
185std::shared_ptr<ApduRequestAdapter>
186Command::getApduRequest()
const
191std::shared_ptr<CommandContextDto>
192Command::getContext()
const
198Command::addControlSamCommand(std::shared_ptr<Command> command)
200 mControlSamCommands.push_back(std::move(command));
204Command::processControlSamCommand()
207 CommandExecutor::processCommands(
209 mContext->getControlSamReader(),
210 ChannelControl::KEEP_OPEN);
212 }
catch (
const std::exception&) {
217 mControlSamCommands.clear();
221Command::setApduRequest(std::shared_ptr<ApduRequestAdapter> apduRequest)
223 mApduRequest = std::move(apduRequest);
226std::shared_ptr<StatusProperties>
227Command::getStatusWordProperties()
229 if (mApduResponse ==
nullptr) {
230 throw std::exception();
233 const auto it = getStatusTable().find(mApduResponse->getStatusWord());
235 return (it != getStatusTable().end()) ? it->second :
nullptr;
239Command::setResponseAndCheckStatus(
240 std::shared_ptr<ApduResponseApi> apduResponse)
242 mApduResponse = apduResponse;
247Command::checkStatus()
249 const auto props = getStatusWordProperties();
250 if (props !=
nullptr && props->isSuccessful()) {
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()));
267 const std::type_info& exceptionClass
268 = props !=
nullptr ? props->getExceptionClass() :
typeid(
nullptr);
271 const std::string message
272 = props !=
nullptr ? props->getInformation() :
"Unknown status";
275 throwCommandException(exceptionClass, message);
278std::shared_ptr<ApduResponseApi>
279Command::getApduResponse()
const
281 return mApduResponse;
284const std::map<int, const std::shared_ptr<StatusProperties>>&
285Command::getStatusTable()
const
291Command::throwCommandException(
292 const std::type_info& exceptionClass,
const std::string& message)
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);
309 throw UnknownStatusException(message);
314: mInformation(information)
316, mExceptionClass(typeid(nullptr))
321 const std::string& information,
const std::type_info& exceptionClass)
322: mInformation(information)
323, mSuccessful(exceptionClass == typeid(nullptr))
324, mExceptionClass(exceptionClass)
329Command::StatusProperties::getInformation()
const
335Command::StatusProperties::isSuccessful()
const
341Command::StatusProperties::getExceptionClass()
const
343 return mExceptionClass;
Command::StatusProperties StatusProperties