Keyple Card Calypso C++ Library 2.1.0
Reference Terminal Reader API for C++
CmdCardCloseSession.cpp
Go to the documentation of this file.
1/**************************************************************************************************
2 * Copyright (c) 2021 Calypso Networks Association https://calypsonet.org/ *
3 * *
4 * See the NOTICE file(s) distributed with this work for additional information regarding *
5 * copyright ownership. *
6 * *
7 * This program and the accompanying materials are made available under the terms of the Eclipse *
8 * Public License 2.0 which is available at http://www.eclipse.org/legal/epl-2.0 *
9 * *
10 * SPDX-License-Identifier: EPL-2.0 *
11 **************************************************************************************************/
12
13#include "CmdCardCloseSession.h"
14
15#include <sstream>
16
17/* Keyple Core Util */
18#include "ApduUtil.h"
19#include "Arrays.h"
20#include "ByteArrayUtil.h"
21#include "IllegalArgumentException.h"
22
23/* Keyple Card Calypso */
24#include "CalypsoCardAdapter.h"
28
29namespace keyple {
30namespace card {
31namespace calypso {
32
33using namespace keyple::core::util;
34using namespace keyple::core::util::cpp;
35
36const CalypsoCardCommand CmdCardCloseSession::mCommand = CalypsoCardCommand::CLOSE_SESSION;
37const std::map<const int, const std::shared_ptr<StatusProperties>>
38 CmdCardCloseSession::STATUS_TABLE = initStatusTable();
39
40CmdCardCloseSession::CmdCardCloseSession(const std::shared_ptr<CalypsoCard> calypsoCard,
41 const bool ratificationAsked,
42 const std::vector<uint8_t> terminalSessionSignature)
43: AbstractCardCommand(mCommand), mCalypsoCard(calypsoCard)
44{
45 /* The optional parameter terminalSessionSignature could contain 4 or 8 bytes */
46 if (!terminalSessionSignature.empty() &&
47 terminalSessionSignature.size() != 4 &&
48 terminalSessionSignature.size() != 8) {
49 throw IllegalArgumentException("Invalid terminal sessionSignature: " +
50 ByteArrayUtil::toHex(terminalSessionSignature));
51 }
52
53 const uint8_t p1 = ratificationAsked ? 0x80 : 0x00;
54
55 /*
56 * Case 4: this command contains incoming and outgoing data. We define le = 0, the actual
57 * length will be processed by the lower layers.
58 */
59 const uint8_t le = 0;
60
62 std::make_shared<ApduRequestAdapter>(
63 ApduUtil::build(
64 std::dynamic_pointer_cast<CalypsoCardAdapter>(calypsoCard)
65 ->getCardClass().getValue(),
66 mCommand.getInstructionByte(),
67 p1,
68 0x00,
69 terminalSessionSignature,
70 le)));
71}
72
73CmdCardCloseSession::CmdCardCloseSession(const std::shared_ptr<CalypsoCard> calypsoCard)
74: AbstractCardCommand(mCommand), mCalypsoCard(calypsoCard)
75{
76 /* CL-CSS-ABORTCMD.1 */
78 std::make_shared<ApduRequestAdapter>(
79 ApduUtil::build(
80 std::dynamic_pointer_cast<CalypsoCardAdapter>(calypsoCard)
81 ->getCardClass().getValue(),
82 mCommand.getInstructionByte(),
83 0x00,
84 0x00,
85 0)));
86}
87
89{
90 return false;
91}
92
94 const std::shared_ptr<ApduResponseApi> apduResponse)
95{
97
98 const std::vector<uint8_t> responseData = getApduResponse()->getDataOut();
99
100 if (mCalypsoCard->isExtendedModeSupported()) {
101 /* 8-byte signature */
102 if (responseData.size() == 8) {
103 /* Signature only */
104 mSignatureLo = Arrays::copyOfRange(responseData, 0, 8);
105 mPostponedData = std::vector<uint8_t>(0);
106 } else if (responseData.size() == 12) {
107 /* Signature + 3 postponed bytes (+1) */
108 mSignatureLo = Arrays::copyOfRange(responseData, 4, 12);
109 mPostponedData = Arrays::copyOfRange(responseData, 1, 4);
110 } else if (responseData.size() == 15) {
111 /* Signature + 6 postponed bytes (+1) */
112 mSignatureLo = Arrays::copyOfRange(responseData, 7, 15);
113 mPostponedData = Arrays::copyOfRange(responseData, 1, 7);
114 } else {
115 if (responseData.size() != 0) {
116 throw IllegalArgumentException("Unexpected length in response to CloseSecureSession " \
117 "command: " +
118 std::to_string(responseData.size()));
119 }
120
121 /* Session abort case */
122 mSignatureLo = std::vector<uint8_t>(0);
123 mPostponedData = std::vector<uint8_t>(0);
124 }
125 } else {
126 /* 4-byte signature */
127 if (responseData.size() == 4) {
128 /* Signature only */
129 mSignatureLo = Arrays::copyOfRange(responseData, 0, 4);
130 mPostponedData = std::vector<uint8_t>(0);
131 } else if (responseData.size() == 8) {
132 /* Signature + 3 postponed bytes (+1) */
133 mSignatureLo = Arrays::copyOfRange(responseData, 4, 8);
134 mPostponedData = Arrays::copyOfRange(responseData, 1, 4);
135 } else if (responseData.size() == 11) {
136 /* Signature + 6 postponed bytes (+1) */
137 mSignatureLo = Arrays::copyOfRange(responseData, 7, 11);
138 mPostponedData = Arrays::copyOfRange(responseData, 1, 7);
139 } else {
140 if (responseData.size() != 0) {
141 throw IllegalArgumentException("Unexpected length in response to CloseSecureSession " \
142 "command: " +
143 std::to_string(responseData.size()));
144 }
145
146 /* Session abort case */
147 mSignatureLo = std::vector<uint8_t>(0);
148 mPostponedData = std::vector<uint8_t>(0);
149 }
150 }
151
152 return *this;
153}
154
155const std::vector<uint8_t>& CmdCardCloseSession::getSignatureLo() const
156{
157 return mSignatureLo;
158}
159
160const std::vector<uint8_t>& CmdCardCloseSession::getPostponedData() const
161{
162 return mPostponedData;
163}
164
165const std::map<const int, const std::shared_ptr<StatusProperties>>
166 CmdCardCloseSession::initStatusTable()
167{
168 std::map<const int, const std::shared_ptr<StatusProperties>> m =
170
171 m.insert({0x6700,
172 std::make_shared<StatusProperties>("Lc signatureLo not supported (e.g. Lc=4 with a " \
173 "Revision 3.2 mode for Open Secure Session).",
175 m.insert({0x6B00,
176 std::make_shared<StatusProperties>("P1 or P2 signatureLo not supported.",
177 typeid(CardIllegalParameterException))});
178 m.insert({0x6988,
179 std::make_shared<StatusProperties>("incorrect signatureLo.",
180 typeid(CardSecurityDataException))});
181 m.insert({0x6985,
182 std::make_shared<StatusProperties>("No session was opened.",
183 typeid(CardAccessForbiddenException))});
184
185 return m;
186}
187
188const std::map<const int, const std::shared_ptr<StatusProperties>>&
190{
191 return STATUS_TABLE;
192}
193
194}
195}
196}
static const std::map< const int, const std::shared_ptr< StatusProperties > > STATUS_TABLE
virtual const std::shared_ptr< ApduResponseApi > getApduResponse() const final
virtual void setApduRequest(const std::shared_ptr< ApduRequestAdapter > apduRequest) final
AbstractCardCommand & setApduResponse(const std::shared_ptr< ApduResponseApi > apduResponse) override
static const CalypsoCardCommand CLOSE_SESSION
const std::vector< uint8_t > & getSignatureLo() const
const std::map< const int, const std::shared_ptr< StatusProperties > > & getStatusTable() const override
CmdCardCloseSession(const std::shared_ptr< CalypsoCard > calypsoCard, const bool ratificationAsked, const std::vector< uint8_t > terminalSessionSignature)
CmdCardCloseSession & setApduResponse(const std::shared_ptr< ApduResponseApi > apduResponse) override
const std::vector< uint8_t > & getPostponedData() const