Click here to Skip to main content
15,884,628 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 32K   1.7K   18  
This article describes a solution to access API and resources that are not available with WinRT.
/**
 * @author Olivier ROUIT
 * 
 * @license CPL, CodeProject license 
 */

using Core.Util;
using Core.WinRTSCard;
using Core.Mvvm;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace SCardDemoApp
{
    class APDUExchangeViewModel : BaseViewModel<APDUExchangeViewModel>
    {
        class ApduCmdProp
        {
            public const string
                Class = "Class",
                Ins = "Ins",
                P1 = "P1",
                P2 = "P2",
                Le = "Le",
                Data = "Data";
        }

        class ApduRespProp
        {
            public const string
                SW1 = "SW1",
                SW2 = "SW2",
                ResponseData = "ResponseData";
        }

        const string 
            ReaderNameProp = "ReaderName",
            ConnectedProp = "Connected",
            StatusProp = "Status";

        #region Fields

        private Smartcard card = null;
        private APDUCommand apduCommand = null;
        private APDUResponse apduResponse = null;
        private string readerName = string.Empty;
        private bool connected = false;
        private string status;

        #endregion

        public APDUExchangeViewModel(Smartcard card)
        {
            this.card = card;
            apduCommand = new APDUCommand();
            apduCommand.Class = 0xA0;
            apduCommand.Ins = 0xA4;
            apduCommand.Data = new byte[] { 0x3F, 0x00 };

            CreateConnectCommand();
            CreateDisconnectCommand();
            CreateTransmitCommand();
        }

        #region Internal Properties 

        public bool Connected
        {
            get { return connected; }
            set
            {
                connected = value;
                RaisePropertyChanged(ConnectedProp);
                ConnectCommand.RaiseCanExecuteChanged();
                DisconnectCommand.RaiseCanExecuteChanged();
                TransmitCommand.RaiseCanExecuteChanged();
            }
        }

        public string[] ReaderList
        {
            get 
            { 
                return card.ListReaders(); 
            }
        }

        public string ReaderName
        {
            get 
            { 
                return readerName; 
            }

            set
            {
                readerName = value;
                RaisePropertyChanged(ReaderNameProp);
                ConnectCommand.RaiseCanExecuteChanged();
                DisconnectCommand.RaiseCanExecuteChanged();
            }
        }
        
        #endregion

        #region APDUCommand view model 

        public string Class
        {
            get 
            { 
                return Core.Util.Convert.HexToString(apduCommand.Class); 
            }

            set
            {
                apduCommand.Class = Core.Util.Convert.ParseByteToHex(value);
                RaisePropertyChanged(ApduCmdProp.Class);
            }
        }

        public string Ins
        {
            get 
            { 
                return Core.Util.Convert.HexToString(apduCommand.Ins);
            }

            set
            {
                apduCommand.Ins = Core.Util.Convert.ParseByteToHex(value);
                RaisePropertyChanged(ApduCmdProp.Ins);
            }
        }

        public string P1
        {
            get 
            {
                return Core.Util.Convert.HexToString(apduCommand.P1);
            }

            set
            {
                apduCommand.P1 = Core.Util.Convert.ParseByteToHex(value);
                RaisePropertyChanged(ApduCmdProp.P1);
            }
        }

        public string P2
        {
            get { return Core.Util.Convert.HexToString(apduCommand.P2); }
            set
            {
                apduCommand.P2 = Core.Util.Convert.ParseByteToHex(value);
                RaisePropertyChanged(ApduCmdProp.P2);
            }
        }

        public string Le
        {
            get { return apduCommand.Le.ToString(); }
            set
            {
                apduCommand.Le = byte.Parse(value);
                RaisePropertyChanged(ApduCmdProp.Le);
            }
        }

        public string Data
        {
            get { return Core.Util.Convert.BufferToString(apduCommand.Data); }
            set
            {
                apduCommand.Data = Core.Util.Convert.ParseBufferToHex(value);
                RaisePropertyChanged(ApduCmdProp.Data);
            }
        }

        #endregion

        #region APDUResponse view model

        public string SW1
        {
            get
            {
                return apduResponse != null ? Core.Util.Convert.HexToString(apduResponse.SW1) : string.Empty;
            }
        }

        public string SW2
        {
            get
            {
                return apduResponse != null ? Core.Util.Convert.HexToString(apduResponse.SW2) : string.Empty;
            }
        }

        public string ResponseData
        {
            get
            {
                return apduResponse != null ? Core.Util.Convert.BufferToString(apduResponse.Data) : string.Empty; 
            }
        }

        public string Status
        {
            get { return status; }
            set
            {
                status = value;
                RaisePropertyChanged(StatusProp);
            }
        }

        #endregion

        #region Commands

        public RelayCommand ConnectCommand
        {
            get;
            internal set;
        }

        private bool CanExecuteConnectCommand()
        {
            return !Connected && !string.IsNullOrEmpty(ReaderName);
        }

        private void CreateConnectCommand()
        {
            ConnectCommand = new RelayCommand(ConnectExecute, CanExecuteConnectCommand);
        }

        public void ConnectExecute()
        {
            try
            {
                card.Connect(ReaderName, SHARE.Shared, PROTOCOL.T0orT1);
                Status = "Card connected.";
                Connected = true;
            }
            catch (Exception ex)
            {
                Status = ex.Message;
                Connected = false;
            }
        }

        public RelayCommand DisconnectCommand
        {
            get;
            internal set;
        }

        private bool CanExecuteDisconnectCommand()
        {
            return Connected;
        }

        private void CreateDisconnectCommand()
        {
            DisconnectCommand = new RelayCommand(DisconnectExecute, CanExecuteDisconnectCommand);
        }

        public void DisconnectExecute()
        {
            try
            {
                card.Disconnect(DISCONNECT.Unpower);
                Status = "Card disconnected.";

                Connected = false;
            }
            catch (Exception ex)
            {
                Status = ex.Message;
                Connected = false;
            }
        }

        public RelayCommand TransmitCommand
        {
            get;
            internal set;
        }

        private bool CanExecuteTransmitCommand()
        {
            return Connected;
        }

        private void CreateTransmitCommand()
        {
            TransmitCommand = new RelayCommand(TransmitExecute, CanExecuteTransmitCommand);
        }

        public void TransmitExecute()
        {
            try
            {
                apduResponse = card.Transmit(apduCommand);

                Status = "Transmit successful.";
                RaisePropertyChanged(ApduRespProp.SW1);
                RaisePropertyChanged(ApduRespProp.SW2);
                RaisePropertyChanged(ApduRespProp.ResponseData);
            }
            catch (Exception ex)
            {
                Status = ex.Message + ". Please disconnect and reconnect to continue";
            }

        }

        #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