Beispiel #1
0
        void DisplayReaderState(uint ReaderState, CardBuffer CardAtr)
        {
            lbReaderStatus.Text = SCARD.ReaderStatusToString(ReaderState);

            if (CardAtr != null)
            {
                lbCardAtr.Text = CardAtr.AsString(" ");
            }
            else
            {
                lbCardAtr.Text = "";
            }

            bool runable = false;

            if ((ReaderState & SCARD.STATE_PRESENT) != 0)
            {
                if ((ReaderState & SCARD.STATE_UNAVAILABLE) == 0)
                {
                    if ((ReaderState & SCARD.STATE_MUTE) == 0)
                    {
                        runable = true;
                    }
                }
            }

            btnRun.Enabled = runable;
        }
Beispiel #2
0
        /// <summary>
        /// Callback used when the reader's status change
        /// As this method is called from a thread, you can't directly modify the user interface
        /// </summary>
        /// <param name="readerState"></param>
        /// <param name="cardAtr"></param>
        private void readerStatusChanged(uint readerState, CardBuffer cardAtr)
        {
            // When you are in a thread you can't directly modify the user interface
            if (InvokeRequired)
            {
                this.BeginInvoke(new readerStatusChangedInvoker(readerStatusChanged), readerState, cardAtr);
                return;
            }
            btnRead.Enabled      = false;
            txtAsciiContent.Text = "";
            txtFinalStatus.Text  = "";
            txtHexData.Text      = "";
            lblCardAtr.Text      = "";
            lblStatus.Text       = SCARD.ReaderStatusToString(readerState);

            if (cardAtr != null)
            {
                lblCardAtr.Text = cardAtr.AsString(" ");
                channel         = new SCardChannel(reader);
                if (!channel.Connect())
                {
                    lblStatus.Text = "Error, can't connect to the card";
                    return;
                }
                CAPDU capdu = new CAPDU(0xFF, 0xCA, 0x00, 0x00);    // Command sent to the reader
                RAPDU rapdu = channel.Transmit(capdu);              // Response sent from card
                if (rapdu.SW != 0x9000)                             // Something failed
                {
                    lblStatus.Text = "Get UID APDU failed!";
                    return;
                }
                btnRead.Enabled = true;
            }
        }
Beispiel #3
0
        /*
         * SCardControl
         * ------------
         */
        void BtnControlClick(object sender, EventArgs e)
        {
            RCtrlClear();

            DynamicByteProvider p;

            byte[] b;

            p = (DynamicByteProvider)hexBoxCCtrl.ByteProvider;

            b = new byte[p.Length];

            for (int i = 0; i < p.Length; i++)
            {
                b[i] = p.ReadByte(i);
            }

            CardBuffer cctrl = new CardBuffer(b);

            Settings.HistoryControl.Add(cctrl.AsString());
            hist_ctrl_idx = -1;

            CardBuffer rctrl = channel.Control(cctrl);

            if (rctrl == null)
            {
                ShowError();
            }
            else
            {
                ShowSuccess();

                b = rctrl.GetBytes();

                p = new DynamicByteProvider(b);

                hexBoxRCtrl.ByteProvider = p;

                if (b.Length > 0)
                {
                    eResultByte.Text = String.Format("{0}", 0 - (int)b[0]);

                    if (b[0] == 0)
                    {
                        eResultByteExplain.Text = "Success";
                    }
                    else
                    {
                        ushort sw = (ushort)(0x6F00 | b[0]);
                        eResultByteExplain.Text = SCARD.CardStatusWordsToString(sw);
                    }
                }

                hexBoxRCtrl.BackColor        = hexBoxCApdu.BackColor;
                eResultByte.BackColor        = eCardAtr.BackColor;
                eResultByteExplain.BackColor = eCardAtr.BackColor;
            }
        }
Beispiel #4
0
        void DisplayReaderState(uint ReaderState, CardBuffer CardAtr)
        {
            lbReaderStatus.Text = SCARD.ReaderStatusToString(ReaderState);

            if (CardAtr != null)
            {
                lbCardAtr.Text = CardAtr.AsString(" ");
            }
            else
            {
                lbCardAtr.Text = "";
            }
        }
        private static bool Transmit(byte[] command, out byte[] response)
        {
            CAPDU capdu = new CAPDU(command);
            RAPDU rapdu = m_hCard.Transmit(capdu);

            response = null;
            if (rapdu == null)
            {
                LogManager.DoLogOperation(string.Format("[ERROR] fails to transmit"));
                return(false);
            }
            response = new byte[rapdu.Length];
            Array.Copy(rapdu.Bytes, 0, response, 0, rapdu.Length);
            if (rapdu.SW != 0x9000)
            {
                LogManager.DoLogOperation(string.Format("[ERROR] failed " + SCARD.CardStatusWordsToString(rapdu.SW) + "(" + SCARD.CardStatusWordsToString(rapdu.SW) + ")"));
                return(false);
            }


            return(true);
        }
