keyple-card-calypso-crypto-legacysam-cpp-lib - 1.0.2
Component of the Keyple C++ middleware
CommandDigestUpdate.cpp
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (c) 2023 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/crypto/legacysam/CommandDigestUpdate.hpp"
15
16#include <map>
17#include <memory>
18#include <string>
19#include <vector>
20
21#include "keyple/card/calypso/crypto/legacysam/AccessForbiddenException.hpp"
22#include "keyple/card/calypso/crypto/legacysam/IllegalParameterException.hpp"
23#include "keyple/card/calypso/crypto/legacysam/IncorrectInputDataException.hpp"
24#include "keyple/core/util/ApduUtil.hpp"
25#include "keyple/core/util/cpp/exception/IllegalArgumentException.hpp"
26
27namespace keyple {
28namespace card {
29namespace calypso {
30namespace crypto {
31namespace legacysam {
32
33using keyple::core::util::ApduUtil;
34using keyple::core::util::cpp::exception::IllegalArgumentException;
35
36const std::map<int, const std::shared_ptr<StatusProperties>>
37 CommandDigestUpdate::STATUS_TABLE = [] {
38 std::map<int, const std::shared_ptr<StatusProperties>> m(
39 Command::STATUS_TABLE);
40
41 m.insert(
42 {{0x6700,
43 std::make_shared<StatusProperties>(
44 "Incorrect Lc", typeid(IllegalParameterException))},
45 {0x6985,
46 std::make_shared<StatusProperties>(
47 "Preconditions not satisfied",
48 typeid(AccessForbiddenException))},
49 {0x6A80,
50 std::make_shared<StatusProperties>(
51 std::string("Incorrect value in the incoming data: session")
52 + " in Rev.3.2 mode with encryption/decryption active "
53 "and not"
54 + " enough data (less than 5 bytes for and odd "
55 "occurrence or "
56 + "less than 2 bytes for an even occurrence)",
57 typeid(IncorrectInputDataException))},
58 {0x6B00,
59 std::make_shared<StatusProperties>(
60 "Incorrect P1 or P2", typeid(IllegalParameterException))}});
61 return m;
62 }();
63
64CommandDigestUpdate::CommandDigestUpdate(
65 std::shared_ptr<CommandContextDto> context,
66 bool encryptedSession,
67 const std::vector<uint8_t>& digestData)
68: Command(CommandRef::DIGEST_UPDATE, 0, context)
69{
70 const std::uint8_t cla = context->getTargetSam()->getClassByte();
71 const std::uint8_t p1 = 0x00;
72 const std::uint8_t p2 = encryptedSession ? 0x80 : 0x00;
73
74 if (digestData.size() > 255) {
75 throw IllegalArgumentException(
76 "Digest data is too long. Expected 0-255 bytes, got "
77 + std::to_string(digestData.size()));
78 }
79
80 setApduRequest(
81 std::make_shared<ApduRequestAdapter>(ApduUtil::build(
82 cla, getCommandRef().getInstructionByte(), p1, p2, digestData)));
83}
84
85const std::vector<std::uint8_t>&
86CommandDigestUpdate::getProcessedData() const
87{
88 return mProcessedData;
89}
90
91void
92CommandDigestUpdate::finalizeRequest()
93{
94 /* Nothing to do */
95}
96
97bool
98CommandDigestUpdate::isControlSamRequiredToFinalizeRequest() const
99{
100 return false;
101}
102
103void
104CommandDigestUpdate::parseResponse(
105 const std::shared_ptr<ApduResponseApi> apduResponse)
106{
107 setResponseAndCheckStatus(apduResponse);
108
109 mProcessedData = apduResponse->getDataOut();
110}
111
112const std::map<int, const std::shared_ptr<StatusProperties>>&
113CommandDigestUpdate::getStatusTable() const
114{
115 return STATUS_TABLE;
116}
117
118} /* namespace legacysam */
119} /* namespace crypto */
120} /* namespace calypso */
121} /* namespace card */
122} /* namespace keyple */