examples/mifare-desfire-*-ndef add support for mapping v2.0 and EV1

This commit is contained in:
Philippe Teuwen 2012-05-15 21:22:25 +00:00
parent 0268a9f975
commit 465a6aaaf0
3 changed files with 183 additions and 63 deletions

View file

@ -179,10 +179,6 @@ main(int argc, char *argv[])
if (res < 0)
errx (EXIT_FAILURE, "Authentication with PICC master key failed");
// TODO
if (info.software.version_major > 0)
errx (EXIT_FAILURE, "Not implemented yet, sorry!");
uint8_t key_settings;
uint8_t max_keys;
mifare_desfire_get_key_settings(tags[i], &key_settings,&max_keys);
@ -199,18 +195,18 @@ main(int argc, char *argv[])
errx (EXIT_FAILURE, "ChangeKeySettings 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);
MifareDESFireAID aid;
if (ndef_mapping == 1) {
// Mifare DESFire Create Application with AID equal to EEEE10h, key settings equal to 0x09, NumOfKeys equal to 01h
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_app);
if (res < 0)
@ -219,13 +215,11 @@ main(int argc, char *argv[])
res = mifare_desfire_change_key_settings (tags[i],0x09);
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);
res = mifare_desfire_create_std_data_file(tags[i],0x03,MDCM_PLAIN,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)
@ -243,18 +237,82 @@ main(int argc, char *argv[])
};
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);
res = mifare_desfire_create_std_data_file(tags[i],0x04,MDCM_PLAIN,0xEEE0,0x000EE0);
if (res < 0)
errx (EXIT_FAILURE, "CreateStdDataFile failed");
} else {
errx (EXIT_FAILURE, "Write CC file content failed");
}
}
else if (ndef_mapping == 2) {
// Mifare DESFire Create Application with AID equal to 000001h, key settings equal to 0x0F, NumOfKeys equal to 01h,
// 2 bytes File Identifiers supported, File-ID equal to E110h
aid = mifare_desfire_aid_new(0x000001);
uint8_t app[] = {0xd2, 0x76, 0x00, 0x00, 0x85, 0x01, 0x01};
res = mifare_desfire_create_application_iso (tags[i], aid, 0x0F, 0x21, 0, 0xE110, app, sizeof (app));
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_app);
if (res < 0)
errx (EXIT_FAILURE, "Authentication with NDEF Tag Application master key failed");
// Mifare DESFire ChangeKeySetting with key settings equal to 00001001b
res = mifare_desfire_change_key_settings (tags[i],0x09);
if (res < 0)
errx (EXIT_FAILURE, "ChangeKeySettings failed");
// Mifare DESFire CreateStdDataFile with FileNo equal to 01h (DESFire FID), ComSet equal to 00h,
// AccesRights equal to E000h, File Size bigger equal to 00000Fh, ISO File ID equal to E103h
res = mifare_desfire_create_std_data_file_iso(tags[i],0x01,MDCM_PLAIN,0xE000,0x00000F,0xE103);
if (res < 0)
errx (EXIT_FAILURE, "CreateStdDataFileIso failed");
// Mifare DESFire WriteData to write the content of the CC File with CClEN equal to 000Fh,
// Mapping Version equal to 20h,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) 0xNNNN (NDEF File size = 0x0800/0x1000/0x1E00 bytes)
// 00 (free read access) 00 free write access
uint8_t capability_container_file_content[15] = {
0x00, 0x0F, // CCLEN: Size of this capability container.CCLEN values are between 000Fh and FFFEh
0x20, // 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, // T & L of NDEF File Control TLV, followed by 6 bytes of V:
0xE1, 0x04, // File Identifier of NDEF File
0x08, 0x00, // Maximum NDEF File size of 2048 bytes
0x00, // free read access
0x00 // free write acces
};
// TODO adapt max size
res = mifare_desfire_write_data(tags[i],0x01,0,sizeof(capability_container_file_content),capability_container_file_content);
if (res>0){
// Mifare DESFire CreateStdDataFile with FileNo equal to 02h (DESFire FID), CmmSet equal to 00h, AccessRigths
// equal to EEE0h, FileSize equal to 000800h (2048 Bytes)
// TODO adapt max size
res = mifare_desfire_create_std_data_file_iso(tags[i],0x02,MDCM_PLAIN,0xEEE0,0x000800, 0xE104);
if (res < 0)
errx (EXIT_FAILURE, "CreateStdDataFileIso failed");
/*
uint8_t ndef_test[19] = {
0x00, 0x11, 0xd1, 0x02, 0x0c, 0x53, 0x70, 0xd1, 0x01, 0x08, 0x55, 0x01, 0x6e, 0x78, 0x70, 0x2e, 0x63, 0x6f, 0x6d
};
res = mifare_desfire_write_data(tags[i],0x02,0,sizeof(ndef_test),ndef_test);
if (res < 0)
errx (EXIT_FAILURE, "WriteStdDataFileIso failed");
*/
} else {
errx (EXIT_FAILURE, "Write CC file content failed");
}
}
mifare_desfire_key_free (key_picc);
mifare_desfire_key_free (key_app);
}
mifare_desfire_disconnect (tags[i]);
free (tag_uid);

