Пример #1
0
        protected byte[] ReadUID()
        {
            CAPDU capdu = new CAPDU(0xFF, 0xCA, 0x00, 0x00, 0x00);

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

            RAPDU rapdu = null;

            rapdu = Channel.Transmit(capdu);

            if (rapdu == null)
            {
                Logger.Trace("Error '" + Channel.LastErrorAsString + "' while reading the UID");
                return(null);
            }

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

            if (rapdu.SW != 0x9000)
            {
                Logger.Trace("Bad status word " + rapdu.SWString + " while reading the UID");
                return(null);
            }

            if (!rapdu.hasData)
            {
                Logger.Trace("Empty response");
                return(null);
            }

            return(rapdu.data.GetBytes());
        }
Пример #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;
            }
        }
Пример #3
0
        protected static byte[] GetUid(SCardChannel channel)
        {
            CAPDU capdu = new CAPDU(0xFF, 0xCA, 0x00, 0x00, 0x00);

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

            RAPDU rapdu = null;

            rapdu = channel.Transmit(capdu);
            if (rapdu == null)
            {
                Trace.WriteLine("Error '" + channel.LastErrorAsString + "' while getting the card's UID");
                return(null);
            }

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

            if (rapdu.SW != 0x9000)
            {
                Trace.WriteLine("Bad status word " + rapdu.SWString + " while getting the card's UID");
                return(null);
            }

            if (!rapdu.hasData)
            {
                Trace.WriteLine("Empty response");
                return(null);
            }

            return(rapdu.data.GetBytes());
        }
Пример #4
0
        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);
        }
Пример #5
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";
            }
        }
Пример #6
0
        public void _0003_EF()
        {
            //Connect to 'OMNIKEY CardMan 3x21 0', share=2, protocol=1
            //Transmit << 00A4040C06FF544143484F
            //Transmit >> 9000
            //Transmit << 00A4020C020520
            //Transmit >> 9000
            //Disconnect, disposition=1
            //AID: ¡FF 54 41 43 48 4F¡ for the Tachograph application
            //MF->DF
            //    ->EF
            //    ->EF
            byte[] command = new byte[] { 0x00, 0xA4, 0x04, 0x0C, 0x06, 0xFF, 0x54, 0x41, 0x43, 0x48, 0x4F };
            CAPDU  capdu   = new CAPDU(command);
            RAPDU  rapdu   = SmartCard.Transmit(capdu);

            byte[] answer = rapdu.GetBytes();
            Assert.AreEqual(new byte[] { 0x90, 0x00 }, answer, "Válasz nem az amit vártam...");

            //Select EF Identification
            command = new byte[] { 0x00, 0xA4, 0x02, 0x0C, 0x02, 0x05, 0x20 };
            capdu   = new CAPDU(command);
            rapdu   = SmartCard.Transmit(capdu);
            answer  = rapdu.GetBytes();
            Assert.AreEqual(new byte[] { 0x90, 0x00 }, answer, "Válasz nem az amit vártam...");
        }
