14#include "keyple/card/calypso/crypto/legacysam/CommandWriteKey.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/CommandSamGenerateKey.hpp"
26#include "keyple/card/calypso/crypto/legacysam/CommandSelectDiversifier.hpp"
27#include "keyple/card/calypso/crypto/legacysam/CounterOverflowException.hpp"
28#include "keyple/card/calypso/crypto/legacysam/DtoAdapters.hpp"
29#include "keyple/card/calypso/crypto/legacysam/IllegalParameterException.hpp"
30#include "keyple/card/calypso/crypto/legacysam/IncorrectInputDataException.hpp"
31#include "keyple/card/calypso/crypto/legacysam/LegacySamUtil.hpp"
32#include "keyple/card/calypso/crypto/legacysam/SecurityDataException.hpp"
33#include "keyple/core/util/ApduUtil.hpp"
34#include "keyple/core/util/ByteArrayUtil.hpp"
35#include "keyple/core/util/cpp/System.hpp"
36#include "keypop/calypso/crypto/legacysam/SystemKeyType.hpp"
37#include "keypop/card/ApduResponseApi.hpp"
45using keyple::core::util::ApduUtil;
46using keyple::core::util::ByteArrayUtil;
47using keyple::core::util::cpp::System;
51const std::map<int, const std::shared_ptr<StatusProperties>>
52 CommandWriteKey::STATUS_TABLE = [] {
53 std::map<int, const std::shared_ptr<StatusProperties>> m(
54 Command::STATUS_TABLE);
58 std::make_shared<StatusProperties>(
59 "Incorrect Lc",
typeid(IllegalParameterException))},
61 std::make_shared<StatusProperties>(
62 "An event counter cannot be incremented",
63 typeid(CounterOverflowException))},
65 std::make_shared<StatusProperties>(
66 std::string(
"Preconditions not satisfied:\n")
67 +
"- The SAM is locked.\n"
68 +
"- Lock secret and LockedLock=1 at last reset.\n"
69 +
"- CipherEnableBit of PAR1 is 0.\n"
70 +
"- Dynamic mode ciphering and the outgoing challenge "
73 +
"- System key and SystemLockEnableBit=1.\n"
74 +
"- Work key and WorkKeysLockEnableBit=1.\n"
75 +
"- Work key and no empty record in the Work Key File.\n"
76 +
"- Plain lock secret loading and PlainWriteDisabled=1 "
79 +
"- Plain work key loading and "
80 "PlainWorkKeyInputEnableBit=0.\n"
81 +
"- Work key loading by record number reference and "
84 +
"- Static mode and StaticCipherEnableBit=0",
85 typeid(AccessForbiddenException))},
87 std::make_shared<StatusProperties>(
88 "Incorrect signature",
typeid(SecurityDataException))},
90 std::make_shared<StatusProperties>(
91 std::string(
"P1 or P2 incorrect:\n")
92 +
"- Incorrect data reference (P2≠01h to 7Eh, C0h, E0h "
95 +
"- Ciphered data and random value (P1=%01xxxxxx).\n"
96 +
"- System key and plain data (P1=%1xxxxxxx and "
98 +
"- Random value for system or lock secret (P2=C0h or "
101 typeid(IllegalParameterException))},
103 std::make_shared<StatusProperties>(
104 std::string(
"Incorrect plain or decrypted data:\n")
105 +
"- P2 different from P2 in command header.\n"
106 +
"- Ciphered mode and incorrect decrypted data.\n"
107 +
"- Received parameters incorrect (unknown ALG).\n"
108 +
"- Incorrect KIF for a system key (KIF≠%1xxxxxxx).\n"
109 +
"- Incorrect KIF for the lock secret (KIF≠EFh).\n"
110 +
"- Ciphered system or work key writing: PAR7 to PAR10 "
112 +
"not all null.\n" +
"- Work key already exists.\n"
113 +
"- Work key, given KIF=E1h or FDh, and "
114 "SystemLockEnableBit "
116 +
"- CAAD Transfer Control data present and PAR6≤80h or "
118 +
"- CAAD Transfer Control data do not match CAAD record "
119 +
"indicated by PAR6–80h",
120 typeid(IncorrectInputDataException))},
122 std::make_shared<StatusProperties>(
123 std::string(
"Record not found:\n")
124 +
"- Deciphering key not found.\n"
125 +
"- CAAD Transfer Control data present and target CAAD "
127 typeid(DataAccessException))},
129 std::make_shared<StatusProperties>(
130 std::string(
"Lc inconsistent with P1 or P2:\n")
131 +
"- Plain data and CAAD Transfer Control data present "
133 +
"- Not a work key and CAAD Transfer Control data "
135 typeid(IncorrectInputDataException))}});
139CommandWriteKey::CommandWriteKey(
140 std::shared_ptr<CommandContextDto> context,
141 SystemKeyType systemKeyType,
143 const std::vector<std::uint8_t>& keyParameters,
144 bool isTransferredKeyDiversified)
145: Command(CommandRef::WRITE_KEY, 0, context)
146, mTargetKeyReference(LegacySamConstants::TARGET_IS_SYSTEM_KEY_FILE)
147, mCipheringKeyType(SystemKeyType::PERSONALIZATION)
148, mSourceKeyKif(LegacySamConstants::SYSTEM_KEY_TYPE_KIF_MAP.at(systemKeyType))
150, mKeyParameters(keyParameters)
151, mIsTransferredObjectDiversified(isTransferredKeyDiversified)
155CommandWriteKey::CommandWriteKey(
156 std::shared_ptr<CommandContextDto> context,
159 int targetRecordNumber,
160 const std::vector<std::uint8_t>& keyParameters,
161 bool isTransferredKeyDiversified)
162: Command(CommandRef::WRITE_KEY, 0, context)
163, mTargetKeyReference(
164 static_cast<std::uint8_t>(
165 targetRecordNumber == 0 ? LegacySamConstants::RECORD_CHOSEN_BY_THE_SAM
166 : targetRecordNumber))
167, mCipheringKeyType(SystemKeyType::KEY_MANAGEMENT)
170, mKeyParameters(keyParameters)
171, mIsTransferredObjectDiversified(isTransferredKeyDiversified)
175CommandWriteKey::CommandWriteKey(
176 std::shared_ptr<CommandContextDto> context,
179 int targetRecordNumber,
180 const std::vector<std::uint8_t>& keyParameters,
181 const std::vector<std::uint8_t>& diversifier)
182: Command(CommandRef::WRITE_KEY, 0, context)
183, mTargetKeyReference(
184 static_cast<std::uint8_t>(
185 targetRecordNumber == 0 ? LegacySamConstants::RECORD_CHOSEN_BY_THE_SAM
186 : targetRecordNumber))
187, mCipheringKeyType(SystemKeyType::KEY_MANAGEMENT)
190, mKeyParameters(keyParameters)
191, mIsTransferredObjectDiversified(true)
192, mArbitraryDiversifier(diversifier)
196CommandWriteKey::CommandWriteKey(
197 std::shared_ptr<CommandContextDto> context,
198 std::uint8_t lockIndex,
199 std::uint8_t lockParameters,
200 bool isTransferredLockDiversified)
201: Command(CommandRef::WRITE_KEY, 0, context)
202, mTargetKeyReference(LegacySamConstants::TARGET_IS_LOCK_FILE)
203, mCipheringKeyType(SystemKeyType::PERSONALIZATION)
204, mSourceKeyKif(LegacySamConstants::LOCK_KIF)
205, mSourceKeyKvc(lockIndex)
206, mIsTransferredObjectDiversified(isTransferredLockDiversified)
208 std::vector<std::uint8_t> lockParametersAsKeyParameters(
209 LegacySamConstants::KEY_PARAMETERS_LENGTH);
210 lockParametersAsKeyParameters[5] = lockParameters;
211 mKeyParameters = lockParametersAsKeyParameters;
214CommandWriteKey::CommandWriteKey(
215 std::shared_ptr<CommandContextDto> context,
216 const std::vector<std::uint8_t>& plainLockDataBlock)
217: Command(CommandRef::WRITE_KEY, 0, context)
218, mTargetKeyReference(LegacySamConstants::TARGET_IS_LOCK_FILE)
219, mCipheringKeyType(SystemKeyType::UNKNOWN)
223, mIsTransferredObjectDiversified(
false)
225, mWriteKeyCommandData(plainLockDataBlock)
230CommandWriteKey::finalizeRequest()
233 if (mWriteKeyCommandData.empty()) {
234 mCipheringKeyKvc = getContext()
236 ->getSystemKeyParameter(mCipheringKeyType)
240 auto controlSamContext = std::make_shared<CommandContextDto>(
241 getContext()->getControlSam(),
nullptr,
nullptr);
242 std::vector<std::uint8_t> diversifier(8);
244 getContext()->getTargetSam()->getSerialNumber(),
249 addControlSamCommand(
250 std::make_shared<CommandSelectDiversifier>(
251 controlSamContext, diversifier));
252 addControlSamCommand(
253 std::make_shared<CommandGiveRandom>(
255 getContext()->getTargetSam()->popChallenge()));
257 auto commandSamGenerateKey = std::make_shared<CommandSamGenerateKey>(
264 mIsTransferredObjectDiversified,
265 mArbitraryDiversifier);
266 addControlSamCommand(commandSamGenerateKey);
267 processControlSamCommand();
269 mWriteKeyCommandData = commandSamGenerateKey->getCipheredData();
275 const std::uint8_t cla = getContext()->getTargetSam()->getClassByte();
276 const std::uint8_t ins = CommandRef::WRITE_KEY.getInstructionByte();
279 std::make_shared<ApduRequestAdapter>(ApduUtil::build(
280 cla, ins, p1, mTargetKeyReference, mWriteKeyCommandData)));
284CommandWriteKey::isControlSamRequiredToFinalizeRequest()
const
290CommandWriteKey::parseResponse(std::shared_ptr<ApduResponseApi> apduResponse)
292 setResponseAndCheckStatus(apduResponse);
295const std::map<int, const std::shared_ptr<StatusProperties>>&
296CommandWriteKey::getStatusTable()
const
301std::vector<std::uint8_t>
302CommandWriteKey::buildPlainLockDataBlock(
303 std::uint8_t lockIndex,
304 std::uint8_t lockParameters,
305 const std::vector<std::uint8_t>& lockValue)
307 std::vector<std::uint8_t> lockFile(LegacySamConstants::LOCK_FILE_SIZE);
308 lockFile[0] = LegacySamConstants::LOCK_KIF;
309 lockFile[1] = lockIndex;
310 lockFile[7] = lockParameters;
312 lockValue, 0, lockFile, 13, LegacySamConstants::LOCK_VALUE_LENGTH);
314 std::vector<std::uint8_t> plainDataBlock(
315 LegacySamConstants::KEY_DATA_BLOCK_SIZE);
317 lockFile, 0, plainDataBlock, 8, LegacySamConstants::LOCK_FILE_SIZE);
318 plainDataBlock[37] = LegacySamConstants::TARGET_IS_LOCK_FILE;
319 plainDataBlock[45] = 0x80;
321 return plainDataBlock;
DtoAdapters::CommandContextDto CommandContextDto