keyple-card-calypso-crypto-legacysam-cpp-lib - 1.0.2
Component of the Keyple C++ middleware
CommandUnlock.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/CommandUnlock.hpp"
15
16#include <map>
17#include <memory>
18#include <stdexcept>
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/SecurityDataException.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 CommandUnlock::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 Le", typeid(IllegalParameterException))},
45 {0x6985,
46 std::make_shared<StatusProperties>(
47 "Preconditions not satisfied (SAM not locked?)",
48 typeid(AccessForbiddenException))},
49 {0x6988,
50 std::make_shared<StatusProperties>(
51 "Incorrect UnlockData", typeid(SecurityDataException))}});
52
53 return m;
54 }();
55
56CommandUnlock::CommandUnlock(
57 LegacySam::ProductType productType, const std::vector<uint8_t>& unlockData)
58: Command(CommandRef::UNLOCK, 0, nullptr)
59{
60 const uint8_t cla
61 = productType == LegacySam::ProductType::SAM_S1DX ? 0x94 : 0x80;
62 const uint8_t p1 = 0x00;
63 const uint8_t p2 = 0x00;
64
65 if (unlockData.size() != 8 && unlockData.size() != 16) {
66 throw IllegalArgumentException(
67 "Unlock data is an incorrect length. Expected 8 or 16 bytes, got "
68 + std::to_string(unlockData.size()));
69 }
70
71 setApduRequest(
72 std::make_shared<ApduRequestAdapter>(ApduUtil::build(
73 cla, getCommandRef().getInstructionByte(), p1, p2, unlockData)));
74}
75
76void
77CommandUnlock::finalizeRequest()
78{
79 /* nothing to do */
80}
81
82bool
83CommandUnlock::isControlSamRequiredToFinalizeRequest() const
84{
85 return false;
86}
87
88void
89CommandUnlock::parseResponse(std::shared_ptr<ApduResponseApi> apduResponse)
90{
91 setResponseAndCheckStatus(apduResponse);
92}
93
94const std::map<int, const std::shared_ptr<StatusProperties>>&
95CommandUnlock::getStatusTable() const
96{
97 return STATUS_TABLE;
98}
99
100} /* namespace legacysam */
101} /* namespace crypto */
102} /* namespace calypso */
103} /* namespace card */
104} /* namespace keyple */