Keyple Card Calypso C++ Library 2.2.2
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
276 int getTransactionCounter() const override;
277
285 void setTransactionCounter(const int transactionCounter);
286
300 void setSvData(const uint8_t svKvc,
301 const std::vector<uint8_t>& svGetHeader,
302 const std::vector<uint8_t>& svGetData,
303 const int svBalance,
304 const int svLastTNum,
305 const std::shared_ptr<SvLoadLogRecord> svLoadLogRecord,
306 const std::shared_ptr<SvDebitLogRecord> svDebitLogRecord);
307
313 int getSvBalance() const override;
314
320 int getSvLastTNum() const override;
321
327 const std::shared_ptr<SvLoadLogRecord> getSvLoadLogRecord() override;
328
334 const std::shared_ptr<SvDebitLogRecord> getSvDebitLogLastRecord() override;
335
341 const std::vector<std::shared_ptr<SvDebitLogRecord>> getSvDebitLogAllRecords() const override;
342
350 void setDfRatified(const bool dfRatified);
351
359 CalypsoCardClass getCardClass() const;
360
366 const std::shared_ptr<DirectoryHeader> getDirectoryHeader() const override;
367
377 CalypsoCard& setDirectoryHeader(const std::shared_ptr<DirectoryHeader> directoryHeader);
378
384 const std::shared_ptr<ElementaryFile> getFileBySfi(const uint8_t sfi) const override;
385
391 const std::shared_ptr<ElementaryFile> getFileByLid(const uint16_t lid) const override;
392
399 const std::map<const uint8_t, const std::shared_ptr<ElementaryFile>> getAllFiles() const
400 override;
401
407 const std::vector<std::shared_ptr<ElementaryFile>>& getFiles() const override;
408
414 bool isPinBlocked() const override;
415
421 int getPinAttemptRemaining() const override;
422
432 void setPinAttemptRemaining(const int pinAttemptCounter);
433
443 void setFileHeader(const uint8_t sfi, const std::shared_ptr<FileHeaderAdapter> header);
444
456 void setContent(const uint8_t sfi,
457 const uint8_t numRecord,
458 const std::vector<uint8_t>& content);
459
470 void setCounter(const uint8_t sfi, const uint8_t numCounter, const std::vector<uint8_t>& content);
471
486 void setContent(const uint8_t sfi,
487 const uint8_t numRecord,
488 const std::vector<uint8_t>& content,
489 const uint8_t offset);
490
504 void fillContent(const uint8_t sfi,
505 const uint8_t numRecord,
506 const std::vector<uint8_t>& content,
507 const uint8_t offset);
508
521 void addCyclicContent(const uint8_t sfi, const std::vector<uint8_t> content);
522
530 void backupFiles();
531
540 void restoreFiles();
541
547 const std::string& getPowerOnData() const override;
548
554 const std::vector<uint8_t> getSelectApplicationResponse() const override;
555
563 void setCardChallenge(const std::vector<uint8_t>& cardChallenge);
564
573 void setTraceabilityInformation(const std::vector<uint8_t>& traceabilityInformation);
574
582 void setSvOperationSignature(const std::vector<uint8_t>& svOperationSignature);
583
592 const std::vector<uint8_t>& getCardChallenge() const;
593
601 uint8_t getSvKvc() const;
602
611 const std::vector<uint8_t>& getSvGetHeader() const;
612
621 const std::vector<uint8_t>& getSvGetData() const;
622
630 const std::vector<uint8_t>& getSvOperationSignature() const;
631
635 friend KEYPLECARDCALYPSO_API std::ostream& operator<<(std::ostream& os,
636 const CalypsoCardAdapter& cca);
637
641 friend KEYPLECARDCALYPSO_API std::ostream& operator<<(
642 std::ostream& os, const std::shared_ptr<CalypsoCardAdapter> cca);
643
644private:
648 const std::unique_ptr<Logger> mLogger = LoggerFactory::getLogger(typeid(CalypsoCardAdapter));
649
653 static const std::string PATTERN_1_BYTE_HEX;
654 static const std::string PATTERN_2_BYTES_HEX;
655
659 static const int CARD_REV1_ATR_LENGTH;
660 static const int REV1_CARD_DEFAULT_WRITE_OPERATIONS_NUMBER_SUPPORTED_PER_SESSION;
661 static const int REV2_CARD_DEFAULT_WRITE_OPERATIONS_NUMBER_SUPPORTED_PER_SESSION;
662 static const int SI_BUFFER_SIZE_INDICATOR;
663 static const int SI_PLATFORM;
664 static const int SI_APPLICATION_TYPE;
665 static const int SI_APPLICATION_SUBTYPE;
666 static const int SI_SOFTWARE_ISSUER;
667 static const int SI_SOFTWARE_VERSION;
668 static const int SI_SOFTWARE_REVISION;
669 static const uint8_t PAY_LOAD_CAPACITY;
670
674 static const uint8_t APP_TYPE_WITH_CALYPSO_PIN;
675 static const uint8_t APP_TYPE_WITH_CALYPSO_SV;
676 static const uint8_t APP_TYPE_RATIFICATION_COMMAND_REQUIRED;
677 static const uint8_t APP_TYPE_CALYPSO_REV_32_MODE;
678 static const uint8_t APP_TYPE_WITH_PUBLIC_AUTHENTICATION;
679
683 static const std::vector<int> BUFFER_SIZE_INDICATOR_TO_BUFFER_SIZE;
684
688 std::shared_ptr<ApduResponseApi> mSelectApplicationResponse;
689
693 std::string mPowerOnData;
694
698 bool mIsExtendedModeSupported = false;
699
703 bool mIsRatificationOnDeselectSupported = false;
704
708 bool mIsSvFeatureAvailable = false;
709
713 bool mIsPinFeatureAvailable = false;
714
718 bool mIsPkiModeSupported = false;
719
723 bool mIsDfInvalidated = false;
724
728 CalypsoCardClass mCalypsoCardClass;
729
733 std::vector<uint8_t> mCalypsoSerialNumber;
734
738 std::vector<uint8_t> mStartupInfo;
739
743 CalypsoCard::ProductType mProductType;
744
748 std::vector<uint8_t> mDfName;
749
753 int mModificationsCounterMax;
754
758 bool mIsModificationCounterInBytes = false;
759
763 std::shared_ptr<DirectoryHeader> mDirectoryHeader;
764
768 std::vector<std::shared_ptr<ElementaryFile>> mFiles;
769
773 std::vector<std::shared_ptr<ElementaryFile>> mFilesBackup;
774
778 uint8_t mCurrentSfi;
779
783 uint16_t mCurrentLid;
784
788 std::shared_ptr<bool> mIsDfRatified;
789
793 std::shared_ptr<int> mTransactionCounter = nullptr;
794
798 std::shared_ptr<int> mPinAttemptCounter;
799
803 std::shared_ptr<int> mSvBalance;
804
808 int mSvLastTNum;
809
813 std::shared_ptr<SvLoadLogRecord> mSvLoadLogRecord;
814
818 std::shared_ptr<SvDebitLogRecord> mSvDebitLogRecord;
819
823 bool mIsHce = false;
824
828 std::vector<uint8_t> mCardChallenge;
829
833 std::vector<uint8_t> mTraceabilityInformation;
834
838 uint8_t mSvKvc;
839
843 std::vector<uint8_t> mSvGetHeader;
844
848 std::vector<uint8_t> mSvGetData;
849
853 std::vector<uint8_t> mSvOperationSignature;
854
858 uint8_t mApplicationSubType;
859
863 uint8_t mApplicationType;
864
868 uint8_t mSessionModification;
869
876 CalypsoCard::ProductType computeProductType(const int applicationType) const;
877
884 void updateCurrentSfi(const uint8_t sfi);
885
892 void updateCurrentLid(const uint16_t lid);
893
903 const std::shared_ptr<ElementaryFileAdapter> getOrCreateFile();
904
912 static void copyFiles(const std::vector<std::shared_ptr<ElementaryFile>>& src,
913 std::vector<std::shared_ptr<ElementaryFile>>& dest);
914};
915
916}
917}
918}
#define KEYPLECARDCALYPSO_API
CalypsoSam::ProductType ProductType
std::ostream & operator<<(std::ostream &os, const std::shared_ptr< ApduRequestAdapter > ara)