keyple-card-calypso-crypto-legacysam-cpp-lib - 1.0.2
Component of the Keyple C++ middleware
CommandCardCipherPin.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 *
5 * information regarding copyright ownership. *
6 * *
7 * This program and the accompanying materials are made available under the *
8 * terms of the Eclipse Public License 2.0 which is available at *
9 * http://www.eclipse.org/legal/epl-2.0 *
10 * *
11 * SPDX-License-Identifier: EPL-2.0 *
12 ******************************************************************************/
13
14#include "keyple/card/calypso/crypto/legacysam/CommandCardCipherPin.hpp"
15
16#include <map>
17#include <memory>
18#include <vector>
19
20#include "keyple/card/calypso/crypto/legacysam/AccessForbiddenException.hpp"
21#include "keyple/card/calypso/crypto/legacysam/CounterOverflowException.hpp"
22#include "keyple/card/calypso/crypto/legacysam/DataAccessException.hpp"
23#include "keyple/card/calypso/crypto/legacysam/IllegalParameterException.hpp"
24#include "keyple/core/util/ApduUtil.hpp"
25#include "keyple/core/util/cpp/System.hpp"
26#include "keyple/core/util/cpp/exception/IllegalArgumentException.hpp"
27
28namespace keyple {
29namespace card {
30namespace calypso {
31namespace crypto {
32namespace legacysam {
33
34using keyple::core::util::ApduUtil;
35using keyple::core::util::cpp::System;
36using keyple::core::util::cpp::exception::IllegalArgumentException;
37
38const std::map<int, const std::shared_ptr<StatusProperties>>
39 CommandCardCipherPin::STATUS_TABLE = [] {
40 std::map<int, const std::shared_ptr<StatusProperties>> m(
41 Command::STATUS_TABLE);
42
43 m.insert(
44 {{0x6700,
45 std::make_shared<StatusProperties>(
46 "Incorrect Lc", typeid(IllegalParameterException))},
47 {0x6900,
48 std::make_shared<StatusProperties>(
49 "An event counter cannot be incremented",
50 typeid(CounterOverflowException))},
51
52 {0x6985,
53 std::make_shared<StatusProperties>(
54 "Preconditions not satisfied",
55 typeid(AccessForbiddenException))},
56 {0x6A00,
57 std::make_shared<StatusProperties>(
58 "Incorrect P1 or P2", typeid(IllegalParameterException))},
59 {0x6A83,
60 std::make_shared<StatusProperties>(
61 "Record not found: ciphering key not found",
62 typeid(DataAccessException))}});
63 return m;
64 }();
65
66CommandCardCipherPin::CommandCardCipherPin(
67 std::shared_ptr<DtoAdapters::CommandContextDto> context,
68 std::uint8_t cipheringKif,
69 std::uint8_t cipheringKvc,
70 const std::vector<std::uint8_t>& currentPin)
71: Command(CommandRef::CARD_CIPHER_PIN, 0, context)
72{
73 if (currentPin.size() != 4) {
74 throw IllegalArgumentException("Bad current PIN value");
75 }
76
77 const std::uint8_t cla = context->getTargetSam()->getClassByte();
78
79 std::uint8_t p1;
80 std::uint8_t p2;
81
82 /* No new PIN is provided, we consider it's a PIN verification. */
83
84 p1 = 0x80;
85 p2 = 0xFF; /* KIF and KVC in incoming data */
86
87 auto data = std::vector<std::uint8_t>(6);
88 data[0] = cipheringKif;
89 data[1] = cipheringKvc;
90 System::arraycopy(currentPin, 0, data, 2, 4);
91
92 setApduRequest(
93 std::make_shared<ApduRequestAdapter>(ApduUtil::build(
94 cla, getCommandRef().getInstructionByte(), p1, p2, data)));
95}
96
97CommandCardCipherPin::CommandCardCipherPin(
98 std::shared_ptr<DtoAdapters::CommandContextDto> context,
99 std::uint8_t cipheringKif,
100 std::uint8_t cipheringKvc,
101 const std::vector<std::uint8_t>& currentPin,
102 const std::vector<std::uint8_t>& newPin)
103: Command(CommandRef::CARD_CIPHER_PIN, 0, context)
104{
105 if (currentPin.size() != 4) {
106 throw IllegalArgumentException("Bad current PIN value");
107 }
108
109 if (newPin.size() != 4) {
110 throw IllegalArgumentException("Bad new PIN value");
111 }
112
113 const std::uint8_t cla = context->getTargetSam()->getClassByte();
114
115 std::uint8_t p1;
116 std::uint8_t p2;
117
118 /* A new PIN is provided, we consider it's a PIN update. */
119
120 p1 = 0x40;
121 p2 = 0xFF; /* KIF and KVC in incoming data */
122
123 auto data = std::vector<std::uint8_t>(10);
124 System::arraycopy(newPin, 0, data, 6, 4);
125 data[0] = cipheringKif;
126 data[1] = cipheringKvc;
127
128 System::arraycopy(currentPin, 0, data, 2, 4);
129
130 setApduRequest(
131 std::make_shared<ApduRequestAdapter>(ApduUtil::build(
132 cla, getCommandRef().getInstructionByte(), p1, p2, data)));
133}
134
135const std::vector<std::uint8_t>&
136CommandCardCipherPin::getCipheredData() const
137{
138 return mCipheredData;
139}
140
141void
142CommandCardCipherPin::finalizeRequest()
143{
144 /* nothing to do */
145}
146
147bool
148CommandCardCipherPin::isControlSamRequiredToFinalizeRequest() const
149{
150 return false;
151}
152
153void
154CommandCardCipherPin::parseResponse(
155 std::shared_ptr<ApduResponseApi> apduResponse)
156{
157 setResponseAndCheckStatus(apduResponse);
158 mCipheredData = apduResponse->getDataOut();
159}
160
161const std::map<int, const std::shared_ptr<StatusProperties>>&
162CommandCardCipherPin::getStatusTable() const
163{
164 return STATUS_TABLE;
165}
166
167} /* namespace legacysam */
168} /* namespace crypto */
169} /* namespace calypso */
170} /* namespace card */
171} /* namespace keyple */