Ejemplo n.º 1
0
        public static bool authenticate(NFC.nfc_device pnd, NFC.nfc_target pnt, uint uiBlock, byte[] key, mifare_cmd mc)
        {
            mifare_param mp = new mifare_param();

            // Set the authentication information (uid)
            MiscTool.memcpy(mp.mpa.abtAuthUid, 0, pnt.nti.nai.abtUid, pnt.nti.nai.szUidLen - 4, 4);
            MiscTool.memcpy(mp.mpa.abtKey, 0, key, 0, 6);
            if (nfc_initiator_mifare_cmd(pnd, mc, (byte)uiBlock, mp))
            {
                return(true);
            }
            if (NFC.nfc_initiator_select_passive_target(pnd, nmMfClassic, pnt.nti.nai.abtUid, pnt.nti.nai.szUidLen, null) <= 0)
            {
                //ERR("tag was removed");
                return(false);
            }
            return(true);
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
0
 public static extern bool nfc_initiator_mifare_cmd(IntPtr pnd, mifare_cmd mc, byte ui8Block, ref mifare_param pmp);