Smart Cards in .NET, Part 3

In part II, we learned how to use platform invoke in .NET to access the raw APIs exported by the WinSCard.dll and use them in our own applications. That was an introduction explaining how we are going to going to access the native Smart Card APIs. In this article, we will learn how to write the library itself, by writing a managed wrapper for using smart cards in .NET.


Support file for this article available here.


Introduction

In the last article, we learned how to use platform invoke in .NET to access the raw APIs exported by the WinSCard.dll and use them in our own applications. The basic example code for finding readers attached to system was also provided. That was just an introduction explaining how we are going to access the native Smart Card APIs, and now we shall write the library itself. If you have not read the last article, I’d recommend that you to skim though it once, just to understand the code in this article.

The code for this library is written in C#, so I’d expect you to be familiar with the C# language. This is not compulsory, however, because equivalent code can be written in VB .NET–or any .NET compliant language for that matter. Because the library generated with our code is a Managed one, we can use it from any .NET compliant language. As for this library, the demo you’ll find is written in VB .NET.

So without any further ado let’s get started with our library.

Designing our classes

To begin with the design of our classes for this library, we can look at the way the API’s in the WinSCard.dll are organized. They’re organized in the following categories:

  • Smart Card Database Query Functions
  • Smart Card Database Management Functions
  • Resource Manager Context Functions
  • Smart Card Tracking Functions
  • Smart Card and Reader Access Functions
  • Direct Card Access Functions

The database query functions query the smart card database, and they can provide a list of smart cards that are supplied by a specific user, the interfaces, and the primary service provider of a specific smart card. These functions can be used to query the complete smart card database, or a part of it, by setting the resource manager context appropriately. The resource manager context is set by calling one of the Context functions prior to invoking a query function. The success of the called function also depends on the state of the Smart Card Service and the Security context of the calling application. Similarly, the database management functions manage the smart card database and update the database by using a specified resource manager context.

The context functions, on other hand, establish and release the resource manager context, which is used by the database query and database management functions. Tracking functions allow for tracking smart cards within readers attached to the system. Most important are the reader access functions that connect to and communicate with a specific smart card, and manage the data structures for communicating and transferring data to/from the Smart Card. All protocol related details are also set and observed here. Since this is not a perfect world, one sometimes gets cards that are not ISO 7816 compliant, and to access such cards Direct Card access functions come to the rescue.

{mospagebreak title=Two classes to rule them}

Working along the following lines, we may have two classes: one for resource manager, and another for reader functions, so that they encapsulate the Resource Manager and the Smart Card reader itself. A few helper classes that let you deal with the data exchanged with the card and the commands that you pass to the card can also be added, as I’ve done in our library.

As with any other component, library errors and exceptions are something that we can’t ignore, so I’ve also included a SCardException class, which itself derives from ApplicationException class and provides encapsulation on the response code from the card. Added to that, you can raise an exception based on a Win32 error code that you usually get from the native WinSCard API. Also, an internal sealed class WinSCard provides the primitives for P/Invoking the native APIs, and you may have a look at it in the attached code for this article.

So at this point our classes look like this:

Figure 1  Classes for our Library

The class SCResMgr contains the code to manage Contexts with the Smart Card Resource Manager and lets us use it with an instance of SCReader. Indeed, you can’t create a SCReader without a SCResMgr!

Let’s have a look at the members of the SCResMgr:

public void Cancel()

Is a member Equivalent to native SCardCancel() and the code looks something like the code below:

// Tracking Functions
public void Cancel()
{
 if (this.m_hContext == IntPtr.Zero)
 {
  throw new InvalidOperationException();
 }
 uint nErrCode = WinSCard.SCardCancel(this.m_hContext);
 if (nErrCode != 0)
 {
  SCardException.RaiseWin32ResponseCode(nErrCode);
 }
}

public bool EstablishContext(SCardContextScope nScope)

Is equivalent to SCardEstablishContext and uses encapsulated Context handle. Note how the Context Scope constants in the platform SDK have been grouped in an enum to facilitate their use.

public bool GetStatusChange(int nTimeout, SCStateInfo[] vaSCStates)

Is similar to the SCardGetStatusChange API and this contains three overloads to make its use easier as compared to the native API.

