keyple-card-calypso-crypto-legacysam-cpp-lib - 1.0.2
Component of the Keyple C++ middleware
CommandPsoComputeSignature.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/CommandPsoComputeSignature.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/core/util/ApduUtil.hpp"
27#include "keyple/core/util/ByteArrayUtil.hpp"
28#include "keyple/core/util/cpp/Arrays.hpp"
29#include "keyple/core/util/cpp/System.hpp"
30
31namespace keyple {
32namespace card {
33namespace calypso {
34namespace crypto {
35namespace legacysam {
36
37using keyple::core::util::ApduUtil;
38using keyple::core::util::ByteArrayUtil;
39using keyple::core::util::cpp::Arrays;
40using keyple::core::util::cpp::System;
41
42const std::map<int, const std::shared_ptr<StatusProperties>>
43 CommandPsoComputeSignature::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 {0x6985,
56 std::make_shared<StatusProperties>(
57 "Preconditions not satisfied",
58 typeid(AccessForbiddenException))},
59 {0x6A80,
60 std::make_shared<StatusProperties>(
61 "Incorrect value in the incoming data",
62 typeid(IncorrectInputDataException))},
63 {0x6A83,
64 std::make_shared<StatusProperties>(
65 "Record not found: signing key not found",
66 typeid(DataAccessException))},
67 {0x6B00,
68 std::make_shared<StatusProperties>(
69 "Incorrect P1 or P2", typeid(IllegalParameterException))}});
70 return m;
71 }();
72
73CommandPsoComputeSignature::CommandPsoComputeSignature(
74 std::shared_ptr<CommandContextDto> context,
75 std::shared_ptr<TraceableSignatureComputationDataAdapter> data)
76: Command(CommandRef::PSO_COMPUTE_SIGNATURE, 0, context)
77, mData(data)
78{
79 const std::uint8_t cla = context->getTargetSam()->getClassByte();
80 const std::uint8_t inst = getCommandRef().getInstructionByte();
81 const std::uint8_t p1 = 0x9E;
82 const std::uint8_t p2 = 0x9A;
83
84 /* DataIn */
85 const int messageOffset = data->isSamTraceabilityMode() ? 6 : 4;
86 const int messageSize = static_cast<int>(data->getData().size());
87 std::vector<std::uint8_t> dataIn(messageOffset + messageSize);
88
89 /*
90 * SignKeyNum: Selection of the key by KIF and KVC given in the incoming
91 * data.
92 */
93 dataIn[0] = 0xFF;
94
95 /* SignKeyRef: KIF and KVC of the signing key. */
96 dataIn[1] = data->getKif();
97 dataIn[2] = data->getKvc();
98
99 /*
100 * OpMode: Operating mode, equal to XYh, with:
101 * X: Mode
102 */
103 std::uint8_t opMode = 0; // %0000 Normal mode
104 if (data->isSamTraceabilityMode()) {
105 if (data->getSamTraceabilityMode()
106 == SamTraceabilityMode::TRUNCATED_SERIAL_NUMBER) {
107 opMode |= 4; // %x100
108 } else {
109 opMode |= 6; // %x110
110 }
111 }
112 if (data->isBusyMode()) {
113 opMode |= 8; // %1xx0
114 }
115 opMode <<= 4;
116 /* Y: Signature size (in bytes) */
117 opMode |= static_cast<std::uint8_t>(data->getSignatureSize());
118 dataIn[3] = opMode;
119
120 /*
121 * TraceOffset (optional): Bit offset in MessageIn of the SAM traceability
122 * data.
123 */
124 if (data->isSamTraceabilityMode()) {
125 ByteArrayUtil::copyBytes(data->getTraceabilityOffset(), dataIn, 4, 2);
126 }
127
128 /* MessageIn: Message to sign. */
129 System::arraycopy(data->getData(), 0, dataIn, messageOffset, messageSize);
130
131 setApduRequest(
132 std::make_shared<ApduRequestAdapter>(
133 ApduUtil::build(cla, inst, p1, p2, dataIn)));
134}
135
136void
137CommandPsoComputeSignature::finalizeRequest()
138{
139 /* Nothing to do */
140}
141
142bool
143CommandPsoComputeSignature::isControlSamRequiredToFinalizeRequest() const
144{
145 return false;
146}
147
148void
149CommandPsoComputeSignature::parseResponse(
150 std::shared_ptr<ApduResponseApi> apduResponse)
151{
152 setResponseAndCheckStatus(apduResponse);
153 if (apduResponse->getDataOut().size() > 0) {
154 if (mData->isSamTraceabilityMode()) {
155 mData->setSignedData(
156 Arrays::copyOf(
157 apduResponse->getDataOut(), mData->getData().size()));
158 } else {
159 mData->setSignedData(mData->getData());
160 }
161
162 mData->setSignature(
163 Arrays::copyOfRange(
164 apduResponse->getDataOut(),
165 apduResponse->getDataOut().size() - mData->getSignatureSize(),
166 apduResponse->getDataOut().size()));
167 }
168}
169
170const std::map<int, const std::shared_ptr<StatusProperties>>&
171CommandPsoComputeSignature::getStatusTable() const
172{
173 return STATUS_TABLE;
174}
175
176} /* namespace legacysam */
177} /* namespace crypto */
178} /* namespace calypso */
179} /* namespace card */
180} /* namespace keyple */