public static void connect()
        {
            hContext = new SCardContext();
            hContext.Establish(SCardScope.System);

            string[] readerList = hContext.GetReaders();
            Boolean noReaders = readerList.Length <= 0;
            if (noReaders)
            {
                throw new PCSCException(SCardError.NoReadersAvailable, "Blad czytnika");
            }

            Console.WriteLine("Nazwa czytnika: " + readerList[0]);

            reader = new SCardReader(hContext);

            err = reader.Connect(readerList[0],
                SCardShareMode.Shared,
                SCardProtocol.T0 | SCardProtocol.T1);
            checkError(err);

            switch (reader.ActiveProtocol)
            {
                case SCardProtocol.T0:
                    protocol = SCardPCI.T0;
                    break;
                case SCardProtocol.T1:
                    protocol = SCardPCI.T1;
                    break;
                default:
                    throw new PCSCException(SCardError.ProtocolMismatch, "nieobslugiwany protokol: "+ reader.ActiveProtocol.ToString());
            }
        }
 public static void sendCommand(byte[] comand, String name)
 {
     byte[] recivedBytes = new byte[256];
     err = reader.Transmit(protocol, comand, ref recivedBytes);
     checkError(err);
     writeResponse(recivedBytes, name);
 }
Beispiel #3
0
        /// <summary>
        /// Throws an exception if <paramref name="sc"/> is not <see cref="F:PCSC.SCardError.Success"/>.
        /// </summary>
        /// <param name="sc">The error code returned from the native PC/SC library.</param>
        protected void ThrowExceptionOnSCardError(SCardError sc)
        {
            if (sc == SCardError.Success)
                return; // No Error

            // An error occurred during connect attempt.
            if (sc == SCardError.InvalidHandle)
                throw new InvalidContextException(sc);
            if (sc == SCardError.InvalidParameter)
                throw new InvalidProtocolException(sc);
            if (sc == SCardError.InvalidParameter)
                throw new InvalidParameterException(sc);
            if (sc == SCardError.InvalidValue)
                throw new InvalidValueException(sc);
            if (sc == SCardError.NoService)
                throw new NoServiceException(sc);
            if (sc == SCardError.NoSmartcard)
                throw new NoSmartcardException(sc);
            if (sc == SCardError.NotReady)
                throw new NotReadyException(sc);
            if (sc == SCardError.ReaderUnavailable)
                throw new ReaderUnavailableException(sc);
            if (sc == SCardError.SharingViolation)
                throw new SharingViolationException(sc);
            if (sc == SCardError.UnknownReader)
                throw new UnknownReaderException(sc);
            if (sc == SCardError.UnsupportedCard)
                throw new UnsupportedFeatureException(sc);
            if (sc == SCardError.CommunicationError)
                throw new CommunicationErrorException(sc);
            if (sc == SCardError.InternalError)
                throw new InternalErrorException(sc);
            if (sc == SCardError.UnpoweredCard)
                throw new UnpoweredCardException(sc);
            if (sc == SCardError.UnresponsiveCard)
                throw new UnresponsiveCardException(sc);
            if (sc == SCardError.RemovedCard)
                throw new RemovedCardException(sc);
            if (sc == SCardError.InsufficientBuffer)
                throw new InsufficientBufferException(sc);

            // unexpected error
            throw new PCSCException(sc);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="InvalidValueException"/> class.
 /// </summary>
 /// <param name="serr">System's error code</param>
 /// <param name="message">An error message text.</param>
 /// <param name="innerException">The inner exception.</param>
 public InvalidValueException(SCardError serr, string message, Exception innerException)
     : base(serr, message, innerException)
 {
 }
 public InvalidShareModeException(SCardError serr)
     : base(serr)
 {
 }
 public NoServiceException(SCardError serr, string message)
     : base(serr, message)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="UnsupportedFeatureException"/> class.
 /// </summary>
 /// <param name="serr">System's error code</param>
 public UnsupportedFeatureException(SCardError serr)
     : base(serr)
 {
 }
Beispiel #8
0
 /// <summary>
 /// Creates a new instance.
 /// </summary>
 /// <param name="serr">System's error code</param>
 public PCSCException(SCardError serr)
     : base(SCardHelper.StringifyError(serr)) {
     SCardError = serr;
 }
 /// <summary>
 /// Creates a new instance.
 /// </summary>
 /// <param name="serr">System's error code</param>
 /// <param name="message">Error message</param>
 public InvalidParameterException(SCardError serr, string message)
     : base(serr, message) {}
Beispiel #10
0
        private SCardError TryTransaction()
        {
            SCardError transError = this.device.sIO.GetReader().BeginTransaction();

            return(transError);
        }
Beispiel #11
0
        static void Main(string[] args)
        {
            try
            {
                SCardContext hContext = new SCardContext();
                hContext.Establish(SCardScope.System);

                var szReaders = hContext.GetReaders();

                if (szReaders.Length <= 0)
                {
                    throw new PCSCException(SCardError.NoReadersAvailable,
                                            "Could not find any Smartcard reader.");
                }

                Console.WriteLine("reader name: " + szReaders[1]);

                // Create a reader object using the existing context
                SCardReader reader = new SCardReader(hContext);

                // Connect to the card
                SCardError err = reader.Connect(szReaders[1],
                                                SCardShareMode.Shared,
                                                SCardProtocol.T0 | SCardProtocol.T1);
                CheckErr(err);

                IntPtr pioSendPci;
                switch (reader.ActiveProtocol)
                {
                case SCardProtocol.T0:
                    pioSendPci = SCardPCI.T0;
                    break;

                case SCardProtocol.T1:
                    pioSendPci = SCardPCI.T1;
                    break;

                default:
                    throw new PCSCException(SCardError.ProtocolMismatch,
                                            "Protocol not supported: "
                                            + reader.ActiveProtocol.ToString());
                }

                byte[] pbRecvBuffer = new byte[256];

                // Send SELECT command
                // Select command 0x00, 0xA4, 0x04, 0x00,
                //Length  0x08
                //AID A0A1A2A3A4000301
                byte[] cmd1 = new byte[] { 0x00, 0xA4, 0x04, 0x00, 0x08, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0x00, 0x03, 0x01 };
                err = reader.Transmit(pioSendPci, cmd1, ref pbRecvBuffer);
                CheckErr(err);

                //(6A82: The application to be selected could not be found.)

                Console.Write("Select response: ");
                for (int i = 0; i < pbRecvBuffer.Length; i++)
                {
                    Console.Write("{0:X2} ", pbRecvBuffer[i]);
                }
                Console.WriteLine();

                pbRecvBuffer = new byte[256];

                // Send test command
                byte[] cmd2 = new byte[] { 0x00, 0x00, 0x00, 0x00 };
                err = reader.Transmit(pioSendPci, cmd2, ref pbRecvBuffer);
                CheckErr(err);

                Console.Write("Test commad response: ");
                for (int i = 0; i < pbRecvBuffer.Length; i++)
                {
                    Console.Write("{0:X2} ", pbRecvBuffer[i]);
                }
                Console.WriteLine();

                hContext.Release();
            }
            catch (PCSCException ex)
            {
                Console.WriteLine("Ouch: "
                                  + ex.Message
                                  + " (" + ex.SCardError.ToString() + ")");
            }
            Console.ReadLine();
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="NoSmartcardException"/> class.
 /// </summary>
 /// <param name="serr">System's error code</param>
 /// <param name="message">An error message text.</param>
 /// <param name="innerException">The inner exception.</param>
 public NoSmartcardException(SCardError serr, string message, Exception innerException)
     : base(serr, message, innerException)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="NoSmartcardException"/> class.
 /// </summary>
 /// <param name="serr">System's error code</param>
 /// <param name="message">An error message text.</param>
 public NoSmartcardException(SCardError serr, string message)
     : base(serr, message)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="NoSmartcardException"/> class.
 /// </summary>
 /// <param name="serr">System's error code</param>
 public NoSmartcardException(SCardError serr)
     : base(serr)
 {
 }
Beispiel #15
0
 /// <summary>
 /// Creates a new instance.
 /// </summary>
 /// <param name="serr">System's error code</param>
 /// <param name="message">Error message</param>
 public InvalidContextException(SCardError serr, string message)
     : base(serr, message)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="SharingViolationException"/> class.
 /// </summary>
 /// <param name="serr">System's error code</param>
 /// <param name="message">An error message text.</param>
 /// <param name="innerException">The inner exception.</param>
 public SharingViolationException(SCardError serr, string message, Exception innerException)
     : base(serr, message, innerException)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="RemovedCardException"/> class.
 /// </summary>
 /// <param name="serr">System's error code</param>
 /// <param name="message">An error message text.</param>
 /// <param name="innerException">The inner exception.</param>
 public RemovedCardException(SCardError serr, string message, Exception innerException)
     : base(serr, message, innerException)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="InsufficientBufferException"/> class.
 /// </summary>
 /// <param name="serr">System's error code</param>
 public InsufficientBufferException(SCardError serr)
     : base(serr)
 {
 }
 public SharingViolationException(SCardError serr, string message)
     : base(serr, message)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="InsufficientBufferException"/> class.
 /// </summary>
 /// <param name="serr">System's error code</param>
 /// <param name="message">An error message text.</param>
 /// <param name="innerException">The inner exception.</param>
 public InsufficientBufferException(SCardError serr, string message, Exception innerException)
     : base(serr, message, innerException)
 {
 }
Beispiel #21
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PCSCException"/> class.
 /// </summary>
 /// <param name="serr">System's error code</param>
 /// <param name="message">An error message text.</param>
 /// <param name="innerException">The inner exception.</param>
 public PCSCException(SCardError serr, string message, Exception innerException)
     : base(message, innerException) {
     SCardError = serr;
 }
Beispiel #22
0
 /// <summary>
 /// Initializes a new instance of the <see cref="UnsupportedFeatureException"/> class.
 /// </summary>
 /// <param name="serr">System's error code</param>
 public UnsupportedFeatureException(SCardError serr)
     : base(serr)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="UnsupportedFeatureException"/> class.
 /// </summary>
 /// <param name="serr">System's error code</param>
 /// <param name="message">An error message text.</param>
 /// <param name="innerException">The inner exception.</param>
 public UnsupportedFeatureException(SCardError serr, string message, Exception innerException)
     : base(serr, message, innerException)
 {
 }
Beispiel #24
0
 /// <summary>
 /// Initializes a new instance of the <see cref="UnsupportedFeatureException"/> class.
 /// </summary>
 /// <param name="serr">System's error code</param>
 /// <param name="message">An error message text.</param>
 public UnsupportedFeatureException(SCardError serr, string message)
     : base(serr, message)
 {
 }
 public InsufficientBufferException(SCardError serr)
     : base(serr) { }
Beispiel #26
0
 /// <summary>
 /// Initializes a new instance of the <see cref="UnsupportedFeatureException"/> class.
 /// </summary>
 /// <param name="serr">System's error code</param>
 /// <param name="message">An error message text.</param>
 /// <param name="innerException">The inner exception.</param>
 public UnsupportedFeatureException(SCardError serr, string message, Exception innerException)
     : base(serr, message, innerException)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="InvalidValueException"/> class.
 /// </summary>
 /// <param name="serr">System's error code</param>
 public InvalidValueException(SCardError serr)
     : base(serr)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="UnresponsiveCardException"/> class.
 /// </summary>
 /// <param name="serr">System's error code</param>
 public UnresponsiveCardException(SCardError serr)
     : base(serr)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="UnresponsiveCardException"/> class.
 /// </summary>
 /// <param name="serr">System's error code</param>
 /// <param name="message">An error message text.</param>
 public UnresponsiveCardException(SCardError serr, string message)
     : base(serr, message)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="UnresponsiveCardException"/> class.
 /// </summary>
 /// <param name="serr">System's error code</param>
 /// <param name="message">An error message text.</param>
 public UnresponsiveCardException(SCardError serr, string message)
     : base(serr, message)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="SharingViolationException"/> class.
 /// </summary>
 /// <param name="serr">System's error code</param>
 /// <param name="message">An error message text.</param>
 public SharingViolationException(SCardError serr, string message)
     : base(serr, message)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="UnresponsiveCardException"/> class.
 /// </summary>
 /// <param name="serr">System's error code</param>
 /// <param name="message">An error message text.</param>
 /// <param name="innerException">The inner exception.</param>
 public UnresponsiveCardException(SCardError serr, string message, Exception innerException)
     : base(serr, message, innerException)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="RemovedCardException"/> class.
 /// </summary>
 /// <param name="serr">System's error code</param>
 /// <param name="message">An error message text.</param>
 public RemovedCardException(SCardError serr, string message)
     : base(serr, message)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="InvalidScopeTypeException"/> class.
 /// </summary>
 /// <param name="serr">System's error code</param>
 public InvalidScopeTypeException(SCardError serr)
     : base(serr)
 {
 }
 /// <summary>
 /// Creates a new instance.
 /// </summary>
 /// <param name="serr">System's error code</param>
 public InvalidParameterException(SCardError serr)
     : base(serr) {}
 /// <summary>
 /// Initializes a new instance of the <see cref="InvalidScopeTypeException"/> class.
 /// </summary>
 /// <param name="serr">System's error code</param>
 /// <param name="message">An error message text.</param>
 public InvalidScopeTypeException(SCardError serr, string message)
     : base(serr, message)
 {
 }
 public SharingViolationException(SCardError serr)
     : base(serr)
 {
 }
Beispiel #38
0
        static void Main(string[] args)
        {
            // Establish Smartcard context
            SCardContext ctx = new SCardContext();

            ctx.Establish(SCardScope.System);

            string[] readernames = ctx.GetReaders();
            if (readernames == null || readernames.Length < 1)
            {
                throw new Exception("You need at least one reader in order to run this example.");
            }

            // we will use the first reader for the transmit test.
            string readername = readernames[0];

            SCardReader reader = new SCardReader(ctx);
            SCardError  rc     = reader.Connect(
                readername,
                SCardShareMode.Shared,
                SCardProtocol.Any);

            if (rc != SCardError.Success)
            {
                Console.WriteLine("Could not connect to card in reader " + readername + "\n"
                                  + "Error: " + SCardHelper.StringifyError(rc));
                return;
            }

            // Build a GET CHALLENGE command
            CommandApdu apdu = new CommandApdu(
                IsoCase.Case2Short,
                reader.ActiveProtocol);

            apdu.CLA = 0x00; // Class
            apdu.INS = 0x84; // Instruction: GET CHALLENGE
            apdu.P1  = 0x00; // Parameter 1
            apdu.P2  = 0x00; // Parameter 2
            apdu.Le  = 0x08; // Expected length of the returned data

            // convert the APDU object into an array of bytes
            byte[] cmd = apdu.ToArray();
            // prepare a buffer for response APDU -> LE + 2 bytes (SW1 SW2)
            byte[] outbuf = new byte[apdu.ExpectedResponseLength];

            rc = reader.Transmit(
                cmd,
                ref outbuf);

            if (rc == SCardError.Success)
            {
                Console.WriteLine("Ok.");

                if (outbuf != null)
                {
                    ResponseApdu response = new ResponseApdu(outbuf, apdu.Case, apdu.Protocol);
                    if (response.IsValid)
                    {
                        Console.WriteLine("SW1 SW2 = {0:X2} {1:X2}", response.SW1, response.SW2);
                        if (response.HasData)
                        {
                            Console.Write("Data: ");
                            for (int i = 0; i < (response.DataSize); i++)
                            {
                                Console.Write("{0:X2} ", response.FullApdu[i]);
                            }
                            Console.WriteLine("");
                        }
                    }
                }
            }
            else
            {
                // Error
                Console.WriteLine(SCardHelper.StringifyError(rc));
            }

            return;
        }
 public SharingViolationException(SCardError serr, string message, Exception innerException)
     : base(serr, message, innerException)
 {
 }
 /// <summary>
 /// Creates a new instance.
 /// </summary>
 /// <param name="serr">System's error code</param>
 public InvalidParameterException(SCardError serr)
     : base(serr)
 {
 }
Beispiel #41
0
 /// <summary>
 /// Creates a new instance.
 /// </summary>
 /// <param name="serr">System's error code</param>
 /// <param name="message">An error message text.</param>
 public PCSCException(SCardError serr, string message)
     : base(message) {
     SCardError = serr;
 }
 /// <summary>
 /// Creates a new instance.
 /// </summary>
 /// <param name="serr">System's error code</param>
 /// <param name="message">Error message</param>
 /// <param name="innerException">Inner exception</param>
 public InvalidParameterException(SCardError serr, string message, Exception innerException)
     : base(serr, message, innerException)
 {
 }
Beispiel #43
0
		/// <summary>
		/// Serialization constructor
		/// </summary>
		/// <param name="info"></param>
		/// <param name="context"></param>
	    protected PCSCException(SerializationInfo info, StreamingContext context) 
			: base(info, context)
	    {
		    SCardError = (SCardError)info.GetValue(SCARD_ERROR_SERIALIZATION_NAME, typeof (SCardError));
	    }
Beispiel #44
0
 /// <summary>
 /// Returns a human readable text for the given PC/SC error code.
 /// </summary>
 /// <param name="code">Error or return code.</param>
 /// <returns>A human readable string.</returns>
 /// <remarks>Warning! This method behaves differently compared to the original PC/SC-Lite pcsc_stringify_error function. Instead of the (const) variable name it returns a short text description.</remarks>
 public static string StringifyError(SCardError code)
 {
     return(GetAttrDesc(code));
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="UnsupportedFeatureException"/> class.
 /// </summary>
 /// <param name="serr">System's error code</param>
 /// <param name="message">An error message text.</param>
 public UnsupportedFeatureException(SCardError serr, string message)
     : base(serr, message)
 {
 }
Beispiel #46
0
        public Boolean Open(string readerName = null)
        {
            try
            {
                // delay 1.5 second for ATR reading.
                Thread.Sleep(1500);

                _hContext = _contextFactory.Establish(SCardScope.System);
                _reader   = new SCardReader(_hContext);

                // Connect to the card
                if (String.IsNullOrEmpty(readerName))
                {
                    // Open first avaliable reader.
                    // Retrieve the list of Smartcard _readers
                    string[] szReaders = _hContext.GetReaders();
                    if (szReaders.Length <= 0)
                    {
                        throw new PCSCException(SCardError.NoReadersAvailable,
                                                "Could not find any Smartcard _reader.");
                    }

                    _err = _reader.Connect(szReaders[0],
                                           SCardShareMode.Shared,
                                           SCardProtocol.T0 | SCardProtocol.T1);
                    CheckErr(_err);
                }
                else
                {
                    _err = _reader.Connect(readerName,
                                           SCardShareMode.Shared,
                                           SCardProtocol.T0 | SCardProtocol.T1);
                    CheckErr(_err);
                }


                _pioSendPci = new IntPtr();
                switch (_reader.ActiveProtocol)
                {
                case SCardProtocol.T0:
                    _pioSendPci = SCardPCI.T0;
                    break;

                case SCardProtocol.T1:
                    _pioSendPci = SCardPCI.T1;
                    break;

                case SCardProtocol.Raw:
                    _pioSendPci = SCardPCI.Raw;
                    break;

                default:
                    throw new PCSCException(SCardError.ProtocolMismatch,
                                            "Protocol not supported: "
                                            + _reader.ActiveProtocol.ToString());
                }

                string[]      readerNames;
                SCardProtocol proto;
                SCardState    state;
                byte[]        atr;

                var sc = _reader.Status(
                    out readerNames,    // contains the reader name(s)
                    out state,          // contains the current state (flags)
                    out proto,          // contains the currently used communication protocol
                    out atr);           // contains the ATR

                if (atr == null || atr.Length < 2)
                {
                    return(false);
                }

                if (atr[0] == 0x3B && atr[1] == 0x67)
                {
                    /* corruption card */
                    _apdu = new APDU_THAILAND_IDCARD_TYPE_01();
                }
                else
                {
                    _apdu = new APDU_THAILAND_IDCARD_TYPE_02();
                }

                // select MOI Applet
                if (SelectApplet())
                {
                    return(true);
                }
                else
                {
                    _error_code    = ECODE_UNSUPPORT_CARD;
                    _error_message = "SmartCard not support(Cannot select Ministry of Interior Applet.)";
                    return(false);
                }
            }
            catch (PCSCException ex)
            {
                _error_code    = ECODE_SCardError;
                _error_message = "Open Err: " + ex.Message + " (" + ex.SCardError.ToString() + ")";
                Debug.Print(_error_message);
                return(false);
            }
        }
 public NoServiceException(SCardError serr)
     : base(serr)
 {
 }
Beispiel #48
0
 /// <summary>
 /// Initializes a new instance of the <see cref="UnpoweredCardException"/> class.
 /// </summary>
 /// <param name="serr">System's error code</param>
 public UnpoweredCardException(SCardError serr)
     : base(serr)
 {
 }
 public NoServiceException(SCardError serr, string message, Exception innerException)
     : base(serr, message, innerException)
 {
 }
Beispiel #50
0
 /// <summary>
 /// Initializes a new instance of the <see cref="UnpoweredCardException"/> class.
 /// </summary>
 /// <param name="serr">System's error code</param>
 /// <param name="message">An error message text.</param>
 public UnpoweredCardException(SCardError serr, string message)
     : base(serr, message)
 {
 }
 public InsufficientBufferException(SCardError serr, string message, Exception innerException)
     : base(serr, message, innerException) { }
Beispiel #52
0
 /// <summary>
 /// Initializes a new instance of the <see cref="UnpoweredCardException"/> class.
 /// </summary>
 /// <param name="serr">System's error code</param>
 /// <param name="message">An error message text.</param>
 /// <param name="innerException">The inner exception.</param>
 public UnpoweredCardException(SCardError serr, string message, Exception innerException)
     : base(serr, message, innerException)
 {
 }
 public InvalidShareModeException(SCardError serr, string message)
     : base(serr, message)
 {
 }
Beispiel #54
0
 public InvalidShareModeException(SCardError serr)
     : base(serr)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="InvalidValueException"/> class.
 /// </summary>
 /// <param name="serr">System's error code</param>
 /// <param name="message">An error message text.</param>
 public InvalidValueException(SCardError serr, string message)
     : base(serr, message)
 {
 }
Beispiel #56
0
 public InvalidShareModeException(SCardError serr, string message)
     : base(serr, message)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="UnresponsiveCardException"/> class.
 /// </summary>
 /// <param name="serr">System's error code</param>
 public UnresponsiveCardException(SCardError serr)
     : base(serr)
 {
 }
Beispiel #58
0
 public InvalidShareModeException(SCardError serr, string message, Exception innerException)
     : base(serr, message, innerException)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="UnresponsiveCardException"/> class.
 /// </summary>
 /// <param name="serr">System's error code</param>
 /// <param name="message">An error message text.</param>
 /// <param name="innerException">The inner exception.</param>
 public UnresponsiveCardException(SCardError serr, string message, Exception innerException)
     : base(serr, message, innerException)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="SharingViolationException"/> class.
 /// </summary>
 /// <param name="serr">System's error code</param>
 public SharingViolationException(SCardError serr)
     : base(serr)
 {
 }