14#include "keyple/card/calypso/crypto/legacysam/CommandReadKeyParameters.hpp"
20#include "keyple/card/calypso/crypto/legacysam/CounterOverflowException.hpp"
21#include "keyple/card/calypso/crypto/legacysam/DataAccessException.hpp"
22#include "keyple/card/calypso/crypto/legacysam/IllegalParameterException.hpp"
23#include "keyple/card/calypso/crypto/legacysam/KeyParameterAdapter.hpp"
24#include "keyple/core/util/ApduUtil.hpp"
25#include "keyple/core/util/cpp/Arrays.hpp"
26#include "keyple/core/util/cpp/exception/IllegalStateException.hpp"
34using keyple::core::util::ApduUtil;
35using keyple::core::util::cpp::Arrays;
36using keyple::core::util::cpp::exception::IllegalStateException;
38const int CommandReadKeyParameters::SW_KEY_NOT_FOUND = 0x6A83;
39const int CommandReadKeyParameters::SW_DATA_NOT_SIGNED_WARNING = 0x6200;
41const std::map<int, const std::shared_ptr<StatusProperties>>
42 CommandReadKeyParameters::STATUS_TABLE = [] {
43 std::map<int, const std::shared_ptr<StatusProperties>> m(
44 Command::STATUS_TABLE);
48 std::make_shared<StatusProperties>(
49 "Incorrect Lc",
typeid(IllegalParameterException))},
51 std::make_shared<StatusProperties>(
52 "An event counter cannot be incremented",
53 typeid(CounterOverflowException))},
55 std::make_shared<StatusProperties>(
56 "Incorrect P2",
typeid(IllegalParameterException))},
58 std::make_shared<StatusProperties>(
59 "Record not found: key to read not found",
60 typeid(DataAccessException))},
62 std::make_shared<StatusProperties>(
63 "Correct execution with warning: data not signed")}});
67CommandReadKeyParameters::CommandReadKeyParameters(
68 std::shared_ptr<DtoAdapters::CommandContextDto> context,
69 SystemKeyType systemKeyType)
70: Command(CommandRef::READ_KEY_PARAMETERS, 32, context)
71, mSystemKeyType(systemKeyType)
76 throw IllegalParameterException(
"context cannot be null");
79 const std::uint8_t cla = context->getTargetSam()->getClassByte();
80 const std::uint8_t inst = getCommandRef().getInstructionByte();
81 const std::uint8_t p1 = 0;
85 switch (systemKeyType) {
86 case SystemKeyType::PERSONALIZATION:
89 case SystemKeyType::KEY_MANAGEMENT:
92 case SystemKeyType::RELOADING:
95 case SystemKeyType::AUTHENTICATION:
99 throw IllegalStateException(
100 "Unexpected SystemKeyType: "
101 + std::to_string(
static_cast<int>(systemKeyType)));
104 const std::vector<std::uint8_t> dataIn = {0x00, 0x00};
106 const auto apdu = std::make_shared<ApduRequestAdapter>(
107 ApduUtil::build(cla, inst, p1, p2, dataIn));
108 apdu->addSuccessfulStatusWord(SW_DATA_NOT_SIGNED_WARNING);
109 apdu->addSuccessfulStatusWord(SW_KEY_NOT_FOUND);
110 setApduRequest(apdu);
112 addSubName(name(mSystemKeyType));
115CommandReadKeyParameters::CommandReadKeyParameters(
116 std::shared_ptr<DtoAdapters::CommandContextDto> context,
117 const std::uint8_t kif,
118 const std::uint8_t kvc)
119: Command(CommandRef::READ_KEY_PARAMETERS, 32, context)
120, mSystemKeyType(SystemKeyType::UNKNOWN)
125 throw IllegalParameterException(
"context cannot be null");
128 const std::uint8_t cla = context->getTargetSam()->getClassByte();
129 const std::uint8_t inst = getCommandRef().getInstructionByte();
130 const std::uint8_t p1 = 0;
131 const std::uint8_t p2 = 0xF0;
132 const std::vector<std::uint8_t> dataIn = {kif, kvc};
134 mKifKvc =
static_cast<std::uint16_t
>((kif << 8) | (kvc & 0xFF));
136 auto adapter = std::make_shared<ApduRequestAdapter>(
137 ApduUtil::build(cla, inst, p1, p2, dataIn));
138 adapter->addSuccessfulStatusWord(SW_DATA_NOT_SIGNED_WARNING);
139 setApduRequest(adapter);
141 addSubName(
"Work key");
144CommandReadKeyParameters::CommandReadKeyParameters(
145 std::shared_ptr<DtoAdapters::CommandContextDto> context,
int recordNumber)
146: Command(CommandRef::READ_KEY_PARAMETERS, 32, context)
147, mSystemKeyType(SystemKeyType::UNKNOWN)
149, mRecordNumber(recordNumber)
152 throw IllegalParameterException(
"context cannot be null");
155 const std::uint8_t cla = context->getTargetSam()->getClassByte();
156 const std::uint8_t inst = getCommandRef().getInstructionByte();
157 const std::uint8_t p1 = 0;
158 const std::uint8_t p2 =
static_cast<std::uint8_t
>(recordNumber);
159 const std::vector<std::uint8_t> dataIn = {0x00, 0x00};
161 auto adapter = std::make_shared<ApduRequestAdapter>(
162 ApduUtil::build(cla, inst, p1, p2, dataIn));
163 adapter->addSuccessfulStatusWord(SW_DATA_NOT_SIGNED_WARNING);
164 adapter->addSuccessfulStatusWord(SW_KEY_NOT_FOUND);
165 setApduRequest(adapter);
167 addSubName(
"Work key");
171CommandReadKeyParameters::finalizeRequest()
177CommandReadKeyParameters::isControlSamRequiredToFinalizeRequest()
const
183CommandReadKeyParameters::parseResponse(
184 std::shared_ptr<ApduResponseApi> apduResponse)
187 setResponseAndCheckStatus(apduResponse);
188 }
catch (
const DataAccessException&) {
192 auto keyParameterAdapter = std::make_shared<KeyParameterAdapter>(
193 Arrays::copyOfRange(apduResponse->getApdu(), 8, 21));
195 std::shared_ptr<LegacySamAdapter> legacySamAdapter
196 = getContext()->getTargetSam();
197 if (mSystemKeyType != SystemKeyType::UNKNOWN) {
198 legacySamAdapter->setSystemKeyParameter(
199 mSystemKeyType, keyParameterAdapter);
200 }
else if (mHasKifKvc) {
201 legacySamAdapter->setWorkKeyParameter(mKifKvc, keyParameterAdapter);
203 legacySamAdapter->setWorkKeyParameter(
204 mRecordNumber, keyParameterAdapter);
208const std::map<int, const std::shared_ptr<StatusProperties>>&
209CommandReadKeyParameters::getStatusTable()
const