2017-04-12 13:45:33 +02:00
|
|
|
#if defined(HAVE_CONFIG_H)
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
2012-03-15 02:48:43 +01:00
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <freefare.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This example was written based on information provided by the
|
|
|
|
* following documents:
|
|
|
|
*
|
|
|
|
* Mifare DESFire as Type 4 Tag
|
|
|
|
* NFC Forum Type 4 Tag Extensions for Mifare DESFire
|
|
|
|
* Rev. 1.1 - 21 August 2007
|
2012-05-15 23:22:25 +02:00
|
|
|
* Rev. 2.2 - 4 January 2012
|
2012-03-15 02:48:43 +01:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Note that it is using specific Desfire commands, not ISO7816 NDEF Tag Type4 commands
|
|
|
|
|
|
|
|
uint8_t key_data_app[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
|
|
|
|
|
|
|
|
uint8_t *cc_data;
|
|
|
|
uint8_t *ndef_msg;
|
|
|
|
uint16_t ndef_msg_len;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
bool interactive;
|
|
|
|
} read_options = {
|
|
|
|
.interactive = true
|
|
|
|
};
|
|
|
|
|
2012-05-18 18:18:45 +02:00
|
|
|
static void
|
2012-03-15 02:48:43 +01:00
|
|
|
usage(char *progname)
|
|
|
|
{
|
2017-06-27 13:58:31 +02:00
|
|
|
fprintf(stderr, "This application reads a NDEF payload from a Mifare DESFire formatted as NFC Forum Type 4 Tag.\n");
|
|
|
|
fprintf(stderr, "usage: %s [-y] -o FILE [-k 11223344AABBCCDD]\n", progname);
|
|
|
|
fprintf(stderr, "\nOptions:\n");
|
|
|
|
fprintf(stderr, " -y Do not ask for confirmation\n");
|
|
|
|
fprintf(stderr, " -o Extract NDEF message if available in FILE\n");
|
|
|
|
fprintf(stderr, " -k Provide another NDEF Tag Application key than the default one\n");
|
2012-03-15 02:48:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int ch;
|
|
|
|
int error = EXIT_SUCCESS;
|
|
|
|
nfc_device *device = NULL;
|
2015-05-11 18:48:10 +02:00
|
|
|
FreefareTag *tags = NULL;
|
2012-03-15 02:48:43 +01:00
|
|
|
|
|
|
|
char *ndef_output = NULL;
|
2017-06-27 13:58:31 +02:00
|
|
|
while ((ch = getopt(argc, argv, "hyo:k:")) != -1) {
|
|
|
|
switch (ch) {
|
|
|
|
case 'h':
|
|
|
|
usage(argv[0]);
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
break;
|
|
|
|
case 'y':
|
|
|
|
read_options.interactive = false;
|
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
ndef_output = optarg;
|
|
|
|
break;
|
|
|
|
case 'k':
|
|
|
|
if (strlen(optarg) != 16) {
|
|
|
|
usage(argv[0]);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
uint64_t n = strtoull(optarg, NULL, 16);
|
|
|
|
int i;
|
|
|
|
for (i = 7; i >= 0; i--) {
|
|
|
|
key_data_app[i] = (uint8_t) n;
|
|
|
|
n >>= 8;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage(argv[0]);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2012-03-15 02:48:43 +01:00
|
|
|
}
|
|
|
|
// Remaining args, if any, are in argv[optind .. (argc-1)]
|
|
|
|
|
|
|
|
if (ndef_output == NULL) {
|
2017-06-27 13:58:31 +02:00
|
|
|
usage(argv[0]);
|
|
|
|
exit(EXIT_FAILURE);
|
2012-03-15 02:48:43 +01:00
|
|
|
}
|
2017-06-27 13:58:31 +02:00
|
|
|
FILE *message_stream = NULL;
|
|
|
|
FILE *ndef_stream = NULL;
|
2012-03-15 02:48:43 +01:00
|
|
|
|
2017-06-27 13:58:31 +02:00
|
|
|
if ((strlen(ndef_output) == 1) && (ndef_output[0] == '-')) {
|
2012-03-15 02:48:43 +01:00
|
|
|
message_stream = stderr;
|
|
|
|
ndef_stream = stdout;
|
|
|
|
} else {
|
|
|
|
message_stream = stdout;
|
|
|
|
ndef_stream = fopen(ndef_output, "wb");
|
|
|
|
if (!ndef_stream) {
|
2017-06-27 13:58:31 +02:00
|
|
|
fprintf(stderr, "Could not open file %s.\n", ndef_output);
|
|
|
|
exit(EXIT_FAILURE);
|
2012-03-15 02:48:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nfc_connstring devices[8];
|
|
|
|
size_t device_count;
|
2015-05-12 13:19:00 +02:00
|
|
|
|
2012-12-23 22:30:10 +01:00
|
|
|
nfc_context *context;
|
2017-06-27 13:58:31 +02:00
|
|
|
nfc_init(&context);
|
2013-03-30 18:07:34 +01:00
|
|
|
if (context == NULL)
|
|
|
|
errx(EXIT_FAILURE, "Unable to init libnfc (malloc)");
|
2012-03-15 02:48:43 +01:00
|
|
|
|
2017-06-27 13:58:31 +02:00
|
|
|
device_count = nfc_list_devices(context, devices, 8);
|
2012-03-15 02:48:43 +01:00
|
|
|
if (device_count <= 0)
|
2017-06-27 13:58:31 +02:00
|
|
|
errx(EXIT_FAILURE, "No NFC device found.");
|
2012-03-15 02:48:43 +01:00
|
|
|
|
|
|
|
for (size_t d = 0; d < device_count; d++) {
|
2017-06-27 13:58:31 +02:00
|
|
|
device = nfc_open(context, devices[d]);
|
2015-05-12 13:19:00 +02:00
|
|
|
|
2017-06-27 13:58:31 +02:00
|
|
|
if (!device) {
|
|
|
|
warnx("nfc_open() failed.");
|
|
|
|
error = EXIT_FAILURE;
|
|
|
|
continue;
|
|
|
|
}
|
2012-03-15 02:48:43 +01:00
|
|
|
|
2017-06-27 13:58:31 +02:00
|
|
|
tags = freefare_get_tags(device);
|
2012-03-15 02:48:43 +01:00
|
|
|
if (!tags) {
|
2017-06-27 13:58:31 +02:00
|
|
|
nfc_close(device);
|
|
|
|
errx(EXIT_FAILURE, "Error listing tags.");
|
2012-03-15 02:48:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; (!error) && tags[i]; i++) {
|
2017-06-27 13:58:31 +02:00
|
|
|
if (MIFARE_DESFIRE != freefare_get_tag_type(tags[i]))
|
2012-03-15 02:48:43 +01:00
|
|
|
continue;
|
|
|
|
|
2017-06-27 13:58:31 +02:00
|
|
|
char *tag_uid = freefare_get_tag_uid(tags[i]);
|
2012-03-15 02:48:43 +01:00
|
|
|
char buffer[BUFSIZ];
|
|
|
|
|
2017-06-27 13:58:31 +02:00
|
|
|
fprintf(message_stream, "Found %s with UID %s. ", freefare_get_tag_friendly_name(tags[i]), tag_uid);
|
2012-03-15 02:48:43 +01:00
|
|
|
|
|
|
|
bool read_ndef = true;
|
|
|
|
if (read_options.interactive) {
|
2017-06-27 13:58:31 +02:00
|
|
|
fprintf(message_stream, "Read NDEF [yN] ");
|
|
|
|
fgets(buffer, BUFSIZ, stdin);
|
2012-03-15 02:48:43 +01:00
|
|
|
read_ndef = ((buffer[0] == 'y') || (buffer[0] == 'Y'));
|
|
|
|
} else {
|
2017-06-27 13:58:31 +02:00
|
|
|
fprintf(message_stream, "\n");
|
2012-03-15 02:48:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (read_ndef) {
|
|
|
|
int res;
|
|
|
|
|
2017-06-27 13:58:31 +02:00
|
|
|
res = mifare_desfire_connect(tags[i]);
|
2012-03-15 02:48:43 +01:00
|
|
|
if (res < 0) {
|
2017-06-27 13:58:31 +02:00
|
|
|
warnx("Can't connect to Mifare DESFire target.");
|
2012-03-15 02:48:43 +01:00
|
|
|
error = EXIT_FAILURE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-05-15 23:22:25 +02:00
|
|
|
// We've to track DESFire version as NDEF mapping is different
|
|
|
|
struct mifare_desfire_version_info info;
|
2017-06-27 13:58:31 +02:00
|
|
|
res = mifare_desfire_get_version(tags[i], &info);
|
2012-05-15 23:22:25 +02:00
|
|
|
if (res < 0) {
|
2017-06-27 13:58:31 +02:00
|
|
|
freefare_perror(tags[i], "mifare_desfire_get_version");
|
2012-05-15 23:22:25 +02:00
|
|
|
error = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-03-15 02:48:43 +01:00
|
|
|
MifareDESFireKey key_app;
|
2017-06-27 13:58:31 +02:00
|
|
|
key_app = mifare_desfire_des_key_new_with_version(key_data_app);
|
2012-03-15 02:48:43 +01:00
|
|
|
|
|
|
|
// Mifare DESFire SelectApplication (Select application)
|
2012-05-15 23:22:25 +02:00
|
|
|
MifareDESFireAID aid;
|
2017-06-27 13:58:31 +02:00
|
|
|
if (info.software.version_major == 0)
|
2012-05-15 23:22:25 +02:00
|
|
|
aid = mifare_desfire_aid_new(0xEEEE10);
|
|
|
|
else
|
|
|
|
// There is no more relationship between DESFire AID and ISO AID...
|
|
|
|
// Let's assume it's in AID 000001h as proposed in the spec
|
|
|
|
aid = mifare_desfire_aid_new(0x000001);
|
2012-03-15 02:48:43 +01:00
|
|
|
res = mifare_desfire_select_application(tags[i], aid);
|
|
|
|
if (res < 0)
|
2017-06-27 13:58:31 +02:00
|
|
|
errx(EXIT_FAILURE, "Application selection failed. Try mifare-desfire-create-ndef before running %s.", argv[0]);
|
|
|
|
free(aid);
|
2012-03-15 02:48:43 +01:00
|
|
|
|
|
|
|
// Authentication with NDEF Tag Application master key (Authentication with key 0)
|
2017-06-27 13:58:31 +02:00
|
|
|
res = mifare_desfire_authenticate(tags[i], 0, key_app);
|
2012-03-15 02:48:43 +01:00
|
|
|
if (res < 0)
|
2017-06-27 13:58:31 +02:00
|
|
|
errx(EXIT_FAILURE, "Authentication with NDEF Tag Application master key failed");
|
2012-03-15 02:48:43 +01:00
|
|
|
|
|
|
|
// Read Capability Container file E103
|
2013-04-29 00:51:06 +02:00
|
|
|
uint8_t lendata[20]; // cf FIXME in mifare_desfire.c read_data()
|
2017-06-27 13:58:31 +02:00
|
|
|
if (info.software.version_major == 0)
|
|
|
|
res = mifare_desfire_read_data(tags[i], 0x03, 0, 2, lendata);
|
2012-05-15 23:22:25 +02:00
|
|
|
else
|
|
|
|
// There is no more relationship between DESFire FID and ISO FileID...
|
|
|
|
// Let's assume it's in FID 01h as proposed in the spec
|
2017-06-27 13:58:31 +02:00
|
|
|
res = mifare_desfire_read_data(tags[i], 0x01, 0, 2, lendata);
|
2012-03-15 02:48:43 +01:00
|
|
|
if (res < 0)
|
2017-06-27 13:58:31 +02:00
|
|
|
errx(EXIT_FAILURE, "Read CC len failed");
|
2012-03-15 02:48:43 +01:00
|
|
|
uint16_t cclen = (((uint16_t) lendata[0]) << 8) + ((uint16_t) lendata[1]);
|
|
|
|
if (cclen < 15)
|
2017-06-27 13:58:31 +02:00
|
|
|
errx(EXIT_FAILURE, "CC too short IMHO");
|
|
|
|
if (!(cc_data = malloc(cclen + 20))) // cf FIXME in mifare_desfire.c read_data()
|
|
|
|
errx(EXIT_FAILURE, "malloc");
|
|
|
|
if (info.software.version_major == 0)
|
|
|
|
res = mifare_desfire_read_data(tags[i], 0x03, 0, cclen, cc_data);
|
2012-05-15 23:22:25 +02:00
|
|
|
else
|
2017-06-27 13:58:31 +02:00
|
|
|
res = mifare_desfire_read_data(tags[i], 0x01, 0, cclen, cc_data);
|
2012-03-15 02:48:43 +01:00
|
|
|
if (res < 0)
|
2017-06-27 13:58:31 +02:00
|
|
|
errx(EXIT_FAILURE, "Read CC data failed");
|
2012-03-15 02:48:43 +01:00
|
|
|
// Search NDEF File Control TLV
|
|
|
|
uint8_t off = 7;
|
2017-06-27 13:58:31 +02:00
|
|
|
while (((off + 7) < cclen) && (cc_data[off] != 0x04)) {
|
2012-03-15 02:48:43 +01:00
|
|
|
// Skip TLV
|
2017-06-27 13:58:31 +02:00
|
|
|
off += cc_data[off + 1] + 2;
|
2012-03-15 02:48:43 +01:00
|
|
|
}
|
2017-06-27 13:58:31 +02:00
|
|
|
if (off + 7 >= cclen)
|
|
|
|
errx(EXIT_FAILURE, "CC does not contain expected NDEF File Control TLV");
|
|
|
|
if (cc_data[off + 2] != 0xE1)
|
|
|
|
errx(EXIT_FAILURE, "Unknown NDEF File reference in CC");
|
2012-05-15 23:22:25 +02:00
|
|
|
uint8_t file_no;
|
2017-06-27 13:58:31 +02:00
|
|
|
if (info.software.version_major == 0)
|
|
|
|
file_no = cc_data[off + 3];
|
2012-05-15 23:22:25 +02:00
|
|
|
else
|
|
|
|
// There is no more relationship between DESFire FID and ISO FileID...
|
|
|
|
// Let's assume it's in FID 02h as proposed in the spec
|
|
|
|
file_no = 2;
|
2017-06-27 13:58:31 +02:00
|
|
|
uint16_t ndefmaxlen = (((uint16_t) cc_data[off + 4]) << 8) + ((uint16_t) cc_data[off + 5]);
|
|
|
|
fprintf(message_stream, "Max NDEF size: %i bytes\n", ndefmaxlen);
|
|
|
|
if (!(ndef_msg = malloc(ndefmaxlen + 20))) // cf FIXME in mifare_desfire.c read_data()
|
|
|
|
errx(EXIT_FAILURE, "malloc");
|
2012-03-15 02:48:43 +01:00
|
|
|
|
2017-06-27 13:58:31 +02:00
|
|
|
res = mifare_desfire_read_data(tags[i], file_no, 0, 2, lendata);
|
2012-03-15 02:48:43 +01:00
|
|
|
if (res < 0)
|
2017-06-27 13:58:31 +02:00
|
|
|
errx(EXIT_FAILURE, "Read NDEF len failed");
|
2012-03-15 02:48:43 +01:00
|
|
|
ndef_msg_len = (((uint16_t) lendata[0]) << 8) + ((uint16_t) lendata[1]);
|
2017-06-27 13:58:31 +02:00
|
|
|
fprintf(message_stream, "NDEF size: %i bytes\n", ndef_msg_len);
|
2012-03-15 02:48:43 +01:00
|
|
|
if (ndef_msg_len + 2 > ndefmaxlen)
|
2017-06-27 13:58:31 +02:00
|
|
|
errx(EXIT_FAILURE, "Declared NDEF size larger than max NDEF size");
|
|
|
|
res = mifare_desfire_read_data(tags[i], file_no, 2, ndef_msg_len, ndef_msg);
|
2012-03-15 02:48:43 +01:00
|
|
|
if (res < 0)
|
2017-06-27 13:58:31 +02:00
|
|
|
errx(EXIT_FAILURE, "Read data failed");
|
|
|
|
if (fwrite(ndef_msg, 1, ndef_msg_len, ndef_stream) != ndef_msg_len)
|
|
|
|
errx(EXIT_FAILURE, "Write to file failed");
|
|
|
|
free(cc_data);
|
|
|
|
free(ndef_msg);
|
|
|
|
mifare_desfire_key_free(key_app);
|
|
|
|
|
|
|
|
mifare_desfire_disconnect(tags[i]);
|
2012-03-15 02:48:43 +01:00
|
|
|
}
|
2017-06-27 13:58:31 +02:00
|
|
|
free(tag_uid);
|
2012-03-15 02:48:43 +01:00
|
|
|
}
|
2017-06-27 13:58:31 +02:00
|
|
|
freefare_free_tags(tags);
|
|
|
|
nfc_close(device);
|
2012-03-15 02:48:43 +01:00
|
|
|
}
|
2017-06-27 13:58:31 +02:00
|
|
|
nfc_exit(context);
|
|
|
|
exit(error);
|
2012-03-15 02:48:43 +01:00
|
|
|
}
|