View file

@ -33,6 +33,7 @@
* Mifare DESFire as Type 4 Tag
* NFC Forum Type 4 Tag Extensions for Mifare DESFire
* Rev. 1.1 - 21 August 2007
* Rev. 2.2 - 4 January 2012
*
*/
@ -172,11 +173,26 @@ main(int argc, char *argv[])
break;
}
// We've to track DESFire version as NDEF mapping is different
struct mifare_desfire_version_info info;
res = mifare_desfire_get_version (tags[i], &info);
if (res < 0) {
freefare_perror (tags[i], "mifare_desfire_get_version");
error = 1;
break;
}
MifareDESFireKey key_app;
key_app = mifare_desfire_des_key_new_with_version (key_data_app);
// Mifare DESFire SelectApplication (Select application)
MifareDESFireAID aid = mifare_desfire_aid_new(0xEEEE10);
MifareDESFireAID aid;
if (info.software.version_major==0)
aid = mifare_desfire_aid_new(0xEEEE10);
else
// There is no more relationship between DESFire AID and ISO AID...
// Let's assume it's in AID 000001h as proposed in the spec
aid = mifare_desfire_aid_new(0x000001);
res = mifare_desfire_select_application(tags[i], aid);
if (res < 0)
errx (EXIT_FAILURE, "Application selection failed. Try mifare-desfire-create-ndef before running %s.", argv[0]);
@ -189,7 +205,12 @@ main(int argc, char *argv[])
// Read Capability Container file E103
uint8_t lendata[2];
if (info.software.version_major==0)
res = mifare_desfire_read_data (tags[i], 0x03, 0, 2, lendata);
else
// There is no more relationship between DESFire FID and ISO FileID...
// Let's assume it's in FID 01h as proposed in the spec
res = mifare_desfire_read_data (tags[i], 0x01, 0, 2, lendata);
if (res < 0)
errx (EXIT_FAILURE, "Read CC len failed");
uint16_t cclen = (((uint16_t) lendata[0]) << 8) + ((uint16_t) lendata[1]);
@ -197,7 +218,10 @@ main(int argc, char *argv[])
errx (EXIT_FAILURE, "CC too short IMHO");
if (!(cc_data = malloc(cclen)))
errx (EXIT_FAILURE, "malloc");
if (info.software.version_major==0)
res = mifare_desfire_read_data (tags[i], 0x03, 0, cclen, cc_data);
else
res = mifare_desfire_read_data (tags[i], 0x01, 0, cclen, cc_data);
if (res < 0)
errx (EXIT_FAILURE, "Read CC data failed");
// Search NDEF File Control TLV
@ -210,7 +234,13 @@ main(int argc, char *argv[])
errx (EXIT_FAILURE, "CC does not contain expected NDEF File Control TLV");
if (cc_data[off+2] != 0xE1)
errx (EXIT_FAILURE, "Unknown NDEF File reference in CC");
uint8_t file_no = cc_data[off+3];
uint8_t file_no;
if (info.software.version_major==0)
file_no = cc_data[off+3];
else
// There is no more relationship between DESFire FID and ISO FileID...
// Let's assume it's in FID 02h as proposed in the spec
file_no = 2;
uint16_t ndefmaxlen = (((uint16_t) cc_data[off+4]) << 8) + ((uint16_t) cc_data[off+5]);
fprintf (message_stream, "Max NDEF size: %i bytes\n", ndefmaxlen);
if (!(ndef_msg = malloc(ndefmaxlen)))

