2010-07-26 23:48:18 +02:00
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <nfc/nfc.h>
|
|
|
|
|
|
|
|
#include <freefare.h>
|
|
|
|
|
|
|
|
uint8_t key_data_null[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int error = EXIT_SUCCESS;
|
2012-01-25 10:58:16 +01:00
|
|
|
nfc_device *device = NULL;
|
2015-05-11 18:48:10 +02:00
|
|
|
FreefareTag *tags = NULL;
|
2010-07-26 23:48:18 +02:00
|
|
|
|
|
|
|
if (argc > 1)
|
2017-06-27 13:58:31 +02:00
|
|
|
errx(EXIT_FAILURE, "usage: %s", argv[0]);
|
2010-07-26 23:48:18 +02:00
|
|
|
|
2012-01-25 10:58:16 +01:00
|
|
|
nfc_connstring devices[8];
|
2010-07-26 23:48:18 +02:00
|
|
|
size_t device_count;
|
|
|
|
|
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-12-23 22:30:10 +01:00
|
|
|
|
2017-06-27 13:58:31 +02:00
|
|
|
device_count = nfc_list_devices(context, devices, 8);
|
2012-01-25 10:58:16 +01:00
|
|
|
if (device_count <= 0)
|
2017-06-27 13:58:31 +02:00
|
|
|
errx(EXIT_FAILURE, "No NFC device found.");
|
2010-07-26 23:48:18 +02:00
|
|
|
|
|
|
|
for (size_t d = 0; d < device_count; d++) {
|
2017-06-27 13:58:31 +02:00
|
|
|
device = nfc_open(context, devices[d]);
|
|
|
|
if (!device) {
|
|
|
|
warnx("nfc_open() failed.");
|
|
|
|
error = EXIT_FAILURE;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
tags = freefare_get_tags(device);
|
2010-09-03 20:01:02 +02:00
|
|
|
if (!tags) {
|
2017-06-27 13:58:31 +02:00
|
|
|
nfc_close(device);
|
|
|
|
errx(EXIT_FAILURE, "Error listing tags.");
|
2010-09-03 20:01:02 +02:00
|
|
|
}
|
2010-07-26 23:48:18 +02:00
|
|
|
|
2010-09-03 20:01:02 +02: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]))
|
2010-09-03 20:01:02 +02:00
|
|
|
continue;
|
2010-07-26 23:48:18 +02:00
|
|
|
|
2010-09-03 20:01:02 +02:00
|
|
|
int res;
|
2017-06-27 13:58:31 +02:00
|
|
|
char *tag_uid = freefare_get_tag_uid(tags[i]);
|
2010-07-26 23:48:18 +02:00
|
|
|
|
2017-06-27 13:58:31 +02:00
|
|
|
res = mifare_desfire_connect(tags[i]);
|
2010-09-03 20:01:02 +02:00
|
|
|
if (res < 0) {
|
2017-06-27 13:58:31 +02:00
|
|
|
warnx("Can't connect to Mifare DESFire target.");
|
2010-09-03 20:01:02 +02:00
|
|
|
error = EXIT_FAILURE;
|
|
|
|
break;
|
|
|
|
}
|
2010-07-26 23:48:18 +02:00
|
|
|
|
|
|
|
|
2017-06-27 13:58:31 +02:00
|
|
|
MifareDESFireKey key = mifare_desfire_des_key_new_with_version(key_data_null);
|
|
|
|
res = mifare_desfire_authenticate(tags[i], 0, key);
|
2010-09-03 20:01:02 +02:00
|
|
|
if (res < 0)
|
2017-06-27 13:58:31 +02:00
|
|
|
errx(EXIT_FAILURE, "Authentication on master application failed");
|
2010-07-26 23:48:18 +02:00
|
|
|
|
2010-09-03 20:01:02 +02:00
|
|
|
MadAid mad_aid = { 0x12, 0x34 };
|
2017-06-27 13:58:31 +02:00
|
|
|
MifareDESFireAID aid = mifare_desfire_aid_new_with_mad_aid(mad_aid, 0x5);
|
|
|
|
res = mifare_desfire_create_application(tags[i], aid, 0xFF, 0x1);
|
2010-09-03 20:01:02 +02:00
|
|
|
if (res < 0)
|
2017-06-27 13:58:31 +02:00
|
|
|
errx(EXIT_FAILURE, "Application creation failed");
|
2010-07-26 23:48:18 +02:00
|
|
|
|
2017-06-27 13:58:31 +02:00
|
|
|
res = mifare_desfire_select_application(tags[i], aid);
|
2010-09-03 20:01:02 +02:00
|
|
|
if (res < 0)
|
2017-06-27 13:58:31 +02:00
|
|
|
errx(EXIT_FAILURE, "Application selection failed");
|
2010-07-26 23:48:18 +02:00
|
|
|
|
2017-06-27 13:58:31 +02:00
|
|
|
res = mifare_desfire_authenticate(tags[i], 0, key);
|
2010-09-03 20:01:02 +02:00
|
|
|
if (res < 0)
|
2017-06-27 13:58:31 +02:00
|
|
|
errx(EXIT_FAILURE, "Authentication on application failed");
|
2010-07-26 23:48:18 +02:00
|
|
|
|
2017-06-27 13:58:31 +02:00
|
|
|
res = mifare_desfire_create_std_data_file(tags[i], 1, MDCM_ENCIPHERED, 0x0000, 20);
|
2010-09-03 20:01:02 +02:00
|
|
|
if (res < 0)
|
2017-06-27 13:58:31 +02:00
|
|
|
errx(EXIT_FAILURE, "File creation failed");
|
2010-07-26 23:48:18 +02:00
|
|
|
|
2017-06-27 13:58:31 +02:00
|
|
|
const char *s = "Hello World";
|
|
|
|
res = mifare_desfire_write_data(tags[i], 1, 0, strlen(s), s);
|
2010-09-03 20:01:02 +02:00
|
|
|
if (res < 0)
|
2017-06-27 13:58:31 +02:00
|
|
|
errx(EXIT_FAILURE, "File write failed");
|
2010-07-26 23:48:18 +02:00
|
|
|
|
2010-09-03 20:01:02 +02:00
|
|
|
char buffer[20];
|
2017-06-27 13:58:31 +02:00
|
|
|
res = mifare_desfire_read_data(tags[i], 1, 0, 0, buffer);
|
2010-09-03 20:01:02 +02:00
|
|
|
if (res < 0)
|
2017-06-27 13:58:31 +02:00
|
|
|
errx(EXIT_FAILURE, "File read failed");
|
2010-07-26 23:48:18 +02:00
|
|
|
|
2017-06-27 13:58:31 +02:00
|
|
|
res = mifare_desfire_select_application(tags[i], NULL);
|
2010-09-03 20:01:02 +02:00
|
|
|
if (res < 0)
|
2017-06-27 13:58:31 +02:00
|
|
|
errx(EXIT_FAILURE, "Master application selection failed");
|
2010-07-26 23:48:18 +02:00
|
|
|
|
2017-06-27 13:58:31 +02:00
|
|
|
res = mifare_desfire_authenticate(tags[i], 0, key);
|
2010-09-03 20:01:02 +02:00
|
|
|
if (res < 0)
|
2017-06-27 13:58:31 +02:00
|
|
|
errx(EXIT_FAILURE, "Authentication on master application failed");
|
2010-07-26 23:48:18 +02:00
|
|
|
|
2017-06-27 13:58:31 +02:00
|
|
|
res = mifare_desfire_format_picc(tags[i]);
|
2010-09-03 20:01:02 +02:00
|
|
|
if (res < 0)
|
2017-06-27 13:58:31 +02:00
|
|
|
errx(EXIT_FAILURE, "PICC format failed");
|
2010-07-26 23:48:18 +02:00
|
|
|
|
2017-06-27 13:58:31 +02:00
|
|
|
mifare_desfire_key_free(key);
|
|
|
|
free(tag_uid);
|
|
|
|
free(aid);
|
2010-07-26 23:48:18 +02:00
|
|
|
|
2017-06-27 13:58:31 +02:00
|
|
|
mifare_desfire_disconnect(tags[i]);
|
2010-09-03 20:01:02 +02:00
|
|
|
}
|
2010-07-26 23:48:18 +02:00
|
|
|
|
2017-06-27 13:58:31 +02:00
|
|
|
freefare_free_tags(tags);
|
|
|
|
nfc_close(device);
|
2010-07-26 23:48:18 +02:00
|
|
|
}
|
2017-06-27 13:58:31 +02:00
|
|
|
nfc_exit(context);
|
|
|
|
exit(error);
|
2010-07-26 23:48:18 +02:00
|
|
|
} /* main() */
|
|
|
|
|