fix mifare frame crc
[librfid] / openct-escape.c
index ecdb2d9..aec258c 100644 (file)
@@ -1,5 +1,4 @@
-
-/*
+/*                                                 -*- linux-c -*-
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License version 2 
  *  as published by the Free Software Foundation
 #include <unistd.h>
 #include <stdlib.h>
 #include <string.h>
-#include <openct/openct.h>
 
 #include <rfid/rfid.h>
 #include <rfid/rfid_reader.h>
 #include <rfid/rfid_layer2.h>
 #include <rfid/rfid_protocol.h>
 #include <rfid/rfid_reader_cm5121.h>
-
-static int slot = 1;
-static ct_handle *h;
-static ct_lock_handle lock;
+#include <rfid/rfid_protocol_mifare_classic.h>
 
 static struct rfid_reader_handle *rh;
 static struct rfid_layer2_handle *l2h;
 static struct rfid_protocol_handle *ph;
 
-
-/* this is the sole function required by rfid_reader_cm5121.c */
-int 
-PC_to_RDR_Escape(void *handle, 
-                const unsigned char *tx_buf, unsigned int tx_len,
-                unsigned char *rx_buf, unsigned int *rx_len)
-{
-       ct_handle *h = (ct_handle *) handle;
-       int rc;
-
-       rc = ct_card_transact(h, 1, tx_buf, tx_len, rx_buf, *rx_len);
-       if (rc >= 0) {
-               *rx_len = rc;
-               return 0;
-       }
-
-       return rc;
-}
-
-
-
 static int init()
 {
        unsigned char buf[0x3f];
-       unsigned char atr[64];
        int rc;
 
-       h = ct_reader_connect(0);
-       if (!h)
-               return -1;
-
-       printf("acquiring card lock\n");
-       rc = ct_card_lock(h, slot, IFD_LOCK_EXCLUSIVE, &lock);
-       if (rc < 0) {
-               fprintf(stderr, "error, no card lock\n");
-               return -1;
-       }
-
-       rc = ct_card_reset(h, slot, atr, sizeof(atr));
-       if (rc < 0) {
-               fprintf(stderr, "error, can't reset virtual card\n");
-               return -1;
-       }
-
        printf("initializing librfid\n");
        rfid_init();
 
        printf("opening reader handle\n");
-       rh = rfid_reader_open(h, RFID_READER_CM5121);
+       rh = rfid_reader_open(NULL, RFID_READER_CM5121);
        if (!rh) {
                fprintf(stderr, "error, no cm5121 handle\n");
                return -1;
@@ -146,7 +102,7 @@ static int select_mf(void)
 }
 
 
-static int get_challenge(unsigned char len)
+static int iso7816_get_challenge(unsigned char len)
 {
        unsigned char cmd[] = { 0x00, 0x84, 0x00, 0x00, 0x08 };
        unsigned char ret[256];
@@ -160,7 +116,7 @@ static int get_challenge(unsigned char len)
        if (rv < 0)
                return rv;
 
-       //printf("%s\n", rfid_hexdump(ret, rlen));
+       printf("%d: [%s]\n", rlen, rfid_hexdump(ret, rlen));
 
        return 0;
 }
@@ -169,7 +125,7 @@ int
 iso7816_select_application(void)
 {
        char cmd[] = { 0x00, 0xa4, 0x04, 0x0c, 0x07,
-                      0x0a, 0x00, 0x00, 0x02, 0x47, 0x10, 0x01 };
+                      0xa0, 0x00, 0x00, 0x02, 0x47, 0x10, 0x01 };
        char resp[7];
        unsigned int rlen = sizeof(resp);
 
@@ -180,6 +136,8 @@ iso7816_select_application(void)
                return rv;
 
        /* FIXME: parse response */
+       printf("%s\n", rfid_hexdump(resp, rlen));
+
        return 0;
 }
 
@@ -200,6 +158,7 @@ iso7816_select_ef(u_int16_t fid)
                return rv;
 
        /* FIXME: parse response */
+       printf("%s\n", rfid_hexdump(resp, rlen));
 
        return 0;
 }
@@ -236,7 +195,30 @@ iso7816_read_ef(u_int16_t fid, unsigned char *buf, unsigned int *len)
        return iso7816_read_binary(buf, len);
 }
 
+/* mifare ultralight helpers */
 int
