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