Beispiel #6
0
        void ReaderStatusChanged(uint ReaderState, CardBuffer CardAtr)
        {
            /* The ReaderStatusChanged function is called as a delegate (callback) by the SCardReader object    */
            /* within its backgroung thread. Therefore we must use the BeginInvoke syntax to switch back from   */
            /* the context of the background thread to the context of the application's main thread. Overwise   */
            /* we'll get a security violation when trying to access the window's visual components (that belong */
            /* to the application's main thread and can't be safely manipulated by background threads).         */
            if (InvokeRequired)
            {
                this.BeginInvoke(new ReaderStatusChangedInvoker(ReaderStatusChanged), ReaderState, CardAtr);
                return;
            }

            eReaderStatus.Text = SCARD.ReaderStatusToString(ReaderState);

            if (CardAtr != null)
            {
                eCardAtr.Text = CardAtr.AsString(" ");
            }
            else
            {
                eCardAtr.Text = "";
            }

            if (ReaderState == SCARD.STATE_UNAWARE)
            {
//			  lbReaderStatus.Text = "";

                MessageBox.Show("The reader we were working on has disappeared from the system. This application will terminate now.",
                                "The reader has been removed",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Exclamation);

                Application.Exit();
            }
            else
            if ((ReaderState & SCARD.STATE_EMPTY) != 0)
            {
//			  lbReaderStatus.Text = "No card in the reader";
                card_available = false;
            }
            else
            if ((ReaderState & SCARD.STATE_UNAVAILABLE) != 0)
            {
//			  lbReaderStatus.Text = "Reader in use";
                card_available = false;
            }
            else
            if ((ReaderState & SCARD.STATE_MUTE) != 0)
            {
//			  lbReaderStatus.Text = "Card is mute";
                card_available = false;
            }
            else
            if ((ReaderState & SCARD.STATE_INUSE) != 0)
            {
//			  lbReaderStatus.Text = "Card in use";
                card_available = false;
            }
            else
            if ((ReaderState & SCARD.STATE_PRESENT) != 0)
            {
                //lbReaderStatus.Text = "Card ready";
                card_available = true;

                if (auto_write)
                {
                    BtnWriteClick(null, null);
                    auto_write = false;
                }
            }

            ShowStatus();
        }
        protected static bool WriteBinary(SCardChannel channel, ushort offset, byte[] buffer)
        {
            CAPDU capdu = new CAPDU(0x00, 0xD6, (byte)(offset / 0x0100), (byte)(offset % 0x0100), buffer);

            Trace.WriteLine("< " + capdu.AsString(" "));

            RAPDU rapdu = channel.Transmit(capdu);

            if (rapdu == null)
            {
                Trace.WriteLine("WriteBinary " + String.Format("{0:X4}", offset) + "," + String.Format("{0:X2}", (byte)buffer.Length) + " error " + channel.LastError + " (" + channel.LastErrorAsString + ")");
                return(false);
            }

            Trace.WriteLine("> " + rapdu.AsString(" "));

            if (rapdu.SW != 0x9000)
            {
                Trace.WriteLine("WriteBinary " + String.Format("{0:X4}", offset) + "," + String.Format("{0:X2}", (byte)buffer.Length) + " failed " + rapdu.SWString + " (" + SCARD.CardStatusWordsToString(rapdu.SW) + ")");
                return(false);
            }
            return(true);
        }
        protected static byte[] ReadBinary(SCardChannel channel, ushort offset, ushort length)
        {
            CAPDU capdu = new CAPDU(0x00, 0xB0, (byte)(offset / 0x0100), (byte)(offset % 0x0100), (byte)length);

            Trace.WriteLine("< " + capdu.AsString(" "));

            RAPDU rapdu = channel.Transmit(capdu);

            if (rapdu == null)
            {
                Trace.WriteLine("ReadBinary " + String.Format("{0:X4}", offset) + "," + String.Format("{0:X2}", (byte)length) + " error " + channel.LastError + " (" + channel.LastErrorAsString + ")");
                return(null);
            }

            Trace.WriteLine("> " + rapdu.AsString(" "));

            if (rapdu.SW != 0x9000)
            {
                Trace.WriteLine("ReadBinary " + String.Format("{0:X4}", offset) + "," + String.Format("{0:X2}", (byte)length) + " failed " + rapdu.SWString + " (" + SCARD.CardStatusWordsToString(rapdu.SW) + ")");
                return(null);
            }

            if (rapdu.hasData)
            {
                return(rapdu.data.GetBytes());
            }

            return(null);
        }
        private static bool SelectNfcApplication(SCardChannel channel)
        {
            CAPDU capdu = new CAPDU(0x00, 0xA4, 0x04, 0x00, (new CardBuffer(NDEF_APPLICATION_ID)).GetBytes(), 0x00);

            Trace.WriteLine("< " + capdu.AsString(" "));

            RAPDU rapdu = channel.Transmit(capdu);

            if (rapdu == null)
            {
                Trace.WriteLine("SelectNfcApplication error " + channel.LastError + " (" + channel.LastErrorAsString + ")");
                return(false);
            }

            Trace.WriteLine("> " + rapdu.AsString(" "));

            if (rapdu.SW != 0x9000)
            {
                Trace.WriteLine("SelectNfcApplication failed " + rapdu.SWString + " (" + SCARD.CardStatusWordsToString(rapdu.SW) + ")");
                return(false);
            }

            return(true);
        }
