Reindent (1/2).

After trying to tweak gindent so that it does not mess-up my ASCII art, give
and read the help of Vim to setup 'cinoptions' according to my needs
(cino=t0(0).  This changeset reindent some bits that where not indented as they
should.  A future commit will reindent the switch/case according to my (bad)
taste now that I have setup cindent correctly on my system (cino=t0(0:0).
This commit is contained in:
Romain Tartiere 2010-09-03 18:01:02 +00:00
parent 036d2956f4
commit af061a3c1a
16 changed files with 632 additions and 615 deletions

View file

@ -167,7 +167,7 @@ main(int argc, char *argv[])
nfc_list_devices (devices, 8, &device_count); nfc_list_devices (devices, 8, &device_count);
if (!device_count) if (!device_count)
errx (EXIT_FAILURE, "No NFC device found."); errx (EXIT_FAILURE, "No NFC device found.");
for (size_t d = 0; d < device_count; d++) { for (size_t d = 0; d < device_count; d++) {
device = nfc_connect (&(devices[d])); device = nfc_connect (&(devices[d]));
@ -177,72 +177,72 @@ main(int argc, char *argv[])
continue; continue;
} }
tags = freefare_get_tags (device); tags = freefare_get_tags (device);
if (!tags) { if (!tags) {
nfc_disconnect (device); nfc_disconnect (device);
errx (EXIT_FAILURE, "Error listing Mifare Classic tag."); errx (EXIT_FAILURE, "Error listing Mifare Classic tag.");
}
for (int i = 0; (!error) && tags[i]; i++) {
switch (freefare_get_tag_type (tags[i])) {
case CLASSIC_1K:
case CLASSIC_4K:
break;
default:
continue;
} }
char *tag_uid = freefare_get_tag_uid (tags[i]); for (int i = 0; (!error) && tags[i]; i++) {
char buffer[BUFSIZ]; switch (freefare_get_tag_type (tags[i])) {
printf ("Found %s with UID %s. ", freefare_get_tag_friendly_name (tags[i]), tag_uid);
bool format = true;
if (format_options.interactive) {
printf ("Format [yN] ");
fgets (buffer, BUFSIZ, stdin);
format = ((buffer[0] == 'y') || (buffer[0] == 'Y'));
} else {
printf ("\n");
}
if (format) {
enum mifare_tag_type tt = freefare_get_tag_type (tags[i]);
at_block = 0;
if (format_options.fast) {
printf (START_FORMAT_N, (tt == CLASSIC_1K) ? 1 : 2);
if (!try_format_sector (tags[i], 0x00))
break;
if (tt == CLASSIC_4K)
if (!try_format_sector (tags[i], 0x10))
break;
printf (DONE_FORMAT);
continue;
}
switch (tt) {
case CLASSIC_1K: case CLASSIC_1K:
mod_block = 4;
if (!format_mifare_classic_1k (tags[i]))
error = 1;
break;
case CLASSIC_4K: case CLASSIC_4K:
mod_block = 10;
if (!format_mifare_classic_4k (tags[i]))
error = 1;
break; break;
default: default:
/* Keep compiler quiet */ continue;
break;
} }
char *tag_uid = freefare_get_tag_uid (tags[i]);
char buffer[BUFSIZ];
printf ("Found %s with UID %s. ", freefare_get_tag_friendly_name (tags[i]), tag_uid);
bool format = true;
if (format_options.interactive) {
printf ("Format [yN] ");
fgets (buffer, BUFSIZ, stdin);
format = ((buffer[0] == 'y') || (buffer[0] == 'Y'));
} else {
printf ("\n");
}
if (format) {
enum mifare_tag_type tt = freefare_get_tag_type (tags[i]);
at_block = 0;
if (format_options.fast) {
printf (START_FORMAT_N, (tt == CLASSIC_1K) ? 1 : 2);
if (!try_format_sector (tags[i], 0x00))
break;
if (tt == CLASSIC_4K)
if (!try_format_sector (tags[i], 0x10))
break;
printf (DONE_FORMAT);
continue;
}
switch (tt) {
case CLASSIC_1K:
mod_block = 4;
if (!format_mifare_classic_1k (tags[i]))
error = 1;
break;
case CLASSIC_4K:
mod_block = 10;
if (!format_mifare_classic_4k (tags[i]))
error = 1;
break;
default:
/* Keep compiler quiet */
break;
}
}
free (tag_uid);
} }
free (tag_uid); freefare_free_tags (tags);
} nfc_disconnect (device);
freefare_free_tags (tags);
nfc_disconnect (device);
} }
exit (error); exit (error);

View file