View file

@ -33,6 +33,7 @@
* Mifare DESFire as Type 4 Tag
* NFC Forum Type 4 Tag Extensions for Mifare DESFire
* Rev. 1.1 - 21 August 2007
* Rev. 2.2 - 4 January 2012
*
*/
@ -200,11 +201,27 @@ main(int argc, char *argv[])
break;
}
// We've to track DESFire version as NDEF mapping is different
struct mifare_desfire_version_info info;
res = mifare_desfire_get_version (tags[i], &info);
if (res < 0) {
freefare_perror (tags[i], "mifare_desfire_get_version");
error = 1;
break;
}
MifareDESFireKey key_app;
key_app = mifare_desfire_des_key_new_with_version (key_data_app);
// Mifare DESFire SelectApplication (Select application)
MifareDESFireAID aid = mifare_desfire_aid_new(0xEEEE10);
MifareDESFireAID aid;
if (info.software.version_major==0)
aid = mifare_desfire_aid_new(0xEEEE10);
else
// There is no more relationship between DESFire AID and ISO AID...
// Let's assume it's in AID 000001h as proposed in the spec
aid = mifare_desfire_aid_new(0x000001);
res = mifare_desfire_select_application(tags[i], aid);
if (res < 0)
errx (EXIT_FAILURE, "Application selection failed. Try mifare-desfire-create-ndef before running %s.", argv[0]);
@ -217,7 +234,13 @@ main(int argc, char *argv[])
// Read Capability Container file E103
uint8_t lendata[2];
if (info.software.version_major==0)
res = mifare_desfire_read_data (tags[i], 0x03, 0, 2, lendata);
else
// There is no more relationship between DESFire FID and ISO FileID...
// Let's assume it's in FID 01h as proposed in the spec
res = mifare_desfire_read_data (tags[i], 0x01, 0, 2, lendata);
if (res < 0)
errx (EXIT_FAILURE, "Read CC len failed");
uint16_t cclen = (((uint16_t) lendata[0]) << 8) + ((uint16_t) lendata[1]);
@ -225,7 +248,10 @@ main(int argc, char *argv[])
errx (EXIT_FAILURE, "CC too short IMHO");
if (!(cc_data = malloc(cclen)))
errx (EXIT_FAILURE, "malloc");
if (info.software.version_major==0)
res = mifare_desfire_read_data (tags[i], 0x03, 0, cclen, cc_data);
else
res = mifare_desfire_read_data (tags[i], 0x01, 0, cclen, cc_data);
if (res < 0)
errx (EXIT_FAILURE, "Read CC data failed");
// Search NDEF File Control TLV
@ -238,7 +264,13 @@ main(int argc, char *argv[])
errx (EXIT_FAILURE, "CC does not contain expected NDEF File Control TLV");
if (cc_data[off+2] != 0xE1)
errx (EXIT_FAILURE, "Unknown NDEF File reference in CC");
uint8_t file_no = cc_data[off+3];
uint8_t file_no;
if (info.software.version_major==0)
file_no = cc_data[off+3];
else
// There is no more relationship between DESFire FID and ISO FileID...
// Let's assume it's in FID 02h as proposed in the spec
file_no = 2;
uint16_t ndefmaxlen = (((uint16_t) cc_data[off+4]) << 8) + ((uint16_t) cc_data[off+5]);
fprintf (stdout, "Max NDEF size: %i bytes\n", ndefmaxlen);
if (ndef_msg_len > ndefmaxlen)