Keyple Card Calypso C++ Library 2.2.5.6
Reference Terminal Reader API for C++
CmdCardCloseSession.cpp
Go to the documentation of this file.
1/**************************************************************************************************
2 * Copyright (c) 2023 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<CalypsoCardAdapter> calypsoCard,
41 const bool ratificationAsked,
42 const std::vector<uint8_t> terminalSessionSignature)
43: AbstractCardCommand(mCommand, -1, 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
50 throw IllegalArgumentException("Invalid terminal sessionSignature: " +
51 HexUtil::toHex(terminalSessionSignature));
52 }
53
54 const uint8_t p1 = ratificationAsked ? 0x80 : 0x00;
55
56 /*
57 * Case 4: this command contains incoming and outgoing data. We define le = 0, the actual
58 * length will be processed by the lower layers.
59 */
61 std::make_shared<ApduRequestAdapter>(
62 ApduUtil::build(calypsoCard->getCardClass().getValue(),
63 mCommand.getInstructionByte(),
64 p1,
65 0x00,
66 terminalSessionSignature,
67 0)));
68}
69
70CmdCardCloseSession::CmdCardCloseSession(const std::shared_ptr<CalypsoCardAdapter> calypsoCard)
71: AbstractCardCommand(mCommand, 0, calypsoCard)
72{
73 /* CL-CSS-ABORTCMD.1 */
74 // APDU Case 1
76 std::make_shared<ApduRequestAdapter>(
77 ApduUtil::build(calypsoCard->getCardClass().getValue(),
78 mCommand.getInstructionByte(),
79 0x00,
80 0x00,
81 0)));
82}
83
85{
86 return false;
87}
88
89void CmdCardCloseSession::parseApduResponse(const std::shared_ptr<ApduResponseApi> apduResponse)
90{
92
93 const std::vector<uint8_t> responseData = getApduResponse()->getDataOut();
94
95 if (responseData.size() > 0) {
96
97 int signatureLength = getCalypsoCard()->isExtendedModeSupported() ? 8 : 4;
98 int i = 0;
99
100 while (i < static_cast<int>(responseData.size() - signatureLength)) {
101
102 const std::vector<uint8_t> data =
103 Arrays::copyOfRange(responseData, i + 1, i + responseData[i]);
104 mPostponedData.push_back(data);
105 i += responseData[i];
106 }
107
108 mSignatureLo = Arrays::copyOfRange(responseData, i, responseData.size());
109
110 } else {
111
112 /* Session abort case */
113 mSignatureLo = std::vector<uint8_t>(0);
114 }
115}
116
117const std::vector<uint8_t>& CmdCardCloseSession::getSignatureLo() const
118{
119 return mSignatureLo;
120}
121
122const std::vector<std::vector<uint8_t>>& CmdCardCloseSession::getPostponedData() const
123{
124 return mPostponedData;
125}
126
127const std::map<const int, const std::shared_ptr<StatusProperties>>
128 CmdCardCloseSession::initStatusTable()
129{
130 std::map<const int, const std::shared_ptr<StatusProperties>> m =
132
133 m.insert({0x6700,
134 std::make_shared<StatusProperties>("Lc signatureLo not supported (e.g. Lc=4 with a " \
135 "Revision 3.2 mode for Open Secure Session).",
137 m.insert({0x6B00,
138 std::make_shared<StatusProperties>("P1 or P2 signatureLo not supported.",
139 typeid(CardIllegalParameterException))});
140 m.insert({0x6985,
141 std::make_shared<StatusProperties>("No session was opened.",
142 typeid(CardAccessForbiddenException))});
143 m.insert({0x6988,
144 std::make_shared<StatusProperties>("incorrect signatureLo.",
145 typeid(CardSecurityDataException))});
146
147 return m;
148}
149
150const std::map<const int, const std::shared_ptr<StatusProperties>>&
152{
153 return STATUS_TABLE;
154}
155
156}
157}
158}
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
void parseApduResponse(const std::shared_ptr< ApduResponseApi > apduResponse) override
std::shared_ptr< CalypsoCardAdapter > getCalypsoCard() const
static const CalypsoCardCommand CLOSE_SESSION
void parseApduResponse(const std::shared_ptr< ApduResponseApi > apduResponse) override
const std::vector< uint8_t > & getSignatureLo() const
const std::vector< std::vector< uint8_t > > & getPostponedData() const
CmdCardCloseSession(const std::shared_ptr< CalypsoCardAdapter > calypsoCard, const bool ratificationAsked, const std::vector< uint8_t > terminalSessionSignature)
const std::map< const int, const std::shared_ptr< StatusProperties > > & getStatusTable() const override