예제 #1
0
        static bool SCardReadFile(IntPtr card, byte[] path, Stream tStream)
        {
            byte[] buffer = new byte[260];
            int    l      = 260;

            if (!SendDnieCmd(card, new byte[] { 0x00, 0xA4, 0x00, 0x00, 0x02, 0x50, 0x15 }, buffer, ref l))
            {
                return(false);
            }

            if (!SendDnieCmd(card, new byte[] { 0x00, 0xA4, 0x00, 0x00, 0x02, path[0], path[1] }, buffer, ref l))
            {
                return(false);
            }

            l = 260;

            // GET RESPONSE
            if (!SendDnieCmd(card, new byte[] { 0x00, 0xC0, 0x00, 0x00, 0x0E }, buffer, ref l))
            {
                return(false);
            }
            if (l != 0x10 || buffer[0] != 0x6F)
            {
                return(false);
            }

            // Obtenemos el tamaño
            ushort size = NFCHelper.ToUInt16(new byte[] { buffer[8], buffer[7] }, 0);

            for (ushort offset = 0; offset < size;)
            {
                byte[] s   = NFCHelper.GetBytesUInt16(offset);
                byte   lee = (byte)Math.Min(size - offset, 0xff);

                l = 258;
                if (!SendDnieCmd(card, new byte[] { 0x00, 0xB0, s[1], s[0], lee }, buffer, ref l))
                {
                    return(false);
                }
                offset += lee;

                if (l > 2)
                {
                    tStream.Write(buffer, 0, l - 2);
                    //tStream.Write(buffer, 2, l - 2 /*- 2*/);
                }
            }

            return(true);
        }
예제 #2
0
            public override string ToString()
            {
                StringBuilder sb = new StringBuilder();

                foreach (Block b in _DataBlocks)
                {
                    if (!b.IsReaded)
                    {
                        continue;
                    }

                    if (sb.Length == 0)
                    {
                        sb.Append(" - ");
                    }
                    sb.Append(NFCHelper.Buffer2Hex(b.Data));
                }

                return("Sector " + _SectorNum + " - " + _SectorNum.ToString("x2") + (sb.Length == 0 ? "" : " {" + sb.ToString() + "}"));
            }
예제 #3
0
        public MainWindow()
        {
            Closed += OnClosing;
            InitializeComponent();

            if (PrinterDiscoveryWindow.IsBluetoothSupported())
            {
                nfcHelper = new NFCHelper(this, SelectedPrinterComboBox);
                nfcHelper.SubscribeForNfcMessage();
            }

            formatInfo    = new List <Dictionary <string, string> >();
            attributeKeys = new List <string> {
                "drive",
                "name",
                "extension",
                "star"
            };

            recentlySelectedPrinters = new LinkedList <PrinterInfo>();
            selectAPrinterNode       = new LinkedListNode <PrinterInfo>(new PrinterInfo("Select a Printer", ""));
            recentlySelectedPrinters.AddFirst(selectAPrinterNode);

            if (!string.IsNullOrEmpty(Properties.Settings.Default.PreviousPrinters))
            {
                previousPrinters = JsonConvert.DeserializeObject <Dictionary <string, string> >(Properties.Settings.Default.PreviousPrinters);
                foreach (string address in previousPrinters.Keys)
                {
                    if (address.Length > 0)
                    {
                        recentlySelectedPrinters.AddAfter(selectAPrinterNode, new PrinterInfo(previousPrinters[address], address));
                    }
                }
            }

            SelectedPrinterComboBox.ItemsSource = recentlySelectedPrinters;
            if (previousPrinters == null)
            {
                previousPrinters = new Dictionary <string, string>();
            }
        }
