keyple-card-calypso-crypto-legacysam-cpp-lib - 1.0.2
Component of the Keyple C++ middleware
CommandReadKeyParameters.cpp
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (c) 2025 Calypso Networks Association https://calypsonet.org/ *
3 * *
4 * See the NOTICE file(s) distributed with this work for additional *
5 * information regarding copyright ownership. *
6 * *
7 * This program and the accompanying materials are made available under the *
8 * terms of the Eclipse Public License 2.0 which is available at *
9 * http://www.eclipse.org/legal/epl-2.0 *
10 * *
11 * SPDX-License-Identifier: EPL-2.0 *
12 ******************************************************************************/
13
14#include "keyple/card/calypso/crypto/legacysam/CommandReadKeyParameters.hpp"
15
16#include <map>
17#include <memory>
18#include <vector>
19
20#include "keyple/card/calypso/crypto/legacysam/CounterOverflowException.hpp"
21#include "keyple/card/calypso/crypto/legacysam/DataAccessException.hpp"
22#include "keyple/card/calypso/crypto/legacysam/IllegalParameterException.hpp"
23#include "keyple/card/calypso/crypto/legacysam/KeyParameterAdapter.hpp"
24#include "keyple/core/util/ApduUtil.hpp"
25#include "keyple/core/util/cpp/Arrays.hpp"
26#include "keyple/core/util/cpp/exception/IllegalStateException.hpp"
27
28namespace keyple {
29namespace card {
30namespace calypso {
31namespace crypto {
32namespace legacysam {
33
34using keyple::core::util::ApduUtil;
35using keyple::core::util::cpp::Arrays;
36using keyple::core::util::cpp::exception::IllegalStateException;
37
38const int CommandReadKeyParameters::SW_KEY_NOT_FOUND = 0x6A83;
39const int CommandReadKeyParameters::SW_DATA_NOT_SIGNED_WARNING = 0x6200;
40
41const std::map<int, const std::shared_ptr<StatusProperties>>
42 CommandReadKeyParameters::STATUS_TABLE = [] {
43 std::map<int, const std::shared_ptr<StatusProperties>> m(
44 Command::STATUS_TABLE);
45
46 m.insert(
47 {{0x6700,
48 std::make_shared<StatusProperties>(
49 "Incorrect Lc", typeid(IllegalParameterException))},
50 {0x6900,
51 std::make_shared<StatusProperties>(
52 "An event counter cannot be incremented",
53 typeid(CounterOverflowException))},
54 {0x6A00,
55 std::make_shared<StatusProperties>(
56 "Incorrect P2", typeid(IllegalParameterException))},
57 {0x6A83,
58 std::make_shared<StatusProperties>(
59 "Record not found: key to read not found",
60 typeid(DataAccessException))},
61 {0x6200,
62 std::make_shared<StatusProperties>(
63 "Correct execution with warning: data not signed")}});
64 return m;
65 }();
66
67CommandReadKeyParameters::CommandReadKeyParameters(
68 std::shared_ptr<DtoAdapters::CommandContextDto> context,
69 SystemKeyType systemKeyType)
70: Command(CommandRef::READ_KEY_PARAMETERS, 32, context)
71, mSystemKeyType(systemKeyType)
72, mHasKifKvc(false)
73, mRecordNumber(0)
74{
75 if (!context) {
76 throw IllegalParameterException("context cannot be null");
77 }
78
79 const std::uint8_t cla = context->getTargetSam()->getClassByte();
80 const std::uint8_t inst = getCommandRef().getInstructionByte();
81 const std::uint8_t p1 = 0;
82
83 std::uint8_t p2;
84
85 switch (systemKeyType) {
86 case SystemKeyType::PERSONALIZATION:
87 p2 = 0xC1;
88 break;
89 case SystemKeyType::KEY_MANAGEMENT:
90 p2 = 0xC2;
91 break;
92 case SystemKeyType::RELOADING:
93 p2 = 0xC3;
94 break;
95 case SystemKeyType::AUTHENTICATION:
96 p2 = 0xC4;
97 break;
98 default:
99 throw IllegalStateException(
100 "Unexpected SystemKeyType: "
101 + std::to_string(static_cast<int>(systemKeyType)));
102 }
103
104 const std::vector<std::uint8_t> dataIn = {0x00, 0x00};
105
106 const auto apdu = std::make_shared<ApduRequestAdapter>(
107 ApduUtil::build(cla, inst, p1, p2, dataIn));
108 apdu->addSuccessfulStatusWord(SW_DATA_NOT_SIGNED_WARNING);
109 apdu->addSuccessfulStatusWord(SW_KEY_NOT_FOUND);
110 setApduRequest(apdu);
111
112 addSubName(name(mSystemKeyType));
113}
114
115CommandReadKeyParameters::CommandReadKeyParameters(
116 std::shared_ptr<DtoAdapters::CommandContextDto> context,
117 const std::uint8_t kif,
118 const std::uint8_t kvc)
119: Command(CommandRef::READ_KEY_PARAMETERS, 32, context)
120, mSystemKeyType(SystemKeyType::UNKNOWN)
121, mHasKifKvc(true)
122, mRecordNumber(0)
123{
124 if (!context) {
125 throw IllegalParameterException("context cannot be null");
126 }
127
128 const std::uint8_t cla = context->getTargetSam()->getClassByte();
129 const std::uint8_t inst = getCommandRef().getInstructionByte();
130 const std::uint8_t p1 = 0;
131 const std::uint8_t p2 = 0xF0;
132 const std::vector<std::uint8_t> dataIn = {kif, kvc};
133
134 mKifKvc = static_cast<std::uint16_t>((kif << 8) | (kvc & 0xFF));
135
136 auto adapter = std::make_shared<ApduRequestAdapter>(
137 ApduUtil::build(cla, inst, p1, p2, dataIn));
138 adapter->addSuccessfulStatusWord(SW_DATA_NOT_SIGNED_WARNING);
139 setApduRequest(adapter);
140
141 addSubName("Work key");
142}
143
144CommandReadKeyParameters::CommandReadKeyParameters(
145 std::shared_ptr<DtoAdapters::CommandContextDto> context, int recordNumber)
146: Command(CommandRef::READ_KEY_PARAMETERS, 32, context)
147, mSystemKeyType(SystemKeyType::UNKNOWN)
148, mHasKifKvc(false)
149, mRecordNumber(recordNumber)
150{
151 if (!context) {
152 throw IllegalParameterException("context cannot be null");
153 }
154
155 const std::uint8_t cla = context->getTargetSam()->getClassByte();
156 const std::uint8_t inst = getCommandRef().getInstructionByte();
157 const std::uint8_t p1 = 0;
158 const std::uint8_t p2 = static_cast<std::uint8_t>(recordNumber);
159 const std::vector<std::uint8_t> dataIn = {0x00, 0x00};
160
161 auto adapter = std::make_shared<ApduRequestAdapter>(
162 ApduUtil::build(cla, inst, p1, p2, dataIn));
163 adapter->addSuccessfulStatusWord(SW_DATA_NOT_SIGNED_WARNING);
164 adapter->addSuccessfulStatusWord(SW_KEY_NOT_FOUND);
165 setApduRequest(adapter);
166
167 addSubName("Work key");
168}
169
170void
171CommandReadKeyParameters::finalizeRequest()
172{
173 /* nothing to do */
174}
175
176bool
177CommandReadKeyParameters::isControlSamRequiredToFinalizeRequest() const
178{
179 return false;
180}
181
182void
183CommandReadKeyParameters::parseResponse(
184 std::shared_ptr<ApduResponseApi> apduResponse)
185{
186 try {
187 setResponseAndCheckStatus(apduResponse);
188 } catch (const DataAccessException&) {
189 return;
190 }
191
192 auto keyParameterAdapter = std::make_shared<KeyParameterAdapter>(
193 Arrays::copyOfRange(apduResponse->getApdu(), 8, 21));
194
195 std::shared_ptr<LegacySamAdapter> legacySamAdapter
196 = getContext()->getTargetSam();
197 if (mSystemKeyType != SystemKeyType::UNKNOWN) {
198 legacySamAdapter->setSystemKeyParameter(
199 mSystemKeyType, keyParameterAdapter);
200 } else if (mHasKifKvc) {
201 legacySamAdapter->setWorkKeyParameter(mKifKvc, keyParameterAdapter);
202 } else {
203 legacySamAdapter->setWorkKeyParameter(
204 mRecordNumber, keyParameterAdapter);
205 }
206}
207
208const std::map<int, const std::shared_ptr<StatusProperties>>&
209CommandReadKeyParameters::getStatusTable() const
210{
211 return STATUS_TABLE;
212}
213
214} /* namespace legacysam */
215} /* namespace crypto */
216} /* namespace calypso */
217} /* namespace card */
218} /* namespace keyple */