-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.c
339 lines (297 loc) · 9.93 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
* File: main.c
* Author: Emerson Ribeiro de Mello <[email protected]>
*
* Created on 30 June 2016, 09:33
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <limits.h>
#include <nfc/nfc.h>
#include "rp_settings.h"
#include "curlutils.h"
#include "wiring-gpio.h"
nfc_device *pnd;
nfc_target nt;
nfc_context *context;
void signal_handler(int signal) {
switch (signal) {
case SIGHUP:
case SIGUSR1:
case SIGINT:
case SIGTERM:
case SIGQUIT:
printf("\nFinalizing....\n");
cleaning();
exit(EXIT_SUCCESS);
default:
fprintf(stderr, "\nCaught wrong signal: %d\n", signal);
exit(EXIT_FAILURE);
}
}
char *charToHex(char *str) {
int size = strlen(str);
int i;
char *buf_str = (char *) malloc(4 * size + 1);
char *buf_ptr = buf_str;
for (i = 0; i < size; i++) {
buf_ptr += sprintf(buf_ptr, "\\x%02X", str[i]);
}
sprintf(buf_ptr, "\n");
*(buf_ptr + 1) = '\0';
return buf_ptr;
}
int CardTransmit(nfc_device *pnd, uint8_t *capdu, size_t capdulen, uint8_t *rapdu, size_t *rapdulen) {
int res;
size_t szPos;
// printf("=> ");
// for (szPos = 0; szPos < capdulen; szPos++) {
// printf("%hhx ", capdu[szPos]);
// }
// printf("\n");
if ((res = nfc_initiator_transceive_bytes(pnd, capdu, capdulen, rapdu, *rapdulen, 500)) < 0) {
return -1;
} else {
*rapdulen = (size_t) res;
// printf("<= ");
// for (szPos = 0; szPos < *rapdulen; szPos++) {
// printf("%hhx ", rapdu[szPos]);
// }
// printf("\n");
// for (szPos = 0; szPos < *rapdulen; szPos++) {
// printf("%c", (char) rapdu[szPos]);
// }
// printf("len: %d\n", *rapdulen);
return 0;
}
}
void nfcInitListen() {
const char *acLibnfcVersion = nfc_version();
pnd = nfc_open(context, NULL);
if (pnd == NULL) {
printf("ERROR: %s", "Unable to open NFC device.");
exit(EXIT_FAILURE);
}
if (nfc_initiator_init(pnd) < 0) {
nfc_perror(pnd, "nfc_initiator_init");
exit(EXIT_FAILURE);
}
printf("NFC reader: %s opened\n", nfc_device_get_name(pnd));
}
void nfcProtocol() {
char message[1024];
char UAFMessage[2048];
memoryStruct chunk;
uint8_t capdu[264];
size_t capdulen;
uint8_t rapdu[264];
size_t rapdulen;
char *response = 0;
const nfc_modulation nmMifare = {
.nmt = NMT_ISO14443A,
.nbr = NBR_106,
};
printf("Polling for target...\n");
while (nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt) <= 0);
printf("Target detected!\n");
// Select application
memcpy(capdu, APDU, sizeof (APDU));
capdulen = sizeof (APDU); // 13
rapdulen = sizeof (rapdu);
if (CardTransmit(pnd, capdu, capdulen, rapdu, &rapdulen) < 0)
exit(EXIT_FAILURE);
printf("Application selected!\n");
if (strncmp(rapdu, DOOR_HELLO, (int) rapdulen)) {
printf("** Opss ** I'm expecting HELLO msg, but card sent to me: %s. len: %d\n", response, (int) rapdulen);
return;
}
// FIDO Auth Request Message
printf("Doing AuthRequest to FIDO UAF Server\n");
sprintf(UAFMessage, AUTH_REQUEST_MSG, SCHEME, HOSTNAME, PORT, AUTH_REQUEST_ENDPOINT);
chunk = getHttpRequest(UAFMessage);
if (chunk.size <= 0) {
printf("** Opss ** Error to connect to FIDO Server\n");
memcpy(capdu, "ERROR", 5);
capdulen = 5;
rapdulen = sizeof (rapdu);
if (CardTransmit(pnd, capdu, capdulen, rapdu, &rapdulen) < 0) {
printf("Error to sent error message to card...\n");
}
return;
}
size_t blocks = (chunk.size / BLOCK_SIZE) + 1;
char *buffer[blocks];
blockSplit(chunk.memory, buffer, blocks);
sprintf(message, "BLOCK:%ld", blocks);
printf("Sending number of blocks: %s\n", message);
free(chunk.memory);
memcpy(capdu, message, strlen(message));
capdulen = strlen(message);
rapdulen = sizeof (rapdu);
if (CardTransmit(pnd, capdu, capdulen, rapdu, &rapdulen) < 0) {
printf("Error to send number of blocks\n");
return;
}
int totalSent = 0;
if (strncmp(rapdu, DOOR_NEXT, (int) rapdulen)) {
printf("** Opss ** I'm expecting NEXT msg, but card sent to me: %s. len: %d", response, (int) rapdulen);
return;
}
response = NULL;
do { // Sending UAFRequestMessage to card
memcpy(capdu, buffer[totalSent], strlen(buffer[totalSent]));
capdulen = strlen(buffer[totalSent]);
rapdulen = sizeof (rapdu);
if (CardTransmit(pnd, capdu, capdulen, rapdu, &rapdulen) < 0) {
int i = strlen(buffer[totalSent]);
printf("error to send UAFRequestMessage to card. Message size: %d, message: %s\n", i, buffer[totalSent]);
return;
}
if (strncmp(rapdu, DOOR_OK, (int) rapdulen)) {
printf("** Opss ** I'm expecting OK msg, but card sent to me: %s. len: %d", response, (int) rapdulen);
return;
}
response = NULL;
free(buffer[totalSent]);
totalSent++;
} while (totalSent < blocks);
printf("Sending READY!\n");
memcpy(capdu, DOOR_READY, sizeof (DOOR_READY));
capdulen = strlen(DOOR_READY);
rapdulen = sizeof (rapdu);
unsigned long timeout = LONG_MAX;
do {
if (CardTransmit(pnd, capdu, capdulen, rapdu, &rapdulen) < 0) {
printf("Error to received WAIT\n");
return;
}
if (strncmp(rapdu, DOOR_WAIT, (int) rapdulen) == 0) {
continue;
}
if (strncmp(rapdu, DOOR_DONE, (int) rapdulen) == 0) {
printf("Sending RESPONSE!\n");
memcpy(capdu, DOOR_RESPONSE, sizeof (DOOR_RESPONSE));
capdulen = strlen(DOOR_RESPONSE);
rapdulen = sizeof (rapdu);
if (CardTransmit(pnd, capdu, capdulen, rapdu, &rapdulen) < 0) {
printf("Error RESPONSE...\n");
return;
}
break;
}
timeout--;
} while (timeout > 0);
if (timeout == 0) {
printf("Error. Client is not responding...\n");
return;
}
strncpy(message, rapdu, (int) rapdulen);
char *p = strtok(message, ":");
char *endptr;
long val;
if (strcmp(p, "BLOCK") == 0) {
p = strtok(NULL, ":");
val = strtol(p, &endptr, 10);
char *UAFmsg = malloc(sizeof (char) * BLOCK_SIZE * val + 1);
UAFmsg[0] = '\0';
do {
printf("Sending NEXT!\n");
memcpy(capdu, DOOR_NEXT, sizeof (DOOR_NEXT));
capdulen = strlen(DOOR_NEXT);
rapdulen = sizeof (rapdu);
if (CardTransmit(pnd, capdu, capdulen, rapdu, &rapdulen) < 0) {
printf("Error NEXT\n");
return;
}
strncat(UAFmsg, rapdu, (int) rapdulen);
val--;
} while (val > 0);
// FIDO Auth Request Message
printf("Forwarding card response to FIDO UAF Server: \n");
sprintf(UAFMessage, AUTH_REQUEST_MSG, SCHEME, HOSTNAME, PORT, AUTH_RESPONSE_ENDPOINT);
//curlFetchStruct *result = postHttpRequest(UAFMessage, UAFmsg);
json_object *result = postHttpRequest(UAFMessage, UAFmsg);
free(UAFmsg);
// if (result->size <= 0) {
if (result == NULL) {
printf("Error to connect to FIDO Server\n");
memcpy(capdu, "ERROR", 5);
capdulen = 5;
rapdulen = sizeof (rapdu);
if (CardTransmit(pnd, capdu, capdulen, rapdu, &rapdulen) < 0) {
printf("Error to send response to card\n");
}
return;
}
printf("Resul payload: %s \n", json_object_to_json_string(result));
const char *resultStr = json_object_to_json_string(result);
if (strstr(resultStr, "SUCCESS") == NULL) {
printf("Access denied!\n");
memcpy(capdu, DOOR_DENY, sizeof (DOOR_DENY));
capdulen = strlen(DOOR_DENY);
rapdulen = sizeof (rapdu);
if (CardTransmit(pnd, capdu, capdulen, rapdu, &rapdulen) < 0) {
return;
}
} else {
printf("Access granted!\n");
memcpy(capdu, DOOR_GRANTED, sizeof (DOOR_GRANTED));
capdulen = strlen(DOOR_GRANTED);
rapdulen = sizeof (rapdu);
if (CardTransmit(pnd, capdu, capdulen, rapdu, &rapdulen) < 0) {
return;
}
doorlock(1);
doorlock(0);
}
/* free json object */
json_object_put(result);
strncpy(message, rapdu, (int) rapdulen);
if (strstr(message, "BYE") != NULL) {
printf("bye!\n");
} else {
printf(":-(\n");
}
}
}
/*
*
*/
int main(int argc, char** argv) {
// Handling Linux Signals
if (signal(SIGHUP, signal_handler) == SIG_ERR) {
perror("\nCan't catch SIGHUP\n");
}
if (signal(SIGQUIT, signal_handler) == SIG_ERR) {
perror("\nCan't catch SIGQUIT\n");
}
if (signal(SIGUSR1, signal_handler) == SIG_ERR) {
perror("\nCan't catch SIGUSR1\n");
}
// supervisord will send this one
if (signal(SIGTERM, signal_handler) == SIG_ERR) {
perror("\nCan't catch SIGTERM\n");
}
if (signal(SIGINT, signal_handler) == SIG_ERR) {
perror("\nCan't catch SIGINT\n");
}
// About GPIO & wiringPI Lib
setupWiring();
while (1) {
nfc_init(&context);
if (context == NULL) {
printf("Unable to init libnfc (malloc)\n");
exit(EXIT_FAILURE);
}
nfcInitListen();
nfcProtocol();
nfc_close(pnd);
nfc_exit(context);
sleep(2);
printf("starting again...\n");
}
return (EXIT_SUCCESS);
}