@ -132,7 +132,7 @@ main(int argc, char *argv[])
nfc_list_devices (devices, 8, &device_count); nfc_list_devices (devices, 8, &device_count);
if (!device_count) if (!device_count)
errx (EXIT_FAILURE, "No NFC device found."); errx (EXIT_FAILURE, "No NFC device found.");
for (size_t d = 0; d < device_count; d++) { for (size_t d = 0; d < device_count; d++) {
device = nfc_connect (&(devices[d])); device = nfc_connect (&(devices[d]));
@ -142,179 +142,179 @@ main(int argc, char *argv[])
continue; continue;
} }
tags = freefare_get_tags (device); tags = freefare_get_tags (device);
if (!tags) { if (!tags) {
nfc_disconnect (device); nfc_disconnect (device);
errx (EXIT_FAILURE, "Error listing MIFARE classic tag."); errx (EXIT_FAILURE, "Error listing MIFARE classic tag.");
}
for (int i = 0; (!error) && tags[i]; i++) {
switch (freefare_get_tag_type (tags[i])) {
case CLASSIC_1K:
case CLASSIC_4K:
break;
default:
continue;
} }
char *tag_uid = freefare_get_tag_uid (tags[i]); for (int i = 0; (!error) && tags[i]; 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'));
if (write_ndef) {
switch (freefare_get_tag_type (tags[i])) { switch (freefare_get_tag_type (tags[i])) {
case CLASSIC_4K:
if (!search_sector_key (tags[i], 0x10, &(card_write_keys[0x10].key), &(card_write_keys[0x10].type))) {
error = 1;
goto error;
}
/* fallthrough */
case CLASSIC_1K: case CLASSIC_1K:
if (!search_sector_key (tags[i], 0x00, &(card_write_keys[0x00].key), &(card_write_keys[0x00].type))) { case CLASSIC_4K:
error = 1;
goto error;
}
break; break;
default: default:
/* Keep compiler quiet */ continue;
break;
} }
if (!error) { char *tag_uid = freefare_get_tag_uid (tags[i]);
/* Ensure the auth key is always a B one. If not, change it! */ 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'));
if (write_ndef) {
switch (freefare_get_tag_type (tags[i])) { switch (freefare_get_tag_type (tags[i])) {
case CLASSIC_4K: case CLASSIC_4K:
if (card_write_keys[0x10].type != MFC_KEY_B) { if (!search_sector_key (tags[i], 0x10, &(card_write_keys[0x10].key), &(card_write_keys[0x10].type))) {
if( 0 != fix_mad_trailer_block( tags[i], 0x10, card_write_keys[0x10].key, card_write_keys[0x10].type)) {
error = 1;
goto error;
}
memcpy (&(card_write_keys[0x10].key), &default_keyb, sizeof (MifareClassicKey));
card_write_keys[0x10].type = MFC_KEY_B;
}
/* fallthrough */
case CLASSIC_1K:
if (card_write_keys[0x00].type != MFC_KEY_B) {
if( 0 != fix_mad_trailer_block( tags[i], 0x00, card_write_keys[0x00].key, card_write_keys[0x00].type)) {
error = 1;
goto error;
}
memcpy (&(card_write_keys[0x00].key), &default_keyb, sizeof (MifareClassicKey));
card_write_keys[0x00].type = MFC_KEY_B;
}
break;
default:
/* Keep compiler quiet */
break;
}
}
size_t encoded_size;
uint8_t *tlv_data = tlv_encode (3, ndef_msg, sizeof (ndef_msg), &encoded_size);
/*
* At his point, we should have collected all information needed to
* succeed.
*/
// If the card already has a MAD, load it.
if ((mad = mad_read (tags[i]))) {
// If our application already exists, erase it.
mifare_application_free (mad, mad_nfcforum_aid);
} else {
// Create a MAD and mark unaccessible sectors in the card
if (!(mad = mad_new ((freefare_get_tag_type (tags[i]) == CLASSIC_4K) ? 2 : 1))) {
perror ("mad_new");
error = 1;
goto error;
}
MifareClassicSectorNumber max_s;
switch (freefare_get_tag_type (tags[i])) {
case CLASSIC_1K:
max_s = 15;
break;
case CLASSIC_4K:
max_s = 39;
break;
default:
/* Keep compiler quiet */
break;
}
MifareClassicKey transport_key = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
// Mark unusable sectors as so
for (size_t s = max_s; s; s--) {
if (s == 0x10) continue;
if (!search_sector_key (tags[i], s, &(card_write_keys[s].key), &(card_write_keys[s].type))) {
mad_set_aid (mad, s, mad_defect_aid);
} else if ((memcmp (card_write_keys[s].key, transport_key, sizeof (transport_key)) != 0) &&
(card_write_keys[s].type != MFC_KEY_A)) {
// Revert to transport configuration
if (mifare_classic_format_sector (tags[i], s) < 0) {
perror ("mifare_classic_format_sector");
error = 1; error = 1;
goto error; goto error;
} }
/* fallthrough */
case CLASSIC_1K:
if (!search_sector_key (tags[i], 0x00, &(card_write_keys[0x00].key), &(card_write_keys[0x00].type))) {
error = 1;
goto error;
}
break;
default:
/* Keep compiler quiet */
break;
}
if (!error) {
/* Ensure the auth key is always a B one. If not, change it! */
switch (freefare_get_tag_type (tags[i])) {
case CLASSIC_4K:
if (card_write_keys[0x10].type != MFC_KEY_B) {
if( 0 != fix_mad_trailer_block( tags[i], 0x10, card_write_keys[0x10].key, card_write_keys[0x10].type)) {
error = 1;
goto error;
}
memcpy (&(card_write_keys[0x10].key), &default_keyb, sizeof (MifareClassicKey));
card_write_keys[0x10].type = MFC_KEY_B;
}
/* fallthrough */
case CLASSIC_1K:
if (card_write_keys[0x00].type != MFC_KEY_B) {
if( 0 != fix_mad_trailer_block( tags[i], 0x00, card_write_keys[0x00].key, card_write_keys[0x00].type)) {
error = 1;
goto error;
}
memcpy (&(card_write_keys[0x00].key), &default_keyb, sizeof (MifareClassicKey));
card_write_keys[0x00].type = MFC_KEY_B;
}
break;
default:
/* Keep compiler quiet */
break;
} }
} }
}
MifareClassicSectorNumber *sectors = mifare_application_alloc (mad, mad_nfcforum_aid, encoded_size); size_t encoded_size;
if (!sectors) { uint8_t *tlv_data = tlv_encode (3, ndef_msg, sizeof (ndef_msg), &encoded_size);
perror ("mifare_application_alloc");
error = EXIT_FAILURE;
goto error;
}
if (mad_write (tags[i], mad, card_write_keys[0x00].key, card_write_keys[0x10].key) < 0) { /*
perror ("mad_write"); * At his point, we should have collected all information needed to
error = EXIT_FAILURE; * succeed.
goto error; */
}
int s = 0; // If the card already has a MAD, load it.
while (sectors[s]) { if ((mad = mad_read (tags[i]))) {
MifareClassicBlockNumber block = mifare_classic_sector_last_block (sectors[s]); // If our application already exists, erase it.
MifareClassicBlock block_data; mifare_application_free (mad, mad_nfcforum_aid);
mifare_classic_trailer_block (&block_data, mifare_classic_nfcforum_public_key_a, 0x0, 0x0, 0x0, 0x6, 0x40, default_keyb); } else {
if (mifare_classic_authenticate (tags[i], block, card_write_keys[sectors[s]].key, card_write_keys[sectors[s]].type) < 0) {
perror ("mifare_classic_authenticate"); // Create a MAD and mark unaccessible sectors in the card
if (!(mad = mad_new ((freefare_get_tag_type (tags[i]) == CLASSIC_4K) ? 2 : 1))) {
perror ("mad_new");
error = 1;
goto error;
}
MifareClassicSectorNumber max_s;
switch (freefare_get_tag_type (tags[i])) {
case CLASSIC_1K:
max_s = 15;
break;
case CLASSIC_4K:
max_s = 39;
break;
default:
/* Keep compiler quiet */
break;
}
MifareClassicKey transport_key = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
// Mark unusable sectors as so
for (size_t s = max_s; s; s--) {
if (s == 0x10) continue;
if (!search_sector_key (tags[i], s, &(card_write_keys[s].key), &(card_write_keys[s].type))) {
mad_set_aid (mad, s, mad_defect_aid);
} else if ((memcmp (card_write_keys[s].key, transport_key, sizeof (transport_key)) != 0) &&
(card_write_keys[s].type != MFC_KEY_A)) {
// Revert to transport configuration
if (mifare_classic_format_sector (tags[i], s) < 0) {
perror ("mifare_classic_format_sector");
error = 1;
goto error;
}
}
}
}
MifareClassicSectorNumber *sectors = mifare_application_alloc (mad, mad_nfcforum_aid, encoded_size);
if (!sectors) {
perror ("mifare_application_alloc");
error = EXIT_FAILURE; error = EXIT_FAILURE;
goto error; goto error;
} }
if (mifare_classic_write (tags[i], block, block_data) < 0) {
perror ("mifare_classic_write"); if (mad_write (tags[i], mad, card_write_keys[0x00].key, card_write_keys[0x10].key) < 0) {
perror ("mad_write");
error = EXIT_FAILURE; error = EXIT_FAILURE;
goto error; goto error;
} }
s++;
int s = 0;
while (sectors[s]) {
MifareClassicBlockNumber block = mifare_classic_sector_last_block (sectors[s]);
MifareClassicBlock block_data;
mifare_classic_trailer_block (&block_data, mifare_classic_nfcforum_public_key_a, 0x0, 0x0, 0x0, 0x6, 0x40, default_keyb);
if (mifare_classic_authenticate (tags[i], block, card_write_keys[sectors[s]].key, card_write_keys[sectors[s]].type) < 0) {
perror ("mifare_classic_authenticate");
error = EXIT_FAILURE;
goto error;
}
if (mifare_classic_write (tags[i], block, block_data) < 0) {
perror ("mifare_classic_write");
error = EXIT_FAILURE;
goto error;
}
s++;
}
if ((ssize_t) encoded_size != mifare_application_write (tags[i], mad, mad_nfcforum_aid, tlv_data, encoded_size, default_keyb, MCAB_WRITE_KEYB)) {
perror ("mifare_application_write");
error = EXIT_FAILURE;
goto error;
}
free (sectors);
free (tlv_data);
free (mad);
} }
if ((ssize_t) encoded_size != mifare_application_write (tags[i], mad, mad_nfcforum_aid, tlv_data, encoded_size, default_keyb, MCAB_WRITE_KEYB)) {
perror ("mifare_application_write");
error = EXIT_FAILURE;
goto error;
}
free (sectors);
free (tlv_data);
free (mad);
}
error: error:
free (tag_uid); free (tag_uid);
} }
freefare_free_tags (tags); freefare_free_tags (tags);
nfc_disconnect (device); nfc_disconnect (device);
} }
free (card_write_keys); free (card_write_keys);

View file

