keyple-card-calypso-crypto-legacysam-cpp-lib - 1.0.2
Component of the Keyple C++ middleware
CommandDigestInit.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/crypto/legacysam/CommandDigestInit.hpp"
15
16#include <map>
17#include <memory>
18#include <vector>
19
20#include "keyple/card/calypso/crypto/legacysam/AccessForbiddenException.hpp"
21#include "keyple/card/calypso/crypto/legacysam/CounterOverflowException.hpp"
22#include "keyple/card/calypso/crypto/legacysam/DataAccessException.hpp"
23#include "keyple/card/calypso/crypto/legacysam/IllegalParameterException.hpp"
24#include "keyple/core/util/ApduUtil.hpp"
25#include "keyple/core/util/cpp/System.hpp"
26#include "keyple/core/util/cpp/exception/IllegalArgumentException.hpp"
27
28namespace keyple {
29namespace card {
30namespace calypso {
31namespace crypto {
32namespace legacysam {
33
34using keyple::core::util::ApduUtil;
35using keyple::core::util::cpp::System;
36using keyple::core::util::cpp::exception::IllegalArgumentException;
37
38const std::map<int, const std::shared_ptr<StatusProperties>>
39 CommandDigestInit::STATUS_TABLE = [] {
40 std::map<int, const std::shared_ptr<StatusProperties>> m(
41 Command::STATUS_TABLE);
42
43 m.insert(
44 {{0x6700,
45 std::make_shared<StatusProperties>(
46 "Incorrect Lc", typeid(IllegalParameterException))},
47 {0x6900,
48 std::make_shared<StatusProperties>(
49 "An event counter cannot be incremented",
50 typeid(CounterOverflowException))},
51 {0x6985,
52 std::make_shared<StatusProperties>(
53 "Preconditions not satisfied",
54 typeid(AccessForbiddenException))},
55 {0x6A00,
56 std::make_shared<StatusProperties>(
57 "Incorrect P2", typeid(IllegalParameterException))},
58 {0x6A83,
59 std::make_shared<StatusProperties>(
60 "Record not found: signing key not found",
61 typeid(DataAccessException))}});
62 return m;
63 }();
64
65CommandDigestInit::CommandDigestInit(
66 std::shared_ptr<CommandContextDto> context,
67 bool verificationMode,
68 bool confidentialSessionMode,
69 std::uint8_t workKif,
70 std::uint8_t workKvc,
71 const std::vector<std::uint8_t>& digestData)
72: Command(CommandRef::DIGEST_INIT, 0, context)
73{
74 if (workKif == 0x00 || workKvc == 0x00) {
75 throw IllegalArgumentException(
76 "KIF or KVC is not set. KIF: " + std::to_string(workKif)
77 + ", KVC: " + std::to_string(workKvc) + "]");
78 }
79
80 const std::uint8_t cla = context->getTargetSam()->getClassByte();
81 std::uint8_t p1 = 0x00;
82
83 if (verificationMode) {
84 p1 += 1;
85 }
86
87 if (confidentialSessionMode) {
88 p1 += 2;
89 }
90
91 const std::uint8_t p2 = 0xFF;
92
93 std::vector<std::uint8_t> dataIn(2 + digestData.size());
94 dataIn[0] = workKif;
95 dataIn[1] = workKvc;
96
97 System::arraycopy(digestData, 0, dataIn, 2, digestData.size());
98
99 setApduRequest(
100 std::make_shared<ApduRequestAdapter>(ApduUtil::build(
101 cla, getCommandRef().getInstructionByte(), p1, p2, dataIn)));
102}
103
104void
105CommandDigestInit::finalizeRequest()
106{
107 /* nothing to do */
108}
109
110bool
111CommandDigestInit::isControlSamRequiredToFinalizeRequest() const
112{
113 return false;
114}
115
116void
117CommandDigestInit::parseResponse(std::shared_ptr<ApduResponseApi> apduResponse)
118{
119 setResponseAndCheckStatus(apduResponse);
120}
121
122const std::map<int, const std::shared_ptr<StatusProperties>>&
123CommandDigestInit::getStatusTable() const
124{
125 return STATUS_TABLE;
126}
127
128} /* namespace legacysam */
129} /* namespace crypto */
130} /* namespace calypso */
131} /* namespace card */
132} /* namespace keyple */