/// <summary>
        /// Read a sector trailer with provided keys
        /// </summary>
        /// <param name="sector"></param>
        /// <returns></returns>
        public bool ReadSectorTrailer(ref Sector sector)
        {
            byte[] data         = null;
            ushort blockAddress = (ushort)MemoryCardMifareClassic.SectorTrailerAddress(sector.Number);

            if (sector.isSetKeyA())
            {
                data = MifareClassicRead(blockAddress, 16, sector.getKeyA());
                if (data == null)
                {
                    if (sector.isSetKeyB())
                    {
                        data = MifareClassicRead(blockAddress, 16, sector.getKeyB());
                    }
                }
            }
            else
            {
                if (sector.isSetKeyB())
                {
                    data = MifareClassicRead(blockAddress, 16, sector.getKeyB());
                }
            }

            if (data == null)
            {
                return(false);
            }

            // Set access bits
            byte[] accessBytes = new byte[4];
            Array.ConstrainedCopy(data, 6, accessBytes, 0, 4);
            sector.AccessBits = accessBytes;
            return(true);
        }
Esempio n. 2
0
        public MifareClassicSector___DEPRECATED(MemoryCardMifareClassic Card, int Sector)
        {
            this.Card   = Card;
            this.Sector = Sector;

            if (Sector < 32)
            {
                Data = new byte[48];
            }
            else
            {
                Data = new byte[240];
            }
        }
        /// <summary>
        /// Try to read a sector with a specified key
        /// </summary>
        /// <param name="sectorNumber">Sector's number</param>
        /// <param name="key">Key used to read</param>
        /// <returns></returns>
        private MemoryCardMifareClassic.Sector ReadSectorWithKey(int sectorNumber, byte[] key)
        {
            MemoryCardMifareClassic.Sector sector = new Sector(sectorNumber);
            int firstBlockAddress = MemoryCardMifareClassic.SectorFirstBlockAddress(sectorNumber);
            int lastBlockAddress  = MemoryCardMifareClassic.SectorTrailerAddress(sectorNumber);
            int counter           = 0;

            for (ushort blockAddress = (ushort)firstBlockAddress; blockAddress < (ushort)lastBlockAddress; blockAddress++)
            {
                byte[] data = MifareClassicRead(blockAddress, 16, key);
                if (data != null)
                {
                    sector.SetBlock(counter, data);
                    counter++;
                }
                else
                {
                    return(null);
                }
            }
            return(sector);
        }
