Keyple Card Calypso C++ Library 2.2.5.6
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
41CmdSamReadKeyParameters::CmdSamReadKeyParameters(const std::shared_ptr<CalypsoSamAdapter> calypsoSam)
42: AbstractSamCommand(mCommand, -1, calypsoSam)
43{
44 const uint8_t cla = SamUtilAdapter::getClassByte(calypsoSam->getProductType());
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 std::shared_ptr<CalypsoSamAdapter> calypsoSam,
56 const uint8_t kif)
57: AbstractSamCommand(mCommand, -1, calypsoSam)
58{
59 const uint8_t cla = SamUtilAdapter::getClassByte(calypsoSam->getProductType());
60
61 const uint8_t p2 = 0xC0;
62 std::vector<uint8_t> sourceKeyId = {0x00, 0x00};
63
64 sourceKeyId[0] = kif;
65
67 std::make_shared<ApduRequestAdapter>(
68 ApduUtil::build(cla, mCommand.getInstructionByte(), 0x00, p2, sourceKeyId, 0x00)));
69}
70
72 const std::shared_ptr<CalypsoSamAdapter> calypsoSam,
73 const uint8_t kif,
74 const uint8_t kvc)
75: AbstractSamCommand(mCommand, -1, calypsoSam)
76{
77 const uint8_t cla = SamUtilAdapter::getClassByte(calypsoSam->getProductType());
78
79 const uint8_t p2 = 0xF0;
80 std::vector<uint8_t> sourceKeyId = {0x00, 0x00};
81
82 sourceKeyId[0] = kif;
83 sourceKeyId[1] = kvc;
84
86 std::make_shared<ApduRequestAdapter>(
87 ApduUtil::build(cla, mCommand.getInstructionByte(), 0x00, p2, sourceKeyId, 0x00)));
88}
89
91 const std::shared_ptr<CalypsoSamAdapter> calypsoSam,
92 const SourceRef sourceKeyRef,
93 const int recordNumber)
94: AbstractSamCommand(mCommand, -1, calypsoSam)
95{
96 if (recordNumber < 1 || recordNumber > MAX_WORK_KEY_REC_NUMB) {
97
98 throw IllegalArgumentException("Record Number must be between 1 and " +
99 std::to_string(MAX_WORK_KEY_REC_NUMB) +
100 ".");
101 }
102
103 const uint8_t cla = SamUtilAdapter::getClassByte(calypsoSam->getProductType());
104
105 uint8_t p2;
106 std::vector<uint8_t> sourceKeyId = {0x00, 0x00};
107
108 switch (sourceKeyRef) {
109
111 p2 = static_cast<uint8_t>(recordNumber);
112 break;
113
115 p2 = static_cast<uint8_t>(0xC0 + recordNumber);
116 break;
117
118 default:
119 std::stringstream ss;
120 ss << sourceKeyRef;
121 throw IllegalStateException("Unsupported SourceRef parameter " +
122 ss.str());
123 }
124
126 std::make_shared<ApduRequestAdapter>(
127 ApduUtil::build(cla, mCommand.getInstructionByte(), 0x00, p2, sourceKeyId, 0x00)));
128}
129
131 const std::shared_ptr<CalypsoSamAdapter> calypsoSam,
132 const uint8_t kif,
133 const NavControl navControl)
134: AbstractSamCommand(mCommand, -1, calypsoSam)
135{
136 const uint8_t cla = SamUtilAdapter::getClassByte(calypsoSam->getProductType());
137
138 uint8_t p2;
139 std::vector<uint8_t> sourceKeyId = {0x00, 0x00};
140
141 switch (navControl) {
142
144 p2 = 0xF8;
145 break;
146
147 case NavControl::NEXT:
148 p2 = 0xFA;
149 break;
150
151 default:
152 std::stringstream ss;
153 ss << navControl;
154 throw IllegalStateException("Unsupported NavControl parameter " +
155 ss.str());
156 }
157
158 sourceKeyId[0] = kif;
159
161 std::make_shared<ApduRequestAdapter>(
162 ApduUtil::build(cla, mCommand.getInstructionByte(), 0x00, p2, sourceKeyId, 0x00)));
163}
164
165const std::vector<uint8_t> CmdSamReadKeyParameters::getKeyParameters() const
166{
167 return isSuccessful() ? getApduResponse()->getDataOut() : std::vector<uint8_t>();
168}
169
170const std::map<const int, const std::shared_ptr<StatusProperties>>
171 CmdSamReadKeyParameters::initStatusTable()
172{
173 std::map<const int, const std::shared_ptr<StatusProperties>> m =
175
176 m.insert({0x6700,
177 std::make_shared<StatusProperties>("Incorrect Lc.",
179 m.insert({0x6900,
180 std::make_shared<StatusProperties>("An event counter cannot be incremented.",
181 typeid(CalypsoSamCounterOverflowException))});
182 m.insert({0x6A00,
183 std::make_shared<StatusProperties>("Incorrect P2.",
184 typeid(CalypsoSamIllegalParameterException))});
185 m.insert({0x6A83,
186 std::make_shared<StatusProperties>("Record not found: key to read not found.",
187 typeid(CalypsoSamDataAccessException))});
188 m.insert({0x6200,
189 std::make_shared<StatusProperties>("Correct execution with warning: data not signed.",
190 typeid(nullptr))});
191
192 return m;
193}
194
195const std::map<const int, const std::shared_ptr<StatusProperties>>&
197{
198 return STATUS_TABLE;
199}
200
201/* SOURCE REF ------------------------------------------------------------------------------------*/
202
203std::ostream& operator<<(std::ostream& os, const CmdSamReadKeyParameters::SourceRef& sr)
204{
205 os << "SOURCE_REF = ";
206
207 switch (sr) {
209 os << "WORK_KEY";
210 break;
212 os << "SYSTEM_KEY";
213 break;
214 default:
215 os << "UNKONWN";
216 break;
217 }
218
219 return os;
220}
221
222/* NAV CONTROL ---------------------------------------------------------------------------------- */
223
224std::ostream& operator<<(std::ostream& os, const CmdSamReadKeyParameters::NavControl& nc)
225{
226 os << "NAV_CONTROL = ";
227
228 switch (nc) {
230 os << "FIRST";
231 break;
233 os << "NEXT";
234 break;
235 default:
236 os << "UNKONWN";
237 break;
238 }
239
240 return os;
241}
242
243}
244}
245}
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 std::shared_ptr< CalypsoSamAdapter > calypsoSam)
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)
std::ostream & operator<<(std::ostream &os, const std::shared_ptr< ApduRequestAdapter > ara)