diff --git a/poll_desfire.c b/poll_desfire.c index 705c375..69461d1 100644 --- a/poll_desfire.c +++ b/poll_desfire.c @@ -10,9 +10,10 @@ #define AID 0x22eaa0 #define RANDOM_UID_SIZE 8 -void init_keys(MifareDESFireKey *null_key, MifareDESFireKey *master_key, char const *name) { +void init_keys(MifareDESFireKey *null_key, MifareDESFireKey *null_key_aes, MifareDESFireKey *master_key, char const *name) { uint8_t const key_data_null[8] = {0, 0, 0, 0, 0, 0, 0, 0}; *null_key = mifare_desfire_des_key_new_with_version(key_data_null); + *null_key_aes = mifare_desfire_aes_key_new(key_data_null); uint8_t key_data_real[16] = {0}; { @@ -34,11 +35,11 @@ int main(int argc, char **argv) { nfc_context *context = NULL; nfc_device *device = NULL; FreefareTag *tags = NULL; - MifareDESFireKey key_null, key_master; + MifareDESFireKey key_null, key_null_aes, key_master; MifareDESFireAID aid = mifare_desfire_aid_new(AID); error = init(&context, &device); - init_keys(&key_null, &key_master, (argc > 1) ? argv[1] : "key"); + init_keys(&key_null, &key_null_aes, &key_master, (argc > 1) ? argv[1] : "key"); FILE *out = argc > 2 ? fopen(argv[2], "w") : stdout; @@ -64,9 +65,9 @@ int main(int argc, char **argv) { goto next_tag; } - if (mifare_desfire_select_application(tag, aid) != 0) { + if (mifare_desfire_select_application(tag, aid) < 0) { // Check for old card with our key as master key - res = mifare_desfire_authenticate(tag, 0, key_master); + res = mifare_desfire_authenticate_aes(tag, 0, key_master); if (res >= 0) { warnx("Old tag detected: %s", tag_uid); res = @@ -79,10 +80,10 @@ int main(int argc, char **argv) { // Card needs to have default null key as master res = mifare_desfire_authenticate(tag, 0, key_null); - if (res < 0) { + if (res < 0 && (res = mifare_desfire_authenticate_aes(tag, 0, key_null) < 0)) { warnx("Could not authenticate: %d", res); goto next_tag; - } + } else { warnx("Authenticated using null key: %d", res); } // create application res = mifare_desfire_create_application_aes( @@ -90,25 +91,22 @@ int main(int argc, char **argv) { if (res < 0) { warnx("Could not create application: %d", res); goto next_tag; - } + } else { warnx("Created application: %d", res); } + } - // Select it - res = mifare_desfire_select_application(tag, aid); - if (res < 0) { - 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; - } + // Select application again + res = mifare_desfire_select_application(tag, aid); + if (res < 0) { + warnx("Application not selectable: %d", res); + goto next_tag; + } else { warnx("Selected application: %d", res); } + // Key might still be null + res = mifare_desfire_authenticate_aes(tag, 0, key_null_aes); + 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 +116,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_tag; + } + 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 +177,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]);