From 40fa0d5d1344a1f07f5de60835d37b16c5e6d020 Mon Sep 17 00:00:00 2001 From: Romain Tartiere Date: Tue, 7 Sep 2010 14:29:11 +0000 Subject: [PATCH] Fix mifare-classic-write-ndef example when using a card with an existing MAD. Also fix some perror() -> nfc_perror(). --- examples/mifare-classic-write-ndef.c | 48 ++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/examples/mifare-classic-write-ndef.c b/examples/mifare-classic-write-ndef.c index be93b78..20b06a1 100644 --- a/examples/mifare-classic-write-ndef.c +++ b/examples/mifare-classic-write-ndef.c @@ -96,16 +96,16 @@ search_sector_key (MifareTag tag, MifareClassicSectorNumber sector, MifareClassi } int -fix_mad_trailer_block (MifareTag tag, MifareClassicSectorNumber sector, MifareClassicKey key, MifareClassicKeyType key_type) +fix_mad_trailer_block (nfc_device_t *device, MifareTag tag, MifareClassicSectorNumber sector, MifareClassicKey key, MifareClassicKeyType key_type) { MifareClassicBlock block; mifare_classic_trailer_block (&block, mad_public_key_a, 0x0, 0x1, 0x1, 0x6, 0x00, default_keyb); if (mifare_classic_authenticate (tag, mifare_classic_sector_last_block (sector), key, key_type) < 0) { - perror ("fix_mad_trailer_block mifare_classic_authenticate"); + nfc_perror (device, "fix_mad_trailer_block mifare_classic_authenticate"); return -1; } if (mifare_classic_write (tag, mifare_classic_sector_last_block (sector), block) < 0) { - perror ("mifare_classic_write"); + nfc_perror (device, "mifare_classic_write"); return -1; } return 0; @@ -118,6 +118,7 @@ main(int argc, char *argv[]) nfc_device_t *device = NULL; MifareTag *tags = NULL; Mad mad; + MifareClassicKey transport_key = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; (void)argc; (void)argv; @@ -164,6 +165,11 @@ main(int argc, char *argv[]) fgets (buffer, BUFSIZ, stdin); bool write_ndef = ((buffer[0] == 'y') || (buffer[0] == 'Y')); + for (int n = 0; n < 40; n++) { + memcpy(card_write_keys[n].key, transport_key, sizeof (transport_key)); + card_write_keys[n].type = MFC_KEY_A; + } + if (write_ndef) { switch (freefare_get_tag_type (tags[i])) { case CLASSIC_4K: @@ -188,7 +194,7 @@ main(int argc, char *argv[]) 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)) { + if( 0 != fix_mad_trailer_block (device, tags[i], 0x10, card_write_keys[0x10].key, card_write_keys[0x10].type)) { error = 1; goto error; } @@ -198,7 +204,7 @@ main(int argc, char *argv[]) /* 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)) { + if( 0 != fix_mad_trailer_block (device, tags[i], 0x00, card_write_keys[0x00].key, card_write_keys[0x00].type)) { error = 1; goto error; } @@ -223,6 +229,24 @@ main(int argc, char *argv[]) // If the card already has a MAD, load it. if ((mad = mad_read (tags[i]))) { // If our application already exists, erase it. + MifareClassicSectorNumber *sectors, *p; + sectors = p = mifare_application_find (mad, mad_nfcforum_aid); + if (sectors) { + while (*p) { + if (mifare_classic_authenticate (tags[i], mifare_classic_sector_last_block(*p), default_keyb, MFC_KEY_B) < 0) { + nfc_perror (device, "mifare_classic_authenticate"); + error = 1; + goto error; + } + if (mifare_classic_format_sector (tags[i], *p) < 0) { + nfc_perror (device, "mifare_classic_format_sector"); + error = 1; + goto error; + } + p++; + } + } + free (sectors); mifare_application_free (mad, mad_nfcforum_aid); } else { @@ -246,8 +270,6 @@ main(int argc, char *argv[]) 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; @@ -257,7 +279,7 @@ main(int argc, char *argv[]) (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"); + nfc_perror (device, "mifare_classic_format_sector"); error = 1; goto error; } @@ -267,13 +289,13 @@ main(int argc, char *argv[]) MifareClassicSectorNumber *sectors = mifare_application_alloc (mad, mad_nfcforum_aid, encoded_size); if (!sectors) { - perror ("mifare_application_alloc"); + nfc_perror (device, "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"); + nfc_perror (device, "mad_write"); error = EXIT_FAILURE; goto error; } @@ -284,12 +306,12 @@ main(int argc, char *argv[]) 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"); + nfc_perror (device, "mifare_classic_authenticate"); error = EXIT_FAILURE; goto error; } if (mifare_classic_write (tags[i], block, block_data) < 0) { - perror ("mifare_classic_write"); + nfc_perror (device, "mifare_classic_write"); error = EXIT_FAILURE; goto error; } @@ -297,7 +319,7 @@ main(int argc, char *argv[]) } 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"); + nfc_perror (device, "mifare_application_write"); error = EXIT_FAILURE; goto error; }