Beispiel #10
0
        private void btnRead_Click(object sender, EventArgs e)
        {
            txtAsciiContent.Text = "";
            txtHexData.Text      = "";

            byte[] readApdu = new byte[] { 0xFF, 0xB0, 0x00, 0x04, 0x30 }; // Read binary
            CAPDU  capdu    = new CAPDU(readApdu);                         // Command sent to the reader
            RAPDU  rapdu    = channel.Transmit(capdu);                     // Response sent from card

            if (rapdu == null)
            {
                txtFinalStatus.Text = "Problem while reading";
                return;
            }
            if (rapdu.SW != 0x9000)                                  // Something failed
            {
                txtFinalStatus.Text = "Error:" + String.Format("{0:X}", rapdu.SW) + ": " + SCARD.CardStatusWordsToString(rapdu.SW);
                return;
            }
            txtHexData.Text      = BitConverter.ToString(rapdu.GetBytes()).Replace('-', ' ');
            txtAsciiContent.Text = ByteArrayToString(rapdu.GetBytes());
            txtFinalStatus.Text  = "Read with success";
        }
Beispiel #11
0
        private void btnWrite_Click(object sender, EventArgs e)
        {
            string dataToWrite = txtData.Text.Trim().PadRight(47) + (char)0x00;
            bool   success     = true;

            txtDataSent.Text = "";

            txtDataSent.Text = txtData.Text + System.Environment.NewLine;
            byte[] content   = Encoding.ASCII.GetBytes(txtData.Text.PadRight(16, ' '));
            byte[] header    = new byte[] { 0xFF, 0xF4, 0x00, (byte)numBlockNumber.Value, 0x10 };          // Write
            byte[] writeApdu = Combine(header, content);
            txtApduSent.Text += BitConverter.ToString(writeApdu).Replace('-', ' ') + System.Environment.NewLine;

            CAPDU capdu = new CAPDU(writeApdu);                                                 // Command sent to the reader
            RAPDU rapdu = channel.Transmit(capdu);                                              // Response sent from card

            if (rapdu == null)
            {
                txtFinalStatus.Text = "Problem while writing";
                success             = false;
            }
            if (rapdu.SW != 0x9000)                                  // Something failed
            {
                txtFinalStatus.Text = "Error:" + String.Format("{0:X}", rapdu.SW) + ": " + SCARD.CardStatusWordsToString(rapdu.SW);
                success             = false;
            }

            if (success)
            {
                txtFinalStatus.Text = "Write with success";
            }
        }
