keyple-card-calypso-crypto-legacysam-cpp-lib - 1.0.2
Component of the Keyple C++ middleware
CommandSamDataCipher.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/CommandSamDataCipher.hpp"
15
16#include <map>
17#include <memory>
18#include <string>
19#include <vector>
20
21#include "keyple/card/calypso/crypto/legacysam/AccessForbiddenException.hpp"
22#include "keyple/card/calypso/crypto/legacysam/CommandRef.hpp"
23#include "keyple/card/calypso/crypto/legacysam/CounterOverflowException.hpp"
24#include "keyple/card/calypso/crypto/legacysam/DataAccessException.hpp"
25#include "keyple/card/calypso/crypto/legacysam/IllegalParameterException.hpp"
26#include "keyple/card/generic/ApduRequestAdapter.hpp"
27#include "keyple/core/util/ApduUtil.hpp"
28#include "keyple/core/util/cpp/System.hpp"
29#include "keyple/core/util/cpp/exception/IllegalArgumentException.hpp"
30
31namespace keyple {
32namespace card {
33namespace calypso {
34namespace crypto {
35namespace legacysam {
36
37using keyple::card::generic::ApduRequestAdapter;
38using keyple::core::util::ApduUtil;
39using keyple::core::util::cpp::System;
40using keyple::core::util::cpp::exception::IllegalArgumentException;
41
43
44const std::map<int, const std::shared_ptr<StatusProperties>>
45 CommandSamDataCipher::STATUS_TABLE = [] {
46 std::map<int, const std::shared_ptr<StatusProperties>> m(
47 Command::STATUS_TABLE);
48
49 m.insert(
50 {{0x6700,
51 std::make_shared<StatusProperties>(
52 "Incorrect Lc", typeid(IllegalParameterException))},
53 {0x6900,
54 std::make_shared<StatusProperties>(
55 "An event counter cannot be incremented",
56 typeid(CounterOverflowException))},
57 {0x6985,
58 std::make_shared<StatusProperties>(
59 std::string("Preconditions not satisfied:\n")
60 + "- The SAM is locked.\n"
61 + "- Ciphering key: ciphering forbidden (CipherEnableBit "
62 "of PAR1 is"
63 + " set to0)",
64 typeid(AccessForbiddenException))},
65 {0x6A00,
66 std::make_shared<StatusProperties>(
67 "Incorrect P1 (!=%0xxxxx0) or P2",
68 typeid(IllegalParameterException))},
69 {0x6A83,
70 std::make_shared<StatusProperties>(
71 "Record not found: deciphering key not found",
72 typeid(DataAccessException))}});
73
74 return m;
75 }();
76
77CommandSamDataCipher::CommandSamDataCipher(
78 std::shared_ptr<DtoAdapters::CommandContextDto> context,
79 int recordNumber,
80 DataType dataType,
81 const std::vector<uint8_t>& plainData)
82: Command(CommandRef::SAM_DATA_CIPHER, 48, context)
83{
84 const uint8_t cla = context->getTargetSam()->getClassByte();
85 const uint8_t inst = getCommandRef().getInstructionByte();
86 const uint8_t p1 = 0;
87
88 uint8_t p2;
89
90 switch (dataType) {
91 case DataType::CAAD_RECORD:
92 p2 = static_cast<uint8_t>(0xE7 + recordNumber);
93 break;
94 case DataType::CEILINGS_FILE_RECORD:
95 p2 = static_cast<uint8_t>(0xB1 + recordNumber);
96 break;
97 case DataType::ONE_CEILING_VALUE:
98 p2 = 0xB8;
99 break;
100 case DataType::PARAMETERS_RECORD:
101 p2 = 0xA0;
102 break;
103 default:
104 throw IllegalArgumentException(
105 "Unsupported DataType: "
106 + std::to_string(static_cast<int>(dataType)));
107 }
108
109 setApduRequest(
110 std::make_shared<ApduRequestAdapter>(
111 ApduUtil::build(cla, inst, p1, p2, plainData)));
112}
113
114const std::map<int, const std::shared_ptr<StatusProperties>>&
115CommandSamDataCipher::getStatusTable() const
116{
117 return STATUS_TABLE;
118}
119
120void
121CommandSamDataCipher::finalizeRequest()
122{
123 /* nothing to do */
124}
125
126bool
127CommandSamDataCipher::isControlSamRequiredToFinalizeRequest() const
128{
129 return false;
130}
131
132void
133CommandSamDataCipher::parseResponse(
134 std::shared_ptr<ApduResponseApi> apduResponse)
135{
136 setResponseAndCheckStatus(apduResponse);
137
138 System::arraycopy(apduResponse->getApdu(), 0, mCipheredData, 0, 48);
139}
140
141const std::vector<uint8_t>&
142CommandSamDataCipher::getCipheredData() const
143{
144 return mCipheredData;
145}
146
147} /* namespace legacysam */
148} /* namespace crypto */
149} /* namespace calypso */
150} /* namespace card */
151} /* namespace keyple */
Command::StatusProperties StatusProperties
Definition: Command.cpp:41