Esempio n. 4
0
        private static MemoryCard Create(SCardChannel Channel, byte[] Atr)
        {
            if (Atr == null)
            {
                return(null);                /* ATR error */
            }
            if (Atr.Length < MemoryCardAtrPrefix.Length + 3)
            {
                return(null);                /* ATR too short for a memory card */
            }
            for (int i = 0; i < MemoryCardAtrPrefix.Length; i++)
            {
                if (Atr[i] != MemoryCardAtrPrefix[i])
                {
                    return(null);                    /* ATR doesn't denote a memory card */
                }
            }
            Logger.Trace("From the ATR, this seems to be a Memory Card");

            MemoryCard Card = new MemoryCard();

            Card.Channel = Channel;

            Card.Atr = Atr;

            Card.PixSS = Card.Atr[MemoryCardAtrPrefix.Length];

            Card.PixNN    = new byte[2];
            Card.PixNN[0] = Card.Atr[MemoryCardAtrPrefix.Length + 1];
            Card.PixNN[1] = Card.Atr[MemoryCardAtrPrefix.Length + 2];

            ushort wPixNN = (ushort)((Card.PixNN[0] * 0x0100) | Card.PixNN[1]);

            Logger.Trace("PIX.SS=" + BinConvert.ToHex(Card.PixSS) + ", PIX.NN=" + BinConvert.ToHex(Card.PixNN));
            try {
                Card.CardType = (MemoryCardType)wPixNN;
            } catch {
                Card.CardType = MemoryCardType.UNKNOWN;
                Logger.Trace("This card is not listed");
            }

            switch (Card.CardType)
            {
            case MemoryCardType.MIFARE_CLASSIC_1K:
            case MemoryCardType.MIFARE_CLASSIC_4K:
            case MemoryCardType.MIFARE_MINI:
                Logger.Trace("Creating a Mifare Classic child object");
                Card = new MemoryCardMifareClassic(Card);
                break;

            default:
                Logger.Trace("(This is not Mifare Classic type)");
                break;
            }

            switch (Card.CardType)
            {
            case MemoryCardType.MIFARE_CLASSIC_1K:
                Card.CardName      = "Mifare Classic 1K";
                Card.BytesPerWrite = Card.BytesPerRead = 16;
                Card.MaxByteCount  = 16 * 64;
                break;

            case MemoryCardType.MIFARE_CLASSIC_4K:
                Card.CardName      = "Mifare Classic 4K";
                Card.BytesPerWrite = Card.BytesPerRead = 16;
                Card.MaxByteCount  = 16 * 256;
                break;

            case MemoryCardType.MIFARE_UL:
                Card.CardName      = "Mifare UltraLight";
                Card.BytesPerRead  = 16;
                Card.BytesPerWrite = 4;
                break;

            case MemoryCardType.ST_MICRO_ST176:
                Card.CardName      = "ST MicroElectronics SR176";
                Card.BytesPerWrite = Card.BytesPerRead = 2;
                Card.MaxByteCount  = 16 * 2;
                break;

            case MemoryCardType.ST_MICRO_OTHER_SR:
                Card.CardName      = "ST MicroElectronics SRI4K, SRIX4K, SRIX512, SRI512 or SRT512";
                Card.BytesPerWrite = Card.BytesPerRead = 4;
                break;

            case MemoryCardType.ATMEL_AT88SC0808CRF:
            case MemoryCardType.ATMEL_AT88SC1616CRF:
            case MemoryCardType.ATMEL_AT88SC3216CRF:
            case MemoryCardType.ATMEL_AT88SC6416CRF:
                Card.CardName = "Atmel AT88 'CryptoRF'";
                break;

            case MemoryCardType.TEXAS_TAGIT:
                Card.CardName      = "Texas Instruments TAG IT ";
                Card.BytesPerWrite = Card.BytesPerRead = 4;
                break;

            case MemoryCardType.ST_MICRO_LRI512:
                Card.CardName = "ST MicroElectronics LRI512";
                break;

            case MemoryCardType.NXP_ICODE_SLI:
                Card.CardName      = "NXP ICODE SLI";
                Card.BytesPerWrite = Card.BytesPerRead = 4;
                break;

            case MemoryCardType.NXP_ICODE1:
                Card.CardName      = "NXP ICODE1";
                Card.BytesPerWrite = Card.BytesPerRead = 4;
                Card.MaxByteCount  = 16 * 4;
                break;

            case MemoryCardType.ST_MICRO_LRI64:
                Card.CardName = "ST MicroElectronics LRI64";
                break;

            case MemoryCardType.ST_MICRO_LR12:
                Card.CardName = "ST MicroElectronics LR12";
                break;

            case MemoryCardType.ST_MICRO_LRI128:
                Card.CardName = "ST MicroElectronics LRI128";
                break;

            case MemoryCardType.MIFARE_MINI:
                Card.CardName      = "Mifare Classic Mini";
                Card.BytesPerWrite = Card.BytesPerRead = 16;
                Card.MaxByteCount  = 24 * 16;
                break;

            case MemoryCardType.INNOVISION_JEWEL:
                Card.CardName      = "Broadcom Jewel";
                Card.BytesPerRead  = 128;
                Card.BytesPerWrite = 1;
                Card.MaxByteCount  = 128;
                break;

            case MemoryCardType.INNOVISION_TOPAZ:
                Card.CardName      = "Broadcom Topaz";
                Card.BytesPerRead  = 128;
                Card.BytesPerWrite = 1;
                Card.MaxByteCount  = 128;
                break;

            case MemoryCardType.ATMEL_AT88RF04C:
                Card.CardName = "Atmel AT88RF04C";
                break;

            case MemoryCardType.NXP_ICODE_SL2:
                Card.CardName = "NXP ICODE SL2";
                break;

            case MemoryCardType.MIFARE_UL_C:
                Card.CardName      = "Mifare UltraLight C";
                Card.BytesPerRead  = 16;
                Card.BytesPerWrite = 4;
                break;

            case MemoryCardType.ASK_CTS_256B:
                Card.CardName     = "ASK CTS 256B";
                Card.BytesPerRead = Card.BytesPerWrite = 2;
                Card.MaxByteCount = 16 * 2;
                break;

            case MemoryCardType.ASK_CTS_512B:
                Card.CardName     = "ASK CTS 512B";
                Card.BytesPerRead = Card.BytesPerWrite = 2;
                Card.MaxByteCount = 32 * 2;
                break;

            case MemoryCardType.INSIDE_CONTACTLESS:
                Card.CardName     = "Inside PicoTag / HID iClass";
                Card.BytesPerRead = Card.BytesPerWrite = 8;
                Card.max_address  = 0x21;
                break;

            case MemoryCardType.UNIDENTIFIED_EMMARIN:
                Card.CardName     = "EM-Marin / Legic ISO 15693";
                Card.BytesPerRead = Card.BytesPerWrite = 8;
                break;

            default:
                break;
            }

            if (Card.CardName != null)
            {
                Logger.Trace("Card name: " + Card.CardName);
            }

            Card.CardUID = BinConvert.ToHex(Card.ReadUID());

            if (Card.CardUID != null)
            {
                Logger.Trace("Card UID: " + Card.CardUID);
            }

            return(Card);
        }