Problems using SCardTransmit from winScard.dll reading a tag 4B

the dark one picture the dark one · Feb 7, 2014 · Viewed 7.1k times · Source

For two weeks I've been working on the task to read the tag, and a stored link, of an active NFC TI Chip.

I also have a problem reading the ATR.

My reader is a Springcard H663.

I use the winScard.dll and managed to use SCardEstablishContext() and SCardConnect() sucessfully to get to work, including them as a wrapper.

Now I have issues for a week and nothing is moving forward.

I do not know how to access the tag or the stored Information or which values to give to the function.

[StructLayout(LayoutKind.Sequential)]
public struct SCARD_IO_REQUEST
{
    public int dwProtocol;
    public int cbPciLength;
}

and

[DllImport("WinScard.dll")]
public static extern int SCardTransmit(IntPtr hCard,
                                       ref SCARD_IO_REQUEST pioSendPci,
                                       ref byte[] pbSendBuffer,
                                       int cbSendLength,
                                       ref SCARD_IO_REQUEST pioRecvPci,
                                       ref byte[] pbRecvBuffer,
                                       ref int pcbRecvLength);

are the implementation of the io structure and the wrapper.

This is the way I tried to access the information:

internal static void _readtag(string readername)
{
    IntPtr phCard = IntPtr.Zero;
    IntPtr activeProtocoll = IntPtr.Zero;

    try
    {
        get_readerconnection(readername,
                             (uint)PROTOCOL_T1,
                             phCard,
                             activeProtocoll); // SCardEstablishContext and SCardConnection

        int Rueckgabewert;

        SCARD_IO_REQUEST ioRequest = new SCARD_IO_REQUEST();
        ioRequest.dwProtocol = (int)wrapper.PROTOCOL_T0;
        ioRequest.cbPciLength = 8;

        byte[] commandBytes = new byte[] { 0xFF, 0xCA, 0x00, 0x00, 0x00 };
        byte[] reciveBytes = new byte[10];
        int rcvLenght = 0;

        Rueckgabewert = SCardTransmit(phCard,
                                      ref ioRequest,
                                      ref commandBytes,
                                      commandBytes.Length,
                                      ref ioRequest,
                                      ref reciveBytes,
                                      ref rcvLenght);

        if (Rueckgabewert != 0)
            Console.WriteLine("Failed querying tag UID: " + Error_in_String((uint)Rueckgabewert));

        Console.WriteLine(Methoden.ByteToString(reciveBytes));
        wrapper.SCardDisconnect(phCard, 0);
    }
    finally
    {
        wrapper.SCardReleaseContext(phCard);
    }
    return;

To be complete I show you my ByteToString function:

public static string ByteToString(byte[] ByteArray)
{
    ASCIIEncoding ascii = new ASCIIEncoding();
    string multi_string = ascii.GetString(ByteArray);
    return multi_string;
}

also my get_readerconnection(...) function:

internal static void get_readerconnection(string readername,
                                          uint Reader_Protocol,
                                          IntPtr phCard,
                                          IntPtr ActiveProtocol)
{
    try
    {
        phCard = IntPtr.Zero;
        ActiveProtocol = IntPtr.Zero;
        IntPtr hContext = get_hContext();
        // using mode =2
        // readerprotocol =3
        int result = SCardConnect(hContext, readername, 2, Reader_Protocol, ref phCard, ref ActiveProtocol);
        Error_in_String((uint)result);
    }
    catch
    {
    }
}

My problem is how do I adress the _readertag(..) function for getting the tag, the link or the ATR. And how can I display the result?

I searched the web, but most of the codes are to far advanced for me to understand and MSDN does not help me, also the code on code-project is to complicated for me.

I would be very thankful for your help!

Frederic

Answer

Michael Roland picture Michael Roland · Feb 12, 2014

I would suggest that you use the following definition for the SCardTransmit function:

[DllImport("winscard.dll")]
private static extern int SCardTransmit(IntPtr hCard,
                                       IntPtr pioSendPci,
                                       byte[] pbSendBuffer,
                                       int cbSendLength,
                                       IntPtr pioRecvPci,
                                       byte[] pbRecvBuffer,
                                       ref int pcbRecvLength);

Then you could use it in a wrapper method like this:

public byte[] SCardTransmit(IntPtr hCard, int dwActiveProtocol,
                            byte[] sendData, int dataLen)
{
    int pcbRecvLength = 255;
    byte[] pbRecvBuffer = new byte[pcbRecvLength];

    if (hCard == IntPtr.Zero)
        throw new Exception("hCard = null");

    IntPtr ioSend = IntPtr.Zero;

    if (dwActiveProtocol == SCARD_PROTOCOL_T0)
    {
        IntPtr handle = LoadLibrary("winscard.dll");
        IntPtr pci = GetProcAddress(handle, "g_rgSCardT0Pci");
        FreeLibrary(handle);
        ioSend = pci;
    }
    else if (dwActiveProtocol == SCARD_PROTOCOL_T1)
    {
        IntPtr handle = LoadLibrary("winscard.dll");
        IntPtr pci = GetProcAddress(handle, "g_rgSCardT1Pci");
        FreeLibrary(handle);
        ioSend = pci;
    }

    int res = SCardTransmit(hCard,
                            ioSend,
                            sendData, dataLen,
                            IntPtr.Zero,
                            pbRecvBuffer, ref pcbRecvLength);
    if (res != 0)
        throw new Exception("transmit error");

    byte[] answer = new byte[pcbRecvLength];
    Array.Copy(pbRecvBuffer, answer, pcbRecvLength);
    return answer;
}

Regarding the ATR: Your card is a contactless card, so it does not have a real ATR. However, PC/SC emulates an ATR for contactless cards. You can get it using the SCardGetStatusChange function.

Regarding communication with the active tag, I suggest you read the NFC Forum's Type 4 Tag Operation specification (and possibly the relevant parts of the Digital Protocol specification too). You can get these specifications from the NFC Forum's website.