Keyple Card Calypso C++ Library 2.2.2
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 "HexUtil.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, 0), 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 HexUtil::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 */
60 std::make_shared<ApduRequestAdapter>(
61 ApduUtil::build(
62 std::dynamic_pointer_cast<CalypsoCardAdapter>(calypsoCard)
63 ->getCardClass().getValue(),
64 mCommand.getInstructionByte(),
65 p1,
66 0x00,
67 terminalSessionSignature,
68 0)));
69}
70
71CmdCardCloseSession::CmdCardCloseSession(const std::shared_ptr<CalypsoCard> calypsoCard)
72: AbstractCardCommand(mCommand, 0), mCalypsoCard(calypsoCard)
73{
74 /* CL-CSS-ABORTCMD.1 */
76 std::make_shared<ApduRequestAdapter>(
77 ApduUtil::build(
78 std::dynamic_pointer_cast<CalypsoCardAdapter>(calypsoCard)
79 ->getCardClass().getValue(),
80 mCommand.getInstructionByte(),
81 0x00,
82 0x00,
83 0)));
84}
85
87{
88 return false;
89}
90
92 const std::shared_ptr<ApduResponseApi> apduResponse)
93{
95
96 const std::vector<uint8_t> responseData = getApduResponse()->getDataOut();
97
98 if (mCalypsoCard->isExtendedModeSupported()) {
99 /* 8-byte signature */
100 if (responseData.size() == 8) {
101 /* Signature only */
102 mSignatureLo = Arrays::copyOfRange(responseData, 0, 8);
103 mPostponedData = std::vector<uint8_t>(0);
104 } else if (responseData.size() == 12) {
105 /* Signature + 3 postponed bytes (+1) */
106 mSignatureLo = Arrays::copyOfRange(responseData, 4, 12);
107 mPostponedData = Arrays::copyOfRange(responseData, 1, 4);
108 } else if (responseData.size() == 15) {
109 /* Signature + 6 postponed bytes (+1) */
110 mSignatureLo = Arrays::copyOfRange(responseData, 7, 15);
111 mPostponedData = Arrays::copyOfRange(responseData, 1, 7);
112 } else {
113 if (responseData.size() != 0) {
114 throw IllegalArgumentException("Unexpected length in response to CloseSecureSession " \
115 "command: " +
116 std::to_string(responseData.size()));
117 }
118
119 /* Session abort case */
120 mSignatureLo = std::vector<uint8_t>(0);
121 mPostponedData = std::vector<uint8_t>(0);
122 }
123 } else {
124 /* 4-byte signature */
125 if (responseData.size() == 4) {
126 /* Signature only */
127 mSignatureLo = Arrays::copyOfRange(responseData, 0, 4);
128 mPostponedData = std::vector<uint8_t>(0);
129 } else if (responseData.size() == 8) {
130 /* Signature + 3 postponed bytes (+1) */
131 mSignatureLo = Arrays::copyOfRange(responseData, 4, 8);
132 mPostponedData = Arrays::copyOfRange(responseData, 1, 4);
133 } else if (responseData.size() == 11) {
134 /* Signature + 6 postponed bytes (+1) */
135 mSignatureLo = Arrays::copyOfRange(responseData, 7, 11);
136 mPostponedData = Arrays::copyOfRange(responseData, 1, 7);
137 } else {
138 if (responseData.size() != 0) {
139 throw IllegalArgumentException("Unexpected length in response to CloseSecureSession " \
140 "command: " +
141 std::to_string(responseData.size()));
142 }
143
144 /* Session abort case */
145 mSignatureLo = std::vector<uint8_t>(0);
146 mPostponedData = std::vector<uint8_t>(0);
147 }
148 }
149
150 return *this;
151}
152
153const std::vector<uint8_t>& CmdCardCloseSession::getSignatureLo() const
154{
155 return mSignatureLo;
156}
157
158const std::vector<uint8_t>& CmdCardCloseSession::getPostponedData() const
159{
160 return mPostponedData;
161}
162
163const std::map<const int, const std::shared_ptr<StatusProperties>>
164 CmdCardCloseSession::initStatusTable()
165{
166 std::map<const int, const std::shared_ptr<StatusProperties>> m =
168
169 m.insert({0x6700,
170 std::make_shared<StatusProperties>("Lc signatureLo not supported (e.g. Lc=4 with a " \
171 "Revision 3.2 mode for Open Secure Session).",
173 m.insert({0x6B00,
174 std::make_shared<StatusProperties>("P1 or P2 signatureLo not supported.",
175 typeid(CardIllegalParameterException))});
176 m.insert({0x6985,
177 std::make_shared<StatusProperties>("No session was opened.",
178 typeid(CardAccessForbiddenException))});
179 m.insert({0x6988,
180 std::make_shared<StatusProperties>("incorrect signatureLo.",
181 typeid(CardSecurityDataException))});
182
183 return m;
184}
185
186const std::map<const int, const std::shared_ptr<StatusProperties>>&
188{
189 return STATUS_TABLE;
190}
191
192}
193}
194}
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