private bool IsThailandSupportCard()
        {
            string[]      readerNames;
            SCardProtocol proto;
            SCardState    state;

            byte[] atr;
            var    isSupportCard = true;

            var sc = _cardReader.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)
            {
                isSupportCard = false;
            }

            if (atr[0] == 0x3B && atr[1] == 0x68)       //Smart card tested with old type (Figure A.)
            {
                _apdu = new APDU_THAILAND_IDCARD_3B68();
            }
            else if (atr[0] == 0x3B && atr[1] == 0x78)   //Smart card tested with new type (figure B.)
            {
                _apdu = new APDU_THAILAND_IDCARD_3B68();
            }
            else if (atr[0] == 0x3B && atr[1] == 0x67)
            {
                _apdu = new APDU_THAILAND_IDCARD_3B67();
            }
            else if (atr[0] == 0x3B && atr[1] == 0x79)
            {
                _apdu = new APDU_THAILAND_IDCARD_3B68();
            }
            else
            {
                _error_code    = ECODE_UNSUPPORT_CARD;
                _error_message = "Card not support";
                isSupportCard  = false;
            }

            return(isSupportCard);
        }
Ejemplo n.º 2
0
        public Boolean Open(string readerName = null)
        {
            try
            {
                // Establish SCard context
                _hContext = new SCardContext();
                _hContext.Establish(SCardScope.System);

                // Create a _reader object using the existing context
                _reader = new SCardReader(_hContext);

                // Connect to the card
                if (readerName == null || readerName == String.Empty)
                {
                    // 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.Exclusive,
                                           SCardProtocol.T0 | SCardProtocol.T1);
                    CheckErr(_err);
                }
                else
                {
                    _err = _reader.Connect(readerName,
                                           SCardShareMode.Exclusive,
                                           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;

                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] == 0x68)       //Smart card tested with old type (Figure A.)
                {
                    _apdu = new APDU_THAILAND_IDCARD_3B68();
                }
                else if (atr[0] == 0x3B && atr[1] == 0x78)   //Smart card tested with new type (figure B.)
                {
                    _apdu = new APDU_THAILAND_IDCARD_3B68();
                }
                else if (atr[0] == 0x3B && atr[1] == 0x67)
                {
                    _apdu = new APDU_THAILAND_IDCARD_3B67();
                }
                else
                {
                    _error_code    = ECODE_UNSUPPORT_CARD;
                    _error_message = "Card not support";
                    Console.WriteLine(_error_message);
                    return(false);
                }


                return(true);
            }
            catch (PCSCException ex)
            {
                _error_code    = ECODE_SCardError;
                _error_message = "Err: " + ex.Message + " (" + ex.SCardError.ToString() + ")";
                Console.WriteLine(_error_message);
                return(false);
            }
        }
Ejemplo n.º 3
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 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.Exclusive,
                                           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;

                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] == 0x68)       //Smart card tested with old type (Figure A.)
                {
                    _apdu = new APDU_THAILAND_IDCARD_TYPE_02();
                }
                else if (atr[0] == 0x3B && atr[1] == 0x78)   //Smart card tested with new type (figure B.)
                {
                    _apdu = new APDU_THAILAND_IDCARD_TYPE_02();
                }
                else if (atr[0] == 0x3B && atr[1] == 0x79)   // add 2016-07-10
                {
                    _apdu = new APDU_THAILAND_IDCARD_TYPE_02();
                }
                else if (atr[0] == 0x3B && atr[1] == 0x67)
                {
                    _apdu = new APDU_THAILAND_IDCARD_TYPE_01();
                }
                else
                {
                    // try unlisted card with TYPE 02
                    _apdu = new APDU_THAILAND_IDCARD_TYPE_02();

                    // Check ID checksum
                    SendCommand(_apdu.CMD_SELECT());
                    if (Checksum(GetUTF8FromAsciiBytes(SendCommand(_apdu.CMD_CID()))))
                    {
                        return(true);
                    }

                    _error_code    = ECODE_UNSUPPORT_CARD;
                    _error_message = "Card not support";
                    Debug.Print(_error_message);
                    return(false);
                }


                return(true);
            }
            catch (PCSCException ex)
            {
                _error_code    = ECODE_SCardError;
                _error_message = "Open Err: " + ex.Message + " (" + ex.SCardError.ToString() + ")";
                Debug.Print(_error_message);
                return(false);
            }
        }
Ejemplo n.º 5
0
        public Boolean Open(string readerName = null)
        {
            try
            {
                // Establish SCard context
                _hContext = new SCardContext();
                _hContext.Establish(SCardScope.System);

                // Create a _reader object using the existing context
                _reader = new SCardReader(_hContext);

                // Connect to the card
                if (readerName == null || readerName == String.Empty)
                {
                    // 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.Exclusive,
                                SCardProtocol.T0 | SCardProtocol.T1);
                    CheckErr(_err);
                }
                else
                {
                    _err = _reader.Connect(readerName,
                                SCardShareMode.Exclusive,
                                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;
                    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] == 0x68)       //Smart card tested with old type (Figure A.)
                {
                    _apdu = new APDU_THAILAND_IDCARD_3B68();
                }
                else if (atr[0] == 0x3B && atr[1] == 0x78)   //Smart card tested with new type (figure B.) 
                {
                    _apdu = new APDU_THAILAND_IDCARD_3B68();
                }
                else if (atr[0] == 0x3B && atr[1] == 0x67)
                {
                    _apdu = new APDU_THAILAND_IDCARD_3B67();
                }
                else
                {
                    _error_code = ECODE_UNSUPPORT_CARD;
                    _error_message = "Card not support";
                    Console.WriteLine(_error_message);
                    return false;
                }


                return true;
            }
            catch (PCSCException ex)
            {
                _error_code = ECODE_SCardError;
                _error_message = "Err: " + ex.Message + " (" + ex.SCardError.ToString() + ")";
                Console.WriteLine(_error_message);
                return false;
            }
        }