These three examples above will be able to explain to you the style adopted for the library. You will find more details as you get to use the library. For a quick look refer to figure 2, which shows all the members of the SCResMgr library. For a more detailed view, look at the source code listing for the SCResMgr API later in the article.

Figure 2  SCResMgr Class View

Next you may see the Class created for SCReader that encapsulates a Smartcard Reader in figure 3.

Figure 3  The SCReaderClassView

Finally you can see all the classes and Enumerations in the figure below. This code, being a part of a commercial product, can’t be released publicly, but a free compiled version is certainly a strong candidate for release with this article.

Figure 4  The completed library

{mospagebreak title=Using the Library: The Demo}

You will find the compiled Library and the demo with source code in the zip file for this article. Tracing through the code, you’ll realize how easy it is to use this library. As I’ve not provided any help manual for the compiled library, you’ll have to rely on the intellisence of the VS.NET IDE for prototypes.

As a reference I’ve included the source code for the SCResMgr Class and the WinSCard.cs file that contains the DllImports to the native functions. They’ll help you understand how the library is implemented.

The WinSCard.cs file contents are as below:

using System;
using System.Text;
using System.Runtime.InteropServices;

namespace SCLib
{
 internal sealed class WinSCard
 {
  // Methods
  public WinSCard()
  {
  }

  // Native Methods Entrypoints
  [DllImport(“WINSCARD.DLL”)]
  internal static extern uint SCardBeginTransaction(IntPtr hCard);

  [DllImport(“WINSCARD.DLL”)]
  internal static extern uint SCardCancel(IntPtr hContext);

  [DllImport(“WINSCARD.DLL”, CharSet=CharSet.Auto)]
  internal static extern uint SCardConnect(IntPtr hContext, string szReader, uint dwShareMode, uint dwPreferredProtocols, out IntPtr phCard, out uint pdwActiveProtocol);

  [DllImport(“WINSCARD.DLL”)]
  internal static extern uint SCardControl(IntPtr hCard, uint dwControlCode, [In] byte[] lpInBuffer, uint nInBufferSize, [In, Out] byte[] lpOutBuffer, uint nOutBufferSize, out uint lpBytesReturned);

  [DllImport(“WINSCARD.DLL”)]
  internal static extern uint SCardDisconnect(IntPtr hCard, uint dwDisposition);

  [DllImport(“WINSCARD.DLL”)]
  internal static extern uint SCardEndTransaction(IntPtr hCard, uint dwDisposition);

  [DllImport(“WINSCARD.DLL”)]
  internal static extern uint SCardEstablishContext(uint dwScope, IntPtr pvReserved1, IntPtr pvReserved2, out IntPtr phContext);

  [DllImport(“WINSCARD.DLL”)]
  internal static extern uint SCardFreeMemory(IntPtr hContext, IntPtr pvMem);

  [DllImport(“WINSCARD.DLL”)]
  internal static extern uint SCardGetAttrib(IntPtr hCard, uint dwAttrId, ref IntPtr pbAttr, ref uint pcbAttrLen);

  [DllImport(“WINSCARD.DLL”, CharSet=CharSet.Auto)]
  internal static extern uint SCardGetStatusChange(IntPtr hContext, uint dwTimeout, [In, Out] SCARD_READERSTATE[] rgReaderStates, uint cReaders);

  [DllImport(“WINSCARD.DLL”)]
  internal static extern uint SCardIsValidContext(IntPtr hContext);

  [DllImport(“WINSCARD.DLL”, CharSet=CharSet.Auto)]
  internal static extern uint SCardListReaderGroups(IntPtr hContext, ref IntPtr pmszGroups, ref uint pcchGroups);

  [DllImport(“WINSCARD.DLL”, CharSet=CharSet.Auto)]
  internal static extern uint SCardListReaders(IntPtr hContext, string mszGroups, ref IntPtr pmszReaders, ref uint pcchReaders);

  [DllImport(“WINSCARD.DLL”)]
  internal static extern uint SCardReconnect(IntPtr hCard, uint dwShareMode, uint dwPreferredProtocols, uint dwInitialization, out uint pdwActiveProtocol);

  [DllImport(“WINSCARD.DLL”)]
  internal static extern uint SCardReleaseContext(IntPtr hContext);

