예제 #1
0
        public static bool unlock_card(NFC.nfc_device pnd)
        {
            byte[] abtRx = new byte[264];
            int    szRx;

            /*
             * if (magic2)
             * {
             *  //printf("Don't use R/W with this card, this is not required!\n");
             *  return false;
             * }*/
            byte[] abtHalt = { 0x50, 0x00, 0x00, 0x00 };
            // special unlock command
            byte[] abtUnlock1 = { 0x40 };
            byte[] abtUnlock2 = { 0x43 };
            // Configure the CRC
            if (NFC.nfc_device_set_property_bool(pnd, NFC.nfc_property.NP_HANDLE_CRC, false) < 0)
            {
                NFC.nfc_perror(pnd, "nfc_configure");
                return(false);
            }
            // Use raw send/receive methods
            if (NFC.nfc_device_set_property_bool(pnd, NFC.nfc_property.NP_EASY_FRAMING, false) < 0)
            {
                NFC.nfc_perror(pnd, "nfc_configure");
                return(false);
            }

            ISO14443Subr.iso14443a_crc_append(abtHalt, 2);
            szRx = NFC.nfc_initiator_transceive_bytes(pnd, abtHalt, 4, abtRx, abtRx.Length, 0);//transmit_bytes(abtHalt, 4);

            // now send unlock
            if ((szRx = NFC.nfc_initiator_transceive_bits(pnd, abtUnlock1, 7, null, abtRx, abtRx.Length, null)) < 0)//!transmit_bits(abtUnlock1, 7)
            {
                //printf("unlock failure!\n");
                return(false);
            }
            if ((szRx = NFC.nfc_initiator_transceive_bits(pnd, abtUnlock2, 1, null, abtRx, abtRx.Length, null)) < 0)//(!transmit_bytes(abtUnlock2, 1))
            {
                //printf("unlock failure!\n");
                return(false);
            }

            // reset reader
            // Configure the CRC
            if (NFC.nfc_device_set_property_bool(pnd, NFC.nfc_property.NP_HANDLE_CRC, true) < 0)
            {
                NFC.nfc_perror(pnd, "nfc_device_set_property_bool");
                return(false);
            }
            // Switch off raw send/receive methods
            if (NFC.nfc_device_set_property_bool(pnd, NFC.nfc_property.NP_EASY_FRAMING, true) < 0)
            {
                NFC.nfc_perror(pnd, "nfc_device_set_property_bool");
                return(false);
            }
            return(true);
        }
예제 #2
0
        transmit_bytes(byte[] pbtTx, int szTx)
        {
            // Show transmitted command
            //printf("Sent bits:     ");
            //print_hex(pbtTx, szTx);
            // Transmit the command bytes
            int res;

            if ((res = NFC.nfc_initiator_transceive_bytes(pnd, pbtTx, szTx, abtRx, abtRx.Length, 0)) < 0)
            {
                return(false);
            }

            // Show received answer
            //printf("Received bits: ");
            //print_hex(abtRx, res);
            // Succesful transfer
            return(true);
        }
예제 #3
0
        public static int get_rats(NFC.nfc_device pnd, NFC.nfc_target pnt, byte[] abtRx)
        {
            int res;

            byte[] abtRats = { 0xe0, 0x50 };
            // Use raw send/receive methods
            if (NFC.nfc_device_set_property_bool(pnd, NFC.nfc_property.NP_EASY_FRAMING, false) < 0)
            {
                NFC.nfc_perror(pnd, "nfc_configure");
                return(-1);
            }
            res = NFC.nfc_initiator_transceive_bytes(pnd, abtRats, abtRats.Length, abtRx, abtRx.Length, 0);
            if (res > 0)
            {
                // ISO14443-4 card, turn RF field off/on to access ISO14443-3 again
                if (NFC.nfc_device_set_property_bool(pnd, NFC.nfc_property.NP_ACTIVATE_FIELD, false) < 0)
                {
                    NFC.nfc_perror(pnd, "nfc_configure");
                    return(-1);
                }
                if (NFC.nfc_device_set_property_bool(pnd, NFC.nfc_property.NP_ACTIVATE_FIELD, true) < 0)
                {
                    NFC.nfc_perror(pnd, "nfc_configure");
                    return(-1);
                }
            }
            // Reselect tag after using get_rats, example:
            if (NFC.nfc_initiator_select_passive_target(pnd, nmMfClassic, null, 0, pnt) <= 0)
            {
                //printf("Error: tag disappeared\n");
                //NFC.nfc_close(pnd);
                //NFC.nfc_exit(context);
                //exit(EXIT_FAILURE);
                return(-1);
            }
            return(res);
        }
