keyple-card-calypso-crypto-legacysam-cpp-lib - 1.0.2
Component of the Keyple C++ middleware
CommandSvCheck.cpp
Go to the documentation of this file.
1
2/******************************************************************************
3 * Copyright (c) 2025 Calypso Networks Association https://calypsonet.org/ *
4 * *
5 * See the NOTICE file(s) distributed with this work for additional *
6 * information regarding copyright ownership. *
7 * *
8 * This program and the accompanying materials are made available under the *
9 * terms of the Eclipse Public License 2.0 which is available at *
10 * http://www.eclipse.org/legal/epl-2.0 *
11 * *
12 * SPDX-License-Identifier: EPL-2.0 *
13 ******************************************************************************/
14
15#include "keyple/card/calypso/crypto/legacysam/CommandSvCheck.hpp"
16
17#include <map>
18#include <memory>
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/LegacySamConstants.hpp"
24#include "keyple/card/calypso/crypto/legacysam/SecurityDataException.hpp"
25#include "keyple/core/util/ApduUtil.hpp"
26#include "keyple/core/util/ByteArrayUtil.hpp"
27#include "keyple/core/util/cpp/Arrays.hpp"
28#include "keyple/core/util/cpp/System.hpp"
29#include "keyple/core/util/cpp/exception/IllegalArgumentException.hpp"
30#include "keypop/calypso/crypto/legacysam/transaction/InvalidSignatureException.hpp"
31
32namespace keyple {
33namespace card {
34namespace calypso {
35namespace crypto {
36namespace legacysam {
37
38using keyple::core::util::ApduUtil;
39using keyple::core::util::ByteArrayUtil;
40using keyple::core::util::cpp::Arrays;
41using keyple::core::util::cpp::System;
42using keyple::core::util::cpp::exception::IllegalArgumentException;
43using keypop::calypso::crypto::legacysam::transaction::
44 InvalidSignatureException;
45
46const std::map<int, const std::shared_ptr<StatusProperties>>
47 CommandSvCheck::STATUS_TABLE = [] {
48 std::map<int, const std::shared_ptr<StatusProperties>> m(
49 Command::STATUS_TABLE);
50
51 m.insert(
52 {{0x6700,
53 std::make_shared<StatusProperties>(
54 "Incorrect Lc", typeid(IllegalParameterException))},
55 {0x6985,
56 std::make_shared<StatusProperties>(
57 "Preconditions not satisfied",
58 typeid(AccessForbiddenException))},
59 {0x6988,
60 std::make_shared<StatusProperties>(
61 "Incorrect signature", typeid(SecurityDataException))}});
62 return m;
63 }();
64
65CommandSvCheck::CommandSvCheck(std::shared_ptr<CommandContextDto> context)
66: Command(CommandRef::SV_CHECK, 0, context)
67{
68 const std::uint8_t cla = context->getTargetSam()->getClassByte();
69 const std::uint8_t p1 = 0x00;
70 const std::uint8_t p2 = 0x00;
71
72 /* The operation is "abort" */
73 const std::vector<std::uint8_t> noInput = {0};
74 setApduRequest(
75 std::make_shared<ApduRequestAdapter>(ApduUtil::build(
76 cla,
77 getCommandRef().getInstructionByte(),
78 p1,
79 p2,
80 noInput))); // Case 3 without input data.
81}
82
83CommandSvCheck::CommandSvCheck(
84 std::shared_ptr<CommandContextDto> context,
85 const std::vector<std::uint8_t>& svCardSignature)
86: Command(CommandRef::SV_CHECK, 0, context)
87{
88 if (svCardSignature.size() != 3 && svCardSignature.size() != 6) {
89 throw IllegalArgumentException(
90 "Invalid SV card signature. Expected 3 or 6 bytes, got "
91 + std::to_string(svCardSignature.size()));
92 }
93
94 const std::uint8_t cla = context->getTargetSam()->getClassByte();
95 const std::uint8_t p1 = 0x00;
96 const std::uint8_t p2 = 0x00;
97
98 /* The operation is not "abort" */
99 setApduRequest(
100 std::make_shared<ApduRequestAdapter>(ApduUtil::build(
101 cla,
102 getCommandRef().getInstructionByte(),
103 p1,
104 p2,
105 svCardSignature)));
106}
107
108void
109CommandSvCheck::finalizeRequest()
110{
111 /* Nothing to do */
112}
113
114bool
115CommandSvCheck::isControlSamRequiredToFinalizeRequest() const
116{
117 return false;
118}
119
120void
121CommandSvCheck::parseResponse(std::shared_ptr<ApduResponseApi> apduResponse)
122{
123 setResponseAndCheckStatus(apduResponse);
124}
125
126const std::map<int, const std::shared_ptr<StatusProperties>>&
127CommandSvCheck::getStatusTable() const
128{
129 return STATUS_TABLE;
130}
131
132} /* namespace legacysam */
133} /* namespace crypto */
134} /* namespace calypso */
135} /* namespace card */
136} /* namespace keyple */