Keyple Card Calypso C++ Library 2.2.5.6
Reference Terminal Reader API for C++
CmdSamWriteKey.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
13#include "CmdSamWriteKey.h"
14
15/* Keyple Card Calypso */
22#include "SamUtilAdapter.h"
23
24/* Keyple Core Util */
25#include "ApduUtil.h"
26#include "IllegalArgumentException.h"
27
28namespace keyple {
29namespace card {
30namespace calypso {
31
32using namespace keyple::core::util;
33using namespace keyple::core::util::cpp::exception;
34
35const CalypsoSamCommand CmdSamWriteKey::mCommand = CalypsoSamCommand::WRITE_KEY;
36const std::map<const int, const std::shared_ptr<StatusProperties>>
37 CmdSamWriteKey::STATUS_TABLE = initStatusTable();
38
39
41 const std::shared_ptr<CalypsoSamAdapter> calypsoSam,
42 const uint8_t writingMode,
43 const uint8_t keyReference,
44 const std::vector<uint8_t>& keyData)
45: AbstractSamCommand(mCommand, -1, calypsoSam)
46{
47 const uint8_t cla = SamUtilAdapter::getClassByte(calypsoSam->getProductType());
48
49 if (keyData.empty()) {
50
51 throw IllegalArgumentException("Key data null!");
52 }
53
54 if (keyData.size() < 48 && keyData.size() > 80) {
55
56 throw IllegalArgumentException("Key data should be between 40 and 80 bytes long!");
57 }
58
60 std::make_shared<ApduRequestAdapter>(
61 ApduUtil::build(cla,
62 mCommand.getInstructionByte(),
63 writingMode,
64 keyReference,
65 keyData)));
66}
67
68const std::map<const int, const std::shared_ptr<StatusProperties>>& CmdSamWriteKey::getStatusTable()
69 const
70{
71 return STATUS_TABLE;
72}
73
74const std::map<const int, const std::shared_ptr<StatusProperties>> CmdSamWriteKey::initStatusTable()
75{
76 std::map<const int, const std::shared_ptr<StatusProperties>> m =
78
79 m.insert({0x6700,
80 std::make_shared<StatusProperties>("Incorrect Lc.",
82 m.insert({0x6900,
83 std::make_shared<StatusProperties>("An event counter cannot be incremented.",
84 typeid(CalypsoSamCounterOverflowException))});
85 m.insert({0x6985,
86 std::make_shared<StatusProperties>("Preconditions not satisfied.",
87 typeid(CalypsoSamAccessForbiddenException))});
88 m.insert({0x6988,
89 std::make_shared<StatusProperties>("Incorrect signature.",
90 typeid(CalypsoSamSecurityDataException))});
91 m.insert({0x6A00,
92 std::make_shared<StatusProperties>("P1 or P2 incorrect.",
93 typeid(CalypsoSamIllegalParameterException))});
94 m.insert({0x6A80,
95 std::make_shared<StatusProperties>("Incorrect plain or decrypted data.",
96 typeid(CalypsoSamIncorrectInputDataException))});
97 m.insert({0x6A83,
98 std::make_shared<StatusProperties>("Record not found: deciphering key not found.",
99 typeid(CalypsoSamDataAccessException))});
100 m.insert({0x6A87,
101 std::make_shared<StatusProperties>("Lc inconsistent with P1 or P2.",
102 typeid(CalypsoSamIncorrectInputDataException))});
103
104 return m;
105}
106
107}
108}
109}
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 WRITE_KEY
CmdSamWriteKey(const std::shared_ptr< CalypsoSamAdapter > calypsoSam, const uint8_t writingMode, const uint8_t keyReference, const std::vector< uint8_t > &keyData)
const std::map< const int, const std::shared_ptr< StatusProperties > > & getStatusTable() const override
static uint8_t getClassByte(const ProductType productType)