Click here to Skip to main content
15,886,422 members
Articles / Programming Languages / C#

Using Smart cards with a Windows Store App

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
10 Feb 2013CPOL8 min read 32.1K   1.7K   18  
This article describes a solution to access API and resources that are not available with WinRT.
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace GemCard 
{
    /// <summary>
    /// Values for AttrId of SCardGetAttrib
    /// </summary>
    public class SCARD_ATTR_VALUE
    {
        private const uint 
            SCARD_CLASS_COMMUNICATIONS = 2,
            SCARD_CLASS_PROTOCOL = 3,
            SCARD_CLASS_MECHANICAL = 6,
            SCARD_CLASS_VENDOR_DEFINED = 7,
            SCARD_CLASS_IFD_PROTOCOL = 8,
            SCARD_CLASS_ICC_STATE = 9,
            SCARD_CLASS_SYSTEM = 0x7fff;

        private static UInt32 SCardAttrValue(UInt32 attrClass, UInt32 val)
        {
            return (attrClass << 16) | val;
        }

        public static UInt32 CHANNEL_ID { get { return SCardAttrValue(SCARD_CLASS_COMMUNICATIONS, 0x0110); } }

        public static UInt32 CHARACTERISTICS { get { return SCardAttrValue(SCARD_CLASS_MECHANICAL, 0x0150); } }

        public static UInt32 CURRENT_PROTOCOL_TYPE { get { return SCardAttrValue(SCARD_CLASS_IFD_PROTOCOL, 0x0201); } }

        public static UInt32 DEVICE_UNIT { get { return SCardAttrValue(SCARD_CLASS_SYSTEM, 0x0001); } }
        public static UInt32 DEVICE_FRIENDLY_NAME { get { return SCardAttrValue(SCARD_CLASS_SYSTEM, 0x0003); } }
        public UInt32 DEVICE_SYSTEM_NAME { get { return SCardAttrValue(SCARD_CLASS_SYSTEM, 0x0004); } }

        public static UInt32 ICC_PRESENCE { get { return SCardAttrValue(SCARD_CLASS_ICC_STATE, 0x0300); } }
        public static UInt32 ICC_INTERFACE_STATUS { get { return SCardAttrValue(SCARD_CLASS_ICC_STATE, 0x0301); } }
        public static UInt32 ATR_STRING { get { return SCardAttrValue(SCARD_CLASS_ICC_STATE, 0x0303); } }
        public static UInt32 ICC_TYPE_PER_ATR { get { return SCardAttrValue(SCARD_CLASS_ICC_STATE, 0x0304); } }

        public static UInt32 PROTOCOL_TYPES { get { return SCardAttrValue(SCARD_CLASS_PROTOCOL, 0x0120); } }

        public static UInt32 VENDOR_NAME { get { return SCardAttrValue(SCARD_CLASS_VENDOR_DEFINED, 0x0100); } }
        public static UInt32 VENDOR_IFD_TYPE { get { return SCardAttrValue(SCARD_CLASS_VENDOR_DEFINED, 0x0101); } }
        public static UInt32 VENDOR_IFD_VERSION { get { return SCardAttrValue(SCARD_CLASS_VENDOR_DEFINED, 0x0102); } }
        public static UInt32 VENDOR_IFD_SERIAL_NO { get { return SCardAttrValue(SCARD_CLASS_VENDOR_DEFINED, 0x0103); } }
    }

    /// <summary>
    /// Delegate for the CardInserted event
    /// </summary>
    public delegate void CardInsertedEventHandler(string reader);

    /// <summary>
    /// Delegate for the CardRemoved event
    /// </summary>
    public delegate void CardRemovedEventHandler(string reader);

    /// <summary>
    /// Abstract class that adds a basic event management to the ICard interface. 
    /// </summary>
    abstract public class CardBase : ICard
    {
        protected const uint INFINITE = 0xFFFFFFFF;
        protected const uint WAIT_TIME = 250;

        protected bool m_bRunCardDetection = true;
        protected Thread m_thread = null;

        /// <summary>
        /// Event handler for the card insertion
        /// </summary>
        public event CardInsertedEventHandler OnCardInserted = null;

        /// <summary>
        /// Event handler for the card removal
        /// </summary>
        public event CardRemovedEventHandler OnCardRemoved = null;

        ~CardBase()
        {
            // Stop any eventual card detection thread
            StopCardEvents();
        }

        #region Abstract method that implement the ICard interface
        abstract public string[] ListReaders();
        abstract public void Connect(string Reader, SHARE ShareMode, PROTOCOL PreferredProtocols);
        abstract public void Disconnect(DISCONNECT Disposition);
        abstract public APDUResponse Transmit(APDUCommand ApduCmd);
        abstract public void BeginTransaction();
        abstract public void EndTransaction(DISCONNECT Disposition);
        abstract public byte[] GetAttribute(UInt32 AttribId);
        #endregion

        /// <summary>
        /// This method should start a thread that checks for card insertion or removal
        /// </summary>
        /// <param name="Reader"></param>
        public void StartCardEvents(string Reader)
        {
            if (m_thread == null)
            {
                m_bRunCardDetection = true;

                m_thread = new Thread(new ParameterizedThreadStart(RunCardDetection));
                m_thread.Start(Reader);
            }
        }

        /// <summary>
        /// Stops the card events thread
        /// </summary>
        public void StopCardEvents()
        {
            if (m_thread != null)
            {
                int
                    nTimeOut = 10,
                    nCount = 0;
                bool m_bStop = false;
                m_bRunCardDetection = false;

                do
                {
                    if (nCount > nTimeOut)
                    {
                        m_thread.Abort();
                        break;
                    }

                    if (m_thread.ThreadState == ThreadState.Aborted)
                        m_bStop = true;

                    if (m_thread.ThreadState == ThreadState.Stopped)
                        m_bStop = true;

                    Thread.Sleep(200);
                    ++nCount;           // Manage time out
                }
                while (!m_bStop);

                m_thread = null;
            }
        }

        /// <summary>
        /// This function must implement a card detection mechanism.
        /// 
        /// When card insertion is detected, it must call the method CardInserted()
        /// When card removal is detected, it must call the method CardRemoved()
        /// 
        /// </summary>
        /// <param name="Reader">Name of the reader to scan for card event</param>
        abstract protected void RunCardDetection(object Reader);

        #region Event methods
        protected void CardInserted(string reader)
        {
            if (OnCardInserted != null)
                OnCardInserted(reader);
        }

        protected void CardRemoved(string reader)
        {
            if (OnCardRemoved != null)
                OnCardRemoved(reader);
        }
        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Connect In Private
Singapore Singapore
Software Architect, COM, .NET and Smartcard based security specialist.

I've been working in the software industry since I graduated in Electrical and Electronics Engineering. I chose software because I preferred digital to analog.

I started to program with 6802 machine code and evolved to the current .NET technologies... that was a long way.

For more than 20 years I have always worked in technical positions as I simply like to get my hands dirty and crack my brain when things don't go right!

After 12 years in the smart card industry I can claim a strong knowledge in security solutions based on those really small computers!
I've been back into business to design the licensing system for the enterprise solution for Consistel using a .NET smart card (yes they can run .NET CLR!)

I'm currently designing a micro-payment solution using the NXP DESFire EV1 with the ACSO6 SAM of ACS. I can then add a full proficient expertise on those systems and NFC payments.
This technology being under strict NDA by NXP I cannot publish any related article about it, however I can provide professional consulting for it.

You can contact me for professional matter by using the forum or via my LinkedIn profile.

Comments and Discussions