Keyple Card Calypso C++ Library - 3.2.2
Component of the Keyple C++ middleware
SecureExtendedModeTransactionManagerAdapter.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/SecureExtendedModeTransactionManagerAdapter.hpp"
15
16#include <memory>
17#include <string>
18
19#include "keyple/card/calypso/CardCommandRef.hpp"
20#include "keyple/card/calypso/CommandManageSession.hpp"
21#include "keyple/core/util/cpp/exception/IllegalStateException.hpp"
22#include "keyple/core/util/cpp/exception/RuntimeException.hpp"
23#include "keyple/core/util/cpp/exception/UnsupportedOperationException.hpp"
24
25namespace keyple {
26namespace card {
27namespace calypso {
28
29using keyple::core::util::cpp::exception::IllegalStateException;
30using keyple::core::util::cpp::exception::RuntimeException;
31using keyple::core::util::cpp::exception::UnsupportedOperationException;
32
33const std::string
34 SecureExtendedModeTransactionManagerAdapter::MSG_MSS_COMMAND_NOT_SUPPORTED
35 = std::string("'Manage Secure Session' command is not available for this ")
36 + "context (Card and/or SAM does not support extended mode)";
37const std::string
38 SecureExtendedModeTransactionManagerAdapter::MSG_ENCRYPTION_ALREADY_ACTIVE
39 = "Encryption is already active";
40const std::string
41 SecureExtendedModeTransactionManagerAdapter::MSG_ENCRYPTION_NOT_ACTIVE
42 = "Encryption is not active";
43
44SecureExtendedModeTransactionManagerAdapter::
45 SecureExtendedModeTransactionManagerAdapter(
46 std::shared_ptr<ProxyReaderApi> cardReader,
47 std::shared_ptr<CalypsoCardAdapter> card,
48 std::shared_ptr<SymmetricCryptoSecuritySettingAdapter>
49 symmetricCryptoSecuritySetting)
50:
51
52 TransactionManagerAdapter<SecureExtendedModeTransactionManagerBase>(
53 cardReader, card)
54, SecureSymmetricCryptoTransactionManagerAdapter<
55 SecureExtendedModeTransactionManager>(
56 cardReader, card, symmetricCryptoSecuritySetting)
57{
58}
59
60SecureExtendedModeTransactionManager&
61SecureExtendedModeTransactionManagerAdapter::prepareEarlyMutualAuthentication()
62{
63 try {
64 if (!mIsExtendedMode) {
65 throw UnsupportedOperationException(MSG_MSS_COMMAND_NOT_SUPPORTED);
66 }
67
68 checkSecureSession();
69
70 /*
71 * Add a new command or update the last command if it is an MSS command.
72 */
73 if (!mCommands.empty()
74 && mCommands[mCommands.size() - 1]->getCommandRef()
75 == CardCommandRef::MANAGE_SECURE_SESSION) {
76 auto& command = mCommands[mCommands.size() - 1];
77 auto session
78 = std::dynamic_pointer_cast<CommandManageSession>(command);
79 session->setMutualAuthenticationRequested(true);
80
81 } else {
82 auto session = std::make_shared<CommandManageSession>(
83 mTransactionContext, getCommandContext());
84 session->setMutualAuthenticationRequested(true)
85 .setEncryptionRequested(mIsEncryptionActive);
86 mCommands.push_back(session);
87 }
88
89 } catch (...) {
90 resetTransaction();
91 throw;
92 }
93
94 return *this;
95}
96
97SecureExtendedModeTransactionManager&
98SecureExtendedModeTransactionManagerAdapter::prepareActivateEncryption()
99{
100 try {
101 if (!mIsExtendedMode) {
102 throw UnsupportedOperationException(MSG_MSS_COMMAND_NOT_SUPPORTED);
103 }
104
105 checkSecureSession();
106
107 if (mIsEncryptionActive) {
108 throw IllegalStateException(MSG_ENCRYPTION_ALREADY_ACTIVE);
109 }
110
111 /*
112 * Add a new command or update the last command if it is an MSS command.
113 */
114 if (!mCommands.empty()
115 && mCommands[mCommands.size() - 1]->getCommandRef()
116 == CardCommandRef::MANAGE_SECURE_SESSION) {
117 auto command = mCommands[mCommands.size() - 1];
118 auto session
119 = std::dynamic_pointer_cast<CommandManageSession>(command);
120 session->setEncryptionRequested(true);
121
122 } else {
123 auto session = std::make_shared<CommandManageSession>(
124 mTransactionContext, getCommandContext());
125 session->setEncryptionRequested(mIsEncryptionActive);
126 mCommands.push_back(session);
127 }
128
129 mIsEncryptionActive = true;
130
131 } catch (...) {
132 resetTransaction();
133 throw;
134 }
135
136 return *this;
137}
138
139SecureExtendedModeTransactionManager&
140SecureExtendedModeTransactionManagerAdapter::prepareDeactivateEncryption()
141{
142 try {
143 if (!mIsExtendedMode) {
144 throw UnsupportedOperationException(MSG_MSS_COMMAND_NOT_SUPPORTED);
145 }
146
147 checkSecureSession();
148
149 if (!mIsEncryptionActive) {
150 throw IllegalStateException(MSG_ENCRYPTION_NOT_ACTIVE);
151 }
152
153 /*
154 * Add a new command or update the last command if it is an MSS command.
155 */
156 if (!mCommands.empty()
157 && mCommands[mCommands.size() - 1]->getCommandRef()
158 == CardCommandRef::MANAGE_SECURE_SESSION) {
159 auto command = mCommands[mCommands.size() - 1];
160 auto session
161 = std::dynamic_pointer_cast<CommandManageSession>(command);
162 session->setEncryptionRequested(false);
163
164 } else {
165 auto session = std::make_shared<CommandManageSession>(
166 mTransactionContext, getCommandContext());
167 session->setEncryptionRequested(false);
168 mCommands.push_back(session);
169 }
170
171 mIsEncryptionActive = false;
172
173 } catch (...) {
174 resetTransaction();
175 throw;
176 }
177
178 return *this;
179}
180
181} /* namespace calypso */
182} /* namespace card */
183} /* namespace keyple */