keyple-card-calypso-crypto-legacysam-cpp-lib - 1.0.2
Component of the Keyple C++ middleware
CommandCardGenerateKey.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/CommandCardGenerateKey.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/DataAccessException.hpp"
22#include "keyple/card/calypso/crypto/legacysam/DtoAdapters.hpp"
23#include "keyple/card/calypso/crypto/legacysam/IllegalParameterException.hpp"
24#include "keyple/card/calypso/crypto/legacysam/IncorrectInputDataException.hpp"
25#include "keyple/core/util/ApduUtil.hpp"
26
27namespace keyple {
28namespace card {
29namespace calypso {
30namespace crypto {
31namespace legacysam {
32
33using keyple::core::util::ApduUtil;
34
35const std::map<int, const std::shared_ptr<StatusProperties>>
36 CommandCardGenerateKey::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 {0x6A00,
49 std::make_shared<StatusProperties>(
50 "Incorrect P1 or P2", typeid(IllegalParameterException))},
51 {0x6A80,
52 std::make_shared<StatusProperties>(
53 "Incorrect incoming data: unknown or incorrect format",
54 typeid(IncorrectInputDataException))},
55 {0x6A83,
56 std::make_shared<StatusProperties>(
57 "Record not found: ciphering key or key to cipher not found",
58 typeid(DataAccessException))}});
59 return m;
60 }();
61
62CommandCardGenerateKey::CommandCardGenerateKey(
63 std::shared_ptr<DtoAdapters::CommandContextDto> context,
64 uint8_t cipheringKif,
65 uint8_t cipheringKvc,
66 uint8_t sourceKif,
67 uint8_t sourceKvc)
68: Command(CommandRef::CARD_GENERATE_KEY, 0, context)
69{
70 const uint8_t cla = context->getTargetSam()->getClassByte();
71
72 uint8_t p1;
73 uint8_t p2;
74 std::vector<uint8_t> data;
75
76 if (cipheringKif == 0 && cipheringKvc == 0) {
77 /* Case where the source key is ciphered by the null key */
78 p1 = 0xFF;
79 p2 = 0x00;
80
81 data.resize(3);
82 data[0] = sourceKif;
83 data[1] = sourceKvc;
84 data[2] = 0x90;
85
86 } else {
87 p1 = 0xFF;
88 p2 = 0xFF;
89
90 data.resize(5);
91 data[0] = cipheringKif;
92 data[1] = cipheringKvc;
93 data[2] = sourceKif;
94 data[3] = sourceKvc;
95 data[4] = 0x90;
96 }
97
98 setApduRequest(
99 std::make_shared<ApduRequestAdapter>(core::util::ApduUtil::build(
100 cla, getCommandRef().getInstructionByte(), p1, p2, data)));
101}
102
103const std::vector<uint8_t>&
104CommandCardGenerateKey::getCipheredData() const
105{
106 return mCipheredData;
107}
108
109void
110CommandCardGenerateKey::finalizeRequest()
111{
112 /* Nothing to do */
113}
114
115bool
116CommandCardGenerateKey::isControlSamRequiredToFinalizeRequest() const
117{
118 return false;
119}
120
121void
122CommandCardGenerateKey::parseResponse(
123 std::shared_ptr<ApduResponseApi> apduResponse)
124{
125 setResponseAndCheckStatus(apduResponse);
126 mCipheredData = apduResponse->getDataOut();
127}
128
129const std::map<int, const std::shared_ptr<StatusProperties>>&
130CommandCardGenerateKey::getStatusTable() const
131{
132 return STATUS_TABLE;
133}
134
135} /* namespace legacysam */
136} /* namespace crypto */
137} /* namespace calypso */
138} /* namespace card */
139} /* namespace keyple */