14#include "keyple/card/calypso/crypto/legacysam/LegacySamAdapter.hpp"
23#include "keyple/core/util/HexUtil.hpp"
24#include "keyple/core/util/cpp/exception/Exception.hpp"
32using keyple::core::util::HexUtil;
33using keyple::core::util::cpp::exception::Exception;
35LegacySamAdapter::LegacySamAdapter(ProductType productType)
36: mSamProductType(productType)
37, mClassByte(computeClassByte(productType))
41LegacySamAdapter::LegacySamAdapter(
42 std::shared_ptr<CardSelectionResponseApi> cardSelectionResponse)
44 parseSelectionResponse(cardSelectionResponse);
48LegacySamAdapter::parseSelectionResponse(
49 std::shared_ptr<CardSelectionResponseApi> cardSelectionResponse)
51 mPowerOnData = cardSelectionResponse->getPowerOnData();
52 if (mPowerOnData.empty()) {
53 throw std::invalid_argument(
"ATR should not be empty");
56 mSerialNumber.resize(4);
58 std::regex extractRegex(
"3B(.{6}|.{10})805A(.{20})829000");
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];
70 switch (mApplicationSubType) {
72 mSamProductType = mSoftwareIssuer == 0x08 ? ProductType::HSM_C1
73 : ProductType::SAM_C1;
83 mSamProductType = ProductType::SAM_S1DX;
86 mSamProductType = ProductType::SAM_S1E1;
89 mSamProductType = ProductType::UNKNOWN;
94 atrSubElements.begin() + 6,
95 atrSubElements.begin() + 10,
96 mSerialNumber.begin());
99 mSamProductType = ProductType::UNKNOWN;
101 mApplicationType = 0;
102 mApplicationSubType = 0;
104 mSoftwareVersion = 0;
105 mSoftwareRevision = 0;
108 mClassByte = computeClassByte(mSamProductType);
112LegacySamAdapter::getClassByte()
const
118LegacySamAdapter::getMaxDigestDataLength()
const
120 switch (mSamProductType) {
121 case ProductType::SAM_C1:
122 case ProductType::HSM_C1:
124 case ProductType::SAM_S1DX:
126 case ProductType::SAM_S1E1:
134LegacySamAdapter::getPowerOnData()
const
140LegacySamAdapter::getProductType()
const
142 return mSamProductType;
146LegacySamAdapter::getProductInfo()
const
148 return "Type: " + std::to_string(
static_cast<int>(mSamProductType))
149 +
", S/N: " + HexUtil::toHex(getSerialNumber());
152const std::vector<uint8_t>&
153LegacySamAdapter::getSerialNumber()
const
155 return mSerialNumber;
159LegacySamAdapter::getPlatform()
const
165LegacySamAdapter::getApplicationType()
const
167 return mApplicationType;
171LegacySamAdapter::getApplicationSubType()
const
173 return mApplicationSubType;
177LegacySamAdapter::getSoftwareIssuer()
const
179 return mSoftwareIssuer;
183LegacySamAdapter::getSoftwareVersion()
const
185 return mSoftwareVersion;
189LegacySamAdapter::getSoftwareRevision()
const
191 return mSoftwareRevision;
195LegacySamAdapter::putCounterValue(
int counterNumber,
int value)
197 mCounters[counterNumber] = value;
201LegacySamAdapter::putCounterCeilingValue(
int counterNumber,
int value)
203 mCounterCeilings[counterNumber] = value;
207LegacySamAdapter::putCounterIncrementConfiguration(
208 int counterNumber, CounterIncrementAccess counterIncrementAccess)
210 mCountersIncrementConfig[counterNumber] = counterIncrementAccess;
214LegacySamAdapter::getCounter(
const int counterNumber)
const
216 auto it = mCounters.find(counterNumber);
217 if (it != mCounters.end()) {
218 return std::make_shared<int>(it->second);
224const std::map<const int, int>&
225LegacySamAdapter::getCounters()
const
230CounterIncrementAccess
231LegacySamAdapter::getCounterIncrementAccess(
const int counterNumber)
const
233 auto it = mCountersIncrementConfig.find(counterNumber);
234 if (it != mCountersIncrementConfig.end()) {
238 throw Exception(
"counterNumber not found");
242LegacySamAdapter::getCounterCeiling(
int counterNumber)
const
244 auto it = mCounterCeilings.find(counterNumber);
245 if (it != mCounterCeilings.end()) {
246 return std::make_shared<int>(it->second);
251const std::map<const int, int>&
252LegacySamAdapter::getCounterCeilings()
const
254 return mCounterCeilings;
257const std::vector<uint8_t>&
258LegacySamAdapter::getCaCertificate()
const
260 return mCaCertificate;
263std::shared_ptr<SamParameters>
264LegacySamAdapter::getSamParameters()
const
266 return mSamParameters;
270LegacySamAdapter::setSamParameters(
271 std::shared_ptr<SamParametersAdapter> samParameters)
273 mSamParameters = samParameters;
277LegacySamAdapter::setSystemKeyParameter(
278 SystemKeyType systemKeyType,
279 std::shared_ptr<KeyParameterAdapter> keyParameter)
281 mSystemKeyParameterMap[systemKeyType] = keyParameter;
284std::shared_ptr<KeyParameter>
285LegacySamAdapter::getSystemKeyParameter(SystemKeyType systemKeyType)
const
287 auto it = mSystemKeyParameterMap.find(systemKeyType);
288 if (it != mSystemKeyParameterMap.end()) {
295LegacySamAdapter::setWorkKeyParameter(
296 int recordNumber, std::shared_ptr<KeyParameterAdapter> keyParameter)
298 mWorkKeyParameterByRecordNumberMap[recordNumber] = keyParameter;
302LegacySamAdapter::setWorkKeyParameter(
303 uint16_t kifKvc, std::shared_ptr<KeyParameterAdapter> keyParameter)
305 mWorkKeyParameterByKifKvcMap[kifKvc] = keyParameter;
308std::shared_ptr<KeyParameter>
309LegacySamAdapter::getWorkKeyParameter(
int recordNumber)
const
311 auto it = mWorkKeyParameterByRecordNumberMap.find(recordNumber);
312 if (it != mWorkKeyParameterByRecordNumberMap.end()) {
318std::shared_ptr<KeyParameter>
319LegacySamAdapter::getWorkKeyParameter(uint8_t kif, uint8_t kvc)
const
321 uint16_t kifKvc = (
static_cast<uint16_t
>(kif) << 8) | kvc;
322 auto it = mWorkKeyParameterByKifKvcMap.find(kifKvc);
323 if (it != mWorkKeyParameterByKifKvcMap.end()) {
330LegacySamAdapter::toString()
const
338LegacySamAdapter::setChallenge(std::vector<uint8_t> challenge)
340 mChallenge = std::move(challenge);
344LegacySamAdapter::popChallenge()
346 std::vector<uint8_t> res = mChallenge;
352LegacySamAdapter::setCaCertificate(std::vector<uint8_t> caCertificate)
354 mCaCertificate = std::move(caCertificate);
358LegacySamAdapter::computeClassByte(ProductType productType)
360 return productType == ProductType::SAM_S1DX ? 0x94 : 0x80;