keyple-card-calypso-crypto-legacysam-cpp-lib - 1.0.2
Component of the Keyple C++ middleware
SecureWriteTransactionManagerAdapter.cpp
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (c) 2023 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/SecureWriteTransactionManagerAdapter.hpp"
15
16#include <memory>
17#include <vector>
18
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"
33
34namespace keyple {
35namespace card {
36namespace calypso {
37namespace crypto {
38namespace legacysam {
39
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;
48
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)
56{
57}
58
59SecureWriteTransactionManager&
60SecureWriteTransactionManagerAdapter::prepareWriteSamParameters(
61 const std::vector<uint8_t>& parameters)
62{
63 core::util::Assert::getInstance().isEqual(
64 parameters.size(),
65 LegacySamConstants::SAM_PARAMETERS_LENGTH,
66 "parameters.length");
67
68 if (!getContext()->getTargetSam()->getSystemKeyParameter(
69 SystemKeyType::PERSONALIZATION)) {
70 addTargetSamCommand(
71 std::make_shared<CommandReadKeyParameters>(
72 getContext(), SystemKeyType::PERSONALIZATION));
73 }
74
75 addTargetSamCommand(std::make_shared<CommandGetChallenge>(getContext(), 8));
76 addTargetSamCommand(
77 std::make_shared<CommandWriteSamParameters>(getContext(), parameters));
78
79 return *this;
80}
81
82SecureWriteTransactionManager&
83SecureWriteTransactionManagerAdapter::prepareTransferSystemKey(
84 SystemKeyType systemKeyType,
85 uint8_t kvc,
86 const std::vector<uint8_t>& systemKeyParameters)
87{
88 return prepareTransferSystemKeyInternal(
89 systemKeyType, kvc, systemKeyParameters, false);
90}
91
92SecureWriteTransactionManager&
93SecureWriteTransactionManagerAdapter::prepareTransferSystemKeyDiversified(
94 SystemKeyType systemKeyType,
95 uint8_t kvc,
96 const std::vector<uint8_t>& systemKeyParameters)
97{
98 return prepareTransferSystemKeyInternal(
99 systemKeyType, kvc, systemKeyParameters, true);
100}
101
102SecureWriteTransactionManager&
103SecureWriteTransactionManagerAdapter::prepareTransferSystemKeyInternal(
104 SystemKeyType systemKeyType,
105 uint8_t kvc,
106 const std::vector<uint8_t>& systemKeyParameters,
107 bool isDiversified)
108{
109 core::util::Assert::getInstance().isEqual(
110 systemKeyParameters.size(),
111 LegacySamConstants::KEY_PARAMETERS_LENGTH,
112 "systemKeyParameters.length");
113
114 if (!getContext()->getTargetSam()->getSystemKeyParameter(
115 SystemKeyType::PERSONALIZATION)) {
116 addTargetSamCommand(
117 std::make_shared<CommandReadKeyParameters>(
118 getContext(), SystemKeyType::PERSONALIZATION));
119 }
120
121 addTargetSamCommand(std::make_shared<CommandGetChallenge>(getContext(), 8));
122
123 addTargetSamCommand(
124 std::make_shared<CommandWriteKey>(
125 getContext(),
126 systemKeyType,
127 kvc,
128 systemKeyParameters,
129 isDiversified));
130
131 return *this;
132}
133
134SecureWriteTransactionManager&
135SecureWriteTransactionManagerAdapter::prepareTransferWorkKey(
136 uint8_t kif,
137 uint8_t kvc,
138 const std::vector<uint8_t>& workKeyParameters,
139 int targetRecordNumber)
140{
141 return prepareTransferWorkKeyInternal(
142 kif, kvc, workKeyParameters, targetRecordNumber, false, {});
143}
144
145SecureWriteTransactionManager&
146SecureWriteTransactionManagerAdapter::prepareTransferWorkKeyDiversified(
147 uint8_t kif,
148 uint8_t kvc,
149 const std::vector<uint8_t>& workKeyParameters,
150 int targetRecordNumber)
151{
152 return prepareTransferWorkKeyInternal(
153 kif, kvc, workKeyParameters, targetRecordNumber, true, {});
154}
155
156SecureWriteTransactionManager&
157SecureWriteTransactionManagerAdapter::prepareTransferWorkKeyDiversified(
158 uint8_t kif,
159 uint8_t kvc,
160 const std::vector<uint8_t>& workKeyParameters,
161 int targetRecordNumber,
162 const std::vector<uint8_t>& diversifier)
163{
164 return prepareTransferWorkKeyInternal(
165 kif, kvc, workKeyParameters, targetRecordNumber, true, diversifier);
166}
167
168SecureWriteTransactionManager&
169SecureWriteTransactionManagerAdapter::prepareTransferWorkKeyInternal(
170 uint8_t kif,
171 uint8_t kvc,
172 const std::vector<uint8_t>& workKeyParameters,
173 int targetRecordNumber,
174 bool diversified,
175 const std::vector<uint8_t>& diversifier)
176{
177 core::util::Assert::getInstance()
178 .isEqual(
179 workKeyParameters.size(),
180 LegacySamConstants::KEY_PARAMETERS_LENGTH,
181 "workKeyParameters.length")
182 .isInRange(targetRecordNumber, 0, 126, "targetRecordNumber");
183
184 if (!getContext()->getTargetSam()->getSystemKeyParameter(
185 SystemKeyType::KEY_MANAGEMENT)) {
186 addTargetSamCommand(
187 std::make_shared<CommandReadKeyParameters>(
188 getContext(), SystemKeyType::KEY_MANAGEMENT));
189 }
190
191 addTargetSamCommand(std::make_shared<CommandGetChallenge>(getContext(), 8));
192
193 if (diversifier.empty()) {
194 addTargetSamCommand(
195 std::make_shared<CommandWriteKey>(
196 getContext(),
197 kif,
198 kvc,
199 targetRecordNumber,
200 workKeyParameters,
201 diversified));
202 } else {
203 addTargetSamCommand(
204 std::make_shared<CommandWriteKey>(
205 getContext(),
206 kif,
207 kvc,
208 targetRecordNumber,
209 workKeyParameters,
210 diversifier));
211 }
212
213 return *this;
214}
215
216SecureWriteTransactionManager&
217SecureWriteTransactionManagerAdapter::prepareTransferLock(
218 uint8_t lockIndex, uint8_t lockParameters)
219{
220 return prepareTransferLockInternal(lockIndex, lockParameters, false);
221}
222
223SecureWriteTransactionManager&
224SecureWriteTransactionManagerAdapter::prepareTransferLockDiversified(
225 uint8_t lockIndex, uint8_t lockParameters)
226{
227 return prepareTransferLockInternal(lockIndex, lockParameters, true);
228}
229
230SecureWriteTransactionManager&
231SecureWriteTransactionManagerAdapter::prepareTransferLockInternal(
232 uint8_t lockIndex, uint8_t lockParameters, bool isDiversified)
233{
234 if (!getContext()->getTargetSam()->getSystemKeyParameter(
235 SystemKeyType::KEY_MANAGEMENT)) {
236 addTargetSamCommand(
237 std::make_shared<CommandReadKeyParameters>(
238 getContext(), SystemKeyType::KEY_MANAGEMENT));
239 }
240
241 addTargetSamCommand(std::make_shared<CommandGetChallenge>(getContext(), 8));
242 addTargetSamCommand(
243 std::make_shared<CommandWriteKey>(
244 getContext(), lockIndex, lockParameters, isDiversified));
245
246 return *this;
247}
248
249SecureWriteTransactionManager&
250SecureWriteTransactionManagerAdapter::preparePlainWriteLock(
251 uint8_t lockIndex,
252 uint8_t lockParameters,
253 const std::vector<uint8_t>& lockValue)
254{
255 core::util::Assert::getInstance().isEqual(
256 lockValue.size(),
257 LegacySamConstants::LOCK_VALUE_LENGTH,
258 "lockValue.length");
259
260 addTargetSamCommand(
261 std::make_shared<CommandWriteKey>(
262 getContext(),
263 CommandWriteKey::buildPlainLockDataBlock(
264 lockIndex, lockParameters, lockValue)));
265
266 return *this;
267}
268
269SecureWriteTransactionManager&
270SecureWriteTransactionManagerAdapter::prepareWriteCounterCeiling(
271 int counterNumber, int ceilingValue)
272{
273 core::util::Assert::getInstance()
274 .isInRange(
275 counterNumber,
276 LegacySamConstants::MIN_COUNTER_CEILING_NUMBER,
277 LegacySamConstants::MAX_COUNTER_CEILING_NUMBER,
278 "counterNumber")
279 .isInRange(
280 ceilingValue,
281 LegacySamConstants::MIN_COUNTER_CEILING_VALUE,
282 LegacySamConstants::MAX_COUNTER_CEILING_VALUE,
283 "ceilingValue");
284
285 if (!getContext()->getTargetSam()->getSystemKeyParameter(
286 SystemKeyType::RELOADING)) {
287 addTargetSamCommand(
288 std::make_shared<CommandReadKeyParameters>(
289 getContext(), SystemKeyType::RELOADING));
290 }
291 addTargetSamCommand(std::make_shared<CommandGetChallenge>(getContext(), 8));
292 addTargetSamCommand(
293 std::make_shared<CommandWriteCeilings>(
294 getContext(), counterNumber, ceilingValue));
295
296 return *this;
297}
298
299SecureWriteTransactionManager&
300SecureWriteTransactionManagerAdapter::prepareWriteCounterConfiguration(
301 int counterNumber,
302 int ceilingValue,
303 CounterIncrementAccess counterIncrementAccess)
304{
305 core::util::Assert::getInstance()
306 .isInRange(
307 counterNumber,
308 LegacySamConstants::MIN_COUNTER_CEILING_NUMBER,
309 LegacySamConstants::MAX_COUNTER_CEILING_NUMBER,
310 "counterNumber")
311 .isInRange(
312 ceilingValue,
313 LegacySamConstants::MIN_COUNTER_CEILING_VALUE,
314 LegacySamConstants::MAX_COUNTER_CEILING_VALUE,
315 "ceilingValue");
316
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
323 [counterNumber]) {
324 writeCeilingsCommand->addCounter(
325 counterNumber, ceilingValue, counterIncrementAccess);
326
327 return *this;
328 }
329 }
330
331 if (!getContext()->getTargetSam()->getSystemKeyParameter(
332 SystemKeyType::RELOADING)) {
333 addTargetSamCommand(
334 std::make_shared<CommandReadKeyParameters>(
335 getContext(), SystemKeyType::RELOADING));
336 }
337 addTargetSamCommand(std::make_shared<CommandGetChallenge>(getContext(), 8));
338 addTargetSamCommand(
339 std::make_shared<CommandWriteCeilings>(
340 getContext(), counterNumber, ceilingValue, counterIncrementAccess));
341
342 return *this;
343}
344
345SecureWriteTransactionManager&
346SecureWriteTransactionManagerAdapter::processCommands()
347{
348 try {
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);
356 }
357}
358
359SecureWriteTransactionManager&
360SecureWriteTransactionManagerAdapter::processCommands(
361 ChannelControl channelControl)
362{
363 processTargetSamCommands(channelControl);
364 return *this;
365}
366
367} /* namespace legacysam */
368} /* namespace crypto */
369} /* namespace calypso */
370} /* namespace card */
371} /* namespace keyple */