Beispiel #12
0
        /// <summary>
        /// Callback used when the reader's status change
        /// As this method is called from a thread, you can't directly modify the user interface
        /// </summary>
        /// <param name="readerState"></param>
        /// <param name="cardAtr"></param>
        private void readerStatusChanged(uint readerState, CardBuffer cardAtr)
        {
            // When you are in a thread you can't directly modify the user interface
            if (InvokeRequired)
            {
                this.BeginInvoke(new readerStatusChangedInvoker(readerStatusChanged), readerState, cardAtr);

                return;
            }

            lblCardUid.Text  = "";
            lblCardAtr.Text  = "";
            lblCardType.Text = "";
            lblProtocol.Text = "";


            lblMonto.BackColor = Color.FromArgb(240, 240, 240);

            pctColor.BackColor = Color.FromArgb(255, 0, 0);


            //  pctFoto.Image = Properties.Resources.atleta;


            lblStatus.Text = SCARD.ReaderStatusToString(readerState);

            if (cardAtr != null)
            {
                lblCardAtr.Text = cardAtr.AsString(" ");
                channel         = new SCardChannel(reader);


                pctColor.BackColor = Color.FromArgb(121, 174, 235);

                //   pctFoto.Image = Properties.Resources.atleta;
                //    pctFoto.SizeMode = PictureBoxSizeMode.StretchImage;


                lblCedula.Text      = "Cédula: ";
                lblId.Text          = "Identificación: ";
                lblEstatus.Text     = "Cobrado: ";
                lblTotal.Text       = "Total: ";
                lblMonto.Text       = "Retraso: ";
                lblNombreNino.Text  = "";
                lblDeporteNino.Text = "";

                pctFoto.Image = Properties.Resources.atleta;
                pctCara.Image = Properties.Resources.blanco;

                if (!channel.Connect())
                {
                    lblCardUid.Text = "Error, no puede connectar la tarjeta";

                    return;
                }
                CAPDU capdu = new CAPDU(0xFF, 0xCA, 0x00, 0x00);    // Command sent to the reader
                RAPDU rapdu = channel.Transmit(capdu);              // Response sent from card

                if (connectCard())
                {
                    string cardUID = getcardUID();
                    lblCardUid.Text = cardUID;
                    lblUid2.Text    = cardUID;
                    lblST.Text      = "OK";
                    lblST.ForeColor = Color.FromArgb(34, 177, 76);



                    string aux = "Data Source=" + conexion.Fuente + ";Initial Catalog=" + conexion.Catalogo + ";User ID=" + conexion.User + ";Password="******"Select * from datos where brasalete =@zip";
                    try
                    {
                        SqlCommand cmd = new SqlCommand(consulta, conn);
                        cmd.Parameters.AddWithValue("@zip", cardUID);

                        conn.Open();
                        cmd.CommandType = CommandType.Text;
                        SqlDataReader dr = cmd.ExecuteReader();


                        while (dr.Read())
                        {
                            //   pctFoto.Image = Properties.Resources.lala;
                            pctFoto.Image    = new Bitmap(conexion.RutaImg + dr[1].ToString() + ".jpg");
                            pctFoto.SizeMode = PictureBoxSizeMode.StretchImage;

                            lblId.Text      = "Identificación: " + dr[0].ToString();
                            lblCedula.Text  = "Cédula: " + dr[1].ToString();
                            lblTotal.Text   = " Total: B/. " + dr[3].ToString();
                            lblEstatus.Text = "Cobrado:  B/. " + dr[4].ToString() + "  " + "Pendiente: B/. " + dr[5].ToString();

                            lblNombreNino.Text  = dr[0].ToString();
                            lblDeporteNino.Text = dr[2].ToString();

                            string monto = dr[10].ToString();
                            lblMonto.Text = "Retraso: " + monto + "  Meses";
                            switch (monto)
                            {
                            case "1":
                                // celeste
                                lblMonto.ForeColor = Color.FromArgb(121, 174, 235);
                                pctCara.Image      = Properties.Resources.caraFeliz;
                                break;

                            case "2":
                                // amarillo
                                lblMonto.BackColor = Color.FromArgb(255, 247, 0);
                                pctCara.Image      = Properties.Resources.caraTriste;
                                //  pctFoto.
                                break;

                            case "3":
                                // rojo
                                lblMonto.ForeColor = Color.FromArgb(255, 0, 0);
                                pctCara.Image      = Properties.Resources.llorando;
                                break;

                            // rojo
                            default:
                                lblMonto.ForeColor = Color.FromArgb(255, 0, 0);
                                pctCara.Image      = Properties.Resources.blanco;
                                break;
                            }


                            dr.GetString(1);
                            break;
                        }


                        dr.Close();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open)
                        {
                            conn.Close();
                        }
                    }
                }//fin de connect card()
                 //----------------------------------------------------------------



                /*
                 *
                 * if (rapdu.SW != 0x9000)                              // Something failed
                 * {
                 *  lblCardUid.Text = "Get UID APDU fallido!";
                 *  return;
                 * }
                 */
                // Display card's UID formated as an hexadecimal string

                /*
                 * byte[] rapduB = rapdu.data.GetBytes();
                 * string hexadecimalResult = BitConverter.ToString(rapduB);
                 *
                 * lblCardUid.Text = hexadecimalResult.Replace("-", " ");
                 */


                // Card's type
                if (cardAtr.Length >= 19)
                {
                    // Card's protocol
                    int cardProdocol = cardAtr[12];
                    if (protocols.ContainsKey(cardProdocol))
                    {
                        lblProtocol.Text = protocols[cardProdocol];
                    }

                    if (cardAtr[4] == 0x80 && cardAtr[5] == 0x4F && cardAtr[6] == 0x0C && cardAtr[7] == 0xA0 && cardAtr[8] == 0x00 && cardAtr[9] == 0x00 && cardAtr[10] == 0x03 && cardAtr[11] == 0x06)
                    {
                        string pixSs = cardAtr[13].ToString("X2") + cardAtr[14].ToString("X2");
                        lblCardType.Text = "Wired-logic: ";
                        if (cardsNames.ContainsKey(pixSs))
                        {
                            lblCardType.Text += cardsNames[pixSs];
                        }
                    }
                    else
                    {
                        lblCardType.Text = "Desconocido tipo de tarjeta";
                    }
                }
                else
                {
                    lblCardType.Text = "Smartcard";
                }
            }
        }
