Keyple Card Calypso C++ Library 2.1.0
Reference Terminal Reader API for C++
CmdCardSvDebit.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 "CmdCardSvDebit.h"
14
15/* Keyple Card Calypso */
16#include "CalypsoCardAdapter.h"
17#include "CalypsoCardClass.h"
23#include "SamUtilAdapter.h"
24
25/* Keyple Core Util */
26#include "ApduUtil.h"
27#include "IllegalArgumentException.h"
28#include "IllegalStateException.h"
29#include "System.h"
30
31namespace keyple {
32namespace card {
33namespace calypso {
34
35using namespace keyple::core::util;
36using namespace keyple::core::util::cpp;
37using namespace keyple::core::util::cpp::exception;
38
39const CalypsoCardCommand CmdCardSvDebit::mCommand = CalypsoCardCommand::SV_DEBIT;
40
41const std::map<const int, const std::shared_ptr<StatusProperties>>
42 CmdCardSvDebit::STATUS_TABLE = initStatusTable();
43
44CmdCardSvDebit::CmdCardSvDebit(const std::shared_ptr<CalypsoCard> calypsoCard,
45 const int amount,
46 const uint8_t kvc,
47 const std::vector<uint8_t>& date,
48 const std::vector<uint8_t>& time)
49: AbstractCardCommand(mCommand)
50{
51 /*
52 * @see Calypso Layer ID 8.02 (200108)
53 * CL-SV-DEBITVAL.1
54 */
55 if (amount < 0 || amount > 32767) {
56 throw IllegalArgumentException("Amount is outside allowed boundaries (0 <= amount <= " \
57 "32767)");
58 }
59
60 if (date.empty() || time.empty()) {
61 throw IllegalArgumentException("date and time cannot be null");
62 }
63
64 if (date.size() != 2 || time.size() != 2) {
65 throw IllegalArgumentException("date and time must be 2-byte arrays");
66 }
67
68 /* Keeps a copy of these fields until the command is finalized */
69 mCalypsoCard = calypsoCard;
70
71 /*
72 * Handle the dataIn size with signatureHi length according to card product type (3.2 rev have a
73 * 10-byte signature)
74 */
75 mDataIn = std::vector<uint8_t>(15 + (mCalypsoCard->isExtendedModeSupported() ? 10 : 5));
76
77 /* mDataIn[0] will be filled in at the finalization phase */
78 const short amountShort = static_cast<short>(-amount);
79 mDataIn[1] = ((amountShort >> 8) & 0xFF);
80 mDataIn[2] = (amountShort & 0xFF);
81 mDataIn[3] = date[0];
82 mDataIn[4] = date[1];
83 mDataIn[5] = time[0];
84 mDataIn[6] = time[1];
85 mDataIn[7] = kvc;
86 /* mDataIn[8]..dataIn[8+7+sigLen] will be filled in at the finalization phase */
87}
88
89void CmdCardSvDebit::finalizeCommand(const std::vector<uint8_t>& debitComplementaryData)
90{
91 if ((mCalypsoCard->isExtendedModeSupported() && debitComplementaryData.size() != 20) ||
92 (!mCalypsoCard->isExtendedModeSupported() && debitComplementaryData.size() != 15)) {
93 throw IllegalArgumentException("Bad SV prepare load data length.");
94 }
95
96 const uint8_t p1 = debitComplementaryData[4];
97 const uint8_t p2 = debitComplementaryData[5];
98
99 mDataIn[0] = debitComplementaryData[6];
100 System::arraycopy(debitComplementaryData, 0, mDataIn, 8, 4);
101 System::arraycopy(debitComplementaryData, 7, mDataIn, 12, 3);
102 System::arraycopy(debitComplementaryData, 10, mDataIn, 15, debitComplementaryData.size() - 10);
103
104 const auto adapter = std::dynamic_pointer_cast<CalypsoCardAdapter>(mCalypsoCard);
105 const uint8_t cardClass = adapter->getCardClass() == CalypsoCardClass::LEGACY ?
108
110 std::make_shared<ApduRequestAdapter>(
111 ApduUtil::build(cardClass,
112 mCommand.getInstructionByte(),
113 p1,
114 p2,
115 mDataIn)));
116}
117
118const std::vector<uint8_t> CmdCardSvDebit::getSvDebitData() const
119{
120 std::vector<uint8_t> svDebitData(12);
121
122 svDebitData[0] = mCommand.getInstructionByte();
123
124 /*
125 * svDebitData[1,2] / P1P2 not set because ignored
126 * Lc is 5 bytes longer in product type 3.2
127 */
128 svDebitData[3] = mCalypsoCard->isExtendedModeSupported() ? 0x19 : 0x14;
129
130 /* Appends the fixed part of dataIn */
131 System::arraycopy(mDataIn, 0, svDebitData, 4, 8);
132
133 return svDebitData;
134}
135
137{
138 return true;
139}
140
141CmdCardSvDebit& CmdCardSvDebit::setApduResponse(const std::shared_ptr<ApduResponseApi> apduResponse)
142{
144
145 const std::vector<uint8_t> dataOut = apduResponse->getDataOut();
146 if (dataOut.size() != 0 && dataOut.size() != 3 && dataOut.size() != 6) {
147 throw IllegalStateException("Bad length in response to SV Reload command.");
148 }
149
150 return *this;
151}
152
153const std::vector<uint8_t> CmdCardSvDebit::getSignatureLo() const
154{
155 return getApduResponse()->getDataOut();
156}
157
158const std::map<const int, const std::shared_ptr<StatusProperties>>
159 CmdCardSvDebit::initStatusTable()
160{
161 std::map<const int, const std::shared_ptr<StatusProperties>> m =
163
164 m.insert({0x6400,
165 std::make_shared<StatusProperties>("Too many modifications in session.",
167 m.insert({0x6700,
168 std::make_shared<StatusProperties>("Lc value not supported.",
169 typeid(CardIllegalParameterException))});
170 m.insert({0x6900,
171 std::make_shared<StatusProperties>("Transaction counter is 0 or SV TNum is FFFEh or" \
172 " FFFFh.",
173 typeid(CalypsoSamCounterOverflowException))});
174 m.insert({0x6985,
175 std::make_shared<StatusProperties>("Preconditions not satisfied.",
176 typeid(CalypsoSamAccessForbiddenException))});
177 m.insert({0x6988,
178 std::make_shared<StatusProperties>("Incorrect signatureHi.",
179 typeid(CardSecurityDataException))});
180 m.insert({0x6200,
181 std::make_shared<StatusProperties>("Successful execution, response data postponed " \
182 "until session closing.",
183 typeid(nullptr))});
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 CalypsoCardClass LEGACY
static const CalypsoCardClass ISO
static const CalypsoCardClass LEGACY_STORED_VALUE
static const CalypsoCardCommand SV_DEBIT
const std::vector< uint8_t > getSvDebitData() const
void finalizeCommand(const std::vector< uint8_t > &debitComplementaryData)
CmdCardSvDebit & setApduResponse(const std::shared_ptr< ApduResponseApi > apduResponse) override
const std::map< const int, const std::shared_ptr< StatusProperties > > & getStatusTable() const override
CmdCardSvDebit(const std::shared_ptr< CalypsoCard > calypsoCard, const int amount, const uint8_t kvc, const std::vector< uint8_t > &date, const std::vector< uint8_t > &time)
const std::vector< uint8_t > getSignatureLo() const