70f2fe2685
Since it is irreversible, the card cannot be used for e.g. running the regression tests suite.
178 lines
8.1 KiB
C
178 lines
8.1 KiB
C
/*-
|
|
* Copyright (C) 2010, Audrey Diacre.
|
|
*
|
|
* 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/>
|
|
*
|
|
*/
|
|
|
|
#include <err.h>
|
|
#include <stdlib.h>
|
|
|
|
#include <freefare.h>
|
|
|
|
/*
|
|
* This example was written based on information provided by the
|
|
* following documents:
|
|
*
|
|
* Mifare DESFire as Type 4 Tag
|
|
* NFC Forum Type 4 Tag Extensions for Mifare DESFire
|
|
* Rev. 1.1 - 21 August 2007
|
|
*
|
|
*/
|
|
|
|
|
|
uint8_t key_data_null[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
|
|
|
|
const uint8_t ndef_msg[33] = {
|
|
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,
|
|
0x67
|
|
};
|
|
|
|
int
|
|
main(int argc, char *argv[])
|
|
{
|
|
int error = EXIT_SUCCESS;
|
|
nfc_device_t *device = NULL;
|
|
MifareTag *tags = NULL;
|
|
|
|
if (argc > 1)
|
|
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.");
|
|
|
|
for (size_t d = 0; d < device_count; d++) {
|
|
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.");
|
|
}
|
|
|
|
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. 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;
|
|
|
|
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");
|
|
|
|
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");
|
|
|
|
// 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);
|
|
|
|
// 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 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 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);
|
|
|
|
mifare_desfire_disconnect (tags[i]);
|
|
}
|
|
free (tag_uid);
|
|
}
|
|
freefare_free_tags (tags);
|
|
nfc_disconnect (device);
|
|
}
|
|
exit (error);
|
|
}
|