Example #1
0
        private void selectApplet()
        {
            myReader = new Reader();
            myReader.Connect(read);

            apdu    = new CommandAPDU(0x00, 0xA4, 0x04, 0x00, AID, (byte)AID.Length);
            resApdu = myReader.Transmit(apdu);
            reponse_texBox.AppendText(resApdu.SW1.ToString("X2") + resApdu.SW2.ToString("X2"));
        }
Example #2
0
        private void write_button_Click(object sender, EventArgs e)
        {
            selectApplet();

            if (testChamp(last_Name_textBox.Text) || testChamp(name_textBox.Text) ||
                testChamp(compte_num_textBox.Text) || testChamp(carte_num_textBox.Text))
            {
                MessageBox.Show("Champ vide !");
            }
            {
                // conversion hexadeci
                name           = Encoding.ASCII.GetBytes(name_textBox.Text);
                las_name       = Encoding.ASCII.GetBytes(last_Name_textBox.Text);
                card_number    = Encoding.ASCII.GetBytes(carte_num_textBox.Text);
                account_number = Encoding.ASCII.GetBytes(compte_num_textBox.Text);

                // appel apdu
                apdu    = new CommandAPDU(0x10, 0xDA, 0x00, 0x5B, (byte)name.Length, name);
                resApdu = myReader.Transmit(apdu);
                reponse_texBox.AppendText(resApdu.SW1.ToString("X2") + resApdu.SW2.ToString("X2"));

                apdu    = new CommandAPDU(0x10, 0xDA, 0x00, 0x87, (byte)las_name.Length, las_name);
                resApdu = myReader.Transmit(apdu);
                reponse_texBox.AppendText(resApdu.SW1.ToString("X2") + resApdu.SW2.ToString("X2"));

                apdu    = new CommandAPDU(0x10, 0xDA, 0x00, 0x8F, (byte)card_number.Length, card_number);
                resApdu = myReader.Transmit(apdu);
                reponse_texBox.AppendText(resApdu.SW1.ToString("X2") + resApdu.SW2.ToString("X2"));

                apdu    = new CommandAPDU(0x10, 0xDA, 0x00, 0x81, (byte)account_number.Length, account_number);
                resApdu = myReader.Transmit(apdu);
                reponse_texBox.AppendText(resApdu.SW1.ToString("X2") + resApdu.SW2.ToString("X2"));

                // annonce si succes
            }
        }
Example #3
0
        private ErrorCode TransmitT0(CommandAPDU cAPDU, ResponseAPDU rAPDU)
        {
            ErrorCode ret;

            // Adapt APDU for T=0 smartcards
            // If C-APDU is CC1: add P3=0
            if (cAPDU.IsCc1)
            {
                var crp = new CommandResponsePair(cAPDU)
                {
                    CApdu = { Le = 0 }, RApdu = rAPDU
                };
                ret = crp.Transmit(_cardChannel);
            }
            // If C-APDU is CC2: test SW1=61/6C
            else if (cAPDU.IsCc2)
            {
                var crp = new CommandResponsePair(cAPDU);
                // Let the crp create a new crp.rAPDU
                ret = crp.Transmit(_cardChannel);
                if (ret == ErrorCode.Success && crp.RApdu.Sw1 == 0x61)
                {
                    var crpGetResponse = new CommandResponsePair(new GetResponseCommand(crp.RApdu.Sw2))
                    {
                        RApdu = rAPDU
                    };
                    ret = crpGetResponse.Transmit(_cardChannel);
                }
                else if (ret == ErrorCode.Success && crp.RApdu.Sw1 == 0x6C)
                {
                    var crpWithLe = new CommandResponsePair {
                        CApdu = crp.CApdu
                    };
                    crpWithLe.CApdu.Le = crp.RApdu.Sw2;
                    crpWithLe.RApdu    = rAPDU;
                    ret = crpWithLe.Transmit(_cardChannel);
                }
                else
                {
                    // last rAPDU must be returned as is
                    rAPDU.Udr = crp.RApdu.Udr;
                    rAPDU.Sw1 = crp.RApdu.Sw1;
                    rAPDU.Sw2 = crp.RApdu.Sw2;
                }
            }
            // If C-APDU is CC3: nothing to do
            else if (cAPDU.IsCc3)
            {
                var crp = new CommandResponsePair(cAPDU)
                {
                    RApdu = rAPDU
                };
                ret = crp.Transmit(_cardChannel);
            }
            // If C-APDU is CC4: first CC3 then CC2 GET RESPONSE
            else
            {
                cAPDU.HasLe = false;
                var crp = new CommandResponsePair(cAPDU);
                // Let the crp create a new crp.rAPDU
                ret = crp.Transmit(_cardChannel);
                if (ret == ErrorCode.Success && crp.RApdu.Sw1 == 0x61)
                {
                    var crpGetResponse = new CommandResponsePair(new GetResponseCommand(crp.RApdu.Sw2))
                    {
                        RApdu = rAPDU
                    };
                    ret = crpGetResponse.Transmit(_cardChannel);
                }
                else if (ret == ErrorCode.Success && crp.RApdu.Sw1 == 0x6C)
                {
                    var crpWithLe = new CommandResponsePair {
                        CApdu = crp.CApdu
                    };
                    crpWithLe.CApdu.Le = crp.RApdu.Sw2;
                    crpWithLe.RApdu    = rAPDU;
                    ret = crpWithLe.Transmit(_cardChannel);
                }
                else
                {
                    // last rAPDU must be returned as is
                    rAPDU.Udr = crp.RApdu.Udr;
                    rAPDU.Sw1 = crp.RApdu.Sw1;
                    rAPDU.Sw2 = crp.RApdu.Sw2;
                }
                // Restore initial cAPDU for logs
                cAPDU.HasLe = true;
            }

            return(ret);
        }
