14#include "keyple/card/calypso/crypto/legacysam/CommandPsoComputeSignature.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/core/util/ApduUtil.hpp"
27#include "keyple/core/util/ByteArrayUtil.hpp"
28#include "keyple/core/util/cpp/Arrays.hpp"
29#include "keyple/core/util/cpp/System.hpp"
37using keyple::core::util::ApduUtil;
38using keyple::core::util::ByteArrayUtil;
39using keyple::core::util::cpp::Arrays;
40using keyple::core::util::cpp::System;
42const std::map<int, const std::shared_ptr<StatusProperties>>
43 CommandPsoComputeSignature::STATUS_TABLE = [] {
44 std::map<int, const std::shared_ptr<StatusProperties>> m(
45 Command::STATUS_TABLE);
49 std::make_shared<StatusProperties>(
50 "Incorrect Lc",
typeid(IllegalParameterException))},
52 std::make_shared<StatusProperties>(
53 "An event counter cannot be incremented",
54 typeid(CounterOverflowException))},
56 std::make_shared<StatusProperties>(
57 "Preconditions not satisfied",
58 typeid(AccessForbiddenException))},
60 std::make_shared<StatusProperties>(
61 "Incorrect value in the incoming data",
62 typeid(IncorrectInputDataException))},
64 std::make_shared<StatusProperties>(
65 "Record not found: signing key not found",
66 typeid(DataAccessException))},
68 std::make_shared<StatusProperties>(
69 "Incorrect P1 or P2",
typeid(IllegalParameterException))}});
73CommandPsoComputeSignature::CommandPsoComputeSignature(
74 std::shared_ptr<CommandContextDto> context,
75 std::shared_ptr<TraceableSignatureComputationDataAdapter> data)
76: Command(CommandRef::PSO_COMPUTE_SIGNATURE, 0, context)
79 const std::uint8_t cla = context->getTargetSam()->getClassByte();
80 const std::uint8_t inst = getCommandRef().getInstructionByte();
81 const std::uint8_t p1 = 0x9E;
82 const std::uint8_t p2 = 0x9A;
85 const int messageOffset = data->isSamTraceabilityMode() ? 6 : 4;
86 const int messageSize =
static_cast<int>(data->getData().size());
87 std::vector<std::uint8_t> dataIn(messageOffset + messageSize);
96 dataIn[1] = data->getKif();
97 dataIn[2] = data->getKvc();
103 std::uint8_t opMode = 0;
104 if (data->isSamTraceabilityMode()) {
105 if (data->getSamTraceabilityMode()
106 == SamTraceabilityMode::TRUNCATED_SERIAL_NUMBER) {
112 if (data->isBusyMode()) {
117 opMode |=
static_cast<std::uint8_t
>(data->getSignatureSize());
124 if (data->isSamTraceabilityMode()) {
125 ByteArrayUtil::copyBytes(data->getTraceabilityOffset(), dataIn, 4, 2);
129 System::arraycopy(data->getData(), 0, dataIn, messageOffset, messageSize);
132 std::make_shared<ApduRequestAdapter>(
133 ApduUtil::build(cla, inst, p1, p2, dataIn)));
137CommandPsoComputeSignature::finalizeRequest()
143CommandPsoComputeSignature::isControlSamRequiredToFinalizeRequest()
const
149CommandPsoComputeSignature::parseResponse(
150 std::shared_ptr<ApduResponseApi> apduResponse)
152 setResponseAndCheckStatus(apduResponse);
153 if (apduResponse->getDataOut().size() > 0) {
154 if (mData->isSamTraceabilityMode()) {
155 mData->setSignedData(
157 apduResponse->getDataOut(), mData->getData().size()));
159 mData->setSignedData(mData->getData());
164 apduResponse->getDataOut(),
165 apduResponse->getDataOut().size() - mData->getSignatureSize(),
166 apduResponse->getDataOut().size()));
170const std::map<int, const std::shared_ptr<StatusProperties>>&
171CommandPsoComputeSignature::getStatusTable()
const