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.

Contributed by
Rating: 5 stars5 stars5 stars5 stars5 stars / 41
November 15, 2004
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

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.

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

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;
  }
 }
}

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] != '\0'; nIter = nTemp + 1)
   {
    nTemp = nIter;

    // While not end of String
    while (msz[nTemp] != '\0')
    {
     nTemp++;
    }
    aList.Add(msz.Substring(nIter, nTemp - nIter));
    nCount++;
   }

   // Return Count
   return nCount;
  }

  ~SCResMgr()
  {
   this._Dispose(false);
  }

  public void Dispose()
  {
   this._Dispose(true);
   GC.SuppressFinalize(this);
  }


  // Context Functions
  public bool EstablishContext(SCardContextScope nScope)
  {
   if (this.m_fDisposed)
   {
    throw new ObjectDisposedException(base.GetType().FullName);
   }
   if (this.m_hContext != IntPtr.Zero)
   {
    throw new InvalidOperationException();
   }
   uint nErrCode = WinSCard.SCardEstablishContext((uint) nScope, IntPtr.Zero, IntPtr.Zero, out this.m_hContext);

   // If Service not running
   if (nErrCode == 0x8010001d)
   {
    MessageBox.Show("The ensure that SCardSvr Service is running. PC/SC routines won't be available.");
    return false;
   }

   // else there is some other problem
   if (nErrCode != 0)
   {
    MessageBox.Show(string.Format("SCardEstablishContext failed: 0x{0:X8}", nErrCode));
    SCardException.RaiseWin32ResponseCode(nErrCode);
   }
   return true;
  }
 

  public void ReleaseContext()
  {
   // Sanity Check
   if (this.m_fDisposed)
   {
    throw new ObjectDisposedException(base.GetType().FullName);
   }

   // Check Context
   if (this.m_hContext == IntPtr.Zero)
   {
    throw new InvalidOperationException();
   }
   uint nErrCode = WinSCard.SCardReleaseContext(this.m_hContext);

   // Set Zero
   this.m_hContext = IntPtr.Zero;

   // Was there an error
   if (nErrCode != 0)
   {
    SCardException.RaiseWin32ResponseCode(nErrCode);
   }
  }


  public bool IsValidContext()
  {
   // Sanity Check
   if (this.m_fDisposed)
   {
    throw new ObjectDisposedException(base.GetType().FullName);
   }

   // Verify Context exists
   if (this.m_hContext == IntPtr.Zero)
   {
    throw new InvalidOperationException();
   }

   // Call native fxn and return appropriately
   uint nErrCode = WinSCard.SCardIsValidContext(this.m_hContext);
   if (nErrCode == 0)
   {
    return true;
   }
   return false;
  }
 

 

  // 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 GetStatusChange(int nTimeout, SCStateInfo[] vaSCStates)
  {
   int nLen = vaSCStates.Length;
   WinSCard.SCARD_READERSTATE[] statearray = new WinSCard.SCARD_READERSTATE[nLen];
   for (int nIter = 0; nIter < nLen; nIter++)
   
{
    statearray[nIter].szReader = vaSCStates[nIter].sReaderName;
    statearray[nIter].pvUserData = IntPtr.Zero;
    statearray[nIter].dwCurrentState = (uint) vaSCStates[nIter].nCurrentState;
    statearray[nIter].dwEventState = 0;
    statearray[nIter].cbAtr = 0;
    statearray[nIter].rgbAtr = null;
   }

   uint nErrCode = WinSCard.SCardGetStatusChange(this.m_hContext, (uint) nTimeout, statearray, (uint) statearray.Length);
   if (nErrCode == 0)
   {
    for (int nLoopCnt = 0; nLoopCnt < nLen; nLoopCnt++)
    {
     vaSCStates[nLoopCnt].nEventState = (SCStates) statearray[nLoopCnt].dwEventState;
     if ((statearray[nLoopCnt].cbAtr > 0) && (statearray[nLoopCnt].rgbAtr != null))
     {
      vaSCStates[nLoopCnt].vbATR = new byte[statearray[nLoopCnt].cbAtr];
      Buffer.BlockCopy(statearray[nLoopCnt].rgbAtr, 0, vaSCStates[nLoopCnt].vbATR, 0, (int) statearray[nLoopCnt].cbAtr);
     }
     else
     {
      vaSCStates[nLoopCnt].vbATR = null;
     }
    }
    return true;
   }

   // If Timeout or SCardCancel happened just bail out
   if ((nErrCode == 0x8010000a) || (nErrCode == 0x80100002))
   {
    return false;
   }
   // If we reach here there is a problem
   SCardException.RaiseWin32ResponseCode(nErrCode);
   throw new InvalidProgramException();
  }

  public bool GetStatusChange(int nTimeout, ref SCStateInfo aSCInfo)
  {
   SCStateInfo[] infoArray = new SCStateInfo[1] { aSCInfo } ;
   bool bStatus = this.GetStatusChange(nTimeout, infoArray);
   aSCInfo = infoArray[0];
   return bStatus;
  }

  public bool GetStatusChange(int nTimeout, string sReaderName, SCStates nCurrentState, out SCStates nEventState)
  {
   SCStateInfo info;
   info.sReaderName = sReaderName;
   info.nCurrentState = nCurrentState;
   info.nEventState = SCStates.Unaware;
   info.vbATR = null;
   bool bStatus = this.GetStatusChange(nTimeout, ref info);
   nEventState = info.nEventState;
   return bStatus;
  }
 

  

  // Database Functions
  public int ListReaderGroups(IList aReaderGroupsList)
  {
   // Sanity Check
   if (this.m_fDisposed)
   {
    throw new ObjectDisposedException(base.GetType().FullName);
   }
   // Sanity Check
   if (this.m_hContext == IntPtr.Zero)
   {
    throw new InvalidOperationException();
   }

   int nRet = 0;
   IntPtr ptrRMgrMem = IntPtr.Zero;
   uint nMax = uint.MaxValue;
   uint nErrCode = WinSCard.SCardListReaderGroups(this.m_hContext, ref ptrRMgrMem, ref nMax);

   // Success
   if (nErrCode == 0)
   {
    string text = Marshal.PtrToStringAuto(ptrRMgrMem, (int) nMax);
    nRet = this._SplitMultiString(text, aReaderGroupsList);
    nErrCode = WinSCard.SCardFreeMemory(this.m_hContext, ptrRMgrMem);
    return nRet;
   }

   // Error
   if (nErrCode == 2148532270)
   {
    return 0;
   }

   // Exception
   SCardException.RaiseWin32ResponseCode(nErrCode);
   return nRet;
  }

  public int ListReaders(IList aReadersList)
  {
   return this.ListReaders((string[]) null, aReadersList);
  }
 

  public int ListReaders(string sGroup, IList aReadersList)
  {
   // Sanity Check
   if ((sGroup == null) || (sGroup.Length == 0))
   {
    throw new ArgumentNullException();
   }
   string[] textArray = new string[1] { sGroup } ;
   return this.ListReaders(textArray, aReadersList);
  }
 

  public int ListReaders(string[] vsGroups, IList aReadersList)
  {
   // Sanity Check
   if (this.m_fDisposed)
   {
    throw new ObjectDisposedException(base.GetType().FullName);
   }
   if (this.m_hContext == IntPtr.Zero)
   {
    throw new InvalidOperationException();
   }

   int nReaders = 0;
   string text = null;
   if ((vsGroups != null) && (vsGroups.Length > 0))
   {
    string[] textArray = vsGroups;
    for (int nIter = 0; nIter < textArray.Length; nIter++)
    {
     string strTemp = textArray[nIter];
     text = text + strTemp;
     text = text + '\0';
    }
    text = text + '\0';
   }
   IntPtr ptrMem = IntPtr.Zero;

   // Autoallocate
   uint nMax = uint.MaxValue;

   uint nErrCode = WinSCard.SCardListReaders(this.m_hContext, text, ref ptrMem, ref nMax);
   
   // Success
   if (nErrCode == 0)
   {
    string sReaders = Marshal.PtrToStringAuto(ptrMem, (int) nMax);
    nReaders = this._SplitMultiString(sReaders, aReadersList);

    // Free Autoallocate
    nErrCode = WinSCard.SCardFreeMemory(this.m_hContext, ptrMem);
    return nReaders;
   }
   
   // Couln't find a Reader???
   if (nErrCode == 2148532270)
   {
    return 0;
   }
   
   // Report Error
   SCardException.RaiseWin32ResponseCode(nErrCode);
   return nReaders;
  }

  // Properties
  public IntPtr Context
  {
   get
   {
    return this.m_hContext;
   }
  }
 
  // Fields
  private bool m_fDisposed;
  private IntPtr m_hContext;
 }
}

