14#include "keyple/card/calypso/crypto/legacysam/CommandCardCipherPin.hpp"
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"
34using keyple::core::util::ApduUtil;
35using keyple::core::util::cpp::System;
36using keyple::core::util::cpp::exception::IllegalArgumentException;
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);
45 std::make_shared<StatusProperties>(
46 "Incorrect Lc",
typeid(IllegalParameterException))},
48 std::make_shared<StatusProperties>(
49 "An event counter cannot be incremented",
50 typeid(CounterOverflowException))},
53 std::make_shared<StatusProperties>(
54 "Preconditions not satisfied",
55 typeid(AccessForbiddenException))},
57 std::make_shared<StatusProperties>(
58 "Incorrect P1 or P2",
typeid(IllegalParameterException))},
60 std::make_shared<StatusProperties>(
61 "Record not found: ciphering key not found",
62 typeid(DataAccessException))}});
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)
73 if (currentPin.size() != 4) {
74 throw IllegalArgumentException(
"Bad current PIN value");
77 const std::uint8_t cla = context->getTargetSam()->getClassByte();
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);
93 std::make_shared<ApduRequestAdapter>(ApduUtil::build(
94 cla, getCommandRef().getInstructionByte(), p1, p2, data)));
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)
105 if (currentPin.size() != 4) {
106 throw IllegalArgumentException(
"Bad current PIN value");
109 if (newPin.size() != 4) {
110 throw IllegalArgumentException(
"Bad new PIN value");
113 const std::uint8_t cla = context->getTargetSam()->getClassByte();
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;
128 System::arraycopy(currentPin, 0, data, 2, 4);
131 std::make_shared<ApduRequestAdapter>(ApduUtil::build(
132 cla, getCommandRef().getInstructionByte(), p1, p2, data)));
135const std::vector<std::uint8_t>&
136CommandCardCipherPin::getCipheredData()
const
138 return mCipheredData;
142CommandCardCipherPin::finalizeRequest()
148CommandCardCipherPin::isControlSamRequiredToFinalizeRequest()
const
154CommandCardCipherPin::parseResponse(
155 std::shared_ptr<ApduResponseApi> apduResponse)
157 setResponseAndCheckStatus(apduResponse);
158 mCipheredData = apduResponse->getDataOut();
161const std::map<int, const std::shared_ptr<StatusProperties>>&
162CommandCardCipherPin::getStatusTable()
const