forked from neilzone/SolarSystem
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfobcommand.c
More file actions
471 lines (457 loc) · 15.5 KB
/
fobcommand.c
File metadata and controls
471 lines (457 loc) · 15.5 KB
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
// Fob command thread
#define _GNU_SOURCE /* See feature_test_macros(7) */
#include "config.h"
#include <stdio.h>
#include <string.h>
#include <popt.h>
#include <time.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <ctype.h>
#include <err.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <syslog.h>
#include "AJL/ajl.h"
#include <openssl/evp.h>
#include "DESFireAES/include/desfireaes.h"
#include "mqttmsg.h"
#include "ssmqtt.h"
#include "fobcommand.h"
extern int mqttdump;
typedef struct nfc_s {
int sock;
slot_t id; // Us
slot_t local; // Local command
slot_t device; // Remote device
const char *deviceid;
unsigned char remote:1;
unsigned char connected:1;
unsigned char held:1;
unsigned char gone:1;
unsigned char done:1;
} nfc_t;
static j_t getmsg(nfc_t * f)
{ // Get next message
while (1)
{
fd_set r;
FD_ZERO(&r);
FD_SET(f->sock, &r);
struct timeval to = { 60, 0 };
if (select(f->sock + 1, &r, NULL, NULL, &to) <= 0)
break;
unsigned char buf[2000];
int len = recv(f->sock, buf, sizeof(buf), 0);
if (len <= 0)
{
f->done = 1;
break;
}
if (*buf != 0x30)
continue;
j_t j = mqtt_decode(buf, len);
if (!j || j_isnull(j))
f->done = 1;
else
{
const char *target = j_get(j, "_meta.target");
if (!target || strcmp(target, f->deviceid))
{
j_delete(&j);
continue;
}
const char *prefix = j_get(j, "_meta.prefix");
const char *suffix = j_get(j, "_meta.suffix");
if (prefix && target && suffix && !strcmp(prefix, "event") && !strcmp(suffix, "fob"))
{
if (j_test(j, "remote", 0))
f->remote = 1;
if (j_test(j, "held", 0))
f->held = 1;
if (j_test(j, "gone", 0))
f->gone = f->done = 1;
else
f->connected = 1;
}
}
return j;
}
return NULL;
}
static int dx(void *obj, unsigned int len, unsigned char *data, unsigned int max, const char **errstr)
{ // DESFire DX
nfc_t *f = obj;
if (!f->done && f->connected)
{ // Send
j_t j = j_create();
j_string(j, j_base16(len, data));
slot_send(f->device, "command", f->deviceid, "nfc", &j);
}
len = 0;
while (!f->done && !len)
{
j_t j = getmsg(f);
if (!f->done)
{
const char *prefix = j_get(j, "_meta.prefix");
const char *suffix = j_get(j, "_meta.suffix");
if (prefix && suffix && !strcmp(prefix, "info"))
{
if (!strcmp(suffix, "nfc"))
{
j_t jdata = j_find(j, "_data");
if (j_isstring(jdata))
{
ssize_t datalen = j_base16D(data, max, j_val(jdata));
if (datalen > max)
*errstr = "Too long";
else
len = datalen;
}
} else if (!strcmp(suffix, "nfcerror"))
len = -1;
}
}
j_delete(&j);
}
return len;
};
void *fobcommand(void *arg)
{
nfc_t f = { };
int sock = -1;
char provision = 0,
identify = 0,
adopt = 0,
format = 0,
hardformat = 0;
unsigned char aid[3] = { };
unsigned char masterkey[17] = { }; // Keys with version on front
unsigned char aid0key[17] = { };
unsigned char aid1key[17] = { };
unsigned char afile[256] = { };
int organisation = 0;
int access = 0;
char *fob = NULL;
char *fobname = NULL;
{ // Get passed settings
const char *v;
j_t j = arg;
if (mqttdump)
{
fprintf(stderr, "FC:");
j_err(j_write(j, stderr));
fprintf(stderr, "\n");
}
f.sock = atoi(j_get(j, "socket") ? : "");
f.id = strtoll(j_get(j, "id") ? : "", NULL, 10);
f.device = strtoll(j_get(j, "device") ? : "", NULL, 10);
f.local = strtoll(j_get(j, "local") ? : "", NULL, 10);
provision = j_test(j, "provision", 0);
organisation = atoi(j_get(j, "organisation") ? : "");
access = atoi(j_get(j, "access") ? : "");
adopt = j_test(j, "adopt", 0);
format = j_test(j, "format", 0);
identify = j_test(j, "identify", 0);
if ((hardformat = j_test(j, "hardformat", 0)))
format = 1;
if ((v = j_get(j, "deviceid")))
f.deviceid = strdupa(v);
if ((v = j_get(j, "fobname")))
fobname = strdupa(v);
v = j_get(j, "fob");
if (v)
fob = strdupa(v);
j_base16D(masterkey, sizeof(masterkey), j_get(j, "masterkey"));
j_base16D(aid0key, sizeof(aid0key), j_get(j, "aid0key"));
j_base16D(aid1key, sizeof(aid1key), j_get(j, "aid1key"));
j_base16D(afile, sizeof(afile), j_get(j, "afile"));
j_base16D(aid, sizeof(aid), j_get(j, "aid"));
if (!sock)
errx(1, "socket not set");
j_delete(&j);
}
if (f.deviceid && *f.deviceid)
{
void status(const char *msg) {
if (mqttdump)
warnx("Fob:%s", msg);
if (!f.local)
return;
j_t j = j_create();
j_store_string(j, "status", msg);
slot_send(f.local, NULL, f.deviceid, NULL, &j);
}
void led(const char *led) {
if (!f.device)
return;
j_t j = j_create();
j_string(j, led);
slot_send(f.device, "command", f.deviceid, "led", &j);
}
const char *e = NULL;
const char *dfcheck(const char *res, const char *func, int line) {
if (res)
{
e = res;
if (mqttdump)
warnx("DF fail (%s): %s line %d", *res ? res : "Gone", func, line);
if (!f.gone)
status("Failed, remove fob");
}
return res;
}
#define df(x) if(dfcheck(df_##x,#x,__LINE__))return
if (identify)
{
slot_send(f.device, "command", f.deviceid, "nfcremote", NULL);
led("R+A-");
char *id = NULL;
while (!f.done && !f.connected && !id)
{
j_t j = getmsg(&f);
const char *prefix = j_get(j, "_meta.prefix");
const char *suffix = j_get(j, "_meta.suffix");
if (prefix && suffix && !strcmp(prefix, "event") && !strcmp(suffix, "fob") && (id = (char *) j_get(j, "id")))
id = strdupa(id);
j_delete(&j);
}
if (id)
{
j_t j = j_create();
j_store_string(j, "fobid", id);
slot_send(f.local, NULL, f.deviceid, NULL, &j);
}
} else if (provision || adopt || format)
{
slot_send(f.device, "command", f.deviceid, "nfcremote", NULL);
//if (fob) f.connected = 1; // Already connected
df_t d;
e = df_init(&d, &f, &dx);
if (!e)
{
if (!f.connected)
{
char *id = NULL;
led("R+A-");
status("Waiting for fob");
// Wait fob connect...
while (!f.done && !f.connected)
{
j_t j = getmsg(&f);
const char *prefix = j_get(j, "_meta.prefix");
const char *suffix = j_get(j, "_meta.suffix");
if (prefix && suffix && !strcmp(prefix, "event") && !strcmp(suffix, "fob") && (id = (char *) j_get(j, "id")))
id = strdupa(id);
j_delete(&j);
}
if (fob && id && strcmp(fob, id))
{
status(id);
e = "Not the expected fob";
}
}
if (f.connected)
led("A-");
unsigned char uid[7];
void doformat(void) {
status("Formatting fob");
df(format(&d, *masterkey, masterkey + 1));
df(get_uid(&d, uid));
status(j_base16(sizeof(uid), uid));
if (hardformat)
{
status("Hard format fob - will need provisioning again");
df(change_key(&d, 0x80, 0, masterkey + 1, NULL)); // Hard reset to zero AES
df(authenticate(&d, 0, NULL));
} else
{
df(change_key_settings(&d, 0x09));
df(set_configuration(&d, 0));
}
unsigned int mem;
df(free_memory(&d, &mem));
{ // Tell system formatted
j_t j = j_create();
j_int(j_path(j, "_meta.loopback"), f.id);
j_true(j_path(j, "_meta.formatted"));
j_store_string(j, "fob", j_base16(sizeof(uid), uid));
if (aid[0] || aid[1] || aid[2])
j_store_string(j, "aid", j_base16(sizeof(aid), aid));
j_store_string(j, "deviceid", f.deviceid);
j_store_int(j, "capacity", mem);
j_store_int(j, "mem", mem);
mqtt_qin(&j);
}
}
void doconnect(void) {
status("Connecting to fob");
df(select_application(&d, NULL));
if (df_authenticate(&d, 0, masterkey + 1))
df(authenticate(&d, 0, NULL));
df(get_uid(&d, uid));
status(j_base16(sizeof(uid), uid));
if (!fob)
return;
if (strcmp(fob, j_base16(sizeof(uid), uid)))
e = "Fob mismatch";
}
void doadopt(void) {
status("Adopting fob");
if (!*aid0key)
randkey(aid0key);
unsigned int n;
{
unsigned char aids[3 * 20];
df(get_application_ids(&d, &n, sizeof(aids), aids));
while (n && memcmp(aids + n * 3 - 3, aid, 3))
n--;
}
if (!n)
{
status("Creating application");
df(create_application(&d, aid, 0xEB, 2));
}
df(select_application(&d, aid));
if (df_authenticate(&d, 0, aid0key + 1))
{ // Set key 0
status("Setting application key");
df(authenticate(&d, 0, NULL)); // own key to change it
df(change_key(&d, 0, *aid0key, NULL, aid0key + 1));
}
df(authenticate(&d, 0, aid0key + 1));
// Check files
unsigned long long fids;
df(get_file_ids(&d, &fids));
if (!(fids & (1 << 0x0A)))
{
status("Making access file");
df(create_file(&d, 0x0A, 'B', 1, 0x0010, 256, 0, 0, 0, 0, 0));
}
// File 0 - name
// Not used - name is in access file
// File 1 - log
if (!(fids & (1 << 0x01)))
{
status("Making log file");
df(create_file(&d, 0x01, 'C', 1, 0x0100, 13, 0, 0, 10, 0, 0));
}
// File 2 - counter
if (!(fids & (1 << 0x02)))
{
status("Making counter file");
df(create_file(&d, 0x02, 'V', 1, 0x0010, 0, 0, 0x7FFFFFFF, 0, 0, 0));
}
// File A - access
status("Storing access file");
df(write_data(&d, 0x0A, 'B', 1, 0, *afile + 1, afile));
df(commit(&d));
// This is last as it is what marks a fob as finally adpoted
if (df_authenticate(&d, 1, aid1key + 1))
{ // Set key 1
status("Setting AID key");
df(authenticate(&d, 1, NULL)); // own key to change it
df(change_key(&d, 1, *aid1key, NULL, aid1key + 1));
}
unsigned int mem;
df(free_memory(&d, &mem));
{ // Tell system adopted
j_t j = j_create();
j_int(j_path(j, "_meta.loopback"), f.id);
j_true(j_path(j, "_meta.adopted"));
j_store_string(j, "fob", j_base16(sizeof(uid), uid));
if (aid[0] || aid[1] || aid[2])
j_store_string(j, "aid", j_base16(sizeof(aid), aid));
j_store_string(j, "aid0key", j_base16(sizeof(aid0key), aid0key));
j_store_string(j, "deviceid", f.deviceid);
if (organisation)
j_store_int(j, "organisation", organisation);
if (access)
j_store_int(j, "access", access);
if (fobname)
j_store_string(j, "fobname", fobname);
j_store_int(j, "mem", mem);
mqtt_qin(&j);
}
}
void doprovision(void) { // Expects to have formatted and connected
status("Setting key");
if (!*masterkey)
randkey(masterkey);
if (adopt && !*aid0key)
randkey(aid0key);
unsigned int mem;
df(free_memory(&d, &mem));
{ // Tell system new key
j_t j = j_create();
j_int(j_path(j, "_meta.loopback"), f.id);
j_true(j_path(j, "_meta.provisioned"));
j_store_string(j, "fob", j_base16(sizeof(uid), uid));
j_store_string(j, "masterkey", j_base16(sizeof(masterkey), masterkey));
if (adopt)
{
if (aid[0] || aid[1] || aid[2])
j_store_string(j, "aid", j_base16(sizeof(aid), aid));
j_store_string(j, "aid0key", j_base16(sizeof(aid0key), aid0key));
}
j_store_int(j, "mem", mem);
mqtt_qin(&j);
}
df(change_key(&d, 0x80, *masterkey, NULL, masterkey + 1));
df(authenticate(&d, 0, masterkey + 1));
}
if (f.connected && !f.done)
{
if (!e && (format || provision))
doformat();
if (!e)
doconnect();
if (!e && provision)
doprovision();
if (!e && adopt)
doadopt();
}
if (!e && !f.gone)
status("Done, remove card");
}
}
led(e ? "R" : "G");
if (f.connected)
while (!f.gone)
{
j_t j = getmsg(&f);
j_delete(&j);
}
slot_send(f.device, "command", f.deviceid, "nfcdone", NULL);
if (e)
status(*e ? e : "Card gone");
if (e && *e && mqttdump)
warnx("Fob failed: %s", e);
}
// Finish
slot_close(f.local);
slot_destroy(f.id);
return NULL;
}
void randblock(unsigned char *data, ssize_t len)
{
int f = open("/dev/urandom", O_RDONLY);
if (f < 0)
err(1, "Cannot open random");
if (read(f, data, len) != len)
err(1, "Cannot read random");
close(f);
}
void randkey(unsigned char key[17])
{
randblock(key, 17);
if (!*key)
*key = 1;
}