Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Smart Card Reader, can't read some cards

Tags:

c#

smartcard

I have an application that is using an smart card reader for allowing the users to access parts of the system. On one location i have no issues. But another, which have an different type of card manufacturer has a lot of issues. It keeps getting an id of zero back. Then looking into the eventlog i saw this: enter image description here And this is the code:

 card.Connect(reader, SHARE.Shared, PROTOCOL.T0orT1);

 var apduGetID = new APDUCommand(0xFF, 0xCA, 0, 0, null, 4);
 var apduRespGetID = card.Transmit(apduGetID);
long cardId = BitConverter.ToUInt32(apduRespGetID.Data.Reverse().ToArray(), 0);

the second problem is that then trying to debug the code, it works perfect, only then remove the breakpoint can i see the issue but not where. Can some one please help me?

P.S. i found this thread, but it does not work: https://superuser.com/questions/715688/smart-card-errors

Update: Here are the Transmit class

 public override APDUResponse Transmit(APDUCommand ApduCmd)
        {
            var RecvLength = (uint)(ApduCmd.Le + APDUResponse.SW_LENGTH);
            byte[] ApduBuffer;
            var ApduResponse = new byte[ApduCmd.Le + APDUResponse.SW_LENGTH];
            var ioRequest = new SCard_IO_Request
            {
                m_dwProtocol = m_nProtocol,
                m_cbPciLength = 8
            };

            // Build the command APDU
            if (ApduCmd.Data == null)
            {
                ApduBuffer = new byte[APDUCommand.APDU_MIN_LENGTH + ((ApduCmd.Le != 0) ? 1 : 0)];

                if (ApduCmd.Le != 0)
                {
                    ApduBuffer[4] = ApduCmd.Le;
                }
            }
            else
            {
                ApduBuffer = new byte[APDUCommand.APDU_MIN_LENGTH + 1 + ApduCmd.Data.Length];

                for (var nI = 0; nI < ApduCmd.Data.Length; nI++)
                {
                    ApduBuffer[APDUCommand.APDU_MIN_LENGTH + 1 + nI] = ApduCmd.Data[nI];
                }

                ApduBuffer[APDUCommand.APDU_MIN_LENGTH] = (byte)ApduCmd.Data.Length;
            }

            ApduBuffer[0] = ApduCmd.Class;
            ApduBuffer[1] = ApduCmd.Ins;
            ApduBuffer[2] = ApduCmd.P1;
            ApduBuffer[3] = ApduCmd.P2;

            m_nLastError = SCardTransmit(m_hCard, ref ioRequest, ApduBuffer, (uint)ApduBuffer.Length, IntPtr.Zero, ApduResponse, out RecvLength);

            if (m_nLastError != 0)
            {
                var msg = "SCardTransmit error: " + m_nLastError;
                throw new SmartCardException(msg, m_nLastError);
            }

            var apduData = new byte[RecvLength];

            for (var nI = 0; nI < RecvLength; nI++)
            {
                apduData[nI] = ApduResponse[nI];
            }

            return new APDUResponse(apduData);
        }

Update 2: I have also tried with to put some Thread.Sleep()

like image 648
mortenstarck Avatar asked May 21 '15 14:05

mortenstarck


People also ask

Why won't my CAC reader read my card?

If your CAC reader is still not seen by ActivClient, make sure that the Smart Card service is running. If you are unable to start the service; It doesn't show up; ActivClient still says no reader attached; or it acknowledges you have a CAC in the reader (but you can't access it) follow these registry edits below.

What are the problems with smart cards?

The biggest problem facing smart cards is security and the problem is two fold. The first issue is that not all smart cards are in fact secure. VISA and MasterCard developed a new standard, SET, in early 1996 in an attempt to get the entire industry on a standard of encryption.


1 Answers

Please check that on the second machine you have all the up-to-date drivers of the smart card. Also, sometimes it helps to replace the driver which is provided by the manufacturer with "Microsoft WUDF driver" - https://msdn.microsoft.com/en-us/library/windows/hardware/dn653571(v=vs.85).aspx

Please note, that you have two type of devices detected by the OS when you plug it in - the smart card enumerator device (smart card reader) and the smart card (sometimes called the smart card container) itself. One smart card reader can contain several smart cards.

Example of the smart card which driver was forcefully replaced with Microsoft WUDF to make the client application (iBank2) work:

four smart cards WUDF type

The four smart card drivers have been forcefully replaced with basic Microsoft driver to make the application work.

like image 154
Samvel Avanesov Avatar answered Oct 11 '22 14:10

Samvel Avanesov