Exemplo n.º 1
0
int
explain_application (MifareTag tag, MifareDESFireAID aid, MifareDESFireKey key)
{
    char buffer[1024];
    if (mifare_desfire_select_application (tag, aid) < 0) {
        fprintf (stderr, "mifare_desfire_select_application() failed\n");
        return -1;
    }

    if (mifare_desfire_authenticate (tag, USER_ACCESS_KEYNO, key) < 0) {
        freefare_perror (tag, "mifare_desfire_authenticate_des");
        return -1;
    }

    ssize_t len = mifare_desfire_read_data_ex (tag, UCARD_METDATA_FILENO, 0, 0, buffer, USER_ACCESS_COMMUNICATION_MODE);
    if (len < 0) {
        fprintf (stderr, "mifare_desfire_read_data_ex() failed\n");
        return -1;
    }

    char *p = buffer;

    fprintf (stdout, "  Application \"%s\" with AID 0x%06x has the following files:\n",
             p,
             mifare_desfire_aid_get_aid (aid));

    p = strchr (p, '\0');
    p++;

    int file_no = 0;

    while (*p) {
        fprintf (stdout, "    File %2d - %s\n", file_no++, p);
        p = strchr (p, '\0');
        p++;

        if (UCARD_METDATA_FILENO == file_no)
            file_no++;
    }

    return 0;
}
Exemplo n.º 2
0
int
main(int argc, char *argv[])
{
    int error = EXIT_SUCCESS;
    nfc_device *device = NULL;
    FreefareTag *tags = NULL;

    if (argc > 1)
	errx (EXIT_FAILURE, "usage: %s", argv[0]);

    nfc_connstring devices[8];
    size_t device_count;

    nfc_context *context;
    nfc_init (&context);
    if (context == NULL)
	errx(EXIT_FAILURE, "Unable to init libnfc (malloc)");

    device_count = nfc_list_devices (context, devices, 8);
    if (device_count <= 0)
	errx (EXIT_FAILURE, "No NFC device found.");

    for (size_t d = 0; d < device_count; d++) {
        device = nfc_open (context, devices[d]);
        if (!device) {
            warnx ("nfc_open() failed.");
            error = EXIT_FAILURE;
            continue;
        }

	tags = freefare_get_tags (device);
	if (!tags) {
	    nfc_close (device);
	    errx (EXIT_FAILURE, "Error listing tags.");
	}

	for (int i = 0; (!error) && tags[i]; i++) {

	    if (MIFARE_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 = 1;
            break;
	    }else{
            printf("Connected uid: %s\n", tag_uid);
        }

        uint8_t key_version;
        mifare_desfire_get_key_version (tags[i], 0, &key_version);
        MifareDESFireKey key = malloc(sizeof(MifareDESFireKey));

        res = mifare_desfire_auto_authenticate(tags[i], 0, key);

        if(res < 0){
            printf("Error while geting the master key.....\n"); 
            break;
        }

        MifareDESFireAID aid = mifare_desfire_aid_new (0x112233);
		//res = mifare_desfire_create_application (tags[i], aid, 0xFF, 1);

        size_t count;
        MifareDESFireAID **aids = malloc(sizeof(MifareDESFireAID*)); 
        res = mifare_desfire_get_application_ids(tags[i], aids, &count);

        if(res==0){
            printf("found %d applicaton/s\n", (int)count);
            //struct MifareDESFireAID aidArr[count] = *aids;
            int j;
            for(j=0; j<count; j++){
                // Fails here
                aid = (*aids)[j];
                printf("aid %d: %x\n", j, mifare_desfire_aid_get_aid(aid));

                if(mifare_desfire_aid_get_aid(aid) == 0x112233){
                    if(mifare_desfire_select_application(tags[i], aid) == 0){ 
                      printf("app selected...\n");
                      size_t fileCount = 0;
                      uint8_t **files = malloc(sizeof(uint8_t));
                      int rra = mifare_desfire_get_file_ids(tags[i], files, &fileCount);
                      printf("%d found %d file ids\n", rra, (int)fileCount);
                    }
                }

            }
            printf("ok\n");
        }else{
            printf("error res is %d\n", res);
        }
        
        mifare_desfire_key_free (key);

	    free (tag_uid);

	    mifare_desfire_disconnect (tags[i]);
	}

	freefare_free_tags (tags);
	nfc_close (device);
    }
    nfc_exit (context);
    exit (error);
} /* main() */