14#include "keyple/card/calypso/crypto/legacysam/CommandPsoVerifySignature.hpp"
20#include "keyple/card/calypso/crypto/legacysam/AccessForbiddenException.hpp"
21#include "keyple/card/calypso/crypto/legacysam/CounterOverflowException.hpp"
22#include "keyple/card/calypso/crypto/legacysam/DataAccessException.hpp"
23#include "keyple/card/calypso/crypto/legacysam/IllegalParameterException.hpp"
24#include "keyple/card/calypso/crypto/legacysam/IncorrectInputDataException.hpp"
25#include "keyple/card/calypso/crypto/legacysam/LegacySamConstants.hpp"
26#include "keyple/card/calypso/crypto/legacysam/SecurityContextException.hpp"
27#include "keyple/card/calypso/crypto/legacysam/SecurityDataException.hpp"
28#include "keyple/core/util/ApduUtil.hpp"
29#include "keyple/core/util/ByteArrayUtil.hpp"
30#include "keyple/core/util/cpp/Arrays.hpp"
31#include "keyple/core/util/cpp/System.hpp"
32#include "keypop/calypso/crypto/legacysam/transaction/InvalidSignatureException.hpp"
40using keyple::core::util::ApduUtil;
41using keyple::core::util::ByteArrayUtil;
42using keyple::core::util::cpp::Arrays;
43using keyple::core::util::cpp::System;
44using keypop::calypso::crypto::legacysam::transaction::
45 InvalidSignatureException;
47const std::map<int, const std::shared_ptr<StatusProperties>>
48 CommandPsoVerifySignature::STATUS_TABLE = [] {
49 std::map<int, const std::shared_ptr<StatusProperties>> m(
50 Command::STATUS_TABLE);
54 std::make_shared<StatusProperties>(
55 "Incorrect Lc",
typeid(IllegalParameterException))},
57 std::make_shared<StatusProperties>(
58 "Busy status: the command is temporarily unavailable",
59 typeid(SecurityContextException))},
61 std::make_shared<StatusProperties>(
62 "Preconditions not satisfied",
63 typeid(AccessForbiddenException))},
65 std::make_shared<StatusProperties>(
66 "Incorrect signature",
typeid(SecurityDataException))},
68 std::make_shared<StatusProperties>(
69 "Incorrect parameters in incoming data",
70 typeid(IncorrectInputDataException))},
72 std::make_shared<StatusProperties>(
73 "Record not found: signing key not found",
74 typeid(DataAccessException))},
76 std::make_shared<StatusProperties>(
77 "Incorrect P1 or P2",
typeid(IllegalParameterException))}});
81CommandPsoVerifySignature::CommandPsoVerifySignature(
82 std::shared_ptr<CommandContextDto> context,
83 std::shared_ptr<TraceableSignatureVerificationDataAdapter> data)
84: Command(CommandRef::PSO_VERIFY_SIGNATURE, 0, context)
87 const std::uint8_t cla = context->getTargetSam()->getClassByte();
88 const std::uint8_t inst = getCommandRef().getInstructionByte();
89 const std::uint8_t p1 = 0x00;
90 const std::uint8_t p2 = 0xA8;
93 const int messageOffset = data->isSamTraceabilityMode() ? 6 : 4;
94 const int messageSize =
static_cast<int>(data->getData().size());
95 const int signatureSize =
static_cast<int>(data->getSignature().size());
96 std::vector<std::uint8_t> dataIn(
97 messageOffset + messageSize + signatureSize);
106 dataIn[1] = data->getKif();
107 dataIn[2] = data->getKvc();
113 std::uint8_t opMode = 0;
114 if (data->isSamTraceabilityMode()) {
115 if (data->getSamTraceabilityMode()
116 == SamTraceabilityMode::TRUNCATED_SERIAL_NUMBER) {
122 if (data->isBusyMode()) {
127 opMode |=
static_cast<std::uint8_t
>(signatureSize);
134 if (data->isSamTraceabilityMode()) {
135 ByteArrayUtil::copyBytes(data->getTraceabilityOffset(), dataIn, 4, 2);
139 System::arraycopy(data->getData(), 0, dataIn, messageOffset, messageSize);
143 data->getSignature(),
146 dataIn.size() - signatureSize,
150 std::make_shared<ApduRequestAdapter>(
151 ApduUtil::build(cla, inst, p1, p2, dataIn)));
155CommandPsoVerifySignature::finalizeRequest()
161CommandPsoVerifySignature::isControlSamRequiredToFinalizeRequest()
const
167CommandPsoVerifySignature::parseResponse(
168 std::shared_ptr<ApduResponseApi> apduResponse)
171 setResponseAndCheckStatus(apduResponse);
172 mData->setSignatureValid(
true);
174 }
catch (
const SecurityDataException& e) {
175 mData->setSignatureValid(
false);
176 throw InvalidSignatureException(
177 "Invalid signature", std::make_shared<SecurityDataException>(e));
181const std::map<int, const std::shared_ptr<StatusProperties>>&
182CommandPsoVerifySignature::getStatusTable()
const