Exemplo n.º 1
0
        /// <summary>
        /// We display reader properties available via CardTerminal class. Note that availability
        /// of these properties depends on the terminal.
        /// </summary>
        /// <param name="aCardSlot">current slot, activated and with powered card</param>
        public void DisplayReaderProperties(CardTerminalSlot aCardSlot)
        {
            string defaultInfo = "not available";

            // we now have a reader and a powered card

            DisplayText("*** Card terminal info ***");

            string manufacturerName = aCardSlot.CardTerminal.ManufacturerName;

            if (manufacturerName == null)
            {
                manufacturerName = defaultInfo;
            }
            DisplayText("manufacturer: " + manufacturerName);

            string productName = aCardSlot.CardTerminal.ProductName;

            if (productName == null)
            {
                productName = defaultInfo;
            }
            DisplayText("name:      " + productName);

            string productVersion = aCardSlot.CardTerminal.ProductVersion;

            if (productVersion == null)
            {
                productVersion = defaultInfo;
            }
            DisplayText("version: " + productVersion);

            string productSerialNumber = aCardSlot.CardTerminal.ProductSerialNumber;

            if (productSerialNumber == null)
            {
                productSerialNumber = defaultInfo;
            }
            DisplayText("serial number: " + productSerialNumber);

            string productAdditionalInfo = aCardSlot.CardTerminal.ProductAdditionalInfo;

            if (productAdditionalInfo == null)
            {
                productAdditionalInfo = defaultInfo;
            }
            DisplayText("additional info:  " + productAdditionalInfo);

            string physicalConnection = aCardSlot.CardTerminal.PhysicalConnection;

            if (physicalConnection == null)
            {
                physicalConnection = defaultInfo;
            }
            DisplayText("physical connection:  " + physicalConnection);

            DisplayText("*** End of card terminal info ***");
        }
Exemplo n.º 2
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
            }
        }