keyple-card-calypso-crypto-legacysam-cpp-lib - 1.0.2
Component of the Keyple C++ middleware
CommandDigestAuthenticate.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/CommandDigestAuthenticate.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/IllegalParameterException.hpp"
22#include "keyple/card/calypso/crypto/legacysam/SecurityDataException.hpp"
23#include "keyple/core/util/ApduUtil.hpp"
24#include "keyple/core/util/cpp/exception/IllegalArgumentException.hpp"
25
26namespace keyple {
27namespace card {
28namespace calypso {
29namespace crypto {
30namespace legacysam {
31
32using keyple::core::util::ApduUtil;
33using keyple::core::util::cpp::exception::IllegalArgumentException;
34
35const std::map<int, const std::shared_ptr<StatusProperties>>
36 CommandDigestAuthenticate::STATUS_TABLE = [] {
37 std::map<int, const std::shared_ptr<StatusProperties>> m(
38 Command::STATUS_TABLE);
39
40 m.insert(
41 {{0x6700,
42 std::make_shared<StatusProperties>(
43 "Incorrect Lc", typeid(IllegalParameterException))},
44 {0x6985,
45 std::make_shared<StatusProperties>(
46 "Preconditions not satisfied",
47 typeid(AccessForbiddenException))},
48 {0x6988,
49 std::make_shared<StatusProperties>(
50 "Incorrect signature", typeid(SecurityDataException))}});
51 return m;
52 }();
53
54CommandDigestAuthenticate::CommandDigestAuthenticate(
55 std::shared_ptr<CommandContextDto> context,
56 const std::vector<uint8_t>& signature)
57: Command(CommandRef::DIGEST_AUTHENTICATE, 0, context)
58{
59 if (signature.size() != 4 && signature.size() != 8
60 && signature.size() != 16) {
61 throw IllegalArgumentException(
62 "Signature is not the right length. Expected: 4|8|16, Actual: "
63 + std::to_string(signature.size()));
64 }
65
66 const std::uint8_t cla = context->getTargetSam()->getClassByte();
67 const std::uint8_t p1 = 0x00;
68 const std::uint8_t p2 = 0x00;
69
70 setApduRequest(
71 std::make_shared<ApduRequestAdapter>(ApduUtil::build(
72 cla, getCommandRef().getInstructionByte(), p1, p2, signature)));
73}
74
75void
76CommandDigestAuthenticate::finalizeRequest()
77{
78 /* nothing to do */
79}
80
81bool
82CommandDigestAuthenticate::isControlSamRequiredToFinalizeRequest() const
83{
84 return false;
85}
86
87void
88CommandDigestAuthenticate::parseResponse(
89 std::shared_ptr<ApduResponseApi> apduResponse)
90{
91 setResponseAndCheckStatus(apduResponse);
92}
93
94const std::map<int, const std::shared_ptr<StatusProperties>>&
95CommandDigestAuthenticate::getStatusTable() const
96{
97 return STATUS_TABLE;
98}
99
100} /* namespace legacysam */
101} /* namespace crypto */
102} /* namespace calypso */
103} /* namespace card */
104} /* namespace keyple */