Keyple Card Calypso C++ Library 2.2.2
Reference Terminal Reader API for C++
CmdSamPsoComputeSignature.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 information regarding *
5 * copyright ownership. *
6 * *
7 * This program and the accompanying materials are made available under the terms of the Eclipse *
8 * Public License 2.0 which is available at http://www.eclipse.org/legal/epl-2.0 *
9 * *
10 * SPDX-License-Identifier: EPL-2.0 *
11 **************************************************************************************************/
12
14
15/* Keyple Core Util */
16#include "ApduUtil.h"
17#include "Arrays.h"
18#include "System.h"
19
20/* Keyple Card Calypso */
26#include "SamUtilAdapter.h"
27
28namespace keyple {
29namespace card {
30namespace calypso {
31
32using namespace keyple::core::util;
33using namespace keyple::core::util::cpp;
34
35const std::map<const int, const std::shared_ptr<StatusProperties>>
36 CmdSamPsoComputeSignature::STATUS_TABLE = initStatusTable();
37
39 const CalypsoSam::ProductType productType,
40 const std::shared_ptr<TraceableSignatureComputationDataAdapter> data)
41: AbstractSamCommand(CalypsoSamCommand::PSO_COMPUTE_SIGNATURE, 0),
42 mData(data)
43{
44 const uint8_t cla = SamUtilAdapter::getClassByte(productType);
45 const uint8_t ins = getCommandRef().getInstructionByte();
46 const uint8_t p1 = 0x9E;
47 const uint8_t p2 = 0x9A;
48
49 /* DataIn */
50 const int messageOffset = data->isSamTraceabilityMode() ? 6 : 4;
51 const int messageSize = static_cast<int>(data->getData().size());
52 std::vector<uint8_t> dataIn(static_cast<uint64_t>(messageOffset) +
53 static_cast<uint64_t>(messageSize));
54
55 /* SignKeyNum: Selection of the key by KIF and KVC given in the incoming data */
56 dataIn[0] = 0xFF;
57
58 /* SignKeyRef: KIF and KVC of the signing key */
59 dataIn[1] = data->getKif();
60 dataIn[2] = data->getKvc();
61
66 uint8_t opMode = 0; /* %0000 Normal mode */
67 if (data->isSamTraceabilityMode()) {
68 if (data->isPartialSamSerialNumber()) {
69 opMode |= 4; /* %x100 */
70 } else {
71 opMode |= 6; /* %x110 */
72 }
73 }
74 if (data->isBusyMode()) {
75 opMode |= 8; /* %1xx0 */
76 }
77 opMode <<= 4;
78
79 /* Y: Signature size (in bytes) */
80 opMode |= data->getSignatureSize();
81 dataIn[3] = opMode;
82
83 /* TraceOffset (optional): Bit offset in MessageIn of the SAM traceability data */
84 if (data->isSamTraceabilityMode()) {
85 dataIn[4] = static_cast<uint8_t>(data->getTraceabilityOffset() >> 8);
86 dataIn[5] = static_cast<uint8_t>(data->getTraceabilityOffset());
87 }
88
89 /* MessageIn: Message to sign */
90 System::arraycopy(data->getData(), 0, dataIn, messageOffset, messageSize);
91
92 setApduRequest(std::make_shared<ApduRequestAdapter>(ApduUtil::build(cla, ins, p1, p2, dataIn)));
93}
94
95const std::map<const int, const std::shared_ptr<StatusProperties>>&
97{
98 return STATUS_TABLE;
99}
100
102 const std::shared_ptr<ApduResponseApi> apduResponse)
103{
105
106 if (static_cast<int>(apduResponse->getDataOut().size()) > 0) {
107 if (mData->isSamTraceabilityMode()) {
108 mData->setSignedData(Arrays::copyOf(apduResponse->getDataOut(),
109 mData->getData().size()));
110 } else {
111 mData->setSignedData(mData->getData());
112 }
113
114 mData->setSignature(
115 Arrays::copyOfRange(
116 apduResponse->getDataOut(),
117 apduResponse->getDataOut().size() - mData->getSignatureSize(),
118 apduResponse->getDataOut().size()));
119 }
120
121 return *this;
122}
123
124const std::map<const int, const std::shared_ptr<StatusProperties>>
125 CmdSamPsoComputeSignature::initStatusTable()
126{
127 std::map<const int, const std::shared_ptr<StatusProperties>> m =
129
130 m.insert({0x6700,
131 std::make_shared<StatusProperties>("Incorrect Lc.",
133 m.insert({0x6900,
134 std::make_shared<StatusProperties>("An event counter cannot be incremented.",
135 typeid(CalypsoSamCounterOverflowException))});
136 m.insert({0x6985,
137 std::make_shared<StatusProperties>("Preconditions not satisfied.",
138 typeid(CalypsoSamAccessForbiddenException))});
139 m.insert({0x6A80,
140 std::make_shared<StatusProperties>("Incorrect value in the incoming data.",
141 typeid(CalypsoSamIncorrectInputDataException))});
142 m.insert({0x6A83,
143 std::make_shared<StatusProperties>("Record not found: signing key not found.",
144 typeid(CalypsoSamDataAccessException))});
145 m.insert({0x6B00,
146 std::make_shared<StatusProperties>("Incorrect P1 or P2.",
147 typeid(CalypsoSamIllegalParameterException))});
148
149 return m;
150}
151
152}
153}
154}
virtual void setApduRequest(const std::shared_ptr< ApduRequestAdapter > apduRequest) final
AbstractSamCommand & setApduResponse(const std::shared_ptr< ApduResponseApi > apduResponse) override
static const std::map< const int, const std::shared_ptr< StatusProperties > > STATUS_TABLE
const CalypsoSamCommand & getCommandRef() const override
CmdSamPsoComputeSignature(const CalypsoSam::ProductType productType, const std::shared_ptr< TraceableSignatureComputationDataAdapter > data)
AbstractSamCommand & setApduResponse(const std::shared_ptr< ApduResponseApi > apduResponse) override
const std::map< const int, const std::shared_ptr< StatusProperties > > & getStatusTable() const override
static uint8_t getClassByte(const ProductType productType)
CalypsoSam::ProductType ProductType