keyple-card-calypso-crypto-legacysam-cpp-lib - 1.0.2
Component of the Keyple C++ middleware
CommandSvPrepareDebitOrUndebit.cpp
Go to the documentation of this file.
1
2/******************************************************************************
3 * Copyright (c) 2025 Calypso Networks Association https://calypsonet.org/ *
4 * *
5 * See the NOTICE file(s) distributed with this work for additional *
6 * information regarding copyright ownership. *
7 * *
8 * This program and the accompanying materials are made available under the *
9 * terms of the Eclipse Public License 2.0 which is available at *
10 * http://www.eclipse.org/legal/epl-2.0 *
11 * *
12 * SPDX-License-Identifier: EPL-2.0 *
13 ******************************************************************************/
14
15#include "keyple/card/calypso/crypto/legacysam/CommandSvPrepareDebitOrUndebit.hpp"
16
17#include <map>
18#include <memory>
19#include <vector>
20
21#include "keyple/card/calypso/crypto/legacysam/AccessForbiddenException.hpp"
22#include "keyple/card/calypso/crypto/legacysam/DataAccessException.hpp"
23#include "keyple/card/calypso/crypto/legacysam/IllegalParameterException.hpp"
24#include "keyple/card/calypso/crypto/legacysam/IncorrectInputDataException.hpp"
25#include "keyple/card/calypso/crypto/legacysam/LegacySamConstants.hpp"
26#include "keyple/card/calypso/crypto/legacysam/SecurityDataException.hpp"
27#include "keyple/core/util/ApduUtil.hpp"
28#include "keyple/core/util/ByteArrayUtil.hpp"
29#include "keyple/core/util/cpp/Arrays.hpp"
30#include "keyple/core/util/cpp/System.hpp"
31#include "keyple/core/util/cpp/exception/IllegalArgumentException.hpp"
32#include "keypop/calypso/crypto/legacysam/transaction/InvalidSignatureException.hpp"
33
34namespace keyple {
35namespace card {
36namespace calypso {
37namespace crypto {
38namespace legacysam {
39
40using keyple::core::util::ApduUtil;
41using keyple::core::util::ByteArrayUtil;
42using keyple::core::util::cpp::Arrays;
43using keyple::core::util::cpp::System;
44using keyple::core::util::cpp::exception::IllegalArgumentException;
45using keypop::calypso::crypto::legacysam::transaction::
46 InvalidSignatureException;
47
48const std::map<int, const std::shared_ptr<StatusProperties>>
49 CommandSvPrepareDebitOrUndebit::STATUS_TABLE = [] {
50 std::map<int, const std::shared_ptr<StatusProperties>> m(
51 Command::STATUS_TABLE);
52
53 m.insert(
54 {{0x6700,
55 std::make_shared<StatusProperties>(
56 "Incorrect Lc", typeid(IllegalParameterException))},
57 {0x6985,
58 std::make_shared<StatusProperties>(
59 "Preconditions not satisfied",
60 typeid(AccessForbiddenException))},
61 {0x6A00,
62 std::make_shared<StatusProperties>(
63 "Incorrect P1 or P2", typeid(IllegalParameterException))},
64 {0x6A80,
65 std::make_shared<StatusProperties>(
66 "Incorrect incoming data",
67 typeid(IncorrectInputDataException))},
68 {0x6A83,
69 std::make_shared<StatusProperties>(
70 "Record not found: ciphering key not found",
71 typeid(DataAccessException))}});
72 return m;
73 }();
74
75CommandSvPrepareDebitOrUndebit::CommandSvPrepareDebitOrUndebit(
76 std::shared_ptr<CommandContextDto> context,
77 std::shared_ptr<SvCommandSecurityDataApi> data)
78: Command(
79 data->getSvCommandPartialRequest()[0] == 0xBA
80 ? CommandRef::SV_PREPARE_DEBIT
81 : CommandRef::SV_PREPARE_UNDEBIT,
82 0,
83 context)
84, mData(data)
85{
86 const std::uint8_t cla = context->getTargetSam()->getClassByte();
87 const std::uint8_t inst = getCommandRef().getInstructionByte();
88 const std::uint8_t p1 = 0x01;
89 const std::uint8_t p2 = 0xFF;
90
91 /* header(4) + SvDebit data (12) = 16 bytes */
92 std::vector<std::uint8_t> dataIn(16 + data->getSvGetResponse().size());
93
94 System::arraycopy(data->getSvGetRequest(), 0, dataIn, 0, 4);
95 System::arraycopy(
96 data->getSvGetResponse(),
97 0,
98 dataIn,
99 4,
100 data->getSvGetResponse().size());
101 System::arraycopy(
102 data->getSvCommandPartialRequest(),
103 0,
104 dataIn,
105 4 + data->getSvGetResponse().size(),
106 data->getSvCommandPartialRequest().size());
107
108 setApduRequest(
109 std::make_shared<ApduRequestAdapter>(
110 ApduUtil::build(cla, inst, p1, p2, dataIn)));
111}
112
113void
114CommandSvPrepareDebitOrUndebit::finalizeRequest()
115{
116 /* Nothing to do */
117}
118
119bool
120CommandSvPrepareDebitOrUndebit::isControlSamRequiredToFinalizeRequest() const
121{
122 return false;
123}
124
125void
126CommandSvPrepareDebitOrUndebit::parseResponse(
127 std::shared_ptr<ApduResponseApi> apduResponse)
128{
129 setResponseAndCheckStatus(apduResponse);
130 const std::vector<std::uint8_t> dataOut = apduResponse->getDataOut();
131 mData->setSerialNumber(getContext()->getTargetSam()->getSerialNumber())
132 .setTerminalChallenge(Arrays::copyOfRange(dataOut, 0, 3))
133 .setTransactionNumber(Arrays::copyOfRange(dataOut, 3, 6))
134 .setTerminalSvMac(Arrays::copyOfRange(dataOut, 6, dataOut.size()));
135}
136
137const std::map<int, const std::shared_ptr<StatusProperties>>&
138CommandSvPrepareDebitOrUndebit::getStatusTable() const
139{
140 return STATUS_TABLE;
141}
142
143} /* namespace legacysam */
144} /* namespace crypto */
145} /* namespace calypso */
146} /* namespace card */
147} /* namespace keyple */