More things

This commit is contained in:
Lynn Ochs 2022-11-22 17:34:27 +01:00
parent 3499cde3f8
commit e39aa4abb0

View File

@ -10,9 +10,10 @@
#define AID 0x22eaa0 #define AID 0x22eaa0
#define RANDOM_UID_SIZE 8 #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, MifareDESFireKey *master_key_aes, char const *name) {
uint8_t const key_data_null[8] = {0, 0, 0, 0, 0, 0, 0, 0}; uint8_t const key_data_null[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
*null_key = mifare_desfire_des_key_new_with_version(key_data_null); *null_key = mifare_desfire_des_key_new_with_version(key_data_null);
*null_key_aes = mifare_desfire_aes_key_new_with_version(key_data_null, 0);
uint8_t key_data_real[16] = {0}; uint8_t key_data_real[16] = {0};
{ {
@ -25,7 +26,8 @@ void init_keys(MifareDESFireKey *null_key, MifareDESFireKey *master_key, char co
} }
fclose(f); fclose(f);
} }
*master_key = mifare_desfire_aes_key_new(key_data_real); *master_key = mifare_desfire_des_key_new_with_version(key_data_real);
*master_key_aes = mifare_desfire_aes_key_new_with_version(key_data_real, 0);
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {
@ -34,11 +36,11 @@ int main(int argc, char **argv) {
nfc_context *context = NULL; nfc_context *context = NULL;
nfc_device *device = NULL; nfc_device *device = NULL;
FreefareTag *tags = NULL; FreefareTag *tags = NULL;
MifareDESFireKey key_null, key_master; MifareDESFireKey key_null, key_null_aes, key_master, key_master_aes;
MifareDESFireAID aid = mifare_desfire_aid_new(AID); MifareDESFireAID aid = mifare_desfire_aid_new(AID);
error = init(&context, &device); error = init(&context, &device);
init_keys(&key_null, &key_master, (argc > 1) ? argv[1] : "key"); init_keys(&key_null, &key_null_aes, &key_master, &key_master_aes, (argc > 1) ? argv[1] : "key");
FILE *out = argc > 2 ? fopen(argv[2], "w") : stdout; FILE *out = argc > 2 ? fopen(argv[2], "w") : stdout;
@ -64,13 +66,13 @@ int main(int argc, char **argv) {
goto next_tag; 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 // 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_aes);
if (res >= 0) { if (res >= 0) {
warnx("Old tag detected: %s", tag_uid); warnx("Old tag detected: %s", tag_uid);
res = res =
mifare_desfire_change_key(tag, 0, key_null, key_master); mifare_desfire_change_key(tag, 0, key_null, key_master_aes);
if (res < 0) { if (res < 0) {
warnx("Could not change key: %d", res); warnx("Could not change key: %d", res);
goto next_tag; goto next_tag;
@ -79,56 +81,73 @@ int main(int argc, char **argv) {
// Card needs to have default null key as master // Card needs to have default null key as master
res = mifare_desfire_authenticate(tag, 0, key_null); res = mifare_desfire_authenticate(tag, 0, key_null);
if (res < 0) { if (res < 0 && (res = mifare_desfire_authenticate_aes(tag, 0, key_null_aes) < 0)) {
warnx("Could not authenticate: %d", res); warnx("Could not authenticate: %d", res);
goto next_tag; goto next_tag;
} } else { warnx("Authenticated using null key: %d", res); }
// create application // create application
res = mifare_desfire_create_application_aes( res = mifare_desfire_create_application_aes(
tag, aid, MDAPP_SETTINGS(0, 1, 0, 0, 1), 1); tag, aid, MDAPP_SETTINGS(0, 1, 1, 1, 1), 1);
if (res < 0) { if (res < 0) {
warnx("Could not create application: %d", res); warnx("Could not create application: %d", res);
goto next_tag; goto next_tag;
} } else { warnx("Created application: %d", res); }
}
// Select it // Select application again
res = mifare_desfire_select_application(tag, aid); res = mifare_desfire_select_application(tag, aid);
if (res < 0) { if (res < 0) {
warnx("Application not selectable after creation: %d", res); warnx("Application not selectable: %d", res);
goto next_tag; goto next_tag;
} } else { warnx("Selected application: %d", res); }
// Key should be null // Key might still be null
res = mifare_desfire_authenticate_aes(tag, 0, key_null); res = mifare_desfire_authenticate_aes(tag, 0, key_null_aes);
if (res < 0) { if (res >= 0) {
warnx( warnx(
"Could not authenticate new application with default " "Application is still using the default key");
"key: %d",
res);
goto next_tag;
}
// Update to our key // Update to our key
res = mifare_desfire_change_key(tag, 0, key_master, key_null); res = mifare_desfire_change_key(tag, 0, key_master_aes, key_null_aes);
if (res < 0) { if (res < 0) {
warnx( warnx(
"Could not change key of new application to master " "Could not change key of new application to master "
"key: %d", "key: %d",
res); res);
goto next_tag; goto next_tag;
} } else { warnx("Updated key"); }
} else { warnx("Could not authenticate using null key: %d", res); }
// Authenticate with new key // Authenticate with new key
res = mifare_desfire_authenticate_aes(tag, 0, key_master); res = mifare_desfire_authenticate(tag, 0, key_master_aes);
if (res < 0) { if (res < 0) {
warnx( warnx(
"Could not authenticate new application with our key: " "Could not authenticate new application with our key: "
"%d", "%d",
res); res);
goto next_tag; goto next_tag;
} }
int found = 0;
size_t count = 0;
uint8_t key_data[RANDOM_UID_SIZE];
uint8_t *files;
res = mifare_desfire_get_file_ids(tag, &files, &count);
if (res < 0) {
warnx("Could not list files: %d", res);
goto next_tag;
}
warnx("%d files", count);
for (int j = 0; j < count; j++) {
warnx("File: %d", key_data[j]);
if (key_data[j] == 0) {
found = 1;
}
}
free(files);
if (!found) {
// Create file // Create file
res = mifare_desfire_create_std_data_file( res = mifare_desfire_create_std_data_file(
tag, 0, 0, MDAR(0, 0, 0, 0), RANDOM_UID_SIZE); tag, 0, 0, MDAR(0, 0, 0, 0), RANDOM_UID_SIZE);
@ -163,13 +182,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 // Grab UID
uint8_t data[RANDOM_UID_SIZE]; uint8_t data[RANDOM_UID_SIZE];
res = mifare_desfire_read_data(tag, 0, 0, RANDOM_UID_SIZE, &data[0]); res = mifare_desfire_read_data(tag, 0, 0, RANDOM_UID_SIZE, &data[0]);
@ -186,15 +198,15 @@ int main(int argc, char **argv) {
fprintf(out, "\n"); fprintf(out, "\n");
fflush(out); fflush(out);
while (freefare_selected_tag_is_present(device))
;
next_tag: next_tag:
free(tag_uid); free(tag_uid);
mifare_desfire_disconnect(tag); mifare_desfire_disconnect(tag);
} }
while (freefare_selected_tag_is_present(device))
;
freefare_free_tags(tags); freefare_free_tags(tags);
fflush(stdout); fflush(stdout);
fflush(stderr); fflush(stderr);