22#include "CalypsoCard.h"
23#include "DirectoryHeader.h"
24#include "ElementaryFile.h"
27#include "ApduResponseApi.h"
28#include "CardSelectionResponseApi.h"
29#include "SmartCardSpi.h"
38#include "LoggerFactory.h"
45using namespace calypsonet::terminal::calypso::card;
46using namespace calypsonet::terminal::card;
47using namespace calypsonet::terminal::card::spi;
48using namespace keyple::core::util::cpp;
51class CmdCardGetDataFci;
62 public std::enable_shared_from_this<CalypsoCardAdapter> {
78 void initialize(
const std::shared_ptr<CardSelectionResponseApi> cardSelectionResponse);
88 void initializeWithFci(
const std::shared_ptr<CmdCardGetDataFci> cmdCardGetDataFci);
102 bool isHce()
const override;
109 const std::vector<uint8_t>& getDfName()
const override;
122 const std::vector<uint8_t>& getCalypsoSerialNumberFull()
const;
129 const std::vector<uint8_t> getApplicationSerialNumber()
const override;
136 const std::vector<uint8_t>& getStartupInfoRawData()
const override;
145 uint8_t getPayloadCapacity()
const;
157 bool isModificationsCounterInBytes()
const;
169 int getModificationsCounter()
const;
176 uint8_t getPlatform()
const override;
183 uint8_t getApplicationType()
const override;
190 bool isExtendedModeSupported()
const override;
197 bool isRatificationOnDeselectSupported()
const override;
204 bool isSvFeatureAvailable()
const override;
211 bool isPinFeatureAvailable()
const override;
218 bool isPkiModeSupported()
const override;
225 uint8_t getApplicationSubtype()
const override;
232 uint8_t getSoftwareIssuer()
const override;
239 uint8_t getSoftwareVersion()
const override;
246 uint8_t getSoftwareRevision()
const override;
253 uint8_t getSessionModification()
const override;
260 const std::vector<uint8_t> getTraceabilityInformation()
const override;
267 bool isDfInvalidated()
const override;
274 bool isDfRatified()
const override;
281 int getTransactionCounter()
const override;
290 void setTransactionCounter(
const int transactionCounter);
305 void setSvData(
const uint8_t svKvc,
306 const std::vector<uint8_t>& svGetHeader,
307 const std::vector<uint8_t>& svGetData,
309 const int svLastTNum,
310 const std::shared_ptr<SvLoadLogRecord> svLoadLogRecord,
311 const std::shared_ptr<SvDebitLogRecord> svDebitLogRecord);
318 int getSvBalance()
const override;
325 int getSvLastTNum()
const override;
332 const std::shared_ptr<SvLoadLogRecord> getSvLoadLogRecord()
override;
339 const std::shared_ptr<SvDebitLogRecord> getSvDebitLogLastRecord()
override;
346 const std::vector<std::shared_ptr<SvDebitLogRecord>> getSvDebitLogAllRecords()
const override;
355 void setDfRatified(
const bool dfRatified);
371 const std::shared_ptr<DirectoryHeader> getDirectoryHeader()
const override;
382 CalypsoCard& setDirectoryHeader(
const std::shared_ptr<DirectoryHeader> directoryHeader);
389 const std::shared_ptr<ElementaryFile> getFileBySfi(
const uint8_t sfi)
const override;
396 const std::shared_ptr<ElementaryFile> getFileByLid(
const uint16_t lid)
const override;
404 const std::map<const uint8_t, const std::shared_ptr<ElementaryFile>> getAllFiles()
const
412 const std::vector<std::shared_ptr<ElementaryFile>>& getFiles()
const override;
419 bool isPinBlocked()
const override;
426 int getPinAttemptRemaining()
const override;
437 void setPinAttemptRemaining(
const int pinAttemptCounter);
448 void setFileHeader(
const uint8_t sfi,
const std::shared_ptr<FileHeaderAdapter> header);
461 void setContent(
const uint8_t sfi,
462 const uint8_t numRecord,
463 const std::vector<uint8_t>& content);
475 void setCounter(
const uint8_t sfi,
476 const uint8_t numCounter,
477 const std::vector<uint8_t>& content);
493 void setContent(
const uint8_t sfi,
494 const uint8_t numRecord,
495 const std::vector<uint8_t>& content,
512 void fillContent(
const uint8_t sfi,
513 const uint8_t numRecord,
514 const std::vector<uint8_t>& content,
529 void addCyclicContent(
const uint8_t sfi,
const std::vector<uint8_t> content);
555 const std::string& getPowerOnData()
const override;
562 const std::vector<uint8_t> getSelectApplicationResponse()
const override;
571 void setCardChallenge(
const std::vector<uint8_t>& cardChallenge);
581 void setTraceabilityInformation(
const std::vector<uint8_t>& traceabilityInformation);
590 void setSvOperationSignature(
const std::vector<uint8_t>& svOperationSignature);
600 const std::vector<uint8_t>& getCardChallenge()
const;
609 uint8_t getSvKvc()
const;
619 const std::vector<uint8_t>& getSvGetHeader()
const;
629 const std::vector<uint8_t>& getSvGetData()
const;
638 const std::vector<uint8_t>& getSvOperationSignature()
const;
648 bool isCounterValuePostponed()
const;
660 std::ostream& os,
const std::shared_ptr<CalypsoCardAdapter> cca);
683 const uint64_t mStartupInfo;
688 const uint64_t mMask = ~0;
693 Patch(
const std::string& startupInfo);
698 Patch(
const std::string& startupInfo,
const std::string& mask);
703 bool isApplicableTo(
const uint64_t startupInfo)
const;
710 class PatchRev3 :
public Patch {
720 virtual ~PatchRev3() =
default;
731 std::shared_ptr<int> mPayloadCapacity;
736 PatchRev3(
const std::string& startupInfo);
741 PatchRev3(
const std::string& startupInfo,
const std::string& mask);
747 PatchRev3& setPayloadCapacity(
const int payloadCapacity);
754 class PatchRev12 :
public Patch {
764 virtual ~PatchRev12() =
default;
775 std::shared_ptr<bool> mIsCounterValuePostponed;
780 PatchRev12(
const std::string& startupInfo);
785 PatchRev12(
const std::string& startupInfo,
const std::string& mask);
790 PatchRev12& setCounterValuePostponed();
796 const std::unique_ptr<Logger> mLogger = LoggerFactory::getLogger(
typeid(
CalypsoCardAdapter));
801 static const std::string PATTERN_1_BYTE_HEX;
802 static const std::string PATTERN_2_BYTES_HEX;
807 static const int CARD_REV1_ATR_LENGTH;
808 static const int REV1_CARD_DEFAULT_WRITE_OPERATIONS_NUMBER_SUPPORTED_PER_SESSION;
809 static const int REV2_CARD_DEFAULT_WRITE_OPERATIONS_NUMBER_SUPPORTED_PER_SESSION;
810 static const int SI_BUFFER_SIZE_INDICATOR;
811 static const int SI_PLATFORM;
812 static const int SI_APPLICATION_TYPE;
813 static const int SI_APPLICATION_SUBTYPE;
814 static const int SI_SOFTWARE_ISSUER;
815 static const int SI_SOFTWARE_VERSION;
816 static const int SI_SOFTWARE_REVISION;
817 static const int DEFAULT_PAYLOAD_CAPACITY;
822 static const uint8_t APP_TYPE_WITH_CALYPSO_PIN;
823 static const uint8_t APP_TYPE_WITH_CALYPSO_SV;
824 static const uint8_t APP_TYPE_RATIFICATION_COMMAND_REQUIRED;
825 static const uint8_t APP_TYPE_CALYPSO_REV_32_MODE;
826 static const uint8_t APP_TYPE_WITH_PUBLIC_AUTHENTICATION;
831 static const std::vector<int> BUFFER_SIZE_INDICATOR_TO_BUFFER_SIZE;
836 std::shared_ptr<ApduResponseApi> mSelectApplicationResponse;
841 std::string mPowerOnData;
846 bool mIsExtendedModeSupported =
false;
851 bool mIsRatificationOnDeselectSupported =
false;
856 bool mIsSvFeatureAvailable =
false;
861 bool mIsPinFeatureAvailable =
false;
866 bool mIsPkiModeSupported =
false;
871 bool mIsDfInvalidated =
false;
881 std::vector<uint8_t> mCalypsoSerialNumber;
886 std::vector<uint8_t> mStartupInfo;
896 std::vector<uint8_t> mDfName;
901 int mModificationsCounterMax = 0;
906 bool mIsModificationCounterInBytes =
true;
911 std::shared_ptr<DirectoryHeader> mDirectoryHeader;
916 std::vector<std::shared_ptr<ElementaryFile>> mFiles;
921 std::vector<std::shared_ptr<ElementaryFile>> mFilesBackup;
926 std::shared_ptr<ElementaryFileAdapter> mCurrentEf;
931 std::shared_ptr<bool> mIsDfRatified;
936 std::shared_ptr<int> mTransactionCounter =
nullptr;
941 std::shared_ptr<int> mPinAttemptCounter;
946 std::shared_ptr<int> mSvBalance;
956 std::shared_ptr<SvLoadLogRecord> mSvLoadLogRecord;
961 std::shared_ptr<SvDebitLogRecord> mSvDebitLogRecord;
971 std::vector<uint8_t> mCardChallenge;
976 std::vector<uint8_t> mTraceabilityInformation;
986 std::vector<uint8_t> mSvGetHeader;
991 std::vector<uint8_t> mSvGetData;
996 std::vector<uint8_t> mSvOperationSignature;
1001 uint8_t mApplicationSubType = 0;
1006 uint8_t mApplicationType = 0;
1011 uint8_t mSessionModification = 0;
1016 int mPayloadCapacity = DEFAULT_PAYLOAD_CAPACITY;
1021 bool mIsCounterValuePostponed =
false;
1026 static const std::vector<std::shared_ptr<PatchRev3>> mPatchesRev3;
1031 static const std::vector<std::shared_ptr<PatchRev12>> mPatchesRev12;
1052 const std::shared_ptr<ElementaryFileAdapter> getOrCreateFile(
const uint8_t sfi,
1053 const uint16_t lid);
1062 static void copyFiles(
const std::vector<std::shared_ptr<ElementaryFile>>& src,
1063 std::vector<std::shared_ptr<ElementaryFile>>& dest);
1075 void initializeWithPowerOnData(
const std::string& powerOnData);
1085 void initializeWithFci(
const std::shared_ptr<ApduResponseApi> selectApplicationResponse);
1090 static const std::vector<std::shared_ptr<PatchRev12>> initPatchRev12();
1095 static const std::vector<std::shared_ptr<PatchRev3>> initPatchRev3();
1102 void applyPatchIfNeeded();
1107 void applyPatchIfNeededForRevision(
const std::vector<std::shared_ptr<Patch>>& patches,
1108 const uint64_t startupInfoLong);
#define KEYPLECARDCALYPSO_API
static const CalypsoCardClass UNKNOWN
CalypsoSam::ProductType ProductType
std::ostream & operator<<(std::ostream &os, const std::shared_ptr< ApduRequestAdapter > ara)