Keyple Card Calypso C++ Library 2.1.0
Reference Terminal Reader API for C++
CalypsoCardUtilAdapter.cpp
Go to the documentation of this file.
1/**************************************************************************************************
2 * Copyright (c) 2021 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 */
16#include "CalypsoCardCommand.h"
17#include "CalypsoCardConstant.h"
19#include "CardPinException.h"
20#include "CmdCardGetDataFci.h"
21#include "CmdCardGetDataFcp.h"
23#include "CmdCardSelectFile.h"
25
26/* Keyple Core Util */
27#include "Arrays.h"
28#include "IllegalStateException.h"
29#include "System.h"
30
31namespace keyple {
32namespace card {
33namespace calypso {
34
35using namespace keyple::core::util::cpp;
36using namespace keyple::core::util::cpp::exception;
37
38CalypsoCardUtilAdapter::CalypsoCardUtilAdapter() {}
39
41 std::shared_ptr<CalypsoCardAdapter> calypsoCard,
42 const std::shared_ptr<AbstractCardCommand> command,
43 const std::shared_ptr<ApduResponseApi> apduResponse,
44 const bool isSessionOpen)
45{
46 if (command->getCommandRef() == CalypsoCardCommand::READ_RECORDS) {
47 updateCalypsoCardReadRecords(calypsoCard,
48 std::dynamic_pointer_cast<CmdCardReadRecords>(command),
49 apduResponse,
50 isSessionOpen);
51 } else if (command->getCommandRef() == CalypsoCardCommand::GET_DATA) {
52 if (std::dynamic_pointer_cast<CmdCardGetDataFci>(command)) {
53 calypsoCard->initializeWithFci(apduResponse);
54 } else if (std::dynamic_pointer_cast<CmdCardGetDataFcp>(command)) {
55 updateCalypsoCardWithFcp(calypsoCard, command, apduResponse);
56 } else if (std::dynamic_pointer_cast<CmdCardGetDataEfList>(command)) {
57 updateCalypsoCardWithEfList(calypsoCard,
58 std::dynamic_pointer_cast<CmdCardGetDataEfList>(command),
59 apduResponse);
60 } else if (std::dynamic_pointer_cast<CmdCardGetDataTraceabilityInformation>(command)) {
61 updateCalypsoCardWithTraceabilityInformation(
62 calypsoCard,
63 std::dynamic_pointer_cast<CmdCardGetDataTraceabilityInformation>(command),
64 apduResponse);
65 } else {
66 throw IllegalStateException("Unknown GET DATA command reference.");
67 }
68 } else if (command->getCommandRef() == CalypsoCardCommand::SEARCH_RECORD_MULTIPLE) {
69 updateCalypsoCardSearchRecordMultiple(
70 calypsoCard,
71 std::dynamic_pointer_cast<CmdCardSearchRecordMultiple>(command),
72 apduResponse,
73 isSessionOpen);
74 } else if (command->getCommandRef() == CalypsoCardCommand::READ_RECORD_MULTIPLE) {
75 updateCalypsoCardReadRecordMultiple(
76 calypsoCard,
77 std::dynamic_pointer_cast<CmdCardReadRecordMultiple>(command),
78 apduResponse,
79 isSessionOpen);
80 } else if (command->getCommandRef() == CalypsoCardCommand::SELECT_FILE) {
81 updateCalypsoCardWithFcp(calypsoCard, command, apduResponse);
82 } else if (command->getCommandRef() == CalypsoCardCommand::UPDATE_RECORD) {
83 updateCalypsoCardUpdateRecord(
84 calypsoCard,
85 std::dynamic_pointer_cast<CmdCardUpdateRecord>(command),
86 apduResponse);
87 } else if (command->getCommandRef() == CalypsoCardCommand::WRITE_RECORD) {
88 updateCalypsoCardWriteRecord(
89 calypsoCard,
90 std::dynamic_pointer_cast<CmdCardWriteRecord>(command),
91 apduResponse);
92 } else if (command->getCommandRef() == CalypsoCardCommand::APPEND_RECORD) {
93 updateCalypsoCardAppendRecord(
94 calypsoCard,
95 std::dynamic_pointer_cast<CmdCardAppendRecord>(command),
96 apduResponse);
97 } else if (command->getCommandRef() == CalypsoCardCommand::INCREASE ||
98 command->getCommandRef() == CalypsoCardCommand::DECREASE) {
99 updateCalypsoCardIncreaseOrDecrease(
100 calypsoCard,
101 std::dynamic_pointer_cast<CmdCardIncreaseOrDecrease>(command),
102 apduResponse);
103 } else if (command->getCommandRef() == CalypsoCardCommand::INCREASE_MULTIPLE ||
104 command->getCommandRef() == CalypsoCardCommand::DECREASE_MULTIPLE) {
105 updateCalypsoCardIncreaseOrDecreaseMultiple(
106 calypsoCard,
107 std::dynamic_pointer_cast<CmdCardIncreaseOrDecreaseMultiple>(command),
108 apduResponse);
109 } else if (command->getCommandRef() == CalypsoCardCommand::OPEN_SESSION) {
110 updateCalypsoCardOpenSession(
111 calypsoCard,
112 std::dynamic_pointer_cast<CmdCardOpenSession>(command),
113 apduResponse);
114 } else if (command->getCommandRef() == CalypsoCardCommand::CLOSE_SESSION) {
115 updateCalypsoCardCloseSession(
116 std::dynamic_pointer_cast<CmdCardCloseSession>(command),
117 apduResponse);
118 } else if (command->getCommandRef() == CalypsoCardCommand::READ_BINARY) {
119 updateCalypsoCardReadBinary(
120 calypsoCard,
121 std::dynamic_pointer_cast<CmdCardReadBinary>(command),
122 apduResponse,
123 isSessionOpen);
124 } else if (command->getCommandRef() == CalypsoCardCommand::UPDATE_BINARY) {
125 updateCalypsoCardUpdateBinary(
126 calypsoCard,
127 std::dynamic_pointer_cast<CmdCardUpdateOrWriteBinary>(command),
128 apduResponse);
129 } else if (command->getCommandRef() == CalypsoCardCommand::WRITE_BINARY) {
130 updateCalypsoCardWriteBinary(
131 calypsoCard,
132 std::dynamic_pointer_cast<CmdCardUpdateOrWriteBinary>(command),
133 apduResponse);
134 } else if (command->getCommandRef() == CalypsoCardCommand::GET_CHALLENGE) {
135 updateCalypsoCardGetChallenge(
136 calypsoCard,
137 std::dynamic_pointer_cast<CmdCardGetChallenge>(command),
138 apduResponse);
139 } else if (command->getCommandRef() == CalypsoCardCommand::VERIFY_PIN) {
140 updateCalypsoVerifyPin(
141 calypsoCard,
142 std::dynamic_pointer_cast<CmdCardVerifyPin>(command),
143 apduResponse);
144 } else if (command->getCommandRef() == CalypsoCardCommand::SV_GET) {
145 updateCalypsoCardSvGet(
146 calypsoCard,
147 std::dynamic_pointer_cast<CmdCardSvGet>(command),
148 apduResponse);
149 } else if (command->getCommandRef() == CalypsoCardCommand::SV_RELOAD ||
150 command->getCommandRef() == CalypsoCardCommand::SV_DEBIT ||
151 command->getCommandRef() == CalypsoCardCommand::SV_UNDEBIT) {
152 updateCalypsoCardSvOperation(calypsoCard, command, apduResponse);
153 } else if (command->getCommandRef() == CalypsoCardCommand::INVALIDATE ||
154 command->getCommandRef() == CalypsoCardCommand::REHABILITATE) {
155 updateCalypsoInvalidateRehabilitate(command, apduResponse);
156 } else if (command->getCommandRef() == CalypsoCardCommand::CHANGE_PIN) {
157 updateCalypsoChangePin(std::dynamic_pointer_cast<CmdCardChangePin>(command), apduResponse);
158 } else if (command->getCommandRef() == CalypsoCardCommand::CHANGE_KEY) {
159 updateCalypsoChangeKey(std::dynamic_pointer_cast<CmdCardChangeKey>(command), apduResponse);
160 } else {
161 throw IllegalStateException("Unknown command reference.");
162 }
163}
164
166 std::shared_ptr<CalypsoCardAdapter> calypsoCard,
167 const std::vector<std::shared_ptr<AbstractCardCommand>>& commands,
168 const std::vector<std::shared_ptr<ApduResponseApi>>& apduResponses,
169 const bool isSessionOpen)
170{
171 auto responseIterator = apduResponses.begin();
172
173 if (!commands.empty()) {
174 for (const auto& command : commands) {
175 const std::shared_ptr<ApduResponseApi> apduResponse = *responseIterator++;
176 updateCalypsoCard(calypsoCard, command, apduResponse, isSessionOpen);
177 }
178 }
179}
180
181void CalypsoCardUtilAdapter::updateCalypsoCardOpenSession(
182 std::shared_ptr<CalypsoCardAdapter> calypsoCard,
183 std::shared_ptr<CmdCardOpenSession> cmdCardOpenSession,
184 const std::shared_ptr<ApduResponseApi> apduResponse)
185{
186 cmdCardOpenSession->setApduResponse(apduResponse);
187
188 /* CL-CSS-INFORAT.1 */
189 calypsoCard->setDfRatified(cmdCardOpenSession->wasRatified());
190
191 const std::vector<uint8_t>& recordDataRead = cmdCardOpenSession->getRecordDataRead();
192
193 if (recordDataRead.size() > 0) {
194 calypsoCard->setContent(cmdCardOpenSession->getSfi(),
195 cmdCardOpenSession->getRecordNumber(),
196 recordDataRead);
197 }
198}
199
200void CalypsoCardUtilAdapter::updateCalypsoCardCloseSession(
201 std::shared_ptr<CmdCardCloseSession> cmdCardCloseSession,
202 const std::shared_ptr<ApduResponseApi> apduResponse)
203{
204 cmdCardCloseSession->setApduResponse(apduResponse).checkStatus();
205}
206
207void CalypsoCardUtilAdapter::updateCalypsoCardReadRecords(
208 const std::shared_ptr<CalypsoCardAdapter> calypsoCard,
209 const std::shared_ptr<CmdCardReadRecords> cmdCardReadRecords,
210 const std::shared_ptr<ApduResponseApi> apduResponse,
211 const bool isSessionOpen)
212{
213 cmdCardReadRecords->setApduResponse(apduResponse);
214 checkResponseStatusForStrictAndBestEffortMode(cmdCardReadRecords, isSessionOpen);
215
216 /* Iterate over read records to fill the CalypsoCard */
217 for (const auto& entry : cmdCardReadRecords->getRecords()) {
218 calypsoCard->setContent(cmdCardReadRecords->getSfi(), entry.first, entry.second);
219 }
220}
221
222void CalypsoCardUtilAdapter::checkResponseStatusForStrictAndBestEffortMode(
223 const std::shared_ptr<AbstractCardCommand> command, const bool isSessionOpen)
224{
225 if (isSessionOpen) {
226 command->checkStatus();
227 } else {
228 try {
229 command->checkStatus();
230 } catch (const CardDataAccessException& e) {
231 /*
232 * Best effort mode, do not throw exception for "file not found" and "record not found"
233 * errors.
234 */
235 if (command->getApduResponse()->getStatusWord() != 0x6A82 &&
236 command->getApduResponse()->getStatusWord() != 0x6A83) {
237 throw e;
238 }
239 }
240 }
241}
242
243void CalypsoCardUtilAdapter::updateCalypsoCardSearchRecordMultiple(
244 std::shared_ptr<CalypsoCardAdapter> calypsoCard,
245 std::shared_ptr<CmdCardSearchRecordMultiple> cmdCardSearchRecordMultiple,
246 const std::shared_ptr<ApduResponseApi> apduResponse,
247 const bool isSessionOpen)
248{
249
250 cmdCardSearchRecordMultiple->setApduResponse(apduResponse);
251 checkResponseStatusForStrictAndBestEffortMode(cmdCardSearchRecordMultiple, isSessionOpen);
252
253 if (cmdCardSearchRecordMultiple->getFirstMatchingRecordContent().size() > 0) {
254 calypsoCard->setContent(
255 cmdCardSearchRecordMultiple->getSearchCommandData()->getSfi(),
256 cmdCardSearchRecordMultiple->getSearchCommandData()->getMatchingRecordNumbers()[0],
257 cmdCardSearchRecordMultiple->getFirstMatchingRecordContent());
258 }
259}
260
261void CalypsoCardUtilAdapter::updateCalypsoCardReadRecordMultiple(
262 std::shared_ptr<CalypsoCardAdapter> calypsoCard,
263 std::shared_ptr<CmdCardReadRecordMultiple> cmdCardReadRecordMultiple,
264 const std::shared_ptr<ApduResponseApi> apduResponse,
265 const bool isSessionOpen)
266{
267 cmdCardReadRecordMultiple->setApduResponse(apduResponse);
268 checkResponseStatusForStrictAndBestEffortMode(cmdCardReadRecordMultiple, isSessionOpen);
269
270 for (const auto& entry : cmdCardReadRecordMultiple->getResults()) {
271 calypsoCard->setContent(cmdCardReadRecordMultiple->getSfi(),
272 entry.first,
273 entry.second,
274 cmdCardReadRecordMultiple->getOffset());
275 }
276}
277
278void CalypsoCardUtilAdapter::updateCalypsoCardReadBinary(
279 std::shared_ptr<CalypsoCardAdapter> calypsoCard,
280 std::shared_ptr<CmdCardReadBinary> cmdCardReadBinary,
281 const std::shared_ptr<ApduResponseApi> apduResponse,
282 const bool isSessionOpen)
283{
284
285 cmdCardReadBinary->setApduResponse(apduResponse);
286 checkResponseStatusForStrictAndBestEffortMode(cmdCardReadBinary, isSessionOpen);
287
288 calypsoCard->setContent(cmdCardReadBinary->getSfi(),
289 1,
290 apduResponse->getDataOut(),
291 cmdCardReadBinary->getOffset());
292}
293
294void CalypsoCardUtilAdapter::updateCalypsoCardWithFcp(
295 std::shared_ptr<CalypsoCardAdapter> calypsoCard,
296 std::shared_ptr<AbstractCardCommand> command,
297 const std::shared_ptr<ApduResponseApi> apduResponse)
298{
299
300 command->setApduResponse(apduResponse).checkStatus();
301
302 std::vector<uint8_t> proprietaryInformation;
303 if (command->getCommandRef() == CalypsoCardCommand::SELECT_FILE) {
304 proprietaryInformation =
305 std::dynamic_pointer_cast<CmdCardSelectFile>(command)->getProprietaryInformation();
306 } else {
307 proprietaryInformation =
308 std::dynamic_pointer_cast<CmdCardGetDataFcp>(command)->getProprietaryInformation();
309 }
310
311 const uint8_t sfi = proprietaryInformation[CalypsoCardConstant::SEL_SFI_OFFSET];
312 const uint8_t fileType = proprietaryInformation[CalypsoCardConstant::SEL_TYPE_OFFSET];
313
314 if (fileType == CalypsoCardConstant::FILE_TYPE_MF ||
316 const auto directoryHeader = createDirectoryHeader(proprietaryInformation);
317 calypsoCard->setDirectoryHeader(directoryHeader);
318 } else if (fileType == CalypsoCardConstant::FILE_TYPE_EF) {
319 auto fileHeader = createFileHeader(proprietaryInformation);
320 calypsoCard->setFileHeader(sfi, fileHeader);
321 } else {
322 throw IllegalStateException("Unknown file type: " + std::to_string(fileType));
323 }
324}
325
326void CalypsoCardUtilAdapter::updateCalypsoCardWithEfList(
327 std::shared_ptr<CalypsoCardAdapter> calypsoCard,
328 std::shared_ptr<CmdCardGetDataEfList> command,
329 const std::shared_ptr<ApduResponseApi> apduResponse)
330{
331
332 command->setApduResponse(apduResponse).checkStatus();
333
334 const std::map<const std::shared_ptr<FileHeaderAdapter>, const uint8_t> fileHeaderToSfiMap =
335 command->getEfHeaders();
336
337 for (const auto& entry : fileHeaderToSfiMap) {
338 calypsoCard->setFileHeader(entry.second, entry.first);
339 }
340}
341
342void CalypsoCardUtilAdapter::updateCalypsoCardWithTraceabilityInformation(
343 std::shared_ptr<CalypsoCardAdapter> calypsoCard,
344 std::shared_ptr<CmdCardGetDataTraceabilityInformation> command,
345 const std::shared_ptr<ApduResponseApi> apduResponse)
346{
347
348 command->setApduResponse(apduResponse).checkStatus();
349
350 calypsoCard->setTraceabilityInformation(apduResponse->getDataOut());
351}
352
353void CalypsoCardUtilAdapter::updateCalypsoCardUpdateRecord(
354 std::shared_ptr<CalypsoCardAdapter> calypsoCard,
355 std::shared_ptr<CmdCardUpdateRecord> cmdCardUpdateRecord,
356 const std::shared_ptr<ApduResponseApi> apduResponse)
357{
358 cmdCardUpdateRecord->setApduResponse(apduResponse).checkStatus();
359
360 calypsoCard->setContent(cmdCardUpdateRecord->getSfi(),
361 cmdCardUpdateRecord->getRecordNumber(),
362 cmdCardUpdateRecord->getData());
363}
364
365void CalypsoCardUtilAdapter::updateCalypsoCardWriteRecord(
366 std::shared_ptr<CalypsoCardAdapter> calypsoCard,
367 std::shared_ptr<CmdCardWriteRecord> cmdCardWriteRecord,
368 const std::shared_ptr<ApduResponseApi> apduResponse)
369{
370
371 cmdCardWriteRecord->setApduResponse(apduResponse).checkStatus();
372
373 calypsoCard->fillContent(cmdCardWriteRecord->getSfi(),
374 cmdCardWriteRecord->getRecordNumber(),
375 cmdCardWriteRecord->getData(),
376 0);
377}
378
379void CalypsoCardUtilAdapter::updateCalypsoCardUpdateBinary(
380 std::shared_ptr<CalypsoCardAdapter> calypsoCard,
381 std::shared_ptr<CmdCardUpdateOrWriteBinary> cmdCardUpdateBinary,
382 const std::shared_ptr<ApduResponseApi> apduResponse)
383{
384 cmdCardUpdateBinary->setApduResponse(apduResponse).checkStatus();
385
386 calypsoCard->setContent(cmdCardUpdateBinary->getSfi(),
387 1,
388 cmdCardUpdateBinary->getData(),
389 cmdCardUpdateBinary->getOffset());
390}
391
392void CalypsoCardUtilAdapter::updateCalypsoCardWriteBinary(
393 std::shared_ptr<CalypsoCardAdapter> calypsoCard,
394 std::shared_ptr<CmdCardUpdateOrWriteBinary> cmdCardWriteBinary,
395 const std::shared_ptr<ApduResponseApi> apduResponse)
396{
397 cmdCardWriteBinary->setApduResponse(apduResponse).checkStatus();
398
399 calypsoCard->fillContent(cmdCardWriteBinary->getSfi(),
400 1,
401 cmdCardWriteBinary->getData(),
402 cmdCardWriteBinary->getOffset());
403}
404
405void CalypsoCardUtilAdapter::updateCalypsoCardAppendRecord(
406 std::shared_ptr<CalypsoCardAdapter> calypsoCard,
407 std::shared_ptr<CmdCardAppendRecord> cmdCardAppendRecord,
408 const std::shared_ptr<ApduResponseApi> apduResponse)
409{
410 cmdCardAppendRecord->setApduResponse(apduResponse).checkStatus();
411
412 calypsoCard->addCyclicContent(cmdCardAppendRecord->getSfi(), cmdCardAppendRecord->getData());
413}
414
415void CalypsoCardUtilAdapter::updateCalypsoCardIncreaseOrDecrease(
416 std::shared_ptr<CalypsoCardAdapter> calypsoCard,
417 std::shared_ptr<CmdCardIncreaseOrDecrease> cmdCardIncreaseOrDecrease,
418 const std::shared_ptr<ApduResponseApi> apduResponse)
419{
420
421 cmdCardIncreaseOrDecrease->setApduResponse(apduResponse).checkStatus();
422
423 calypsoCard->setCounter(cmdCardIncreaseOrDecrease->getSfi(),
424 cmdCardIncreaseOrDecrease->getCounterNumber(),
425 apduResponse->getDataOut());
426}
427
428void CalypsoCardUtilAdapter::updateCalypsoCardIncreaseOrDecreaseMultiple(
429 std::shared_ptr<CalypsoCardAdapter> calypsoCard,
430 std::shared_ptr<CmdCardIncreaseOrDecreaseMultiple> cmdCardIncreaseOrDecreaseMultiple,
431 const std::shared_ptr<ApduResponseApi> apduResponse)
432{
433
434 cmdCardIncreaseOrDecreaseMultiple->setApduResponse(apduResponse).checkStatus();
435
436 for (const auto& entry : cmdCardIncreaseOrDecreaseMultiple->getNewCounterValues()) {
437 calypsoCard->setCounter(cmdCardIncreaseOrDecreaseMultiple->getSfi(),
438 entry.first,
439 entry.second);
440 }
441}
442
443void CalypsoCardUtilAdapter::updateCalypsoCardGetChallenge(
444 std::shared_ptr<CalypsoCardAdapter> calypsoCard,
445 std::shared_ptr<CmdCardGetChallenge> cmdCardGetChallenge,
446 const std::shared_ptr<ApduResponseApi> apduResponse)
447{
448 cmdCardGetChallenge->setApduResponse(apduResponse).checkStatus();
449 calypsoCard->setCardChallenge(cmdCardGetChallenge->getCardChallenge());
450}
451
452void CalypsoCardUtilAdapter::updateCalypsoVerifyPin(
453 std::shared_ptr<CalypsoCardAdapter> calypsoCard,
454 std::shared_ptr<CmdCardVerifyPin> cmdCardVerifyPin,
455 const std::shared_ptr<ApduResponseApi> apduResponse)
456{
457 cmdCardVerifyPin->setApduResponse(apduResponse);
458 calypsoCard->setPinAttemptRemaining(cmdCardVerifyPin->getRemainingAttemptCounter());
459
460 try {
461 cmdCardVerifyPin->checkStatus();
462 } catch (const CardPinException& ex) {
463 /*
464 * Forward the exception if the operation do not target the reading of the attempt counter.
465 * Catch it silently otherwise
466 */
467 if (!cmdCardVerifyPin->isReadCounterOnly()) {
468 throw ex;
469 }
470 }
471}
472
473void CalypsoCardUtilAdapter::updateCalypsoChangePin(
474 std::shared_ptr<CmdCardChangePin> cmdCardChangePin,
475 const std::shared_ptr<ApduResponseApi> apduResponse)
476{
477 cmdCardChangePin->setApduResponse(apduResponse).checkStatus();
478}
479
480void CalypsoCardUtilAdapter::updateCalypsoChangeKey(
481 std::shared_ptr<CmdCardChangeKey> cmdCardChangeKey,
482 const std::shared_ptr<ApduResponseApi> apduResponse)
483{
484 cmdCardChangeKey->setApduResponse(apduResponse).checkStatus();
485}
486
487void CalypsoCardUtilAdapter::updateCalypsoCardSvGet(
488 std::shared_ptr<CalypsoCardAdapter> calypsoCard,
489 std::shared_ptr<CmdCardSvGet> cmdCardSvGet,
490 const std::shared_ptr<ApduResponseApi> apduResponse)
491{
492 cmdCardSvGet->setApduResponse(apduResponse).checkStatus();
493
494 calypsoCard->setSvData(cmdCardSvGet->getCurrentKVC(),
495 cmdCardSvGet->getSvGetCommandHeader(),
496 cmdCardSvGet->getApduResponse()->getApdu(),
497 cmdCardSvGet->getBalance(),
498 cmdCardSvGet->getTransactionNumber(),
499 cmdCardSvGet->getLoadLog(),
500 cmdCardSvGet->getDebitLog());
501}
502
503void CalypsoCardUtilAdapter::updateCalypsoCardSvOperation(
504 std::shared_ptr<CalypsoCardAdapter> calypsoCard,
505 std::shared_ptr<AbstractCardCommand> cmdCardSvOperation,
506 const std::shared_ptr<ApduResponseApi> apduResponse)
507{
508 cmdCardSvOperation->setApduResponse(apduResponse).checkStatus();
509 calypsoCard->setSvOperationSignature(cmdCardSvOperation->getApduResponse()->getDataOut());
510}
511
512void CalypsoCardUtilAdapter::updateCalypsoInvalidateRehabilitate(
513 std::shared_ptr<AbstractCardCommand> cmdCardInvalidateRehabilitate,
514 const std::shared_ptr<ApduResponseApi> apduResponse)
515{
516 cmdCardInvalidateRehabilitate->setApduResponse(apduResponse).checkStatus();
517}
518
519const std::shared_ptr<DirectoryHeader> CalypsoCardUtilAdapter::createDirectoryHeader(
520 const std::vector<uint8_t>& proprietaryInformation)
521{
522 std::vector<uint8_t> accessConditions(CalypsoCardConstant::SEL_AC_LENGTH);
523 System::arraycopy(proprietaryInformation,
525 accessConditions,
526 0,
528
529 std::vector<uint8_t> keyIndexes(CalypsoCardConstant::SEL_NKEY_LENGTH);
530 System::arraycopy(proprietaryInformation,
532 keyIndexes,
533 0,
535
536 const uint8_t dfStatus = proprietaryInformation[CalypsoCardConstant::SEL_DF_STATUS_OFFSET];
537
538 uint16_t lid = (((proprietaryInformation[CalypsoCardConstant::SEL_LID_OFFSET] << 8) & 0xff00) |
539 (proprietaryInformation[CalypsoCardConstant::SEL_LID_OFFSET + 1] & 0x00ff));
540
542 ->lid(lid)
543 .accessConditions(accessConditions)
544 .keyIndexes(keyIndexes)
545 .dfStatus(dfStatus)
546 .kvc(WriteAccessLevel::PERSONALIZATION,
547 proprietaryInformation[CalypsoCardConstant::SEL_KVCS_OFFSET])
548 .kvc(WriteAccessLevel::LOAD,
549 proprietaryInformation[CalypsoCardConstant::SEL_KVCS_OFFSET + 1])
550 .kvc(WriteAccessLevel::DEBIT,
551 proprietaryInformation[CalypsoCardConstant::SEL_KVCS_OFFSET + 2])
552 .kif(WriteAccessLevel::PERSONALIZATION,
553 proprietaryInformation[CalypsoCardConstant::SEL_KIFS_OFFSET])
554 .kif(WriteAccessLevel::LOAD,
555 proprietaryInformation[CalypsoCardConstant::SEL_KIFS_OFFSET + 1])
556 .kif(WriteAccessLevel::DEBIT,
557 proprietaryInformation[CalypsoCardConstant::SEL_KIFS_OFFSET + 2])
558 .build();
559}
560
561ElementaryFile::Type CalypsoCardUtilAdapter::getEfTypeFromCardValue(const uint8_t efType)
562{
563 ElementaryFile::Type fileType;
564
566 fileType = ElementaryFile::Type::BINARY;
567 } else if (efType == CalypsoCardConstant::EF_TYPE_LINEAR) {
568 fileType = ElementaryFile::Type::LINEAR;
569 } else if (efType == CalypsoCardConstant::EF_TYPE_CYCLIC) {
570 fileType = ElementaryFile::Type::CYCLIC;
572 fileType = ElementaryFile::Type::SIMULATED_COUNTERS;
573 } else if (efType == CalypsoCardConstant::EF_TYPE_COUNTERS) {
574 fileType = ElementaryFile::Type::COUNTERS;
575 } else {
576 throw IllegalStateException("Unknown EF Type: " + std::to_string(efType));
577 }
578
579 return fileType;
580}
581
582const std::shared_ptr<FileHeaderAdapter> CalypsoCardUtilAdapter::createFileHeader(
583 const std::vector<uint8_t>& proprietaryInformation)
584{
585 const ElementaryFile::Type fileType =
586 getEfTypeFromCardValue(proprietaryInformation[CalypsoCardConstant::SEL_EF_TYPE_OFFSET]);
587
588 int recordSize;
589 int recordsNumber;
590
591 if (fileType == ElementaryFile::Type::BINARY) {
592 recordSize =
593 ((proprietaryInformation[CalypsoCardConstant::SEL_REC_SIZE_OFFSET] << 8) & 0x0000ff00) |
594 (proprietaryInformation[CalypsoCardConstant::SEL_NUM_REC_OFFSET] & 0x000000ff);
595 recordsNumber = 1;
596 } else {
597 recordSize = proprietaryInformation[CalypsoCardConstant::SEL_REC_SIZE_OFFSET];
598 recordsNumber = proprietaryInformation[CalypsoCardConstant::SEL_NUM_REC_OFFSET];
599 }
600
601 std::vector<uint8_t> accessConditions(CalypsoCardConstant::SEL_AC_LENGTH);
602 System::arraycopy(proprietaryInformation,
604 accessConditions,
605 0,
607
608 std::vector<uint8_t> keyIndexes(CalypsoCardConstant::SEL_NKEY_LENGTH);
609 System::arraycopy(proprietaryInformation,
611 keyIndexes,
612 0,
614
615 const uint8_t dfStatus = proprietaryInformation[CalypsoCardConstant::SEL_DF_STATUS_OFFSET];
616
617 const uint16_t sharedReference =
618 ((proprietaryInformation[CalypsoCardConstant::SEL_DATA_REF_OFFSET] << 8) & 0xff00) |
619 (proprietaryInformation[CalypsoCardConstant::SEL_DATA_REF_OFFSET + 1] & 0x00ff);
620
621 const uint16_t lid =
622 ((proprietaryInformation[CalypsoCardConstant::SEL_LID_OFFSET] << 8) & 0xff00) |
623 (proprietaryInformation[CalypsoCardConstant::SEL_LID_OFFSET + 1] & 0x00ff);
624
625 return FileHeaderAdapter::builder()->lid(lid)
626 .recordsNumber(recordsNumber)
627 .recordSize(recordSize)
628 .type(fileType)
629 .accessConditions(accessConditions)
630 .keyIndexes(keyIndexes)
631 .dfStatus(dfStatus)
632 .sharedReference(sharedReference)
633 .build();
634}
635
636}
637}
638}
static const CalypsoCardCommand DECREASE
static const CalypsoCardCommand SV_RELOAD
static const CalypsoCardCommand UPDATE_RECORD
static const CalypsoCardCommand INCREASE
static const CalypsoCardCommand SV_DEBIT
static const CalypsoCardCommand READ_RECORD_MULTIPLE
static const CalypsoCardCommand SELECT_FILE
static const CalypsoCardCommand GET_DATA
static const CalypsoCardCommand READ_RECORDS
static const CalypsoCardCommand CHANGE_PIN
static const CalypsoCardCommand READ_BINARY
static const CalypsoCardCommand WRITE_RECORD
static const CalypsoCardCommand CHANGE_KEY
static const CalypsoCardCommand WRITE_BINARY
static const CalypsoCardCommand INVALIDATE
static const CalypsoCardCommand SEARCH_RECORD_MULTIPLE
static const CalypsoCardCommand REHABILITATE
static const CalypsoCardCommand DECREASE_MULTIPLE
static const CalypsoCardCommand SV_GET
static const CalypsoCardCommand GET_CHALLENGE
static const CalypsoCardCommand VERIFY_PIN
static const CalypsoCardCommand SV_UNDEBIT
static const CalypsoCardCommand CLOSE_SESSION
static const CalypsoCardCommand UPDATE_BINARY
static const CalypsoCardCommand OPEN_SESSION
static const CalypsoCardCommand INCREASE_MULTIPLE
static const CalypsoCardCommand APPEND_RECORD
static void updateCalypsoCard(std::shared_ptr< CalypsoCardAdapter > calypsoCard, const std::shared_ptr< AbstractCardCommand > command, const std::shared_ptr< ApduResponseApi > apduResponse, const bool isSessionOpen)
static std::shared_ptr< DirectoryHeaderBuilder > builder()
static std::shared_ptr< FileHeaderBuilder > builder()