Keyple Card Calypso C++ Library - 3.2.2
Component of the Keyple C++ middleware
CalypsoCardApiFactoryAdapter.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/CalypsoCardApiFactoryAdapter.hpp"
15
16#include <memory>
17#include <string>
18
19#include "keyple/card/calypso/AsymmetricCryptoSecuritySettingAdapter.hpp"
20#include "keyple/card/calypso/CalypsoCardSelectionExtensionAdapter.hpp"
21#include "keyple/card/calypso/FreeTransactionManagerAdapter.hpp"
22#include "keyple/card/calypso/SecureExtendedModeTransactionManagerAdapter.hpp"
23#include "keyple/card/calypso/SecurePkiModeTransactionManagerAdapter.hpp"
24#include "keyple/card/calypso/SecureRegularModeTransactionManagerAdapter.hpp"
25#include "keyple/card/calypso/SymmetricCryptoSecuritySettingAdapter.hpp"
26#include "keyple/core/util/cpp/exception/IllegalArgumentException.hpp"
27#include "keypop/calypso/crypto/asymmetric/transaction/spi/AsymmetricCryptoCardTransactionManagerFactorySpi.hpp"
28#include "keypop/calypso/crypto/symmetric/spi/SymmetricCryptoCardTransactionManagerFactorySpi.hpp"
29#include "keypop/card/ProxyReaderApi.hpp"
30
31namespace keyple {
32namespace card {
33namespace calypso {
34
35using keyple::core::util::cpp::exception::IllegalArgumentException;
36using keypop::calypso::crypto::asymmetric::transaction::spi::
37 AsymmetricCryptoCardTransactionManagerFactorySpi;
38using keypop::calypso::crypto::symmetric::spi::
39 SymmetricCryptoCardTransactionManagerFactorySpi;
40using keypop::card::ProxyReaderApi;
41
42const std::string CalypsoCardApiFactoryAdapter ::
43 MSG_THE_PROVIDED_CARD_HAS_AN_UNDEFINED_PRODUCT_TYPE
44 = "The provided 'card' has an undefined product type";
45const std::string CalypsoCardApiFactoryAdapter::MSG_CARD_READER = "cardReader";
46const std::string CalypsoCardApiFactoryAdapter::MSG_CARD = "card";
47const std::string CalypsoCardApiFactoryAdapter::MSG_SECURITY_SETTING
48 = "securitySetting";
49
50std::unique_ptr<CalypsoCardSelectionExtension>
51CalypsoCardApiFactoryAdapter::createCalypsoCardSelectionExtension()
52{
53 return std::unique_ptr<CalypsoCardSelectionExtensionAdapter>(
54 new CalypsoCardSelectionExtensionAdapter());
55}
56
57std::unique_ptr<SymmetricCryptoSecuritySetting>
58CalypsoCardApiFactoryAdapter::createSymmetricCryptoSecuritySetting(
59 const std::shared_ptr<SymmetricCryptoCardTransactionManagerFactory>&
60 cryptoCardTransactionManagerFactory)
61{
62 Assert::getInstance().notNull(
63 cryptoCardTransactionManagerFactory,
64 "cryptoCardTransactionManagerFactory");
65
66 const auto& symmetricCryptoCardTransactionManagerFactorySpi
67 = std::dynamic_pointer_cast<
68 SymmetricCryptoCardTransactionManagerFactorySpi>(
69 cryptoCardTransactionManagerFactory);
70
71 if (!symmetricCryptoCardTransactionManagerFactorySpi) {
72 throw IllegalArgumentException(
73 std::string("Cannot cast 'factory' to ")
74 + "SymmetricCryptoCardTransactionManagerFactorySpi. Actual type: "
75 + typeid(cryptoCardTransactionManagerFactory).name());
76 }
77
78 return std::unique_ptr<SymmetricCryptoSecuritySettingAdapter>(
79 new SymmetricCryptoSecuritySettingAdapter(
80 std::dynamic_pointer_cast<
81 SymmetricCryptoCardTransactionManagerFactorySpi>(
82 cryptoCardTransactionManagerFactory)));
83}
84
85std::unique_ptr<AsymmetricCryptoSecuritySetting>
86CalypsoCardApiFactoryAdapter::createAsymmetricCryptoSecuritySetting(
87 const std::shared_ptr<AsymmetricCryptoCardTransactionManagerFactory>&
88 cryptoCardTransactionManagerFactory)
89{
90 Assert::getInstance().notNull(
91 cryptoCardTransactionManagerFactory,
92 "cryptoCardTransactionManagerFactory");
93
94 const auto asymmetric = std::dynamic_pointer_cast<
95 AsymmetricCryptoCardTransactionManagerFactorySpi>(
96 cryptoCardTransactionManagerFactory);
97 if (asymmetric == nullptr) {
98 throw IllegalArgumentException(
99 std::string("Cannot cast 'factory' to")
100 + "AsymmetricCryptoCardTransactionManagerFactorySpi. Actual type: "
101 + +typeid(cryptoCardTransactionManagerFactory).name());
102 }
103
104 return std::unique_ptr<AsymmetricCryptoSecuritySettingAdapter>(
105 new AsymmetricCryptoSecuritySettingAdapter(
106 std::dynamic_pointer_cast<
107 AsymmetricCryptoCardTransactionManagerFactorySpi>(
108 cryptoCardTransactionManagerFactory)));
109}
110
111std::unique_ptr<FreeTransactionManager>
112CalypsoCardApiFactoryAdapter::createFreeTransactionManager(
113 const std::shared_ptr<CardReader>& cardReader,
114 const std::shared_ptr<CalypsoCard>& card)
115{
116 Assert::getInstance()
117 .notNull(cardReader, MSG_CARD_READER)
118 .notNull(card, MSG_CARD);
119
120 const auto proxyReaderApi
121 = std::dynamic_pointer_cast<ProxyReaderApi>(cardReader);
122 if (!proxyReaderApi) {
123 throw IllegalArgumentException(
124 std::string("Cannot cast 'cardReader' to ProxyReaderApi. Actual ")
125 + "type: " + typeid(cardReader).name());
126 }
127
128 const auto calypsoCardAdapter
129 = std::dynamic_pointer_cast<CalypsoCardAdapter>(card);
130 if (!calypsoCardAdapter) {
131 throw IllegalArgumentException(
132 std::string("Cannot cast 'card' to CalypsoCardAdapter. Actual ")
133 + "type: " + typeid(card).name());
134 }
135
136 if (card->getProductType() == CalypsoCard::ProductType::UNKNOWN) {
137 throw IllegalArgumentException(
138 MSG_THE_PROVIDED_CARD_HAS_AN_UNDEFINED_PRODUCT_TYPE);
139 }
140
141 return std::unique_ptr<FreeTransactionManagerAdapter>(
142 new FreeTransactionManagerAdapter(proxyReaderApi, calypsoCardAdapter));
143}
144
145std::unique_ptr<SecureRegularModeTransactionManager>
146CalypsoCardApiFactoryAdapter::createSecureRegularModeTransactionManager(
147 const std::shared_ptr<CardReader>& cardReader,
148 const std::shared_ptr<CalypsoCard>& card,
149 const std::shared_ptr<SymmetricCryptoSecuritySetting>& securitySetting)
150{
151 Assert::getInstance()
152 .notNull(cardReader, MSG_CARD_READER)
153 .notNull(card, MSG_CARD)
154 .notNull(securitySetting, MSG_SECURITY_SETTING);
155
156 const auto proxyReaderApi
157 = std::dynamic_pointer_cast<ProxyReaderApi>(cardReader);
158 if (!proxyReaderApi) {
159 throw IllegalArgumentException(
160 std::string("Cannot cast 'cardReader' to ProxyReaderApi. Actual ")
161 + "type: " + typeid(cardReader).name());
162 }
163
164 const auto calypsoCardAdapter
165 = std::dynamic_pointer_cast<CalypsoCardAdapter>(card);
166 if (!calypsoCardAdapter) {
167 throw IllegalArgumentException(
168 std::string("Cannot cast 'card' to CalypsoCardAdapter. Actual ")
169 + "type: " + typeid(card).name());
170 }
171
172 const auto symmetricCryptoSecuritySettingAdapter
173 = std::dynamic_pointer_cast<SymmetricCryptoSecuritySettingAdapter>(
174 securitySetting);
175 if (!symmetricCryptoSecuritySettingAdapter) {
176 throw IllegalArgumentException(
177 std::string("Cannot cast 'securitySetting' to ")
178 + "SymmetricCryptoSecuritySettingAdapter. Actual type: "
179 + typeid(securitySetting).name());
180 }
181
182 if (card->getProductType() == CalypsoCard::ProductType::UNKNOWN) {
183 throw IllegalArgumentException(
184 MSG_THE_PROVIDED_CARD_HAS_AN_UNDEFINED_PRODUCT_TYPE);
185 }
186
187 return std::unique_ptr<SecureRegularModeTransactionManagerAdapter>(
188 new SecureRegularModeTransactionManagerAdapter(
189 std::dynamic_pointer_cast<ProxyReaderApi>(cardReader),
190 std::dynamic_pointer_cast<CalypsoCardAdapter>(card),
191 std::dynamic_pointer_cast<SymmetricCryptoSecuritySettingAdapter>(
192 securitySetting)));
193}
194
195std::unique_ptr<SecureExtendedModeTransactionManager>
196CalypsoCardApiFactoryAdapter::createSecureExtendedModeTransactionManager(
197 const std::shared_ptr<CardReader>& cardReader,
198 const std::shared_ptr<CalypsoCard>& card,
199 const std::shared_ptr<SymmetricCryptoSecuritySetting>& securitySetting)
200{
201 Assert::getInstance()
202 .notNull(cardReader, MSG_CARD_READER)
203 .notNull(card, MSG_CARD)
204 .notNull(securitySetting, MSG_SECURITY_SETTING);
205
206 const auto proxyReaderApi
207 = std::dynamic_pointer_cast<ProxyReaderApi>(cardReader);
208 if (!proxyReaderApi) {
209 throw IllegalArgumentException(
210 std::string("Cannot cast 'cardReader' to ProxyReaderApi. Actual ")
211 + "type: " + typeid(cardReader).name());
212 }
213
214 const auto calypsoCardAdapter
215 = std::dynamic_pointer_cast<CalypsoCardAdapter>(card);
216 if (!calypsoCardAdapter) {
217 throw IllegalArgumentException(
218 std::string("Cannot cast 'card' to CalypsoCardAdapter. Actual ")
219 + "type: " + typeid(card).name());
220 }
221
222 const auto symmetricCryptoSecuritySettingAdapter
223 = std::dynamic_pointer_cast<SymmetricCryptoSecuritySettingAdapter>(
224 securitySetting);
225 if (!symmetricCryptoSecuritySettingAdapter) {
226 throw IllegalArgumentException(
227 std::string("Cannot cast 'securitySetting' to ")
228 + "SymmetricCryptoSecuritySettingAdapter. Actual type: "
229 + typeid(securitySetting).name());
230 }
231
232 if (card->getProductType() == CalypsoCard::ProductType::UNKNOWN) {
233 throw IllegalArgumentException(
234 MSG_THE_PROVIDED_CARD_HAS_AN_UNDEFINED_PRODUCT_TYPE);
235 }
236
237 return std::unique_ptr<SecureExtendedModeTransactionManagerAdapter>(
238 new SecureExtendedModeTransactionManagerAdapter(
239 std::dynamic_pointer_cast<ProxyReaderApi>(cardReader),
240 std::dynamic_pointer_cast<CalypsoCardAdapter>(card),
241 std::dynamic_pointer_cast<SymmetricCryptoSecuritySettingAdapter>(
242 securitySetting)));
243}
244
245std::unique_ptr<SecurePkiModeTransactionManager>
246CalypsoCardApiFactoryAdapter::createSecurePkiModeTransactionManager(
247 const std::shared_ptr<CardReader>& cardReader,
248 const std::shared_ptr<CalypsoCard>& card,
249 const std::shared_ptr<AsymmetricCryptoSecuritySetting>& securitySetting)
250{
251 Assert::getInstance()
252 .notNull(cardReader, MSG_CARD_READER)
253 .notNull(card, MSG_CARD)
254 .notNull(securitySetting, MSG_SECURITY_SETTING);
255
256 const auto proxyReaderApi
257 = std::dynamic_pointer_cast<ProxyReaderApi>(cardReader);
258 if (!proxyReaderApi) {
259 throw IllegalArgumentException(
260 std::string("Cannot cast 'cardReader' to ProxyReaderApi. Actual ")
261 + "type: " + typeid(cardReader).name());
262 }
263
264 const auto calypsoCardAdapter
265 = std::dynamic_pointer_cast<CalypsoCardAdapter>(card);
266 if (!calypsoCardAdapter) {
267 throw IllegalArgumentException(
268 std::string("Cannot cast 'card' to CalypsoCardAdapter. Actual ")
269 + "type: " + typeid(card).name());
270 }
271
272 const auto asymmetricCryptoSecuritySettingAdapter
273 = std::dynamic_pointer_cast<AsymmetricCryptoSecuritySettingAdapter>(
274 securitySetting);
275 if (!asymmetricCryptoSecuritySettingAdapter) {
276 throw IllegalArgumentException(
277 std::string("Cannot cast 'securitySetting' to ")
278 + "AsymmetricCryptoSecuritySettingAdapter. Actual type: "
279 + typeid(securitySetting).name());
280 }
281
282 return std::unique_ptr<SecurePkiModeTransactionManagerAdapter>(
283 new SecurePkiModeTransactionManagerAdapter(
284 std::dynamic_pointer_cast<ProxyReaderApi>(cardReader),
285 std::dynamic_pointer_cast<CalypsoCardAdapter>(card),
286 std::dynamic_pointer_cast<AsymmetricCryptoSecuritySettingAdapter>(
287 securitySetting)));
288}
289
290std::shared_ptr<SearchCommandData>
291CalypsoCardApiFactoryAdapter::createSearchCommandData()
292{
293 return std::make_shared<DtoAdapters::SearchCommandDataAdapter>();
294}
295
296} /* namespace calypso */
297} /* namespace card */
298} /* namespace keyple */