Keyple Card Calypso C++ Library 2.1.0
Reference Terminal Reader API for C++
CalypsoCardAdapter.h
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
13#pragma once
14
15#include <cstdint>
16#include <memory>
17#include <ostream>
18#include <string>
19#include <vector>
20
21/* Calypsonet Terminal Calypso */
22#include "CalypsoCard.h"
23#include "DirectoryHeader.h"
24#include "ElementaryFile.h"
25
26/* Calypsonet Terminal Card */
27#include "ApduResponseApi.h"
28#include "SmartCardSpi.h"
29
30/* Keyple Card Calypso */
31#include "CalypsoCardClass.h"
34
35/* Keyple Core Util */
36#include "LoggerFactory.h"
37
38
39namespace keyple {
40namespace card {
41namespace calypso {
42
43using namespace calypsonet::terminal::calypso::card;
44using namespace calypsonet::terminal::card;
45using namespace calypsonet::terminal::card::spi;
46using namespace keyple::core::util::cpp;
47
54class KEYPLECARDCALYPSO_API CalypsoCardAdapter final : public CalypsoCard, public SmartCardSpi {
55public:
62
73 void initializeWithPowerOnData(const std::string& powerOnData);
74
83 void initializeWithFci(const std::shared_ptr<ApduResponseApi> selectApplicationResponse);
84
90 const CalypsoCard::ProductType& getProductType() const override;
91
97 bool isHce() const override;
98
104 const std::vector<uint8_t>& getDfName() const override;
105
117 const std::vector<uint8_t>& getCalypsoSerialNumberFull() const;
118
124 const std::vector<uint8_t> getApplicationSerialNumber() const override;
125
131 const std::vector<uint8_t>& getStartupInfoRawData() const override;
132
140 uint8_t getPayloadCapacity() const;
141
152 bool isModificationsCounterInBytes() const;
153
164 int getModificationsCounter() const;
165
171 uint8_t getPlatform() const override;
172
178 uint8_t getApplicationType() const override;
179
185 bool isExtendedModeSupported() const override;
186
192 bool isRatificationOnDeselectSupported() const override;
193
199 bool isSvFeatureAvailable() const override;
200
206 bool isPinFeatureAvailable() const override;
207
213 bool isPkiModeSupported() const override;
214
220 uint8_t getApplicationSubtype() const override;
221
227 uint8_t getSoftwareIssuer() const override;
228
234 uint8_t getSoftwareVersion() const override;
235
241 uint8_t getSoftwareRevision() const override;
242
248 uint8_t getSessionModification() const override;
249
255 const std::vector<uint8_t> getTraceabilityInformation() const override;
256
262 bool isDfInvalidated() const override;
263
269 bool isDfRatified() const override;
270
284 void setSvData(const uint8_t svKvc,
285 const std::vector<uint8_t>& svGetHeader,
286 const std::vector<uint8_t>& svGetData,
287 const int svBalance,
288 const int svLastTNum,
289 const std::shared_ptr<SvLoadLogRecord> svLoadLogRecord,
290 const std::shared_ptr<SvDebitLogRecord> svDebitLogRecord);
291
297 int getSvBalance() const override;
298
304 int getSvLastTNum() const override;
305
311 const std::shared_ptr<SvLoadLogRecord> getSvLoadLogRecord() override;
312
318 const std::shared_ptr<SvDebitLogRecord> getSvDebitLogLastRecord() override;
319
325 const std::vector<std::shared_ptr<SvDebitLogRecord>> getSvDebitLogAllRecords() const override;
326
334 void setDfRatified(const bool dfRatified);
335
343 CalypsoCardClass getCardClass() const;
344
350 const std::shared_ptr<DirectoryHeader> getDirectoryHeader() const override;
351
361 CalypsoCard& setDirectoryHeader(const std::shared_ptr<DirectoryHeader> directoryHeader);
362
368 const std::shared_ptr<ElementaryFile> getFileBySfi(const uint8_t sfi) const override;
369
375 const std::shared_ptr<ElementaryFile> getFileByLid(const uint16_t lid) const override;
376
383 const std::map<const uint8_t, const std::shared_ptr<ElementaryFile>> getAllFiles() const
384 override;
385
391 const std::vector<std::shared_ptr<ElementaryFile>>& getFiles() const override;
392
398 bool isPinBlocked() const override;
399
405 int getPinAttemptRemaining() const override;
406
416 void setPinAttemptRemaining(const int pinAttemptCounter);
417
427 void setFileHeader(const uint8_t sfi, const std::shared_ptr<FileHeaderAdapter> header);
428
440 void setContent(const uint8_t sfi,
441 const uint8_t numRecord,
442 const std::vector<uint8_t>& content);
443
454 void setCounter(const uint8_t sfi, const uint8_t numCounter, const std::vector<uint8_t>& content);
455
470 void setContent(const uint8_t sfi,
471 const uint8_t numRecord,
472 const std::vector<uint8_t>& content,
473 const uint8_t offset);
474
488 void fillContent(const uint8_t sfi,
489 const uint8_t numRecord,
490 const std::vector<uint8_t>& content,
491 const uint8_t offset);
492
505 void addCyclicContent(const uint8_t sfi, const std::vector<uint8_t> content);
506
514 void backupFiles();
515
524 void restoreFiles();
525
531 const std::string& getPowerOnData() const override;
532
538 const std::vector<uint8_t> getSelectApplicationResponse() const override;
539
547 void setCardChallenge(const std::vector<uint8_t>& cardChallenge);
548
557 void setTraceabilityInformation(const std::vector<uint8_t>& traceabilityInformation);
558
566 void setSvOperationSignature(const std::vector<uint8_t>& svOperationSignature);
567
576 const std::vector<uint8_t>& getCardChallenge() const;
577
585 uint8_t getSvKvc() const;
586
595 const std::vector<uint8_t>& getSvGetHeader() const;
596
605 const std::vector<uint8_t>& getSvGetData() const;
606
614 const std::vector<uint8_t>& getSvOperationSignature() const;
615
619 friend KEYPLECARDCALYPSO_API std::ostream& operator<<(std::ostream& os,
620 const CalypsoCardAdapter& cca);
621
625 friend KEYPLECARDCALYPSO_API std::ostream& operator<<(
626 std::ostream& os, const std::shared_ptr<CalypsoCardAdapter> cca);
627
628private:
632 const std::unique_ptr<Logger> mLogger = LoggerFactory::getLogger(typeid(CalypsoCardAdapter));
633
637 static const std::string PATTERN_1_BYTE_HEX;
638 static const std::string PATTERN_2_BYTES_HEX;
639
643 static const int CARD_REV1_ATR_LENGTH;
644 static const int REV1_CARD_DEFAULT_WRITE_OPERATIONS_NUMBER_SUPPORTED_PER_SESSION;
645 static const int REV2_CARD_DEFAULT_WRITE_OPERATIONS_NUMBER_SUPPORTED_PER_SESSION;
646 static const int SI_BUFFER_SIZE_INDICATOR;
647 static const int SI_PLATFORM;
648 static const int SI_APPLICATION_TYPE;
649 static const int SI_APPLICATION_SUBTYPE;
650 static const int SI_SOFTWARE_ISSUER;
651 static const int SI_SOFTWARE_VERSION;
652 static const int SI_SOFTWARE_REVISION;
653 static const uint8_t PAY_LOAD_CAPACITY;
654
658 static const uint8_t APP_TYPE_WITH_CALYPSO_PIN;
659 static const uint8_t APP_TYPE_WITH_CALYPSO_SV;
660 static const uint8_t APP_TYPE_RATIFICATION_COMMAND_REQUIRED;
661 static const uint8_t APP_TYPE_CALYPSO_REV_32_MODE;
662 static const uint8_t APP_TYPE_WITH_PUBLIC_AUTHENTICATION;
663
667 static const std::vector<int> BUFFER_SIZE_INDICATOR_TO_BUFFER_SIZE;
668
672 std::shared_ptr<ApduResponseApi> mSelectApplicationResponse;
673
677 std::string mPowerOnData;
678
682 bool mIsExtendedModeSupported;
683
687 bool mIsRatificationOnDeselectSupported;
688
692 bool mIsSvFeatureAvailable;
693
697 bool mIsPinFeatureAvailable;
698
702 bool mIsPkiModeSupported;
703
707 bool mIsDfInvalidated;
708
712 CalypsoCardClass mCalypsoCardClass;
713
717 std::vector<uint8_t> mCalypsoSerialNumber;
718
722 std::vector<uint8_t> mStartupInfo;
723
727 CalypsoCard::ProductType mProductType;
728
732 std::vector<uint8_t> mDfName;
733
737 int mModificationsCounterMax;
738
742 bool mIsModificationCounterInBytes;
743
747 std::shared_ptr<DirectoryHeader> mDirectoryHeader;
748
752 std::vector<std::shared_ptr<ElementaryFile>> mFiles;
753
757 std::vector<std::shared_ptr<ElementaryFile>> mFilesBackup;
758
762 uint8_t mCurrentSfi;
763
767 uint16_t mCurrentLid;
768
772 std::shared_ptr<bool> mIsDfRatified;
773
777 std::shared_ptr<int> mPinAttemptCounter;
778
782 std::shared_ptr<int> mSvBalance;
783
787 int mSvLastTNum;
788
792 std::shared_ptr<SvLoadLogRecord> mSvLoadLogRecord;
793
797 std::shared_ptr<SvDebitLogRecord> mSvDebitLogRecord;
798
802 bool mIsHce;
803
807 std::vector<uint8_t> mCardChallenge;
808
812 std::vector<uint8_t> mTraceabilityInformation;
813
817 uint8_t mSvKvc;
818
822 std::vector<uint8_t> mSvGetHeader;
823
827 std::vector<uint8_t> mSvGetData;
828
832 std::vector<uint8_t> mSvOperationSignature;
833
837 uint8_t mApplicationSubType;
838
842 uint8_t mApplicationType;
843
847 uint8_t mSessionModification;
848
855 CalypsoCard::ProductType computeProductType(const int applicationType) const;
856
863 void updateCurrentSfi(const uint8_t sfi);
864
871 void updateCurrentLid(const uint16_t lid);
872
882 const std::shared_ptr<ElementaryFileAdapter> getOrCreateFile();
883
891 static void copyFiles(const std::vector<std::shared_ptr<ElementaryFile>>& src,
892 std::vector<std::shared_ptr<ElementaryFile>>& dest);
893};
894
895}
896}
897}
#define KEYPLECARDCALYPSO_API
CalypsoSam::ProductType ProductType
std::ostream & operator<<(std::ostream &os, const std::shared_ptr< ApduRequestAdapter > ara)