14#include "keyple/card/calypso/CommandWriteRecord.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 CommandWriteRecord::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 "Wrong EF type (not a Linear EF, or Cyclic EF with Record "
60 typeid(CardDataAccessException))},
62 std::make_shared<Command::StatusProperties>(
63 "Security conditions not fulfilled (no session, wrong key, "
64 "encryption required)",
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 "Command not allowed (no current EF)",
74 typeid(CardDataAccessException))},
76 std::make_shared<Command::StatusProperties>(
77 "File not found",
typeid(CardDataAccessException))},
79 std::make_shared<Command::StatusProperties>(
80 "Record is not found (record index is 0 or above NumRec)",
81 typeid(CardDataAccessException))},
83 std::make_shared<Command::StatusProperties>(
84 "P2 value not supported",
85 typeid(CardIllegalParameterException))}});
89CommandWriteRecord::CommandWriteRecord(
90 const std::shared_ptr<DtoAdapters::TransactionContextDto>&
92 const std::shared_ptr<DtoAdapters::CommandContextDto>& commandContext,
94 std::uint8_t recordNumber,
95 const std::vector<std::uint8_t>& newRecordData)
97 CardCommandRef::WRITE_RECORD,
98 std::unique_ptr<int>(new int(0)),
102, mRecordNumber(recordNumber)
103, mData(newRecordData)
105 const std::uint8_t p2 = (sfi == 0) ? 0x04 : sfi * 8 + 4;
109 std::make_shared<DtoAdapters::ApduRequestAdapter>(ApduUtil::build(
110 transactionContext->getCard()->getCardClass().getValue(),
111 getCommandRef().getInstructionByte(),
116 std::stringstream extraInfo;
117 extraInfo <<
"SFI:" << sfi <<
"h, "
118 <<
"REC:" << recordNumber;
120 addSubName(extraInfo.str());
124CommandWriteRecord::finalizeRequest()
126 encryptRequestAndUpdateTerminalSessionMacIfNeeded();
130CommandWriteRecord::isCryptoServiceRequiredToFinalizeRequest()
const
132 return getCommandContext()->isEncryptionActive();
136CommandWriteRecord::synchronizeCryptoServiceBeforeCardProcessing()
138 if (getCommandContext()->isEncryptionActive()) {
142 updateTerminalSessionIfNeeded(APDU_RESPONSE_9000);
148CommandWriteRecord::parseResponse(std::shared_ptr<ApduResponseApi> apduResponse)
150 decryptResponseAndUpdateTerminalSessionMacIfNeeded(apduResponse);
151 Command::setApduResponseAndCheckStatus(apduResponse);
152 getTransactionContext()->getCard()->fillContent(
153 mSfi, mRecordNumber, mData, 0);
154 updateTerminalSessionIfNeeded();
157const std::map<int, const std::shared_ptr<Command::StatusProperties>>&
158CommandWriteRecord::getStatusTable()
const