Keyple Card Calypso C++ Library 2.2.2
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 mSamProductType = ProductType::UNKNOWN;
94 mPlatform = 0;
95 mApplicationType = 0;
96 mApplicationSubType = 0;
97 mSoftwareIssuer = 0;
98 mSoftwareVersion = 0;
99 mSoftwareRevision = 0;
100 }
101}
102
104{
105 /* CL-CLA-SAM.1 */
106 if (type == CalypsoSam::ProductType::SAM_S1DX ||
107 type == CalypsoSam::ProductType::CSAM_F) {
108 return 0x94;
109 }
110
111 return 0x80;
112}
113
115{
116 return getClassByte(mSamProductType);
117}
118
120{
121 switch (mSamProductType) {
122 case CalypsoSam::ProductType::SAM_C1:
123 case CalypsoSam::ProductType::HSM_C1:
124 return 255;
125 case CalypsoSam::ProductType::SAM_S1DX:
126 return 70;
127 case CalypsoSam::ProductType::SAM_S1E1:
128 return 240;
129 case CalypsoSam::ProductType::CSAM_F:
130 return 247;
131 default:
132 return 0;
133 }
134}
135
136const std::vector<uint8_t> CalypsoSamAdapter::getSelectApplicationResponse() const
137{
138 return std::vector<uint8_t>(0);
139}
140
141const std::string& CalypsoSamAdapter::getPowerOnData() const
142{
143 return mPowerOnData;
144}
145
147{
148 return mSamProductType;
149}
150
151const std::string CalypsoSamAdapter::getProductInfo() const
152{
153 std::stringstream ss;
154 ss << "Type: " << getProductType() << ", S/N: " << HexUtil::toHex(getSerialNumber());
155
156 return ss.str();
157}
158
159const std::vector<uint8_t>& CalypsoSamAdapter::getSerialNumber() const
160{
161 return mSerialNumber;
162}
163
165{
166 return mPlatform;
167}
168
170{
171 return mApplicationType;
172}
173
175{
176 return mApplicationSubType;
177}
178
180{
181 return mSoftwareIssuer;
182}
183
185{
186 return mSoftwareVersion;
187}
188
190{
191 return mSoftwareRevision;
192}
193
194}
195}
196}
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
const std::vector< uint8_t > & getSerialNumber() const final
CalypsoSam::ProductType getProductType() const final
CalypsoSam::ProductType ProductType