예제 #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="aCommandAPDU"></param>
        /// <returns></returns>

        public CardResponseAPDU RunGsmAlgo(byte[] rand)
        {
            _Select(0x7F20);
            CardCommandAPDU  aCommandAPDU = new CardCommandAPDU(0xA0, 0x88, 0x00, 0x00, rand);
            CardResponseAPDU resp         = _SendSimCommand(aCommandAPDU);

            return(resp);
        }
예제 #2
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>

        CardPinControl _GetChangeChvPinControl()
        {
            CardCommandAPDU aChangeChvAPDU = new CardCommandAPDU(0xA0, 0x24, 0x00, 0x01,
                                                                 CardHex.ToByteArray("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"));
            CardPinControl aChangeChvPinControl = new CardPinControl(aChangeChvAPDU,
                                                                     CardPinEncoding.Ascii, 0, 8);

            aChangeChvPinControl.MinLength = 4;
            aChangeChvPinControl.MaxLength = 4;
            return(aChangeChvPinControl);
        }
예제 #3
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>

        CardPinControl _GetVerifyChvPinControl()
        {
            CardCommandAPDU aVerifyChvAPDU = new CardCommandAPDU(0xA0, 0x20, 0x00, 0x01,
                                                                 CardHex.ToByteArray("FFFFFFFFFFFFFFFF"));
            CardPinControl aVerifyChvPinControl = new CardPinControl(aVerifyChvAPDU,
                                                                     CardPinEncoding.Ascii, 0);

            aVerifyChvPinControl.MinLength = 4;
            aVerifyChvPinControl.MaxLength = 4;
            return(aVerifyChvPinControl);
        }
예제 #4
0
        /// <summary>
        /// </summary>

        CardResponseAPDU _Select(int nFileID)
        {
            CardCommandAPDU aCommandAPDU;

            byte[] vbFileID = new byte[2];

            vbFileID[0] = (byte)((nFileID >> 8) & 0xFF);
            vbFileID[1] = (byte)(nFileID & 0xFF);

            aCommandAPDU = new CardCommandAPDU(0xA0, 0xA4, 0x00, 0x00, vbFileID);
            return(_SendSimCommand(aCommandAPDU));
        }
예제 #5
0
        /// <summary>
        /// </summary>

        byte[] _ReadRecord(int nRecord, int nLength)
        {
            CardCommandAPDU  aCommandAPDU;
            CardResponseAPDU aResponseAPDU;

            aCommandAPDU  = new CardCommandAPDU(0xA0, 0xB2, (byte)(nRecord), 0x04, nLength);
            aResponseAPDU = _SendSimCommand(aCommandAPDU);
            if (!aResponseAPDU.IsSuccessful)
            {
                return(null);
            }
            else
            {
                return(aResponseAPDU.GetData());
            }
        }
예제 #6
0
        /// <summary>
        /// </summary>

        byte[] _ReadBinary(int nOffset, int nLength)
        {
            CardCommandAPDU  aCommandAPDU;
            CardResponseAPDU aResponseAPDU;

            aCommandAPDU  = new CardCommandAPDU(0xA0, 0xB0, (byte)((nOffset >> 8) & 0xFF), (byte)(nOffset & 0xFF), nLength);
            aResponseAPDU = _SendSimCommand(aCommandAPDU);
            if (!aResponseAPDU.IsSuccessful)
            {
                return(null);
            }
            else
            {
                return(aResponseAPDU.GetData());
            }
        }
예제 #7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="aCommandAPDU"></param>
        /// <returns></returns>

        CardResponseAPDU _SendSimCommand(CardCommandAPDU aCommandAPDU)
        {
            CardResponseAPDU aResponseAPDU = m_aCard.SendCommand(aCommandAPDU);

            if ((aResponseAPDU.SW1 == 0x9F) && (aResponseAPDU.Lr == 0))
            {
                CardCommandAPDU aGetResponseAPDU = new CardCommandAPDU(0xA0, 0xC0, 0x00, 0x00, aResponseAPDU.SW2);
                aResponseAPDU = m_aCard.SendCommand(aGetResponseAPDU);
            }
            else if ((aResponseAPDU.SW1 == 0x67) && (aResponseAPDU.SW2 != 0x00))
            {
                aCommandAPDU.Le = aResponseAPDU.SW2;
                aResponseAPDU   = m_aCard.SendCommand(aCommandAPDU);
            }

            return(aResponseAPDU);
        }