예제 #4
0
        /// <summary>
        /// Obtiene el UID de una tarjeta
        /// </summary>
        /// <param name="card">Tarjeta obtenida</param>
        /// <param name="bAtr">ATR de la tarjeta</param>
        /// <param name="config">Configuración de lectura de la tarjeta</param>
        /// <returns>Devuelve el tipo de retorno de la lectura</returns>
        public bool GetCard(out ICard card, byte[] bAtr, ICardReadConfig config)
        {
            card = null;

            if (bAtr == null)
            {
                bAtr = GetAtr(_hCard, _Name, API.SCARD_PCI_T0 | API.SCARD_PCI_T1);
                if (bAtr == null)
                {
                    return(false);
                }
            }
            if (IsDniE(bAtr))
            {
                // Leer el Contenido del DNIe

                //http://delphi.jmrds.com/node/78
                //https://social.msdn.microsoft.com/Forums/es-ES/044965fe-5a3c-4ec9-8d30-3880cc6d420a/traducir-cdigo-c-as-vbnet?forum=vcses
                //https://social.msdn.microsoft.com/Forums/es-ES/24a95872-8207-499c-b158-167991d00343/lector-de-tarjetas?forum=vbes

                using (MemoryStream ms = new MemoryStream())
                {
                    if (SCardReadFile(_hCard, new byte[] { 0x60, 0x04 }, ms))
                    {
                        Asn1Parser a = new Asn1Parser();
                        a.LoadData(ms);

                        Asn1Node pais = a.GetNodeByPath("/2/0/1/0/0/1");
                        Asn1Node niff = a.GetNodeByPath("/2/0/1/1/0/1");
                        Asn1Node fame = a.GetNodeByPath("/2/0/1/2/0/1");
                        Asn1Node xame = a.GetNodeByPath("/2/0/1/3/0/1");
                        Asn1Node name = a.GetNodeByPath("/2/0/1/4/0/1");

                        string snif   = Encoding.UTF8.GetString(niff.Data);
                        string spais  = Encoding.UTF8.GetString(pais.Data);
                        string ssname = Encoding.UTF8.GetString(xame.Data);
                        string fname  = Encoding.UTF8.GetString(fame.Data);
                        string sname  = Encoding.UTF8.GetString(name.Data).Replace("(AUTENTICACIÓN)", "").Trim();

                        card = new CardDnie(bAtr)
                        {
                            Id = snif, Country = spais, CompleteName = sname, Name = ssname, Surname1 = fname
                        };
                    }
                }

                // Leemos el IDESP con la ruta 0006
                // Leemos la version con la ruta 2F03
                // Leemos el CDF con la ruta 5015 6004
            }
            else
            {
                // Buscar tipo de tarjeta Mifare según el ATR

                CardMifare.EMifareType type = CardMifare.EMifareType.Unknown;
                if (bAtr.Length >= 18)
                {
                    if (bAtr[0] == 0x3B)
                    {
                        // http://downloads.acs.com.hk/drivers/en/API-ACR122U-2.02.pdf

                        // Mifare
                        if (bAtr[13] == 0)
                        {
                            switch (bAtr[14])
                            {
                            case 0x01: type = CardMifare.EMifareType.Classic1K; break;

                            case 0x02: type = CardMifare.EMifareType.Classic4K; break;

                            case 0x03: type = CardMifare.EMifareType.UltraLight; break;

                            case 0x26: type = CardMifare.EMifareType.Mini; break;
                            }
                        }
                    }
                }

                ConfigMifareRead cfg = null;
                if (config != null)
                {
                    if (!(config is ConfigMifareRead))
                    {
                        throw (new ArgumentException("Config must be ConfigMifareRead for Mifare Reads", "config"));
                    }

                    cfg = (ConfigMifareRead)config;
                }

                // Get UID command for Mifare cards
                byte[] receivedUID = SendCmd(_hCard, new byte[] { 0xFF, 0xCA, 0x00, 0x00, 0x00 });
                if (receivedUID == null)
                {
                    return(false);
                }

                card = new CardMifare(type, bAtr)
                {
                    Id = NFCHelper.Buffer2Hex(receivedUID, 0, receivedUID.Length - 2).ToUpperInvariant(),
                };

                if (cfg != null && cfg.RequireReadSomething)
                {
                    CardMifare xcard = (CardMifare)card;

                    // Creamos la tarjeta según su tipo
                    xcard.InitCard();

                    byte[] data;

                    // Establecemos las claves en el lector
                    if (cfg.KeysZero != null)
                    {
                        // Cargar la K0
                        data = SendCmd(_hCard, new byte[] { 0xFF, 0x82, 0x00, 0x00, 0x06, }.Concat(cfg.KeysZero).ToArray());
                        if (data == null || data.Length != 2)
                        {
                            return(false);
                        }
                    }

                    if (cfg.KeysOne != null)
                    {
                        // Cargar la K1
                        data = SendCmd(_hCard, new byte[] { 0xFF, 0x82, 0x00, 0x01, 0x06, }.Concat(cfg.KeysOne).ToArray());
                        if (data == null || data.Length != 2)
                        {
                            return(false);
                        }
                    }

                    // Leer los sectores que se precisan
                    //byte blockNum;
                    ConfigMifareReadSector[] readSectors = cfg.ReadSectors;

                    foreach (CardMifare.Sector sector in xcard.Sectors)
                    {
                        ConfigMifareReadSector readCfg = readSectors[sector.SectorNum];
                        if (readCfg == null || !readCfg.ReadSomething)
                        {
                            continue;
                        }

                        // General Authenticate - Peer sector

                        if (readCfg.Login != null)
                        {
                            // Login al primer sector
                            data = SendCmd(_hCard, new byte[] { 0xFF, 0x86, 0x00, 0x00, 0x05, 0x01, 0x00, sector.DataBlocks[0].BlockNum,
                                                                (byte)(readCfg.Login.KeyType == ConfigMifareRead.EKeyType.A ? 0x60 : 0x61),
                                                                (byte)(readCfg.Login.KeyNum == ConfigMifareRead.EKeyNum.Zero ? 0x00 : 0x01) });
                        }

                        List <CardMifare.Block> bRead = new List <CardMifare.Block>();

                        if (readCfg.ReadDataBlocks)
                        {
                            for (int x = (int)readCfg.ReadDataBlockStart, m = (int)readCfg.ReadDataBlockEnd; x <= m; x++)
                            {
                                bRead.Add(sector.DataBlocks[x]);
                            }
                        }
                        if (readCfg.ReadTrailBlock)
                        {
                            bRead.Add(sector.TrailingBlock);
                        }

                        //if (data != null && data.Length == 2)
                        foreach (CardMifare.Block block in bRead)
                        {
                            //blockNum = block.BlockNum;// byte.Parse(block.BlockNum.ToString().PadLeft(2, '0'), NumberStyles.HexNumber);

                            //if (cfg.Login != null)
                            //{
                            //    data = SendCmd(_hCard, new byte[] { 0xFF, 0x86, 0x00, 0x00, 0x05, 0x01, 0x00, block.BlockNum,
                            //       (byte)(cfg.Login.KeyType == ConfigMifareRead.EKeyType.A ? 0x60 : 0x61),
                            //       (byte)(cfg.Login.KeyNum == ConfigMifareRead.EKeyNum.Zero ? 0x00 : 0x01) });

                            //    if (data == null || data.Length != 2) continue;
                            //}

                            // Read Binary
                            data = SendCmd(_hCard, new byte[] { 0xFF, 0xB0, 0x00, block.BlockNum, 0x10 });
                            if (data != null && data.Length == 18)
                            {
                                block.CopyFrom(data);
                            }
                        }
                    }
                }
            }

            // Eliminar ids vacios
            if (card != null && card.Id.Trim('0') == "")
            {
                card = null;
            }

            return(card != null);
        }
예제 #5
0
 public override string ToString()
 {
     return("Block " + _BlockNum + " - " + _BlockNum.ToString("x2") + (_IsReaded ? " {" + NFCHelper.Buffer2Hex(Data) + "}" : ""));
 }