Keyple Card Calypso C++ Library 2.2.2
Reference Terminal Reader API for C++
CmdSamPsoVerifySignature.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 "System.h"
18
19/* 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 CmdSamPsoVerifySignature::STATUS_TABLE = initStatusTable();
37
39 const CalypsoSam::ProductType productType,
40 const std::shared_ptr<TraceableSignatureVerificationDataAdapter> data)
41: AbstractSamCommand(CalypsoSamCommand::PSO_VERIFY_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 = 0x00;
47 const uint8_t p2 = 0xA8;
48
49 /* DataIn */
50 const int messageOffset = data->isSamTraceabilityMode() ? 6 : 4;
51 const int messageSize = static_cast<int>(data->getData().size());
52 const int signatureSize = static_cast<int>(data->getSignature().size());
53 std::vector<uint8_t> dataIn(static_cast<uint64_t>(messageOffset) +
54 static_cast<uint64_t>(messageSize) +
55 static_cast<uint64_t>(signatureSize));
56
57 /* SignKeyNum: Selection of the key by KIF and KVC given in the incoming data */
58 dataIn[0] = 0xFF;
59
60 /* SignKeyRef: KIF and KVC of the signing key */
61 dataIn[1] = data->getKif();
62 dataIn[2] = data->getKvc();
63
68 uint8_t opMode = 0; /* %0000 Normal mode */
69 if (data->isSamTraceabilityMode()) {
70 if (data->isPartialSamSerialNumber()) {
71 opMode |= 4; /* %x100 */
72 } else {
73 opMode |= 6; /* %x110 */
74 }
75 }
76 if (data->isBusyMode()) {
77 opMode |= 8; /* %1xx0 */
78 }
79 opMode <<= 4;
80
81 /* Y: Signature size (in bytes) */
82 opMode |= signatureSize;
83 dataIn[3] = opMode;
84
85 /* TraceOffset (optional): Bit offset in MessageIn of the SAM traceability data */
86 if (data->isSamTraceabilityMode()) {
87 dataIn[4] = static_cast<uint8_t>(data->getTraceabilityOffset() >> 8);
88 dataIn[5] = static_cast<uint8_t>(data->getTraceabilityOffset());
89 }
90
91 /* MessageIn: Message to sign */
92 System::arraycopy(data->getData(), 0, dataIn, messageOffset, messageSize);
93
94 /* Signature */
95 System::arraycopy(data->getSignature(),
96 0,
97 dataIn,
98 dataIn.size() - signatureSize,
99 signatureSize);
100
101 setApduRequest(std::make_shared<ApduRequestAdapter>(ApduUtil::build(cla, ins, p1, p2, dataIn)));
102}
103
105 const std::shared_ptr<ApduResponseApi> apduResponse)
106{
108
109 if (isSuccessful()) {
110 mData->setSignatureValid(true);
111 } else if (apduResponse->getStatusWord() == 0x6988) {
112 mData->setSignatureValid(false);
113 }
114
115 return *this;
116}
117
118const std::map<const int, const std::shared_ptr<StatusProperties>>&
120{
121 return STATUS_TABLE;
122}
123
124const std::map<const int, const std::shared_ptr<StatusProperties>>
125 CmdSamPsoVerifySignature::initStatusTable()
126{
127 std::map<const int, const std::shared_ptr<StatusProperties>> m =
129
130 m.insert({0x6982,
131 std::make_shared<StatusProperties>("Busy status: the command is temporarily" \
132 " unavailable.",
134 m.insert({0x6985,
135 std::make_shared<StatusProperties>("Preconditions not satisfied.",
136 typeid(CalypsoSamAccessForbiddenException))});
137 m.insert({0x6988,
138 std::make_shared<StatusProperties>("Incorrect signature.",
139 typeid(CalypsoSamSecurityDataException))});
140 m.insert({0x6A80,
141 std::make_shared<StatusProperties>("Incorrect parameters in incoming data.",
142 typeid(CalypsoSamIncorrectInputDataException))});
143 m.insert({0x6A83,
144 std::make_shared<StatusProperties>("Record not found: signing key not found.",
145 typeid(CalypsoSamDataAccessException))});
146 m.insert({0x6B00,
147 std::make_shared<StatusProperties>("Incorrect P1 or P2.",
148 typeid(CalypsoSamIllegalParameterException))});
149
150 return m;
151}
152
153}
154}
155}
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
CmdSamPsoVerifySignature(const CalypsoSam::ProductType productType, const std::shared_ptr< TraceableSignatureVerificationDataAdapter > data)
const std::map< const int, const std::shared_ptr< StatusProperties > > & getStatusTable() const override
AbstractSamCommand & setApduResponse(const std::shared_ptr< ApduResponseApi > apduResponse) override
static uint8_t getClassByte(const ProductType productType)
CalypsoSam::ProductType ProductType