예제 #8
0
        /// <summary>
        /// Wysyła do karty polecenie APDU
        /// </summary>
        /// <param name="command">Polecenie APDU</param>
        /// <param name="checkSuccessful">Ma sprawdzać, czy odpowiedź jest poprawna (90 00)</param>
        /// <exception cref="APDUException"></exception>
        /// <returns>Odpowiedź na polecenie APDU</returns>
        public ByteArray SendCommand(ByteArray command, bool checkSuccessful = true)
        {
            ConnectCard(false);

            CardCommandAPDU apdu = null;

            if (command.Length == 4)
            {
                apdu = new CardCommandAPDU(command[0], command[1], command[2], command[3]);
            }
            else if (command.Length == 5)
            {
                apdu = new CardCommandAPDU(command[0], command[1], command[2], command[3], command[4]);
            }
            else if (command.Length > 5)
            {
                ByteArray data   = command.Extract(5, command[4]);
                int       length = 5 + command[4];
                if (length == command.Length)
                {
                    apdu = new CardCommandAPDU(command[0], command[1], command[2], command[3], data.ByteData);
                }
                else
                {
                    apdu = new CardCommandAPDU(command[0], command[1], command[2], command[3], data.ByteData, command[command.Length - 1]);
                }
            }

            if (apdu == null)
            {
                throw new Exception("Nieprawidłowa komenda APDU: " + command);
            }

            int tryCounter            = 5;
            CardResponseAPDU response = null;

            do
            {
                try
                {
                    Logger.Log("[Encoder] -> " + command);
                    response = CardHandle.SendCommand(apdu);
                    break;
                }
                catch (System.Runtime.InteropServices.COMException comException)
                {
                    tryCounter--;
                    if (tryCounter == 0)
                    {
                        throw new Exception("Sprzętowy błąd wykonywania komendy APDU (Message: " + comException.Message + ", HResult: " + comException.ErrorCode + ")");
                    }
                }
                catch (SCardException scException)
                {
                    tryCounter--;
                    if (tryCounter == 0)
                    {
                        throw new Exception("Sprzętowy błąd wykonywania komendy APDU (Kod: " + scException.ResponseCode + ").", scException);
                    }
                }
                catch (Exception terminalException)
                {
                    tryCounter--;
                    if (tryCounter == 0)
                    {
                        throw new Exception("Sprzętowy błąd wykonywania komendy APDU.", terminalException);
                    }
                }
            } while (true);

            ByteArray result = new ByteArray(response.GenerateBytes());

            Logger.Log("[Encoder] <- " + result);

            if (checkSuccessful && !response.IsSuccessful && !response.IsWarning)
            {
                throw new APDUException("Błąd wykonywania komendy APDU", command, new ByteArray(response.GenerateBytes()), response.SW1, response.SW2);
            }

            return(result);
        }
예제 #9
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="aCardSlot"></param>

        public void AnalyzeCard(CardTerminalSlot aCardSlot)
        {
            // Acquire any processor card (T=0 or T=1) that may be present in the given card
            // terminal slot
            string readerName = aCardSlot.CardTerminalName;

            CardActivationResult nActivationResult;

            DisplayText("Reader Name: " + readerName);

            //aCardSlot.CardTerminal.Config
            CardHandle aCard = aCardSlot.AcquireCard((CardTypes.T0 | CardTypes.T1), out nActivationResult);

            if (nActivationResult != CardActivationResult.Success)
            {
                Debug.Assert(aCard == null);

                switch (nActivationResult)
                {
                case CardActivationResult.NoCard:
                    m_aPromptLabel.Text = readerName + ": Please insert card ...";
                    break;

                case CardActivationResult.UnresponsiveCard:
                    m_aPromptLabel.Text = readerName + ": Unresponsive card.";
                    break;

                case CardActivationResult.InUse:
                    m_aPromptLabel.Text = readerName + ": Card in use";
                    break;

                default:
                    m_aPromptLabel.Text = readerName + ": Can't power up card!";
                    break;
                }
                return;
            }
            m_aPromptLabel.Text = aCardSlot.CardTerminalName + ": Found card";
            DisplayText("Found card in reader " + aCardSlot.CardTerminalName);
            DisplayReaderProperties(aCardSlot);

            aCardSlot.BeginTransaction();

            try

            // We are doing a few things here that any card system should support.
            // Note that the CardHandle represents the combination of card terminal and
            // powered-up card.
            {
                // =========================== ATR DETECTION ======================================
                // Every card accessed through PC/SC must return an Answer To Reset (ATR).
                // So let's see what we've got here.
                byte[] atr = aCard.GetATR();
                if (atr.Length == 0)
                {
                    throw new Exception("Invalid ATR");
                }
                DisplayText("ATR: " + CardHex.FromByteArray(atr, 0, atr.Length));
                // ================================================================================

                // Go a little deeper: is this a contact or contactless card system we are dealing with?
                CardHelper cardHelper = new CardHelper(aCard);
                DisplayText(cardHelper.cardInfo);
                if (cardHelper.isContactless)
                {
                    // =========================== APDU EXCHANGE ==================================
                    // Now we can try to get a unique identifier (UID). Any contactless card with a
                    // PC/C 2.01 compliant card reader should be able to generate a UID. But never mind,
                    // even if this fails it still shows how to create a command APDU with our SmartCardAPI
                    // frameqwork and get a response APDU back from a card.
                    //
                    // Known issues:
                    // SendCommand with CLA=0xFF can cause an exception with some smart card systems,
                    // triggered by an "Unknown Error" (-2146435025) on PC/SC level.
                    // Therefore this code should only be run if we are accessing a contactless reader
                    // interface that is PC/SC 2.01 compliant
                    // ============================================================================
                    byte             CL_CLA         = 0xFF;
                    byte             CL_INS_GET_UID = 0xCA;
                    byte             P1             = 0;
                    byte             P2             = 0;
                    CardCommandAPDU  aCmdAPDU       = new CardCommandAPDU(CL_CLA, CL_INS_GET_UID, P1, P2, 256);
                    CardResponseAPDU aRespAPDU;
                    aRespAPDU = aCard.SendCommand(aCmdAPDU);
                    if (!aRespAPDU.IsSuccessful)
                    {
                        DisplayText("WARNING: can't get a UID - this might be a contact card.");
                    }
                    else
                    {
                        byte[] uidWithSw12 = aRespAPDU.GenerateBytes();
                        if (uidWithSw12.Length < 2)
                        {
                            throw new Exception("Invalid UID");
                        }
                        var uid = CardHex.FromByteArray(uidWithSw12, 0, uidWithSw12.Length - 2);
                        DisplayText("UID: " + uid);

                        if (uid.Length > 0)
                        {
                            _reader.SendRFID(uid);
                        }
                    }
                }
                aCardSlot.EndTransaction();
            }
            catch (Exception x)
            {
                Trace.WriteLine(x.ToString());
                DisplayText(x.ToString());
                m_aPromptLabel.Text = "Card access error";
            }
            finally
            {
                aCard.Dispose(); // release card handle
            }
        }