Beispiel #13
0
        void ReaderStatusChanged(uint ReaderState, CardBuffer CardAtr)
        {
            /* The ReaderStatusChanged function is called as a delegate (callback) by the SCardReader object    */
            /* within its backgroung thread. Therefore we must use the BeginInvoke syntax to switch back from   */
            /* the context of the background thread to the context of the application's main thread. Overwise   */
            /* we'll get a security violation when trying to access the window's visual components (that belong */
            /* to the application's main thread and can't be safely manipulated by background threads).         */
            if (InvokeRequired)
            {
                this.BeginInvoke(new ReaderStatusChangedInvoker(ReaderStatusChanged), ReaderState, CardAtr);
                return;
            }

            string s = SCARD.ReaderStatusToString(ReaderState);

            Trace.WriteLine("ReaderStatusChanged: " + s);
            eReaderStatus.Text = s;

            if (CardAtr != null)
            {
                s             = CardAtr.AsString(" ");
                eCardAtr.Text = s;
                Trace.WriteLine("\t" + s);
            }
            else
            {
                eCardAtr.Text = "";
            }

            if (ReaderState == SCARD.STATE_UNAWARE)
            {
                if (llcp != null)
                {
                    llcp.Stop();
                    llcp = null;
                }

                MessageBox.Show("The reader we were working on has disappeared from the system. This application will terminate now.",
                                "The reader has been removed",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Exclamation);

                Application.Exit();
            }
            else if ((ReaderState & SCARD.STATE_UNAVAILABLE) != 0)
            {
                Trace.WriteLine("Card unavailable");
            }
            else if ((ReaderState & SCARD.STATE_MUTE) != 0)
            {
                Trace.WriteLine("Card mute");
            }
            else if ((ReaderState & SCARD.STATE_INUSE) != 0)
            {
                Trace.WriteLine("Card in use");
            }
            else if ((ReaderState & SCARD.STATE_EMPTY) != 0)
            {
                Trace.WriteLine("Card absent");

                if (llcp != null)
                {
                    Trace.WriteLine("*** Target lost ***");

                    llcp.Stop();
                    llcp = null;

                    PerformStatusChange();
                }

                pControl.Enabled = true;
            }
            else if ((ReaderState & SCARD.STATE_PRESENT) != 0)
            {
                Trace.WriteLine("Card present");

                if ((llcp == null) && ((ReaderState & SCARD.STATE_INUSE) == 0))
                {
                    /* Try to work with this card, it may be a LLCP peer */
                    reader.StopMonitor();

                    pControl.Enabled = false;
                    Trace.WriteLine("*** Starting LLCP ***");

                    llcp = new LlcpInitiator(reader);

                    switch (status)
                    {
                    case Status.SendAndRecv_Ready:
                        /* We are both a receiver and a sender */
                        StartReceiver();
                        StartSender();
                        break;

                    case Status.RecvOnly_Ready:
                    case Status.RecvThenSend_RecvReady:
                    case Status.SendThenRecv_RecvReady:
                        /* We are a receiver */
                        StartReceiver();
                        break;

                    case Status.SendOnly_Ready:
                    case Status.RecvThenSend_SendReady:
                    case Status.SendThenRecv_SendReady:
                        /* We are a sender */
                        StartSender();
                        break;

                    default:
                        break;
                    }

                    if (!llcp.Start())
                    {
                        Trace.WriteLine("*** Failed to start LLCP ***");
                        MessageBox.Show("Failed to instantiate LLCP layer on the newly inserted card. This is likely to be caused by another application taking control of the card before us. Please close all other PC/SC-aware applications. It may also be required to instruct Windows to stop probing the card, see the paragraph pcsc_no_minidriver in the Readme of the PC/SC SDK for more informations.", "Communication error");
                        llcp             = null;
                        pControl.Enabled = true;
                    }

                    reader.StartMonitor(new SCardReader.StatusChangeCallback(ReaderStatusChanged));
                }
            }
        }
Beispiel #14
0
        void ReaderStatusChanged(uint ReaderState, CardBuffer CardAtr)
        {
            /* The ReaderStatusChanged function is called as a delegate (callback) by the SCardReader object    */
            /* within its backgroung thread. Therefore we must use the BeginInvoke syntax to switch back from   */
            /* the context of the background thread to the context of the application's main thread. Overwise   */
            /* we'll get a security violation when trying to access the window's visual components (that belong */
            /* to the application's main thread and can't be safely manipulated by background threads).         */
            if (InvokeRequired)
            {
                this.BeginInvoke(new ReaderStatusChangedInvoker(ReaderStatusChanged), ReaderState, CardAtr);
                return;
            }

            eReaderStatus.Text = SCARD.ReaderStatusToString(ReaderState);

            if (CardAtr != null)
            {
                eCardAtr.Text = CardAtr.AsString(" ");
            }
            else
            {
                eCardAtr.Text = "";
            }

            if (ReaderState == SCARD.STATE_UNAWARE)
            {
                if (cardchannel != null)
                {
                    cardchannel.Disconnect();
                    cardchannel = null;
                }

                MessageBox.Show("The reader we were working on has disappeared from the system. This application will terminate now.",
                                "The reader has been removed",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Exclamation);

                Application.Exit();
            }
            else
            if ((ReaderState & SCARD.STATE_EMPTY) != 0)
            {
                Trace.WriteLine("Reader: EMPTY");

                if (cardchannel != null)
                {
                    cardchannel.Disconnect();
                    cardchannel = null;
                }

                /* No card -> leave edit mode */
                setEditable(false);
            }
            else
            if ((ReaderState & SCARD.STATE_UNAVAILABLE) != 0)
            {
                Trace.WriteLine("Reader: UNAVAILABLE");
            }
            else
            if ((ReaderState & SCARD.STATE_MUTE) != 0)
            {
                Trace.WriteLine("Reader: MUTE");
            }
            else
            if ((ReaderState & SCARD.STATE_INUSE) != 0)
            {
                Trace.WriteLine("Reader: INUSE");
            }
            else
            if ((ReaderState & SCARD.STATE_PRESENT) != 0)
            {
                Trace.WriteLine("Reader: PRESENT");

                if (cardchannel == null)
                {
                    /* New card -> leave edit mode */
                    setEditable(false);

                    cardchannel = new SCardChannel(reader);

                    if (cardchannel.Connect())
                    {
                        Trace.WriteLine("Connected to the card");
                        cardthread = new Thread(card_read_proc);
                        cardthread.Start();
                    }
                    else
                    {
                        Trace.WriteLine("Connection to the card failed");

                        MessageBox.Show("NfcTool failed to connect to the card in the reader. Check that you don't have another application running in background that tries to work with the smartcards in the same time as NFCTool");

                        cardchannel = null;
                    }
                }


                //card_status = 1;
            }
        }