Example #4
0
 private static void PrintResponse(ResponseAPDU response)
 {
     Console.WriteLine($"R-APDU: {response}");
 }
 /// <summary>
 /// Creates a new instance from given <paramref name="errorCode"/> and <paramref name="response"/>.
 /// </summary>
 /// <param name="errorCode"></param>
 /// <param name="response"></param>
 public FakeCardFeedback(ErrorCode errorCode, ResponseAPDU response)
 {
     ErrorCode = errorCode;
     RApdu     = response;
 }
Example #6
0
        private static void Main(/*string[] args*/)
        {
            try
            {
                Console.WindowWidth  = 132;
                Console.WindowHeight = 50;
            }
            catch (Exception)
            {
            }
            Console.ForegroundColor = ConsoleColor.Gray;

            Console.WriteLine();
            Console.WriteLine("=========== S t a t u s W o r d D i c t i o n a r y");

            #region >> StatusWordDictionary

            //            StatusWordDictionary swd = SerializedObject<StatusWordDictionary>.loadFromXml(@"Dictionary.StatusWord.xml");
            //            Console.WriteLine("SW: {0:X4} Description: {1}", 0x9000, swd.getDescription(0x90, 0x00));
            //            Console.WriteLine("SW: {0:X4} Description: {1}", 0x6800, swd.getDescription(0x68, 0x00));
            //            Console.WriteLine("SW: {0:X4} Description: {1}", 0x6800, swd.getDescription(0x68, 0x84));
            //            Console.WriteLine("SW: {0:X4} Description: {1}", 0x6A00, swd.getDescription(0x6A, 0x83));

            #endregion

            Console.WriteLine();
            Console.WriteLine("=========== C o n s o l e O b s e r v e r");

            #region >> ConsoleObserver

            var logger = new ConsoleObserver();

            #endregion

            Console.WriteLine();
            Console.WriteLine("=========== C a r d C o n t e x t");

            #region >> CardContext

            ICardContext context = new CardContext();
            logger.ObserveContext((ICardContextObservable)context);

            context.Establish();
            context.ListReaderGroups();
            context.ListReaders(context.Groups[0]);

            #endregion

            Console.WriteLine();
            Console.WriteLine("=========== C a r d   i n s e r t i o n   d e t e c t i o n");

            #region >> StatusChangeMonitor

            var monitor = new StatusChangeMonitor(context);

            logger.ObserveMonitor(monitor);

            var readerState = monitor.WaitForCardPresence(0);
            if (readerState == null)
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine(">> Insert a card in one of the {0} readers (time out in 15s)", context.ReadersCount);
                readerState = monitor.WaitForCardPresence(15000);
            }

            if (readerState == null)
            {
                Console.WriteLine(">> Time Out! No card found");
                return;
            }

            #endregion

            Console.WriteLine();
            Console.WriteLine("=========== C a r d C h a n n e l");

            #region >> CardChannel

            ICardChannel cardChannel = new CardChannel(context, readerState.ReaderName);
            logger.ObserveChannel((ICardChannelObservable)cardChannel);

            cardChannel.Connect(ShareMode.Shared, Protocol.Any);

            //Console.WriteLine(cardChannel.getStatus());

            byte[] recvBuffer = null;
            cardChannel.GetAttrib(Attrib.AtrString, ref recvBuffer);

            recvBuffer = null;
            cardChannel.GetAttrib(Attrib.DeviceFriendlyName, ref recvBuffer);

            recvBuffer = null;
            cardChannel.GetAttrib(Attrib.AtrString, ref recvBuffer);

            cardChannel.Reconnect(ShareMode.Shared, Protocol.Any, Disposition.ResetCard);

            Console.WriteLine();

            var           cAPDU = new CommandAPDU("00A4040005A000000069");
            ICardResponse rAPDU = new ResponseAPDU();

            cardChannel.Transmit(cAPDU, rAPDU);
            if (((ResponseAPDU)rAPDU).Sw1 == 0x61)
            {
                cAPDU = new CommandAPDU(String.Format("00C00000{0:X2}", ((ResponseAPDU)rAPDU).Sw2));
                rAPDU = new ResponseAPDU();
                cardChannel.Transmit(cAPDU, rAPDU);
            }

            Console.WriteLine();

            cAPDU = new CommandAPDU("00A404000E315041592E5359532E4444463031");
            rAPDU = new ResponseAPDU();
            cardChannel.Transmit(cAPDU, rAPDU);
            if (((ResponseAPDU)rAPDU).Sw1 == 0x61)
            {
                cAPDU = new CommandAPDU(String.Format("00C00000{0:X2}", ((ResponseAPDU)rAPDU).Sw2));
                rAPDU = new ResponseAPDU();
                cardChannel.Transmit(cAPDU, rAPDU);
            }

            cAPDU = new CommandAPDU(0x00, 0xA4, 0x00, 0x00, 0x02, new byte[] { 0x3F, 0x00 });
            rAPDU = new ResponseAPDU();
            cardChannel.Transmit(cAPDU, rAPDU);
            if (((ResponseAPDU)rAPDU).Sw1 == 0x61)
            {
                cAPDU = new CommandAPDU(String.Format("00C00000{0:X2}", ((ResponseAPDU)rAPDU).Sw2));
                rAPDU = new ResponseAPDU();
                cardChannel.Transmit(cAPDU, rAPDU);
            }

            cardChannel.Disconnect(Disposition.UnpowerCard);

            #endregion

            Console.WriteLine();
            Console.WriteLine("=========== C a r d C h a n n e l S t a c k");

            #region >> CardChannelStack

            var layers = new List <ICardChannelLayer> {
                new CardChannelLayer61(), new CardChannelLayer()
            }
            .ToObservableLayers()
            .ForEach(l => logger.ObserveChannel(l));

            ICardChannelStack cardStack = new CardChannelStack(layers);

            cardStack.Attach(context, readerState.ReaderName);

            cardStack.Connect(ShareMode.Shared, Protocol.Any);

            cardStack.Reconnect(ShareMode.Shared, Protocol.Any, Disposition.ResetCard);

            // Use of a CommandResponsePair object to manage the dialog
            cAPDU = new SelectCommand(SelectCommand.SelectionMode.SelectDFName, SelectCommand.FileOccurrence.FirstOrOnly, SelectCommand.FileControlInformation.ReturnFci, "A000000069".FromHexa(), 0xFF);
            var crp = new CommandResponsePair(cAPDU);
            crp.Transmit(cardStack);

            cardStack.Disconnect(Disposition.UnpowerCard);

            #endregion

            Console.WriteLine();
            Console.WriteLine("=========== C a r d   r e m o v a l   d e t e c t i o n");

            #region >> StatusChangeMonitor

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine(">> Waiting for a change since last call (time out in 10s)");
            // "unpower" change should be fired for the previously used reader
            monitor.WaitForChange(10000);

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine(">> Remove the card in one of the readers {0} (time out in 10s)", readerState.ReaderName);
            // Wait for another change
            monitor.WaitForChange(10000);

            #endregion

            Console.WriteLine();

            context.Release();
        }