Keyple Card Calypso C++ Library 2.2.5.6
Reference Terminal Reader API for C++
CalypsoSamAdapter.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 information regarding *
5 * copyright ownership. *
6 * *
7 * This program and the accompanying materials are made available under the terms of the Eclipse *
8 * Public License 2.0 which is available at http://www.eclipse.org/legal/epl-2.0 *
9 * *
10 * SPDX-License-Identifier: EPL-2.0 *
11 ***********************************"***************************************************************/
12
13#include "CalypsoSamAdapter.h"
14
15#include <sstream>
16
17/* Keyple Core Util */
18#include "HexUtil.h"
19#include "IllegalStateException.h"
20#include "Pattern.h"
21#include "System.h"
22
23namespace keyple {
24namespace card {
25namespace calypso {
26
27using namespace keyple::core::util;
28using namespace keyple::core::util::cpp;
29using namespace keyple::core::util::cpp::exception;
30
32 const std::shared_ptr<CardSelectionResponseApi> cardSelectionResponse)
33{
34 /* In the case of a SAM, the power-on data corresponds to the ATR of the card */
35 mPowerOnData = cardSelectionResponse->getPowerOnData();
36 if (mPowerOnData.empty()) {
37 throw IllegalStateException("ATR should not be empty.");
38 }
39
40 mSerialNumber = std::vector<uint8_t>(4);
41
42 /*
43 * Extract the historical bytes from T3 to T12
44 * CL-SAM-ATR.1
45 */
46 const std::string extractRegex = "3B(.{6}|.{10})805A(.{20})829000";
47 std::unique_ptr<Pattern> pattern = Pattern::compile(extractRegex);
48
49 /* To use */
50 std::unique_ptr<Matcher> matcher = pattern->matcher(mPowerOnData);
51 if (matcher->find(0)) {
52 const std::vector<uint8_t> atrSubElements = HexUtil::toByteArray(matcher->group(2));
53 mPlatform = atrSubElements[0];
54 mApplicationType = atrSubElements[1];
55 mApplicationSubType = atrSubElements[2];
56 mSoftwareIssuer = atrSubElements[3];
57 mSoftwareVersion = atrSubElements[4];
58 mSoftwareRevision = atrSubElements[5];
59
60 /* Determine SAM product type from Application Subtype */
61 switch (mApplicationSubType) {
62 case 0xC1:
63 mSamProductType = mSoftwareIssuer == 0x08 ? ProductType::HSM_C1 : ProductType::SAM_C1;
64 break;
65 case 0xD0:
66 case 0xD1:
67 case 0xD2:
68 mSamProductType = ProductType::SAM_S1DX;
69 break;
70 case 0xE1:
71 mSamProductType = ProductType::SAM_S1E1;
72 break;
73 default:
74 mSamProductType = ProductType::UNKNOWN;
75 break;
76 }
77
78
79 System::arraycopy(atrSubElements, 6, mSerialNumber, 0, 4);
80
81 std::stringstream ss;
82 ss << "SAM " << mSamProductType << ", "
83 << "PLATFORM = " << mPlatform << ", "
84 << "APPTYPE = " << mApplicationType << "h, "
85 << "APPSUBTYPE = " << mApplicationSubType << "h, "
86 << "SWISSUER = " << mSoftwareIssuer << "h, "
87 << "SWVERSION = " << mSoftwareVersion << "h, "
88 << "SWREVISION = " << mSoftwareRevision;
89 mLogger->trace("%\n", ss.str());
90 mLogger->trace("SAM SERIALNUMBER = %\n", HexUtil::toHex(mSerialNumber));
91
92 } else {
93
94 mSamProductType = ProductType::UNKNOWN;
95 mPlatform = 0;
96 mApplicationType = 0;
97 mApplicationSubType = 0;
98 mSoftwareIssuer = 0;
99 mSoftwareVersion = 0;
100 mSoftwareRevision = 0;
101 }
102}
103
105{
106 /* CL-CLA-SAM.1 */
107 if (type == CalypsoSam::ProductType::SAM_S1DX ||
108 type == CalypsoSam::ProductType::CSAM_F) {
109 return 0x94;
110 }
111
112 return 0x80;
113}
114
116{
117 return getClassByte(mSamProductType);
118}
119
121{
122 switch (mSamProductType) {
123 case CalypsoSam::ProductType::SAM_C1:
124 case CalypsoSam::ProductType::HSM_C1:
125 return 255;
126 case CalypsoSam::ProductType::SAM_S1DX:
127 return 70;
128 case CalypsoSam::ProductType::SAM_S1E1:
129 return 240;
130 case CalypsoSam::ProductType::CSAM_F:
131 return 247;
132 default:
133 return 0;
134 }
135}
136
137const std::vector<uint8_t> CalypsoSamAdapter::getSelectApplicationResponse() const
138{
139 return std::vector<uint8_t>(0);
140}
141
142const std::string& CalypsoSamAdapter::getPowerOnData() const
143{
144 return mPowerOnData;
145}
146
148{
149 return mSamProductType;
150}
151
152const std::string CalypsoSamAdapter::getProductInfo() const
153{
154 std::stringstream ss;
155 ss << "Type: " << getProductType() << ", S/N: " << HexUtil::toHex(getSerialNumber());
156
157 return ss.str();
158}
159
160const std::vector<uint8_t>& CalypsoSamAdapter::getSerialNumber() const
161{
162 return mSerialNumber;
163}
164
166{
167 return mPlatform;
168}
169
171{
172 return mApplicationType;
173}
174
176{
177 return mApplicationSubType;
178}
179
181{
182 return mSoftwareIssuer;
183}
184
186{
187 return mSoftwareVersion;
188}
189
191{
192 return mSoftwareRevision;
193}
194
195void CalypsoSamAdapter::putEventCounter(const int eventCounterNumber, const int eventCounterValue)
196{
197 mEventCounters.insert({eventCounterNumber, eventCounterValue});
198}
199
200void CalypsoSamAdapter::putEventCeiling(const int eventCeilingNumber, const int eventCeilingValue)
201{
202 mEventCeilings.insert({eventCeilingNumber, eventCeilingValue});
203}
204
205// std::shared_ptr<int> CalypsoSamAdapter::getEventCounter(const int eventCounterNumber) const
206// {
207// const auto it = mEventCounters.find(eventCounterNumber);
208// if (it != mEventCounters.end()) {
209// return std::make_shared<int>(it->second);
210// } else {
211// return nullptr;
212// }
213// }
214
215// const std::map<int, int>& CalypsoSamAdapter::getEventCounters() const
216// {
217// return mEventCounters;
218// }
219
220// std::shared_ptr<int> CalypsoSamAdapter::getEventCeiling(const int eventCeilingNumber) const
221// {
222// const auto it = mEventCeilings.find(eventCeilingNumber);
223// if (it != mEventCeilings.end()) {
224// return std::make_shared<int>(it->second);
225// } else {
226// return nullptr;
227// }
228// }
229
230// const std::map<int, int>& CalypsoSamAdapter::getEventCeilings() const
231// {
232// return mEventCeilings;
233// }
234
235}
236}
237}
const std::vector< uint8_t > & getSerialNumber() const override
void putEventCounter(const int eventCounterNumber, const int eventCounterValue)
const std::string & getPowerOnData() const override
CalypsoSamAdapter(const std::shared_ptr< CardSelectionResponseApi > cardSelectionResponse)
const std::vector< uint8_t > getSelectApplicationResponse() const override
const std::string getProductInfo() const override
void putEventCeiling(const int eventCeilingNumber, const int eventCeilingValue)
CalypsoSam::ProductType getProductType() const override
CalypsoSam::ProductType ProductType