Keyple Card Calypso C++ Library 2.2.5.6
Reference Terminal Reader API for C++
CalypsoCardAdapter.h
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#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 "CardSelectionResponseApi.h"
29#include "SmartCardSpi.h"
30
31/* Keyple Card Calypso */
32#include "CalypsoCardClass.h"
33#include "CmdCardGetDataFci.h"
36
37/* Keyple Core Util */
38#include "LoggerFactory.h"
39
40
41namespace keyple {
42namespace card {
43namespace calypso {
44
45using namespace calypsonet::terminal::calypso::card;
46using namespace calypsonet::terminal::card;
47using namespace calypsonet::terminal::card::spi;
48using namespace keyple::core::util::cpp;
49
50/* Forward declaration */
51class CmdCardGetDataFci;
52
60: public CalypsoCard,
61 public SmartCardSpi,
62 public std::enable_shared_from_this<CalypsoCardAdapter> {
63public:
70
78 void initialize(const std::shared_ptr<CardSelectionResponseApi> cardSelectionResponse);
79
88 void initializeWithFci(const std::shared_ptr<CmdCardGetDataFci> cmdCardGetDataFci);
89
95 const CalypsoCard::ProductType& getProductType() const override;
96
102 bool isHce() const override;
103
109 const std::vector<uint8_t>& getDfName() const override;
110
122 const std::vector<uint8_t>& getCalypsoSerialNumberFull() const;
123
129 const std::vector<uint8_t> getApplicationSerialNumber() const override;
130
136 const std::vector<uint8_t>& getStartupInfoRawData() const override;
137
145 uint8_t getPayloadCapacity() const;
146
157 bool isModificationsCounterInBytes() const;
158
169 int getModificationsCounter() const;
170
176 uint8_t getPlatform() const override;
177
183 uint8_t getApplicationType() const override;
184
190 bool isExtendedModeSupported() const override;
191
197 bool isRatificationOnDeselectSupported() const override;
198
204 bool isSvFeatureAvailable() const override;
205
211 bool isPinFeatureAvailable() const override;
212
218 bool isPkiModeSupported() const override;
219
225 uint8_t getApplicationSubtype() const override;
226
232 uint8_t getSoftwareIssuer() const override;
233
239 uint8_t getSoftwareVersion() const override;
240
246 uint8_t getSoftwareRevision() const override;
247
253 uint8_t getSessionModification() const override;
254
260 const std::vector<uint8_t> getTraceabilityInformation() const override;
261
267 bool isDfInvalidated() const override;
268
274 bool isDfRatified() const override;
275
281 int getTransactionCounter() const override;
282
290 void setTransactionCounter(const int transactionCounter);
291
305 void setSvData(const uint8_t svKvc,
306 const std::vector<uint8_t>& svGetHeader,
307 const std::vector<uint8_t>& svGetData,
308 const int svBalance,
309 const int svLastTNum,
310 const std::shared_ptr<SvLoadLogRecord> svLoadLogRecord,
311 const std::shared_ptr<SvDebitLogRecord> svDebitLogRecord);
312
318 int getSvBalance() const override;
319
325 int getSvLastTNum() const override;
326
332 const std::shared_ptr<SvLoadLogRecord> getSvLoadLogRecord() override;
333
339 const std::shared_ptr<SvDebitLogRecord> getSvDebitLogLastRecord() override;
340
346 const std::vector<std::shared_ptr<SvDebitLogRecord>> getSvDebitLogAllRecords() const override;
347
355 void setDfRatified(const bool dfRatified);
356
364 CalypsoCardClass getCardClass() const;
365
371 const std::shared_ptr<DirectoryHeader> getDirectoryHeader() const override;
372
382 CalypsoCard& setDirectoryHeader(const std::shared_ptr<DirectoryHeader> directoryHeader);
383
389 const std::shared_ptr<ElementaryFile> getFileBySfi(const uint8_t sfi) const override;
390
396 const std::shared_ptr<ElementaryFile> getFileByLid(const uint16_t lid) const override;
397
404 const std::map<const uint8_t, const std::shared_ptr<ElementaryFile>> getAllFiles() const
405 override;
406
412 const std::vector<std::shared_ptr<ElementaryFile>>& getFiles() const override;
413
419 bool isPinBlocked() const override;
420
426 int getPinAttemptRemaining() const override;
427
437 void setPinAttemptRemaining(const int pinAttemptCounter);
438
448 void setFileHeader(const uint8_t sfi, const std::shared_ptr<FileHeaderAdapter> header);
449
461 void setContent(const uint8_t sfi,
462 const uint8_t numRecord,
463 const std::vector<uint8_t>& content);
464
475 void setCounter(const uint8_t sfi,
476 const uint8_t numCounter,
477 const std::vector<uint8_t>& content);
478
493 void setContent(const uint8_t sfi,
494 const uint8_t numRecord,
495 const std::vector<uint8_t>& content,
496 const int offset);
497
512 void fillContent(const uint8_t sfi,
513 const uint8_t numRecord,
514 const std::vector<uint8_t>& content,
515 const int offset);
516
529 void addCyclicContent(const uint8_t sfi, const std::vector<uint8_t> content);
530
538 void backupFiles();
539
548 void restoreFiles();
549
555 const std::string& getPowerOnData() const override;
556
562 const std::vector<uint8_t> getSelectApplicationResponse() const override;
563
571 void setCardChallenge(const std::vector<uint8_t>& cardChallenge);
572
581 void setTraceabilityInformation(const std::vector<uint8_t>& traceabilityInformation);
582
590 void setSvOperationSignature(const std::vector<uint8_t>& svOperationSignature);
591
600 const std::vector<uint8_t>& getCardChallenge() const;
601
609 uint8_t getSvKvc() const;
610
619 const std::vector<uint8_t>& getSvGetHeader() const;
620
629 const std::vector<uint8_t>& getSvGetData() const;
630
638 const std::vector<uint8_t>& getSvOperationSignature() const;
639
648 bool isCounterValuePostponed() const;
649
653 friend KEYPLECARDCALYPSO_API std::ostream& operator<<(std::ostream& os,
654 const CalypsoCardAdapter& cca);
655
659 friend KEYPLECARDCALYPSO_API std::ostream& operator<<(
660 std::ostream& os, const std::shared_ptr<CalypsoCardAdapter> cca);
661
662private:
667 class Patch {
668 public:
672 friend class CalypsoCardAdapter;
673
677 virtual void apply(CalypsoCardAdapter* calypsoCard) = 0;
678
679 private:
683 const uint64_t mStartupInfo;
684
688 const uint64_t mMask = ~0;
689
693 Patch(const std::string& startupInfo);
694
698 Patch(const std::string& startupInfo, const std::string& mask);
699
703 bool isApplicableTo(const uint64_t startupInfo) const;
704 };
705
710 class PatchRev3 : public Patch {
711 public:
715 friend class CalypsoCardAdapter;
716
720 virtual ~PatchRev3() = default;
721
725 void apply(CalypsoCardAdapter* calypsoCard) override;
726
727 private:
731 std::shared_ptr<int> mPayloadCapacity;
732
736 PatchRev3(const std::string& startupInfo);
737
741 PatchRev3(const std::string& startupInfo, const std::string& mask);
742
743
747 PatchRev3& setPayloadCapacity(const int payloadCapacity);
748 };
749
754 class PatchRev12 : public Patch {
755 public:
759 friend class CalypsoCardAdapter;
760
764 virtual ~PatchRev12() = default;
765
769 void apply(CalypsoCardAdapter* calypsoCard) override;
770
771 private:
775 std::shared_ptr<bool> mIsCounterValuePostponed;
776
780 PatchRev12(const std::string& startupInfo);
781
785 PatchRev12(const std::string& startupInfo, const std::string& mask);
786
790 PatchRev12& setCounterValuePostponed();
791 };
792
796 const std::unique_ptr<Logger> mLogger = LoggerFactory::getLogger(typeid(CalypsoCardAdapter));
797
801 static const std::string PATTERN_1_BYTE_HEX;
802 static const std::string PATTERN_2_BYTES_HEX;
803
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;
818
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;
827
831 static const std::vector<int> BUFFER_SIZE_INDICATOR_TO_BUFFER_SIZE;
832
836 std::shared_ptr<ApduResponseApi> mSelectApplicationResponse;
837
841 std::string mPowerOnData;
842
846 bool mIsExtendedModeSupported = false;
847
851 bool mIsRatificationOnDeselectSupported = false;
852
856 bool mIsSvFeatureAvailable = false;
857
861 bool mIsPinFeatureAvailable = false;
862
866 bool mIsPkiModeSupported = false;
867
871 bool mIsDfInvalidated = false;
872
877
881 std::vector<uint8_t> mCalypsoSerialNumber;
882
886 std::vector<uint8_t> mStartupInfo;
887
891 CalypsoCard::ProductType mProductType = ProductType::UNKNOWN;
892
896 std::vector<uint8_t> mDfName;
897
901 int mModificationsCounterMax = 0;
902
906 bool mIsModificationCounterInBytes = true;
907
911 std::shared_ptr<DirectoryHeader> mDirectoryHeader;
912
916 std::vector<std::shared_ptr<ElementaryFile>> mFiles;
917
921 std::vector<std::shared_ptr<ElementaryFile>> mFilesBackup;
922
926 std::shared_ptr<ElementaryFileAdapter> mCurrentEf;
927
931 std::shared_ptr<bool> mIsDfRatified;
932
936 std::shared_ptr<int> mTransactionCounter = nullptr;
937
941 std::shared_ptr<int> mPinAttemptCounter;
942
946 std::shared_ptr<int> mSvBalance;
947
951 int mSvLastTNum = 0;
952
956 std::shared_ptr<SvLoadLogRecord> mSvLoadLogRecord;
957
961 std::shared_ptr<SvDebitLogRecord> mSvDebitLogRecord;
962
966 bool mIsHce = false;
967
971 std::vector<uint8_t> mCardChallenge;
972
976 std::vector<uint8_t> mTraceabilityInformation;
977
981 uint8_t mSvKvc = 0;
982
986 std::vector<uint8_t> mSvGetHeader;
987
991 std::vector<uint8_t> mSvGetData;
992
996 std::vector<uint8_t> mSvOperationSignature;
997
1001 uint8_t mApplicationSubType = 0;
1002
1006 uint8_t mApplicationType = 0;
1007
1011 uint8_t mSessionModification = 0;
1012
1016 int mPayloadCapacity = DEFAULT_PAYLOAD_CAPACITY;
1017
1021 bool mIsCounterValuePostponed = false;
1022
1026 static const std::vector<std::shared_ptr<PatchRev3>> mPatchesRev3;
1027
1031 static const std::vector<std::shared_ptr<PatchRev12>> mPatchesRev12;
1032
1039 CalypsoCard::ProductType computeProductType(const int applicationType) const;
1040
1052 const std::shared_ptr<ElementaryFileAdapter> getOrCreateFile(const uint8_t sfi,
1053 const uint16_t lid);
1054
1062 static void copyFiles(const std::vector<std::shared_ptr<ElementaryFile>>& src,
1063 std::vector<std::shared_ptr<ElementaryFile>>& dest);
1064
1075 void initializeWithPowerOnData(const std::string& powerOnData);
1076
1085 void initializeWithFci(const std::shared_ptr<ApduResponseApi> selectApplicationResponse);
1086
1090 static const std::vector<std::shared_ptr<PatchRev12>> initPatchRev12();
1091
1095 static const std::vector<std::shared_ptr<PatchRev3>> initPatchRev3();
1096
1102 void applyPatchIfNeeded();
1103
1107 void applyPatchIfNeededForRevision(const std::vector<std::shared_ptr<Patch>>& patches,
1108 const uint64_t startupInfoLong);
1109};
1110
1111}
1112}
1113}
#define KEYPLECARDCALYPSO_API
static const CalypsoCardClass UNKNOWN
CalypsoSam::ProductType ProductType
std::ostream & operator<<(std::ostream &os, const std::shared_ptr< ApduRequestAdapter > ara)