Keyple Card Calypso C++ Library 2.2.2
Reference Terminal Reader API for C++
CmdSamReadKeyParameters.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
14
15#include <sstream>
16
17/* Keyple Card Calypso */
21#include "SamUtilAdapter.h"
22
23/* Keyple Core Util */
24#include "ApduUtil.h"
25#include "IllegalArgumentException.h"
26#include "IllegalStateException.h"
27
28namespace keyple {
29namespace card {
30namespace calypso {
31
32using namespace keyple::core::util;
33using namespace keyple::core::util::cpp::exception;
34
35const CalypsoSamCommand CmdSamReadKeyParameters::mCommand = CalypsoSamCommand::READ_KEY_PARAMETERS;
36const int CmdSamReadKeyParameters::MAX_WORK_KEY_REC_NUMB = 126;
37
38const std::map<const int, const std::shared_ptr<StatusProperties>>
39 CmdSamReadKeyParameters::STATUS_TABLE = initStatusTable();
40
42: AbstractSamCommand(mCommand, 0)
43{
44 const uint8_t cla = SamUtilAdapter::getClassByte(productType);
45
46 const uint8_t p2 = 0xE0;
47 const std::vector<uint8_t> sourceKeyId = {0x00, 0x00};
48
50 std::make_shared<ApduRequestAdapter>(
51 ApduUtil::build(cla, mCommand.getInstructionByte(), 0x00, p2, sourceKeyId, 0x00)));
52}
53
55 const uint8_t kif)
56: AbstractSamCommand(mCommand, 0)
57{
58 const uint8_t cla = SamUtilAdapter::getClassByte(productType);
59
60 const uint8_t p2 = 0xC0;
61 std::vector<uint8_t> sourceKeyId = {0x00, 0x00};
62
63 sourceKeyId[0] = kif;
64
66 std::make_shared<ApduRequestAdapter>(
67 ApduUtil::build(cla, mCommand.getInstructionByte(), 0x00, p2, sourceKeyId, 0x00)));
68}
69
71 const uint8_t kif,
72 const uint8_t kvc)
73: AbstractSamCommand(mCommand, 0)
74{
75 const uint8_t cla = SamUtilAdapter::getClassByte(productType);
76
77 const uint8_t p2 = 0xF0;
78 std::vector<uint8_t> sourceKeyId = {0x00, 0x00};
79
80 sourceKeyId[0] = kif;
81 sourceKeyId[1] = kvc;
82
84 std::make_shared<ApduRequestAdapter>(
85 ApduUtil::build(cla, mCommand.getInstructionByte(), 0x00, p2, sourceKeyId, 0x00)));
86}
87
89 const SourceRef sourceKeyRef,
90 const int recordNumber)
91: AbstractSamCommand(mCommand, 0)
92{
93 if (recordNumber < 1 || recordNumber > MAX_WORK_KEY_REC_NUMB) {
94 throw IllegalArgumentException("Record Number must be between 1 and " +
95 std::to_string(MAX_WORK_KEY_REC_NUMB) +
96 ".");
97 }
98
99 const uint8_t cla = SamUtilAdapter::getClassByte(productType);
100
101 uint8_t p2;
102 std::vector<uint8_t> sourceKeyId = {0x00, 0x00};
103
104 switch (sourceKeyRef) {
106 p2 = static_cast<uint8_t>(recordNumber);
107 break;
109 p2 = static_cast<uint8_t>(0xC0 + recordNumber);
110 break;
111 default:
112 std::stringstream ss;
113 ss << sourceKeyRef;
114 throw IllegalStateException("Unsupported SourceRef parameter " +
115 ss.str());
116 }
117
119 std::make_shared<ApduRequestAdapter>(
120 ApduUtil::build(cla, mCommand.getInstructionByte(), 0x00, p2, sourceKeyId, 0x00)));
121}
122
124 const uint8_t kif,
125 const NavControl navControl)
126: AbstractSamCommand(mCommand, 0)
127{
128 const uint8_t cla = SamUtilAdapter::getClassByte(productType);
129
130 uint8_t p2;
131 std::vector<uint8_t> sourceKeyId = {0x00, 0x00};
132
133 switch (navControl) {
135 p2 = 0xF8;
136 break;
137 case NavControl::NEXT:
138 p2 = 0xFA;
139 break;
140 default:
141 std::stringstream ss;
142 ss << navControl;
143 throw IllegalStateException("Unsupported NavControl parameter " +
144 ss.str());
145 }
146
147 sourceKeyId[0] = kif;
148
150 std::make_shared<ApduRequestAdapter>(
151 ApduUtil::build(cla, mCommand.getInstructionByte(), 0x00, p2, sourceKeyId, 0x00)));
152}
153
154const std::vector<uint8_t> CmdSamReadKeyParameters::getKeyParameters() const
155{
156 return isSuccessful() ? getApduResponse()->getDataOut() : std::vector<uint8_t>();
157}
158
159const std::map<const int, const std::shared_ptr<StatusProperties>>
160 CmdSamReadKeyParameters::initStatusTable()
161{
162 std::map<const int, const std::shared_ptr<StatusProperties>> m =
164
165 m.insert({0x6700,
166 std::make_shared<StatusProperties>("Incorrect Lc.",
168 m.insert({0x6900,
169 std::make_shared<StatusProperties>("An event counter cannot be incremented.",
170 typeid(CalypsoSamCounterOverflowException))});
171 m.insert({0x6A00,
172 std::make_shared<StatusProperties>("Incorrect P2.",
173 typeid(CalypsoSamIllegalParameterException))});
174 m.insert({0x6A83,
175 std::make_shared<StatusProperties>("Record not found: key to read not found.",
176 typeid(CalypsoSamDataAccessException))});
177 m.insert({0x6200,
178 std::make_shared<StatusProperties>("Correct execution with warning: data not signed.",
179 typeid(nullptr))});
180
181 return m;
182}
183
184const std::map<const int, const std::shared_ptr<StatusProperties>>&
186{
187 return STATUS_TABLE;
188}
189
190/* SOURCE REF ------------------------------------------------------------------------------------*/
191
192std::ostream& operator<<(std::ostream& os, const CmdSamReadKeyParameters::SourceRef& sr)
193{
194 os << "SOURCE_REF = ";
195
196 switch (sr) {
198 os << "WORK_KEY";
199 break;
201 os << "SYSTEM_KEY";
202 break;
203 default:
204 os << "UNKONWN";
205 break;
206 }
207
208 return os;
209}
210
211/* NAV CONTROL ---------------------------------------------------------------------------------- */
212
213std::ostream& operator<<(std::ostream& os, const CmdSamReadKeyParameters::NavControl& nc)
214{
215 os << "NAV_CONTROL = ";
216
217 switch (nc) {
219 os << "FIRST";
220 break;
222 os << "NEXT";
223 break;
224 default:
225 os << "UNKONWN";
226 break;
227 }
228
229 return os;
230}
231
232}
233}
234}
virtual const std::shared_ptr< ApduResponseApi > getApduResponse() const final
virtual void setApduRequest(const std::shared_ptr< ApduRequestAdapter > apduRequest) final
static const std::map< const int, const std::shared_ptr< StatusProperties > > STATUS_TABLE
static const CalypsoSamCommand READ_KEY_PARAMETERS
CmdSamReadKeyParameters(const CalypsoSam::ProductType productType)
const std::map< const int, const std::shared_ptr< StatusProperties > > & getStatusTable() const override
const std::vector< uint8_t > getKeyParameters() const
static uint8_t getClassByte(const ProductType productType)
CalypsoSam::ProductType ProductType
std::ostream & operator<<(std::ostream &os, const std::shared_ptr< ApduRequestAdapter > ara)