14#include "keyple/card/calypso/crypto/legacysam/CommandWriteCeilings.hpp"
22#include "keyple/card/calypso/crypto/legacysam/AccessForbiddenException.hpp"
23#include "keyple/card/calypso/crypto/legacysam/CommandGiveRandom.hpp"
24#include "keyple/card/calypso/crypto/legacysam/CommandSamDataCipher.hpp"
25#include "keyple/card/calypso/crypto/legacysam/CommandSelectDiversifier.hpp"
26#include "keyple/card/calypso/crypto/legacysam/CounterOverflowException.hpp"
27#include "keyple/card/calypso/crypto/legacysam/DtoAdapters.hpp"
28#include "keyple/card/calypso/crypto/legacysam/IllegalParameterException.hpp"
29#include "keyple/card/calypso/crypto/legacysam/IncorrectInputDataException.hpp"
30#include "keyple/card/calypso/crypto/legacysam/LegacySamUtil.hpp"
31#include "keyple/card/calypso/crypto/legacysam/SecurityDataException.hpp"
32#include "keyple/core/util/ApduUtil.hpp"
33#include "keyple/core/util/ByteArrayUtil.hpp"
34#include "keypop/calypso/crypto/legacysam/SystemKeyType.hpp"
35#include "keypop/card/ApduResponseApi.hpp"
43using keyple::core::util::ApduUtil;
44using keyple::core::util::ByteArrayUtil;
48const std::map<int, const std::shared_ptr<StatusProperties>>
49 CommandWriteCeilings::STATUS_TABLE = [] {
50 std::map<int, const std::shared_ptr<StatusProperties>> m(
51 Command::STATUS_TABLE);
55 std::make_shared<StatusProperties>(
56 "Incorrect Lc",
typeid(IllegalParameterException))},
58 std::make_shared<StatusProperties>(
59 "Preconditions not satisfied:\n"
60 "- The SAM is locked.\n"
61 "- In P1, b7 is equal to 1.\n"
62 "- CipherEnableBit of PAR1 is 0.\n"
63 "- Dynamic mode and the outgoing challenge is unavailable.\n"
64 "- Static mode and StaticCipherEnableBit=0",
65 typeid(AccessForbiddenException))},
67 std::make_shared<StatusProperties>(
68 "An event counter cannot be incremented",
69 typeid(CounterOverflowException))},
71 std::make_shared<StatusProperties>(
72 "Incorrect signature",
typeid(SecurityDataException))},
74 std::make_shared<StatusProperties>(
75 "Incorrect P2",
typeid(IllegalParameterException))},
77 std::make_shared<StatusProperties>(
78 "Incorrect decrypted data, including:\n"
79 "- Full ceiling record update (P2!=B8h) with DES/DESX key44 "
81 "least one ceiling is increased by more than 131,071.\n"
82 "- One ceiling update (P2=B8h): reference>26, or the 25 last "
84 "bytes are not all null",
85 typeid(IncorrectInputDataException))},
87 std::make_shared<StatusProperties>(
88 "Record not found: deciphering key not found",
89 typeid(DataAccessException))}});
94CommandWriteCeilings::CommandWriteCeilings(
95 std::shared_ptr<DtoAdapters::CommandContextDto> context,
96 std::shared_ptr<DtoAdapters::TargetSamContextDto> targetSamContext,
100 CommandRef::WRITE_CEILINGS,
101 LegacySamConstants::PLAIN_CEILING_DATA_BLOCK_SIZE,
103, mTargetSamContext(targetSamContext)
104, mPlainData(LegacySamConstants::PLAIN_CEILING_DATA_BLOCK_SIZE)
105, mCounterFileRecordNumber(-1)
112 mPlainData[0] = (mTargetSamContext ==
nullptr)
115 ->getSystemKeyParameter(SystemKeyType::RELOADING)
117 : mTargetSamContext->getSystemKeyTypeToKvcMap()
118 .find(SystemKeyType::RELOADING)
120 mPlainData[1] =
static_cast<uint8_t
>(counterNumber);
121 ByteArrayUtil::copyBytes(ceilingValue, mPlainData, 2, 3);
124CommandWriteCeilings::CommandWriteCeilings(
125 std::shared_ptr<DtoAdapters::CommandContextDto> context,
128: CommandWriteCeilings(context, nullptr, counterNumber, ceilingValue)
132CommandWriteCeilings::CommandWriteCeilings(
133 std::shared_ptr<DtoAdapters::CommandContextDto> context,
134 std::shared_ptr<DtoAdapters::TargetSamContextDto> targetSamContext,
137 CounterIncrementAccess counterIncrementAccess)
139 CommandRef::WRITE_CEILINGS,
140 LegacySamConstants::PLAIN_CEILING_DATA_BLOCK_SIZE,
142, mTargetSamContext(targetSamContext)
143, mPlainData(LegacySamConstants::PLAIN_CEILING_DATA_BLOCK_SIZE)
144, mCounterFileRecordNumber(
145 LegacySamConstants::COUNTER_TO_RECORD_LOOKUP[counterNumber])
151 mPlainData[0] = (mTargetSamContext ==
nullptr)
154 ->getSystemKeyParameter(SystemKeyType::RELOADING)
156 : mTargetSamContext->getSystemKeyTypeToKvcMap()
157 .find(SystemKeyType::RELOADING)
160 addCounter(counterNumber, ceilingValue, counterIncrementAccess);
163CommandWriteCeilings::CommandWriteCeilings(
164 std::shared_ptr<DtoAdapters::CommandContextDto> context,
167 CounterIncrementAccess counterIncrementAccess)
168: CommandWriteCeilings(
169 context, nullptr, counterNumber, ceilingValue, counterIncrementAccess)
174CommandWriteCeilings::addCounter(
177 CounterIncrementAccess counterIncrementAccess)
180 ByteArrayUtil::copyBytes(
181 ceilingValue, mPlainData, (counterNumber % 9) * 3 + 1, 3);
184 mCounterNumberToManualCounterIncrementAuthorizedMap[counterNumber % 9]
185 = counterIncrementAccess;
189CommandWriteCeilings::getCounterFileRecordNumber()
const
191 return mCounterFileRecordNumber;
194const std::map<int, const std::shared_ptr<StatusProperties>>&
195CommandWriteCeilings::getStatusTable()
const
201CommandWriteCeilings::finalizeRequest()
203 auto controlSamContext = std::make_shared<CommandContextDto>(
204 getContext()->getControlSam(),
nullptr,
nullptr);
207 addControlSamCommand(
208 std::make_shared<CommandSelectDiversifier>(
210 (mTargetSamContext ==
nullptr)
211 ? getContext()->getTargetSam()->getSerialNumber()
212 : mTargetSamContext->getSerialNumber()));
214 std::vector<uint8_t> challenge
215 = (mTargetSamContext ==
nullptr)
216 ? getContext()->getTargetSam()->popChallenge()
217 : LegacySamUtil::computeStaticModeChallenge(
218 mTargetSamContext, SystemKeyType::RELOADING);
220 addControlSamCommand(
221 std::make_shared<CommandGiveRandom>(controlSamContext, challenge));
223 if (mCounterFileRecordNumber != -1) {
227 auto commandSamDataCipher = std::make_shared<CommandSamDataCipher>(
229 mCounterFileRecordNumber,
230 (mCounterFileRecordNumber == -1)
231 ? CommandSamDataCipher::DataType::ONE_CEILING_VALUE
232 : CommandSamDataCipher::DataType::CEILINGS_FILE_RECORD,
235 addControlSamCommand(commandSamDataCipher);
237 processControlSamCommand();
239 const uint8_t cla = 0x80;
240 const uint8_t inst = getCommandRef().getInstructionByte();
241 const uint8_t p1 = (mTargetSamContext ==
nullptr)
242 ? LegacySamConstants::DYNAMIC_MODE_CIPHERING
243 : LegacySamConstants::STATIC_MODE_CIPHERING;
246 std::make_shared<ApduRequestAdapter>(ApduUtil::build(
250 (mCounterFileRecordNumber == -1)
251 ? LegacySamConstants::SINGLE_CEILING_REFERENCE
252 :
static_cast<uint8_t
>(
253 LegacySamConstants::FIRST_EVENT_CEILING_RECORD_REFERENCE
254 + mCounterFileRecordNumber),
255 commandSamDataCipher->getCipheredData())));
259CommandWriteCeilings::computePlainData()
261 std::uint16_t configBits = 0;
263 for (
int i = 0; i < 9; i++) {
264 auto it = mCounterNumberToManualCounterIncrementAuthorizedMap.find(i);
265 CounterIncrementAccess config;
266 if (it == mCounterNumberToManualCounterIncrementAuthorizedMap.end()) {
267 config = getContext()->getTargetSam()->getCounterIncrementAccess(
268 (mCounterFileRecordNumber * 9) + i);
273 if (config == CounterIncrementAccess::FREE_COUNTING_ENABLED
274 && (i != 0 || mCounterFileRecordNumber != 0)) {
275 configBits |=
static_cast<std::int16_t
>(1 << i);
279 ByteArrayUtil::copyBytes(configBits, mPlainData, 28, 2);
283CommandWriteCeilings::isControlSamRequiredToFinalizeRequest()
const
289CommandWriteCeilings::parseResponse(
290 std::shared_ptr<ApduResponseApi> apduResponse)
292 setResponseAndCheckStatus(apduResponse);
DtoAdapters::CommandContextDto CommandContextDto