keyple-card-calypso-crypto-legacysam-cpp-lib - 1.0.2
Component of the Keyple C++ middleware
CommandWriteKey.cpp
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (c) 2019 Calypso Networks Association https://calypsonet.org/ *
3 * *
4 * See the NOTICE file(s) distributed with this work for additional *
5 * information regarding copyright ownership. *
6 * *
7 * This program and the accompanying materials are made available under the *
8 * terms of the Eclipse Public License 2.0 which is available at *
9 * http://www.eclipse.org/legal/epl-2.0 *
10 * *
11 * SPDX-License-Identifier: EPL-2.0 *
12 ******************************************************************************/
13
14#include "keyple/card/calypso/crypto/legacysam/CommandWriteKey.hpp"
15
16#include <cstdint>
17#include <map>
18#include <memory>
19#include <string>
20#include <vector>
21
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"
38
39namespace keyple {
40namespace card {
41namespace calypso {
42namespace crypto {
43namespace legacysam {
44
45using keyple::core::util::ApduUtil;
46using keyple::core::util::ByteArrayUtil;
47using keyple::core::util::cpp::System;
48
50
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);
55
56 m.insert(
57 {{0x6700,
58 std::make_shared<StatusProperties>(
59 "Incorrect Lc", typeid(IllegalParameterException))},
60 {0x6900,
61 std::make_shared<StatusProperties>(
62 "An event counter cannot be incremented",
63 typeid(CounterOverflowException))},
64 {0x6985,
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 "
71 "is "
72 + "unavailable.\n"
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 "
77 "at last"
78 + " reset.\n"
79 + "- Plain work key loading and "
80 "PlainWorkKeyInputEnableBit=0.\n"
81 + "- Work key loading by record number reference and "
82 "record is "
83 + "not empty.\n"
84 + "- Static mode and StaticCipherEnableBit=0",
85 typeid(AccessForbiddenException))},
86 {0x6988,
87 std::make_shared<StatusProperties>(
88 "Incorrect signature", typeid(SecurityDataException))},
89 {0x6A00,
90 std::make_shared<StatusProperties>(
91 std::string("P1 or P2 incorrect:\n")
92 + "- Incorrect data reference (P2≠01h to 7Eh, C0h, E0h "
93 "and "
94 + "F0h).\n"
95 + "- Ciphered data and random value (P1=%01xxxxxx).\n"
96 + "- System key and plain data (P1=%1xxxxxxx and "
97 "P2=C0h).\n"
98 + "- Random value for system or lock secret (P2=C0h or "
99 "E0h and "
100 + "P1=%x1xxxxxx).",
101 typeid(IllegalParameterException))},
102 {0x6A80,
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 "
111 "bytes "
112 + "not all null.\n" + "- Work key already exists.\n"
113 + "- Work key, given KIF=E1h or FDh, and "
114 "SystemLockEnableBit "
115 + "=1.\n"
116 + "- CAAD Transfer Control data present and PAR6≤80h or "
117 ">87h.\n"
118 + "- CAAD Transfer Control data do not match CAAD record "
119 + "indicated by PAR6–80h",
120 typeid(IncorrectInputDataException))},
121 {0x6A83,
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 "
126 + "record empty",
127 typeid(DataAccessException))},
128 {0x6A87,
129 std::make_shared<StatusProperties>(
130 std::string("Lc inconsistent with P1 or P2:\n")
131 + "- Plain data and CAAD Transfer Control data present "
132 "(Lc=50h).\n"
133 + "- Not a work key and CAAD Transfer Control data "
134 "present (Lc=50h).",
135 typeid(IncorrectInputDataException))}});
136 return m;
137 }();
138
139CommandWriteKey::CommandWriteKey(
140 std::shared_ptr<CommandContextDto> context,
141 SystemKeyType systemKeyType,
142 std::uint8_t kvc,
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))
149, mSourceKeyKvc(kvc)
150, mKeyParameters(keyParameters)
151, mIsTransferredObjectDiversified(isTransferredKeyDiversified)
152{
153}
154
155CommandWriteKey::CommandWriteKey(
156 std::shared_ptr<CommandContextDto> context,
157 std::uint8_t kif,
158 std::uint8_t kvc,
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)
168, mSourceKeyKif(kif)
169, mSourceKeyKvc(kvc)
170, mKeyParameters(keyParameters)
171, mIsTransferredObjectDiversified(isTransferredKeyDiversified)
172{
173}
174
175CommandWriteKey::CommandWriteKey(
176 std::shared_ptr<CommandContextDto> context,
177 std::uint8_t kif,
178 std::uint8_t kvc,
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)
188, mSourceKeyKif(kif)
189, mSourceKeyKvc(kvc)
190, mKeyParameters(keyParameters)
191, mIsTransferredObjectDiversified(true)
192, mArbitraryDiversifier(diversifier)
193{
194}
195
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)
207{
208 std::vector<std::uint8_t> lockParametersAsKeyParameters(
209 LegacySamConstants::KEY_PARAMETERS_LENGTH);
210 lockParametersAsKeyParameters[5] = lockParameters;
211 mKeyParameters = lockParametersAsKeyParameters;
212}
213
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)
220, mSourceKeyKif(0)
221, mSourceKeyKvc(0)
222, mKeyParameters({0})
223, mIsTransferredObjectDiversified(false)
224, mCipheringKeyKvc(0)
225, mWriteKeyCommandData(plainLockDataBlock)
226{
227}
228
229void
230CommandWriteKey::finalizeRequest()
231{
232 std::uint8_t p1;
233 if (mWriteKeyCommandData.empty()) {
234 mCipheringKeyKvc = getContext()
235 ->getTargetSam()
236 ->getSystemKeyParameter(mCipheringKeyType)
237 ->getKvc();
238
239 /* It is a key transfer */
240 auto controlSamContext = std::make_shared<CommandContextDto>(
241 getContext()->getControlSam(), nullptr, nullptr);
242 std::vector<std::uint8_t> diversifier(8);
243 System::arraycopy(
244 getContext()->getTargetSam()->getSerialNumber(),
245 0,
246 diversifier,
247 4,
248 4);
249 addControlSamCommand(
250 std::make_shared<CommandSelectDiversifier>(
251 controlSamContext, diversifier));
252 addControlSamCommand(
253 std::make_shared<CommandGiveRandom>(
254 controlSamContext,
255 getContext()->getTargetSam()->popChallenge()));
256
257 auto commandSamGenerateKey = std::make_shared<CommandSamGenerateKey>(
258 controlSamContext,
259 mTargetKeyReference,
260 mCipheringKeyKvc,
261 mSourceKeyKif,
262 mSourceKeyKvc,
263 mKeyParameters,
264 mIsTransferredObjectDiversified,
265 mArbitraryDiversifier);
266 addControlSamCommand(commandSamGenerateKey);
267 processControlSamCommand();
268
269 mWriteKeyCommandData = commandSamGenerateKey->getCipheredData();
270 p1 = 0x00;
271 } else {
272 p1 = (0x80); // plain data mode
273 }
274
275 const std::uint8_t cla = getContext()->getTargetSam()->getClassByte();
276 const std::uint8_t ins = CommandRef::WRITE_KEY.getInstructionByte();
277
278 setApduRequest(
279 std::make_shared<ApduRequestAdapter>(ApduUtil::build(
280 cla, ins, p1, mTargetKeyReference, mWriteKeyCommandData)));
281}
282
283bool
284CommandWriteKey::isControlSamRequiredToFinalizeRequest() const
285{
286 return true;
287}
288
289void
290CommandWriteKey::parseResponse(std::shared_ptr<ApduResponseApi> apduResponse)
291{
292 setResponseAndCheckStatus(apduResponse);
293}
294
295const std::map<int, const std::shared_ptr<StatusProperties>>&
296CommandWriteKey::getStatusTable() const
297{
298 return STATUS_TABLE;
299}
300
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)
306{
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;
311 System::arraycopy(
312 lockValue, 0, lockFile, 13, LegacySamConstants::LOCK_VALUE_LENGTH);
313
314 std::vector<std::uint8_t> plainDataBlock(
315 LegacySamConstants::KEY_DATA_BLOCK_SIZE);
316 System::arraycopy(
317 lockFile, 0, plainDataBlock, 8, LegacySamConstants::LOCK_FILE_SIZE);
318 plainDataBlock[37] = LegacySamConstants::TARGET_IS_LOCK_FILE;
319 plainDataBlock[45] = 0x80;
320
321 return plainDataBlock;
322}
323
324} /* namespace legacysam */
325} /* namespace crypto */
326} /* namespace calypso */
327} /* namespace card */
328} /* namespace keyple */
DtoAdapters::CommandContextDto CommandContextDto