2010-06-23 00:44:53 +02:00
|
|
|
/*-
|
|
|
|
* Copyright (C) 2010, Romain Tartiere, Romuald Conty.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU Lesser General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
* option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
|
|
|
* $Id$
|
|
|
|
*/
|
|
|
|
|
2011-10-12 00:24:26 +02:00
|
|
|
#include "config.h"
|
|
|
|
|
2010-06-23 00:44:53 +02:00
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2010-07-26 23:48:18 +02:00
|
|
|
#include <strings.h>
|
2011-10-12 00:24:26 +02:00
|
|
|
#include <unistd.h>
|
2010-06-23 00:44:53 +02:00
|
|
|
|
|
|
|
#include <nfc/nfc.h>
|
|
|
|
|
|
|
|
#include <freefare.h>
|
|
|
|
|
2010-06-23 04:05:28 +02:00
|
|
|
#define MIN(a,b) ((a < b) ? a: b)
|
|
|
|
|
2010-06-23 00:44:53 +02:00
|
|
|
MifareClassicKey default_keys[] = {
|
|
|
|
{ 0xff,0xff,0xff,0xff,0xff,0xff },
|
|
|
|
{ 0xd3,0xf7,0xd3,0xf7,0xd3,0xf7 },
|
|
|
|
{ 0xa0,0xa1,0xa2,0xa3,0xa4,0xa5 },
|
|
|
|
{ 0xb0,0xb1,0xb2,0xb3,0xb4,0xb5 },
|
|
|
|
{ 0x4d,0x3a,0x99,0xc3,0x51,0xdd },
|
|
|
|
{ 0x1a,0x98,0x2c,0x7e,0x45,0x9a },
|
|
|
|
{ 0xaa,0xbb,0xcc,0xdd,0xee,0xff },
|
|
|
|
{ 0x00,0x00,0x00,0x00,0x00,0x00 }
|
|
|
|
};
|
|
|
|
|
2010-07-26 22:27:21 +02:00
|
|
|
struct mifare_classic_key_and_type {
|
|
|
|
MifareClassicKey key;
|
|
|
|
MifareClassicKeyType type;
|
|
|
|
};
|
|
|
|
|
2013-07-14 21:48:30 +02:00
|
|
|
struct {
|
|
|
|
bool interactive;
|
|
|
|
} write_options = {
|
|
|
|
.interactive = true
|
|
|
|
};
|
|
|
|
|
2010-06-23 00:44:53 +02:00
|
|
|
const MifareClassicKey default_keyb = {
|
|
|
|
0xd3, 0xf7, 0xd3, 0xf7, 0xd3, 0xf7
|
|
|
|
};
|
|
|
|
|
2011-10-12 00:24:26 +02:00
|
|
|
const uint8_t ndef_default_msg[33] = {
|
2010-06-23 04:05:28 +02:00
|
|
|
0xd1, 0x02, 0x1c, 0x53, 0x70, 0x91, 0x01, 0x09,
|
|
|
|
0x54, 0x02, 0x65, 0x6e, 0x4c, 0x69, 0x62, 0x6e,
|
|
|
|
0x66, 0x63, 0x51, 0x01, 0x0b, 0x55, 0x03, 0x6c,
|
|
|
|
0x69, 0x62, 0x6e, 0x66, 0x63, 0x2e, 0x6f, 0x72,
|
2010-06-24 01:21:47 +02:00
|
|
|
0x67
|
2010-06-23 04:05:28 +02:00
|
|
|
};
|
2011-10-12 00:24:26 +02:00
|
|
|
uint8_t *ndef_msg;
|
|
|
|
size_t ndef_msg_len;
|
2010-06-23 04:05:28 +02:00
|
|
|
|
2012-05-18 18:20:15 +02:00
|
|
|
static int
|
2010-07-26 22:27:21 +02:00
|
|
|
search_sector_key (MifareTag tag, MifareClassicSectorNumber sector, MifareClassicKey *key, MifareClassicKeyType *key_type)
|
2010-06-23 00:44:53 +02:00
|
|
|
{
|
2010-07-26 22:27:21 +02:00
|
|
|
MifareClassicBlockNumber block = mifare_classic_sector_last_block (sector);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* FIXME: We should not assume that if we have full access to trailer block
|
|
|
|
* we also have a full access to data blocks.
|
|
|
|
*/
|
2010-06-23 04:05:28 +02:00
|
|
|
mifare_classic_disconnect (tag);
|
2010-06-26 15:48:25 +02:00
|
|
|
for (size_t i = 0; i < (sizeof (default_keys) / sizeof (MifareClassicKey)); i++) {
|
2010-06-23 00:44:53 +02:00
|
|
|
if ((0 == mifare_classic_connect (tag)) && (0 == mifare_classic_authenticate (tag, block, default_keys[i], MFC_KEY_A))) {
|
2010-07-26 22:27:21 +02:00
|
|
|
if ((1 == mifare_classic_get_trailer_block_permission (tag, block, MCAB_WRITE_KEYA, MFC_KEY_A)) &&
|
|
|
|
(1 == mifare_classic_get_trailer_block_permission (tag, block, MCAB_WRITE_ACCESS_BITS, MFC_KEY_A)) &&
|
|
|
|
(1 == mifare_classic_get_trailer_block_permission (tag, block, MCAB_WRITE_KEYB, MFC_KEY_A))) {
|
2010-06-23 00:44:53 +02:00
|
|
|
memcpy (key, &default_keys[i], sizeof (MifareClassicKey));
|
|
|
|
*key_type = MFC_KEY_A;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2010-06-24 02:19:05 +02:00
|
|
|
mifare_classic_disconnect (tag);
|
2010-06-23 00:44:53 +02:00
|
|
|
|
|
|
|
if ((0 == mifare_classic_connect (tag)) && (0 == mifare_classic_authenticate (tag, block, default_keys[i], MFC_KEY_B))) {
|
2010-07-26 22:27:21 +02:00
|
|
|
if ((1 == mifare_classic_get_trailer_block_permission (tag, block, MCAB_WRITE_KEYA, MFC_KEY_B)) &&
|
|
|
|
(1 == mifare_classic_get_trailer_block_permission (tag, block, MCAB_WRITE_ACCESS_BITS, MFC_KEY_B)) &&
|
|
|
|
(1 == mifare_classic_get_trailer_block_permission (tag, block, MCAB_WRITE_KEYB, MFC_KEY_B))) {
|
2010-06-23 00:44:53 +02:00
|
|
|
memcpy (key, &default_keys[i], sizeof (MifareClassicKey));
|
|
|
|
*key_type = MFC_KEY_B;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2010-06-24 02:19:05 +02:00
|
|
|
mifare_classic_disconnect (tag);
|
2010-06-23 00:44:53 +02:00
|
|
|
}
|
|
|
|
|
2010-07-26 22:27:21 +02:00
|
|
|
warnx ("No known authentication key for sector 0x%02x\n", sector);
|
2010-06-23 00:44:53 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-05-18 18:20:15 +02:00
|
|
|
static int
|
2012-01-25 10:58:16 +01:00
|
|
|
fix_mad_trailer_block (nfc_device *device, MifareTag tag, MifareClassicSectorNumber sector, MifareClassicKey key, MifareClassicKeyType key_type)
|
2010-06-29 17:16:35 +02:00
|
|
|
{
|
|
|
|
MifareClassicBlock block;
|
2010-07-28 12:23:56 +02:00
|
|
|
mifare_classic_trailer_block (&block, mad_public_key_a, 0x0, 0x1, 0x1, 0x6, 0x00, default_keyb);
|
2010-07-26 22:20:22 +02:00
|
|
|
if (mifare_classic_authenticate (tag, mifare_classic_sector_last_block (sector), key, key_type) < 0) {
|
2010-09-07 16:29:11 +02:00
|
|
|
nfc_perror (device, "fix_mad_trailer_block mifare_classic_authenticate");
|
2010-06-29 17:16:35 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2010-07-26 22:20:22 +02:00
|
|
|
if (mifare_classic_write (tag, mifare_classic_sector_last_block (sector), block) < 0) {
|
2010-09-07 16:29:11 +02:00
|
|
|
nfc_perror (device, "mifare_classic_write");
|
2010-06-29 17:16:35 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-05-18 18:18:45 +02:00
|
|
|
static void
|
2011-10-12 00:24:26 +02:00
|
|
|
usage(char *progname)
|
|
|
|
{
|
|
|
|
fprintf (stderr, "usage: %s -i FILE\n", progname);
|
|
|
|
fprintf (stderr, "\nOptions:\n");
|
2013-07-14 21:48:30 +02:00
|
|
|
fprintf (stderr, " -y Do not ask for confirmation\n");
|
2012-03-15 00:52:05 +01:00
|
|
|
fprintf (stderr, " -i Use FILE as NDEF message to write on card (\"-\" = stdin)\n");
|
2011-10-12 00:24:26 +02:00
|
|
|
}
|
|
|
|
|
2010-06-23 00:44:53 +02:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int error = 0;
|
2012-01-25 10:58:16 +01:00
|
|
|
nfc_device *device = NULL;
|
2010-06-23 00:44:53 +02:00
|
|
|
MifareTag *tags = NULL;
|
|
|
|
Mad mad;
|
2010-09-07 16:29:11 +02:00
|
|
|
MifareClassicKey transport_key = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
|
2010-06-23 00:44:53 +02:00
|
|
|
|
2011-10-12 00:24:26 +02:00
|
|
|
int ch;
|
|
|
|
char *ndef_input = NULL;
|
2013-07-14 21:48:30 +02:00
|
|
|
while ((ch = getopt (argc, argv, "hyi:")) != -1) {
|
2011-10-12 00:24:26 +02:00
|
|
|
switch (ch) {
|
|
|
|
case 'h':
|
|
|
|
usage(argv[0]);
|
|
|
|
exit (EXIT_SUCCESS);
|
|
|
|
break;
|
2013-07-14 21:48:30 +02:00
|
|
|
case 'y':
|
|
|
|
write_options.interactive = false;
|
|
|
|
break;
|
2011-10-12 00:24:26 +02:00
|
|
|
case 'i':
|
|
|
|
ndef_input = optarg;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
if (optopt == 'i')
|
|
|
|
fprintf (stderr, "Option -%c requires an argument.\n", optopt);
|
2014-04-13 19:41:17 +02:00
|
|
|
/* FALLTHROUGH */
|
2011-10-12 00:24:26 +02:00
|
|
|
default:
|
|
|
|
usage (argv[0]);
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ndef_input == NULL) {
|
|
|
|
ndef_msg = (uint8_t*)ndef_default_msg;
|
|
|
|
ndef_msg_len = sizeof(ndef_default_msg);
|
|
|
|
} else {
|
|
|
|
FILE* ndef_stream = NULL;
|
|
|
|
if ((strlen (ndef_input) == 1) && (ndef_input[0] == '-')) {
|
|
|
|
// FIXME stdin as input have to be readed and buffered in ndef_msg
|
|
|
|
ndef_stream = stdin;
|
|
|
|
fprintf (stderr, "stdin as NDEF is not implemented");
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
} else {
|
|
|
|
ndef_stream = fopen(ndef_input, "rb");
|
|
|
|
if (!ndef_stream) {
|
|
|
|
fprintf (stderr, "Could not open file %s.\n", ndef_input);
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
fseek(ndef_stream, 0L, SEEK_END);
|
|
|
|
ndef_msg_len = ftell(ndef_stream);
|
|
|
|
fseek(ndef_stream, 0L, SEEK_SET);
|
|
|
|
|
|
|
|
if (!(ndef_msg = malloc (ndef_msg_len))) {
|
|
|
|
err (EXIT_FAILURE, "malloc");
|
|
|
|
}
|
|
|
|
if (fread (ndef_msg, 1, ndef_msg_len, ndef_stream) != ndef_msg_len) {
|
|
|
|
fprintf (stderr, "Could not read NDEF from file: %s\n", ndef_input);
|
|
|
|
fclose (ndef_stream);
|
2012-03-15 10:16:55 +01:00
|
|
|
free (ndef_msg);
|
2011-10-12 00:24:26 +02:00
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
fclose (ndef_stream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf ("NDEF file is %zu bytes long.\n", ndef_msg_len);
|
2010-06-23 00:44:53 +02:00
|
|
|
|
2010-07-26 23:48:18 +02:00
|
|
|
struct mifare_classic_key_and_type *card_write_keys;
|
|
|
|
if (!(card_write_keys = malloc (40 * sizeof (*card_write_keys)))) {
|
|
|
|
err (EXIT_FAILURE, "malloc");
|
|
|
|
}
|
|
|
|
|
2012-01-25 10:58:16 +01:00
|
|
|
nfc_connstring devices[8];
|
2010-07-26 23:48:18 +02:00
|
|
|
size_t device_count;
|
2012-01-25 10:58:16 +01:00
|
|
|
|
2012-12-23 22:30:10 +01:00
|
|
|
nfc_context *context;
|
|
|
|
nfc_init (&context);
|
2013-03-30 18:07:34 +01:00
|
|
|
if (context == NULL)
|
|
|
|
errx(EXIT_FAILURE, "Unable to init libnfc (malloc)");
|
2010-07-26 23:48:18 +02:00
|
|
|
|
2012-12-23 22:30:10 +01:00
|
|
|
device_count = nfc_list_devices (context, devices, 8);
|
2012-01-25 10:58:16 +01:00
|
|
|
if (device_count <= 0)
|
2010-09-03 20:01:02 +02:00
|
|
|
errx (EXIT_FAILURE, "No NFC device found.");
|
2010-07-26 23:48:18 +02:00
|
|
|
|
|
|
|
for (size_t d = 0; d < device_count; d++) {
|
2012-12-23 22:30:10 +01:00
|
|
|
device = nfc_open (context, devices[d]);
|
2012-01-25 14:49:55 +01:00
|
|
|
if (!device) {
|
|
|
|
warnx ("nfc_open() failed.");
|
|
|
|
error = EXIT_FAILURE;
|
|
|
|
continue;
|
|
|
|
}
|
2010-06-23 00:44:53 +02:00
|
|
|
|
2010-09-03 20:01:02 +02:00
|
|
|
tags = freefare_get_tags (device);
|
|
|
|
if (!tags) {
|
2012-01-25 10:58:16 +01:00
|
|
|
nfc_close (device);
|
2010-09-03 20:01:02 +02:00
|
|
|
errx (EXIT_FAILURE, "Error listing MIFARE classic tag.");
|
2010-06-23 00:44:53 +02:00
|
|
|
}
|
|
|
|
|
2010-09-03 20:01:02 +02:00
|
|
|
for (int i = 0; (!error) && tags[i]; i++) {
|
2010-06-23 00:44:53 +02:00
|
|
|
switch (freefare_get_tag_type (tags[i])) {
|
2010-09-03 20:04:12 +02:00
|
|
|
case CLASSIC_1K:
|
|
|
|
case CLASSIC_4K:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
continue;
|
2010-06-23 00:44:53 +02:00
|
|
|
}
|
|
|
|
|
2010-09-03 20:01:02 +02:00
|
|
|
char *tag_uid = freefare_get_tag_uid (tags[i]);
|
|
|
|
char buffer[BUFSIZ];
|
|
|
|
|
2013-07-14 21:48:30 +02:00
|
|
|
printf ("Found %s with UID %s. ", freefare_get_tag_friendly_name (tags[i]), tag_uid);
|
2010-09-03 20:01:02 +02:00
|
|
|
|
2013-07-14 21:48:30 +02:00
|
|
|
bool write_ndef = true;
|
|
|
|
if (write_options.interactive) {
|
|
|
|
printf ("Write NDEF [yN] ");
|
|
|
|
fgets (buffer, BUFSIZ, stdin);
|
|
|
|
write_ndef = ((buffer[0] == 'y') || (buffer[0] == 'Y'));
|
|
|
|
} else {
|
|
|
|
printf ("\n");
|
|
|
|
}
|
|
|
|
|
2010-09-07 16:29:11 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-09-03 20:01:02 +02:00
|
|
|
if (write_ndef) {
|
2010-06-23 00:44:53 +02:00
|
|
|
switch (freefare_get_tag_type (tags[i])) {
|
2010-09-03 20:04:12 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
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])) {
|
2010-06-23 00:44:53 +02:00
|
|
|
case CLASSIC_4K:
|
2010-09-03 20:04:12 +02:00
|
|
|
if (card_write_keys[0x10].type != MFC_KEY_B) {
|
2010-09-07 16:29:11 +02:00
|
|
|
if( 0 != fix_mad_trailer_block (device, tags[i], 0x10, card_write_keys[0x10].key, card_write_keys[0x10].type)) {
|
2010-09-03 20:04:12 +02:00
|
|
|
error = 1;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
memcpy (&(card_write_keys[0x10].key), &default_keyb, sizeof (MifareClassicKey));
|
|
|
|
card_write_keys[0x10].type = MFC_KEY_B;
|
2010-06-23 00:44:53 +02:00
|
|
|
}
|
2010-07-26 22:20:22 +02:00
|
|
|
/* fallthrough */
|
2010-06-23 00:44:53 +02:00
|
|
|
case CLASSIC_1K:
|
2010-09-03 20:04:12 +02:00
|
|
|
if (card_write_keys[0x00].type != MFC_KEY_B) {
|
2010-09-07 16:29:11 +02:00
|
|
|
if( 0 != fix_mad_trailer_block (device, tags[i], 0x00, card_write_keys[0x00].key, card_write_keys[0x00].type)) {
|
2010-09-03 20:04:12 +02:00
|
|
|
error = 1;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
memcpy (&(card_write_keys[0x00].key), &default_keyb, sizeof (MifareClassicKey));
|
|
|
|
card_write_keys[0x00].type = MFC_KEY_B;
|
2010-06-23 00:44:53 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* Keep compiler quiet */
|
|
|
|
break;
|
2010-09-03 20:01:02 +02:00
|
|
|
}
|
|
|
|
}
|
2010-07-26 22:27:21 +02:00
|
|
|
|
2010-09-03 20:01:02 +02:00
|
|
|
size_t encoded_size;
|
2011-10-12 00:24:26 +02:00
|
|
|
uint8_t *tlv_data = tlv_encode (3, ndef_msg, ndef_msg_len, &encoded_size);
|
2010-07-26 22:27:21 +02:00
|
|
|
|
2010-09-03 20:01:02 +02:00
|
|
|
/*
|
|
|
|
* At his point, we should have collected all information needed to
|
|
|
|
* succeed.
|
|
|
|
*/
|
2010-07-26 22:27:21 +02:00
|
|
|
|
2010-09-03 20:01:02 +02:00
|
|
|
// If the card already has a MAD, load it.
|
|
|
|
if ((mad = mad_read (tags[i]))) {
|
|
|
|
// If our application already exists, erase it.
|
2010-09-07 16:29:11 +02:00
|
|
|
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);
|
2010-09-03 20:01:02 +02:00
|
|
|
mifare_application_free (mad, mad_nfcforum_aid);
|
|
|
|
} else {
|
2010-06-23 00:44:53 +02:00
|
|
|
|
2010-09-03 20:01:02 +02:00
|
|
|
// 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;
|
|
|
|
}
|
2010-08-31 14:00:02 +02:00
|
|
|
|
2010-09-03 20:01:02 +02:00
|
|
|
MifareClassicSectorNumber max_s;
|
|
|
|
switch (freefare_get_tag_type (tags[i])) {
|
2010-09-03 20:04:12 +02:00
|
|
|
case CLASSIC_1K:
|
|
|
|
max_s = 15;
|
|
|
|
break;
|
|
|
|
case CLASSIC_4K:
|
|
|
|
max_s = 39;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* Keep compiler quiet */
|
|
|
|
break;
|
2010-09-03 20:01:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2010-09-07 16:29:11 +02:00
|
|
|
nfc_perror (device, "mifare_classic_format_sector");
|
2010-09-03 20:01:02 +02:00
|
|
|
error = 1;
|
|
|
|
goto error;
|
|
|
|
}
|
2010-08-31 14:00:56 +02:00
|
|
|
}
|
2010-08-31 14:00:02 +02:00
|
|
|
}
|
2010-07-26 22:27:21 +02:00
|
|
|
}
|
2010-06-23 04:05:28 +02:00
|
|
|
|
2010-09-03 20:01:02 +02:00
|
|
|
MifareClassicSectorNumber *sectors = mifare_application_alloc (mad, mad_nfcforum_aid, encoded_size);
|
|
|
|
if (!sectors) {
|
2010-09-07 16:29:11 +02:00
|
|
|
nfc_perror (device, "mifare_application_alloc");
|
2010-07-26 22:27:21 +02:00
|
|
|
error = EXIT_FAILURE;
|
|
|
|
goto error;
|
|
|
|
}
|
2010-09-03 20:01:02 +02:00
|
|
|
|
|
|
|
if (mad_write (tags[i], mad, card_write_keys[0x00].key, card_write_keys[0x10].key) < 0) {
|
2010-09-07 16:29:11 +02:00
|
|
|
nfc_perror (device, "mad_write");
|
2010-07-26 22:27:21 +02:00
|
|
|
error = EXIT_FAILURE;
|
2010-06-23 04:05:28 +02:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2010-09-03 20:01:02 +02:00
|
|
|
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) {
|
2010-09-07 16:29:11 +02:00
|
|
|
nfc_perror (device, "mifare_classic_authenticate");
|
2010-09-03 20:01:02 +02:00
|
|
|
error = EXIT_FAILURE;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if (mifare_classic_write (tags[i], block, block_data) < 0) {
|
2010-09-07 16:29:11 +02:00
|
|
|
nfc_perror (device, "mifare_classic_write");
|
2010-09-03 20:01:02 +02:00
|
|
|
error = EXIT_FAILURE;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
s++;
|
|
|
|
}
|
2010-07-27 16:03:20 +02:00
|
|
|
|
2010-09-03 20:01:02 +02:00
|
|
|
if ((ssize_t) encoded_size != mifare_application_write (tags[i], mad, mad_nfcforum_aid, tlv_data, encoded_size, default_keyb, MCAB_WRITE_KEYB)) {
|
2010-09-07 16:29:11 +02:00
|
|
|
nfc_perror (device, "mifare_application_write");
|
2010-09-03 20:01:02 +02:00
|
|
|
error = EXIT_FAILURE;
|
|
|
|
goto error;
|
|
|
|
}
|
2010-06-24 01:21:47 +02:00
|
|
|
|
2010-09-03 20:01:02 +02:00
|
|
|
free (sectors);
|
2010-06-23 04:05:28 +02:00
|
|
|
|
2010-09-03 20:01:02 +02:00
|
|
|
free (tlv_data);
|
|
|
|
|
|
|
|
free (mad);
|
|
|
|
}
|
2010-06-23 00:44:53 +02:00
|
|
|
|
|
|
|
error:
|
2010-09-03 20:01:02 +02:00
|
|
|
free (tag_uid);
|
|
|
|
}
|
2010-06-23 00:44:53 +02:00
|
|
|
|
2012-03-15 10:16:55 +01:00
|
|
|
if (ndef_msg != ndef_default_msg)
|
|
|
|
free (ndef_msg);
|
2010-09-03 20:01:02 +02:00
|
|
|
freefare_free_tags (tags);
|
2012-01-25 10:58:16 +01:00
|
|
|
nfc_close (device);
|
2010-07-26 23:48:18 +02:00
|
|
|
}
|
2010-06-23 00:44:53 +02:00
|
|
|
|
2010-07-26 22:27:21 +02:00
|
|
|
free (card_write_keys);
|
2012-12-23 22:30:10 +01:00
|
|
|
nfc_exit (context);
|
2010-06-23 00:44:53 +02:00
|
|
|
exit (error);
|
|
|
|
}
|