keyple-card-calypso-crypto-legacysam-cpp-lib - 1.0.2
Component of the Keyple C++ middleware
CardTransactionUtil.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/CardTransactionUtil.hpp"
15
16#include <memory>
17#include <string>
18#include <utility>
19#include <vector>
20
21#include "keypop/calypso/crypto/symmetric/SymmetricCryptoIOException.hpp"
22#include "keypop/card/CardBrokenCommunicationException.hpp"
23#include "keypop/card/ChannelControl.hpp"
24#include "keypop/card/ReaderBrokenCommunicationException.hpp"
25#include "keypop/card/UnexpectedStatusWordException.hpp"
26#include "keypop/reader/CardCommunicationException.hpp"
27#include "keypop/reader/ReaderCommunicationException.hpp"
28
29namespace keyple {
30namespace card {
31namespace calypso {
32namespace crypto {
33namespace legacysam {
34
35using keypop::calypso::crypto::symmetric::SymmetricCryptoIOException;
36using keypop::card::CardBrokenCommunicationException;
37using keypop::card::ReaderBrokenCommunicationException;
38using keypop::card::UnexpectedStatusWordException;
39using keypop::reader::CardCommunicationException;
40using keypop::reader::ReaderCommunicationException;
41
42const std::string CardTransactionUtil::MSG_SAM_READER_COMMUNICATION_ERROR
43 = "Failed to communicate with SAM reader";
44const std::string CardTransactionUtil::MSG_SAM_COMMUNICATION_ERROR
45 = "Failed to communicate with SAM";
46const std::string CardTransactionUtil::MSG_WHILE_TRANSMITTING_COMMANDS
47 = " while transmitting commands.";
48const std::string CardTransactionUtil::MSG_FAILED_TO_PROCESS_SAM_RESPONSE
49 = "Failed to process SAM response.";
50
51std::vector<std::shared_ptr<ApduRequestSpi>>
52CardTransactionUtil::getApduRequests(
53 const std::vector<std::shared_ptr<Command>>& commands)
54{
55 std::vector<std::shared_ptr<ApduRequestSpi>> apduRequests;
56 for (const auto& command : commands) {
57 apduRequests.push_back(command->getApduRequest());
58 }
59
60 return apduRequests;
61}
62
63std::shared_ptr<CardResponseApi>
64CardTransactionUtil::transmitCardRequest(
65 std::shared_ptr<CardRequestSpi> cardRequest,
66 std::shared_ptr<ProxyReaderApi> samReader,
67 std::shared_ptr<LegacySam> sam,
68 std::vector<std::vector<uint8_t>>& transactionAuditData)
69{
70 std::shared_ptr<CardResponseApi> cardResponse;
71 try {
72 cardResponse = samReader->transmitCardRequest(
73 cardRequest, keypop::card::ChannelControl::KEEP_OPEN);
74
75 } catch (const ReaderBrokenCommunicationException& e) {
76 saveTransactionAuditData(
77 cardRequest, e.getCardResponse(), transactionAuditData);
78 throw SymmetricCryptoIOException(
79 MSG_SAM_READER_COMMUNICATION_ERROR
80 + MSG_WHILE_TRANSMITTING_COMMANDS,
81 ReaderCommunicationException(
82 MSG_SAM_READER_COMMUNICATION_ERROR
83 + MSG_WHILE_TRANSMITTING_COMMANDS
84 + getTransactionAuditDataAsString(
85 transactionAuditData, sam),
86 e));
87
88 } catch (const CardBrokenCommunicationException& e) {
89 saveTransactionAuditData(
90 cardRequest, e.getCardResponse(), transactionAuditData);
91 throw SymmetricCryptoIOException(
92 MSG_SAM_COMMUNICATION_ERROR + MSG_WHILE_TRANSMITTING_COMMANDS,
93 CardCommunicationException(
94 MSG_SAM_COMMUNICATION_ERROR + MSG_WHILE_TRANSMITTING_COMMANDS
95 + getTransactionAuditDataAsString(
96 transactionAuditData, sam),
97 e));
98
99 } catch (const UnexpectedStatusWordException& e) {
100 cardResponse = e.getCardResponse();
101 }
102
103 saveTransactionAuditData(cardRequest, cardResponse, transactionAuditData);
104
105 return cardResponse;
106}
107
108void
109CardTransactionUtil::saveTransactionAuditData(
110 std::shared_ptr<CardRequestSpi> cardRequest,
111 std::shared_ptr<CardResponseApi> cardResponse,
112 std::vector<std::vector<uint8_t>>& transactionAuditData)
113{
114 if (cardResponse) {
115 auto requests = cardRequest->getApduRequests();
116 auto responses = cardResponse->getApduResponses();
117 for (size_t i = 0; i < responses.size(); i++) {
118 transactionAuditData.push_back(requests[i]->getApdu());
119 transactionAuditData.push_back(responses[i]->getApdu());
120 }
121 }
122}
123
124std::string
125CardTransactionUtil::getTransactionAuditDataAsString(
126 const std::vector<std::vector<uint8_t>>& /*transactionAuditData*/,
127 std::shared_ptr<LegacySam> /*sam*/)
128{
129 return std::string("\nTransaction audit JSON data: {") + "\"sam\":" + "TODO"
130 + ", " + "\"apdus\":"
131 + "TODO" /*JsonUtil.toJson(transactionAuditData) */ + "}";
132}
133
134} /* namespace legacysam */
135} /* namespace crypto */
136} /* namespace calypso */
137} /* namespace card */
138} /* namespace keyple */