keyple-card-calypso-crypto-legacysam-cpp-lib - 1.0.2
Component of the Keyple C++ middleware
CommandPsoVerifySignature.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/CommandPsoVerifySignature.hpp"
15
16#include <map>
17#include <memory>
18#include <vector>
19
20#include "keyple/card/calypso/crypto/legacysam/AccessForbiddenException.hpp"
21#include "keyple/card/calypso/crypto/legacysam/CounterOverflowException.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/SecurityContextException.hpp"
27#include "keyple/card/calypso/crypto/legacysam/SecurityDataException.hpp"
28#include "keyple/core/util/ApduUtil.hpp"
29#include "keyple/core/util/ByteArrayUtil.hpp"
30#include "keyple/core/util/cpp/Arrays.hpp"
31#include "keyple/core/util/cpp/System.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 keypop::calypso::crypto::legacysam::transaction::
45 InvalidSignatureException;
46
47const std::map<int, const std::shared_ptr<StatusProperties>>
48 CommandPsoVerifySignature::STATUS_TABLE = [] {
49 std::map<int, const std::shared_ptr<StatusProperties>> m(
50 Command::STATUS_TABLE);
51
52 m.insert(
53 {{0x6700,
54 std::make_shared<StatusProperties>(
55 "Incorrect Lc", typeid(IllegalParameterException))},
56 {0x6982,
57 std::make_shared<StatusProperties>(
58 "Busy status: the command is temporarily unavailable",
59 typeid(SecurityContextException))},
60 {0x6985,
61 std::make_shared<StatusProperties>(
62 "Preconditions not satisfied",
63 typeid(AccessForbiddenException))},
64 {0x6988,
65 std::make_shared<StatusProperties>(
66 "Incorrect signature", typeid(SecurityDataException))},
67 {0x6A80,
68 std::make_shared<StatusProperties>(
69 "Incorrect parameters in incoming data",
70 typeid(IncorrectInputDataException))},
71 {0x6A83,
72 std::make_shared<StatusProperties>(
73 "Record not found: signing key not found",
74 typeid(DataAccessException))},
75 {0x6B00,
76 std::make_shared<StatusProperties>(
77 "Incorrect P1 or P2", typeid(IllegalParameterException))}});
78 return m;
79 }();
80
81CommandPsoVerifySignature::CommandPsoVerifySignature(
82 std::shared_ptr<CommandContextDto> context,
83 std::shared_ptr<TraceableSignatureVerificationDataAdapter> data)
84: Command(CommandRef::PSO_VERIFY_SIGNATURE, 0, context)
85, mData(data)
86{
87 const std::uint8_t cla = context->getTargetSam()->getClassByte();
88 const std::uint8_t inst = getCommandRef().getInstructionByte();
89 const std::uint8_t p1 = 0x00;
90 const std::uint8_t p2 = 0xA8;
91
92 /* DataIn */
93 const int messageOffset = data->isSamTraceabilityMode() ? 6 : 4;
94 const int messageSize = static_cast<int>(data->getData().size());
95 const int signatureSize = static_cast<int>(data->getSignature().size());
96 std::vector<std::uint8_t> dataIn(
97 messageOffset + messageSize + signatureSize);
98
99 /*
100 * SignKeyNum: Selection of the key by KIF and KVC given in the incoming
101 * data.
102 */
103 dataIn[0] = 0xFF;
104
105 /* SignKeyRef: KIF and KVC of the signing key. */
106 dataIn[1] = data->getKif();
107 dataIn[2] = data->getKvc();
108
109 /*
110 * OpMode: Operating mode, equal to XYh, with:
111 * X: Mode
112 */
113 std::uint8_t opMode = 0; // %0000 Normal mode
114 if (data->isSamTraceabilityMode()) {
115 if (data->getSamTraceabilityMode()
116 == SamTraceabilityMode::TRUNCATED_SERIAL_NUMBER) {
117 opMode |= 4; // %x100
118 } else {
119 opMode |= 6; // %x110
120 }
121 }
122 if (data->isBusyMode()) {
123 opMode |= 8; // %1xx0
124 }
125 opMode <<= 4;
126 /* Y: Signature size (in bytes) */
127 opMode |= static_cast<std::uint8_t>(signatureSize);
128 dataIn[3] = opMode;
129
130 /*
131 * TraceOffset (optional): Bit offset in MessageIn of the SAM traceability
132 * data.
133 */
134 if (data->isSamTraceabilityMode()) {
135 ByteArrayUtil::copyBytes(data->getTraceabilityOffset(), dataIn, 4, 2);
136 }
137
138 /* MessageIn: Message to sign. */
139 System::arraycopy(data->getData(), 0, dataIn, messageOffset, messageSize);
140
141 /* Signature */
142 System::arraycopy(
143 data->getSignature(),
144 0,
145 dataIn,
146 dataIn.size() - signatureSize,
147 signatureSize);
148
149 setApduRequest(
150 std::make_shared<ApduRequestAdapter>(
151 ApduUtil::build(cla, inst, p1, p2, dataIn)));
152}
153
154void
155CommandPsoVerifySignature::finalizeRequest()
156{
157 /* Nothing to do */
158}
159
160bool
161CommandPsoVerifySignature::isControlSamRequiredToFinalizeRequest() const
162{
163 return false;
164}
165
166void
167CommandPsoVerifySignature::parseResponse(
168 std::shared_ptr<ApduResponseApi> apduResponse)
169{
170 try {
171 setResponseAndCheckStatus(apduResponse);
172 mData->setSignatureValid(true);
173
174 } catch (const SecurityDataException& e) {
175 mData->setSignatureValid(false);
176 throw InvalidSignatureException(
177 "Invalid signature", std::make_shared<SecurityDataException>(e));
178 }
179}
180
181const std::map<int, const std::shared_ptr<StatusProperties>>&
182CommandPsoVerifySignature::getStatusTable() const
183{
184 return STATUS_TABLE;
185}
186
187} /* namespace legacysam */
188} /* namespace crypto */
189} /* namespace calypso */
190} /* namespace card */
191} /* namespace keyple */