@ -44,7 +44,7 @@ main(int argc, char *argv[])
nfc_list_devices (devices, 8, &device_count); nfc_list_devices (devices, 8, &device_count);
if (!device_count) if (!device_count)
errx (EXIT_FAILURE, "No NFC device found."); errx (EXIT_FAILURE, "No NFC device found.");
for (size_t d = 0; d < device_count; d++) { for (size_t d = 0; d < device_count; d++) {
device = nfc_connect (&(devices[d])); device = nfc_connect (&(devices[d]));
@ -54,81 +54,81 @@ main(int argc, char *argv[])
continue; continue;
} }
tags = freefare_get_tags (device); tags = freefare_get_tags (device);
if (!tags) { if (!tags) {
nfc_disconnect (device); nfc_disconnect (device);
errx (EXIT_FAILURE, "Error listing tags."); errx (EXIT_FAILURE, "Error listing tags.");
}
for (int i = 0; (!error) && tags[i]; i++) {
if (DESFIRE != freefare_get_tag_type (tags[i]))
continue;
int res;
char *tag_uid = freefare_get_tag_uid (tags[i]);
res = mifare_desfire_connect (tags[i]);
if (res < 0) {
warnx ("Can't connect to Mifare DESFire target.");
error = EXIT_FAILURE;
break;
} }
for (int i = 0; (!error) && tags[i]; i++) {
if (DESFIRE != freefare_get_tag_type (tags[i]))
continue;
MifareDESFireKey key = mifare_desfire_des_key_new_with_version (key_data_null); int res;
res = mifare_desfire_authenticate (tags[i], 0, key); char *tag_uid = freefare_get_tag_uid (tags[i]);
if (res < 0)
errx (EXIT_FAILURE, "Authentication on master application failed");
MadAid mad_aid = { 0x12, 0x34 }; res = mifare_desfire_connect (tags[i]);
MifareDESFireAID aid = mifare_desfire_aid_new_with_mad_aid (mad_aid, 0x5); if (res < 0) {
res = mifare_desfire_create_application (tags[i], aid, 0xFF, 0x1); warnx ("Can't connect to Mifare DESFire target.");
if (res < 0) error = EXIT_FAILURE;
errx (EXIT_FAILURE, "Application creation failed"); break;
}
res = mifare_desfire_select_application (tags[i], aid);
if (res < 0)
errx (EXIT_FAILURE, "Application selection failed");
res = mifare_desfire_authenticate (tags[i], 0, key); MifareDESFireKey key = mifare_desfire_des_key_new_with_version (key_data_null);
if (res < 0) res = mifare_desfire_authenticate (tags[i], 0, key);
errx (EXIT_FAILURE, "Authentication on application failed"); if (res < 0)
errx (EXIT_FAILURE, "Authentication on master application failed");
res = mifare_desfire_create_std_data_file (tags[i], 1, MDCM_FULLDES, 0x0000, 20); MadAid mad_aid = { 0x12, 0x34 };
if (res < 0) MifareDESFireAID aid = mifare_desfire_aid_new_with_mad_aid (mad_aid, 0x5);
errx (EXIT_FAILURE, "File creation failed"); res = mifare_desfire_create_application (tags[i], aid, 0xFF, 0x1);
if (res < 0)
errx (EXIT_FAILURE, "Application creation failed");
char *s= "Hello World"; res = mifare_desfire_select_application (tags[i], aid);
res = mifare_desfire_write_data (tags[i], 1, 0, strlen (s), s); if (res < 0)
if (res < 0) errx (EXIT_FAILURE, "Application selection failed");
errx (EXIT_FAILURE, "File write failed");
char buffer[20]; res = mifare_desfire_authenticate (tags[i], 0, key);
res = mifare_desfire_read_data (tags[i], 1, 0, 0, buffer); if (res < 0)
if (res < 0) errx (EXIT_FAILURE, "Authentication on application failed");
errx (EXIT_FAILURE, "File read failed");
res = mifare_desfire_select_application (tags[i], NULL); res = mifare_desfire_create_std_data_file (tags[i], 1, MDCM_FULLDES, 0x0000, 20);
if (res < 0) if (res < 0)
errx (EXIT_FAILURE, "Master application selection failed"); errx (EXIT_FAILURE, "File creation failed");
res = mifare_desfire_authenticate (tags[i], 0, key); char *s= "Hello World";
if (res < 0) res = mifare_desfire_write_data (tags[i], 1, 0, strlen (s), s);
errx (EXIT_FAILURE, "Authentication on master application failed"); if (res < 0)
errx (EXIT_FAILURE, "File write failed");
res = mifare_desfire_format_picc (tags[i]); char buffer[20];
if (res < 0) res = mifare_desfire_read_data (tags[i], 1, 0, 0, buffer);
errx (EXIT_FAILURE, "PICC format failed"); if (res < 0)
errx (EXIT_FAILURE, "File read failed");
mifare_desfire_key_free (key); res = mifare_desfire_select_application (tags[i], NULL);
free (tag_uid); if (res < 0)
free (aid); errx (EXIT_FAILURE, "Master application selection failed");
mifare_desfire_disconnect (tags[i]); res = mifare_desfire_authenticate (tags[i], 0, key);
} if (res < 0)
errx (EXIT_FAILURE, "Authentication on master application failed");
freefare_free_tags (tags); res = mifare_desfire_format_picc (tags[i]);
nfc_disconnect (device); if (res < 0)
errx (EXIT_FAILURE, "PICC format failed");
mifare_desfire_key_free (key);
free (tag_uid);
free (aid);
mifare_desfire_disconnect (tags[i]);
}
freefare_free_tags (tags);
nfc_disconnect (device);
} }
exit (error); exit (error);

View file

@ -75,7 +75,7 @@ main(int argc, char *argv[])
nfc_list_devices (devices, 8, &device_count); nfc_list_devices (devices, 8, &device_count);
if (!device_count) if (!device_count)
errx (EXIT_FAILURE, "No NFC device found."); errx (EXIT_FAILURE, "No NFC device found.");
for (size_t d = 0; d < device_count; d++) { for (size_t d = 0; d < device_count; d++) {
device = nfc_connect (&(devices[d])); device = nfc_connect (&(devices[d]));
@ -85,62 +85,62 @@ main(int argc, char *argv[])
continue; continue;
} }
tags = freefare_get_tags (device); tags = freefare_get_tags (device);
if (!tags) { if (!tags) {
nfc_disconnect (device);
errx (EXIT_FAILURE, "Error listing Mifare DESFire tags.");
}
for (int i = 0; (!error) && tags[i]; i++) {
if (DESFIRE != freefare_get_tag_type (tags[i]))
continue;
char *tag_uid = freefare_get_tag_uid (tags[i]);
char buffer[BUFSIZ];
printf ("Found %s with UID %s. ", freefare_get_tag_friendly_name (tags[i]), tag_uid);
bool format = true;
if (format_options.interactive) {
printf ("Format [yN] ");
fgets (buffer, BUFSIZ, stdin);
format = ((buffer[0] == 'y') || (buffer[0] == 'Y'));
} else {
printf ("\n");
}
if (format) {
int res;
MifareDESFireKey default_key = mifare_desfire_des_key_new_with_version (null_key_data);
res = mifare_desfire_connect (tags[i]);
if (res < 0) {
warnx ("Can't connect to Mifare DESFire target.");
error = EXIT_FAILURE;
break;
}
res = mifare_desfire_authenticate (tags[i], 0, default_key);
if (res < 0) {
warnx ("Can't authenticate on Mifare DESFire target.");
error = EXIT_FAILURE;
break;
}
res = mifare_desfire_format_picc (tags[i]);
if (res < 0) {
warn ("Can't format PICC.");
error = EXIT_FAILURE;
break;
}
mifare_desfire_disconnect (tags[i]);
}
free (tag_uid);
}
freefare_free_tags (tags);
nfc_disconnect (device); nfc_disconnect (device);
errx (EXIT_FAILURE, "Error listing Mifare DESFire tags.");
}
for (int i = 0; (!error) && tags[i]; i++) {
if (DESFIRE != freefare_get_tag_type (tags[i]))
continue;
char *tag_uid = freefare_get_tag_uid (tags[i]);
char buffer[BUFSIZ];
printf ("Found %s with UID %s. ", freefare_get_tag_friendly_name (tags[i]), tag_uid);
bool format = true;
if (format_options.interactive) {
printf ("Format [yN] ");
fgets (buffer, BUFSIZ, stdin);
format = ((buffer[0] == 'y') || (buffer[0] == 'Y'));
} else {
printf ("\n");
}
if (format) {
int res;
MifareDESFireKey default_key = mifare_desfire_des_key_new_with_version (null_key_data);
res = mifare_desfire_connect (tags[i]);
if (res < 0) {
warnx ("Can't connect to Mifare DESFire target.");
error = EXIT_FAILURE;
break;
}
res = mifare_desfire_authenticate (tags[i], 0, default_key);
if (res < 0) {
warnx ("Can't authenticate on Mifare DESFire target.");
error = EXIT_FAILURE;
break;
}
res = mifare_desfire_format_picc (tags[i]);
if (res < 0) {
warn ("Can't format PICC.");
error = EXIT_FAILURE;
break;
}
mifare_desfire_disconnect (tags[i]);
}
free (tag_uid);
}
freefare_free_tags (tags);
nfc_disconnect (device);
} }
exit (error); exit (error);

View file

