From a59ea0bd3d973a74ea45de23bfa21973854024cf Mon Sep 17 00:00:00 2001 From: Valentin Ochs Date: Tue, 22 Nov 2022 17:34:27 +0100 Subject: [PATCH] More things --- poll_desfire.c | 56 ++++++++++++++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 25 deletions(-) diff --git a/poll_desfire.c b/poll_desfire.c index 705c375..58f94f0 100644 --- a/poll_desfire.c +++ b/poll_desfire.c @@ -98,17 +98,14 @@ int main(int argc, char **argv) { warnx("Application not selectable after creation: %d", res); goto next_tag; } + } - // Key should be null - res = mifare_desfire_authenticate_aes(tag, 0, key_null); - if (res < 0) { - warnx( - "Could not authenticate new application with default " - "key: %d", - res); - goto next_tag; - } - + // Key might still be null + res = mifare_desfire_authenticate_aes(tag, 0, key_null); + if (res >= 0) { + warnx( + "Application is still using the default key"); + // Update to our key res = mifare_desfire_change_key(tag, 0, key_master, key_null); if (res < 0) { @@ -118,17 +115,33 @@ int main(int argc, char **argv) { res); goto next_tag; } + } - // Authenticate with new key - res = mifare_desfire_authenticate_aes(tag, 0, key_master); - if (res < 0) { - warnx( - "Could not authenticate new application with our key: " - "%d", - res); - goto next_tag; + // Authenticate with new key + res = mifare_desfire_authenticate_aes(tag, 0, key_master); + if (res < 0) { + warnx( + "Could not authenticate new application with our key: " + "%d", + res); + goto next_tag; + } + + int found = 0; + size_t count = 0; + uint8_t key_data[RANDOM_UID_SIZE]; + res = mifare_desfire_get_file_ids(tag, &key_data[0], &count); + if (res < 0) { + warnx("Could not list files: %d", res); + goto next_file; + } + for (int j = 0; j < count; j++) { + if (key_data[j] == 0) { + found = 1; } + } + if (!found) { // Create file res = mifare_desfire_create_std_data_file( tag, 0, 0, MDAR(0, 0, 0, 0), RANDOM_UID_SIZE); @@ -163,13 +176,6 @@ int main(int argc, char **argv) { } } - // Authenticate with our key - res = mifare_desfire_authenticate_aes(tag, 0, key_master); - if (res < 0) { - warnx("Authentication on application failed: %d", res); - goto next_tag; - } - // Grab UID uint8_t data[RANDOM_UID_SIZE]; res = mifare_desfire_read_data(tag, 0, 0, RANDOM_UID_SIZE, &data[0]);