keyple-card-calypso-crypto-legacysam-cpp-lib - 1.0.2
Component of the Keyple C++ middleware
CommandWriteSamParameters.cpp
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (c) 2019 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/CommandWriteSamParameters.hpp"
15
16#include <cstdint>
17#include <map>
18#include <memory>
19#include <string>
20#include <vector>
21
22#include "keyple/card/calypso/crypto/legacysam/AccessForbiddenException.hpp"
23#include "keyple/card/calypso/crypto/legacysam/CommandGiveRandom.hpp"
24#include "keyple/card/calypso/crypto/legacysam/CommandSamDataCipher.hpp"
25#include "keyple/card/calypso/crypto/legacysam/CommandSamGenerateKey.hpp"
26#include "keyple/card/calypso/crypto/legacysam/CommandSelectDiversifier.hpp"
27#include "keyple/card/calypso/crypto/legacysam/CounterOverflowException.hpp"
28#include "keyple/card/calypso/crypto/legacysam/DtoAdapters.hpp"
29#include "keyple/card/calypso/crypto/legacysam/IllegalParameterException.hpp"
30#include "keyple/card/calypso/crypto/legacysam/IncorrectInputDataException.hpp"
31#include "keyple/card/calypso/crypto/legacysam/LegacySamUtil.hpp"
32#include "keyple/card/calypso/crypto/legacysam/SecurityDataException.hpp"
33#include "keyple/core/util/ApduUtil.hpp"
34#include "keyple/core/util/ByteArrayUtil.hpp"
35#include "keyple/core/util/cpp/System.hpp"
36#include "keypop/calypso/crypto/legacysam/SystemKeyType.hpp"
37#include "keypop/card/ApduResponseApi.hpp"
38
39namespace keyple {
40namespace card {
41namespace calypso {
42namespace crypto {
43namespace legacysam {
44
45using keyple::core::util::ApduUtil;
46using keyple::core::util::ByteArrayUtil;
47using keyple::core::util::cpp::System;
48
50
51const std::map<int, const std::shared_ptr<StatusProperties>>
52 CommandWriteSamParameters::STATUS_TABLE = [] {
53 std::map<int, const std::shared_ptr<StatusProperties>> m(
54 Command::STATUS_TABLE);
55
56 m.insert(
57 {{0x6700,
58 std::make_shared<StatusProperties>(
59 "Incorrect Lc", typeid(IllegalParameterException))},
60 {0x6985,
61 std::make_shared<StatusProperties>(
62 std::string("Preconditions not satisfied:\n")
63 + "- The SAM is locked.\n"
64 + "- In P1, b7 is equal to 1.\n"
65 + "- CipherEnableBit of PAR1 is 0.\n"
66 + "- Dynamic mode and the outgoing challenge is "
67 "unavailable.\n"
68 + "- SystemLockEnableBit=1.\n"
69 + "- Static mode and StaticCipherEnableBit=0",
70 typeid(AccessForbiddenException))},
71 {0x6900,
72 std::make_shared<StatusProperties>(
73 "An event counter cannot be incremented",
74 typeid(CounterOverflowException))},
75 {0x6988,
76 std::make_shared<StatusProperties>(
77 "Incorrect signature", typeid(SecurityDataException))},
78 {0x6A00,
79 std::make_shared<StatusProperties>(
80 "Incorrect P2", typeid(IllegalParameterException))},
81 {0x6A80,
82 std::make_shared<StatusProperties>(
83 "Incorrect decrypted data",
84 typeid(IncorrectInputDataException))},
85 {0x6A83,
86 std::make_shared<StatusProperties>(
87 "Record not found: deciphering key not found",
88 typeid(DataAccessException))}});
89 return m;
90 }();
91
92CommandWriteSamParameters::CommandWriteSamParameters(
93 std::shared_ptr<CommandContextDto> context,
94 const std::vector<std::uint8_t>& samParameters)
95: Command(CommandRef::WRITE_PARAMETERS, 0, context)
96{
97 mPlainData = std::vector<std::uint8_t>(30);
98 System::arraycopy(samParameters, 0, mPlainData, 1, samParameters.size());
99}
100
101void
102CommandWriteSamParameters::finalizeRequest()
103{
104 mPlainData[0] = getContext()
105 ->getTargetSam()
106 ->getSystemKeyParameter(SystemKeyType::PERSONALIZATION)
107 ->getKvc();
108
109 auto controlSamContext = std::make_shared<CommandContextDto>(
110 getContext()->getControlSam(), nullptr, nullptr);
111
112 /* Add commands */
113 addControlSamCommand(
114 std::make_shared<CommandSelectDiversifier>(
115 controlSamContext,
116 getContext()->getTargetSam()->getSerialNumber()));
117
118 addControlSamCommand(
119 std::make_shared<CommandGiveRandom>(
120 controlSamContext, getContext()->getTargetSam()->popChallenge()));
121
122 auto commandSamDataCipher = std::make_shared<CommandSamDataCipher>(
123 controlSamContext,
124 0,
125 CommandSamDataCipher::DataType::PARAMETERS_RECORD,
126 mPlainData);
127
128 addControlSamCommand(commandSamDataCipher);
129 processControlSamCommand();
130
131 const std::uint8_t cla = getContext()->getTargetSam()->getClassByte();
132 const std::uint8_t inst = CommandRef::WRITE_PARAMETERS.getInstructionByte();
133 const std::uint8_t p1 = LegacySamConstants::DYNAMIC_MODE_CIPHERING;
134 const std::uint8_t p2 = 0xA0;
135
136 setApduRequest(
137 std::make_shared<ApduRequestAdapter>(ApduUtil::build(
138 cla, inst, p1, p2, commandSamDataCipher->getCipheredData())));
139}
140
141bool
142CommandWriteSamParameters::isControlSamRequiredToFinalizeRequest() const
143{
144 return true;
145}
146
147void
148CommandWriteSamParameters::parseResponse(
149 std::shared_ptr<ApduResponseApi> apduResponse)
150{
151 setResponseAndCheckStatus(apduResponse);
152}
153
154const std::map<int, const std::shared_ptr<StatusProperties>>&
155CommandWriteSamParameters::getStatusTable() const
156{
157 return STATUS_TABLE;
158}
159
160} /* namespace legacysam */
161} /* namespace crypto */
162} /* namespace calypso */
163} /* namespace card */
164} /* namespace keyple */
DtoAdapters::CommandContextDto CommandContextDto