Keyple Card Calypso C++ Library 2.2.5.6
Reference Terminal Reader API for C++
CmdSamCardGenerateKey.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 information regarding *
5 * copyright ownership. *
6 * *
7 * This program and the accompanying materials are made available under the terms of the Eclipse *
8 * Public License 2.0 which is available at http://www.eclipse.org/legal/epl-2.0 *
9 * *
10 * SPDX-License-Identifier: EPL-2.0 *
11 **************************************************************************************************/
12
14
15/* Keyple Card Calypso */
20#include "SamUtilAdapter.h"
21
22/* Keyple Core Util */
23#include "ApduUtil.h"
24#include "IllegalArgumentException.h"
25
26namespace keyple {
27namespace card {
28namespace calypso {
29
30using namespace keyple::core::util;
31using namespace keyple::core::util::cpp::exception;
32
33const CalypsoSamCommand CmdSamCardGenerateKey::mCommand = CalypsoSamCommand::CARD_GENERATE_KEY;
34
35const std::map<const int, const std::shared_ptr<StatusProperties>>
36 CmdSamCardGenerateKey::STATUS_TABLE = initStatusTable();
37
38CmdSamCardGenerateKey::CmdSamCardGenerateKey(const std::shared_ptr<CalypsoSamAdapter> calypsoSam,
39 const uint8_t cipheringKif,
40 const uint8_t cipheringKvc,
41 const uint8_t sourceKif,
42 const uint8_t sourceKvc)
43: AbstractSamCommand(mCommand, -1, calypsoSam)
44{
45 const uint8_t cla = SamUtilAdapter::getClassByte(calypsoSam->getProductType());
46
47 uint8_t p1;
48 uint8_t p2;
49 std::vector<uint8_t> data;
50
51 if (cipheringKif == 0 && cipheringKvc == 0) {
52
53 /* Case where the source key is ciphered by the null key */
54 p1 = 0xFF;
55 p2 = 0x00;
56
57 data = std::vector<uint8_t>(3);
58 data[0] = sourceKif;
59 data[1] = sourceKvc;
60 data[2] = 0x90;
61
62 } else {
63
64 p1 = 0xFF;
65 p2 = 0xFF;
66
67 data = std::vector<uint8_t>(5);
68 data[0] = cipheringKif;
69 data[1] = cipheringKvc;
70 data[2] = sourceKif;
71 data[3] = sourceKvc;
72 data[4] = 0x90;
73 }
74
76 std::make_shared<ApduRequestAdapter>(
77 ApduUtil::build(cla, mCommand.getInstructionByte(), p1, p2, data)));
78}
79
80const std::vector<uint8_t> CmdSamCardGenerateKey::getCipheredData() const
81{
82 return isSuccessful() ? getApduResponse()->getDataOut() : std::vector<uint8_t>();
83}
84
85const std::map<const int, const std::shared_ptr<StatusProperties>>
86 CmdSamCardGenerateKey::initStatusTable()
87{
88 std::map<const int, const std::shared_ptr<StatusProperties>> m =
90
91 m.insert({0x6700,
92 std::make_shared<StatusProperties>("Incorrect Lc.",
94 m.insert({0x6985,
95 std::make_shared<StatusProperties>("Preconditions not satisfied.",
96 typeid(CalypsoSamAccessForbiddenException))});
97 m.insert({0x6A00,
98 std::make_shared<StatusProperties>("Incorrect P1 or P2",
99 typeid(CalypsoSamIllegalParameterException))});
100 m.insert({0x6A80,
101 std::make_shared<StatusProperties>("Incorrect incoming data: unknown or incorrect " \
102 "format",
103 typeid(CalypsoSamIncorrectInputDataException))});
104 m.insert({0x6A83,
105 std::make_shared<StatusProperties>("Record not found: ciphering key or key to " \
106 "cipher not found",
107 typeid(CalypsoSamDataAccessException))});
108
109 return m;
110}
111
112const std::map<const int, const std::shared_ptr<StatusProperties>>&
114{
115 return STATUS_TABLE;
116}
117
118}
119}
120}
virtual const std::shared_ptr< ApduResponseApi > getApduResponse() const final
virtual void setApduRequest(const std::shared_ptr< ApduRequestAdapter > apduRequest) final
static const std::map< const int, const std::shared_ptr< StatusProperties > > STATUS_TABLE
static const CalypsoSamCommand CARD_GENERATE_KEY
const std::map< const int, const std::shared_ptr< StatusProperties > > & getStatusTable() const override
CmdSamCardGenerateKey(const std::shared_ptr< CalypsoSamAdapter > calypsoSam, const uint8_t cipheringKif, const uint8_t cipheringKvc, const uint8_t sourceKif, const uint8_t sourceKvc)
const std::vector< uint8_t > getCipheredData() const
static uint8_t getClassByte(const ProductType productType)