keyple-card-calypso-crypto-legacysam-cpp-lib - 1.0.2
Component of the Keyple C++ middleware
CommandWriteCeilings.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/CommandWriteCeilings.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/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"
36
37namespace keyple {
38namespace card {
39namespace calypso {
40namespace crypto {
41namespace legacysam {
42
43using keyple::core::util::ApduUtil;
44using keyple::core::util::ByteArrayUtil;
45
47
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);
52
53 m.insert(
54 {{0x6700,
55 std::make_shared<StatusProperties>(
56 "Incorrect Lc", typeid(IllegalParameterException))},
57 {0x6985,
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))},
66 {0x6900,
67 std::make_shared<StatusProperties>(
68 "An event counter cannot be incremented",
69 typeid(CounterOverflowException))},
70 {0x6988,
71 std::make_shared<StatusProperties>(
72 "Incorrect signature", typeid(SecurityDataException))},
73 {0x6A00,
74 std::make_shared<StatusProperties>(
75 "Incorrect P2", typeid(IllegalParameterException))},
76 {0x6A80,
77 std::make_shared<StatusProperties>(
78 "Incorrect decrypted data, including:\n"
79 "- Full ceiling record update (P2!=B8h) with DES/DESX key44 "
80 "and at "
81 "least one ceiling is increased by more than 131,071.\n"
82 "- One ceiling update (P2=B8h): reference>26, or the 25 last "
83 "data "
84 "bytes are not all null",
85 typeid(IncorrectInputDataException))},
86 {0x6A83,
87 std::make_shared<StatusProperties>(
88 "Record not found: deciphering key not found",
89 typeid(DataAccessException))}});
90
91 return m;
92 }();
93
94CommandWriteCeilings::CommandWriteCeilings(
95 std::shared_ptr<DtoAdapters::CommandContextDto> context,
96 std::shared_ptr<DtoAdapters::TargetSamContextDto> targetSamContext,
97 int counterNumber,
98 int ceilingValue)
99: Command(
100 CommandRef::WRITE_CEILINGS,
101 LegacySamConstants::PLAIN_CEILING_DATA_BLOCK_SIZE,
102 context)
103, mTargetSamContext(targetSamContext)
104, mPlainData(LegacySamConstants::PLAIN_CEILING_DATA_BLOCK_SIZE)
105, mCounterFileRecordNumber(-1)
106{
107 /*
108 * Build the plain data block to be ciphered later.
109 * If targetSamContext is null (dynamic mode), get the KVC from the
110 * LegacySam.
111 */
112 mPlainData[0] = (mTargetSamContext == nullptr)
113 ? getContext()
114 ->getTargetSam()
115 ->getSystemKeyParameter(SystemKeyType::RELOADING)
116 ->getKvc()
117 : mTargetSamContext->getSystemKeyTypeToKvcMap()
118 .find(SystemKeyType::RELOADING)
119 ->second;
120 mPlainData[1] = static_cast<uint8_t>(counterNumber);
121 ByteArrayUtil::copyBytes(ceilingValue, mPlainData, 2, 3);
122}
123
124CommandWriteCeilings::CommandWriteCeilings(
125 std::shared_ptr<DtoAdapters::CommandContextDto> context,
126 int counterNumber,
127 int ceilingValue)
128: CommandWriteCeilings(context, nullptr, counterNumber, ceilingValue)
129{
130}
131
132CommandWriteCeilings::CommandWriteCeilings(
133 std::shared_ptr<DtoAdapters::CommandContextDto> context,
134 std::shared_ptr<DtoAdapters::TargetSamContextDto> targetSamContext,
135 int counterNumber,
136 int ceilingValue,
137 CounterIncrementAccess counterIncrementAccess)
138: Command(
139 CommandRef::WRITE_CEILINGS,
140 LegacySamConstants::PLAIN_CEILING_DATA_BLOCK_SIZE,
141 context)
142, mTargetSamContext(targetSamContext)
143, mPlainData(LegacySamConstants::PLAIN_CEILING_DATA_BLOCK_SIZE)
144, mCounterFileRecordNumber(
145 LegacySamConstants::COUNTER_TO_RECORD_LOOKUP[counterNumber])
146{
147 /*
148 * If targetSamContext is null (dynamic mode), get the KVC from the
149 * LegacySam.
150 */
151 mPlainData[0] = (mTargetSamContext == nullptr)
152 ? getContext()
153 ->getTargetSam()
154 ->getSystemKeyParameter(SystemKeyType::RELOADING)
155 ->getKvc()
156 : mTargetSamContext->getSystemKeyTypeToKvcMap()
157 .find(SystemKeyType::RELOADING)
158 ->second;
159
160 addCounter(counterNumber, ceilingValue, counterIncrementAccess);
161}
162
163CommandWriteCeilings::CommandWriteCeilings(
164 std::shared_ptr<DtoAdapters::CommandContextDto> context,
165 int counterNumber,
166 int ceilingValue,
167 CounterIncrementAccess counterIncrementAccess)
168: CommandWriteCeilings(
169 context, nullptr, counterNumber, ceilingValue, counterIncrementAccess)
170{
171}
172
173void
174CommandWriteCeilings::addCounter(
175 int counterNumber,
176 int ceilingValue,
177 CounterIncrementAccess counterIncrementAccess)
178{
179 /* Update the plain data block to be ciphered later. */
180 ByteArrayUtil::copyBytes(
181 ceilingValue, mPlainData, (counterNumber % 9) * 3 + 1, 3);
182
183 /* Keep the config into a map. */
184 mCounterNumberToManualCounterIncrementAuthorizedMap[counterNumber % 9]
185 = counterIncrementAccess;
186}
187
188int
189CommandWriteCeilings::getCounterFileRecordNumber() const
190{
191 return mCounterFileRecordNumber;
192}
193
194const std::map<int, const std::shared_ptr<StatusProperties>>&
195CommandWriteCeilings::getStatusTable() const
196{
197 return STATUS_TABLE;
198}
199
200void
201CommandWriteCeilings::finalizeRequest()
202{
203 auto controlSamContext = std::make_shared<CommandContextDto>(
204 getContext()->getControlSam(), nullptr, nullptr);
205
206 /* Add commands. */
207 addControlSamCommand(
208 std::make_shared<CommandSelectDiversifier>(
209 controlSamContext,
210 (mTargetSamContext == nullptr)
211 ? getContext()->getTargetSam()->getSerialNumber()
212 : mTargetSamContext->getSerialNumber()));
213
214 std::vector<uint8_t> challenge
215 = (mTargetSamContext == nullptr)
216 ? getContext()->getTargetSam()->popChallenge()
217 : LegacySamUtil::computeStaticModeChallenge(
218 mTargetSamContext, SystemKeyType::RELOADING);
219
220 addControlSamCommand(
221 std::make_shared<CommandGiveRandom>(controlSamContext, challenge));
222
223 if (mCounterFileRecordNumber != -1) {
224 computePlainData();
225 }
226
227 auto commandSamDataCipher = std::make_shared<CommandSamDataCipher>(
228 controlSamContext,
229 mCounterFileRecordNumber,
230 (mCounterFileRecordNumber == -1)
231 ? CommandSamDataCipher::DataType::ONE_CEILING_VALUE
232 : CommandSamDataCipher::DataType::CEILINGS_FILE_RECORD,
233 mPlainData);
234
235 addControlSamCommand(commandSamDataCipher);
236
237 processControlSamCommand();
238
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;
244
245 setApduRequest(
246 std::make_shared<ApduRequestAdapter>(ApduUtil::build(
247 cla,
248 inst,
249 p1,
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())));
256}
257
258void
259CommandWriteCeilings::computePlainData()
260{
261 std::uint16_t configBits = 0;
262
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);
269 } else {
270 config = it->second;
271 }
272
273 if (config == CounterIncrementAccess::FREE_COUNTING_ENABLED
274 && (i != 0 || mCounterFileRecordNumber != 0)) {
275 configBits |= static_cast<std::int16_t>(1 << i);
276 }
277 }
278
279 ByteArrayUtil::copyBytes(configBits, mPlainData, 28, 2);
280}
281
282bool
283CommandWriteCeilings::isControlSamRequiredToFinalizeRequest() const
284{
285 return true;
286}
287
288void
289CommandWriteCeilings::parseResponse(
290 std::shared_ptr<ApduResponseApi> apduResponse)
291{
292 setResponseAndCheckStatus(apduResponse);
293}
294
295} /* namespace legacysam */
296} /* namespace crypto */
297} /* namespace calypso */
298} /* namespace card */
299} /* namespace keyple */
DtoAdapters::CommandContextDto CommandContextDto