keyple-card-calypso-crypto-legacysam-cpp-lib - 1.0.2
Component of the Keyple C++ middleware
CommandDataCipher.cpp
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (c) 2023 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/CommandDataCipher.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/CounterOverflowException.hpp"
23#include "keyple/card/calypso/crypto/legacysam/DataAccessException.hpp"
24#include "keyple/card/calypso/crypto/legacysam/IllegalParameterException.hpp"
25#include "keyple/core/util/ApduUtil.hpp"
26#include "keyple/core/util/cpp/Arrays.hpp"
27#include "keyple/core/util/cpp/System.hpp"
28#include "keypop/calypso/crypto/legacysam/transaction/InvalidSignatureException.hpp"
29
30namespace keyple {
31namespace card {
32namespace calypso {
33namespace crypto {
34namespace legacysam {
35
36using keyple::core::util::ApduUtil;
37using keyple::core::util::cpp::Arrays;
38using keyple::core::util::cpp::System;
39using keypop::calypso::crypto::legacysam::transaction::
40 InvalidSignatureException;
41
42const std::map<int, const std::shared_ptr<StatusProperties>>
43 CommandDataCipher::STATUS_TABLE = [] {
44 std::map<int, const std::shared_ptr<StatusProperties>> m(
45 Command::STATUS_TABLE);
46
47 m.insert(
48 {{0x6700,
49 std::make_shared<StatusProperties>(
50 "Incorrect Lc", typeid(IllegalParameterException))},
51 {0x6900,
52 std::make_shared<StatusProperties>(
53 "An event counter cannot be incremented",
54 typeid(CounterOverflowException))},
55
56 {0x6985,
57 std::make_shared<StatusProperties>(
58 std::string("Preconditions not satisfied:\n")
59 + "- The SAM is locked.\n"
60 + "- Cipher or sign forbidden (DataCipherEnableBit of "
61 + "PAR5 is 0).\n"
62 + "- Ciphering or signing mode, and ciphering forbidden"
63 + "(CipherEnableBit of PAR1 is 0).\n"
64 + "- Decipher mode, and deciphering forbidden "
65 + "(DecipherDataEnableBit of PAR1 is 0).\n" + "- AES key",
66 typeid(AccessForbiddenException))},
67 {0x6A83,
68 std::make_shared<StatusProperties>(
69 "Record not found: ciphering key not found",
70 typeid(DataAccessException))},
71 {0x6B00,
72 std::make_shared<StatusProperties>(
73 "Incorrect P1", typeid(IllegalParameterException))}});
74 return m;
75 }();
76
77CommandDataCipher::CommandDataCipher(
78 std::shared_ptr<CommandContextDto> context,
79 std::shared_ptr<BasicSignatureComputationDataAdapter>
80 signatureComputationData,
81 std::shared_ptr<BasicSignatureVerificationDataAdapter>
82 signatureVerificationData)
83: Command(CommandRef::DATA_CIPHER, 0, context)
84, mSignatureComputationData(signatureComputationData)
85, mSignatureVerificationData(signatureVerificationData)
86{
87 const std::uint8_t cla = context->getTargetSam()->getClassByte();
88 const std::uint8_t inst = getCommandRef().getInstructionByte();
89 const std::uint8_t p1 = 0x40;
90 const std::uint8_t p2 = 0x00;
91
92 std::vector<std::uint8_t> dataIn(0);
93
94 if (signatureComputationData != nullptr) {
95 dataIn.resize(2 + signatureComputationData->getData().size());
96 dataIn[0] = signatureComputationData->getKif();
97 dataIn[1] = signatureComputationData->getKvc();
98 System::arraycopy(
99 signatureComputationData->getData(),
100 0,
101 dataIn,
102 2,
103 signatureComputationData->getData().size());
104
105 } else if (signatureVerificationData != nullptr) {
106 dataIn.resize(2 + signatureVerificationData->getData().size());
107 dataIn[0] = signatureVerificationData->getKif();
108 dataIn[1] = signatureVerificationData->getKvc();
109 System::arraycopy(
110 signatureVerificationData->getData(),
111 0,
112 dataIn,
113 2,
114 signatureVerificationData->getData().size());
115 }
116
117 const std::vector<uint8_t> apdu
118 = signatureVerificationData == nullptr
119 && signatureComputationData == nullptr
120 ? ApduUtil::build(cla, inst, p1, p2)
121 : ApduUtil::build(cla, inst, p1, p2, dataIn);
122
123 setApduRequest(std::make_shared<ApduRequestAdapter>(apdu));
124}
125
126void
127CommandDataCipher::finalizeRequest()
128{
129 /* nothing to do */
130}
131
132bool
133CommandDataCipher::isControlSamRequiredToFinalizeRequest() const
134{
135 return false;
136}
137
138void
139CommandDataCipher::parseResponse(std::shared_ptr<ApduResponseApi> apduResponse)
140{
141 setResponseAndCheckStatus(apduResponse);
142 if (apduResponse->getDataOut().size() > 0) {
143 if (mSignatureComputationData != nullptr) {
144 mSignatureComputationData->setSignature(
145 Arrays::copyOfRange(
146 apduResponse->getDataOut(),
147 0,
148 mSignatureComputationData->getSignatureSize()));
149
150 } else if (mSignatureVerificationData != nullptr) {
151 const std::vector<std::uint8_t> computedSignature
152 = Arrays::copyOfRange(
153 apduResponse->getDataOut(),
154 0,
155 mSignatureVerificationData->getSignature().size());
156 mSignatureVerificationData->setSignatureValid(
157 Arrays::equals(
158 computedSignature,
159 mSignatureVerificationData->getSignature()));
160 }
161 if (mSignatureVerificationData != nullptr
162 && !mSignatureVerificationData->isSignatureValid()) {
163 throw InvalidSignatureException("Invalid signature");
164 }
165 }
166}
167
168const std::map<int, const std::shared_ptr<StatusProperties>>&
169CommandDataCipher::getStatusTable() const
170{
171 return STATUS_TABLE;
172}
173
174} /* namespace legacysam */
175} /* namespace crypto */
176} /* namespace calypso */
177} /* namespace card */
178} /* namespace keyple */