keyple-card-calypso-crypto-legacysam-cpp-lib - 1.0.2
Component of the Keyple C++ middleware
CommandGetData.cpp
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (c) 2025 Calypso Networks Association https://calypsonet.org/ *
3 * *
4 * See the NOTICE file(s) distributed with this work for additional *
5 * information regarding copyright ownership. *
6 * *
7 * This program and the accompanying materials are made available under the *
8 * terms of the Eclipse Public License 2.0 which is available at *
9 * http://www.eclipse.org/legal/epl-2.0 *
10 * *
11 * SPDX-License-Identifier: EPL-2.0 *
12 ******************************************************************************/
13
14#include "keyple/card/calypso/crypto/legacysam/CommandGetData.hpp"
15
16#include <algorithm>
17#include <map>
18#include <memory>
19#include <vector>
20
21#include "keyple/card/calypso/crypto/legacysam/DataAccessException.hpp"
22#include "keyple/card/calypso/crypto/legacysam/IllegalParameterException.hpp"
23#include "keyple/core/util/ApduUtil.hpp"
24#include "keyple/core/util/cpp/Arrays.hpp"
25
26namespace keyple {
27namespace card {
28namespace calypso {
29namespace crypto {
30namespace legacysam {
31
32using keyple::core::util::ApduUtil;
33using keyple::core::util::cpp::Arrays;
34
35const std::map<int, const std::shared_ptr<StatusProperties>>
36 CommandGetData::STATUS_TABLE = [] {
37 std::map<int, const std::shared_ptr<StatusProperties>> m(
38 Command::STATUS_TABLE);
39
40 m.insert(
41 {{0x6700,
42 std::make_shared<StatusProperties>(
43 "Incorrect Lc", typeid(IllegalParameterException))},
44 {0x6A88,
45 std::make_shared<StatusProperties>(
46 "Data referenced by P1-P2 not available",
47 typeid(DataAccessException))}});
48
49 return m;
50 }();
51
52CommandGetData::CommandGetData(
53 std::shared_ptr<DtoAdapters::CommandContextDto> context, GetDataTag tag)
54: Command(CommandRef::GET_DATA, getExpectedTotalLength(tag), context)
55, mTagInfo(LegacySamConstants::TagInfo::valueOf(name(tag)))
56{
57 setApduRequest(
58 std::make_shared<ApduRequestAdapter>(ApduUtil::build(
59 context->getTargetSam()->getClassByte(),
60 getCommandRef().getInstructionByte(),
61 mTagInfo.getMsb(),
62 mTagInfo.getLsb(),
63 static_cast<uint8_t>(std::min(mTagInfo.getLength(), 255)))));
64}
65
66int
67CommandGetData::getExpectedTotalLength(GetDataTag tag)
68{
69 return LegacySamConstants::TagInfo::valueOf(name(tag)).getTotalLength();
70}
71
72void
73CommandGetData::finalizeRequest()
74{
75 /* nothing to do */
76}
77
78bool
79CommandGetData::isControlSamRequiredToFinalizeRequest() const
80{
81 return false;
82}
83
84void
85CommandGetData::parseResponse(std::shared_ptr<ApduResponseApi> apduResponse)
86{
87 setResponseAndCheckStatus(apduResponse);
88 const std::vector<std::uint8_t> dataOut = apduResponse->getDataOut();
89
90 // check BER-TLV header
91 const std::vector<std::uint8_t> header = mTagInfo.getHeader();
92
93 for (int i = 0; i < static_cast<int>(header.size()); i++) {
94 if (dataOut[i] != header[i]) {
95 throw DataAccessException(
96 "Inconsistent BER-TLV tag. Expected: "
97 + std::to_string(header[i])
98 + "h, Actual: " + std::to_string(dataOut[i]) + "h");
99 }
100 }
101
102 getContext()->getTargetSam()->setCaCertificate(
103 Arrays::copyOfRange(dataOut, header.size(), dataOut.size()));
104}
105
106const std::map<int, const std::shared_ptr<StatusProperties>>&
107CommandGetData::getStatusTable() const
108{
109 return STATUS_TABLE;
110}
111
112} /* namespace legacysam */
113} /* namespace crypto */
114} /* namespace calypso */
115} /* namespace card */
116} /* namespace keyple */