Beispiel #15
0
        void UpdateReaderState(string ReaderName, uint ReaderState, CardBuffer CardAtr)
        {
            Console.WriteLine(DateTime.Now.ToString(NowFormat));
            Console.WriteLine("\tStatus changed for '" + ReaderName + "'");
            Console.WriteLine("\t\tState: " + SCARD.ReaderStatusToString(ReaderState));
            if (CardAtr != null)
            {
                Console.WriteLine("\t\tATR: " + CardAtr.AsString(" "));
            }

            for (int i = 0; i < lvReaders.Items.Count; i++)
            {
                EXImageListViewItem item = (EXImageListViewItem)lvReaders.Items[i];

                if (item.MyValue.Equals(ReaderName))
                {
                    /* Reader found in the list */
                    /* ------------------------ */

                    lvReaders.BeginUpdate();

                    /* Set status image */
                    int    statusImage;
                    string statusText;

                    if ((ReaderState & SCARD.STATE_PRESENT) != 0)
                    {
                        /* Card is present */
                        if ((ReaderState & SCARD.STATE_INUSE) != 0)
                        {
                            /* Card in use */
                            if ((ReaderState & SCARD.STATE_EXCLUSIVE) != 0)
                            {
                                /* Card in exclusive use */
                                statusText  = "In use (exclusive)";
                                statusImage = StatusImageExclusive;
                            }
                            else
                            {
                                statusText  = "In use (shared)";
                                statusImage = StatusImageInUse;
                            }
                        }
                        else
                        if ((ReaderState & SCARD.STATE_MUTE) != 0)
                        {
                            /* Card is mute */
                            statusText  = "Mute";
                            statusImage = StatusImageMute;
                        }
                        else
                        if ((ReaderState & SCARD.STATE_UNPOWERED) != 0)
                        {
                            /* Card is not powered */
                            statusText  = "Present, not powered";
                            statusImage = StatusImagePresent;
                        }
                        else
                        {
                            /* Card is powered */
                            statusText  = "Present, powered";
                            statusImage = StatusImagePresent;
                        }
                    }
                    else
                    if ((ReaderState & SCARD.STATE_UNAVAILABLE) != 0)
                    {
                        /* Problem */
                        statusText  = "Reserved (direct)";
                        statusImage = StatusImageUnavailable;
                    }
                    else
                    if ((ReaderState & SCARD.STATE_IGNORE) != 0)
                    {
                        /* Problem */
                        statusText  = "Error (ignore)";
                        statusImage = StatusImageError;
                    }
                    else
                    if ((ReaderState & SCARD.STATE_UNKNOWN) != 0)
                    {
                        /* Problem */
                        statusText  = "Error (status unknown)";
                        statusImage = StatusImageUnknown;
                    }
                    else
                    if ((ReaderState & SCARD.STATE_EMPTY) != 0)
                    {
                        /* No card */
                        statusText  = "Absent";
                        statusImage = StatusImageAbsent;
                    }
                    else
                    {
                        /* Problem */
                        statusText  = "Bad status";
                        statusImage = StatusImageError;
                    }

                    EXImageListViewSubItem subitem = (EXImageListViewSubItem)item.SubItems[2];
                    subitem.MyImage       = statusImages.Images[statusImage];
                    item.SubItems[3].Text = statusText;

                    if (CardAtr != null)
                    {
                        item.SubItems[4].Text = CardAtr.AsString("");
                    }
                    else
                    {
                        item.SubItems[4].Text = "";
                    }

                    lvReaders.EndUpdate();
                    break;
                }
                /* NB : we ignore the event in case the reader is not already listed */
            }
        }
