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