Пример #7
0
        protected override LLCP_PDU Exchange(LLCP_PDU send_pdu)
        {
            Trace.WriteLine("< " + send_pdu.AsString());

            CAPDU capdu = new CAPDU(0xFF, 0xFE, 0x00, 0x00, send_pdu.GetBytes());

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

            RAPDU rapdu = _channel.Transmit(capdu);

            if (rapdu == null)
            {
                return(null);
            }

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

            if (rapdu.SW != 0x9000)
            {
                return(null);
            }

            LLCP_PDU recv_pdu = new LLCP_PDU(rapdu.data);

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

            return(recv_pdu);
        }
        /// <summary>
        /// Write all sectors to the card
        /// </summary>
        /// <param name="address"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        private bool MifareClassicWrite(ushort address, byte[] data, byte[] key)
        {
            if (data == null)
            {
                return(false);
            }

            CAPDU capdu = null;

            if (key == null)
            {
                capdu = new CAPDU(0xFF, 0xF4, (byte)(address / 0x0100), (byte)(address % 0x0100), data);
            }
            else
            {
                // Concat data and key
                byte[] sentData = new byte[data.Length + key.Length];
                System.Buffer.BlockCopy(data, 0, sentData, 0, data.Length);
                System.Buffer.BlockCopy(key, 0, sentData, data.Length, key.Length);
                capdu = new CAPDU(0xFF, 0xF4, (byte)(address / 0x0100), (byte)(address % 0x0100), sentData);
            }

            Logger.Trace("< " + capdu.AsString(" ") + " (writing without specified key)");

            RAPDU rapdu = null;

            for (int retry = 0; retry < 4; retry++)
            {
                rapdu = Channel.Transmit(capdu);
                if (rapdu == null)
                {
                    break;
                }
                if ((rapdu.SW != 0x6F01) && (rapdu.SW != 0x6F02) && (rapdu.SW != 0x6F0B))
                {
                    break;
                }

                Thread.Sleep(15);
            }

            if (rapdu == null)
            {
                Logger.Trace("Error '" + Channel.LastErrorAsString + "' while writing the card");
                return(false);
            }

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

            if (rapdu.SW != 0x9000)
            {
                Logger.Trace("Bad status word " + rapdu.SWString + " while writing the card");
                return(false);
            }

            return(true);
        }
        public static bool IsDesfireEV1(SCardChannel channel)
        {
            bool is_desfire_ev1 = false;

            CAPDU capdu = new CAPDU(0x90, 0x60, 0x00, 0x00, 0x00);

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

            RAPDU rapdu = channel.Transmit(capdu);

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

            if (rapdu.SW != 0x91AF)
            {
                Trace.WriteLine("Desfire GetVersion function failed");
                return(false);
            }

            if (rapdu.GetByte(3) > 0)
            {
                Trace.WriteLine("This is a Desfire EV1");
                is_desfire_ev1 = true;
            }
            else
            {
                Trace.WriteLine("This is a Desfire EV0");
            }

            capdu = new CAPDU(0x90, 0xAF, 0x00, 0x00, 0x00);

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

            rapdu = channel.Transmit(capdu);
            Trace.WriteLine("> " + rapdu.AsString(" "));

            if (rapdu.SW != 0x91AF)
            {
                Trace.WriteLine("Desfire GetVersion(2) function failed");
                return(false);
            }

            capdu = new CAPDU(0x90, 0xAF, 0x00, 0x00, 0x00);

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

            rapdu = channel.Transmit(capdu);
            Trace.WriteLine("> " + rapdu.AsString(" "));

            if (rapdu.SW != 0x9100)
            {
                Trace.WriteLine("Desfire GetVersion(3) function failed");
                return(false);
            }

            return(is_desfire_ev1);
        }
Пример #10
0
        public void _0002_Connect_To_DriverCard()
        {
            //00 A4 02 0C 02 00 02
            //SELECT FILE
            byte[] command = new byte[] { 0x00, 0xA4, 0x02, 0x0C, 0x02, 0x00, 02 };
            CAPDU  capdu   = new CAPDU(command);
            RAPDU  rapdu   = SmartCard.Transmit(capdu);

            byte[] answer = rapdu.GetBytes();
            Assert.AreEqual(new byte[] { 0x90, 0x00 }, answer, "Válasz nem az amit vártam...");
        }
Пример #11
0
        private bool WriteBinaryWithKey(ushort address, byte[] data, byte[] key)
        {
            if (data == null)
            {
                return(false);
            }

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

            byte[] dataAndKey = new byte[data.Length + key.Length];
            Array.ConstrainedCopy(data, 0, dataAndKey, 0, data.Length);
            Array.ConstrainedCopy(key, 0, dataAndKey, data.Length, key.Length);

            CAPDU capdu = new CAPDU(0xFF, 0xF4, (byte)(address / 0x0100), (byte)(address % 0x0100), dataAndKey);

            log("< " + capdu.AsString(" ") + " (writing with specified key)");

            RAPDU rapdu = null;

            for (int retry = 0; retry < 4; retry++)
            {
                rapdu = _channel.Transmit(capdu);

                if (rapdu == null)
                {
                    break;
                }
                if ((rapdu.SW != 0x6F01) && (rapdu.SW != 0x6F02) && (rapdu.SW != 0x6F0B))
                {
                    break;
                }

                Thread.Sleep(15);
            }

            if (rapdu == null)
            {
                log("Error '" + _channel.LastErrorAsString + "' while writing the card");
                return(false);
            }

            log("> " + rapdu.AsString(" "));

            if (rapdu.SW != 0x9000)
            {
                log("Bad status word " + rapdu.SWString + " while writing the card");
                return(false);
            }

            return(true);
        }