예제 #4
0
        nfc_initiator_mifare_cmd(NFCInternal.nfc_device pnd, mifare_cmd mc, byte ui8Block, mifare_param pmp)
        {
            byte[] abtRx = new byte[265];
            byte   szParamLen;

            byte[] abtCmd = new byte[265];
            //bool    bEasyFraming;

            abtCmd[0] = (byte)mc;         // The MIFARE Classic command
            abtCmd[1] = ui8Block;         // The block address (1K=0x00..0x39, 4K=0x00..0xff)

            switch (mc)
            {
            // Read and store command have no parameter
            case mifare_cmd.MC_READ:
            case mifare_cmd.MC_STORE:
                szParamLen = 0;
                break;

            // Authenticate command
            case mifare_cmd.MC_AUTH_A:
            case mifare_cmd.MC_AUTH_B:
                szParamLen = (byte)pmp.mpa.Length();    //sizeof( mifare_param_auth);
                if (szParamLen > 0)
                {
                    pmp.mpa.FillRawData(abtCmd, 2);
                }
                break;

            // Data command
            case mifare_cmd.MC_WRITE:
                szParamLen = (byte)pmp.mpd.Length();    //sizeof( mifare_param_data);
                if (szParamLen > 0)
                {
                    pmp.mpd.FillRawData(abtCmd, 2);
                }
                break;

            // Value command
            case mifare_cmd.MC_DECREMENT:
            case mifare_cmd.MC_INCREMENT:
            case mifare_cmd.MC_TRANSFER:
                szParamLen = (byte)pmp.mpv.Length();    //sizeof( mifare_param_value);
                if (szParamLen > 0)
                {
                    pmp.mpv.FillRawData(abtCmd, 2);
                }
                break;

            // Please fix your code, you never should reach this statement
            default:
                return(false);

                break;
            }


            // FIXME: Save and restore bEasyFraming
            // bEasyFraming = nfc_device_get_property_bool (pnd, NP_EASY_FRAMING, &bEasyFraming);
            if (NFC.nfc_device_set_property_bool(pnd, NFC.nfc_property.NP_EASY_FRAMING, true) < 0)
            {
                //nfc_perror(pnd, "nfc_device_set_property_bool");
                return(false);
            }
            // Fire the mifare command
            int res;

            if ((res = NFC.nfc_initiator_transceive_bytes(pnd, abtCmd, 2 + szParamLen, abtRx, abtRx.Length, -1)) < 0)
            {
                if (res == NFC.NFC_ERFTRANS)
                {
                    // "Invalid received frame",  usual means we are
                    // authenticated on a sector but the requested MIFARE cmd (read, write)
                    // is not permitted by current acces bytes;
                    // So there is nothing to do here.
                }
                else
                {
                    //nfc_perror(pnd, "nfc_initiator_transceive_bytes");
                }
                // XXX nfc_device_set_property_bool (pnd, NP_EASY_FRAMING, bEasyFraming);
                return(false);
            }

            /* XXX
             * if (nfc_device_set_property_bool (pnd, NP_EASY_FRAMING, bEasyFraming) < 0) {
             * nfc_perror (pnd, "nfc_device_set_property_bool");
             * return false;
             * }
             */

            // When we have executed a read command, copy the received bytes into the param
            if (mc == mifare_cmd.MC_READ)
            {
                if (res == 16)
                {
                    MiscTool.memcpy(pmp.mpd.abtData, 0, abtRx, 0, 16);
                }
                else
                {
                    return(false);
                }
            }
            // Command succesfully executed
            return(true);
        }