  [DllImport(“WINSCARD.DLL”)]
  internal static extern uint SCardSetAttrib(IntPtr hCard, uint dwAttrId, [In] byte[] pbAttr, uint cbAttrLen);

  [DllImport(“WINSCARD.DLL”, CharSet=CharSet.Auto)]
  internal static extern uint SCardStatus(IntPtr hCard, StringBuilder szReaderName, ref uint pcchReaderLen, out uint pdwState, out uint pdwProtocol, [Out] byte[] pbAtr, ref uint pcbAtrLen);

  [DllImport(“WINSCARD.DLL”)]
  internal static extern uint SCardTransmit(IntPtr hCard, SCARD_IO_REQUEST pioSendPci, [In] byte[] pbSendBuffer, uint cbSendLength, SCARD_IO_REQUEST pioRecvPci, [In, Out] byte[] pbRecvBuffer, ref uint pcbRecvLength);

  // Private/Internal Types
  [StructLayout(LayoutKind.Sequential)]
  internal class SCARD_IO_REQUEST
  {
   internal uint dwProtocol;
   internal uint cbPciLength;
   public SCARD_IO_REQUEST()
   {
    dwProtocol = 0;
   }
  }

  [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
   internal struct SCARD_READERSTATE
  {
   internal string szReader;
   internal IntPtr pvUserData;
   internal uint dwCurrentState;
   internal uint dwEventState;
   internal uint cbAtr;
   [MarshalAs(UnmanagedType.ByValArray, SizeConst=0x24, ArraySubType=UnmanagedType.U1)]
   internal byte[] rgbAtr;
  }
 }
}

{mospagebreak title=Smart card resource manager}

The WinSCard is a sealed class, so you won’t be able to derive from it, but you may refer to it occasionally if you wish to invoke a function directly from your own P/Invoke code. And below, you’ll find code for the SCResMgr.cs that encapsulates the Smart Card resource manager.

using System;
using System.Collections;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace SCLib
{
 public sealed class SCResMgr : IDisposable
 {
  // Methods
  public SCResMgr()
  {
   this.m_fDisposed = false;
   this.m_hContext = IntPtr.Zero;
  }

  private void _Dispose(bool fDisposing)
  {
   if (!this.m_fDisposed)
   {
    if (this.m_hContext != IntPtr.Zero)
    {
     WinSCard.SCardCancel(this.m_hContext);
     WinSCard.SCardReleaseContext(this.m_hContext);
     this.m_hContext = IntPtr.Zero;
    }
    this.m_fDisposed = true;
   }
  }

  private int _SplitMultiString(string msz, IList aList)
  {
   int nTemp;
   int nCount = 0;

   // For each string
   for (int nIter = 0; msz[nIter] != ‘https://forums.aspfree.com/showthread.php?t=229537" target="_blank">Mother not so smart about Japan's smoking smart card (AP)

  • Smart Spending: How to use that gift card wisely (AP)
  • Smart Spending: How to use that gift card wisely (AP)
  • New Device Makes Your Credit Card Smart (LiveScience.com)
  • EU Investigates Possible Smart Card Chip Cartel (PC World)
  • Related Articles

    18 thoughts on “Smart Cards in .NET, Part 3

    1. Hi,

      I’m Digvijay …. the author of this article.
      Please do let me know how you liked the article.

      I’d be happy to answer any of the queries you’ve.

      And if you find it good please do give it a good rating 😉

      -Digvijay

    2. Article looks very helpful. Will start looking and using them to suit our application. However ours is not vb.net. can we use them if we are in vb?
      I’m trying to integrate them into our apps for login authentication as well as PKI purposes.

    3. Hi,

      This is basically for managed code that is accessible from all .NET languages.
      But if you’re in plain old VB6 then you might consider using the SCardComm component by Microsoft directly.

      Regards,

      Digvijay

    4. Hi, I am trying to access memory card (sle4428), as far as I know I need to use SCardControl in order to set the card type. I tried to recompile the source code, and create a new public function to let me do it, but I cannot recompile at all, because there are some classes missed.

      Thanks in advance.

    5. Hi,

      I jut provided the code to give an overview of how to go about it that is why not all the code is there.It’s part of a commercial library that i’m writing so i couldn’t provide the complete code.

      Regards,

      Digvijay

    6. I am attempting to use the code provided and running into a little problem. I have migrated the code to .NET 2.0 and am running it in debug mode. I hit line 150 (Reader.Connect(strReaderName, SCardAccessMode.Exclusive, SCardProtocolIdentifiers.TO) and it throws an error.

      The smart card is not responding to a reset. (Exception from HRESULT: 0x80100066)

      However, when I check the reader.isconnected, it returns false. So, I am wondering if the connection is really not there (even though your test app correctly identified it and its name is showing up in the label) or if it is a bad smart card, or something else. Can you provide assistance?

      Kevon

    7. Hi.
      In yur first article, what is the SCardLIB 1.0 component?
      In my Visual Basic .NET, is not present.

      Thanks and your article is very great!
      Gerard

    8. Hi there,

      This code is quite interesting to me. It works great. Nice work. But I am having trouble about how can I capture events like CardInserted, CardRemoved etc.?

      I would highly appreciate if you could give me a pointer about it.

      Thanks,
      -Krishna

    9. hi sir
      i am Ramzy Ebied
      i am using C#, ASP.Net VS2005

      i did test your sample
      thank you so much, god bless you
      i just have a specific problem
      so if you dont mind i want to call you this is my phone no. = 720 212 0810
      i can tell you what is the problem i am in.

      1 – i need to read and write from the card
      2 – there are files in the card which are like this
      Master file FID (3F00)
      Dedicated file FID (7F10)
      Elementary file FID (6F31)
      Elementary file FID (6F32)
      Elementary file FID (6F33)
      Elementary file FID (6F34)

      i have to look for the master file first and then go the Dedicated file
      and then the Elementary file and so on, i olsa have to look for that the length of every file too. if the file if full, then i will create a new one and write in it.
      so can you tell me how to do that.
      i really need your help.
      thank you very much

    10. Dear DigVijay,
      I would appreciate if you help me out….I’m developing an application in VB.net 2005, and going to read data and write to smart crd, i have seen ur sample code, and it’s great deal and helpful, but i can’t read the card information so far, so plzzzzzzz help me, give me a guide me (Code Snippets would be helpful) how can i can utilize ur library and read the card’s info….????
      Thanks…

    11. hi,

      I had this problem too, I tested my application with different cards,
      some work, some not…
      I think the card is damaged or the card protocol is different..
      I think this Libarary just supports ISO 7816 series…

      Good Luck
      -Soheil Jahangiri

    12. Hi all,
      I’m very grateful to Mr. Digvijay for his article. Very interesting.
      I’m a neophyte on Smart Card programmation, so I ask your help to understand something more.

      I have a Smart Card SLE4442 (ISO7816 compatible) and I’m writing a VB.NET application that read and write information on this card.

      I need to save into the card the customer ID, and evntually his money account, but I’ve a lot of doubts on how use the SCLib.DLL to do it.

      Is it correct to procede in this manner:

      1. I read my customer ID: ID_Customer=12345
      2. I encrypt this information with my encrypt system: (ID_Customer)en = 99999
      3. I transform this value in Hexadecimal value: (99999)dec = (1869F)hex
      4. I write this value in teh card.

      If it is correct, which commands of the class I need to use? Can someone do an explicit example of code to do this operation?

      Thanks a lot for your help.
      Igor

    13. Hi all,
      I’m very grateful to Mr. Digvijay for his article. Very interesting.
      I’m a neophyte on Smart Card programmation, so I ask your help to understand something more.

      I have a Smart Card SLE4442 (ISO7816 compatible) and I’m writing a VB.NET application that read and write information on this card.

      I need to save into the card the customer ID, and evntually his money account, but I’ve a lot of doubts on how use the SCLib.DLL to do it.

      Is it correct to procede in this manner:

      1. I read my customer ID: ID_Customer=12345
      2. I encrypt this information with my encrypt system: (ID_Customer)en = 99999
      3. I transform this value in Hexadecimal value: (99999)dec = (1869F)hex
      4. I write this value in teh card.

      If it is correct, which commands of the class I need to use? Can someone do an explicit example of code to do this operation?

      Thanks a lot for your help.
      Igor

    14. Hello,
      U Can Download SDK & API from Card Vendor site. You have to use the functions provider by vendors & U Can easily get the data from Card

      i will be happy to help you with any further queries
      Digvijay Gupta

    [gp-comments width="770" linklove="off" ]