@ -42,7 +42,7 @@ main(int argc, char *argv[])
nfc_list_devices (devices, 8, &device_count); nfc_list_devices (devices, 8, &device_count);
if (!device_count) if (!device_count)
errx (EXIT_FAILURE, "No NFC device found."); errx (EXIT_FAILURE, "No NFC device found.");
for (size_t d = 0; d < device_count; d++) { for (size_t d = 0; d < device_count; d++) {
device = nfc_connect (&(devices[d])); device = nfc_connect (&(devices[d]));
@ -52,76 +52,76 @@ main(int argc, char *argv[])
continue; continue;
} }
tags = freefare_get_tags (device); tags = freefare_get_tags (device);
if (!tags) { if (!tags) {
nfc_disconnect (device); nfc_disconnect (device);
errx (EXIT_FAILURE, "Error listing tags."); errx (EXIT_FAILURE, "Error listing tags.");
}
for (int i = 0; (!error) && tags[i]; i++) {
if (DESFIRE != freefare_get_tag_type (tags[i]))
continue;
int res;
char *tag_uid = freefare_get_tag_uid (tags[i]);
struct mifare_desfire_version_info info;
res = mifare_desfire_connect (tags[i]);
if (res < 0) {
warnx ("Can't connect to Mifare DESFire target.");
error = 1;
break;
} }
res = mifare_desfire_get_version (tags[i], &info); for (int i = 0; (!error) && tags[i]; i++) {
if (res < 0) { if (DESFIRE != freefare_get_tag_type (tags[i]))
warnx ("Can't get Mifare DESFire version information."); continue;
error = 1;
break; int res;
char *tag_uid = freefare_get_tag_uid (tags[i]);
struct mifare_desfire_version_info info;
res = mifare_desfire_connect (tags[i]);
if (res < 0) {
warnx ("Can't connect to Mifare DESFire target.");
error = 1;
break;
}
res = mifare_desfire_get_version (tags[i], &info);
if (res < 0) {
warnx ("Can't get Mifare DESFire version information.");
error = 1;
break;
}
printf ("===> Version information for tag %s:\n", tag_uid);
printf ("UID: 0x%02x%02x%02x%02x%02x%02x%02x\n", info.uid[0], info.uid[1], info.uid[2], info.uid[3], info.uid[4], info.uid[5], info.uid[6]);
printf ("Batch number: 0x%02x%02x%02x%02x%02x\n", info.batch_number[0], info.batch_number[1], info.batch_number[2], info.batch_number[3], info.batch_number[4]);
printf ("Production date: week %x, 20%02x\n", info.production_week, info.production_year);
printf ("Hardware Information:\n");
printf (" Vendor ID: 0x%02x\n", info.hardware.vendor_id);
printf (" Type: 0x%02x\n", info.hardware.type);
printf (" Subtype: 0x%02x\n", info.hardware.subtype);
printf (" Version: %d.%d\n", info.hardware.version_major, info.hardware.version_minor);
printf (" Storage size: 0x%02x (%s%d bytes)\n", info.hardware.storage_size, (info.hardware.storage_size & 1) ? ">" : "=", (int)pow (2, info.hardware.storage_size >> 1));
printf (" Protocol: 0x%02x\n", info.hardware.protocol);
printf ("Software Information:\n");
printf (" Vendor ID: 0x%02x\n", info.software.vendor_id);
printf (" Type: 0x%02x\n", info.software.type);
printf (" Subtype: 0x%02x\n", info.software.subtype);
printf (" Version: %d.%d\n", info.software.version_major, info.software.version_minor);
printf (" Storage size: 0x%02x (%s%d bytes)\n", info.software.storage_size, (info.software.storage_size & 1) ? ">" : "=", (int)pow (2, info.software.storage_size >> 1));
printf (" Protocol: 0x%02x\n", info.software.protocol);
uint8_t settings;
uint8_t max_keys;
res = mifare_desfire_get_key_settings (tags[i], &settings, &max_keys);
if (res < 0) {
warnx ("Cant' get Mifare DESFire Master Key settings: %s", mifare_desfire_error_lookup (mifare_desfire_get_last_error (tags[i])));
error = 1;
break;
}
printf ("Master Key settings (0x%02x):\n", settings);
printf (" 0x%02x configuration changeable;\n", settings & 0x08);
printf (" 0x%02x PICC Master Key not required for create / delete;\n", settings & 0x04);
printf (" 0x%02x Free directory list access without PICC Master Key;\n", settings & 0x02);
printf (" 0x%02x Allow changing the Master Key;\n", settings & 0x01);
free (tag_uid);
mifare_desfire_disconnect (tags[i]);
} }
printf ("===> Version information for tag %s:\n", tag_uid); freefare_free_tags (tags);
printf ("UID: 0x%02x%02x%02x%02x%02x%02x%02x\n", info.uid[0], info.uid[1], info.uid[2], info.uid[3], info.uid[4], info.uid[5], info.uid[6]); nfc_disconnect (device);
printf ("Batch number: 0x%02x%02x%02x%02x%02x\n", info.batch_number[0], info.batch_number[1], info.batch_number[2], info.batch_number[3], info.batch_number[4]);
printf ("Production date: week %x, 20%02x\n", info.production_week, info.production_year);
printf ("Hardware Information:\n");
printf (" Vendor ID: 0x%02x\n", info.hardware.vendor_id);
printf (" Type: 0x%02x\n", info.hardware.type);
printf (" Subtype: 0x%02x\n", info.hardware.subtype);
printf (" Version: %d.%d\n", info.hardware.version_major, info.hardware.version_minor);
printf (" Storage size: 0x%02x (%s%d bytes)\n", info.hardware.storage_size, (info.hardware.storage_size & 1) ? ">" : "=", (int)pow (2, info.hardware.storage_size >> 1));
printf (" Protocol: 0x%02x\n", info.hardware.protocol);
printf ("Software Information:\n");
printf (" Vendor ID: 0x%02x\n", info.software.vendor_id);
printf (" Type: 0x%02x\n", info.software.type);
printf (" Subtype: 0x%02x\n", info.software.subtype);
printf (" Version: %d.%d\n", info.software.version_major, info.software.version_minor);
printf (" Storage size: 0x%02x (%s%d bytes)\n", info.software.storage_size, (info.software.storage_size & 1) ? ">" : "=", (int)pow (2, info.software.storage_size >> 1));
printf (" Protocol: 0x%02x\n", info.software.protocol);
uint8_t settings;
uint8_t max_keys;
res = mifare_desfire_get_key_settings (tags[i], &settings, &max_keys);
if (res < 0) {
warnx ("Cant' get Mifare DESFire Master Key settings: %s", mifare_desfire_error_lookup (mifare_desfire_get_last_error (tags[i])));
error = 1;
break;
}
printf ("Master Key settings (0x%02x):\n", settings);
printf (" 0x%02x configuration changeable;\n", settings & 0x08);
printf (" 0x%02x PICC Master Key not required for create / delete;\n", settings & 0x04);
printf (" 0x%02x Free directory list access without PICC Master Key;\n", settings & 0x02);
printf (" 0x%02x Allow changing the Master Key;\n", settings & 0x01);
free (tag_uid);
mifare_desfire_disconnect (tags[i]);
}
freefare_free_tags (tags);
nfc_disconnect (device);
} }
exit (error); exit (error);

View file

