-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.c
215 lines (182 loc) · 5.86 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <winscard.h>
SCARDCONTEXT applicationContext;
LPSTR reader = NULL;
SCARDHANDLE connectionHandler;
DWORD activeProtocol;
void establishContext() {
LONG status = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &applicationContext);
if (status == SCARD_S_SUCCESS) {
printf("Context established\n");
} else {
printf("Establish context error: %s\n", pcsc_stringify_error(status));
exit(1);
}
}
void releaseContext() {
LONG status = SCardReleaseContext(applicationContext);
if (status == SCARD_S_SUCCESS) {
printf("Context released\n");
} else {
printf("Release context error: %s\n", pcsc_stringify_error(status));
exit(1);
}
}
void listReaders() {
DWORD readers = SCARD_AUTOALLOCATE;
LONG status = SCardListReaders(applicationContext, NULL, (LPSTR)&reader, &readers);
if (status == SCARD_S_SUCCESS) {
char *p = reader;
while (*p) {
printf("Reader found: %s\n", p);
p += strlen(p) +1;
}
} else {
printf("List reader error: %s\n", pcsc_stringify_error(status));
exit(1);
}
}
void freeListReader() {
LONG status = SCardFreeMemory(applicationContext, reader);
if (status == SCARD_S_SUCCESS) {
printf("Reader list free\n");
} else {
printf("Free reader list error: %s\n", pcsc_stringify_error(status));
exit(1);
}
}
void connectToCard() {
activeProtocol = -1;
LONG status = SCardConnect(applicationContext, reader, SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1, &connectionHandler, &activeProtocol);
if (status == SCARD_S_SUCCESS) {
printf("Connected to card\n");
} else {
printf("Card connection error: %s\n", pcsc_stringify_error(status));
exit(1);
}
}
void disconnectFromCard() {
LONG status = SCardDisconnect(connectionHandler, SCARD_LEAVE_CARD);
if (status == SCARD_S_SUCCESS) {
printf("Disconnected from card\n");
} else {
printf("Card deconnection error: %s\n", pcsc_stringify_error(status));
exit(1);
}
}
void getCardInformation() {
BYTE ATR[MAX_ATR_SIZE] = "";
DWORD ATRLength = sizeof(ATR);
char readerName[MAX_READERNAME] = "";
DWORD readerLength = sizeof(readerName);
DWORD readerState;
DWORD readerProtocol;
LONG status = SCardStatus(connectionHandler, readerName, &readerLength, &readerState, &readerProtocol, ATR, &ATRLength);
if (status == SCARD_S_SUCCESS) {
printf("\n");
printf("Name of the reader: %s\n", readerName);
printf("ATR: ");
for (int i=0; i<ATRLength; i++) {
printf("%02X ", ATR[i]);
}
printf("\n\n");
} else {
printf("Get card information error: %s\n", pcsc_stringify_error(status));
exit(1);
}
}
void sendCommand(uint8_t command[], unsigned short commandLength) {
const SCARD_IO_REQUEST *pioSendPci;
SCARD_IO_REQUEST pioRecvPci;
uint8_t response[300];
unsigned long responseLength = sizeof(response);
switch(activeProtocol) {
case SCARD_PROTOCOL_T0:
pioSendPci = SCARD_PCI_T0;
break;
case SCARD_PROTOCOL_T1:
pioSendPci = SCARD_PCI_T1;
break;
default:
printf("Protocol not found\n");
exit(1);
}
LONG status = SCardTransmit(connectionHandler, pioSendPci, command, commandLength, &pioRecvPci, response, &responseLength);
if (status == SCARD_S_SUCCESS) {
printf("Command sent: \n");
for (int i=0; i<commandLength; i++) {
printf("%02X ", command[i]);
}
printf("\nResponse: \n");
for (int i=0; i<responseLength; i++) {
printf("%02X ", response[i]);
}
printf("\n\n");
} else {
printf("Send command error: %s\n", pcsc_stringify_error(status));
exit(1);
}
}
void mifareUltralight() {
printf("### MIFARE Ultralight ###\n");
uint8_t pageNumber = 0x04;
// Read 4 blocks (16 bytes) starting from pageNumber
uint8_t readCommand[] = { 0xFF, 0xB0, 0x00, pageNumber, 0x10 };
unsigned short readCommandLength = sizeof(readCommand);
sendCommand(readCommand, readCommandLength);
// Write 1 block (4 bytes) to pageNumber
uint8_t data[] = { 0x00, 0x01, 0x02, 0x03 };
uint8_t writeCommand[9] = { 0xFF, 0xD6, 0x00, pageNumber, 0x04 };
unsigned short writeCommandLength = sizeof(writeCommand);
for (int i=0; i<4; i++) {
writeCommand[i+5] = data[i];
}
sendCommand(writeCommand, writeCommandLength);
}
void mifareClassic() {
printf("### MIFARE Classic ###\n");
uint8_t blockNumber = 0x04;
// Load Authentication Keys
uint8_t key[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
uint8_t authenticationKeysCommand[11] = { 0xFF, 0x82, 0x00, 0x00, 0x06 };
unsigned short authenticationKeysCommandLength = sizeof(authenticationKeysCommand);
for (int i=0; i<6; i++) {
authenticationKeysCommand[i+5] = key[i];
}
sendCommand(authenticationKeysCommand, authenticationKeysCommandLength);
// Authenticate
uint8_t authenticateCommand[] = { 0xFF, 0x86, 0x00, 0x00, 0x05, 0x01, 0x00, blockNumber, 0x60, 0x00 };
unsigned short authenticateCommandLength = sizeof(authenticateCommand);
sendCommand(authenticateCommand, authenticateCommandLength);
// Read 1 blocks (16 bytes) at blockNumber
uint8_t readCommand[] = { 0xFF, 0xB0, 0x00, blockNumber, 0x10 };
unsigned short readCommandLength = sizeof(readCommand);
sendCommand(readCommand, readCommandLength);
// Write 1 blocks (16 bytes) at blockNumber
uint8_t data[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
uint8_t writeCommand[21] = { 0xFF, 0xD6, 0x00, blockNumber, 0x10 };
unsigned short writeCommandLength = sizeof(writeCommand);
for (int i=0; i<16; i++) {
writeCommand[i+5] = data[i];
}
sendCommand(writeCommand, writeCommandLength);
}
int main() {
establishContext();
listReaders();
connectToCard();
getCardInformation();
printf("Firmware command:\n");
uint8_t firmwareCommand[] = { 0xFF, 0x00, 0x48, 0x00, 0x00 };
unsigned short firmwareCommandLength = sizeof(firmwareCommand);
sendCommand(firmwareCommand, firmwareCommandLength);
mifareUltralight();
//mifareClassic();
disconnectFromCard();
freeListReader();
releaseContext();
return 0;
}