forked from nfc-tools/libnfc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnfc-read-forum-tag3.c
404 lines (361 loc) · 12.3 KB
/
nfc-read-forum-tag3.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
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
/*-
* Free/Libre Near Field Communication (NFC) library
*
* Libnfc historical contributors:
* Copyright (C) 2009 Roel Verdult
* Copyright (C) 2009-2013 Romuald Conty
* Copyright (C) 2010-2012 Romain Tartière
* Copyright (C) 2010-2013 Philippe Teuwen
* Copyright (C) 2012-2013 Ludovic Rousseau
* See AUTHORS file for a more comprehensive list of contributors.
* Additional contributors of this file:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1) Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2 )Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* Note that this license only applies on the examples, NFC library itself is under LGPL
*
*/
/**
* @file nfc-read-forum-tag3.c
* @brief Extract NDEF Message from a NFC Forum Tag Type 3
* This utility extract (if available) the NDEF Message contained in an NFC Forum Tag Type 3.
*/
/*
* This implementation was written based on information provided by the
* following documents:
*
* NFC Forum Type 3 Tag Operation Specification
* Technical Specification
* NFCForum-TS-Type-3-Tag_1.1 - 2011-06-28
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif // HAVE_CONFIG_H
#include <errno.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <nfc/nfc.h>
#include "nfc-utils.h"
#if defined(WIN32) && defined(__GNUC__) /* mingw compiler */
#include <getopt.h>
#endif
static nfc_device *pnd;
static nfc_context *context;
static void
print_usage(char *progname)
{
fprintf(stderr, "usage: %s [-q] -o FILE\n", progname);
fprintf(stderr, "\nOptions:\n");
fprintf(stderr, " -o FILE Extract NDEF message if available in FILE\n");
fprintf(stderr, " -o - Extract NDEF message if available to stdout\n");
fprintf(stderr, " -q Be quiet, don't display Attribute Block parsing info\n");
}
static void stop_select(int sig)
{
(void) sig;
if (pnd != NULL) {
nfc_abort_command(pnd);
} else {
nfc_exit(context);
exit(EXIT_FAILURE);
}
}
static void
build_felica_frame(const nfc_felica_info nfi, const uint8_t command, const uint8_t *payload, const size_t payload_len, uint8_t *frame, size_t *frame_len)
{
frame[0] = 1 + 1 + 8 + payload_len;
*frame_len = frame[0];
frame[1] = command;
memcpy(frame + 2, nfi.abtId, 8);
memcpy(frame + 10, payload, payload_len);
}
#define CHECK 0x06
static int
nfc_forum_tag_type3_check(nfc_device *dev, const nfc_target *nt, const uint16_t block, const uint8_t block_count, uint8_t *data, size_t *data_len)
{
uint8_t payload[1024] = {
1, // Services
0x0B, 0x00, // NFC Forum Tag Type 3's Service code
block_count,
0x80, block, // block 0
};
size_t payload_len = 1 + 2 + 1;
for (uint8_t b = 0; b < block_count; b++) {
if (block < 0x100) {
payload[payload_len++] = 0x80;
payload[payload_len++] = block + b;
} else {
payload[payload_len++] = 0x00;
payload[payload_len++] = (block + b) >> 8;
payload[payload_len++] = (block + b) & 0xff;
}
}
uint8_t frame[1024];
size_t frame_len = sizeof(frame);
build_felica_frame(nt->nti.nfi, CHECK, payload, payload_len, frame, &frame_len);
uint8_t rx[1024];
int res;
if ((res = nfc_initiator_transceive_bytes(dev, frame, frame_len, rx, sizeof(rx), 0)) < 0) {
return res;
}
const int res_overhead = 1 + 1 + 8 + 2; // 1+1+8+2: LEN + CMD + NFCID2 + STATUS
if (res < res_overhead) {
// Not enough data
return -1;
}
uint8_t felica_res_len = rx[0];
if (res != felica_res_len) {
// Error while receiving felica frame
return -1;
}
if ((CHECK + 1) != rx[1]) {
// Command return does not match
return -1;
}
if (0 != memcmp(&rx[2], nt->nti.nfi.abtId, 8)) {
// NFCID2 does not match
return -1;
}
const uint8_t status_flag1 = rx[10];
const uint8_t status_flag2 = rx[11];
if ((status_flag1) || (status_flag2)) {
// Felica card's error
fprintf(stderr, "Status bytes: %02x, %02x\n", status_flag1, status_flag2);
return -1;
}
// const uint8_t res_block_count = res[12];
*data_len = res - res_overhead + 1; // +1 => block count is stored on 1 byte
memcpy(data, &rx[res_overhead + 1], *data_len);
return *data_len;
}
int
main(int argc, char *argv[])
{
(void)argc;
(void)argv;
int ch;
bool quiet = false;
char *ndef_output = NULL;
while ((ch = getopt(argc, argv, "hqo:")) != -1) {
switch (ch) {
case 'h':
print_usage(argv[0]);
exit(EXIT_SUCCESS);
case 'q':
quiet = true;
break;
case 'o':
ndef_output = optarg;
break;
default:
print_usage(argv[0]);
exit(EXIT_FAILURE);
}
}
if (ndef_output == NULL) {
print_usage(argv[0]);
exit(EXIT_FAILURE);
}
FILE *message_stream = NULL;
FILE *ndef_stream = NULL;
if ((strlen(ndef_output) == 1) && (ndef_output[0] == '-')) {
message_stream = stderr;
ndef_stream = stdout;
} else {
message_stream = stdout;
ndef_stream = fopen(ndef_output, "wb");
if (!ndef_stream) {
fprintf(stderr, "Could not open file %s.\n", ndef_output);
exit(EXIT_FAILURE);
}
}
nfc_init(&context);
if (context == NULL) {
ERR("Unable to init libnfc (malloc)\n");
exit(EXIT_FAILURE);
}
pnd = nfc_open(context, NULL);
if (pnd == NULL) {
ERR("Unable to open NFC device");
fclose(ndef_stream);
nfc_exit(context);
exit(EXIT_FAILURE);
}
if (!quiet) {
fprintf(message_stream, "NFC device: %s opened\n", nfc_device_get_name(pnd));
}
nfc_modulation nm = {
.nmt = NMT_FELICA,
.nbr = NBR_212,
};
signal(SIGINT, stop_select);
nfc_target nt;
if (nfc_initiator_init(pnd) < 0) {
nfc_perror(pnd, "nfc_initiator_init");
fclose(ndef_stream);
nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
}
if (!quiet) {
fprintf(message_stream, "Place your NFC Forum Tag Type 3 in the field...\n");
}
// Polling payload (SENSF_REQ) must be present (see NFC Digital Protol)
const uint8_t *pbtSensfReq = (uint8_t *)"\x00\xff\xff\x01\x00";
if (nfc_initiator_select_passive_target(pnd, nm, pbtSensfReq, 5, &nt) <= 0) {
nfc_perror(pnd, "nfc_initiator_select_passive_target");
fclose(ndef_stream);
nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
}
// Check if System Code equals 0x12fc
const uint8_t abtNfcForumSysCode[] = { 0x12, 0xfc };
if (0 != memcmp(nt.nti.nfi.abtSysCode, abtNfcForumSysCode, 2)) {
// Retry with special polling
const uint8_t *pbtSensfReqNfcForum = (uint8_t *)"\x00\x12\xfc\x01\x00";
if (nfc_initiator_select_passive_target(pnd, nm, pbtSensfReqNfcForum, 5, &nt) <= 0) {
nfc_perror(pnd, "nfc_initiator_select_passive_target");
fclose(ndef_stream);
nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
}
// Check again if System Code equals 0x12fc
if (0 != memcmp(nt.nti.nfi.abtSysCode, abtNfcForumSysCode, 2)) {
fprintf(stderr, "Tag is not NFC Forum Tag Type 3 compliant.\n");
fclose(ndef_stream);
nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
}
}
//print_nfc_felica_info(nt.nti.nfi, true);
if ((nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, false) < 0) || (nfc_device_set_property_bool(pnd, NP_INFINITE_SELECT, false) < 0)) {
nfc_perror(pnd, "nfc_device_set_property_bool");
fclose(ndef_stream);
nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
}
uint8_t data[1024];
size_t data_len = sizeof(data);
if (nfc_forum_tag_type3_check(pnd, &nt, 0, 1, data, &data_len) <= 0) {
nfc_perror(pnd, "nfc_forum_tag_type3_check");
fclose(ndef_stream);
nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
}
const int ndef_major_version = (data[0] & 0xf0) >> 4;
const int ndef_minor_version = (data[0] & 0x0f);
const int ndef_nbr = data[1];
const int ndef_nbw = data[2];
const int ndef_nmaxb = (data[3] << 8) + data[4];
const int ndef_writeflag = data[9];
const int ndef_rwflag = data[10];
uint32_t ndef_data_len = (data[11] << 16) + (data[12] << 8) + data[13];
uint16_t ndef_calculated_checksum = 0;
for (size_t n = 0; n < 14; n++)
ndef_calculated_checksum += data[n];
const uint16_t ndef_checksum = (data[14] << 8) + data[15];
if (!quiet) {
fprintf(message_stream, "NDEF Attribute Block:\n");
fprintf(message_stream, "* Mapping version: %d.%d\n", ndef_major_version, ndef_minor_version);
fprintf(message_stream, "* Maximum nr of blocks to read by Check Command: %3d block%s\n", ndef_nbr, ndef_nbr > 1 ? "s" : "");
fprintf(message_stream, "* Maximum nr of blocks to write by Update Command: %3d block%s\n", ndef_nbw, ndef_nbw > 1 ? "s" : "");
fprintf(message_stream, "* Maximum nr of blocks available for NDEF data: %3d block%s (%d bytes)\n", ndef_nmaxb, ndef_nmaxb > 1 ? "s" : "", ndef_nmaxb * 16);
fprintf(message_stream, "* NDEF writing state: ");
switch (ndef_writeflag) {
case 0x00:
fprintf(message_stream, "finished (0x00)\n");
break;
case 0x0f:
fprintf(message_stream, "in progress (0x0F)\n");
break;
default:
fprintf(message_stream, "invalid (0x%02X)\n", ndef_writeflag);
break;
}
fprintf(message_stream, "* NDEF Access Attribute: ");
switch (ndef_rwflag) {
case 0x00:
fprintf(message_stream, "Read only (0x00)\n");
break;
case 0x01:
fprintf(message_stream, "Read/Write (0x01)\n");
break;
default:
fprintf(message_stream, "invalid (0x%02X)\n", ndef_rwflag);
break;
}
fprintf(message_stream, "* NDEF message length: %d bytes\n", ndef_data_len);
if (ndef_calculated_checksum != ndef_checksum) {
fprintf(message_stream, "* Checksum: fail (0x%04X != 0x%04X)\n", ndef_calculated_checksum, ndef_checksum);
} else {
fprintf(message_stream, "* Checksum: ok (0x%04X)\n", ndef_checksum);
}
}
if (ndef_calculated_checksum != ndef_checksum) {
fprintf(stderr, "Error: Checksum failed! Exiting now.\n");
fclose(ndef_stream);
nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
}
if (!ndef_data_len) {
fprintf(stderr, "Error: empty NFC Forum Tag Type 3, nothing to read!\n");
fclose(ndef_stream);
nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
}
const uint8_t block_max_per_check = data[1];
const uint16_t block_count_to_check = (ndef_data_len / 16) + 1;
data_len = 0;
for (uint16_t b = 0; b < ((block_count_to_check - 1) / block_max_per_check + 1); b += block_max_per_check) {
size_t size = sizeof(data) - data_len;
if (!nfc_forum_tag_type3_check(pnd, &nt, 1 + b, MIN(block_max_per_check, (block_count_to_check - (b * block_max_per_check))), data + data_len, &size)) {
nfc_perror(pnd, "nfc_forum_tag_type3_check");
fclose(ndef_stream);
nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
}
data_len += size;
}
if (fwrite(data, 1, ndef_data_len, ndef_stream) != ndef_data_len) {
fprintf(stderr, "Error: could not write to file.\n");
fclose(ndef_stream);
nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
} else {
if (!quiet) {
fprintf(stderr, "%i bytes written to %s\n", ndef_data_len, ndef_output);
}
}
fclose(ndef_stream);
nfc_close(pnd);
nfc_exit(context);
exit(EXIT_SUCCESS);
}