14#include "keyple/card/calypso/CommandUpdateOrWriteBinary.hpp"
21#include "keyple/card/calypso/CalypsoCardAdapter.hpp"
22#include "keyple/card/calypso/CalypsoCardClass.hpp"
23#include "keyple/card/calypso/CardAccessForbiddenException.hpp"
24#include "keyple/card/calypso/CardCommandRef.hpp"
25#include "keyple/card/calypso/CardDataAccessException.hpp"
26#include "keyple/card/calypso/CardIllegalParameterException.hpp"
27#include "keyple/card/calypso/CardSecurityContextException.hpp"
28#include "keyple/card/calypso/CardSessionBufferOverflowException.hpp"
29#include "keyple/core/util/ApduUtil.hpp"
30#include "keyple/core/util/HexUtil.hpp"
31#include "keyple/core/util/cpp/Arrays.hpp"
32#include "keyple/core/util/cpp/System.hpp"
38using keyple::core::util::ApduUtil;
39using keyple::core::util::HexUtil;
40using keyple::core::util::cpp::Arrays;
41using keyple::core::util::cpp::System;
43const std::map<int, const std::shared_ptr<Command::StatusProperties>>
44 CommandUpdateOrWriteBinary::STATUS_TABLE = [] {
45 std::map<int, const std::shared_ptr<Command::StatusProperties>> m(
46 Command::STATUS_TABLE);
50 std::make_shared<Command::StatusProperties>(
51 "Too many modifications in session",
52 typeid(CardSessionBufferOverflowException))},
54 std::make_shared<Command::StatusProperties>(
55 "Lc value not supported",
typeid(CardDataAccessException))},
57 std::make_shared<Command::StatusProperties>(
58 "Incorrect EF type: not a Binary EF",
59 typeid(CardDataAccessException))},
61 std::make_shared<Command::StatusProperties>(
62 "Security conditions not fulfilled (no secure session, "
63 "incorrect key, encryption required, PKI mode and not Always "
65 typeid(CardSecurityContextException))},
67 std::make_shared<Command::StatusProperties>(
68 "Access forbidden (Never access mode, DF is invalidated, "
70 typeid(CardSecurityContextException))},
72 std::make_shared<Command::StatusProperties>(
73 "Incorrect file type: the Current File is not an EF. "
75 typeid(CardDataAccessException))},
77 std::make_shared<Command::StatusProperties>(
78 "File not found",
typeid(CardDataAccessException))},
80 std::make_shared<Command::StatusProperties>(
81 "Offset not in the file (offset overflow)",
82 typeid(CardDataAccessException))},
84 std::make_shared<Command::StatusProperties>(
85 "P1 value not supported",
86 typeid(CardIllegalParameterException))}});
90CommandUpdateOrWriteBinary::CommandUpdateOrWriteBinary(
92 const std::shared_ptr<DtoAdapters::TransactionContextDto>&
94 const std::shared_ptr<DtoAdapters::CommandContextDto>& commandContext,
97 const std::vector<uint8_t>& data)
99 isUpdateCommand ? CardCommandRef::UPDATE_BINARY
100 : CardCommandRef::WRITE_BINARY,
101 std::unique_ptr<int>(new int(0)),
108 const std::uint8_t msb
109 =
static_cast<std::uint8_t
>((offset & 0x0000FF00) >> 8);
110 const std::uint8_t lsb =
static_cast<std::uint8_t
>((offset & 0x000000FF));
116 const std::uint8_t p1 = msb > 0 ? msb : (0x80 + sfi);
120 std::make_shared<DtoAdapters::ApduRequestAdapter>(ApduUtil::build(
121 transactionContext->getCard()->getCardClass().getValue(),
122 getCommandRef().getInstructionByte(),
127 std::stringstream extraInfo;
128 extraInfo <<
"SFI:" << sfi <<
"h, "
129 <<
"Offset:" << offset;
131 addSubName(extraInfo.str());
135CommandUpdateOrWriteBinary::finalizeRequest()
137 encryptRequestAndUpdateTerminalSessionMacIfNeeded();
141CommandUpdateOrWriteBinary::isCryptoServiceRequiredToFinalizeRequest()
const
143 return getCommandContext()->isEncryptionActive();
147CommandUpdateOrWriteBinary::synchronizeCryptoServiceBeforeCardProcessing()
149 if (getCommandContext()->isEncryptionActive()) {
153 updateTerminalSessionIfNeeded(APDU_RESPONSE_9000);
159CommandUpdateOrWriteBinary::parseResponse(
160 std::shared_ptr<ApduResponseApi> apduResponse)
162 decryptResponseAndUpdateTerminalSessionMacIfNeeded(apduResponse);
163 Command::setApduResponseAndCheckStatus(apduResponse);
165 if (getCommandRef() == CardCommandRef::UPDATE_BINARY) {
166 getTransactionContext()->getCard()->setContent(mSfi, 1, mData, mOffset);
169 getTransactionContext()->getCard()->fillContent(
170 mSfi, 1, mData, mOffset);
173 updateTerminalSessionIfNeeded();
176const std::map<int, const std::shared_ptr<Command::StatusProperties>>&
177CommandUpdateOrWriteBinary::getStatusTable()
const