CardCommand and SelectSmart

The CardCommand Class will allow you to create and use the ISO-7816 compliant APDUs and the SCUtil helper class can help you convert strings to byte arrays for Transmitting and converting the Smart Card response from a byte array to String for display in the UI or console. Other enums often will be used as function parameters and are self-explanatory.

Another feature is the SelectSmartCard Dialog, which lets the user select a reader from the system readers through UI. To use it, just call this static method from the SCReaser class. Also, take a look at the types you’ll be dealing with in the listing for file (can be found in the source code) Types.cs:

using System;
using System.Runtime.InteropServices;

namespace SCLib
{
 public enum SCardAccessMode
 {
  Direct = 3,
  Exclusive = 1,
  Shared = 2
 }
 public enum SCardResponseCode
 {
  Cancelled = 2,
  InsufficientBuffer = 8,
  InvalidHandle = 3,
  InvalidParameter = 4,
  InvalidValue = 0x11,
  NoMemory = 6,
  NoService = 0x1d,
  NoSmartCard = 12,
  NotReady = 0x10,
  NotTransacted = 0x16,
  ProtoMismatch = 15,
  ReaderUnavailable = 0x17,
  RemovedCard = 0x69,
  ResetCard = 0x68,
  ServiceStopped = 30,
  SharingViolation = 11,
  SystemCancelled = 0x12,
  Timeout = 10,
  UnknownCard = 13,
  UnknownReader = 9,
  UnpoweredCard = 0x67,
  UnresponsiveCard = 0x66,
  UnsupportedCard = 0x65
 }
 
