Keyple Card Calypso C++ Library 2.1.0
Reference Terminal Reader API for C++
CmdSamCardGenerateKey.cpp
Go to the documentation of this file.
1/**************************************************************************************************
2 * Copyright (c) 2022 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
39 const uint8_t cipheringKif,
40 const uint8_t cipheringKvc,
41 const uint8_t sourceKif,
42 const uint8_t sourceKvc)
43: AbstractSamCommand(mCommand)
44{
45 const uint8_t cla = SamUtilAdapter::getClassByte(productType);
46
47 uint8_t p1;
48 uint8_t p2;
49 std::vector<uint8_t> data;
50
51 if (cipheringKif == 0 && cipheringKvc == 0) {
52 /* Case where the source key is ciphered by the null key */
53 p1 = 0xFF;
54 p2 = 0x00;
55
56 data = std::vector<uint8_t>(3);
57 data[0] = sourceKif;
58 data[1] = sourceKvc;
59 data[2] = 0x90;
60 } else {
61 p1 = 0xFF;
62 p2 = 0xFF;
63
64 data = std::vector<uint8_t>(5);
65 data[0] = cipheringKif;
66 data[1] = cipheringKvc;
67 data[2] = sourceKif;
68 data[3] = sourceKvc;
69 data[4] = 0x90;
70 }
71
73 std::make_shared<ApduRequestAdapter>(
74 ApduUtil::build(cla, mCommand.getInstructionByte(), p1, p2, data)));
75}
76
77const std::vector<uint8_t> CmdSamCardGenerateKey::getCipheredData() const
78{
79 return isSuccessful() ? getApduResponse()->getDataOut() : std::vector<uint8_t>();
80}
81
82const std::map<const int, const std::shared_ptr<StatusProperties>>
83 CmdSamCardGenerateKey::initStatusTable()
84{
85 std::map<const int, const std::shared_ptr<StatusProperties>> m =
87
88 m.insert({0x6700,
89 std::make_shared<StatusProperties>("Incorrect Lc.",
91 m.insert({0x6985,
92 std::make_shared<StatusProperties>("Preconditions not satisfied.",
93 typeid(CalypsoSamAccessForbiddenException))});
94 m.insert({0x6A00,
95 std::make_shared<StatusProperties>("Incorrect P1 or P2",
96 typeid(CalypsoSamIllegalParameterException))});
97 m.insert({0x6A80,
98 std::make_shared<StatusProperties>("Incorrect incoming data: unknown or incorrect " \
99 "format",
100 typeid(CalypsoSamIncorrectInputDataException))});
101 m.insert({0x6A83,
102 std::make_shared<StatusProperties>("Record not found: ciphering key or key to " \
103 "cipher not found",
104 typeid(CalypsoSamDataAccessException))});
105
106 return m;
107}
108
109const std::map<const int, const std::shared_ptr<StatusProperties>>&
111{
112 return STATUS_TABLE;
113}
114
115}
116}
117}
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
CmdSamCardGenerateKey(const CalypsoSam::ProductType productType, const uint8_t cipheringKif, const uint8_t cipheringKvc, const uint8_t sourceKif, const uint8_t sourceKvc)
const std::map< const int, const std::shared_ptr< StatusProperties > > & getStatusTable() const override
const std::vector< uint8_t > getCipheredData() const
static uint8_t getClassByte(const ProductType productType)
CalypsoSam::ProductType ProductType