Пример #12
0
        protected static bool WriteBinary(SCardChannel channel, ushort address, byte[] data)
        {
            if (data == null)
            {
                return(false);
            }

            if (data.Length != 4)
            {
                Trace.WriteLine("Type 2 Tag: Write Binary accepts only 4B");
                return(false);
            }

            CAPDU capdu = new CAPDU(0xFF, 0xD6, (byte)(address / 0x0100), (byte)(address % 0x0100), data);

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


            RAPDU rapdu = null;

            for (int retry = 0; retry < 4; retry++)
            {
                rapdu = channel.Transmit(capdu);

                if (rapdu == null)
                {
                    break;
                }
                if ((rapdu.SW != 0x6F01) && (rapdu.SW != 0x6F02) && (rapdu.SW != 0x6F0B))
                {
                    break;
                }

                Thread.Sleep(15);
            }

            if (rapdu == null)
            {
                Trace.WriteLine("Error '" + channel.LastErrorAsString + "' while writing the card");
                return(false);
            }

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

            if (rapdu.SW != 0x9000)
            {
                Trace.WriteLine("Bad status word " + rapdu.SWString + " while writing the card");
                return(false);
            }


            return(true);
        }
Пример #13
0
        private bool ReadBinaryWithKey(byte[] key)
        {
            int  address;
            byte length;

            GetSectorData(out address, out length);

            CAPDU capdu = new CAPDU(0xFF, 0xF3, (byte)(address / 0x0100), (byte)(address % 0x0100), key, length);

            Logger.Trace("< " + capdu.AsString(" ") + " (reading with specified key)");

            RAPDU rapdu = null;

            for (int retry = 0; retry < 4; retry++)
            {
                rapdu = Card.Channel.Transmit(capdu);

                if (rapdu == null)
                {
                    break;
                }
                if ((rapdu.SW != 0x6F01) && (rapdu.SW != 0x6F02) && (rapdu.SW != 0x6F0B))
                {
                    break;
                }

                Thread.Sleep(10);
            }

            if (rapdu == null)
            {
                Logger.Trace("Error '" + Card.Channel.LastErrorAsString + "' while reading the card");
                return(false);
            }

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

            if (rapdu.SW != 0x9000)
            {
                Logger.Trace("Bad status word " + rapdu.SWString + " while reading the card");
                return(false);
            }

            if (!rapdu.hasData)
            {
                Logger.Trace("Empty response");
                return(false);
            }

            Data = rapdu.data.GetBytes();
            PopulateBlocks();
            return(true);
        }
        protected byte[] MifareClassicRead(ushort blockNumber, byte dataLength = 0, byte[] keyValue = null)
        {
            CAPDU capdu = null;

            if (keyValue == null)               // Reading without key
            {
                capdu = new CAPDU(0xFF, 0xF3, (byte)(blockNumber / 0x0100), (byte)(blockNumber % 0x0100), dataLength);
            }
            else                                                // Reading with key
            {
                capdu = new CAPDU(0xFF, 0xF3, (byte)(blockNumber / 0x0100), (byte)(blockNumber % 0x0100), keyValue, dataLength);
            }

            Logger.Trace("< " + capdu.AsString(" "));
            RAPDU rapdu = null;

            for (int retry = 0; retry < 4; retry++)
            {
                rapdu = Channel.Transmit(capdu);

                if (rapdu == null)
                {
                    break;
                }
                if ((rapdu.SW != 0x6F01) && (rapdu.SW != 0x6F02) && (rapdu.SW != 0x6F0B))
                {
                    break;
                }
            }

            if (rapdu == null)
            {
                Logger.Trace("Error '" + Channel.LastErrorAsString + "' while reading the card");
                return(null);
            }

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

            if (rapdu.SW != 0x9000)
            {
                Logger.Trace("Bad status word " + rapdu.SWString + " while reading the card");
                return(null);
            }

            if (!rapdu.hasData)
            {
                Logger.Trace("Empty response");
                return(null);
            }
            return(rapdu.data.GetBytes());
        }
Пример #15
0
        public void _0005_GetChallange()
        {
            RAPDU rapdu = null;

            byte[] command;


            command = new byte[] { 0x00, 0x84, 0x00, 0x00, 0x08, 0x00, 0x57, 0x03, 0x8C, 0xE9, 0x9A, 0xFC, 0x38,
                                   0x00, 0x00, 0x17, 0x36, 0x12, 0x10, 0x06, 0xA2, 0x00, 0x00, 0x00, 0xF0, 0xE7,
                                   0xBE, 0x3E, 0xFD, 0x7F, 0x00, 0x00, 0x70, 0x06, 0x88, 0xC6, 0x74, 0x7F, 0x00, 0x00, 0xEF };
            rapdu = SmartCard.Transmit(new CAPDU(command));

            // SELECT FILE tachograf application
            byte[] cmd1 = new byte[] { 0x00, 0xA4, 0x04, 0x0C, 0x06, 0xFF, 0x54, 0x41, 0x43, 0x48, 0x4F };
            rapdu = SmartCard.Transmit(new CAPDU(cmd1));
            Assert.AreEqual(new byte[] { 0x90, 0x00 }, rapdu.GetBytes(), "Válasz nem az amit vártam...");
        }