+mifare_ulight_write(struct rfid_protocol_handle *ph)
+{
+       unsigned char buf[4] = { 0xa1, 0xa2, 0xa3, 0xa4 };
+
+       return rfid_protocol_write(ph, 10, buf, 4);
+}
+
+int
+mifare_ulight_blank(struct rfid_protocol_handle *ph)
+{
+       unsigned char buf[4] = { 0x00, 0x00, 0x00, 0x00 };
+       int i, ret;
+
+       for (i = 4; i <= MIFARE_UL_PAGE_MAX; i++) {
+               ret = rfid_protocol_write(ph, i, buf, 4);
+               if (ret < 0)
+                       return ret;
+       }
+       return 0;
+}
+
+static int
 mifare_ulight_read(struct rfid_protocol_handle *ph)
 {
        unsigned char buf[20];
@@ -244,26 +226,72 @@ mifare_ulight_read(struct rfid_protocol_handle *ph)
        int ret;
        int i;
 
-       for (i = 0; i < 7; i++) {
+       for (i = 0; i <= MIFARE_UL_PAGE_MAX; i++) {
                ret = rfid_protocol_read(ph, i, buf, &len);
                if (ret < 0)
                        return ret;
 
-               rfid_hexdump(buf, 4);
+               printf("Page 0x%x: %s\n", i, rfid_hexdump(buf, 4));
        }
        return 0;
 }
 
+/* mifare classic helpers */
+static int
+mifare_classic_read(struct rfid_protocol_handle *ph)
+{
+       unsigned char buf[20];
+       unsigned int len = sizeof(buf);
+       int ret;
+       int i;
+
+       for (i = 0; i <= MIFARE_CL_PAGE_MAX; i++) {
+               ret = rfid_protocol_read(ph, i, buf, &len);
+               if (ret < 0)
+                       return ret;
+
+               printf("Page 0x%x: %s\n", i, rfid_hexdump(buf, 4));
+       }
+       return 0;
+}
+
+
 int main(int argc, char **argv)
 {
        int rc;
        char buf[0x40];
        int i, protocol;
 
+#if 0
+        if (argc) {
+                argc--;
+                argv++;
+        }
+        
+        while (argc) {
+                if ( !strcmp (*argv, "--list")) {
+                        char *p;
+                        p = ccid_get_reader_list ();
+                        if (!p)
+                                return 1;
+                        fputs (p, stderr);
+                        free (p);
+                        return 0;
+                }
+                else if ( !strcmp (*argv, "--debug")) {
+                        ccid_set_debug_level (ccid_set_debug_level (-1) + 1);
+                        argc--; argv++;
+                }
+                else
+                        break;
+        }
+#endif
+
        if (init() < 0)
                exit(1);
 
-       protocol = RFID_PROTOCOL_MIFARE_UL;
+       //protocol = RFID_PROTOCOL_MIFARE_UL;
+       //protocol = RFID_PROTOCOL_MIFARE_CLASSIC;
        protocol = RFID_PROTOCOL_TCL;
 
        if (l3(protocol) < 0)
@@ -274,23 +302,38 @@ int main(int argc, char **argv)
                /* we've established T=CL at this point */
                select_mf();
 
-               rc632_register_dump(rh->ah, buf);
-               select_mf();
-
                iso7816_select_application();
                iso7816_select_ef(0x011e);
                iso7816_select_ef(0x0101);
-#if 0
+#if 1
                for (i = 0; i < 4; i++)
-                       get_challenge(0x60);
+                       iso7816_get_challenge(0x60);
 #endif
                break;
        case RFID_PROTOCOL_MIFARE_UL:
                mifare_ulight_read(ph);
+#if 0
+               mifare_ulight_blank(ph);
+               mifare_ulight_write(ph);
+               mifare_ulight_read(ph);
+#endif
+               break;
+       case RFID_PROTOCOL_MIFARE_CLASSIC:
+               rc = mfcl_set_key(ph, MIFARE_CLASSIC_KEYB_DEFAULT);
+               if (rc < 0) {
+                       printf("key format error\n");
+                       exit(1);
+               }
+               rc = mfcl_auth(ph, RFID_CMD_MIFARE_AUTH1B, 10);
+               if (rc < 0) {
+                       printf("mifare auth error\n");
+                       exit(1);
+               } else 
+                       printf("mifare authe succeeded!\n");
+               mifare_classic_read(ph);
                break;
        }
 
-
        rfid_reader_close(rh);
        
        exit(0);