Beispiel #16
0
        void MiATRRegistryClick(object sender, EventArgs e)
        {
            EXImageListViewItem item = (EXImageListViewItem)lvReaders.SelectedItems[0];

            if (!item.SubItems[4].Text.Equals(""))
            {
                IntPtr hContext    = IntPtr.Zero;
                uint   _last_error = SCARD.EstablishContext(SCARD.SCOPE_SYSTEM, IntPtr.Zero, IntPtr.Zero, ref hContext);
                if (_last_error != SCARD.S_SUCCESS)
                {
                    MessageBox.Show("Error: can't establish context");
                    return;
                }

                CardBuffer cardBufAtr = new CardBuffer(item.SubItems[4].Text);
                byte[]     atr        = cardBufAtr.GetBytes();

                byte[] dummy    = new byte[99];
                int    cchCards = -1;              /*	SCARD_AUTOALLOCATE */
                _last_error = SCardListCards(hContext,
                                             atr,
                                             null,
                                             0,
                                             null,
                                             ref cchCards);

                if (_last_error != SCARD.S_SUCCESS)
                {
                    MessageBox.Show("Error: can't list cards");
                    return;
                }

                if (cchCards <= 1)
                {
                    /* Card not found. We need to add it. */
                    string name = "NoMinidriver-" + item.SubItems[4].Text;
                    _last_error = SCardIntroduceCardType(hContext,
                                                         name,
                                                         null,
                                                         null,
                                                         0,
                                                         atr,
                                                         null,
                                                         (uint)atr.Length);

                    if (_last_error != SCARD.S_SUCCESS)
                    {
                        MessageBox.Show("Error: can't introduce card type");
                        return;
                    }

                    _last_error = SCardSetCardTypeProviderName(hContext,
                                                               name,
                                                               2, /*	SCARD_PROVIDER_CSP	*/
                                                               "$DisableSCPnP$");

                    if (_last_error != SCARD.S_SUCCESS)
                    {
                        MessageBox.Show("Error: can't set card type provider name");
                        return;
                    }
                    else
                    {
                        MessageBox.Show("This card will now be recognized by the system");
                    }
                }
                else
                {
                    MessageBox.Show("This card is already recognized by the system");
                }
            }
        }
        void ReaderStatusChanged(uint readerState, CardBuffer cardAtr)
        {
            try
            {
                string msg;

                if (InvokeRequired)
                {
                    this.BeginInvoke(new ReaderStatusChangedInvoker(ReaderStatusChanged), readerState, cardAtr);
                    return;
                }

                SCARD.ReaderStatusToString(readerState);

                if (cardAtr != null)
                {
                    _tagATR = RemoveSpaces(cardAtr.AsString(" "));
                }
                else
                {
                }

                if (readerState == SCARD.STATE_UNAWARE)
                {
                    if (_cardchannel != null)
                    {
                        _cardchannel.Disconnect();
                        _cardchannel = null;
                    }

                    if (!_inLoop)
                    {
                        msg = "The reader we were working with has gone AWOL from the system.";
                        WarnNotify("Warning|" + msg);
                        _inLoop = true;
                    }

                    tmrTagMonitor.Enabled = true;
                    msg = "Reader not Found. Please check Connection.";
                    WarnNotify("Warning|" + msg);
                }
                else if ((readerState & SCARD.STATE_EMPTY) != 0)
                {
                    _tag    = null;
                    _inLoop = false;

                    _studentData = null;
                    InfoNotify("Ready|Please insert another card for Tagging");

                    if (_cardchannel == null)
                    {
                        return;
                    }

                    _cardchannel.Disconnect();
                    _cardchannel = null;
                }
                else if ((readerState & SCARD.STATE_UNAVAILABLE) != 0)
                {
                }
                else if ((readerState & SCARD.STATE_MUTE) != 0)
                {
                }
                else if ((readerState & SCARD.STATE_INUSE) != 0)
                {
                    _inLoop = false;
                }
                else if ((readerState & SCARD.STATE_PRESENT) != 0)
                {
                    _inLoop = false;
                    if (_cardchannel != null)
                    {
                        return;
                    }

                    _cardchannel = new SCardChannel(DeviceManager.DeviceSpec.Nfc);

                    if (_cardchannel.Connect())
                    {
                        var mifare = new MiFareCardProg();
                        _tagUID = RemoveSpaces(mifare.GetUID(DeviceManager.DeviceSpec.Nfc).Trim());

                        _cardthread = new Thread(card_read_proc);
                        _cardthread.Start();
                    }
                    else
                    {
                        msg =
                            "NearField failed to connect to the card in the reader. Check that you don't have another application running in background that tries to work with the smartcards in the same time as NearField";
                        WarnNotify("Warning|" + msg);
                        _cardchannel = null;
                    }
                }
            }
            catch (Exception ex)
            {
                ErrorHandler.TreatError(ex);
            }
        }
        private static bool SelectFile(SCardChannel channel, ushort file_id)
        {
            CAPDU capdu = new CAPDU(0x00, 0xA4, 0x00, 0x0C, (new CardBuffer(file_id)).GetBytes());

            Trace.WriteLine("< " + capdu.AsString(" "));

            RAPDU rapdu = channel.Transmit(capdu);

            if (rapdu == null)
            {
                Trace.WriteLine("SelectFile " + String.Format("{0:X4}", file_id) + " error " + channel.LastError + " (" + channel.LastErrorAsString + ")");
                return(false);
            }

            Trace.WriteLine("> " + rapdu.AsString(" "));

            if (rapdu.SW != 0x9000)
            {
                Trace.WriteLine("SelectFile " + String.Format("{0:X4}", file_id) + " failed " + rapdu.SWString + " (" + SCARD.CardStatusWordsToString(rapdu.SW) + ")");
                return(false);
            }

            return(true);
        }
        private static bool SelectRootApplication(SCardChannel channel)
        {
            CAPDU capdu = new CAPDU(0x00, 0xA4, 0x00, 0x00, "3F00");

            Trace.WriteLine("< " + capdu.AsString(" "));

            RAPDU rapdu = channel.Transmit(capdu);

            if (rapdu == null)
            {
                Trace.WriteLine("SelectRootApplication error " + channel.LastError + " (" + channel.LastErrorAsString + ")");
                return(false);
            }

            Trace.WriteLine("> " + rapdu.AsString(" "));

            if (rapdu.SW != 0x9000)
            {
                Trace.WriteLine("SelectRootApplication failed " + rapdu.SWString + " (" + SCARD.CardStatusWordsToString(rapdu.SW) + ")");
                return(false);
            }

            return(true);
        }
