keyple-card-calypso-crypto-legacysam-cpp-lib - 1.0.2
Component of the Keyple C++ middleware
CommandExecutor.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 *
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/CommandExecutor.hpp"
15
16#include <memory>
17#include <string>
18#include <vector>
19
20#include "keyple/card/calypso/crypto/legacysam/Command.hpp"
21#include "keyple/card/calypso/crypto/legacysam/CommandException.hpp"
22#include "keyple/card/calypso/crypto/legacysam/CommandRef.hpp"
23#include "keyple/core/util/HexUtil.hpp"
24#include "keypop/calypso/card/transaction/InconsistentDataException.hpp"
25#include "keypop/card/CardBrokenCommunicationException.hpp"
26#include "keypop/card/CardResponseApi.hpp"
27#include "keypop/card/ChannelControl.hpp"
28#include "keypop/card/ReaderBrokenCommunicationException.hpp"
29#include "keypop/card/UnexpectedStatusWordException.hpp"
30#include "keypop/reader/CardCommunicationException.hpp"
31#include "keypop/reader/InvalidCardResponseException.hpp"
32#include "keypop/reader/ReaderCommunicationException.hpp"
33
34namespace keyple {
35namespace card {
36namespace calypso {
37namespace crypto {
38namespace legacysam {
39
40using keyple::core::util::HexUtil;
41using keypop::calypso::card::transaction::InconsistentDataException;
42using keypop::card::CardBrokenCommunicationException;
43using keypop::card::CardResponseApi;
44using keypop::card::ReaderBrokenCommunicationException;
45using keypop::card::UnexpectedStatusWordException;
46using keypop::reader::CardCommunicationException;
47using keypop::reader::InvalidCardResponseException;
48using keypop::reader::ReaderCommunicationException;
49
50const std::string CommandExecutor::MSG_SAM_READER_COMMUNICATION_ERROR
51 = "Failed to communicate with SAM reader";
52const std::string CommandExecutor::MSG_SAM_COMMUNICATION_ERROR
53 = "Failed to communicate with SAM";
54const std::string CommandExecutor::MSG_WHILE_TRANSMITTING_COMMANDS
55 = " while transmitting commands.";
56
57void
58CommandExecutor::processCommands(
59 const std::vector<std::shared_ptr<Command>>& commands,
60 std::shared_ptr<ProxyReaderApi> samReader,
61 ChannelControl channelControl)
62{
63 if (commands.empty()) {
64 return;
65 }
66
67 std::vector<std::shared_ptr<Command>> cardRequestCommands;
68
69 for (const auto& command : commands) {
70 if (command->isControlSamRequiredToFinalizeRequest()) {
71 executeCommands(
72 cardRequestCommands, samReader, ChannelControl::KEEP_OPEN);
73 cardRequestCommands.clear();
74 }
75
76 command->finalizeRequest();
77 cardRequestCommands.push_back(command);
78 }
79
80 executeCommands(cardRequestCommands, samReader, channelControl);
81}
82
83void
84CommandExecutor::processCommandsAlreadyFinalized(
85 const std::vector<std::shared_ptr<Command>>& commands,
86 std::shared_ptr<ProxyReaderApi> samReader,
87 ChannelControl channelControl)
88{
89 if (commands.empty()) {
90 return;
91 }
92
93 executeCommands(commands, samReader, channelControl);
94}
95
96void
97CommandExecutor::executeCommands(
98 const std::vector<std::shared_ptr<Command>>& commands,
99 std::shared_ptr<ProxyReaderApi> samReader,
100 ChannelControl channelControl)
101{
102 /* Retrieve the list of C-APDUs */
103 std::vector<std::shared_ptr<ApduRequestSpi>> apduRequests
104 = getApduRequests(commands);
105
106 /* Wrap the list of C-APDUs into a card request */
107 std::shared_ptr<CardRequestSpi> cardRequest
108 = std::make_shared<DtoAdapters::CardRequestAdapter>(apduRequests, true);
109
110 /* Transmit the commands to the card */
111 std::shared_ptr<CardResponseApi> cardResponse = transmitCardRequest(
112 cardRequest,
113 samReader,
114 channelControl == ChannelControl::CLOSE_AFTER
115 ? keypop::card::ChannelControl::CLOSE_AFTER
116 : keypop::card::ChannelControl::KEEP_OPEN);
117
118 /* Retrieve the list of R-APDUs */
119 std::vector<std::shared_ptr<ApduResponseApi>> apduResponses
120 = cardResponse->getApduResponses();
121
122 /*
123 * If there are more responses than requests, then we are unable to fill the
124 * card image. In this case we stop processing immediately because it may be
125 * a case of fraud, and we throw a desynchronized exception.
126 */
127 if (apduResponses.size() > commands.size()) {
128 throw InconsistentDataException(
129 "The number of commands/responses does not match. Expected "
130 + std::to_string(commands.size()) + " responses, got "
131 + std::to_string(apduResponses.size()));
132 }
133
134 /*
135 * We go through all the responses (and not the requests) because there may
136 * be fewer in the case of an error that occurred in strict mode. In this
137 * case the last response will raise an exception.
138 */
139 for (int i = 0; i < static_cast<int>(apduResponses.size()); i++) {
140 std::shared_ptr<Command> command = commands[i];
141
142 try {
143 command->parseResponse(apduResponses[i]);
144
145 } catch (const CommandException& e) {
146 const std::string sw
147 = command->getApduResponse() != nullptr
148 ? HexUtil::toHex(
149 static_cast<std::uint16_t>(
150 command->getApduResponse()->getStatusWord()))
151 : "null";
152 throw InvalidCardResponseException(
153 "Failed to process SAM response. Command: "
154 + command->getCommandRef().getName() + ", SW: " + sw,
155 e);
156 }
157 }
158
159 /*
160 * Finally, if no error has occurred and there are fewer responses than
161 * requests, then we throw a desynchronized exception.
162 */
163 if (apduResponses.size() < commands.size()) {
164 throw InconsistentDataException(
165 "The number of commands/responses does not match. Expected "
166 + std::to_string(commands.size()) + " responses, got "
167 + std::to_string(apduResponses.size()));
168 }
169}
170
171std::vector<std::shared_ptr<ApduRequestSpi>>
172CommandExecutor::getApduRequests(
173 const std::vector<std::shared_ptr<Command>>& commands)
174{
175 std::vector<std::shared_ptr<ApduRequestSpi>> apduRequests;
176
177 if (!commands.empty()) {
178 for (const auto& command : commands) {
179 apduRequests.push_back(command->getApduRequest());
180 }
181 }
182
183 return apduRequests;
184}
185
186std::shared_ptr<CardResponseApi>
187CommandExecutor::transmitCardRequest(
188 std::shared_ptr<CardRequestSpi> cardRequest,
189 std::shared_ptr<ProxyReaderApi> samReader,
190 keypop::card::ChannelControl channelControl)
191{
192 std::shared_ptr<CardResponseApi> cardResponse;
193
194 try {
195 cardResponse
196 = samReader->transmitCardRequest(cardRequest, channelControl);
197
198 } catch (const ReaderBrokenCommunicationException& e) {
199 throw ReaderCommunicationException(
200 MSG_SAM_READER_COMMUNICATION_ERROR
201 + MSG_WHILE_TRANSMITTING_COMMANDS,
202 e);
203
204 } catch (const CardBrokenCommunicationException& e) {
205 throw CardCommunicationException(
206 MSG_SAM_COMMUNICATION_ERROR + MSG_WHILE_TRANSMITTING_COMMANDS, e);
207
208 } catch (const UnexpectedStatusWordException& e) {
209 cardResponse = e.getCardResponse();
210 }
211
212 return cardResponse;
213}
214
215} /* namespace legacysam */
216} /* namespace crypto */
217} /* namespace calypso */
218} /* namespace card */
219} /* namespace keyple */