Keyple Card Calypso C++ Library 2.1.0
Reference Terminal Reader API for C++
CmdCardVerifyPin.cpp
Go to the documentation of this file.
1/**************************************************************************************************
2 * Copyright (c) 2021 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 CalypsoCardClass calypsoCardClass,
45 const bool encryptPinTransmission,
46 const std::vector<uint8_t>& pin)
47: AbstractCardCommand(mCommand), mCla(calypsoCardClass.getValue())
48{
49 if (pin.empty() ||
50 (!encryptPinTransmission && pin.size() != 4) ||
51 (encryptPinTransmission && pin.size() != 8)) {
52 throw IllegalArgumentException("The PIN must be 4 bytes long");
53 }
54
55 /* CL-PIN-PP1P2.1 */
56 const uint8_t p1 = 0x00;
57 const uint8_t p2 = 0x00;
58
60 std::make_shared<ApduRequestAdapter>(
61 ApduUtil::build(mCla, mCommand.getInstructionByte(), p1, p2, pin)));
62
63 addSubName(encryptPinTransmission ? "ENCRYPTED" : "PLAIN");
64
65 mReadCounterOnly = false;
66}
67
69: AbstractCardCommand(mCommand), mCla(calypsoCardClass.getValue())
70{
71 const uint8_t p1 = 0x00;
72 const uint8_t p2 = 0x00;
73
75 std::make_shared<ApduRequestAdapter>(
76 ApduUtil::build(mCla, mCommand.getInstructionByte(), p1, p2)));
77
78 addSubName("Read presentation counter");
79
80 mReadCounterOnly = true;
81}
82
84{
85 return false;
86}
87
89{
90 return mReadCounterOnly;
91}
92
94{
95 int attemptCounter;
96
97 switch (getApduResponse()->getStatusWord()) {
98 case 0x6983:
99 attemptCounter = 0;
100 break;
101 case 0x63C1:
102 attemptCounter = 1;
103 break;
104 case 0x63C2:
105 attemptCounter = 2;
106 break;
107 case 0x9000:
108 attemptCounter = 3;
109 break;
110 default:
111 std::stringstream ss;
112 ss << std::setfill ('0') << std::setw(4) << std::hex << getApduResponse()->getStatusWord();
113 throw IllegalStateException("Incorrect status word: " + ss.str());
114 }
115
116 return attemptCounter;
117}
118
119const std::map<const int, const std::shared_ptr<StatusProperties>>
120 CmdCardVerifyPin::initStatusTable()
121{
122 std::map<const int, const std::shared_ptr<StatusProperties>> m =
124
125 m.insert({0x6700,
126 std::make_shared<StatusProperties>("Lc value not supported (only 00h, 04h or 08h " \
127 "are supported).",
129 m.insert({0x6900,
130 std::make_shared<StatusProperties>("Transaction Counter is 0.",
131 typeid(CardTerminatedException))});
132 m.insert({0x6982,
133 std::make_shared<StatusProperties>("Security conditions not fulfilled (Get " \
134 "Challenge not done: challenge unavailable).",
135 typeid(CardSecurityContextException))});
136 m.insert({0x6985,
137 std::make_shared<StatusProperties>("Access forbidden (a session is open or DF is " \
138 "invalidated).",
139 typeid(CardAccessForbiddenException))});
140 m.insert({0x63C1,
141 std::make_shared<StatusProperties>("Incorrect PIN (1 attempt remaining).",
142 typeid(CardPinException))});
143 m.insert({0x63C2,
144 std::make_shared<StatusProperties>("Incorrect PIN (2 attempt remaining).",
145 typeid(CardPinException))});
146 m.insert({0x6983,
147 std::make_shared<StatusProperties>("Presentation rejected (PIN is blocked).",
148 typeid(CardPinException))});
149 m.insert({0x6D00,
150 std::make_shared<StatusProperties>("PIN function not present.",
151 typeid(CardIllegalParameterException))});
152
153 return m;
154}
155
156const std::map<const int, const std::shared_ptr<StatusProperties>>&
158{
159 return STATUS_TABLE;
160}
161
162}
163}
164}
static const std::map< const int, const std::shared_ptr< StatusProperties > > STATUS_TABLE
virtual void addSubName(const std::string &subName) final
virtual const std::shared_ptr< ApduResponseApi > getApduResponse() const final
virtual void setApduRequest(const std::shared_ptr< ApduRequestAdapter > apduRequest) final
static const CalypsoCardCommand VERIFY_PIN
const std::map< const int, const std::shared_ptr< StatusProperties > > & getStatusTable() const override
CmdCardVerifyPin(const CalypsoCardClass calypsoCardClass, const bool encryptPinTransmission, const std::vector< uint8_t > &pin)