keyple-card-calypso-crypto-legacysam-cpp-lib - 1.0.2
Component of the Keyple C++ middleware
LegacySamAdapter.cpp
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (c) 2025 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/LegacySamAdapter.hpp"
15
16#include <map>
17#include <memory>
18#include <regex>
19#include <string>
20#include <utility>
21#include <vector>
22
23#include "keyple/core/util/HexUtil.hpp"
24#include "keyple/core/util/cpp/exception/Exception.hpp"
25
26namespace keyple {
27namespace card {
28namespace calypso {
29namespace crypto {
30namespace legacysam {
31
32using keyple::core::util::HexUtil;
33using keyple::core::util::cpp::exception::Exception;
34
35LegacySamAdapter::LegacySamAdapter(ProductType productType)
36: mSamProductType(productType)
37, mClassByte(computeClassByte(productType))
38{
39}
40
41LegacySamAdapter::LegacySamAdapter(
42 std::shared_ptr<CardSelectionResponseApi> cardSelectionResponse)
43{
44 parseSelectionResponse(cardSelectionResponse);
45}
46
47void
48LegacySamAdapter::parseSelectionResponse(
49 std::shared_ptr<CardSelectionResponseApi> cardSelectionResponse)
50{
51 mPowerOnData = cardSelectionResponse->getPowerOnData();
52 if (mPowerOnData.empty()) {
53 throw std::invalid_argument("ATR should not be empty");
54 }
55
56 mSerialNumber.resize(4);
57
58 std::regex extractRegex("3B(.{6}|.{10})805A(.{20})829000");
59 std::smatch matcher;
60
61 if (std::regex_search(mPowerOnData, matcher, extractRegex)) {
62 std::vector<uint8_t> atrSubElements = HexUtil::toByteArray(matcher[2]);
63 mPlatform = atrSubElements[0];
64 mApplicationType = atrSubElements[1];
65 mApplicationSubType = atrSubElements[2];
66 mSoftwareIssuer = atrSubElements[3];
67 mSoftwareVersion = atrSubElements[4];
68 mSoftwareRevision = atrSubElements[5];
69
70 switch (mApplicationSubType) {
71 case 0xC1:
72 mSamProductType = mSoftwareIssuer == 0x08 ? ProductType::HSM_C1
73 : ProductType::SAM_C1;
74 break;
75 case 0xD0:
76 case 0xD1:
77 case 0xD2:
78 case 0xD3:
79 case 0xD4:
80 case 0xD5:
81 case 0xD6:
82 case 0xD7:
83 mSamProductType = ProductType::SAM_S1DX;
84 break;
85 case 0xE1:
86 mSamProductType = ProductType::SAM_S1E1;
87 break;
88 default:
89 mSamProductType = ProductType::UNKNOWN;
90 break;
91 }
92
93 std::copy(
94 atrSubElements.begin() + 6,
95 atrSubElements.begin() + 10,
96 mSerialNumber.begin());
97
98 } else {
99 mSamProductType = ProductType::UNKNOWN;
100 mPlatform = 0;
101 mApplicationType = 0;
102 mApplicationSubType = 0;
103 mSoftwareIssuer = 0;
104 mSoftwareVersion = 0;
105 mSoftwareRevision = 0;
106 }
107
108 mClassByte = computeClassByte(mSamProductType);
109}
110
111uint8_t
112LegacySamAdapter::getClassByte() const
113{
114 return mClassByte;
115}
116
117int
118LegacySamAdapter::getMaxDigestDataLength() const
119{
120 switch (mSamProductType) {
121 case ProductType::SAM_C1:
122 case ProductType::HSM_C1:
123 return 255;
124 case ProductType::SAM_S1DX:
125 return 70;
126 case ProductType::SAM_S1E1:
127 return 240;
128 default:
129 return 0;
130 }
131}
132
133const std::string&
134LegacySamAdapter::getPowerOnData() const
135{
136 return mPowerOnData;
137}
138
139ProductType
140LegacySamAdapter::getProductType() const
141{
142 return mSamProductType;
143}
144
145std::string
146LegacySamAdapter::getProductInfo() const
147{
148 return "Type: " + std::to_string(static_cast<int>(mSamProductType))
149 + ", S/N: " + HexUtil::toHex(getSerialNumber());
150}
151
152const std::vector<uint8_t>&
153LegacySamAdapter::getSerialNumber() const
154{
155 return mSerialNumber;
156}
157
158uint8_t
159LegacySamAdapter::getPlatform() const
160{
161 return mPlatform;
162}
163
164uint8_t
165LegacySamAdapter::getApplicationType() const
166{
167 return mApplicationType;
168}
169
170uint8_t
171LegacySamAdapter::getApplicationSubType() const
172{
173 return mApplicationSubType;
174}
175
176uint8_t
177LegacySamAdapter::getSoftwareIssuer() const
178{
179 return mSoftwareIssuer;
180}
181
182uint8_t
183LegacySamAdapter::getSoftwareVersion() const
184{
185 return mSoftwareVersion;
186}
187
188uint8_t
189LegacySamAdapter::getSoftwareRevision() const
190{
191 return mSoftwareRevision;
192}
193
194void
195LegacySamAdapter::putCounterValue(int counterNumber, int value)
196{
197 mCounters[counterNumber] = value;
198}
199
200void
201LegacySamAdapter::putCounterCeilingValue(int counterNumber, int value)
202{
203 mCounterCeilings[counterNumber] = value;
204}
205
206void
207LegacySamAdapter::putCounterIncrementConfiguration(
208 int counterNumber, CounterIncrementAccess counterIncrementAccess)
209{
210 mCountersIncrementConfig[counterNumber] = counterIncrementAccess;
211}
212
213std::shared_ptr<int>
214LegacySamAdapter::getCounter(const int counterNumber) const
215{
216 auto it = mCounters.find(counterNumber);
217 if (it != mCounters.end()) {
218 return std::make_shared<int>(it->second);
219 }
220
221 return nullptr;
222}
223
224const std::map<const int, int>&
225LegacySamAdapter::getCounters() const
226{
227 return mCounters;
228}
229
230CounterIncrementAccess
231LegacySamAdapter::getCounterIncrementAccess(const int counterNumber) const
232{
233 auto it = mCountersIncrementConfig.find(counterNumber);
234 if (it != mCountersIncrementConfig.end()) {
235 return it->second;
236 }
237
238 throw Exception("counterNumber not found");
239}
240
241std::shared_ptr<int>
242LegacySamAdapter::getCounterCeiling(int counterNumber) const
243{
244 auto it = mCounterCeilings.find(counterNumber);
245 if (it != mCounterCeilings.end()) {
246 return std::make_shared<int>(it->second);
247 }
248 return nullptr;
249}
250
251const std::map<const int, int>&
252LegacySamAdapter::getCounterCeilings() const
253{
254 return mCounterCeilings;
255}
256
257const std::vector<uint8_t>&
258LegacySamAdapter::getCaCertificate() const
259{
260 return mCaCertificate;
261}
262
263std::shared_ptr<SamParameters>
264LegacySamAdapter::getSamParameters() const
265{
266 return mSamParameters;
267}
268
269void
270LegacySamAdapter::setSamParameters(
271 std::shared_ptr<SamParametersAdapter> samParameters)
272{
273 mSamParameters = samParameters;
274}
275
276void
277LegacySamAdapter::setSystemKeyParameter(
278 SystemKeyType systemKeyType,
279 std::shared_ptr<KeyParameterAdapter> keyParameter)
280{
281 mSystemKeyParameterMap[systemKeyType] = keyParameter;
282}
283
284std::shared_ptr<KeyParameter>
285LegacySamAdapter::getSystemKeyParameter(SystemKeyType systemKeyType) const
286{
287 auto it = mSystemKeyParameterMap.find(systemKeyType);
288 if (it != mSystemKeyParameterMap.end()) {
289 return it->second;
290 }
291 return nullptr;
292}
293
294void
295LegacySamAdapter::setWorkKeyParameter(
296 int recordNumber, std::shared_ptr<KeyParameterAdapter> keyParameter)
297{
298 mWorkKeyParameterByRecordNumberMap[recordNumber] = keyParameter;
299}
300
301void
302LegacySamAdapter::setWorkKeyParameter(
303 uint16_t kifKvc, std::shared_ptr<KeyParameterAdapter> keyParameter)
304{
305 mWorkKeyParameterByKifKvcMap[kifKvc] = keyParameter;
306}
307
308std::shared_ptr<KeyParameter>
309LegacySamAdapter::getWorkKeyParameter(int recordNumber) const
310{
311 auto it = mWorkKeyParameterByRecordNumberMap.find(recordNumber);
312 if (it != mWorkKeyParameterByRecordNumberMap.end()) {
313 return it->second;
314 }
315 return nullptr;
316}
317
318std::shared_ptr<KeyParameter>
319LegacySamAdapter::getWorkKeyParameter(uint8_t kif, uint8_t kvc) const
320{
321 uint16_t kifKvc = (static_cast<uint16_t>(kif) << 8) | kvc;
322 auto it = mWorkKeyParameterByKifKvcMap.find(kifKvc);
323 if (it != mWorkKeyParameterByKifKvcMap.end()) {
324 return it->second;
325 }
326 return nullptr;
327}
328
329std::string
330LegacySamAdapter::toString() const
331{
332 // This is a placeholder implementation.
333 // A proper implementation would require a JSON library.
334 return "{}";
335}
336
337void
338LegacySamAdapter::setChallenge(std::vector<uint8_t> challenge)
339{
340 mChallenge = std::move(challenge);
341}
342
343std::vector<uint8_t>
344LegacySamAdapter::popChallenge()
345{
346 std::vector<uint8_t> res = mChallenge;
347 mChallenge.clear();
348 return res;
349}
350
351void
352LegacySamAdapter::setCaCertificate(std::vector<uint8_t> caCertificate)
353{
354 mCaCertificate = std::move(caCertificate);
355}
356
357uint8_t
358LegacySamAdapter::computeClassByte(ProductType productType)
359{
360 return productType == ProductType::SAM_S1DX ? 0x94 : 0x80;
361}
362
363} /* namespace legacysam */
364} /* namespace crypto */
365} /* namespace calypso */
366} /* namespace card */
367} /* namespace keyple */