14#include "keyple/card/calypso/crypto/legacysam/SecureWriteTransactionManagerAdapter.hpp"
19#include "keyple/card/calypso/crypto/legacysam/CommandGetChallenge.hpp"
20#include "keyple/card/calypso/crypto/legacysam/CommandReadKeyParameters.hpp"
21#include "keyple/card/calypso/crypto/legacysam/CommandWriteCeilings.hpp"
22#include "keyple/card/calypso/crypto/legacysam/CommandWriteKey.hpp"
23#include "keyple/card/calypso/crypto/legacysam/CommandWriteSamParameters.hpp"
24#include "keyple/card/calypso/crypto/legacysam/LegacySamAdapter.hpp"
25#include "keyple/card/calypso/crypto/legacysam/LegacySamConstants.hpp"
26#include "keyple/core/util/KeypleAssert.hpp"
27#include "keypop/calypso/crypto/legacysam/transaction/ReaderIOException.hpp"
28#include "keypop/calypso/crypto/legacysam/transaction/SamIOException.hpp"
29#include "keypop/calypso/crypto/legacysam/transaction/UnexpectedCommandStatusException.hpp"
30#include "keypop/reader/CardCommunicationException.hpp"
31#include "keypop/reader/InvalidCardResponseException.hpp"
32#include "keypop/reader/ReaderCommunicationException.hpp"
40using keyple::core::util::Assert;
41using keypop::calypso::crypto::legacysam::transaction::ReaderIOException;
42using keypop::calypso::crypto::legacysam::transaction::SamIOException;
43using keypop::calypso::crypto::legacysam::transaction::
44 UnexpectedCommandStatusException;
45using keypop::reader::CardCommunicationException;
46using keypop::reader::InvalidCardResponseException;
47using keypop::reader::ReaderCommunicationException;
49SecureWriteTransactionManagerAdapter::SecureWriteTransactionManagerAdapter(
50 std::shared_ptr<ProxyReaderApi> targetSamReader,
51 std::shared_ptr<LegacySamAdapter> targetSam,
52 std::shared_ptr<ProxyReaderApi> controlSamReader,
53 std::shared_ptr<LegacySamAdapter> controlSam)
54: CommonTransactionManagerAdapter(
55 targetSamReader, targetSam, controlSamReader, controlSam)
59SecureWriteTransactionManager&
60SecureWriteTransactionManagerAdapter::prepareWriteSamParameters(
61 const std::vector<uint8_t>& parameters)
63 core::util::Assert::getInstance().isEqual(
65 LegacySamConstants::SAM_PARAMETERS_LENGTH,
68 if (!getContext()->getTargetSam()->getSystemKeyParameter(
69 SystemKeyType::PERSONALIZATION)) {
71 std::make_shared<CommandReadKeyParameters>(
72 getContext(), SystemKeyType::PERSONALIZATION));
75 addTargetSamCommand(std::make_shared<CommandGetChallenge>(getContext(), 8));
77 std::make_shared<CommandWriteSamParameters>(getContext(), parameters));
82SecureWriteTransactionManager&
83SecureWriteTransactionManagerAdapter::prepareTransferSystemKey(
84 SystemKeyType systemKeyType,
86 const std::vector<uint8_t>& systemKeyParameters)
88 return prepareTransferSystemKeyInternal(
89 systemKeyType, kvc, systemKeyParameters,
false);
92SecureWriteTransactionManager&
93SecureWriteTransactionManagerAdapter::prepareTransferSystemKeyDiversified(
94 SystemKeyType systemKeyType,
96 const std::vector<uint8_t>& systemKeyParameters)
98 return prepareTransferSystemKeyInternal(
99 systemKeyType, kvc, systemKeyParameters,
true);
102SecureWriteTransactionManager&
103SecureWriteTransactionManagerAdapter::prepareTransferSystemKeyInternal(
104 SystemKeyType systemKeyType,
106 const std::vector<uint8_t>& systemKeyParameters,
109 core::util::Assert::getInstance().isEqual(
110 systemKeyParameters.size(),
111 LegacySamConstants::KEY_PARAMETERS_LENGTH,
112 "systemKeyParameters.length");
114 if (!getContext()->getTargetSam()->getSystemKeyParameter(
115 SystemKeyType::PERSONALIZATION)) {
117 std::make_shared<CommandReadKeyParameters>(
118 getContext(), SystemKeyType::PERSONALIZATION));
121 addTargetSamCommand(std::make_shared<CommandGetChallenge>(getContext(), 8));
124 std::make_shared<CommandWriteKey>(
134SecureWriteTransactionManager&
135SecureWriteTransactionManagerAdapter::prepareTransferWorkKey(
138 const std::vector<uint8_t>& workKeyParameters,
139 int targetRecordNumber)
141 return prepareTransferWorkKeyInternal(
142 kif, kvc, workKeyParameters, targetRecordNumber,
false, {});
145SecureWriteTransactionManager&
146SecureWriteTransactionManagerAdapter::prepareTransferWorkKeyDiversified(
149 const std::vector<uint8_t>& workKeyParameters,
150 int targetRecordNumber)
152 return prepareTransferWorkKeyInternal(
153 kif, kvc, workKeyParameters, targetRecordNumber,
true, {});
156SecureWriteTransactionManager&
157SecureWriteTransactionManagerAdapter::prepareTransferWorkKeyDiversified(
160 const std::vector<uint8_t>& workKeyParameters,
161 int targetRecordNumber,
162 const std::vector<uint8_t>& diversifier)
164 return prepareTransferWorkKeyInternal(
165 kif, kvc, workKeyParameters, targetRecordNumber,
true, diversifier);
168SecureWriteTransactionManager&
169SecureWriteTransactionManagerAdapter::prepareTransferWorkKeyInternal(
172 const std::vector<uint8_t>& workKeyParameters,
173 int targetRecordNumber,
175 const std::vector<uint8_t>& diversifier)
177 core::util::Assert::getInstance()
179 workKeyParameters.size(),
180 LegacySamConstants::KEY_PARAMETERS_LENGTH,
181 "workKeyParameters.length")
182 .isInRange(targetRecordNumber, 0, 126,
"targetRecordNumber");
184 if (!getContext()->getTargetSam()->getSystemKeyParameter(
185 SystemKeyType::KEY_MANAGEMENT)) {
187 std::make_shared<CommandReadKeyParameters>(
188 getContext(), SystemKeyType::KEY_MANAGEMENT));
191 addTargetSamCommand(std::make_shared<CommandGetChallenge>(getContext(), 8));
193 if (diversifier.empty()) {
195 std::make_shared<CommandWriteKey>(
204 std::make_shared<CommandWriteKey>(
216SecureWriteTransactionManager&
217SecureWriteTransactionManagerAdapter::prepareTransferLock(
218 uint8_t lockIndex, uint8_t lockParameters)
220 return prepareTransferLockInternal(lockIndex, lockParameters,
false);
223SecureWriteTransactionManager&
224SecureWriteTransactionManagerAdapter::prepareTransferLockDiversified(
225 uint8_t lockIndex, uint8_t lockParameters)
227 return prepareTransferLockInternal(lockIndex, lockParameters,
true);
230SecureWriteTransactionManager&
231SecureWriteTransactionManagerAdapter::prepareTransferLockInternal(
232 uint8_t lockIndex, uint8_t lockParameters,
bool isDiversified)
234 if (!getContext()->getTargetSam()->getSystemKeyParameter(
235 SystemKeyType::KEY_MANAGEMENT)) {
237 std::make_shared<CommandReadKeyParameters>(
238 getContext(), SystemKeyType::KEY_MANAGEMENT));
241 addTargetSamCommand(std::make_shared<CommandGetChallenge>(getContext(), 8));
243 std::make_shared<CommandWriteKey>(
244 getContext(), lockIndex, lockParameters, isDiversified));
249SecureWriteTransactionManager&
250SecureWriteTransactionManagerAdapter::preparePlainWriteLock(
252 uint8_t lockParameters,
253 const std::vector<uint8_t>& lockValue)
255 core::util::Assert::getInstance().isEqual(
257 LegacySamConstants::LOCK_VALUE_LENGTH,
261 std::make_shared<CommandWriteKey>(
263 CommandWriteKey::buildPlainLockDataBlock(
264 lockIndex, lockParameters, lockValue)));
269SecureWriteTransactionManager&
270SecureWriteTransactionManagerAdapter::prepareWriteCounterCeiling(
271 int counterNumber,
int ceilingValue)
273 core::util::Assert::getInstance()
276 LegacySamConstants::MIN_COUNTER_CEILING_NUMBER,
277 LegacySamConstants::MAX_COUNTER_CEILING_NUMBER,
281 LegacySamConstants::MIN_COUNTER_CEILING_VALUE,
282 LegacySamConstants::MAX_COUNTER_CEILING_VALUE,
285 if (!getContext()->getTargetSam()->getSystemKeyParameter(
286 SystemKeyType::RELOADING)) {
288 std::make_shared<CommandReadKeyParameters>(
289 getContext(), SystemKeyType::RELOADING));
291 addTargetSamCommand(std::make_shared<CommandGetChallenge>(getContext(), 8));
293 std::make_shared<CommandWriteCeilings>(
294 getContext(), counterNumber, ceilingValue));
299SecureWriteTransactionManager&
300SecureWriteTransactionManagerAdapter::prepareWriteCounterConfiguration(
303 CounterIncrementAccess counterIncrementAccess)
305 core::util::Assert::getInstance()
308 LegacySamConstants::MIN_COUNTER_CEILING_NUMBER,
309 LegacySamConstants::MAX_COUNTER_CEILING_NUMBER,
313 LegacySamConstants::MIN_COUNTER_CEILING_VALUE,
314 LegacySamConstants::MAX_COUNTER_CEILING_VALUE,
317 for (
const auto& command : getTargetSamCommands()) {
318 auto writeCeilingsCommand
319 = std::dynamic_pointer_cast<CommandWriteCeilings>(command);
320 if (writeCeilingsCommand
321 && writeCeilingsCommand->getCounterFileRecordNumber()
322 == LegacySamConstants::COUNTER_TO_RECORD_LOOKUP
324 writeCeilingsCommand->addCounter(
325 counterNumber, ceilingValue, counterIncrementAccess);
331 if (!getContext()->getTargetSam()->getSystemKeyParameter(
332 SystemKeyType::RELOADING)) {
334 std::make_shared<CommandReadKeyParameters>(
335 getContext(), SystemKeyType::RELOADING));
337 addTargetSamCommand(std::make_shared<CommandGetChallenge>(getContext(), 8));
339 std::make_shared<CommandWriteCeilings>(
340 getContext(), counterNumber, ceilingValue, counterIncrementAccess));
345SecureWriteTransactionManager&
346SecureWriteTransactionManagerAdapter::processCommands()
349 return processCommands(ChannelControl::KEEP_OPEN);
350 }
catch (
const ReaderCommunicationException& e) {
351 throw ReaderIOException(e.what(), e);
352 }
catch (
const CardCommunicationException& e) {
353 throw SamIOException(e.what(), e);
354 }
catch (
const InvalidCardResponseException& e) {
355 throw UnexpectedCommandStatusException(e.what(), e);
359SecureWriteTransactionManager&
360SecureWriteTransactionManagerAdapter::processCommands(
361 ChannelControl channelControl)
363 processTargetSamCommands(channelControl);