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);
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++) {
device = nfc_connect (&(devices[d]));
@ -177,72 +177,72 @@ main(int argc, char *argv[])
continue;
}
tags = freefare_get_tags (device);
if (!tags) {
nfc_disconnect (device);
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;
tags = freefare_get_tags (device);
if (!tags) {
nfc_disconnect (device);
errx (EXIT_FAILURE, "Error listing Mifare Classic tag.");
}
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) {
for (int i = 0; (!error) && tags[i]; i++) {
switch (freefare_get_tag_type (tags[i])) {
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;
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) {
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);

View file

@ -132,7 +132,7 @@ main(int argc, char *argv[])
nfc_list_devices (devices, 8, &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++) {
device = nfc_connect (&(devices[d]));
@ -142,179 +142,179 @@ main(int argc, char *argv[])
continue;
}
tags = freefare_get_tags (device);
if (!tags) {
nfc_disconnect (device);
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;
tags = freefare_get_tags (device);
if (!tags) {
nfc_disconnect (device);
errx (EXIT_FAILURE, "Error listing MIFARE classic tag.");
}
char *tag_uid = freefare_get_tag_uid (tags[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) {
for (int i = 0; (!error) && tags[i]; 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:
if (!search_sector_key (tags[i], 0x00, &(card_write_keys[0x00].key), &(card_write_keys[0x00].type))) {
error = 1;
goto error;
}
case CLASSIC_4K:
break;
default:
/* Keep compiler quiet */
break;
continue;
}
if (!error) {
/* Ensure the auth key is always a B one. If not, change it! */
char *tag_uid = freefare_get_tag_uid (tags[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])) {
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;
}
}
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");
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:
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);
if (!sectors) {
perror ("mifare_application_alloc");
error = EXIT_FAILURE;
goto error;
}
size_t encoded_size;
uint8_t *tlv_data = tlv_encode (3, ndef_msg, sizeof (ndef_msg), &encoded_size);
if (mad_write (tags[i], mad, card_write_keys[0x00].key, card_write_keys[0x10].key) < 0) {
perror ("mad_write");
error = EXIT_FAILURE;
goto error;
}
/*
* At his point, we should have collected all information needed to
* succeed.
*/
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");
// 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;
goto error;
}
}
}
}
MifareClassicSectorNumber *sectors = mifare_application_alloc (mad, mad_nfcforum_aid, encoded_size);
if (!sectors) {
perror ("mifare_application_alloc");
error = EXIT_FAILURE;
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;
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:
free (tag_uid);
}
free (tag_uid);
}
freefare_free_tags (tags);
nfc_disconnect (device);
freefare_free_tags (tags);
nfc_disconnect (device);
}
free (card_write_keys);

View file

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

View file

@ -75,7 +75,7 @@ main(int argc, char *argv[])
nfc_list_devices (devices, 8, &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++) {
device = nfc_connect (&(devices[d]));
@ -85,62 +85,62 @@ main(int argc, char *argv[])
continue;
}
tags = freefare_get_tags (device);
if (!tags) {
tags = freefare_get_tags (device);
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);
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);

View file

@ -42,7 +42,7 @@ main(int argc, char *argv[])
nfc_list_devices (devices, 8, &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++) {
device = nfc_connect (&(devices[d]));
@ -52,76 +52,76 @@ main(int argc, char *argv[])
continue;
}
tags = freefare_get_tags (device);
if (!tags) {
nfc_disconnect (device);
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;
tags = freefare_get_tags (device);
if (!tags) {
nfc_disconnect (device);
errx (EXIT_FAILURE, "Error listing tags.");
}
res = mifare_desfire_get_version (tags[i], &info);
if (res < 0) {
warnx ("Can't get Mifare DESFire version information.");
error = 1;
break;
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);
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);
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]);
}
freefare_free_tags (tags);
nfc_disconnect (device);
freefare_free_tags (tags);
nfc_disconnect (device);
}
exit (error);

View file

