Keyple Card Calypso C++ Library 2.2.2
Reference Terminal Reader API for C++
CardSecuritySettingAdapter.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 Core Util */
16#include "Arrays.h"
17#include "KeypleAssert.h"
18
19namespace keyple {
20namespace card {
21namespace calypso {
22
23using namespace keyple::core::util;
24using namespace keyple::core::util::cpp;
25
27
28const std::string CardSecuritySettingAdapter::WRITE_ACCESS_LEVEL = "writeAccessLevel";
29
31 const std::shared_ptr<CardReader> samReader, const std::shared_ptr<CalypsoSam> calypsoSam)
32{
33
34 return setControlSamResource(samReader, calypsoSam);
35}
36
38{
39 mIsMultipleSessionEnabled = true;
40
41 return *this;
42}
43
45{
46 mIsRatificationMechanismEnabled = true;
47
48 return *this;
49}
50
52{
53 mIsPinPlainTransmissionEnabled = true;
54
55 return *this;
56}
57
59{
60 mIsSvLoadAndDebitLogEnabled = true;
61
62 return *this;
63}
64
66{
67 mIsSvNegativeBalanceAuthorized = true;
68
69 return *this;
70}
71
73 const WriteAccessLevel writeAccessLevel, const uint8_t kvc, const uint8_t kif)
74{
75 /* Map will be auto-created if not existing */
76 mKifMap[writeAccessLevel].insert({kvc, kif});
77
78 return *this;
79}
80
82 const WriteAccessLevel writeAccessLevel, const uint8_t kif)
83{
84 mDefaultKifMap.insert({writeAccessLevel, kif});
85
86 return *this;
87}
88
90 const WriteAccessLevel writeAccessLevel, const uint8_t kvc)
91{
92 mDefaultKvcMap.insert({writeAccessLevel, kvc});
93
94 return *this;
95}
96
98 const uint8_t kvc)
99{
100 mAuthorizedSessionKeys.push_back(((kif << 8) & 0xff00) | (kvc & 0x00ff));
101
102 return *this;
103}
104
106 const uint8_t kvc)
107{
108 mAuthorizedSvKeys.push_back(((kif << 8) & 0xff00) | (kvc & 0x00ff));
109
110 return *this;
111}
112
114 const uint8_t kif, const uint8_t kvc)
115{
116 mPinVerificationCipheringKif = std::make_shared<uint8_t>(kif);
117 mPinVerificationCipheringKvc = std::make_shared<uint8_t>(kvc);
118
119 return *this;
120}
121
123 const uint8_t kif, const uint8_t kvc)
124{
125 mPinModificationCipheringKif = std::make_shared<uint8_t>(kif);
126 mPinModificationCipheringKvc = std::make_shared<uint8_t>(kvc);
127
128 return *this;
129}
130
132{
133 return mIsMultipleSessionEnabled;
134}
135
137{
138 return mIsRatificationMechanismEnabled;
139}
140
142{
143 return mIsPinPlainTransmissionEnabled;
144}
145
147{
148 return mIsSvLoadAndDebitLogEnabled;
149}
150
152{
153 return mIsSvNegativeBalanceAuthorized;
154}
155
156const std::shared_ptr<uint8_t> CardSecuritySettingAdapter::getKif(
157 const WriteAccessLevel writeAccessLevel, const uint8_t kvc) const
158{
159 const auto it = mKifMap.find(writeAccessLevel);
160 if (it == mKifMap.end()) {
161 return nullptr;
162 } else {
163 const auto itt = it->second.find(kvc);
164 if (itt == it->second.end()) {
165 return nullptr;
166 } else {
167 return std::make_shared<uint8_t>(itt->second);
168 }
169 }
170}
171
172const std::shared_ptr<uint8_t> CardSecuritySettingAdapter::getDefaultKif(
173 const WriteAccessLevel writeAccessLevel) const
174{
175 const auto it = mDefaultKifMap.find(writeAccessLevel);
176 if (it == mDefaultKifMap.end()) {
177 return nullptr;
178 } else {
179 return std::make_shared<uint8_t>(it->second);
180 }
181}
182
183const std::shared_ptr<uint8_t> CardSecuritySettingAdapter::getDefaultKvc(
184 const WriteAccessLevel writeAccessLevel) const
185{
186 const auto it = mDefaultKvcMap.find(writeAccessLevel);
187 if (it == mDefaultKvcMap.end()) {
188 return nullptr;
189 } else {
190 return std::make_shared<uint8_t>(it->second);
191 }
192}
193
194bool CardSecuritySettingAdapter::isSessionKeyAuthorized(const std::shared_ptr<uint8_t> kif,
195 const std::shared_ptr<uint8_t> kvc) const
196{
197 if (kif == nullptr || kvc == nullptr) {
198 return false;
199 }
200
201 if (mAuthorizedSessionKeys.empty()) {
202 return true;
203 }
204
205 return Arrays::contains(mAuthorizedSessionKeys,
206 ((*kif.get() << 8) & 0xff00) | (*kvc.get() & 0x00ff));
207}
208
209bool CardSecuritySettingAdapter::isSvKeyAuthorized(const std::shared_ptr<uint8_t> kif,
210 const std::shared_ptr<uint8_t> kvc) const
211{
212 if (kif == nullptr || kvc == nullptr) {
213 return false;
214 }
215
216 if (mAuthorizedSvKeys.empty()) {
217 return true;
218 }
219
220 return Arrays::contains(mAuthorizedSvKeys,
221 ((*kif.get() << 8) & 0xff00) | (*kvc.get() & 0x00ff));
222}
223
225{
226 return mPinVerificationCipheringKif;
227}
228
230{
231 return mPinVerificationCipheringKvc;
232}
233
235{
236 return mPinModificationCipheringKif;
237}
238
240{
241 return mPinModificationCipheringKvc;
242}
243
244}
245}
246}
CardSecuritySettingAdapter & addAuthorizedSvKey(const uint8_t kif, const uint8_t kvc) override
CardSecuritySettingAdapter & enableMultipleSession() override
CardSecuritySettingAdapter & addAuthorizedSessionKey(const uint8_t kif, const uint8_t kvc) override
const std::shared_ptr< uint8_t > getPinModificationCipheringKvc() const
CardSecuritySettingAdapter & enableSvLoadAndDebitLog() override
bool isSvKeyAuthorized(const std::shared_ptr< uint8_t > kif, const std::shared_ptr< uint8_t > kvc) const
const std::shared_ptr< uint8_t > getKif(const WriteAccessLevel writeAccessLevel, const uint8_t kvc) const
const std::shared_ptr< uint8_t > getPinVerificationCipheringKvc() const
bool isSessionKeyAuthorized(const std::shared_ptr< uint8_t > kif, const std::shared_ptr< uint8_t > kvc) const
CardSecuritySettingAdapter & enableRatificationMechanism() override
CardSecuritySettingAdapter & assignDefaultKvc(const WriteAccessLevel writeAccessLevel, const uint8_t kvc) override
CardSecuritySettingAdapter & enablePinPlainTransmission() override
const std::shared_ptr< uint8_t > getPinModificationCipheringKif() const
CardSecuritySettingAdapter & setPinModificationCipheringKey(const uint8_t kif, const uint8_t kvc) override
CardSecuritySettingAdapter & authorizeSvNegativeBalance() override
const std::shared_ptr< uint8_t > getDefaultKif(const WriteAccessLevel writeAccessLevel) const
CardSecuritySetting & setSamResource(const std::shared_ptr< CardReader > samReader, const std::shared_ptr< CalypsoSam > calypsoSam) override
CardSecuritySettingAdapter & assignDefaultKif(const WriteAccessLevel writeAccessLevel, const uint8_t kif) override
CardSecuritySettingAdapter & assignKif(const WriteAccessLevel writeAccessLevel, const uint8_t kvc, const uint8_t kif) override
const std::shared_ptr< uint8_t > getDefaultKvc(const WriteAccessLevel writeAccessLevel) const
const std::shared_ptr< uint8_t > getPinVerificationCipheringKif() const
CardSecuritySettingAdapter & setPinVerificationCipheringKey(const uint8_t kif, const uint8_t kvc) override
CardSecuritySetting & setControlSamResource(const std::shared_ptr< CardReader > samReader, const std::shared_ptr< CalypsoSam > calypsoSam) final
CalypsoSam::ProductType ProductType