keyple-card-calypso-crypto-legacysam-cpp-lib - 1.0.2
Component of the Keyple C++ middleware
CommandSelectDiversifier.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/CommandSelectDiversifier.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/CommandRef.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
27namespace keyple {
28namespace card {
29namespace calypso {
30namespace crypto {
31namespace legacysam {
32
33using keyple::card::calypso::crypto::legacysam::CommandRef;
34using keyple::core::util::ApduUtil;
35using keyple::core::util::cpp::System;
36
37const std::map<int, const std::shared_ptr<StatusProperties>>
38 CommandSelectDiversifier::STATUS_TABLE = [] {
39 std::map<int, const std::shared_ptr<StatusProperties>> m(
40 Command::STATUS_TABLE);
41
42 m.insert(
43 {0x6700,
44 std::make_shared<StatusProperties>(
45 "Incorrect Lc", typeid(IllegalParameterException))});
46 m.insert(
47 {0x6985,
48 std::make_shared<StatusProperties>(
49 "Preconditions not satisfied: the SAM is locked",
50 typeid(AccessForbiddenException))});
51
52 return m;
53 }();
54
55CommandSelectDiversifier::CommandSelectDiversifier(
56 std::shared_ptr<DtoAdapters::CommandContextDto> context,
57 const std::vector<uint8_t>& diversifier)
58: Command(CommandRef::SELECT_DIVERSIFIER, 0, context)
59{
60 auto _diversifier = diversifier;
61
62 /* Format the diversifier on 4 or 8 bytes if needed. */
63 if (diversifier.size() != 4 && diversifier.size() != 8) {
64 const int newLength = diversifier.size() < 4 ? 4 : 8;
65 std::vector<uint8_t> tmp(newLength);
66 System::arraycopy(
67 diversifier,
68 0,
69 tmp,
70 newLength - diversifier.size(),
71 diversifier.size());
72 _diversifier = tmp;
73 }
74
75 setApduRequest(
76 std::make_shared<ApduRequestAdapter>(ApduUtil::build(
77 context->getTargetSam()->getClassByte(),
78 getCommandRef().getInstructionByte(),
79 0,
80 0,
81 _diversifier)));
82}
83
84void
85CommandSelectDiversifier::finalizeRequest()
86{
87 /* Nothing to do */
88}
89
90bool
91CommandSelectDiversifier::isControlSamRequiredToFinalizeRequest() const
92{
93 return false;
94}
95
96void
97CommandSelectDiversifier::parseResponse(
98 std::shared_ptr<ApduResponseApi> apduResponse)
99{
100 setResponseAndCheckStatus(apduResponse);
101}
102
103const std::map<int, const std::shared_ptr<StatusProperties>>&
104CommandSelectDiversifier::getStatusTable() const
105{
106 return STATUS_TABLE;
107}
108
109} /* namespace legacysam */
110} /* namespace crypto */
111} /* namespace calypso */
112} /* namespace card */
113} /* namespace keyple */