Keyple Card Calypso C++ Library 2.1.0
Reference Terminal Reader API for C++
CmdSamCardCipherPin.cpp
Go to the documentation of this file.
1/**************************************************************************************************
2 * Copyright (c) 2022 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 "CmdSamCardCipherPin.h"
14
15/* Keyple Card Calypso */
20#include "SamUtilAdapter.h"
21
22/* Keyple Core Util */
23#include "ApduUtil.h"
24#include "IllegalArgumentException.h"
25#include "System.h"
26
27namespace keyple {
28namespace card {
29namespace calypso {
30
31using namespace keyple::core::util;
32using namespace keyple::core::util::cpp;
33using namespace keyple::core::util::cpp::exception;
34
35const CalypsoSamCommand CmdSamCardCipherPin::mCommand = CalypsoSamCommand::CARD_CIPHER_PIN;
36
37const std::map<const int, const std::shared_ptr<StatusProperties>>
38 CmdSamCardCipherPin::STATUS_TABLE = initStatusTable();
39
41 const uint8_t cipheringKif,
42 const uint8_t cipheringKvc,
43 const std::vector<uint8_t>& currentPin,
44 const std::vector<uint8_t>& newPin)
45: AbstractSamCommand(mCommand)
46{
47 if (currentPin.size() != 4) {
48 throw IllegalArgumentException("Bad current PIN value.");
49 }
50
51 if (!newPin.empty() && newPin.size() != 4) {
52 throw IllegalArgumentException("Bad new PIN value.");
53 }
54
55 const uint8_t cla = SamUtilAdapter::getClassByte(productType);
56
57 uint8_t p1;
58 uint8_t p2;
59 std::vector<uint8_t> data;
60
61 if (newPin.empty()) {
62 /* No new PIN is provided, we consider it's a PIN verification */
63 p1 = 0x80;
64 data = std::vector<uint8_t>(6);
65 } else {
66 /* A new PIN is provided, we consider it's a PIN update */
67 p1 = 0x40;
68 data = std::vector<uint8_t>(10);
69 System::arraycopy(newPin, 0, data, 6, 4);
70 }
71 p2 = 0xFF; /* KIF and KVC in incoming data */
72
73 data[0] = cipheringKif;
74 data[1] = cipheringKvc;
75
76 System::arraycopy(currentPin, 0, data, 2, 4);
77
79 std::make_shared<ApduRequestAdapter>(
80 ApduUtil::build(cla, mCommand.getInstructionByte(), p1, p2, data)));
81}
82
83const std::vector<uint8_t> CmdSamCardCipherPin::getCipheredData() const
84{
85 return getApduResponse()->getDataOut();
86}
87
88const std::map<const int, const std::shared_ptr<StatusProperties>>
89 CmdSamCardCipherPin::initStatusTable()
90{
91 std::map<const int, const std::shared_ptr<StatusProperties>> m =
93
94 m.insert({0x6700,
95 std::make_shared<StatusProperties>("Incorrect Lc.",
97 m.insert({0x6900,
98 std::make_shared<StatusProperties>("An event counter cannot be incremented.",
99 typeid(CalypsoSamCounterOverflowException))});
100 m.insert({0x6985,
101 std::make_shared<StatusProperties>("Preconditions not satisfied.",
102 typeid(CalypsoSamAccessForbiddenException))});
103 m.insert({0x6A00,
104 std::make_shared<StatusProperties>("Incorrect P1 or P2",
105 typeid(CalypsoSamIllegalParameterException))});
106 m.insert({0x6A83,
107 std::make_shared<StatusProperties>("Record not found: ciphering key not found",
108 typeid(CalypsoSamDataAccessException))});
109
110 return m;
111}
112
113const std::map<const int, const std::shared_ptr<StatusProperties>>&
115{
116 return STATUS_TABLE;
117}
118
119}
120}
121}
virtual const std::shared_ptr< ApduResponseApi > getApduResponse() const final
virtual void setApduRequest(const std::shared_ptr< ApduRequestAdapter > apduRequest) final
static const std::map< const int, const std::shared_ptr< StatusProperties > > STATUS_TABLE
static const CalypsoSamCommand CARD_CIPHER_PIN
const std::vector< uint8_t > getCipheredData() const
const std::map< const int, const std::shared_ptr< StatusProperties > > & getStatusTable() const override
CmdSamCardCipherPin(const CalypsoSam::ProductType productType, const uint8_t cipheringKif, const uint8_t cipheringKvc, const std::vector< uint8_t > &currentPin, const std::vector< uint8_t > &newPin)
static uint8_t getClassByte(const ProductType productType)
CalypsoSam::ProductType ProductType