Beispiel #20
0
        /// <summary>
        /// Callback used when the reader's status change
        /// As this method is called from a thread, you can't directly modify the user interface
        /// </summary>
        /// <param name="readerState"></param>
        /// <param name="cardAtr"></param>
        private void readerStatusChanged(uint readerState, CardBuffer cardAtr)
        {
            // When you are in a thread you can't directly modify the user interface
            if (InvokeRequired)
            {
                this.BeginInvoke(new readerStatusChangedInvoker(readerStatusChanged), readerState, cardAtr);
                return;
            }

            lblCardUid.Text  = "";
            lblCardAtr.Text  = "";
            lblCardType.Text = "";
            lblProtocol.Text = "";
            lblStatus.Text   = SCARD.ReaderStatusToString(readerState);

            if (cardAtr != null)
            {
                lblCardAtr.Text = cardAtr.AsString(" ");
                channel         = new SCardChannel(reader);
                if (!channel.Connect())
                {
                    lblCardUid.Text = "Error, can't connect to the card";
                    return;
                }
                CAPDU capdu = new CAPDU(0xFF, 0xCA, 0x00, 0x00);    // Command sent to the reader
                RAPDU rapdu = channel.Transmit(capdu);              // Response sent from card
                if (rapdu.SW != 0x9000)                             // Something failed
                {
                    lblCardUid.Text = "Get UID APDU failed!";
                    return;
                }
                // Display card's UID formated as an hexadecimal string
                byte[] rapduB            = rapdu.data.GetBytes();
                string hexadecimalResult = BitConverter.ToString(rapduB);
                lblCardUid.Text = hexadecimalResult.Replace("-", " ");

                // Card's type
                if (cardAtr.Length >= 19)
                {
                    // Card's protocol
                    int cardProdocol = cardAtr[12];
                    if (protocols.ContainsKey(cardProdocol))
                    {
                        lblProtocol.Text = protocols[cardProdocol];
                    }

                    if (cardAtr[4] == 0x80 && cardAtr[5] == 0x4F && cardAtr[6] == 0x0C && cardAtr[7] == 0xA0 && cardAtr[8] == 0x00 && cardAtr[9] == 0x00 && cardAtr[10] == 0x03 && cardAtr[11] == 0x06)
                    {
                        string pixSs = cardAtr[13].ToString("X2") + cardAtr[14].ToString("X2");
                        lblCardType.Text = "Wired-logic: ";
                        if (cardsNames.ContainsKey(pixSs))
                        {
                            lblCardType.Text += cardsNames[pixSs];
                        }
                    }
                    else
                    {
                        lblCardType.Text = "Unknow card type";
                    }
                }
                else
                {
                    lblCardType.Text = "Smartcard";
                }
            }
        }
Beispiel #21
0
        private void btnWrite_Click(object sender, EventArgs e)
        {
            string dataToWrite = txtData.Text.Trim().PadRight(47) + (char)0x00;
            int    page        = 4;
            bool   success     = true;

            txtDataSent.Text = "";

            foreach (var chunck in ChunksUpto(dataToWrite, 4))
            {
                txtDataSent.Text += chunck + System.Environment.NewLine;
                byte[] content   = Encoding.ASCII.GetBytes(chunck.PadRight(4, ' '));
                byte[] header    = new byte[] { 0xFF, 0xD6, 0x00, (byte)page, 0x04 }; // Update Binary
                byte[] writeApdu = Combine(header, content);
                txtApduSent.Text += BitConverter.ToString(writeApdu).Replace('-', ' ') + System.Environment.NewLine;

                CAPDU capdu = new CAPDU(writeApdu);                         // Command sent to the reader
                RAPDU rapdu = channel.Transmit(capdu);                      // Response sent from card
                if (rapdu == null)
                {
                    txtFinalStatus.Text = "Problem while writing";
                    success             = false;
                    break;
                }
                if (rapdu.SW != 0x9000)                                  // Something failed
                {
                    txtFinalStatus.Text = "Error:" + String.Format("{0:X}", rapdu.SW) + ": " + SCARD.CardStatusWordsToString(rapdu.SW);
                    success             = false;
                    break;
                }
                page++;
            }

            if (success)
            {
                txtFinalStatus.Text = "Write with success";
            }
        }