Keyple Card Calypso C++ Library 2.1.0
Reference Terminal Reader API for C++
CmdCardSvGet.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 "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 std::shared_ptr<CalypsoCard> calypsoCard,
46 const SvOperation svOperation)
47: AbstractCardCommand(mCommand)
48{
49 const uint8_t cla = calypsoCardClass == CalypsoCardClass::LEGACY ?
52
53 /* CL-SV-CMDMODE.1 Requirement fullfilled only for SAM C1 */
54 const uint8_t p1 = calypsoCard->isExtendedModeSupported() ? 0x01 : 0x00;
55 const uint8_t p2 = svOperation == SvOperation::RELOAD ? 0x07 : 0x09;
56
58 std::make_shared<ApduRequestAdapter>(
59 ApduUtil::build(cla, mCommand.getInstructionByte(), p1, p2, 0x00)));
60
61 std::stringstream ss;
62 ss << "OPERATION:" << svOperation;
63 addSubName(ss.str());
64
65 mHeader = std::vector<uint8_t>(4);
66 mHeader[0] = mCommand.getInstructionByte();
67 mHeader[1] = p1;
68 mHeader[2] = p2;
69 mHeader[3] = 0x00;
70}
71
73{
74 return false;
75}
76
77CmdCardSvGet& CmdCardSvGet::setApduResponse(const std::shared_ptr<ApduResponseApi> apduResponse)
78{
80
81 const std::vector<uint8_t> cardResponse = apduResponse->getDataOut();
82
83 switch (cardResponse.size()) {
84 case 0x21: /* Compatibility mode, Reload */
85 case 0x1E: /* Compatibility mode, Debit or Undebit */
86 mChallengeOut = std::vector<uint8_t>(2);
87 mPreviousSignatureLo = std::vector<uint8_t>(3);
88 mCurrentKVC = cardResponse[0];
89 mTransactionNumber = ByteArrayUtil::twoBytesToInt(cardResponse, 1);
90 System::arraycopy(cardResponse, 3, mPreviousSignatureLo, 0, 3);
91 mChallengeOut[0] = cardResponse[6];
92 mChallengeOut[1] = cardResponse[7];
93 mBalance = ByteArrayUtil::threeBytesSignedToInt(cardResponse, 8);
94 if (cardResponse.size() == 0x21) {
95 /* Reload */
96 mLoadLog = std::make_shared<SvLoadLogRecordAdapter>(cardResponse, 11);
97 mDebitLog = nullptr;
98 } else {
99 /* Debit */
100 mLoadLog = nullptr;
101 mDebitLog = std::make_shared<SvDebitLogRecordAdapter>(cardResponse, 11);
102 }
103 break;
104 case 0x3D: /* Revision 3.2 mode */
105 mChallengeOut = std::vector<uint8_t>(8);
106 mPreviousSignatureLo = std::vector<uint8_t>(6);
107 System::arraycopy(cardResponse, 0, mChallengeOut, 0, 8);
108 mCurrentKVC = cardResponse[8];
109 mTransactionNumber = ByteArrayUtil::twoBytesToInt(cardResponse, 9);
110 System::arraycopy(cardResponse, 11, mPreviousSignatureLo, 0, 6);
111 mBalance = ByteArrayUtil::threeBytesSignedToInt(cardResponse, 17);
112 mLoadLog = std::make_shared<SvLoadLogRecordAdapter>(cardResponse, 20);
113 mDebitLog = std::make_shared<SvDebitLogRecordAdapter>(cardResponse, 42);
114 break;
115 default:
116 throw IllegalStateException("Incorrect data length in response to SVGet");
117 }
118
119 return *this;
120}
121
122const std::vector<uint8_t>& CmdCardSvGet::getSvGetCommandHeader() const
123{
124 return mHeader;
125}
126
128{
129 return mCurrentKVC;
130}
131
133{
134 return mTransactionNumber;
135}
136
137const std::vector<uint8_t>& CmdCardSvGet::getPreviousSignatureLo() const
138{
139 return mPreviousSignatureLo;
140}
141
142const std::vector<uint8_t>& CmdCardSvGet::getChallengeOut() const
143{
144 return mChallengeOut;
145}
146
148{
149 return mBalance;
150}
151
152const std::shared_ptr<SvLoadLogRecord> CmdCardSvGet::getLoadLog() const
153{
154 return mLoadLog;
155}
156
157const std::shared_ptr<SvDebitLogRecord> CmdCardSvGet::getDebitLog() const
158{
159 return mDebitLog;
160}
161
162const std::map<const int, const std::shared_ptr<StatusProperties>>
163 CmdCardSvGet::initStatusTable()
164{
165 std::map<const int, const std::shared_ptr<StatusProperties>> m =
167
168 m.insert({0x6982,
169 std::make_shared<StatusProperties>("Security conditions not fulfilled.",
171 m.insert({0x6985,
172 std::make_shared<StatusProperties>("Preconditions not satisfied (a store value " \
173 "operation was already done in the current " \
174 "session).",
175 typeid(CalypsoSamAccessForbiddenException))});
176 m.insert({0x6A81,
177 std::make_shared<StatusProperties>("Incorrect P1 or P2.",
178 typeid(CardIllegalParameterException))});
179 m.insert({0x6A86,
180 std::make_shared<StatusProperties>("Le inconsistent with P2.",
181 typeid(CardIllegalParameterException))});
182 m.insert({0x6D00,
183 std::make_shared<StatusProperties>("SV function not present.",
184 typeid(CardIllegalParameterException))});
185
186 return m;
187}
188
189const std::map<const int, const std::shared_ptr<StatusProperties>>& CmdCardSvGet::getStatusTable()
190 const
191{
192 return STATUS_TABLE;
193}
194
195}
196}
197}
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
CmdCardSvGet(const CalypsoCardClass calypsoCardClass, const std::shared_ptr< CalypsoCard > calypsoCard, const SvOperation svOperation)
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
const std::map< const int, const std::shared_ptr< StatusProperties > > & getStatusTable() const override
CmdCardSvGet & setApduResponse(const std::shared_ptr< ApduResponseApi > apduResponse) override