Exemplo n.º 1
0
        void OnNetworkReaderListInstantiated(SCardReaderList_CcidOverNetwork NetworkReaders)
        {
            if (InvokeRequired)
            {
                IAsyncResult ar = this.BeginInvoke(new OnNetworkReaderListInstantiatedInvoker(OnNetworkReaderListInstantiated), NetworkReaders);
                this.EndInvoke(ar);
                return;
            }

            if (NetworkReaders == null)
            {
                MessageBox.Show(this, "Failed to connect to the network reader. Please check your hardware and your configuration, and click the 'Reader' link to try to re-open the reader, or to select another reader.", Title, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            SCardReader_CcidOver NetworkReader = NetworkReaders.GetReader(0);

            if (NetworkReader == null)
            {
                MessageBox.Show(this, "The network reader has not been correctly activated. Please click the 'Reader' link to try to re-open the reader, or to select another reader.", Title, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            ReaderList = NetworkReaders;
            Reader     = NetworkReader;
            DisplayReaderPresent();

            NetworkReader.StartMonitor(new SCardReader_CcidOver.StatusChangeCallback(ReaderStatusChanged));
        }
Exemplo n.º 2
0
        void MainFormShown(object sender, EventArgs e)
        {
            waitForm = new WaitForm();

            SplashForm f = new SplashForm();

            f.ShowDialog();

            if (AppConfig.ReadBoolean("reader_reconnect"))
            {
                ReaderList = new SCardReaderList();
                if (ReaderList != null)
                {
                    string reader_name = AppConfig.ReadString("reader_name");
                    if (ReaderList.Contains(reader_name))
                    {
                        Reader = new SCardReader(reader_name);
                        DisplayReaderPresent();
                        Reader.StartMonitor(new SCardReader.StatusChangeCallback(ReaderStatusChanged));
                        return;
                    }
                    ReaderList = null;
                }
            }

            SelectReader();
        }
Exemplo n.º 3
0
        void SelectReader()
        {
            if ((ReaderList != null) || (Reader != null))
            {
                if (!UserWantsToClose())
                {
                    return;
                }

                CloseDevice();
            }

            ReaderSelectForm f  = new ReaderSelectForm();
            bool             rc = (f.ShowDialog(this) == DialogResult.OK);

            if (rc)
            {
                string ReaderName = f.SelectedReader;

                ReaderList = new SCardReaderList();
                if (ReaderList != null)
                {
                    Reader = ReaderList.GetReader(ReaderName);
                    if (Reader != null)
                    {
                        DisplayReaderPresent();
                        Reader.StartMonitor(new SCardReader.StatusChangeCallback(ReaderStatusChanged));
                        return;
                    }
                    else
                    {
                        MessageBox.Show(this,
                                        "Failed to connect to the PC/SC reader. Please click the 'Reader' link to select another reader.",
                                        Title,
                                        MessageBoxButtons.OK,
                                        MessageBoxIcon.Exclamation);
                    }
                    ReaderList = null;
                }
                else
                {
                    MessageBox.Show(this,
                                    "The PC/SC subsystem doesn't seem to be running. Please check your system's configuration.",
                                    Title,
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Exclamation);
                }
            }
        }
Exemplo n.º 4
0
 void MainFormShown(object sender, EventArgs e)
 {
     if (AppConfig.ReadBoolean("reader_reconnect"))
     {
         if (AppConfig.ReadString("reader_mode") == "serial")
         {
             SCardReaderList_CcidOverSerial.BackgroundInstantiate(
                 new SCardReaderList_CcidOverSerial.BackgroundInstantiateCallback(OnSerialReaderListInstantiated),
                 AppConfig.ReadString("serial_port"),
                 true,
                 AppConfig.ReadBoolean("serial_use_lpcd"));
         }
         else
         {
             ReaderList = new SCardReaderList();
             if (ReaderList != null)
             {
                 string reader_name = AppConfig.ReadString("reader_name");
                 Logger.Trace("Looking for PC/SC reader '" + reader_name + "'");
                 if (ReaderList.Contains(reader_name))
                 {
                     Logger.Trace("Found!");
                     Reader = new SCardReader(reader_name);
                     DisplayReaderPresent();
                     Reader.StartMonitor(new SCardReader.StatusChangeCallback(ReaderStatusChanged));
                     return;
                 }
                 else
                 {
                     Logger.Trace("Not found!");
                     MessageBox.Show(this,
                                     "Failed to reconnect to the previous PC/SC reader. Please click the 'Reader' link to select another reader.",
                                     Title, MessageBoxButtons.OK,
                                     MessageBoxIcon.Exclamation);
                 }
                 ReaderList = null;
             }
             else
             {
                 MessageBox.Show(this,
                                 "The PC/SC subsystem doesn't seem to be running. Please check your system's configuration.",
                                 Title,
                                 MessageBoxButtons.OK,
                                 MessageBoxIcon.Exclamation);
             }
         }
     }
 }
Exemplo n.º 5
0
        void StartReaderMonitor()
        {
            if (reader_list != null)
            {
                reader_list.StopMonitor();
                reader_list = null;
            }

            string s = "";

            switch (Settings.ContextScope)
            {
            case SCARD.SCOPE_USER:
                s += "User scope";
                break;

            case SCARD.SCOPE_SYSTEM:
                s += "System scope";
                break;

            default:
                s += "Scope=" + Settings.ContextScope.ToString();
                break;
            }

            s += ", ";

            if (Settings.ListGroup.Equals(SCARD.ALL_READERS))
            {
                s += "All readers";
            }
            else
            if (Settings.ListGroup.Equals(SCARD.DEFAULT_READERS))
            {
                s += "Default readers";
            }
            else
            {
                s += Settings.ListGroup;
            }

            lbOptions.Text = s;

            reader_list = new SCardReaderList(Settings.ContextScope, Settings.ListGroup);
            reader_list.StartMonitor(new SCardReaderList.StatusChangeCallback(ReaderListChanged));
        }
Exemplo n.º 6
0
        void MainFormFormClosed(object sender, FormClosedEventArgs e)
        {
            if (reader_list != null)
            {
                reader_list.StopMonitor();
                reader_list = null;
            }

            for (int i = 0; i < card_forms.Count; i++)
            {
                CardForm f = card_forms[i];
                f.Close();
                f.Dispose();
            }

            Settings.Save();
        }
Exemplo n.º 7
0
        void CloseDevice()
        {
            if (Reader != null)
            {
                Logger.Trace("Release the Reader...");
                Reader.Release();
                Reader = null;
            }
            if (ReaderList != null)
            {
                Logger.Trace("Release the ReaderList...");
                ReaderList.Release();
                ReaderList = null;
            }

            DisplayReaderState(0, null);
            DisplayReaderAbsent();
        }
Exemplo n.º 8
0
        void ReaderStatusChanged(uint ReaderState, CardBuffer CardAtr)
        {
            /* The ReaderStatusChanged function is called as a delegate (callback) by the SCardReader object    */
            /* within its backgroung thread. Therefore we must use the BeginInvoke syntax to switch back from   */
            /* the context of the background thread to the context of the application's main thread. Overwise   */
            /* we'll get a security violation when trying to access the window's visual components (that belong */
            /* to the application's main thread and can't be safely manipulated by background threads).         */
            if (InvokeRequired)
            {
                this.BeginInvoke(new ReaderStatusChangedInvoker(ReaderStatusChanged), ReaderState, CardAtr);
                return;
            }

            DisplayReaderState(ReaderState, CardAtr);

            if (ReaderState == SCARD.STATE_UNAWARE)
            {
                if (cardChannel != null)
                {
                    cardChannel.Disconnect();
                    cardChannel = null;
                }
                if (Reader != null)
                {
                    Reader.StopMonitor();
                    Reader.Release();
                    Reader = null;
                }
                if (ReaderList != null)
                {
                    ReaderList.Release();
                    ReaderList = null;
                }

                btnReadAgain.Enabled = false;
                btnWrite.Enabled     = false;

                MessageBox.Show(this,
                                "The reader we were working on has been removed from the system. Please click the 'Reader' link to select another reader.",
                                Title,
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Exclamation);

                DisplayReaderState(0, null);
                DisplayReaderAbsent();
                return;
            }

            if ((ReaderState & SCARD.STATE_EMPTY) != 0)
            {
                if (cardChannel != null)
                {
                    cardChannel.Disconnect();
                    cardChannel = null;
                }

                btnReadAgain.Enabled = false;
                btnWrite.Enabled     = false;
            }
            else if ((ReaderState & SCARD.STATE_UNAVAILABLE) != 0)
            {
                btnReadAgain.Enabled = false;
                btnWrite.Enabled     = false;
            }
            else if ((ReaderState & SCARD.STATE_MUTE) != 0)
            {
                btnReadAgain.Enabled = false;
                btnWrite.Enabled     = false;
            }
            else if ((ReaderState & SCARD.STATE_INUSE) != 0)
            {
            }
            else if ((ReaderState & SCARD.STATE_PRESENT) != 0)
            {
                if (cardChannel == null)
                {
                    /* New card -> leave edit mode */
                    cardChannel = Reader.GetChannel();

                    DisplayWorking(false);

                    if (cardChannel.Connect())
                    {
                        cardReaderThread = new Thread(ReadCardFirst);
                        cardReaderThread.Start();
                    }
                    else
                    {
                        ClearDisplay();
                        MessageBox.Show(this,
                                        "Failed to connect to the card in the reader. Please verify that you don't have another application running in the background, that tries to access to the smartcards in the same reader.",
                                        Title,
                                        MessageBoxButtons.OK,
                                        MessageBoxIcon.Exclamation);
                        cardChannel = null;
                    }
                }
            }
        }
Exemplo n.º 9
0
        void SelectReader()
        {
            if ((ReaderList != null) || (Reader != null))
            {
                if (!UserWantsToClose())
                {
                    return;
                }

                CloseDevice();
            }

            ReaderSelectAnyForm f = new ReaderSelectAnyForm();
            bool rc = (f.ShowDialog(this) == DialogResult.OK);

            if (rc)
            {
                if (f.Mode == "pc/sc")
                {
                    string ReaderName;
                    f.GetPCSCParameters(out ReaderName);

                    ReaderList = new SCardReaderList();
                    if (ReaderList != null)
                    {
                        Reader = ReaderList.GetReader(ReaderName);
                        if (Reader != null)
                        {
                            DisplayReaderPresent();
                            Reader.StartMonitor(new SCardReader.StatusChangeCallback(ReaderStatusChanged));
                        }
                        else
                        {
                            MessageBox.Show(this, "Failed to connect to the PC/SC reader. Please click the 'Reader' link to select another reader.", Title, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                    }
                    else
                    {
                        MessageBox.Show(this, "The PC/SC subsystem doesn't seem to be running. Please check your system's configuration.", Title, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
                else if (f.Mode == "serial")
                {
                    string PortName;
                    bool   UseNotifications;
                    bool   UseLpcdPolling;

                    f.GetSerialParameters(out PortName, out UseNotifications, out UseLpcdPolling);

                    SCardReaderList_CcidOverSerial.BackgroundInstantiate(
                        new SCardReaderList_CcidOverSerial.BackgroundInstantiateCallback(OnSerialReaderListInstantiated),
                        PortName,
                        UseNotifications,
                        UseLpcdPolling);
                }
                else if (f.Mode == "network")
                {
                    string Address;
                    ushort Port;

                    f.GetNetworkParameters(out Address, out Port);

                    SCardReaderList_CcidOverNetwork.BackgroundInstantiate(
                        new SCardReaderList_CcidOverNetwork.BackgroundInstantiateCallback(OnNetworkReaderListInstantiated),
                        Address,
                        Port);
                }
            }
        }
Exemplo n.º 10
0
        void ReaderStatusChanged(uint ReaderState, CardBuffer CardAtr)
        {
            /* The ReaderStatusChanged function is called as a delegate (callback) by the SCardReader object    */
            /* within its backgroung thread. Therefore we must use the BeginInvoke syntax to switch back from   */
            /* the context of the background thread to the context of the application's main thread. Overwise   */
            /* we'll get a security violation when trying to access the window's visual components (that belong */
            /* to the application's main thread and can't be safely manipulated by background threads).         */
            if (InvokeRequired)
            {
                IAsyncResult ar = this.BeginInvoke(new ReaderStatusChangedInvoker(ReaderStatusChanged), ReaderState, CardAtr);
                this.EndInvoke(ar);
                return;
            }

            DisplayReaderState(ReaderState, CardAtr);

            if (ReaderState == SCARD.STATE_UNAWARE)
            {
                if (Card != null)
                {
                    Card.Disconnect();
                    Card = null;
                }
                if (Reader != null)
                {
                    Reader.Release();
                    Reader = null;
                }
                if (ReaderList != null)
                {
                    ReaderList.Release();
                    ReaderList = null;
                }

                MessageBox.Show(this,
                                "The reader we were working on has been removed from the system. Please click the 'Reader' link to select another reader.",
                                Title,
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Exclamation);

                DisplayReaderState(0, null);
                DisplayReaderAbsent();
                return;
            }

            if ((ReaderState & SCARD.STATE_EMPTY) != 0)
            {
                if (Card != null)
                {
                    AddToResult("\nCard removed\n", Color.Black, true);
                    Card.Disconnect();
                    Card = null;
                }
            }
            else if ((ReaderState & SCARD.STATE_UNAVAILABLE) != 0)
            {
            }
            else if ((ReaderState & SCARD.STATE_MUTE) != 0)
            {
            }
            else if ((ReaderState & SCARD.STATE_INUSE) != 0)
            {
            }
            else if ((ReaderState & SCARD.STATE_PRESENT) != 0)
            {
                if (Card == null)
                {
                    /* New card -> leave edit mode */
                    Card = Reader.GetChannel();

                    if (Card.Connect())
                    {
                        AddToResult("\nCard inserted\n", Color.Black, true);
                        if (cbAutorun.Checked)
                        {
                            if (!StartScript())
                            {
                                AddToResult("Autorun failed\n", Color.Red);
                            }
                        }
                    }
                    else
                    {
                        MessageBox.Show(this, "Failed to connect to the card in the reader. Please verify that you don't have another application running in the background, that tries to access to the smartcards in the same reader.", Title, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        Card = null;
                    }
                }
                else
                {
                    AddToResult("Application state error, please remove the card\n", Color.Orange);
                }
            }
        }