Пример #16
0
        protected static byte[] ReadBinary(SCardChannel channel, ushort address, byte length)
        {
            CAPDU capdu = new CAPDU(0xFF, 0xB0, (byte)(address / 0x0100), (byte)(address % 0x0100), length);

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

            RAPDU rapdu = null;

            for (int retry = 0; retry < 4; retry++)
            {
                rapdu = channel.Transmit(capdu);

                if (rapdu == null)
                {
                    break;
                }
                if ((rapdu.SW != 0x6F01) && (rapdu.SW != 0x6F02) && (rapdu.SW != 0x6F0B))
                {
                    break;
                }

                Thread.Sleep(10);
            }

            if (rapdu == null)
            {
                Trace.WriteLine("Error '" + channel.LastErrorAsString + "' while reading the card");
                return(null);
            }

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

            if (rapdu.SW != 0x9000)
            {
                Trace.WriteLine("Bad status word " + rapdu.SWString + " while reading the card");
                return(null);
            }

            if (!rapdu.hasData)
            {
                Trace.WriteLine("Empty response");
                return(null);
            }

            return(rapdu.data.GetBytes());
        }
Пример #17
0
        /*
         * SCardTransmit
         * ------------
         */
        void BtnTransmitClick(object sender, EventArgs e)
        {
            RApduClear();

            DynamicByteProvider p;

            byte[] b;

            p = (DynamicByteProvider)hexBoxCApdu.ByteProvider;

            b = new byte[p.Length];

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

            CAPDU capdu = new CAPDU(b);

            Settings.HistoryTransmit.Add(capdu.AsString());
            hist_apdu_idx = -1;

            RAPDU rapdu = channel.Transmit(capdu);

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

                b = rapdu.GetBytes();

                p = new DynamicByteProvider(b);

                hexBoxRApdu.ByteProvider = p;

                eStatusWord.Text        = String.Format("{0:X2} {1:X2}", rapdu.SW1, rapdu.SW2);
                eStatusWordExplain.Text = rapdu.SWString;

                hexBoxRApdu.BackColor        = hexBoxCApdu.BackColor;
                eStatusWord.BackColor        = eCardAtr.BackColor;
                eStatusWordExplain.BackColor = eCardAtr.BackColor;
            }
        }
Пример #18
0
        public void _0004_Read_EF_Identification()
        {
            RAPDU rapdu = null;

            // SELECT FILE tachograf application
            byte[] cmd1 = new byte[] { 0x00, 0xA4, 0x04, 0x0C, 0x06, 0xFF, 0x54, 0x41, 0x43, 0x48, 0x4F };
            rapdu = SmartCard.Transmit(new CAPDU(cmd1));
            Assert.AreEqual(new byte[] { 0x90, 0x00 }, rapdu.GetBytes(), "Válasz nem az amit vártam...");

            // SELECT FILE which file is EF Identification [0520] under DF Tachograf [0500] section
            byte[] cmd2 = new byte[] { 0x00, 0xA4, 0x02, 0x0C, 0x02, 0x05, 0x20 };
            rapdu = SmartCard.Transmit(new CAPDU(cmd2));

            // READ BINARY CardIdentification
            byte[] cmd3 = new byte[] { 0x00, 0xB0, 0x00, 0x00, 0x41 };
            rapdu = SmartCard.Transmit(new CAPDU(cmd3));
        }
Пример #19
0
        private bool WriteBinary(ushort address, byte[] data)
        {
            if (data == null)
            {
                return(false);
            }

            CAPDU capdu = new CAPDU(0xFF, 0xD6, (byte)(address / 0x0100), (byte)(address % 0x0100), data);

            log("< " + capdu.AsString(" "));

            RAPDU rapdu = null;

            for (int retry = 0; retry < 4; retry++)
            {
                rapdu = _channel.Transmit(capdu);

                if (rapdu == null)
                {
                    break;
                }
                if ((rapdu.SW != 0x6F01) && (rapdu.SW != 0x6F02) && (rapdu.SW != 0x6F0B))
                {
                    break;
                }

                Thread.Sleep(15);
            }

            if (rapdu == null)
            {
                log("Error '" + _channel.LastErrorAsString + "' while writing the card");
                return(false);
            }

            log("> " + rapdu.AsString(" "));

            if (rapdu.SW != 0x9000)
            {
                log("Bad status word " + rapdu.SWString + " while writing the card");

                return(false);
            }

            return(true);
        }
