Keyple Card Calypso C++ Library 2.2.5.6
Reference Terminal Reader API for C++
CmdCardVerifyPin.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
13#include "CmdCardVerifyPin.h"
14
15#include <sstream>
16
17/* Keyple Core Util */
18#include "ApduUtil.h"
19#include "IllegalArgumentException.h"
20#include "IllegalStateException.h"
21
22/* Keyple Card Calypso */
26#include "CardPinException.h"
30
31namespace keyple {
32namespace card {
33namespace calypso {
34
35using namespace keyple::core::util;
36using namespace keyple::core::util::cpp;
37using namespace keyple::core::util::cpp::exception;
38
39const CalypsoCardCommand CmdCardVerifyPin::mCommand = CalypsoCardCommand::VERIFY_PIN;
40const std::map<const int, const std::shared_ptr<StatusProperties>>
41 CmdCardVerifyPin::STATUS_TABLE = initStatusTable();
42
44 const std::shared_ptr<CalypsoCardAdapter> calypsoCard,
45 const bool encryptPinTransmission,
46 const std::vector<uint8_t>& pin)
47: AbstractCardCommand(mCommand, 0, calypsoCard), mCla(calypsoCard->getCardClass().getValue())
48{
49 if (pin.empty() ||
50 (!encryptPinTransmission && pin.size() != 4) ||
51 (encryptPinTransmission && pin.size() != 8)) {
52
53 throw IllegalArgumentException("The PIN must be 4 bytes long");
54 }
55
56 /* CL-PIN-PP1P2.1 */
57 const uint8_t p1 = 0x00;
58 const uint8_t p2 = 0x00;
59
60 // APDU Case 3
62 std::make_shared<ApduRequestAdapter>(
63 ApduUtil::build(mCla, mCommand.getInstructionByte(), p1, p2, pin)));
64
65 addSubName(encryptPinTransmission ? "ENCRYPTED" : "PLAIN");
66
67 mReadCounterOnly = false;
68}
69
70CmdCardVerifyPin::CmdCardVerifyPin(const std::shared_ptr<CalypsoCardAdapter> calypsoCard)
71: AbstractCardCommand(mCommand, 0, calypsoCard), mCla(calypsoCard->getCardClass().getValue())
72{
73 const uint8_t p1 = 0x00;
74 const uint8_t p2 = 0x00;
75
77 std::make_shared<ApduRequestAdapter>(
78 ApduUtil::build(mCla, mCommand.getInstructionByte(), p1, p2)));
79
80 addSubName("Read presentation counter");
81
82 mReadCounterOnly = true;
83}
84
85void CmdCardVerifyPin::parseApduResponse(const std::shared_ptr<ApduResponseApi> apduResponse)
86{
87 try {
88
90 getCalypsoCard()->setPinAttemptRemaining(3);
91
92 } catch (const CardPinException& e) {
93
94 switch (apduResponse->getStatusWord()) {
95
96 case 0x63C2:
97 getCalypsoCard()->setPinAttemptRemaining(2);
98 break;
99
100 case 0x63C1:
101 getCalypsoCard()->setPinAttemptRemaining(1);
102 break;
103
104 case 0x6983:
105 getCalypsoCard()->setPinAttemptRemaining(0);
106 break;
107
108 default:
109 /* NOP */
110 break;
111 }
112
113 /*
114 * Forward the exception if the operation do not target the reading of the attempt counter.
115 * Catch it silently otherwise
116 */
117 if (!mReadCounterOnly) {
118
119 throw e;
120 }
121 }
122}
123
125{
126 return false;
127}
128
129const std::map<const int, const std::shared_ptr<StatusProperties>>
130 CmdCardVerifyPin::initStatusTable()
131{
132 std::map<const int, const std::shared_ptr<StatusProperties>> m =
134
135 m.insert({0x6700,
136 std::make_shared<StatusProperties>("Lc value not supported (only 00h, 04h or 08h " \
137 "are supported).",
139 m.insert({0x6900,
140 std::make_shared<StatusProperties>("Transaction Counter is 0.",
141 typeid(CardTerminatedException))});
142 m.insert({0x6982,
143 std::make_shared<StatusProperties>("Security conditions not fulfilled (Get " \
144 "Challenge not done: challenge unavailable).",
145 typeid(CardSecurityContextException))});
146 m.insert({0x6985,
147 std::make_shared<StatusProperties>("Access forbidden (a session is open or DF is " \
148 "invalidated).",
149 typeid(CardAccessForbiddenException))});
150 m.insert({0x63C1,
151 std::make_shared<StatusProperties>("Incorrect PIN (1 attempt remaining).",
152 typeid(CardPinException))});
153 m.insert({0x63C2,
154 std::make_shared<StatusProperties>("Incorrect PIN (2 attempt remaining).",
155 typeid(CardPinException))});
156 m.insert({0x6983,
157 std::make_shared<StatusProperties>("Presentation rejected (PIN is blocked).",
158 typeid(CardPinException))});
159 m.insert({0x6D00,
160 std::make_shared<StatusProperties>("PIN function not present.",
161 typeid(CardIllegalParameterException))});
162
163 return m;
164}
165
166const std::map<const int, const std::shared_ptr<StatusProperties>>&
168{
169 return STATUS_TABLE;
170}
171
172}
173}
174}
static const std::map< const int, const std::shared_ptr< StatusProperties > > STATUS_TABLE
virtual void addSubName(const std::string &subName) final
virtual void setApduRequest(const std::shared_ptr< ApduRequestAdapter > apduRequest) final
void parseApduResponse(const std::shared_ptr< ApduResponseApi > apduResponse) override
std::shared_ptr< CalypsoCardAdapter > getCalypsoCard() const
static const CalypsoCardCommand VERIFY_PIN
void parseApduResponse(const std::shared_ptr< ApduResponseApi > apduResponse) override
const std::map< const int, const std::shared_ptr< StatusProperties > > & getStatusTable() const override
CmdCardVerifyPin(const std::shared_ptr< CalypsoCardAdapter > calypsoCard, const bool encryptPinTransmission, const std::vector< uint8_t > &pin)