examples/mifare-desfire-ev1*: test version to ensure we've at least an EV1

This commit is contained in:
Philippe Teuwen 2012-05-15 11:50:10 +00:00
parent ce902387aa
commit 5bec80fb07
3 changed files with 68 additions and 38 deletions

View file

@ -115,10 +115,29 @@ main(int argc, char *argv[])
char *tag_uid = freefare_get_tag_uid (tags[i]); char *tag_uid = freefare_get_tag_uid (tags[i]);
char buffer[BUFSIZ]; char buffer[BUFSIZ];
int res;
res = mifare_desfire_connect (tags[i]);
if (res < 0) {
warnx ("Can't connect to Mifare DESFire target.");
error = EXIT_FAILURE;
break;
}
// Make sure we've at least an EV1 version
struct mifare_desfire_version_info info;
res = mifare_desfire_get_version (tags[i], &info);
if (res < 0) {
freefare_perror (tags[i], "mifare_desfire_get_version");
error = 1;
break;
}
if (info.software.version_major < 1) {
warnx ("Found old DESFire, skipping");
continue;
}
printf ("Found %s with UID %s. ", freefare_get_tag_friendly_name (tags[i]), tag_uid); printf ("Found %s with UID %s. ", freefare_get_tag_friendly_name (tags[i]), tag_uid);
bool do_it = true; bool do_it = true;
int res;
if (configure_options.interactive) { if (configure_options.interactive) {
printf ("Change ATS? [yN] "); printf ("Change ATS? [yN] ");
@ -130,13 +149,6 @@ main(int argc, char *argv[])
if (do_it) { if (do_it) {
res = mifare_desfire_connect (tags[i]);
if (res < 0) {
warnx ("Can't connect to Mifare DESFire target.");
error = EXIT_FAILURE;
break;
}
MifareDESFireKey key_picc = mifare_desfire_des_key_new_with_version (key_data_picc); MifareDESFireKey key_picc = mifare_desfire_des_key_new_with_version (key_data_picc);
res = mifare_desfire_authenticate (tags[i], 0, key_picc); res = mifare_desfire_authenticate (tags[i], 0, key_picc);
if (res < 0) { if (res < 0) {
@ -153,10 +165,9 @@ main(int argc, char *argv[])
break; break;
} }
mifare_desfire_disconnect (tags[i]);
} }
mifare_desfire_disconnect (tags[i]);
free (tag_uid); free (tag_uid);
} }

View file

@ -102,9 +102,30 @@ main(int argc, char *argv[])
char *tag_uid = freefare_get_tag_uid (tags[i]); char *tag_uid = freefare_get_tag_uid (tags[i]);
char buffer[BUFSIZ]; char buffer[BUFSIZ];
int res;
res = mifare_desfire_connect (tags[i]);
if (res < 0) {
warnx ("Can't connect to Mifare DESFire target.");
error = EXIT_FAILURE;
break;
}
// Make sure we've at least an EV1 version
struct mifare_desfire_version_info info;
res = mifare_desfire_get_version (tags[i], &info);
if (res < 0) {
freefare_perror (tags[i], "mifare_desfire_get_version");
error = 1;
break;
}
if (info.software.version_major < 1) {
warnx ("Found old DESFire, skipping");
continue;
}
printf ("Found %s with UID %s. ", freefare_get_tag_friendly_name (tags[i]), tag_uid); printf ("Found %s with UID %s. ", freefare_get_tag_friendly_name (tags[i]), tag_uid);
bool do_it = true; bool do_it = true;
int res;
if (configure_options.interactive) { if (configure_options.interactive) {
printf ("Change default key? [yN] "); printf ("Change default key? [yN] ");
@ -116,13 +137,6 @@ main(int argc, char *argv[])
if (do_it) { if (do_it) {
res = mifare_desfire_connect (tags[i]);
if (res < 0) {
warnx ("Can't connect to Mifare DESFire target.");
error = EXIT_FAILURE;
break;
}
MifareDESFireKey default_key = mifare_desfire_des_key_new_with_version (null_key_data); MifareDESFireKey default_key = mifare_desfire_des_key_new_with_version (null_key_data);
res = mifare_desfire_authenticate (tags[i], 0, default_key); res = mifare_desfire_authenticate (tags[i], 0, default_key);
if (res < 0) { if (res < 0) {
@ -224,9 +238,9 @@ main(int argc, char *argv[])
break; break;
} }
mifare_desfire_disconnect (tags[i]);
} }
mifare_desfire_disconnect (tags[i]);
free (tag_uid); free (tag_uid);
} }

View file

@ -112,9 +112,30 @@ main(int argc, char *argv[])
char *tag_uid = freefare_get_tag_uid (tags[i]); char *tag_uid = freefare_get_tag_uid (tags[i]);
char buffer[BUFSIZ]; char buffer[BUFSIZ];
int res;
res = mifare_desfire_connect (tags[i]);
if (res < 0) {
warnx ("Can't connect to Mifare DESFire target.");
error = EXIT_FAILURE;
break;
}
// Make sure we've at least an EV1 version
struct mifare_desfire_version_info info;
res = mifare_desfire_get_version (tags[i], &info);
if (res < 0) {
freefare_perror (tags[i], "mifare_desfire_get_version");
error = 1;
break;
}
if (info.software.version_major < 1) {
warnx ("Found old DESFire, skipping");
continue;
}
printf ("Found %s with UID %s. ", freefare_get_tag_friendly_name (tags[i]), tag_uid); printf ("Found %s with UID %s. ", freefare_get_tag_friendly_name (tags[i]), tag_uid);
bool do_it = true; bool do_it = true;
int res;
size_t tag_uid_len = strlen (tag_uid) / 2; size_t tag_uid_len = strlen (tag_uid) / 2;
switch (tag_uid_len) { switch (tag_uid_len) {
@ -129,13 +150,6 @@ main(int argc, char *argv[])
if (do_it) { if (do_it) {
res = mifare_desfire_connect (tags[i]);
if (res < 0) {
warnx ("Can't connect to Mifare DESFire target.");
error = EXIT_FAILURE;
break;
}
MifareDESFireKey key_picc = mifare_desfire_des_key_new_with_version (key_data_picc); MifareDESFireKey key_picc = mifare_desfire_des_key_new_with_version (key_data_picc);
res = mifare_desfire_authenticate (tags[i], 0, key_picc); res = mifare_desfire_authenticate (tags[i], 0, key_picc);
if (res < 0) { if (res < 0) {
@ -151,19 +165,10 @@ main(int argc, char *argv[])
error = EXIT_FAILURE; error = EXIT_FAILURE;
break; break;
} }
mifare_desfire_disconnect (tags[i]);
} }
break; break;
case 4: // Random UID case 4: // Random UID
{} // Compilation fails if label is directly followed by the declaration rather than a statement
res = mifare_desfire_connect (tags[i]);
if (res < 0) {
warnx ("Can't connect to Mifare DESFire target.");
error = EXIT_FAILURE;
break;
}
MifareDESFireKey key_picc = mifare_desfire_des_key_new_with_version (key_data_picc); MifareDESFireKey key_picc = mifare_desfire_des_key_new_with_version (key_data_picc);
res = mifare_desfire_authenticate (tags[i], 0, key_picc); res = mifare_desfire_authenticate (tags[i], 0, key_picc);
if (res < 0) { if (res < 0) {
@ -184,7 +189,6 @@ main(int argc, char *argv[])
printf ("Old card UID: %s\n", old_tag_uid); printf ("Old card UID: %s\n", old_tag_uid);
free (old_tag_uid); free (old_tag_uid);
mifare_desfire_disconnect (tags[i]);
break; break;
default: // Should not happen default: // Should not happen
warnx ("Unsupported UID length %d.", (int) tag_uid_len); warnx ("Unsupported UID length %d.", (int) tag_uid_len);
@ -192,6 +196,7 @@ main(int argc, char *argv[])
break; break;
} }
mifare_desfire_disconnect (tags[i]);
free (tag_uid); free (tag_uid);
} }