 public enum SCardContextScope
 {
  System = 2,
  Terminal = 1,
  User = 0
 }
 
 public enum SCardReaderState
 {
  Absent = 1,
  Negotiable = 5,
  Powered = 4,
  Present = 2,
  Specific = 6,
  Swallowed = 3,
  Unknown = 0
 }

 public enum SCardReaderCapability
 {
  ATRString = 0x90303,
  ChannelID = 0x20110,
  Characteristics = 0x60150,
  CurrentBWT = 0x80209,
  CurrentCLK = 0x80202,
  CurrentCWT = 524810,
  CurrentD = 0x80204,
  CurrentEBCEncoding = 0x8020b,
  CurrentF = 0x80203,
  CurrentIFSC = 0x80207,
  CurrentIFSD = 0x80208,
  CurrentIOState = 0x90302,
  CurrentN = 0x80205,
  CurrentProtocolType = 0x80201,
  CurrentW = 0x80206,
  DeviceFriendlyName = 0x7fff0003,
  DeviceInUse = 0x7fff0002,
  DeviceSystemName = 0x7fff0004,
  DeviceUnit = 0x7fff0001,
  EscAuthRequest = 0x7a005,
  EscCancel = 0x7a003,
  EscReset = 0x7a000,
  ExtendedBWT = 0x8020c,
  ICCInterfaceStatus = 0x90301,
  ICCPresence = 0x90300,
  ICCTypePerATR = 0x90304,
  MaxInput = 0x7a007,
  PerfBytesTransmitted = 0x7ffe0002,
  PerfNumTransmissions = 0x7ffe0001,
  PerfTransmissionTime = 0x7ffe0003,
  PowerMgmtSupport = 0x40131,
  ProtocolDefaultCLK = 0x30121,
  ProtocolDefaultDataRate = 0x30123,
  ProtocolMaxCLK = 0x30122,
  ProtocolMaxDataRate = 196900,
  ProtocolMaxIFSD = 0x30125,
  ProtocolTypes = 0x30120,
  SuppressT1IFSRequest = 0x7fff0007,
  UserAuthInputDevice = 0x50142,
  UserToCardAuthDevice = 328000,
  VendorIFDSerialNo = 0x10103,
  VendorIFDType = 0x10101,
  VendorIFDVersion = 0x10102,
  VendorName = 0x10100
 }

 [Flags]
 public enum SCardProtocolIdentifiers
 {
  Default = -2147483648,
  Optimal = 0,
  Raw = 0x10000,
  T0 = 1,
  T1 = 2,
  Undefined = 0
 }
 
 [Flags]
 public enum SCStates
 {
  AtrMatch = 0x40,
  Changed = 2,
  Empty = 0x10,
  Exclusive = 0x80,
  Ignore = 1,
  InUse = 0x100,
  Mute = 0x200,
  Present = 0x20,
  Unavailable = 8,
  Unaware = 0,
  Unknown = 4,
  Unpowered = 0x400
 }
 
