Keyple Card Calypso C++ Library - 3.2.2
Component of the Keyple C++ middleware
CommandRehabilitate.cpp
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (c) 2025 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/CommandRehabilitate.hpp"
15
16#include <map>
17#include <memory>
18#include <string>
19#include <utility>
20#include <vector>
21
22#include "keyple/card/calypso/CalypsoCardAdapter.hpp"
23#include "keyple/card/calypso/CardAccessForbiddenException.hpp"
24#include "keyple/card/calypso/CardIllegalParameterException.hpp"
25#include "keyple/card/calypso/CardSecurityContextException.hpp"
26#include "keyple/card/calypso/CardSecurityDataException.hpp"
27#include "keyple/card/calypso/CardSessionBufferOverflowException.hpp"
28#include "keyple/core/util/ApduUtil.hpp"
29#include "keyple/core/util/HexUtil.hpp"
30#include "keyple/core/util/cpp/exception/IllegalStateException.hpp"
31#include "keyple/core/util/cpp/exception/UnsupportedOperationException.hpp"
32#include "keypop/calypso/card/transaction/CryptoException.hpp"
33#include "keypop/calypso/card/transaction/CryptoIOException.hpp"
34#include "keypop/calypso/card/transaction/InvalidCardSignatureException.hpp"
35#include "keypop/calypso/crypto/symmetric/SymmetricCryptoException.hpp"
36#include "keypop/calypso/crypto/symmetric/SymmetricCryptoIOException.hpp"
37
38namespace keyple {
39namespace card {
40namespace calypso {
41
42using keyple::core::util::ApduUtil;
43using keyple::core::util::HexUtil;
44using keyple::core::util::cpp::exception::IllegalStateException;
45using keyple::core::util::cpp::exception::UnsupportedOperationException;
46using keypop::calypso::card::transaction::CryptoException;
47using keypop::calypso::card::transaction::CryptoIOException;
48using keypop::calypso::card::transaction::InvalidCardSignatureException;
49using keypop::calypso::crypto::symmetric::SymmetricCryptoException;
50using keypop::calypso::crypto::symmetric::SymmetricCryptoIOException;
51
52const std::map<int, const std::shared_ptr<Command::StatusProperties>>
53 CommandRehabilitate::STATUS_TABLE = [] {
54 std::map<int, const std::shared_ptr<Command::StatusProperties>> m(
55 Command::STATUS_TABLE);
56
57 m.insert(
58 {{0x6400,
59 std::make_shared<StatusProperties>(
60 "Too many modifications in session",
61 typeid(CardSessionBufferOverflowException))},
62 {0x6700,
63 std::make_shared<StatusProperties>(
64 "Lc value not supported",
65 typeid(CardIllegalParameterException))},
66 {0x6982,
67 std::make_shared<StatusProperties>(
68 "Security conditions not fulfilled (no session, wrong key)",
69 typeid(CardSecurityContextException))},
70 {0x6985,
71 std::make_shared<StatusProperties>(
72 "Access forbidden (DF context is invalid)",
73 typeid(CardAccessForbiddenException))}});
74 return m;
75 }();
76
77CommandRehabilitate::CommandRehabilitate(
78 const std::shared_ptr<DtoAdapters::TransactionContextDto>&
79 transactionContext,
80 const std::shared_ptr<DtoAdapters::CommandContextDto>& commandContext)
81: Command(CardCommandRef::REHABILITATE, 0, transactionContext, commandContext)
82{
83 /* APDU Case 1 */
84 setApduRequest(
85 std::make_shared<DtoAdapters::ApduRequestAdapter>(ApduUtil::build(
86 getTransactionContext()->getCard()->getCardClass().getValue(),
87 getCommandRef().getInstructionByte(),
88 0x00,
89 0x00,
90 0x00))); /* CL-C1-5BYTE.1 */
91}
92
93void
94CommandRehabilitate::finalizeRequest()
95{
96 encryptRequestAndUpdateTerminalSessionMacIfNeeded();
97}
98
99const std::map<int, const std::shared_ptr<Command::StatusProperties>>&
100CommandRehabilitate::getStatusTable() const
101{
102 return STATUS_TABLE;
103}
104
105bool
106CommandRehabilitate::isCryptoServiceRequiredToFinalizeRequest() const
107{
108 return getCommandContext()->isEncryptionActive();
109}
110
111bool
112CommandRehabilitate::synchronizeCryptoServiceBeforeCardProcessing()
113{
114 if (getCommandContext()->isEncryptionActive()) {
115 return false;
116 }
117
118 updateTerminalSessionIfNeeded(APDU_RESPONSE_9000);
119
120 return true;
121}
122
123void
124CommandRehabilitate::parseResponse(
125 std::shared_ptr<ApduResponseApi> apduResponse)
126{
127 decryptResponseAndUpdateTerminalSessionMacIfNeeded(apduResponse);
128 Command::setApduResponseAndCheckStatus(apduResponse);
129 updateTerminalSessionIfNeeded();
130
131 /*
132 * The DF has been successfully invalidated, update the DF status in the
133 * card object.
134 */
135 getTransactionContext()->getCard()->setDfInvalidated(true);
136}
137
138} /* namespace calypso */
139} /* namespace card */
140} /* namespace keyple */