@ -50,129 +50,130 @@ main(int argc, char *argv[])
MifareTag *tags = NULL;
if (argc > 1)
errx (EXIT_FAILURE, "usage: %s", argv[0]);
errx (EXIT_FAILURE, "usage: %s", argv[0]);
nfc_device_desc_t devices[8];
size_t device_count;
nfc_list_devices (devices, 8, &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++) {
device = nfc_connect (&(devices[d]));
if (!device) {
warnx ("nfc_connect() failed.");
error = EXIT_FAILURE;
continue;
}
device = nfc_connect (&(devices[d]));
if (!device) {
warnx ("nfc_connect() failed.");
error = EXIT_FAILURE;
continue;
}
tags = freefare_get_tags (device);
if (!tags) {
nfc_disconnect (device);
errx (EXIT_FAILURE, "Error listing tags.");
}
tags = freefare_get_tags (device);
if (!tags) {
nfc_disconnect (device);
errx (EXIT_FAILURE, "Error listing tags.");
}
for (int i = 0; (!error) && tags[i]; i++) {
if (DESFIRE != freefare_get_tag_type (tags[i]))
continue;
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];
char *tag_uid = freefare_get_tag_uid (tags[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'));
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) {
int res;
if (write_ndef) {
int res;
res = mifare_desfire_connect (tags[i]);
if (res < 0) {
warnx ("Can't connect to Mifare DESFire target.");
error = EXIT_FAILURE;
break;
}
res = mifare_desfire_connect (tags[i]);
if (res < 0) {
warnx ("Can't connect to Mifare DESFire target.");
error = EXIT_FAILURE;
break;
}
/* 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
res = mifare_desfire_select_application(tags[i], NULL);
if (res < 0)
errx (EXIT_FAILURE, "Application selection failed");
/* 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
res = mifare_desfire_select_application(tags[i], NULL);
if (res < 0)
errx (EXIT_FAILURE, "Application selection failed");
MifareDESFireKey key;
key = mifare_desfire_des_key_new_with_version (key_data_null);
MifareDESFireKey key;
key = mifare_desfire_des_key_new_with_version (key_data_null);
// Authentication with PICC master key MAY be needed to issue ChangeKeySettings command
res = mifare_desfire_authenticate (tags[i], 0, key);
if (res < 0)
errx (EXIT_FAILURE, "Authentication with PICC master key failed");
// Authentication with PICC master key MAY be needed to issue ChangeKeySettings command
res = mifare_desfire_authenticate (tags[i], 0, key);
if (res < 0)
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
MifareDESFireAID aid = mifare_desfire_aid_new(0xEEEE10);
res = mifare_desfire_create_application (tags[i], aid, 0x09, 1);
if (res < 0)
errx (EXIT_FAILURE, "Application creation failed. Try mifare-desfire-format before running %s.", argv[0]);
// 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);
res = mifare_desfire_create_application (tags[i], aid, 0x09, 1);
if (res < 0)
errx (EXIT_FAILURE, "Application creation failed. Try mifare-desfire-format before running %s.", argv[0]);
// Mifare DESFire SelectApplication (Select previously creates application)
res = mifare_desfire_select_application(tags[i], aid);
if (res < 0)
errx (EXIT_FAILURE, "Application selection failed");
free (aid);
// Mifare DESFire SelectApplication (Select previously creates application)
res = mifare_desfire_select_application(tags[i], aid);
if (res < 0)
errx (EXIT_FAILURE, "Application selection failed");
free (aid);
// Authentication with NDEF Tag Application master key (Authentication with key 0)
res = mifare_desfire_authenticate (tags[i], 0, key);
if (res < 0)
errx (EXIT_FAILURE, "Authentication with NDEF Tag Application master key failed");
// Mifare DESFire ChangeKeySetting with key settings equal to 00000000b
res = mifare_desfire_change_key_settings (tags[i],0x00);
if (res < 0)
errx (EXIT_FAILURE, "ChangeKeySettings failed");
// Authentication with NDEF Tag Application master key (Authentication with key 0)
res = mifare_desfire_authenticate (tags[i], 0, key);
if (res < 0)
errx (EXIT_FAILURE, "Authentication with NDEF Tag Application master key failed");
// Mifare DESFire ChangeKeySetting with key settings equal to 00000000b
res = mifare_desfire_change_key_settings (tags[i],0x00);
if (res < 0)
errx (EXIT_FAILURE, "ChangeKeySettings failed");
// 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
res = mifare_desfire_create_std_data_file(tags[i],0x03,0x00,0xE000,0x00000F);
if (res < 0)
errx (EXIT_FAILURE, "CreateStdDataFile failed");
// 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
res = mifare_desfire_create_std_data_file(tags[i],0x03,0x00,0xE000,0x00000F);
if (res < 0)
errx (EXIT_FAILURE, "CreateStdDataFile failed");
// 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
// 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
byte_t capability_container_file_content[15] = { 0x00, 0x0F, // CCLEN: Size of this capability container.CCLEN values are between 000Fh and FFFEh
0x10, // Mapping version
0x00, 0x3B, // MLe: Maximum data size that can be read using a single ReadBinary command. MLe = 000Fh-FFFFh
0x00, 0x34, // MLc: Maximum data size that can be sent using a single UpdateBinary command. MLc = 0001h-FFFFh
0x04, 0x06, 0xE1, 0x04, // TLV
0x0E, 0xE0, // NDEF File size
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){
// 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
// 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
byte_t capability_container_file_content[15] = {
0x00, 0x0F, // CCLEN: Size of this capability container.CCLEN values are between 000Fh and FFFEh
0x10, // Mapping version
0x00, 0x3B, // MLe: Maximum data size that can be read using a single ReadBinary command. MLe = 000Fh-FFFFh
0x00, 0x34, // MLc: Maximum data size that can be sent using a single UpdateBinary command. MLc = 0001h-FFFFh
0x04, 0x06, 0xE1, 0x04, // TLV
0x0E, 0xE0, // NDEF File size
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){
// 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)
res = mifare_desfire_create_std_data_file(tags[i],0x04,0x00,0xEEE0,0x000EE0);
if (res < 0)
errx (EXIT_FAILURE, "CreateStdDataFile failed");
// 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)
res = mifare_desfire_create_std_data_file(tags[i],0x04,0x00,0xEEE0,0x000EE0);
if (res < 0)
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);
if (res < 0)
errx (EXIT_FAILURE, " Write data failed");
} else {
errx (EXIT_FAILURE, "Write CC file content failed");
}
mifare_desfire_key_free (key);
res = mifare_desfire_write_data(tags[i], 0x04, 0, sizeof(ndef_msg), (uint8_t *) ndef_msg);
if (res < 0)
errx (EXIT_FAILURE, " Write data failed");
} else {
errx (EXIT_FAILURE, "Write CC file content failed");
}
mifare_desfire_key_free (key);
mifare_desfire_disconnect (tags[i]);
}
free (tag_uid);
}
freefare_free_tags (tags);
nfc_disconnect (device);
mifare_desfire_disconnect (tags[i]);
}
free (tag_uid);
}
freefare_free_tags (tags);
nfc_disconnect (device);
}
exit (error);
}