 [StructLayout(LayoutKind.Sequential)]
 public struct SCStateInfo
 {
  public string sReaderName;
  public SCStates nCurrentState;
  public SCStates nEventState;
  public byte[] vbATR;
 }
 
    [Flags]
 public enum SCardMechanicalCharacteristics
 {
  Captures = 4,
  Ejects = 2,
  None = 0,
  Swallows = 1
 }
 
 public enum SCardDisposition
 
{
  Confiscate = 4,
  EjectCard = 3,
  LeaveCard = 0,
  ResetCard = 1,
  UnpowerCard = 2
 }
 public enum SCardControlCodes
 {
  Absent = 0x31002c,
  Confiscate = 3211280,
  Eject = 0x310018,
  GetAttribute = 0x310008,
  GetLastError = 0x31003c,
  GetPerfCntr = 0x310040,
  Power = 0x310004,
  Present = 0x310028,
  Protocol = 0x310030,
  Read = 0x310020,
  SetAttribute = 0x31000c,
  State = 3211320,
  Swallow = 0x31001c,
  Transmit = 0x310014,
  Write = 3211300
 }

 public enum SCardChannelType
 {
  IDE = 0x10,
  Keyboard = 4,
  Parallel = 2,
  PCMCIA = 0x40,
  SCSI = 8,
  Serial = 1,
  Unknown = 0,
  USB = 0x20,
  Vendor = 240
 }
 
 [Flags]
 public enum SCardAuthDevices
 {
  Display = 0x80,
  EncryptedInput = 0x8000,
  Fingerprint = 8,
  Image = 0x20,
  Keyboard = 4,
  NoDevices = 0,
  Numeric = 2,
  ReservedFU = 1,
  Retinal = 0x10,
  Voice = 0x40
 }
 

 public enum SCLibExceptionCode
 {
  CardCommunicationError = 2,
  CardWithdrawn = 1,
  DriverError = 3,
  DriverException = 4,
  DriverIncompatible = 5,
  InvalidAPDU = 0
 }

}

I believe this will be more than enough to get you started writing applications using the library, or maybe writing your own version.

Source code for the library

You can download the compiled library and source code for the demo using the library here. It contains the library, as a DLL, that we designed above, as well as the sample application that we can use to peek at how the library works. The library is written using VS .NET 2003 architect, and the library code and the demo application code are in C# and VB .NET respectively.

The compiled library and the demo with source code come with no warranty of any kind. You’re free to use it in any way, but I’d appreciate it if you mail me if you find any bugs while using it, and use it in a Commercial, Freeware or Shareware application or product.

Points of Interest

The design of this library is not very fine, but it is more than enough for writing a full-fledged Smart Card application. You have unrestricted rights to use and distribute the compiled library as you wish, but I’d appreciate an email about the same, or maybe some code snippets. Feel free to contact me if you have any feature requests.

The underlying WinSCard.dll itself complies with PC/SC v1.0, and the draft version of 2.0 is out on http://www.pcscworkgroup.com, so you may want to have a look at the new specification before you get the new implementations, just to have an idea of the shortcomings in the current implementation.

Summary

In this article we learned about writing a pure managed wrapper on the WinSCard API, and used it in a VB .NET application to communicate with a Smart Card. This can be a good starting point for writing a Smart Cards commercial library in .NET to many of you. I hope that it will get you started with writing applications that access and use smart cards in .NET. In case you have any doubts or confusion about the code, you may always write to me and I shall surely write back to you with an answer. Any comments and suggestions are also welcome.

blog comments powered by Disqus
.NET ARTICLES

- .Net 4.5 Brings Changes
- Understanding Events in VB.NET
- Objects, Properties, Events and Methods in V...
- Install Visual Web Developer Express 2010
- Microsoft Gadgeteer an Open Source Alternati...
- Best DotNetNuke Modules
- Facebook Image Viewer in Visual Basic
- Murach`s ADO.NET 4 Database Programming with...
- 5 Must Have Visual Studio 2010 Extensions
- Dynamic Web Applications with ASP.NET Mono u...
- PDFSharp: HTML to PDF in ASP.NET 3.5 using V...
- Using the PDFSharp Library in ASP.NET 3.5 wi...
- Sending Email in ASP.NET 3.5 using VB.NET wi...
- ASP.NET 3.5 Role Based Security and User Aut...
- Creating ASP.NET Login Web Pages and Basic C...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 6 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials