Keyple Card Calypso C++ Library - 3.2.2
Component of the Keyple C++ middleware
SecureTransactionManagerAdapter.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/SecureTransactionManagerAdapter.hpp"
15
16#include <memory>
17#include <string>
18
19#include "keyple/card/calypso/CalypsoCardAdapter.hpp"
20#include "keyple/card/calypso/CommandCloseSecureSession.hpp"
21#include "keyple/core/util/cpp/exception/IllegalStateException.hpp"
22#include "keyple/core/util/cpp/exception/RuntimeException.hpp"
23#include "keypop/calypso/card/WriteAccessLevel.hpp"
24
25namespace keyple {
26namespace card {
27namespace calypso {
28
29using keyple::core::util::cpp::exception::IllegalStateException;
30using keyple::core::util::cpp::exception::RuntimeException;
31using keypop::calypso::card::WriteAccessLevel;
32
33template <typename T>
34const std::string
35 SecureTransactionManagerAdapter<T>::MSG_SECURE_SESSION_NOT_OPEN
36 = "Secure session is not open";
37template <typename T>
38const std::string SecureTransactionManagerAdapter<T>::MSG_SECURE_SESSION_OPEN
39 = "Secure session is open";
40
41template <typename T>
42SecureTransactionManagerAdapter<T>::SecureTransactionManagerAdapter(
43 std::shared_ptr<ProxyReaderApi> /* cardReader */,
44 std::shared_ptr<CalypsoCardAdapter> /* card */)
45: mIsSecureSessionOpen(false)
46{
47}
48
49template <typename T>
50void
51SecureTransactionManagerAdapter<T>::checkSecureSession() const
52{
53 if (!mIsSecureSessionOpen) {
54 throw IllegalStateException(MSG_SECURE_SESSION_NOT_OPEN);
55 }
56}
57
58template <typename T>
59void
60SecureTransactionManagerAdapter<T>::checkNoSecureSession() const
61{
62 if (mIsSecureSessionOpen) {
63 throw IllegalStateException(MSG_SECURE_SESSION_OPEN);
64 }
65}
66
67template <typename T>
68void
69SecureTransactionManagerAdapter<T>::disablePreOpenMode()
70{
71 TransactionManagerAdapter<T>::mCard->setPreOpenWriteAccessLevel(
72 WriteAccessLevel::UNKOWN);
73 TransactionManagerAdapter<T>::mCard->setPreOpenDataOut({});
74}
75
76template <typename T>
77T&
78SecureTransactionManagerAdapter<T>::prepareCancelSecureSession()
79{
80 try {
81 TransactionManagerAdapter<T>::mCommands.push_back(
82 std::unique_ptr<CommandCloseSecureSession>(
83 new CommandCloseSecureSession(
84 this->getTransactionContext(),
85 this->getCommandContext(),
86 true)));
87
88 } catch (...) {
89 this->resetTransaction();
90
91 /* Finally */
92 resetCommandContext();
93 disablePreOpenMode();
94
95 throw;
96 }
97
98 /* Finally */
99 resetCommandContext();
100 disablePreOpenMode();
101
102 return dynamic_cast<T&>(*this);
103}
104
105} /* namespace calypso */
106} /* namespace card */
107} /* namespace keyple */
108
109/* Explicit template instantiations for concrete transaction manager bases */
110#include "keypop/calypso/card/cpp/SecureExtendedModeTransactionManagerBase.hpp"
111#include "keypop/calypso/card/cpp/SecureRegularModeTransactionManagerBase.hpp"
112#include "keypop/calypso/card/transaction/SecurePkiModeTransactionManager.hpp"
113
114template class keyple::card::calypso::SecureTransactionManagerAdapter<
115 keypop::calypso::card::cpp::SecureExtendedModeTransactionManagerBase>;
116
117template class keyple::card::calypso::SecureTransactionManagerAdapter<
118 keypop::calypso::card::cpp::SecureRegularModeTransactionManagerBase>;
119
120template class keyple::card::calypso::SecureTransactionManagerAdapter<
121 keypop::calypso::card::transaction::SecurePkiModeTransactionManager>;