private void Open()
        {
            try
            {
                Thread.Sleep(1500);

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

                error = reader.Connect(szReaders[0], SCardShareMode.Shared, SCardProtocol.T0 | SCardProtocol.T1);
                HandleError();

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

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

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

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

                error = reader.Status(out string[] readerNames, out SCardState state, out SCardProtocol protocol, out byte[] atrs);
                HandleError();

                if (atrs == null || atrs.Length < 2)
                {
                    throw new PCSCException(SCardError.InvalidAtr);
                }

                if (atrs[0] == 0x3B && atrs[1] == 0x67)
                {
                    apdu = new ThaiNationalIDCardAPDUCommandType01();
                }

                if (!SelectApplet())
                {
                    throw new Exception("SmartCard not support (Can't select Ministry of Interior Applet).");
                }
            }
            catch (PCSCException e)
            {
                throw e;
            }
        }
Beispiel #2
0
        // Połączenie z czytnikiem
        private static bool ConnectReader(ISCardReader reader, string name)
        {
            // Przypisanie połączenia do zmiennej
            var rc = reader.Connect(name, SCardShareMode.Shared, SCardProtocol.Any);

            // Jeżeli połączono z sukcesem
            if (rc == SCardError.Success)
            {
                return(true);
            }
            // Jeżeli nie udało się połączyć
            return(false);
        }
Beispiel #3
0
        /// <summary>
        /// Connect to reader using <paramref name="name"/>
        /// </summary>
        /// <param name="reader">Smartcard reader instance</param>
        /// <param name="name">Requested reader name</param>
        /// <returns><c>true</c> if connection attempt was successful</returns>
        private static bool ConnectReader(ISCardReader reader, string name)
        {
            Console.Write($"Trying to connect to reader.. {name}");
            var rc = reader.Connect(name, SCardShareMode.Shared, SCardProtocol.Any);

            if (rc == SCardError.Success)
            {
                Console.WriteLine(" done.");
                return(true);
            }

            Console.WriteLine(" failed. No smart card present? " + SCardHelper.StringifyError(rc) + "\n");
            return(false);
        }
Beispiel #4
0
        public override IOption <IReader> Content()
        {
            Debug.WriteLine("GetEnumerator Reader TreadID " + Thread.CurrentThread.ManagedThreadId);
            var cardError = _reader.Connect(_readerName, SCardShareMode.Shared, SCardProtocol.Any);

            if (cardError != SCardError.Success)
            {
                Console.WriteLine("Could not begin transaction. {0}", SCardHelper.StringifyError(cardError));
                return(new Option <IReader>());
            }
            SCardProtocol proto;
            SCardState    state;

            byte[] atr;
            var    readerNames = new[] { _readerName };
            var    sc          = _reader.Status(
                out readerNames,
                out state,
                out proto,
                out atr
                );

            if (sc != SCardError.Success)
            {
                Console.WriteLine("Could not begin transaction. {0}", SCardHelper.StringifyError(sc));
                return(new Option <IReader>());
            }

            sc = _reader.BeginTransaction();
            if (sc != SCardError.Success)
            {
                Console.WriteLine("Could not begin transaction. {0}", SCardHelper.StringifyError(sc));
                return(new Option <IReader>());
            }

            Console.WriteLine("Connected with protocol {0} in state {1}", proto, state);
            Console.WriteLine("Card ATR: {0}", BitConverter.ToString(atr));

            return(new Option <IReader>(
                       new WrappedReader(
                           //new LogedReader(
                           _reader
                           //)
                           )
                       ));
        }
Beispiel #5
0
        public virtual void Connect(string readerName, SCardShareMode mode, SCardProtocol proto)
        {
            if (readerName == null)             // Invalid reader name?
            {
                throw new ArgumentNullException("readerName");
            }
            if (proto == SCardProtocol.Unset)   // Invalid protocol?
            {
                throw new InvalidProtocolException(SCardError.InvalidValue);
            }
            if (((long)mode) == 0)              // Invalid sharing mode?
            {
                throw new InvalidShareModeException(SCardError.InvalidValue);
            }

            SCardError sc = reader.Connect(readerName, mode, proto);

            // Throws an exception if sc != SCardError.Success
            ThrowExceptionOnSCardError(sc);
        }
Beispiel #6
0
        /// <summary>Connects the specified reader.</summary>
        /// <param name="readerName">Name of the reader.</param>
        /// <param name="mode">The share mode.</param>
        /// <param name="protocol">The communication protocol. <seealso cref="ISCardReader.Connect(string,SCardShareMode,SCardProtocol)" /></param>
        public virtual void Connect(string readerName, SCardShareMode mode, SCardProtocol protocol)
        {
            if (readerName == null)
            {
                throw new ArgumentNullException(nameof(readerName));
            }

            if (protocol == SCardProtocol.Unset)
            {
                throw new InvalidProtocolException(SCardError.InvalidValue);
            }

            if (((long)mode) == 0)
            {
                throw new InvalidShareModeException(SCardError.InvalidValue);
            }

            var sc = _reader.Connect(readerName, mode, protocol);

            // Throws an exception if sc != SCardError.Success
            sc.ThrowIfNotSuccess();
        }