diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index edb94ce..68fab6b 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -3,8 +3,8 @@ set(EXAMPLES-SOURCES mifare-classic-format mifare-classic-write-ndef mifare-desfire-access + mifare-desfire-create-ndef mifare-desfire-format - mifare-desfire-format-ndef mifare-desfire-info mifare-desfire-write-ndef mifare-desfire-ev1-configure-ats diff --git a/examples/Makefile.am b/examples/Makefile.am index 031535a..d8cc667 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -7,11 +7,11 @@ bin_PROGRAMS = mifare-classic-format \ mifare-classic-write-ndef \ mifare-classic-read-ndef \ mifare-desfire-access \ + mifare-desfire-create-ndef \ mifare-desfire-ev1-configure-ats \ mifare-desfire-ev1-configure-default-key \ mifare-desfire-ev1-configure-random-uid \ mifare-desfire-format \ - mifare-desfire-format-ndef \ mifare-desfire-info \ mifare-desfire-write-ndef @@ -27,6 +27,9 @@ mifare_classic_write_ndef_LDADD = -lnfc $(top_builddir)/libfreefare/libfreefare. mifare_desfire_access_SOURCES = mifare-desfire-access.c mifare_desfire_access_LDADD = -lnfc $(top_builddir)/libfreefare/libfreefare.la +mifare_desfire_create_ndef_SOURCES = mifare-desfire-create-ndef.c +mifare_desfire_create_ndef_LDADD = -lnfc $(top_builddir)/libfreefare/libfreefare.la + mifare_desfire_ev1_configure_ats_SOURCES = mifare-desfire-ev1-configure-ats.c mifare_desfire_ev1_configure_ats_LDADD = -lnfc $(top_builddir)/libfreefare/libfreefare.la @@ -39,9 +42,6 @@ mifare_desfire_ev1_configure_random_uid_LDADD = -lnfc $(top_builddir)/libfreefar mifare_desfire_format_SOURCES = mifare-desfire-format.c mifare_desfire_format_LDADD = -lnfc $(top_builddir)/libfreefare/libfreefare.la -mifare_desfire_format_ndef_SOURCES = mifare-desfire-format-ndef.c -mifare_desfire_format_ndef_LDADD = -lnfc $(top_builddir)/libfreefare/libfreefare.la - mifare_desfire_info_SOURCES = mifare-desfire-info.c mifare_desfire_info_LDADD = -lnfc $(top_builddir)/libfreefare/libfreefare.la -lm diff --git a/examples/mifare-classic-format.c b/examples/mifare-classic-format.c index 5a84e4b..8d841cf 100644 --- a/examples/mifare-classic-format.c +++ b/examples/mifare-classic-format.c @@ -159,8 +159,7 @@ main(int argc, char *argv[]) exit (EXIT_FAILURE); } } - argc -= optind; - argv += optind; + // Remaining args, if any, are in argv[optind .. (argc-1)] nfc_connstring devices[8]; diff --git a/examples/mifare-desfire-format-ndef.c b/examples/mifare-desfire-create-ndef.c similarity index 82% rename from examples/mifare-desfire-format-ndef.c rename to examples/mifare-desfire-create-ndef.c index 14c589e..5d585a6 100644 --- a/examples/mifare-desfire-format-ndef.c +++ b/examples/mifare-desfire-create-ndef.c @@ -17,8 +17,12 @@ * $Id$ */ +#include "config.h" + #include #include +#include +#include #include @@ -36,17 +40,58 @@ uint8_t key_data_picc[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; uint8_t key_data_app[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; +struct { + bool interactive; +} create_options = { + .interactive = true +}; + +void +usage(char *progname) +{ + fprintf (stderr, "usage: %s [-y]\n", progname); + fprintf (stderr, "\nOptions:\n"); + fprintf (stderr, " -y Do not ask for confirmation\n"); + fprintf (stderr, " -K 11223344AABBCCDD Provide another PICC key than the default one\n"); +} + int main(int argc, char *argv[]) { + int ch; int error = EXIT_SUCCESS; nfc_device *device = NULL; MifareTag *tags = NULL; - printf ("NOTE: This application turns Mifare DESFire targets into NFC Forum Type 4 Tags.\n"); + while ((ch = getopt (argc, argv, "hyK:")) != -1) { + switch (ch) { + case 'h': + usage(argv[0]); + exit (EXIT_SUCCESS); + break; + case 'y': + create_options.interactive = false; + 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_picc[i] = (uint8_t) n; + n >>= 8; + } + break; + default: + usage(argv[0]); + exit (EXIT_FAILURE); + } + } + // Remaining args, if any, are in argv[optind .. (argc-1)] - if (argc > 1) - errx (EXIT_FAILURE, "usage: %s", argv[0]); + printf ("NOTE: This application turns Mifare DESFire targets into NFC Forum Type 4 Tags.\n"); nfc_connstring devices[8]; size_t device_count; @@ -79,11 +124,17 @@ main(int argc, char *argv[]) char *tag_uid = freefare_get_tag_uid (tags[i]); char buffer[BUFSIZ]; - printf ("Found %s with UID %s. Format as NDEF [yN] ", freefare_get_tag_friendly_name (tags[i]), tag_uid); - fgets (buffer, BUFSIZ, stdin); - bool write_ndef = ((buffer[0] == 'y') || (buffer[0] == 'Y')); + printf ("Found %s with UID %s. ", freefare_get_tag_friendly_name (tags[i]), tag_uid); + bool create_ndef = true; + if (create_options.interactive) { + printf ("Create NDEF app [yN] "); + fgets (buffer, BUFSIZ, stdin); + create_ndef = ((buffer[0] == 'y') || (buffer[0] == 'Y')); + } else { + printf ("\n"); + } - if (write_ndef) { + if (create_ndef) { int res; res = mifare_desfire_connect (tags[i]); diff --git a/examples/mifare-desfire-ev1-configure-ats.c b/examples/mifare-desfire-ev1-configure-ats.c index ecc9ae6..aa614ca 100644 --- a/examples/mifare-desfire-ev1-configure-ats.c +++ b/examples/mifare-desfire-ev1-configure-ats.c @@ -29,8 +29,9 @@ #include -uint8_t null_key_data[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; +uint8_t key_data_picc[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; +// TODO: Allow to pass another ATS as option // Default Mifare DESFire ATS uint8_t new_ats[] = { 0x06, 0x75, 0x77, 0x81, 0x02, 0x80 }; @@ -46,6 +47,7 @@ usage(char *progname) fprintf (stderr, "usage: %s [-y]\n", progname); fprintf (stderr, "\nOptions:\n"); fprintf (stderr, " -y Do not ask for confirmation (dangerous)\n"); + fprintf (stderr, " -K 11223344AABBCCDD Provide another PICC key than the default one\n"); } int @@ -56,7 +58,7 @@ main(int argc, char *argv[]) nfc_device *device = NULL; MifareTag *tags = NULL; - while ((ch = getopt (argc, argv, "hy")) != -1) { + while ((ch = getopt (argc, argv, "hyK:")) != -1) { switch (ch) { case 'h': usage(argv[0]); @@ -65,13 +67,24 @@ main(int argc, char *argv[]) case 'y': configure_options.interactive = false; 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_picc[i] = (uint8_t) n; + n >>= 8; + } + break; default: usage(argv[0]); exit (EXIT_FAILURE); } } - argc -= optind; - argv += optind; + // Remaining args, if any, are in argv[optind .. (argc-1)] nfc_connstring devices[8]; size_t device_count; @@ -124,14 +137,14 @@ main(int argc, char *argv[]) break; } - MifareDESFireKey default_key = mifare_desfire_des_key_new_with_version (null_key_data); - res = mifare_desfire_authenticate (tags[i], 0, default_key); + MifareDESFireKey key_picc = mifare_desfire_des_key_new_with_version (key_data_picc); + res = mifare_desfire_authenticate (tags[i], 0, key_picc); if (res < 0) { freefare_perror (tags[i], "mifare_desfire_authenticate"); error = EXIT_FAILURE; break; } - mifare_desfire_key_free (default_key); + mifare_desfire_key_free (key_picc); res = mifare_desfire_set_ats (tags[i], new_ats); if (res < 0) { diff --git a/examples/mifare-desfire-ev1-configure-default-key.c b/examples/mifare-desfire-ev1-configure-default-key.c index 01e209b..a9e15f2 100644 --- a/examples/mifare-desfire-ev1-configure-default-key.c +++ b/examples/mifare-desfire-ev1-configure-default-key.c @@ -70,8 +70,7 @@ main(int argc, char *argv[]) exit (EXIT_FAILURE); } } - argc -= optind; - argv += optind; + // Remaining args, if any, are in argv[optind .. (argc-1)] nfc_connstring devices[8]; size_t device_count; diff --git a/examples/mifare-desfire-ev1-configure-random-uid.c b/examples/mifare-desfire-ev1-configure-random-uid.c index d19ec2f..b3b24c0 100644 --- a/examples/mifare-desfire-ev1-configure-random-uid.c +++ b/examples/mifare-desfire-ev1-configure-random-uid.c @@ -29,7 +29,7 @@ #include -uint8_t null_key_data[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; +uint8_t key_data_picc[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; struct { bool interactive; @@ -43,6 +43,7 @@ usage(char *progname) fprintf (stderr, "usage: %s [-y]\n", progname); fprintf (stderr, "\nOptions:\n"); fprintf (stderr, " -y Do not ask for confirmation (dangerous)\n"); + fprintf (stderr, " -K 11223344AABBCCDD Provide another PICC key than the default one\n"); } int @@ -53,7 +54,7 @@ main(int argc, char *argv[]) nfc_device *device = NULL; MifareTag *tags = NULL; - while ((ch = getopt (argc, argv, "hy")) != -1) { + while ((ch = getopt (argc, argv, "hyK:")) != -1) { switch (ch) { case 'h': usage(argv[0]); @@ -62,13 +63,24 @@ main(int argc, char *argv[]) case 'y': configure_options.interactive = false; 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_picc[i] = (uint8_t) n; + n >>= 8; + } + break; default: usage(argv[0]); exit (EXIT_FAILURE); } } - argc -= optind; - argv += optind; + // Remaining args, if any, are in argv[optind .. (argc-1)] nfc_connstring devices[8]; size_t device_count; @@ -124,14 +136,14 @@ main(int argc, char *argv[]) break; } - MifareDESFireKey default_key = mifare_desfire_des_key_new_with_version (null_key_data); - res = mifare_desfire_authenticate (tags[i], 0, default_key); + MifareDESFireKey key_picc = mifare_desfire_des_key_new_with_version (key_data_picc); + res = mifare_desfire_authenticate (tags[i], 0, key_picc); if (res < 0) { freefare_perror (tags[i], "mifare_desfire_authenticate"); error = EXIT_FAILURE; break; } - mifare_desfire_key_free (default_key); + mifare_desfire_key_free (key_picc); res = mifare_desfire_set_configuration (tags[i], false, true); if (res < 0) { @@ -152,14 +164,14 @@ main(int argc, char *argv[]) break; } - MifareDESFireKey default_key = mifare_desfire_des_key_new_with_version (null_key_data); - res = mifare_desfire_authenticate (tags[i], 0, default_key); + MifareDESFireKey key_picc = mifare_desfire_des_key_new_with_version (key_data_picc); + res = mifare_desfire_authenticate (tags[i], 0, key_picc); if (res < 0) { freefare_perror (tags[i], "mifare_desfire_authenticate"); error = EXIT_FAILURE; break; } - mifare_desfire_key_free (default_key); + mifare_desfire_key_free (key_picc); char *old_tag_uid; res = mifare_desfire_get_card_uid (tags[i], &old_tag_uid); diff --git a/examples/mifare-desfire-format.c b/examples/mifare-desfire-format.c index 4da00b0..aac39fd 100644 --- a/examples/mifare-desfire-format.c +++ b/examples/mifare-desfire-format.c @@ -29,8 +29,7 @@ #include -// TODO: allow to read non-default key from options, also for the other mifare-desfire* tools -uint8_t null_key_data[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; +uint8_t key_data_picc[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; struct { bool interactive; @@ -44,6 +43,7 @@ usage(char *progname) fprintf (stderr, "usage: %s [-y]\n", progname); fprintf (stderr, "\nOptions:\n"); fprintf (stderr, " -y Do not ask for confirmation (dangerous)\n"); + fprintf (stderr, " -K 11223344AABBCCDD Provide another PICC key than the default one\n"); } int @@ -54,7 +54,7 @@ main(int argc, char *argv[]) nfc_device *device = NULL; MifareTag *tags = NULL; - while ((ch = getopt (argc, argv, "hy")) != -1) { + while ((ch = getopt (argc, argv, "hyK:")) != -1) { switch (ch) { case 'h': usage(argv[0]); @@ -63,13 +63,24 @@ main(int argc, char *argv[]) case 'y': format_options.interactive = false; 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_picc[i] = (uint8_t) n; + n >>= 8; + } + break; default: usage(argv[0]); exit (EXIT_FAILURE); } } - argc -= optind; - argv += optind; + // Remaining args, if any, are in argv[optind .. (argc-1)] nfc_connstring devices[8]; size_t device_count; @@ -121,14 +132,14 @@ main(int argc, char *argv[]) break; } - MifareDESFireKey default_key = mifare_desfire_des_key_new_with_version (null_key_data); - res = mifare_desfire_authenticate (tags[i], 0, default_key); + MifareDESFireKey key_picc = mifare_desfire_des_key_new_with_version (key_data_picc); + res = mifare_desfire_authenticate (tags[i], 0, key_picc); if (res < 0) { warnx ("Can't authenticate on Mifare DESFire target."); error = EXIT_FAILURE; break; } - mifare_desfire_key_free (default_key); + mifare_desfire_key_free (key_picc); res = mifare_desfire_format_picc (tags[i]); if (res < 0) { diff --git a/examples/mifare-desfire-write-ndef.c b/examples/mifare-desfire-write-ndef.c index 8f3d243..f1c83b6 100644 --- a/examples/mifare-desfire-write-ndef.c +++ b/examples/mifare-desfire-write-ndef.c @@ -17,8 +17,12 @@ * $Id$ */ +#include "config.h" + #include #include +#include +#include #include @@ -45,17 +49,58 @@ const uint8_t ndef_msg[35] = { 0x67 }; +struct { + bool interactive; +} write_options = { + .interactive = true +}; + +void +usage(char *progname) +{ + fprintf (stderr, "usage: %s [-y]\n", progname); + fprintf (stderr, "\nOptions:\n"); + fprintf (stderr, " -y Do not ask for confirmation\n"); + fprintf (stderr, " -k 11223344AABBCCDD Provide another NDEF Tag Application key than the default one\n"); +} + int main(int argc, char *argv[]) { + int ch; int error = EXIT_SUCCESS; nfc_device *device = NULL; MifareTag *tags = NULL; - printf ("NOTE: This application writes a NDEF payload into a Mifare DESFire formatted as NFC Forum Type 4 Tag.\n"); + while ((ch = getopt (argc, argv, "hyk:")) != -1) { + switch (ch) { + case 'h': + usage(argv[0]); + exit (EXIT_SUCCESS); + break; + case 'y': + write_options.interactive = false; + 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); + } + } + // Remaining args, if any, are in argv[optind .. (argc-1)] - if (argc > 1) - errx (EXIT_FAILURE, "usage: %s", argv[0]); + printf ("NOTE: This application writes a NDEF payload into a Mifare DESFire formatted as NFC Forum Type 4 Tag.\n"); nfc_connstring devices[8]; size_t device_count; @@ -88,9 +133,15 @@ main(int argc, char *argv[]) char *tag_uid = freefare_get_tag_uid (tags[i]); char buffer[BUFSIZ]; - printf ("Found %s with UID %s. Write NDEF [yN] ", freefare_get_tag_friendly_name (tags[i]), tag_uid); - fgets (buffer, BUFSIZ, stdin); - bool write_ndef = ((buffer[0] == 'y') || (buffer[0] == 'Y')); + printf ("Found %s with UID %s. ", freefare_get_tag_friendly_name (tags[i]), tag_uid); + bool write_ndef = true; + if (write_options.interactive) { + printf ("Write NDEF [yN] "); + fgets (buffer, BUFSIZ, stdin); + write_ndef = ((buffer[0] == 'y') || (buffer[0] == 'Y')); + } else { + printf ("\n"); + } if (write_ndef) { int res; @@ -109,7 +160,7 @@ main(int argc, char *argv[]) MifareDESFireAID aid = mifare_desfire_aid_new(0xEEEE10); res = mifare_desfire_select_application(tags[i], aid); if (res < 0) - errx (EXIT_FAILURE, "Application selection failed. Try mifare-desfire-format-ndef before running %s.", argv[0]); + errx (EXIT_FAILURE, "Application selection failed. Try mifare-desfire-create-ndef before running %s.", argv[0]); free (aid); // Authentication with NDEF Tag Application master key (Authentication with key 0)