@ -50,129 +50,130 @@ main(int argc, char *argv[])
MifareTag *tags = NULL; MifareTag *tags = NULL;
if (argc > 1) if (argc > 1)
errx (EXIT_FAILURE, "usage: %s", argv[0]); errx (EXIT_FAILURE, "usage: %s", argv[0]);
nfc_device_desc_t devices[8]; nfc_device_desc_t devices[8];
size_t device_count; size_t device_count;
nfc_list_devices (devices, 8, &device_count); nfc_list_devices (devices, 8, &device_count);
if (!device_count) if (!device_count)
errx (EXIT_FAILURE, "No NFC device found."); errx (EXIT_FAILURE, "No NFC device found.");
for (size_t d = 0; d < device_count; d++) { for (size_t d = 0; d < device_count; d++) {
device = nfc_connect (&(devices[d])); device = nfc_connect (&(devices[d]));
if (!device) { if (!device) {
warnx ("nfc_connect() failed."); warnx ("nfc_connect() failed.");
error = EXIT_FAILURE; error = EXIT_FAILURE;
continue; continue;
} }
tags = freefare_get_tags (device); tags = freefare_get_tags (device);
if (!tags) { if (!tags) {
nfc_disconnect (device); nfc_disconnect (device);
errx (EXIT_FAILURE, "Error listing tags."); errx (EXIT_FAILURE, "Error listing tags.");
} }
for (int i = 0; (!error) && tags[i]; i++) { for (int i = 0; (!error) && tags[i]; i++) {
if (DESFIRE != freefare_get_tag_type (tags[i])) if (DESFIRE != freefare_get_tag_type (tags[i]))
continue; continue;
char *tag_uid = freefare_get_tag_uid (tags[i]); char *tag_uid = freefare_get_tag_uid (tags[i]);
char buffer[BUFSIZ]; char buffer[BUFSIZ];
printf ("Found %s with UID %s. Write NDEF [yN] ", freefare_get_tag_friendly_name (tags[i]), tag_uid); printf ("Found %s with UID %s. Write NDEF [yN] ", freefare_get_tag_friendly_name (tags[i]), tag_uid);
fgets (buffer, BUFSIZ, stdin); fgets (buffer, BUFSIZ, stdin);
bool write_ndef = ((buffer[0] == 'y') || (buffer[0] == 'Y')); bool write_ndef = ((buffer[0] == 'y') || (buffer[0] == 'Y'));
if (write_ndef) { if (write_ndef) {
int res; int res;
res = mifare_desfire_connect (tags[i]); res = mifare_desfire_connect (tags[i]);
if (res < 0) { if (res < 0) {
warnx ("Can't connect to Mifare DESFire target."); warnx ("Can't connect to Mifare DESFire target.");
error = EXIT_FAILURE; error = EXIT_FAILURE;
break; break;
} }
/* Initialised Formatting Procedure. See section 6.5.1 and 8.1 of Mifare DESFire as Type 4 Tag document*/ /* Initialised Formatting Procedure. See section 6.5.1 and 8.1 of Mifare DESFire as Type 4 Tag document*/
// Send Mifare DESFire Select Application with AID equal to 000000h to select the PICC level // Send Mifare DESFire Select Application with AID equal to 000000h to select the PICC level
res = mifare_desfire_select_application(tags[i], NULL); res = mifare_desfire_select_application(tags[i], NULL);
if (res < 0) if (res < 0)
errx (EXIT_FAILURE, "Application selection failed"); errx (EXIT_FAILURE, "Application selection failed");
MifareDESFireKey key; MifareDESFireKey key;
key = mifare_desfire_des_key_new_with_version (key_data_null); key = mifare_desfire_des_key_new_with_version (key_data_null);
// Authentication with PICC master key MAY be needed to issue ChangeKeySettings command // Authentication with PICC master key MAY be needed to issue ChangeKeySettings command
res = mifare_desfire_authenticate (tags[i], 0, key); res = mifare_desfire_authenticate (tags[i], 0, key);
if (res < 0) if (res < 0)
errx (EXIT_FAILURE, "Authentication with PICC master key failed"); errx (EXIT_FAILURE, "Authentication with PICC master key failed");
// Mifare DESFire Create Application with AID equal to EEEE10h, key settings equal to 09, NumOfKeys equal to 01h // Mifare DESFire Create Application with AID equal to EEEE10h, key settings equal to 09, NumOfKeys equal to 01h
MifareDESFireAID aid = mifare_desfire_aid_new(0xEEEE10); MifareDESFireAID aid = mifare_desfire_aid_new(0xEEEE10);
res = mifare_desfire_create_application (tags[i], aid, 0x09, 1); res = mifare_desfire_create_application (tags[i], aid, 0x09, 1);
if (res < 0) if (res < 0)
errx (EXIT_FAILURE, "Application creation failed. Try mifare-desfire-format before running %s.", argv[0]); errx (EXIT_FAILURE, "Application creation failed. Try mifare-desfire-format before running %s.", argv[0]);
// Mifare DESFire SelectApplication (Select previously creates application) // Mifare DESFire SelectApplication (Select previously creates application)
res = mifare_desfire_select_application(tags[i], aid); res = mifare_desfire_select_application(tags[i], aid);
if (res < 0) if (res < 0)
errx (EXIT_FAILURE, "Application selection failed"); errx (EXIT_FAILURE, "Application selection failed");
free (aid); free (aid);
// Authentication with NDEF Tag Application master key (Authentication with key 0) // Authentication with NDEF Tag Application master key (Authentication with key 0)
res = mifare_desfire_authenticate (tags[i], 0, key); res = mifare_desfire_authenticate (tags[i], 0, key);
if (res < 0) if (res < 0)
errx (EXIT_FAILURE, "Authentication with NDEF Tag Application master key failed"); errx (EXIT_FAILURE, "Authentication with NDEF Tag Application master key failed");
// Mifare DESFire ChangeKeySetting with key settings equal to 00000000b // Mifare DESFire ChangeKeySetting with key settings equal to 00000000b
res = mifare_desfire_change_key_settings (tags[i],0x00); res = mifare_desfire_change_key_settings (tags[i],0x00);
if (res < 0) if (res < 0)
errx (EXIT_FAILURE, "ChangeKeySettings failed"); errx (EXIT_FAILURE, "ChangeKeySettings failed");
// Mifare DESFire CreateStdDataFile with FileNo equal to 03h (CC File DESFire FID), ComSet equal to 00h, // Mifare DESFire CreateStdDataFile with FileNo equal to 03h (CC File DESFire FID), ComSet equal to 00h,
// AccesRights equal to E000h, File Size bigger equal to 00000Fh // AccesRights equal to E000h, File Size bigger equal to 00000Fh
res = mifare_desfire_create_std_data_file(tags[i],0x03,0x00,0xE000,0x00000F); res = mifare_desfire_create_std_data_file(tags[i],0x03,0x00,0xE000,0x00000F);
if (res < 0) if (res < 0)
errx (EXIT_FAILURE, "CreateStdDataFile failed"); errx (EXIT_FAILURE, "CreateStdDataFile failed");
// Mifare DESFire WriteData to write the content of the CC File with CClEN equal to 000Fh, // Mifare DESFire WriteData to write the content of the CC File with CClEN equal to 000Fh,
// Mapping Version equal to 10h,MLe equal to 003Bh, MLc equal to 0034h, and NDEF File Control TLV // Mapping Version equal to 10h,MLe equal to 003Bh, MLc equal to 0034h, and NDEF File Control TLV
// equal to T =04h, L=06h, V=E1 04 (NDEF ISO FID=E104h) 0E E0 (NDEF File size =3808 Bytes) 00 (free read access) // equal to T =04h, L=06h, V=E1 04 (NDEF ISO FID=E104h) 0E E0 (NDEF File size =3808 Bytes) 00 (free read access)
// 00 free write access // 00 free write access
byte_t capability_container_file_content[15] = { 0x00, 0x0F, // CCLEN: Size of this capability container.CCLEN values are between 000Fh and FFFEh byte_t capability_container_file_content[15] = {
0x10, // Mapping version 0x00, 0x0F, // CCLEN: Size of this capability container.CCLEN values are between 000Fh and FFFEh
0x00, 0x3B, // MLe: Maximum data size that can be read using a single ReadBinary command. MLe = 000Fh-FFFFh 0x10, // Mapping version
0x00, 0x34, // MLc: Maximum data size that can be sent using a single UpdateBinary command. MLc = 0001h-FFFFh 0x00, 0x3B, // MLe: Maximum data size that can be read using a single ReadBinary command. MLe = 000Fh-FFFFh
0x04, 0x06, 0xE1, 0x04, // TLV 0x00, 0x34, // MLc: Maximum data size that can be sent using a single UpdateBinary command. MLc = 0001h-FFFFh
0x0E, 0xE0, // NDEF File size 0x04, 0x06, 0xE1, 0x04, // TLV
0x00, // free read access 0x0E, 0xE0, // NDEF File size
0x00 // free write acces 0x00, // free read access
}; 0x00 // free write acces
res = mifare_desfire_write_data(tags[i],0x03,0,sizeof(capability_container_file_content),capability_container_file_content); };
if (res>0){ res = mifare_desfire_write_data(tags[i],0x03,0,sizeof(capability_container_file_content),capability_container_file_content);
if (res>0){
// Mifare DESFire CreateStdDataFile with FileNo equal to 04h (NDEF FileDESFire FID), CmmSet equal to 00h, AccessRigths // Mifare DESFire CreateStdDataFile with FileNo equal to 04h (NDEF FileDESFire FID), CmmSet equal to 00h, AccessRigths
// equal to EEE0h, FileSize equal to 000EE0h (3808 Bytes) // equal to EEE0h, FileSize equal to 000EE0h (3808 Bytes)
res = mifare_desfire_create_std_data_file(tags[i],0x04,0x00,0xEEE0,0x000EE0); res = mifare_desfire_create_std_data_file(tags[i],0x04,0x00,0xEEE0,0x000EE0);
if (res < 0) if (res < 0)
errx (EXIT_FAILURE, "CreateStdDataFile failed"); errx (EXIT_FAILURE, "CreateStdDataFile failed");
//Mifare DESFire WriteData to write the content of the NDEF File with NLEN equal to NDEF Message length and NDEF Message //Mifare DESFire WriteData to write the content of the NDEF File with NLEN equal to NDEF Message length and NDEF Message
res = mifare_desfire_write_data(tags[i], 0x04, 0, sizeof(ndef_msg), (uint8_t *) ndef_msg); res = mifare_desfire_write_data(tags[i], 0x04, 0, sizeof(ndef_msg), (uint8_t *) ndef_msg);
if (res < 0) if (res < 0)
errx (EXIT_FAILURE, " Write data failed"); errx (EXIT_FAILURE, " Write data failed");
} else { } else {
errx (EXIT_FAILURE, "Write CC file content failed"); errx (EXIT_FAILURE, "Write CC file content failed");
} }
mifare_desfire_key_free (key); mifare_desfire_key_free (key);
mifare_desfire_disconnect (tags[i]); mifare_desfire_disconnect (tags[i]);
} }
free (tag_uid); free (tag_uid);
} }
freefare_free_tags (tags); freefare_free_tags (tags);
nfc_disconnect (device); nfc_disconnect (device);
} }
exit (error); exit (error);
} }

View file

@ -49,9 +49,9 @@ freefare_tag_new (nfc_device_t *device, nfc_iso14443a_info_t nai)
/* Ensure the target is supported */ /* Ensure the target is supported */
for (size_t i = 0; i < sizeof (supported_tags) / sizeof (struct supported_tag); i++) { for (size_t i = 0; i < sizeof (supported_tags) / sizeof (struct supported_tag); i++) {
if (((nai.szUidLen == 4) || (nai.abtUid[0] == NXP_MANUFACTURER_CODE)) && if (((nai.szUidLen == 4) || (nai.abtUid[0] == NXP_MANUFACTURER_CODE)) &&
(nai.btSak == supported_tags[i].SAK) && (nai.btSak == supported_tags[i].SAK) &&
(nai.szAtsLen == supported_tags[i].ATS_length) && (nai.szAtsLen == supported_tags[i].ATS_length) &&
(0 == memcmp (nai.abtAts, supported_tags[i].ATS, supported_tags[i].ATS_length))) { (0 == memcmp (nai.abtAts, supported_tags[i].ATS, supported_tags[i].ATS_length))) {
tag_info = &(supported_tags[i]); tag_info = &(supported_tags[i]);
found = true; found = true;

View file

@ -85,8 +85,9 @@
DEBUG_XFER (msg, __##msg##_n, "===> "); \ DEBUG_XFER (msg, __##msg##_n, "===> "); \
if (!(nfc_initiator_transceive_bytes (tag->device, msg, __##msg##_n, res, &__##res##_n))) { \ if (!(nfc_initiator_transceive_bytes (tag->device, msg, __##msg##_n, res, &__##res##_n))) { \
nfc_perror (tag->device, __FUNCTION__); \ nfc_perror (tag->device, __FUNCTION__); \
if (disconnect) \ if (disconnect) { \
tag->active = false; \ tag->active = false; \
} \
return errno = EIO, -1; \ return errno = EIO, -1; \
} \ } \
DEBUG_XFER (res, __##res##_n, "<=== "); \ DEBUG_XFER (res, __##res##_n, "<=== "); \
@ -120,56 +121,56 @@ union mifare_classic_block {
typedef unsigned char MifareClassicAccessBits; typedef unsigned char MifareClassicAccessBits;
unsigned char mifare_data_access_permissions[] = { unsigned char mifare_data_access_permissions[] = {
/* /*
* [ Key A ] [ Key B ] * [ Key A ] [ Key B ]
* | | * | |
* ,----------- r(ead) | * ,----------- r(ead) |
* |,---------- w(rite) | * |,---------- w(rite) |
* ||,--------- d(ecrement) | * ||,--------- d(ecrement) |
* |||,-------- i(ncrement) | * |||,-------- i(ncrement) |
* |||| | * |||| |
* |||| ,------------------------ r * |||| ,------------------------ r
* ,----- C3 |||| |,----------------------- w * ,----- C3 |||| |,----------------------- w
* |,---- C2 |||| ||,---------------------- d * |,---- C2 |||| ||,---------------------- d
* ||,--- C1 |||| |||,--------------------- i * ||,--- C1 |||| |||,--------------------- i
* ||| |||| |||| * ||| |||| ||||
* 0b000 0b 1111 1111 */ 0xff, /* Default (blank card) */ * 0b000 0b 1111 1111 */ 0xff, /* Default (blank card) */
/* 0b001 0b 1000 1100 */ 0x8c, /* 0b001 0b 1000 1100 */ 0x8c,
/* 0b010 0b 1000 1000 */ 0x88, /* 0b010 0b 1000 1000 */ 0x88,
/* 0b011 0b 1010 1111 */ 0xaf, /* 0b011 0b 1010 1111 */ 0xaf,
/* 0b100 0b 1010 1010 */ 0xaa, /* 0b100 0b 1010 1010 */ 0xaa,
/* 0b101 0b 0000 1000 */ 0x08, /* 0b101 0b 0000 1000 */ 0x08,
/* 0b110 0b 0000 1100 */ 0x0c, /* 0b110 0b 0000 1100 */ 0x0c,
/* 0b111 0b 0000 0000 */ 0x00 /* 0b111 0b 0000 0000 */ 0x00
}; };
uint16_t mifare_trailer_access_permissions[] = { uint16_t mifare_trailer_access_permissions[] = {
/* /*
* [ Key A ] [ Access bits ] [ Key B ] * [ Key A ] [ Access bits ] [ Key B ]
* | | | * | | |
* ,----------- read A | | * ,----------- read A | |
* |,---------- read B | | * |,---------- read B | |
* ||,--------- write A | | * ||,--------- write A | |
* |||,-------- write B | | * |||,-------- write B | |
* |||| | | * |||| | |
* |||| ,----------------------- read A | * |||| ,----------------------- read A |
* |||| |,---------------------- read B | * |||| |,---------------------- read B |
* |||| ||,--------------------- write A | * |||| ||,--------------------- write A |
* |||| |||,-------------------- write B | * |||| |||,-------------------- write B |
* |||| |||| | * |||| |||| |
* |||| |||| ,----------------------------------- read A * |||| |||| ,----------------------------------- read A
* ,----- C3 |||| |||| |,---------------------------------- read B * ,----- C3 |||| |||| |,---------------------------------- read B
* |,---- C2 |||| |||| ||,--------------------------------- write A * |,---- C2 |||| |||| ||,--------------------------------- write A
* ||,--- C1 |||| |||| |||,-------------------------------- write B * ||,--- C1 |||| |||| |||,-------------------------------- write B
* ||| |||| |||| |||| * ||| |||| |||| ||||
* 0b000 0b 0010 1000 1010*/ 0x28a, * 0b000 0b 0010 1000 1010*/ 0x28a,
/* 0b001 0b 0001 1100 0001*/ 0x1c1, /* 0b001 0b 0001 1100 0001*/ 0x1c1,
/* 0b010 0b 0000 1000 1000*/ 0x088, /* 0b010 0b 0000 1000 1000*/ 0x088,
/* 0b011 0b 0000 1100 0000*/ 0x0c0, /* 0b011 0b 0000 1100 0000*/ 0x0c0,
/* 0b100 0b 0010 1010 1010*/ 0x2aa, /* Default (blank card) */ /* 0b100 0b 0010 1010 1010*/ 0x2aa, /* Default (blank card) */
/* 0b101 0b 0000 1101 0000*/ 0x0d0, /* 0b101 0b 0000 1101 0000*/ 0x0d0,
/* 0b110 0b 0001 1101 0001*/ 0x1d1, /* 0b110 0b 0001 1101 0001*/ 0x1d1,
/* 0b111 0b 0000 1100 0000*/ 0x0c0 /* 0b111 0b 0000 1100 0000*/ 0x0c0
}; };
@ -543,7 +544,7 @@ get_block_access_bits (MifareTag tag, const MifareClassicBlockNumber block, Mifa
*/ */
if (MIFARE_CLASSIC(tag)->cached_access_bits.sector_trailer_block_number == trailer) { if (MIFARE_CLASSIC(tag)->cached_access_bits.sector_trailer_block_number == trailer) {
/* cache hit! */ /* cache hit! */
sector_access_bits = MIFARE_CLASSIC(tag)->cached_access_bits.sector_access_bits; sector_access_bits = MIFARE_CLASSIC(tag)->cached_access_bits.sector_access_bits;
} else { } else {
MifareClassicBlock trailer_data; MifareClassicBlock trailer_data;
@ -561,7 +562,7 @@ get_block_access_bits (MifareTag tag, const MifareClassicBlockNumber block, Mifa
} }
MIFARE_CLASSIC(tag)->cached_access_bits.sector_trailer_block_number = trailer; MIFARE_CLASSIC(tag)->cached_access_bits.sector_trailer_block_number = trailer;
MIFARE_CLASSIC(tag)->cached_access_bits.block_number = -1; MIFARE_CLASSIC(tag)->cached_access_bits.block_number = -1;
MIFARE_CLASSIC(tag)->cached_access_bits.sector_access_bits = sector_access_bits; MIFARE_CLASSIC(tag)->cached_access_bits.sector_access_bits = sector_access_bits;
} }
/* /*
@ -730,7 +731,7 @@ MifareClassicBlockNumber
mifare_classic_sector_last_block (MifareClassicSectorNumber sector) mifare_classic_sector_last_block (MifareClassicSectorNumber sector)
{ {
return mifare_classic_sector_first_block (sector) + return mifare_classic_sector_first_block (sector) +
mifare_classic_sector_block_count (sector) - 1; mifare_classic_sector_block_count (sector) - 1;
} }
/* /*

View file

@ -93,8 +93,9 @@ static ssize_t read_data (MifareTag tag, uint8_t command, uint8_t file_no, off_
#define ASSERT_AUTHENTICATED(tag) \ #define ASSERT_AUTHENTICATED(tag) \
do { \ do { \
if (MIFARE_DESFIRE (tag)->authenticated_key_no == NOT_YET_AUTHENTICATED) \ if (MIFARE_DESFIRE (tag)->authenticated_key_no == NOT_YET_AUTHENTICATED) { \
return errno = EINVAL, -1;\ return errno = EINVAL, -1;\
} \
} while (0) } while (0)
/* /*
@ -103,18 +104,20 @@ static ssize_t read_data (MifareTag tag, uint8_t command, uint8_t file_no, off_
*/ */
#define ASSERT_CS(cs) \ #define ASSERT_CS(cs) \
do { \ do { \
if (cs < 0) \ if (cs < 0) { \
return errno = EINVAL, -1; \ return errno = EINVAL, -1; \
if (cs == 0x02) \ } else if (cs == 0x02) { \
return errno = EINVAL, -1; \ return errno = EINVAL, -1; \
if (cs > 0x03) \ } else if (cs > 0x03) { \
return errno = EINVAL, -1; \ return errno = EINVAL, -1; \
} \
} while (0) } while (0)
#define ASSERT_NOT_NULL(argument) \ #define ASSERT_NOT_NULL(argument) \
do { \ do { \
if (!argument) \ if (!argument) { \
return errno = EINVAL, -1; \ return errno = EINVAL, -1; \
} \
} while (0) } while (0)
@ -144,8 +147,9 @@ static ssize_t read_data (MifareTag tag, uint8_t command, uint8_t file_no, off_
errno = 0; \ errno = 0; \
MIFARE_DESFIRE (tag)->last_picc_error = OPERATION_OK; \ MIFARE_DESFIRE (tag)->last_picc_error = OPERATION_OK; \
DEBUG_XFER (__##msg, __##msg##_n+1, "===> "); \ DEBUG_XFER (__##msg, __##msg##_n+1, "===> "); \
if (!(nfc_initiator_transceive_bytes (tag->device, __##msg, __##msg##_n+1, __##res, &__##res##_n))) \ if (!(nfc_initiator_transceive_bytes (tag->device, __##msg, __##msg##_n+1, __##res, &__##res##_n))) { \
return errno = EIO, -1; \ return errno = EIO, -1; \
} \
DEBUG_XFER (__##res, __##res##_n, "<=== "); \ DEBUG_XFER (__##res, __##res##_n, "<=== "); \
__##res##_n -= 1; \ __##res##_n -= 1; \
while (__##res[0] == 0xf2) { \ while (__##res[0] == 0xf2) { \
@ -157,8 +161,9 @@ static ssize_t read_data (MifareTag tag, uint8_t command, uint8_t file_no, off_
DEBUG_XFER (__##res, __##res##_n, "<=== "); \ DEBUG_XFER (__##res, __##res##_n, "<=== "); \
__##res##_n -= 1; \ __##res##_n -= 1; \
} \ } \
if ((1 == __##res##_n) && (OPERATION_OK != res[0]) && (ADDITIONAL_FRAME != res[0])) \ if ((1 == __##res##_n) && (OPERATION_OK != res[0]) && (ADDITIONAL_FRAME != res[0])) { \
return MIFARE_DESFIRE (tag)->last_picc_error = res[0], -1; \ return MIFARE_DESFIRE (tag)->last_picc_error = res[0], -1; \
} \
} while (0) } while (0)
@ -757,15 +762,15 @@ mifare_desfire_change_file_settings (MifareTag tag, uint8_t file_no, uint8_t com
cached_file_settings_current[file_no] = false; cached_file_settings_current[file_no] = false;
if (MDAR_CHANGE_AR(settings.access_rights) == MDAR_FREE) { if (MDAR_CHANGE_AR(settings.access_rights) == MDAR_FREE) {
BUFFER_INIT (cmd, 5); BUFFER_INIT (cmd, 5);
BUFFER_INIT (res, 1); BUFFER_INIT (res, 1);
BUFFER_APPEND (cmd, 0x5F); BUFFER_APPEND (cmd, 0x5F);
BUFFER_APPEND (cmd, file_no); BUFFER_APPEND (cmd, file_no);
BUFFER_APPEND (cmd, communication_settings); BUFFER_APPEND (cmd, communication_settings);
BUFFER_APPEND_LE (cmd, access_rights, 2, sizeof (uint16_t)); BUFFER_APPEND_LE (cmd, access_rights, 2, sizeof (uint16_t));
DESFIRE_TRANSCEIVE (tag, cmd, res); DESFIRE_TRANSCEIVE (tag, cmd, res);
} else { } else {
BUFFER_INIT (cmd, 10); BUFFER_INIT (cmd, 10);
BUFFER_INIT (res, 1); BUFFER_INIT (res, 1);
@ -946,8 +951,8 @@ read_data (MifareTag tag, uint8_t command, uint8_t file_no, off_t offset, size_t
return -1; return -1;
} }
BUFFER_CLEAR (cmd); BUFFER_CLEAR (cmd);
BUFFER_APPEND (cmd, 0xAF); BUFFER_APPEND (cmd, 0xAF);
} }
} while (res[0] != 0x00); } while (res[0] != 0x00);

View file

@ -56,7 +56,7 @@ mifare_desfire_aid_new (uint32_t aid)
uint32_t aid_le = htole32 (aid); uint32_t aid_le = htole32 (aid);
if ((res = malloc (sizeof (*res)))) { if ((res = malloc (sizeof (*res)))) {
memcpy(res->data, ((uint8_t*)&aid_le), 3); memcpy(res->data, ((uint8_t*)&aid_le), 3);
} }
return res; return res;

View file

@ -256,7 +256,7 @@ mifare_des (MifareDESFireKey key, uint8_t *data, uint8_t *ivect, MifareDirection
if (mac) { if (mac) {
DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_ENCRYPT); DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_ENCRYPT);
} else { } else {
DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_DECRYPT); DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_DECRYPT);
} }
memcpy (data, edata, 8); memcpy (data, edata, 8);
break; break;
@ -266,9 +266,9 @@ mifare_des (MifareDESFireKey key, uint8_t *data, uint8_t *ivect, MifareDirection
DES_ecb_encrypt ((DES_cblock *) edata, (DES_cblock *) data, &(key->ks2), DES_DECRYPT); DES_ecb_encrypt ((DES_cblock *) edata, (DES_cblock *) data, &(key->ks2), DES_DECRYPT);
DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_ENCRYPT); DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_ENCRYPT);
} else { } else {
DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_DECRYPT); DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_DECRYPT);
DES_ecb_encrypt ((DES_cblock *) edata, (DES_cblock *) data, &(key->ks2), DES_ENCRYPT); DES_ecb_encrypt ((DES_cblock *) edata, (DES_cblock *) data, &(key->ks2), DES_ENCRYPT);
DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_DECRYPT); DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_DECRYPT);
} }
memcpy (data, edata, 8); memcpy (data, edata, 8);
break; break;

View file

@ -73,7 +73,7 @@ tlv_encode (const uint8_t type, const uint8_t *istream, uint16_t isize, size_t *
return NULL; return NULL;
if ((res = malloc (1 + ((isize > 254) ? 3 : 1) + isize + 1))) { if ((res = malloc (1 + ((isize > 254) ? 3 : 1) + isize + 1))) {
/* type + size + payload + terminator */ /* type + size + payload + terminator */
res[n++] = type; res[n++] = type;
if (isize > 254) { if (isize > 254) {

View file

@ -47,7 +47,7 @@ cut_setup ()
tag = NULL; tag = NULL;
for (int i=0; tags[i]; i++) { for (int i=0; tags[i]; i++) {
if ((freefare_get_tag_type(tags[i]) == CLASSIC_1K) || if ((freefare_get_tag_type(tags[i]) == CLASSIC_1K) ||
(freefare_get_tag_type(tags[i]) == CLASSIC_4K)) { (freefare_get_tag_type(tags[i]) == CLASSIC_4K)) {
tag = tags[i]; tag = tags[i];
res = mifare_classic_connect (tag); res = mifare_classic_connect (tag);
cut_assert_equal_int (0, res, cut_message ("mifare_classic_connect() failed")); cut_assert_equal_int (0, res, cut_message ("mifare_classic_connect() failed"));

View file

@ -57,10 +57,14 @@ test_mifare_classic_mad (void)
// Check the empty MAD // Check the empty MAD
MifareClassicBlock ref_01 = { 0xce, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, MifareClassicBlock ref_01 = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 0xce, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
MifareClassicBlock ref_02 = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; };
MifareClassicBlock ref_02 = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
MifareClassicBlock data; MifareClassicBlock data;
res = mifare_classic_authenticate (tag, 0x01, mad_public_key_a, MFC_KEY_A); res = mifare_classic_authenticate (tag, 0x01, mad_public_key_a, MFC_KEY_A);
@ -78,12 +82,12 @@ test_mifare_classic_mad (void)
cut_assert_not_null (mad2, cut_message ("mad_read() failed")); cut_assert_not_null (mad2, cut_message ("mad_read() failed"));
cut_assert_equal_memory (mad, sizeof (mad), mad2, sizeof (mad2), cut_message ("Wrong MAD")); cut_assert_equal_memory (mad, sizeof (mad), mad2, sizeof (mad2), cut_message ("Wrong MAD"));
const char application_data[] = "APPLICATION DATA >> APPLICATION DATA >> APPLICATION DATA >> " const char application_data[] = "APPLICATION DATA >> APPLICATION DATA >> APPLICATION DATA >> " \
"APPLICATION DATA >> APPLICATION DATA >> APPLICATION DATA >> " "APPLICATION DATA >> APPLICATION DATA >> APPLICATION DATA >> " \
"APPLICATION DATA >> APPLICATION DATA >> APPLICATION DATA >> " "APPLICATION DATA >> APPLICATION DATA >> APPLICATION DATA >> " \
"APPLICATION DATA >> APPLICATION DATA >> APPLICATION DATA >> " "APPLICATION DATA >> APPLICATION DATA >> APPLICATION DATA >> " \
"APPLICATION DATA >> APPLICATION DATA >> APPLICATION DATA >> " "APPLICATION DATA >> APPLICATION DATA >> APPLICATION DATA >> " \
"APPLICATION DATA >> APPLICATION DATA >> APPLICATION DATA >> "; "APPLICATION DATA >> APPLICATION DATA >> APPLICATION DATA >> ";
MadAid aid = { MadAid aid = {
.function_cluster_code = 0x01, .function_cluster_code = 0x01,
@ -164,12 +168,18 @@ test_mifare_classic_mad (void)
cut_assert_equal_int (0, res, cut_message ("mad_read() failed")); cut_assert_equal_int (0, res, cut_message ("mad_read() failed"));
cut_assert_equal_memory (ref_02, sizeof (ref_02), data, sizeof (data), cut_message ("Wrong data")); cut_assert_equal_memory (ref_02, sizeof (ref_02), data, sizeof (data), cut_message ("Wrong data"));
MifareClassicBlock ref_40 = { 0x16, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, MifareClassicBlock ref_40 = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 0x16, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
MifareClassicBlock ref_41 = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; };
MifareClassicBlock ref_42 = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, MifareClassicBlock ref_41 = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
MifareClassicBlock ref_42 = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
res = mifare_classic_authenticate (tag, 0x40, mad_public_key_a, MFC_KEY_A); res = mifare_classic_authenticate (tag, 0x40, mad_public_key_a, MFC_KEY_A);
cut_assert_equal_int (0, res, cut_message ("mifare_classic_authenticate() failed")); cut_assert_equal_int (0, res, cut_message ("mifare_classic_authenticate() failed"));

View file

@ -24,42 +24,42 @@
const uint8_t shortdata[8] = "elephant"; const uint8_t shortdata[8] = "elephant";
const uint8_t eshortdata[11] = "\x03" "\x08" "elephant" "\xfe"; const uint8_t eshortdata[11] = "\x03" "\x08" "elephant" "\xfe";
/* /*
* Many thanks to Charles Baudelaire for helping me * Many thanks to Charles Baudelaire for helping me
* test things and helping you realize your f**king * test things and helping you realize your f**king
* OS / compiler does not support UTF-8 ;-) * OS / compiler does not support UTF-8 ;-)
*/ */
const uint8_t longdata[660] = "Dans une terre grasse et pleine d'escargots\n" const uint8_t longdata[660] = "Dans une terre grasse et pleine d'escargots\n" \
"Je veux creuser moi-même une fosse profonde,\n" "Je veux creuser moi-même une fosse profonde,\n" \
"Où je puisse à loisir étaler mes vieux os\n" "Où je puisse à loisir étaler mes vieux os\n" \
"Et dormir dans l'oubli comme un requin dans l'onde.\n" "Et dormir dans l'oubli comme un requin dans l'onde.\n" \
"Je hais les testaments et je hais les tombeaux;\n" "Je hais les testaments et je hais les tombeaux;\n" \
"Plutôt que d'implorer une larme du monde,\n" "Plutôt que d'implorer une larme du monde,\n" \
"Vivant, j'aimerais mieux inviter les corbeaux\n" "Vivant, j'aimerais mieux inviter les corbeaux\n" \
"À saigner tous les bouts de ma carcasse immonde.\n" "À saigner tous les bouts de ma carcasse immonde.\n" \
"Ô vers! noirs compagnons sans oreille et sans yeux,\n" "Ô vers! noirs compagnons sans oreille et sans yeux,\n" \
"Voyez venir à vous un mort libre et joyeux;\n" "Voyez venir à vous un mort libre et joyeux;\n" \
"Philosophes viveurs, fils de la pourriture,\n" "Philosophes viveurs, fils de la pourriture,\n" \
"À travers ma ruine allez donc sans remords,\n" "À travers ma ruine allez donc sans remords,\n" \
"Et dites-moi s'il est encor quelque torture\n" "Et dites-moi s'il est encor quelque torture\n" \
"Pour ce vieux corps sans âme et mort parmi les morts!\n"; "Pour ce vieux corps sans âme et mort parmi les morts!\n";
const uint8_t elongdata[665] = "\x07" "\xff\x02\x94" const uint8_t elongdata[665] = "\x07" "\xff\x02\x94" \
"Dans une terre grasse et pleine d'escargots\n" "Dans une terre grasse et pleine d'escargots\n" \
"Je veux creuser moi-même une fosse profonde,\n" "Je veux creuser moi-même une fosse profonde,\n" \
"Où je puisse à loisir étaler mes vieux os\n" "Où je puisse à loisir étaler mes vieux os\n" \
"Et dormir dans l'oubli comme un requin dans l'onde.\n" "Et dormir dans l'oubli comme un requin dans l'onde.\n" \
"Je hais les testaments et je hais les tombeaux;\n" "Je hais les testaments et je hais les tombeaux;\n" \
"Plutôt que d'implorer une larme du monde,\n" "Plutôt que d'implorer une larme du monde,\n" \
"Vivant, j'aimerais mieux inviter les corbeaux\n" "Vivant, j'aimerais mieux inviter les corbeaux\n" \
"À saigner tous les bouts de ma carcasse immonde.\n" "À saigner tous les bouts de ma carcasse immonde.\n" \
"Ô vers! noirs compagnons sans oreille et sans yeux,\n" "Ô vers! noirs compagnons sans oreille et sans yeux,\n" \
"Voyez venir à vous un mort libre et joyeux;\n" "Voyez venir à vous un mort libre et joyeux;\n" \
"Philosophes viveurs, fils de la pourriture,\n" "Philosophes viveurs, fils de la pourriture,\n" \
"À travers ma ruine allez donc sans remords,\n" "À travers ma ruine allez donc sans remords,\n" \
"Et dites-moi s'il est encor quelque torture\n" "Et dites-moi s'il est encor quelque torture\n" \
"Pour ce vieux corps sans âme et mort parmi les morts!\n" "Pour ce vieux corps sans âme et mort parmi les morts!\n"
"\xfe"; "\xfe";
void void
test_tlv_encode_short (void) test_tlv_encode_short (void)