14#include "keyple/card/calypso/crypto/legacysam/CommandWriteSamParameters.hpp"
22#include "keyple/card/calypso/crypto/legacysam/AccessForbiddenException.hpp"
23#include "keyple/card/calypso/crypto/legacysam/CommandGiveRandom.hpp"
24#include "keyple/card/calypso/crypto/legacysam/CommandSamDataCipher.hpp"
25#include "keyple/card/calypso/crypto/legacysam/CommandSamGenerateKey.hpp"
26#include "keyple/card/calypso/crypto/legacysam/CommandSelectDiversifier.hpp"
27#include "keyple/card/calypso/crypto/legacysam/CounterOverflowException.hpp"
28#include "keyple/card/calypso/crypto/legacysam/DtoAdapters.hpp"
29#include "keyple/card/calypso/crypto/legacysam/IllegalParameterException.hpp"
30#include "keyple/card/calypso/crypto/legacysam/IncorrectInputDataException.hpp"
31#include "keyple/card/calypso/crypto/legacysam/LegacySamUtil.hpp"
32#include "keyple/card/calypso/crypto/legacysam/SecurityDataException.hpp"
33#include "keyple/core/util/ApduUtil.hpp"
34#include "keyple/core/util/ByteArrayUtil.hpp"
35#include "keyple/core/util/cpp/System.hpp"
36#include "keypop/calypso/crypto/legacysam/SystemKeyType.hpp"
37#include "keypop/card/ApduResponseApi.hpp"
45using keyple::core::util::ApduUtil;
46using keyple::core::util::ByteArrayUtil;
47using keyple::core::util::cpp::System;
51const std::map<int, const std::shared_ptr<StatusProperties>>
52 CommandWriteSamParameters::STATUS_TABLE = [] {
53 std::map<int, const std::shared_ptr<StatusProperties>> m(
54 Command::STATUS_TABLE);
58 std::make_shared<StatusProperties>(
59 "Incorrect Lc",
typeid(IllegalParameterException))},
61 std::make_shared<StatusProperties>(
62 std::string(
"Preconditions not satisfied:\n")
63 +
"- The SAM is locked.\n"
64 +
"- In P1, b7 is equal to 1.\n"
65 +
"- CipherEnableBit of PAR1 is 0.\n"
66 +
"- Dynamic mode and the outgoing challenge is "
68 +
"- SystemLockEnableBit=1.\n"
69 +
"- Static mode and StaticCipherEnableBit=0",
70 typeid(AccessForbiddenException))},
72 std::make_shared<StatusProperties>(
73 "An event counter cannot be incremented",
74 typeid(CounterOverflowException))},
76 std::make_shared<StatusProperties>(
77 "Incorrect signature",
typeid(SecurityDataException))},
79 std::make_shared<StatusProperties>(
80 "Incorrect P2",
typeid(IllegalParameterException))},
82 std::make_shared<StatusProperties>(
83 "Incorrect decrypted data",
84 typeid(IncorrectInputDataException))},
86 std::make_shared<StatusProperties>(
87 "Record not found: deciphering key not found",
88 typeid(DataAccessException))}});
92CommandWriteSamParameters::CommandWriteSamParameters(
93 std::shared_ptr<CommandContextDto> context,
94 const std::vector<std::uint8_t>& samParameters)
95: Command(CommandRef::WRITE_PARAMETERS, 0, context)
97 mPlainData = std::vector<std::uint8_t>(30);
98 System::arraycopy(samParameters, 0, mPlainData, 1, samParameters.size());
102CommandWriteSamParameters::finalizeRequest()
104 mPlainData[0] = getContext()
106 ->getSystemKeyParameter(SystemKeyType::PERSONALIZATION)
109 auto controlSamContext = std::make_shared<CommandContextDto>(
110 getContext()->getControlSam(),
nullptr,
nullptr);
113 addControlSamCommand(
114 std::make_shared<CommandSelectDiversifier>(
116 getContext()->getTargetSam()->getSerialNumber()));
118 addControlSamCommand(
119 std::make_shared<CommandGiveRandom>(
120 controlSamContext, getContext()->getTargetSam()->popChallenge()));
122 auto commandSamDataCipher = std::make_shared<CommandSamDataCipher>(
125 CommandSamDataCipher::DataType::PARAMETERS_RECORD,
128 addControlSamCommand(commandSamDataCipher);
129 processControlSamCommand();
131 const std::uint8_t cla = getContext()->getTargetSam()->getClassByte();
132 const std::uint8_t inst = CommandRef::WRITE_PARAMETERS.getInstructionByte();
133 const std::uint8_t p1 = LegacySamConstants::DYNAMIC_MODE_CIPHERING;
134 const std::uint8_t p2 = 0xA0;
137 std::make_shared<ApduRequestAdapter>(ApduUtil::build(
138 cla, inst, p1, p2, commandSamDataCipher->getCipheredData())));
142CommandWriteSamParameters::isControlSamRequiredToFinalizeRequest()
const
148CommandWriteSamParameters::parseResponse(
149 std::shared_ptr<ApduResponseApi> apduResponse)
151 setResponseAndCheckStatus(apduResponse);
154const std::map<int, const std::shared_ptr<StatusProperties>>&
155CommandWriteSamParameters::getStatusTable()
const
DtoAdapters::CommandContextDto CommandContextDto