Keyple Card Calypso C++ Library 2.2.2
Reference Terminal Reader API for C++
CmdCardSvGet.cpp
Go to the documentation of this file.
1/**************************************************************************************************
2 * Copyright (c) 2022 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 "CmdCardSvGet.h"
14
15#include <sstream>
16
17/* Keyple Core Util */
18#include "ApduUtil.h"
19#include "ByteArrayUtil.h"
20#include "IllegalStateException.h"
21#include "System.h"
22
23/* Keyple Card Calypso */
27#include "CardPinException.h"
33
34namespace keyple {
35namespace card {
36namespace calypso {
37
38using namespace keyple::core::util;
39
40const CalypsoCardCommand CmdCardSvGet::mCommand = CalypsoCardCommand::SV_GET;
41const std::map<const int, const std::shared_ptr<StatusProperties>>
42 CmdCardSvGet::STATUS_TABLE = initStatusTable();
43
45 const SvOperation svOperation,
46 const bool useExtendedMode)
47: AbstractCardCommand(mCommand, 0)
48{
49 const uint8_t cla = calypsoCardClass == CalypsoCardClass::LEGACY ?
52
53 const uint8_t p1 = useExtendedMode ? 0x01 : 0x00;
54 const uint8_t p2 = svOperation == SvOperation::RELOAD ? 0x07 : 0x09;
55
57 std::make_shared<ApduRequestAdapter>(
58 ApduUtil::build(cla, mCommand.getInstructionByte(), p1, p2, 0x00)));
59
60 std::stringstream ss;
61 ss << "OPERATION:" << svOperation;
62 addSubName(ss.str());
63
64 mHeader = std::vector<uint8_t>(4);
65 mHeader[0] = mCommand.getInstructionByte();
66 mHeader[1] = p1;
67 mHeader[2] = p2;
68 mHeader[3] = 0x00;
69}
70
72{
73 return false;
74}
75
76CmdCardSvGet& CmdCardSvGet::setApduResponse(const std::shared_ptr<ApduResponseApi> apduResponse)
77{
79
80 const std::vector<uint8_t> cardResponse = apduResponse->getDataOut();
81
82 switch (cardResponse.size()) {
83 case 0x21: /* Compatibility mode, Reload */
84 case 0x1E: /* Compatibility mode, Debit or Undebit */
85 mChallengeOut = std::vector<uint8_t>(2);
86 mPreviousSignatureLo = std::vector<uint8_t>(3);
87 mCurrentKVC = cardResponse[0];
88 mTransactionNumber = ByteArrayUtil::extractInt(cardResponse, 1, 2, false);
89 System::arraycopy(cardResponse, 3, mPreviousSignatureLo, 0, 3);
90 mChallengeOut[0] = cardResponse[6];
91 mChallengeOut[1] = cardResponse[7];
92 mBalance = ByteArrayUtil::extractInt(cardResponse, 8, 3, true);
93 if (cardResponse.size() == 0x21) {
94 /* Reload */
95 mLoadLog = std::make_shared<SvLoadLogRecordAdapter>(cardResponse, 11);
96 mDebitLog = nullptr;
97 } else {
98 /* Debit */
99 mLoadLog = nullptr;
100 mDebitLog = std::make_shared<SvDebitLogRecordAdapter>(cardResponse, 11);
101 }
102 break;
103 case 0x3D: /* Revision 3.2 mode */
104 mChallengeOut = std::vector<uint8_t>(8);
105 mPreviousSignatureLo = std::vector<uint8_t>(6);
106 System::arraycopy(cardResponse, 0, mChallengeOut, 0, 8);
107 mCurrentKVC = cardResponse[8];
108 mTransactionNumber = ByteArrayUtil::extractInt(cardResponse, 9, 2, false);
109 System::arraycopy(cardResponse, 11, mPreviousSignatureLo, 0, 6);
110 mBalance = ByteArrayUtil::extractInt(cardResponse, 17, 3, true);
111 mLoadLog = std::make_shared<SvLoadLogRecordAdapter>(cardResponse, 20);
112 mDebitLog = std::make_shared<SvDebitLogRecordAdapter>(cardResponse, 42);
113 break;
114 default:
115 throw IllegalStateException("Incorrect data length in response to SVGet");
116 }
117
118 return *this;
119}
120
121const std::vector<uint8_t>& CmdCardSvGet::getSvGetCommandHeader() const
122{
123 return mHeader;
124}
125
127{
128 return mCurrentKVC;
129}
130
132{
133 return mTransactionNumber;
134}
135
136const std::vector<uint8_t>& CmdCardSvGet::getPreviousSignatureLo() const
137{
138 return mPreviousSignatureLo;
139}
140
141const std::vector<uint8_t>& CmdCardSvGet::getChallengeOut() const
142{
143 return mChallengeOut;
144}
145
147{
148 return mBalance;
149}
150
151const std::shared_ptr<SvLoadLogRecord> CmdCardSvGet::getLoadLog() const
152{
153 return mLoadLog;
154}
155
156const std::shared_ptr<SvDebitLogRecord> CmdCardSvGet::getDebitLog() const
157{
158 return mDebitLog;
159}
160
161const std::map<const int, const std::shared_ptr<StatusProperties>>
162 CmdCardSvGet::initStatusTable()
163{
164 std::map<const int, const std::shared_ptr<StatusProperties>> m =
166
167 m.insert({0x6982,
168 std::make_shared<StatusProperties>("Security conditions not fulfilled.",
170 m.insert({0x6985,
171 std::make_shared<StatusProperties>("Preconditions not satisfied (a store value " \
172 "operation was already done in the current " \
173 "session).",
174 typeid(CalypsoSamAccessForbiddenException))});
175 m.insert({0x6A81,
176 std::make_shared<StatusProperties>("Incorrect P1 or P2.",
177 typeid(CardIllegalParameterException))});
178 m.insert({0x6A86,
179 std::make_shared<StatusProperties>("Le inconsistent with P2.",
180 typeid(CardIllegalParameterException))});
181 m.insert({0x6D00,
182 std::make_shared<StatusProperties>("SV function not present.",
183 typeid(CardIllegalParameterException))});
184
185 return m;
186}
187
188const std::map<const int, const std::shared_ptr<StatusProperties>>& CmdCardSvGet::getStatusTable()
189 const
190{
191 return STATUS_TABLE;
192}
193
194}
195}
196}
static const std::map< const int, const std::shared_ptr< StatusProperties > > STATUS_TABLE
virtual void addSubName(const std::string &subName) final
virtual void setApduRequest(const std::shared_ptr< ApduRequestAdapter > apduRequest) final
AbstractCardCommand & setApduResponse(const std::shared_ptr< ApduResponseApi > apduResponse) override
static const CalypsoCardClass LEGACY
static const CalypsoCardClass ISO
static const CalypsoCardClass LEGACY_STORED_VALUE
static const CalypsoCardCommand SV_GET
const std::shared_ptr< SvLoadLogRecord > getLoadLog() const
bool isSessionBufferUsed() const override
const std::shared_ptr< SvDebitLogRecord > getDebitLog() const
const std::vector< uint8_t > & getChallengeOut() const
const std::vector< uint8_t > & getSvGetCommandHeader() const
const std::vector< uint8_t > & getPreviousSignatureLo() const
CmdCardSvGet(const CalypsoCardClass calypsoCardClass, const SvOperation svOperation, const bool useExtendedMode)
const std::map< const int, const std::shared_ptr< StatusProperties > > & getStatusTable() const override
CmdCardSvGet & setApduResponse(const std::shared_ptr< ApduResponseApi > apduResponse) override