LibLogicalAccess  2.5.0
An Open Source RFID Library
Loading...
Searching...
No Matches
desfirecommands.hpp
Go to the documentation of this file.
1
7#ifndef LOGICALACCESS_DESFIRECOMMANDS_HPP
8#define LOGICALACCESS_DESFIRECOMMANDS_HPP
9
13#include <vector>
15
16namespace logicalaccess
17{
18#define EXCEPTION_MSG_SELECTAPPLICATION \
19 "Select application failed."
21#ifndef DF_INS
22#define DF_INS
23
24#define DF_INS_AUTHENTICATE 0x0A
25#define DF_INS_CHANGE_KEY_SETTINGS 0x54
26#define DF_INS_GET_KEY_SETTINGS 0x45
27#define DF_INS_CHANGE_KEY 0xC4
28#define DF_INS_GET_KEY_VERSION 0x64
29
30#define DF_INS_CREATE_APPLICATION 0xCA
31#define DF_INS_DELETE_APPLICATION 0xDA
32#define DF_INS_GET_APPLICATION_IDS 0x6A
33#define DF_INS_SELECT_APPLICATION 0x5A
34#define DF_INS_FORMAT_PICC 0xFC
35#define DF_INS_GET_VERSION 0x60
36
37#define DF_INS_GET_FILE_IDS 0x6F
38#define DF_INS_GET_FILE_SETTINGS 0xF5
39#define DF_INS_CHANGE_FILE_SETTINGS 0x5F
40#define DF_INS_CREATE_STD_DATA_FILE 0xCD
41#define DF_INS_CREATE_BACKUP_DATA_FILE 0xCB
42#define DF_INS_CREATE_VALUE_FILE 0xCC
43#define DF_INS_CREATE_LINEAR_RECORD_FILE 0xC1
44#define DF_INS_CREATE_CYCLIC_RECORD_FILE 0xC0
45#define DF_INS_DELETE_FILE 0xDF
46
47#define DF_INS_READ_DATA 0xBD
48#define DF_INS_WRITE_DATA 0x3D
49#define DF_INS_GET_VALUE 0x6C
50#define DF_INS_CREDIT 0x0C
51#define DF_INS_DEBIT 0xDC
52#define DF_INS_LIMITED_CREDIT 0x1C
53#define DF_INS_WRITE_RECORD 0x3B
54#define DF_INS_READ_RECORDS 0xBB
55#define DF_INS_CLEAR_RECORD_FILE 0xEB
56#define DF_COMMIT_TRANSACTION 0xC7
57#define DF_INS_ABORT_TRANSACTION 0xA7
58
59#define DF_INS_ADDITIONAL_FRAME 0xAF
60
61#define DF_CLA_ISO_WRAP 0x90
62#endif
63
64#define CMD_DESFIRE "DESFire"
65
69typedef enum {
77 KS_DEFAULT = 0x0B
79
83typedef enum {
84 AR_KEY0 = 0x00,
85 AR_KEY1 = 0x01,
86 AR_KEY2 = 0x02,
87 AR_KEY3 = 0x03,
88 AR_KEY4 = 0x04,
89 AR_KEY5 = 0x05,
90 AR_KEY6 = 0x06,
91 AR_KEY7 = 0x07,
92 AR_KEY8 = 0x08,
93 AR_KEY9 = 0x09,
94 AR_KEY10 = 0x0A,
95 AR_KEY11 = 0x0B,
96 AR_KEY12 = 0x0C,
97 AR_KEY13 = 0x0D,
98 AR_FREE = 0x0E,
99 AR_NEVER = 0x0F
101
106{
111};
112
118inline short AccessRightsInMemory(const DESFireAccessRights &rights)
119{
120 return static_cast<short>((rights.readAccess << 12) | (rights.writeAccess << 8) |
121 (rights.readAndWriteAccess << 4) | rights.changeAccess);
122}
123
124class DESFireChip;
125
129class LLA_CARDS_DESFIRE_API DESFireCommands : public Commands
130{
131 public:
137 {
138 }
139
144 explicit DESFireCommands(std::string cmdtype)
145 : Commands(cmdtype)
146 {
147 }
148
149 virtual ~DESFireCommands();
150
154 virtual void erase() = 0;
155
159 struct LLA_CARDS_DESFIRE_API DataFileSetting
160 {
161 unsigned char fileSize[3];
162 };
163
167 struct LLA_CARDS_DESFIRE_API ValueFileSetting
168 {
169 int32_t lowerLimit;
170 int32_t upperLimit;
174 };
175
179 struct LLA_CARDS_DESFIRE_API RecordFileSetting
180 {
181 unsigned char recordSize[3];
182 unsigned char maxNumberRecords[3];
183 unsigned char
184 currentNumberRecords[3];
185 };
186
190 struct LLA_CARDS_DESFIRE_API FileSetting
191 {
192 unsigned char fileType;
193 unsigned char comSett;
194 unsigned char accessRights[2];
195 union {
199 } type;
200 DataFileSetting getDataFile() const;
201 void setDataFile(const DataFileSetting &);
202 ValueFileSetting getValueFile() const;
203 void setValueFile(const ValueFileSetting &);
204 RecordFileSetting getRecordFile() const;
205 void setRecordFile(const RecordFileSetting &);
206 };
207
211 struct LLA_CARDS_DESFIRE_API DESFireCardVersion
212 {
213 unsigned char hardwareVendor;
214 unsigned char hardwareType;
215 unsigned char hardwareSubType;
216 unsigned char hardwareMjVersion;
217 unsigned char hardwareMnVersion;
218 unsigned char hardwareStorageSize;
219 unsigned char hardwareProtocol;
221 unsigned char softwareVendor;
222 unsigned char softwareType;
223 unsigned char softwareSubType;
224 unsigned char softwareMjVersion;
225 unsigned char softwareMnVersion;
226 unsigned char softwareStorageSize;
227 unsigned char softwareProtocol;
229 unsigned char uid[7];
230 unsigned char batchNo[5];
231 unsigned char cwProd;
232 unsigned char yearProd;
233 };
234
239 virtual void selectApplication(std::shared_ptr<DESFireLocation> location);
240
247 virtual void createApplication(std::shared_ptr<DESFireLocation> location,
248 DESFireKeySettings settings, unsigned char maxNbKeys);
249
256 virtual void createStdDataFile(std::shared_ptr<DESFireLocation> location,
257 const DESFireAccessRights &accessRights,
258 unsigned int fileSize);
259
267 virtual EncryptionMode getEncryptionMode(unsigned char fileno, bool isReadMode,
268 bool *needLoadKey = nullptr);
269
270 virtual EncryptionMode getEncryptionMode(const FileSetting &fileSetting,
271 bool isReadMode,
272 bool *needLoadKey = nullptr);
273
279 virtual unsigned int getFileLength(unsigned char fileno);
280
285 virtual void selectApplication(unsigned int aid) = 0;
286
294 virtual void createApplication(unsigned int aid, DESFireKeySettings settings,
295 unsigned char maxNbKeys) = 0;
296
301 virtual void deleteApplication(unsigned int aid) = 0;
302
307 virtual std::vector<unsigned int> getApplicationIDs() = 0;
308
314 virtual void changeKey(unsigned char keyno, std::shared_ptr<DESFireKey> key) = 0;
315
321 virtual void getKeySettings(DESFireKeySettings &settings,
322 unsigned char &maxNbKeys) = 0;
323
329 virtual void changeKeySettings(DESFireKeySettings settings) = 0;
330
336 virtual ByteVector getFileIDs() = 0;
337
343 virtual FileSetting getFileSettings(unsigned char fileno) = 0;
344
352 virtual void changeFileSettings(unsigned char fileno, EncryptionMode comSettings,
353 const DESFireAccessRights &accessRights,
354 bool plain) = 0;
355
363 virtual void createStdDataFile(unsigned char fileno, EncryptionMode comSettings,
364 const DESFireAccessRights &accessRights,
365 unsigned int fileSize) = 0;
366
374 virtual void createBackupFile(unsigned char fileno, EncryptionMode comSettings,
375 const DESFireAccessRights &accessRights,
376 unsigned int fileSize) = 0;
377
388 virtual void createValueFile(unsigned char fileno, EncryptionMode comSettings,
389 const DESFireAccessRights &accessRights,
390 int32_t lowerLimit, int32_t upperLimit,
391 int32_t value, bool limitedCreditEnabled) = 0;
392
401 virtual void createLinearRecordFile(unsigned char fileno, EncryptionMode comSettings,
402 const DESFireAccessRights &accessRights,
403 unsigned int fileSize,
404 unsigned int maxNumberOfRecords) = 0;
405
414 virtual void createCyclicRecordFile(unsigned char fileno, EncryptionMode comSettings,
415 const DESFireAccessRights &accessRights,
416 unsigned int fileSize,
417 unsigned int maxNumberOfRecords) = 0;
418
423 virtual void deleteFile(unsigned char fileno) = 0;
424
433 virtual ByteVector readData(unsigned char fileno, unsigned int offset,
434 unsigned int length, EncryptionMode mode) = 0;
435
443 virtual void writeData(unsigned char fileno, unsigned int offset,
444 const ByteVector &data, EncryptionMode mode) = 0;
445
452 virtual int32_t getValue(unsigned char fileno, EncryptionMode mode) = 0;
453
460 virtual void credit(unsigned char fileno, unsigned int value,
461 EncryptionMode mode) = 0;
462
469 virtual void debit(unsigned char fileno, unsigned int value, EncryptionMode mode) = 0;
470
477 virtual void limitedCredit(unsigned char fileno, int32_t value,
478 EncryptionMode mode) = 0;
479
487 virtual void writeRecord(unsigned char fileno, unsigned int offset,
488 const ByteVector &data, EncryptionMode mode) = 0;
489
498 virtual ByteVector readRecords(unsigned char fileno, unsigned int offset,
499 unsigned int length, EncryptionMode mode) = 0;
500
505 virtual void clearRecordFile(unsigned char fileno) = 0;
506
510 virtual void commitTransaction() = 0;
511
515 virtual void abortTransaction() = 0;
516
521 virtual void authenticate(unsigned char keyno = 0) = 0;
522
528 virtual void authenticate(unsigned char keyno, std::shared_ptr<DESFireKey> key) = 0;
529
535
544 {
545 return SignatureResult{};
546 }
547
548 protected:
549 std::shared_ptr<DESFireChip> getDESFireChip() const;
550};
551}
552
553#endif
LLA_CARDS_DESFIRE_API void getDESFireChip(std::shared_ptr< logicalaccess::Chip > *chip)
Definition: libraryentry.cpp:27
The base commands class for all card commands.
Definition: commands.hpp:21
The DESFire commands class.
Definition: desfirecommands.hpp:130
virtual SignatureResult IKS_getLastReadSignature() const
Definition: desfirecommands.hpp:543
virtual void abortTransaction()=0
Abort the transaction.
virtual void createLinearRecordFile(unsigned char fileno, EncryptionMode comSettings, const DESFireAccessRights &accessRights, unsigned int fileSize, unsigned int maxNumberOfRecords)=0
Create a new linear record file in the current application.
virtual void getKeySettings(DESFireKeySettings &settings, unsigned char &maxNbKeys)=0
Get key settings on the current application.
virtual void clearRecordFile(unsigned char fileno)=0
Clear a specific record file.
virtual void deleteFile(unsigned char fileno)=0
Delete a file in the current application.
virtual int32_t getValue(unsigned char fileno, EncryptionMode mode)=0
Get value from a specific value file.
virtual DESFireCardVersion getVersion()=0
Get the card version information.
virtual void writeData(unsigned char fileno, unsigned int offset, const ByteVector &data, EncryptionMode mode)=0
Write data into a specific file.
virtual void credit(unsigned char fileno, unsigned int value, EncryptionMode mode)=0
Credit a specific value file.
virtual void createBackupFile(unsigned char fileno, EncryptionMode comSettings, const DESFireAccessRights &accessRights, unsigned int fileSize)=0
Create a new backup file in the current application.
virtual void debit(unsigned char fileno, unsigned int value, EncryptionMode mode)=0
Debit a specific value file.
virtual void selectApplication(unsigned int aid)=0
Select an application.
virtual ByteVector readData(unsigned char fileno, unsigned int offset, unsigned int length, EncryptionMode mode)=0
Read data from a specific file.
virtual void changeKeySettings(DESFireKeySettings settings)=0
Change key settings for the current application.
DESFireCommands(std::string cmdtype)
Constructor.
Definition: desfirecommands.hpp:144
virtual void changeFileSettings(unsigned char fileno, EncryptionMode comSettings, const DESFireAccessRights &accessRights, bool plain)=0
Change file settings of a specific file in the current application.
virtual void createApplication(unsigned int aid, DESFireKeySettings settings, unsigned char maxNbKeys)=0
Create a new application.
virtual ByteVector readRecords(unsigned char fileno, unsigned int offset, unsigned int length, EncryptionMode mode)=0
Read record from a specific record file.
virtual void commitTransaction()=0
Commit the transaction.
virtual void erase()=0
Erase the card.
virtual void writeRecord(unsigned char fileno, unsigned int offset, const ByteVector &data, EncryptionMode mode)=0
Write record into a specific record file.
virtual void limitedCredit(unsigned char fileno, int32_t value, EncryptionMode mode)=0
Limit credit on a specific value file.
virtual ByteVector getFileIDs()=0
Get the File IDentifiers of all active files within the currently selected application.
virtual std::vector< unsigned int > getApplicationIDs()=0
Get the current application list.
virtual void authenticate(unsigned char keyno=0)=0
Authenticate a block, given a key number.
virtual void createValueFile(unsigned char fileno, EncryptionMode comSettings, const DESFireAccessRights &accessRights, int32_t lowerLimit, int32_t upperLimit, int32_t value, bool limitedCreditEnabled)=0
Create a new value file in the current application.
DESFireCommands()
Constructor.
Definition: desfirecommands.hpp:135
virtual void createStdDataFile(unsigned char fileno, EncryptionMode comSettings, const DESFireAccessRights &accessRights, unsigned int fileSize)=0
Create a new data file in the current application.
virtual FileSetting getFileSettings(unsigned char fileno)=0
Get settings of a specific file in the current application.
virtual void changeKey(unsigned char keyno, std::shared_ptr< DESFireKey > key)=0
Change a key in the current application.
virtual void authenticate(unsigned char keyno, std::shared_ptr< DESFireKey > key)=0
Authenticate a block, given a key number.
virtual void deleteApplication(unsigned int aid)=0
Delete an application.
virtual void createCyclicRecordFile(unsigned char fileno, EncryptionMode comSettings, const DESFireAccessRights &accessRights, unsigned int fileSize, unsigned int maxNumberOfRecords)=0
Create a new cyclic record file in the current application.
Commands.
DESFireAccessInfo.
#define CMD_DESFIRE
Definition: desfirecommands.hpp:64
DESFireLocation.
std::vector< uint8_t > ByteVector
Definition: lla_fwd.hpp:80
Definition: asn1.hpp:9
TaskAccessRights
The access key flag.
Definition: desfirecommands.hpp:83
@ AR_KEY11
Definition: desfirecommands.hpp:95
@ AR_KEY4
Definition: desfirecommands.hpp:88
@ AR_KEY13
Definition: desfirecommands.hpp:97
@ AR_KEY1
Definition: desfirecommands.hpp:85
@ AR_NEVER
Definition: desfirecommands.hpp:99
@ AR_FREE
Definition: desfirecommands.hpp:98
@ AR_KEY10
Definition: desfirecommands.hpp:94
@ AR_KEY5
Definition: desfirecommands.hpp:89
@ AR_KEY3
Definition: desfirecommands.hpp:87
@ AR_KEY0
Definition: desfirecommands.hpp:84
@ AR_KEY7
Definition: desfirecommands.hpp:91
@ AR_KEY9
Definition: desfirecommands.hpp:93
@ AR_KEY12
Definition: desfirecommands.hpp:96
@ AR_KEY6
Definition: desfirecommands.hpp:90
@ AR_KEY2
Definition: desfirecommands.hpp:86
@ AR_KEY8
Definition: desfirecommands.hpp:92
EncryptionMode
The file communication mode (or encryption mode)
Definition: desfirelocation.hpp:18
short AccessRightsInMemory(const DESFireAccessRights &rights)
Convert file access rights into flags.
Definition: desfirecommands.hpp:118
DESFireKeySettings
The key settings.
Definition: desfirecommands.hpp:69
@ KS_DEFAULT
Definition: desfirecommands.hpp:77
@ KS_CHANGE_KEY_FROZEN
Definition: desfirecommands.hpp:76
@ KS_ALLOW_CHANGE_MK
Definition: desfirecommands.hpp:70
@ KS_FREE_CREATE_DELETE_WITHOUT_MK
Definition: desfirecommands.hpp:72
@ KS_FREE_LISTING_WITHOUT_MK
Definition: desfirecommands.hpp:71
@ KS_CHANGE_KEY_WITH_TARGETED_KEYNO
Definition: desfirecommands.hpp:75
@ KS_CHANGE_KEY_WITH_MK
Definition: desfirecommands.hpp:74
@ KS_CONFIGURATION_CHANGEABLE
Definition: desfirecommands.hpp:73
Access rights on DESFire file.
Definition: desfirecommands.hpp:106
TaskAccessRights writeAccess
Write access.
Definition: desfirecommands.hpp:108
TaskAccessRights readAndWriteAccess
Read and Write access.
Definition: desfirecommands.hpp:109
TaskAccessRights readAccess
Read access.
Definition: desfirecommands.hpp:107
TaskAccessRights changeAccess
Change access.
Definition: desfirecommands.hpp:110
Card information about software and hardware version.
Definition: desfirecommands.hpp:212
unsigned char hardwareType
The hardware type.
Definition: desfirecommands.hpp:214
unsigned char softwareType
The software type.
Definition: desfirecommands.hpp:222
unsigned char softwareMnVersion
The software minor version.
Definition: desfirecommands.hpp:225
unsigned char softwareMjVersion
The software major version.
Definition: desfirecommands.hpp:224
unsigned char hardwareMjVersion
The hardware major version.
Definition: desfirecommands.hpp:216
unsigned char hardwareSubType
The hardware subtype.
Definition: desfirecommands.hpp:215
unsigned char softwareVendor
The software vendor.
Definition: desfirecommands.hpp:221
unsigned char cwProd
The production id.
Definition: desfirecommands.hpp:231
unsigned char softwareSubType
The software subtype.
Definition: desfirecommands.hpp:223
unsigned char hardwareStorageSize
The hardware storage size.
Definition: desfirecommands.hpp:218
unsigned char hardwareMnVersion
The hardware minor version.
Definition: desfirecommands.hpp:217
unsigned char hardwareProtocol
The hardware protocol.
Definition: desfirecommands.hpp:219
unsigned char yearProd
The production year.
Definition: desfirecommands.hpp:232
unsigned char hardwareVendor
The hardware vendor.
Definition: desfirecommands.hpp:213
unsigned char softwareProtocol
The software protocol.
Definition: desfirecommands.hpp:227
unsigned char softwareStorageSize
The software storage size.
Definition: desfirecommands.hpp:226
Describe setting for Data File type.
Definition: desfirecommands.hpp:160
Describe file setting.
Definition: desfirecommands.hpp:191
unsigned char comSett
The communication setting.
Definition: desfirecommands.hpp:193
ValueFileSetting valueFile
Definition: desfirecommands.hpp:197
unsigned char fileType
The file type.
Definition: desfirecommands.hpp:192
DataFileSetting dataFile
Definition: desfirecommands.hpp:196
RecordFileSetting recordFile
Definition: desfirecommands.hpp:198
Describe setting for Record File type.
Definition: desfirecommands.hpp:180
Describe setting for Value File type.
Definition: desfirecommands.hpp:168
int32_t upperLimit
The upper value limit.
Definition: desfirecommands.hpp:170
bool limitedCreditEnabled
True if limited credit is enabled, false otherwise.
Definition: desfirecommands.hpp:172
int32_t limitedCreditValue
The limited credit value.
Definition: desfirecommands.hpp:171
int32_t lowerLimit
The lower value limit.
Definition: desfirecommands.hpp:169
Definition: RemoteCrypto.hpp:9