keyple-card-calypso-crypto-legacysam-cpp-lib - 1.0.2
Component of the Keyple C++ middleware
CommandSvPrepareLoad.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/CommandSvPrepareLoad.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 CommandSvPrepareLoad::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
75CommandSvPrepareLoad::CommandSvPrepareLoad(
76 std::shared_ptr<CommandContextDto> context,
77 std::shared_ptr<SvCommandSecurityDataApi> data)
78: Command(CommandRef::SV_PREPARE_LOAD, 0, context)
79, mData(data)
80{
81 const std::uint8_t cla = context->getTargetSam()->getClassByte();
82 const std::uint8_t inst = getCommandRef().getInstructionByte();
83 const std::uint8_t p1 = 0x01;
84 const std::uint8_t p2 = 0xFF;
85
86 /* header(4) + SvReload data (15) = 19 bytes */
87 std::vector<std::uint8_t> dataIn(19 + data->getSvGetResponse().size());
88
89 System::arraycopy(data->getSvGetRequest(), 0, dataIn, 0, 4);
90 System::arraycopy(
91 data->getSvGetResponse(),
92 0,
93 dataIn,
94 4,
95 data->getSvGetResponse().size());
96 System::arraycopy(
97 data->getSvCommandPartialRequest(),
98 0,
99 dataIn,
100 4 + data->getSvGetResponse().size(),
101 data->getSvCommandPartialRequest().size());
102
103 setApduRequest(
104 std::make_shared<ApduRequestAdapter>(
105 ApduUtil::build(cla, inst, p1, p2, dataIn)));
106}
107
108void
109CommandSvPrepareLoad::finalizeRequest()
110{
111 /* Nothing to do */
112}
113
114bool
115CommandSvPrepareLoad::isControlSamRequiredToFinalizeRequest() const
116{
117 return false;
118}
119
120void
121CommandSvPrepareLoad::parseResponse(
122 std::shared_ptr<ApduResponseApi> apduResponse)
123{
124 setResponseAndCheckStatus(apduResponse);
125 const std::vector<std::uint8_t> dataOut = apduResponse->getDataOut();
126 mData->setSerialNumber(getContext()->getTargetSam()->getSerialNumber())
127 .setTerminalChallenge(Arrays::copyOfRange(dataOut, 0, 3))
128 .setTransactionNumber(Arrays::copyOfRange(dataOut, 3, 6))
129 .setTerminalSvMac(Arrays::copyOfRange(dataOut, 6, dataOut.size()));
130}
131
132const std::map<int, const std::shared_ptr<StatusProperties>>&
133CommandSvPrepareLoad::getStatusTable() const
134{
135 return STATUS_TABLE;
136}
137
138} /* namespace legacysam */
139} /* namespace crypto */
140} /* namespace calypso */
141} /* namespace card */
142} /* namespace keyple */