Keyple Card Calypso C++ Library 2.2.2
Reference Terminal Reader API for C++
CmdCardSvReload.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 "CmdCardSvReload.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 int CmdCardSvReload::SV_POSTPONED_DATA_IN_SESSION = 0x6200;
40const CalypsoCardCommand CmdCardSvReload::mCommand = CalypsoCardCommand::SV_RELOAD;
41
42const std::map<const int, const std::shared_ptr<StatusProperties>>
43 CmdCardSvReload::STATUS_TABLE = initStatusTable();
44
46 const int amount,
47 const uint8_t kvc,
48 const std::vector<uint8_t>& date,
49 const std::vector<uint8_t>& time,
50 const std::vector<uint8_t>& free,
51 const bool isExtendedModeAllowed)
52: AbstractCardCommand(mCommand, 0),
53 /* Keeps a copy of these fields until the builder is finalized */
54 mCalypsoCardClass(calypsoCardClass),
55 mIsExtendedModeAllowed(isExtendedModeAllowed)
56{
57 if (amount < -8388608 || amount > 8388607) {
58 throw IllegalArgumentException("Amount is outside allowed boundaries (-8388608 <= amount " \
59 "<= 8388607)");
60 }
61
62 if (date.empty() || time.empty() || free.empty()) {
63 throw IllegalArgumentException("date, time and free cannot be null");
64 }
65
66 if (date.size() != 2 || time.size() != 2 || free.size() != 2) {
67 throw IllegalArgumentException("date, time and free must be 2-byte arrays");
68 }
69
70 /*
71 * Handle the dataIn size with signatureHi length according to card revision (3.2 rev have a
72 * 10-byte signature)
73 */
74 mDataIn = std::vector<uint8_t>(18 + (isExtendedModeAllowed ? 10 : 5));
75
76 /* dataIn[0] will be filled in at the finalization phase */
77 mDataIn[1] = date[0];
78 mDataIn[2] = date[1];
79 mDataIn[3] = free[0];
80 mDataIn[4] = kvc;
81 mDataIn[5] = free[1];
82 mDataIn[6] = ((amount >> 16) & 0xFF);
83 mDataIn[7] = ((amount >> 8) & 0xFF);
84 mDataIn[8] = (amount & 0xFF);
85 mDataIn[9] = time[0];
86 mDataIn[10] = time[1];
87 /* mDataIn[11]..mDataIn[11+7+sigLen] will be filled in at the finalization phase */
88}
89
90void CmdCardSvReload::finalizeCommand(const std::vector<uint8_t>& reloadComplementaryData)
91{
92 if ((mIsExtendedModeAllowed && reloadComplementaryData.size() != 20) ||
93 (!mIsExtendedModeAllowed && reloadComplementaryData.size() != 15)) {
94 throw IllegalArgumentException("Bad SV prepare load data length.");
95 }
96
97 const uint8_t p1 = reloadComplementaryData[4];
98 const uint8_t p2 = reloadComplementaryData[5];
99
100 mDataIn[0] = reloadComplementaryData[6];
101 System::arraycopy(reloadComplementaryData, 0, mDataIn, 11, 4);
102 System::arraycopy(reloadComplementaryData, 7, mDataIn, 15, 3);
103 System::arraycopy(reloadComplementaryData, 10, mDataIn, 18, reloadComplementaryData.size()-10);
104
105 const uint8_t cardClass = mCalypsoCardClass == CalypsoCardClass::LEGACY ?
108
109 auto apduRequest = std::make_shared<ApduRequestAdapter>(
110 ApduUtil::build(cardClass,
111 mCommand.getInstructionByte(),
112 p1,
113 p2,
114 mDataIn));
115 apduRequest->addSuccessfulStatusWord(SV_POSTPONED_DATA_IN_SESSION);
116 setApduRequest(apduRequest);
117}
118
119const std::vector<uint8_t> CmdCardSvReload::getSvReloadData() const
120{
121 std::vector<uint8_t> svReloadData(15);
122
123 svReloadData[0] = mCommand.getInstructionByte();
124
125 /*
126 * svReloadData[1,2] / P1P2 not set because ignored
127 * Lc is 5 bytes longer in revision 3.2
128 */
129 svReloadData[3] = mIsExtendedModeAllowed ? 0x1C : 0x17;
130
131 /* Appends the fixed part of dataIn */
132 System::arraycopy(mDataIn, 0, svReloadData, 4, 11);
133
134 return svReloadData;
135}
136
138{
139 return true;
140}
141
142CmdCardSvReload& CmdCardSvReload::setApduResponse(const std::shared_ptr<ApduResponseApi> apduResponse)
143{
145
146 const std::vector<uint8_t> dataOut = apduResponse->getDataOut();
147 if (dataOut.size() != 0 && dataOut.size() != 3 && dataOut.size() != 6) {
148 throw IllegalStateException("Bad length in response to SV Reload command.");
149 }
150
151 return *this;
152}
153
154const std::vector<uint8_t> CmdCardSvReload::getSignatureLo() const
155{
156 return getApduResponse()->getDataOut();
157}
158
159const std::map<const int, const std::shared_ptr<StatusProperties>>
160 CmdCardSvReload::initStatusTable()
161{
162 std::map<const int, const std::shared_ptr<StatusProperties>> m =
164
165 m.insert({SV_POSTPONED_DATA_IN_SESSION,
166 std::make_shared<StatusProperties>("Successful execution, response data postponed " \
167 "until session closing.",
168 typeid(nullptr))});
169 m.insert({0x6400,
170 std::make_shared<StatusProperties>("Too many modifications in session.",
171 typeid(CardSessionBufferOverflowException))});
172 m.insert({0x6700,
173 std::make_shared<StatusProperties>("Lc value not supported.",
174 typeid(CardIllegalParameterException))});
175 m.insert({0x6900,
176 std::make_shared<StatusProperties>("Transaction counter is 0 or SV TNum is FFFEh or" \
177 " FFFFh.",
178 typeid(CalypsoSamCounterOverflowException))});
179 m.insert({0x6985,
180 std::make_shared<StatusProperties>("Preconditions not satisfied.",
181 typeid(CalypsoSamAccessForbiddenException))});
182 m.insert({0x6988,
183 std::make_shared<StatusProperties>("Incorrect signatureHi.",
184 typeid(CardSecurityDataException))});
185
186 return m;
187}
188
189const std::map<const int, const std::shared_ptr<StatusProperties>>&
191{
192 return STATUS_TABLE;
193}
194
195}
196}
197}
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_RELOAD
CmdCardSvReload(const CalypsoCardClass calypsoCardClass, const int amount, const uint8_t kvc, const std::vector< uint8_t > &date, const std::vector< uint8_t > &time, const std::vector< uint8_t > &free, const bool isExtendedModeAllowed)
const std::vector< uint8_t > getSvReloadData() const
const std::vector< uint8_t > getSignatureLo() const
CmdCardSvReload & setApduResponse(const std::shared_ptr< ApduResponseApi > apduResponse) override
const std::map< const int, const std::shared_ptr< StatusProperties > > & getStatusTable() const override
void finalizeCommand(const std::vector< uint8_t > &reloadComplementaryData)