Пример #20
0
        private byte[] ReadBinaryWithKey(ushort address, byte length, byte[] key)
        {
            CAPDU capdu = new CAPDU(0xFF, 0xF3, (byte)(address / 0x0100), (byte)(address % 0x0100), key, length);

            log("< " + capdu.AsString(" ") + " (reading with specified key)");

            RAPDU rapdu = null;

            for (int retry = 0; retry < 4; retry++)
            {
                rapdu = _channel.Transmit(capdu);

                if (rapdu == null)
                {
                    break;
                }
                if ((rapdu.SW != 0x6F01) && (rapdu.SW != 0x6F02) && (rapdu.SW != 0x6F0B))
                {
                    break;
                }

                Thread.Sleep(10);
            }

            if (rapdu == null)
            {
                log("Error '" + _channel.LastErrorAsString + "' while reading the card");
                return(null);
            }

            log("> " + rapdu.AsString(" "));

            if (rapdu.SW != 0x9000)
            {
                log("Bad status word " + rapdu.SWString + " while reading the card");
                return(null);
            }

            if (!rapdu.hasData)
            {
                log("Empty response");
                return(null);
            }

            return(rapdu.data.GetBytes());
        }
Пример #21
0
        protected byte[] ReadBinary(ushort P1P2, byte Le = 0)
        {
            CAPDU capdu = new CAPDU(0xFF, 0xB0, (byte)(P1P2 / 0x0100), (byte)(P1P2 % 0x0100), Le);

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

            RAPDU rapdu = null;

            for (int retry = 0; retry < 4; retry++)
            {
                rapdu = Channel.Transmit(capdu);

                if (rapdu == null)
                {
                    break;
                }
                if ((rapdu.SW != 0x6F01) && (rapdu.SW != 0x6F02) && (rapdu.SW != 0x6F0B))
                {
                    break;
                }
            }

            if (rapdu == null)
            {
                Logger.Trace("Error '" + Channel.LastErrorAsString + "' while reading the card");
                return(null);
            }

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

            if (rapdu.SW != 0x9000)
            {
                Logger.Trace("Bad status word " + rapdu.SWString + " while reading the card");
                return(null);
            }

            if (!rapdu.hasData)
            {
                Logger.Trace("Empty response");
                return(null);
            }

            return(rapdu.data.GetBytes());
        }
Пример #22
0
        protected bool WriteBinary(ushort P1P1, byte[] Data)
        {
            if (Data == null)
            {
                return(false);
            }

            CAPDU capdu = new CAPDU(0xFF, 0xD6, (byte)(P1P1 / 0x0100), (byte)(P1P1 % 0x0100), Data);

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

            RAPDU rapdu = null;

            for (int retry = 0; retry < 4; retry++)
            {
                rapdu = Channel.Transmit(capdu);

                if (rapdu == null)
                {
                    break;
                }
                if ((rapdu.SW != 0x6F01) && (rapdu.SW != 0x6F02) && (rapdu.SW != 0x6F0B))
                {
                    break;
                }
            }

            if (rapdu == null)
            {
                Logger.Trace("Error '" + Channel.LastErrorAsString + "' while writing the card");
                return(false);
            }

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

            if (rapdu.SW != 0x9000)
            {
                Logger.Trace("Bad status word " + rapdu.SWString + " while writing the card");
                return(false);
            }

            return(true);
        }
Пример #23
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public byte[] GetUID()
        {
            SCardChannel channel = new SCardChannel(reader);

            byte[] uid = new byte[0];

            if (!channel.Connect())
            {
                return(uid);
            }

            CAPDU command  = NFCCommand.ReadCardUid();
            RAPDU response = channel.Transmit(command);

            if (response.SW != 0x9000)
            {
                return(uid);
            }

            return(response.data.GetBytes());;
        }
Пример #24
0
        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);
        }
Пример #25
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";
            }
        }
Пример #26
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";
        }
Пример #27
0
        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);
        }
Пример #28
0
        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);
        }
Пример #29
0
        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);
        }
Пример #30
0
        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);
        }