keyple-card-calypso-crypto-legacysam-cpp-lib - 1.0.2
Component of the Keyple C++ middleware
SymmetricCryptoCardTransactionManagerFactoryAdapter.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/SymmetricCryptoCardTransactionManagerFactoryAdapter.hpp"
15
16#include <memory>
17#include <string>
18#include <utility>
19#include <vector>
20
21#include "keyple/card/calypso/crypto/legacysam/CardTransactionUtil.hpp"
22#include "keyple/card/calypso/crypto/legacysam/CommandGetChallenge.hpp"
23#include "keyple/card/calypso/crypto/legacysam/DtoAdapters.hpp"
24#include "keyple/card/calypso/crypto/legacysam/SymmetricCryptoCardTransactionManagerAdapter.hpp"
25#include "keyple/core/util/HexUtil.hpp"
26#include "keyple/core/util/cpp/exception/IllegalStateException.hpp"
27#include "keypop/calypso/crypto/symmetric/SymmetricCryptoException.hpp"
28#include "keypop/reader/InvalidCardResponseException.hpp"
29
30namespace keyple {
31namespace card {
32namespace calypso {
33namespace crypto {
34namespace legacysam {
35
36using keyple::core::util::HexUtil;
37using keyple::core::util::cpp::exception::IllegalStateException;
38using keypop::calypso::crypto::symmetric::SymmetricCryptoException;
39using keypop::reader::InvalidCardResponseException;
40
41SymmetricCryptoCardTransactionManagerFactoryAdapter::
42 SymmetricCryptoCardTransactionManagerFactoryAdapter(
43 std::shared_ptr<ProxyReaderApi> samReader,
44 std::shared_ptr<LegacySamAdapter> sam,
45 std::shared_ptr<ContextSettingAdapter> contextSetting)
46: mSamReader(samReader)
47, mSam(sam)
48, mIsExtendedModeSupported(
49 mSam->getProductType() == ProductType::SAM_C1
50 || mSam->getProductType() == ProductType::HSM_C1)
51, mMaxCardApduLengthSupported(
52 contextSetting->getContactReaderPayloadCapacity() != nullptr
53 ? std::min(
54 mSam->getMaxDigestDataLength(),
55 *contextSetting->getContactReaderPayloadCapacity())
56 : mSam->getMaxDigestDataLength())
57{
58}
59
60bool
61SymmetricCryptoCardTransactionManagerFactoryAdapter::isExtendedModeSupported()
62 const
63{
64 return mIsExtendedModeSupported;
65}
66
67int
68SymmetricCryptoCardTransactionManagerFactoryAdapter::
69 getMaxCardApduLengthSupported() const
70{
71 return mMaxCardApduLengthSupported;
72}
73
74void
75SymmetricCryptoCardTransactionManagerFactoryAdapter::
76 preInitTerminalSessionContext()
77{
78 processCommand(
79 std::make_shared<CommandGetChallenge>(
80 std::make_shared<DtoAdapters::CommandContextDto>(
81 mSam, nullptr, nullptr),
82 8));
83}
84
85std::shared_ptr<SymmetricCryptoCardTransactionManagerSpi>
86SymmetricCryptoCardTransactionManagerFactoryAdapter::
87 createCardTransactionManager(
88 const std::vector<uint8_t>& cardKeyDiversifier,
89 bool useExtendedMode,
90 const std::vector<std::vector<uint8_t>>& transactionAuditData)
91{
92 if (useExtendedMode && !mIsExtendedModeSupported) {
93 throw IllegalStateException(
94 "The extended mode is not supported by the crypto service");
95 }
96
97 return std::make_shared<SymmetricCryptoCardTransactionManagerAdapter>(
98 mSamReader,
99 mSam,
100 cardKeyDiversifier,
101 useExtendedMode,
102 mMaxCardApduLengthSupported,
103 transactionAuditData);
104}
105
106void
107SymmetricCryptoCardTransactionManagerFactoryAdapter::processCommand(
108 std::shared_ptr<Command> command)
109{
110 std::vector<std::vector<uint8_t>> transactionAuditData;
111 try {
112 /* Get the list of C-APDU to transmit */
113 auto apduRequests = CardTransactionUtil::getApduRequests({command});
114
115 /* Wrap the list of C-APDUs into a card request */
116 auto cardRequest = std::make_shared<DtoAdapters::CardRequestAdapter>(
117 apduRequests, true);
118
119 /* Transmit the commands to the SAM */
120 auto cardResponse = CardTransactionUtil::transmitCardRequest(
121 cardRequest, mSamReader, mSam, transactionAuditData);
122
123 auto apduResponse
124 = cardResponse
125 ->getApduResponses()[0]; // Assuming only one response.
126 command->parseResponse(apduResponse);
127
128 } catch (const CommandException& e) {
129 auto commandRef = command->getCommandRef();
130 const std::string sw
131 = command->getApduResponse()
132 ? HexUtil::toHex(
133 static_cast<std::uint16_t>(
134 command->getApduResponse()->getStatusWord()))
135 : "null";
136
137 const std::string errorMessage
138 = CardTransactionUtil::MSG_FAILED_TO_PROCESS_SAM_RESPONSE
139 + " Command: " + commandRef.getName() + ", SW: " + sw;
140
141 const std::string detailedErrorMessage
142 = CardTransactionUtil::MSG_FAILED_TO_PROCESS_SAM_RESPONSE
143 + " Command: " + commandRef.getName() + ", SW: " + sw
144 + CardTransactionUtil::getTransactionAuditDataAsString(
145 transactionAuditData, mSam);
146
147 throw SymmetricCryptoException(
148 errorMessage,
149 InvalidCardResponseException(detailedErrorMessage, e));
150 }
151}
152
153} /* namespace legacysam */
154} /* namespace crypto */
155} /* namespace calypso */
156} /* namespace card */
157} /* namespace keyple */