コード例 #1
0
ファイル: MainForm.cs プロジェクト: jnsgsbz/EnrollmentStation
        private void btnViewCert_Click(object sender, EventArgs e) //Angepasst, sodass beide Zertifikate angezeigt werden können
        {
            X509Certificate2 cert  = null;                         //Standard Cert
            X509Certificate2 cert2 = null;                         //Admin Cert

            string devName = YubikeyPivManager.Instance.ListDevices().FirstOrDefault();

            if (!string.IsNullOrEmpty(devName))
            {
                using (YubikeyPivDevice dev = YubikeyPivManager.Instance.OpenDevice(devName))
                {
                    cert  = dev.GetCertificate9a();
                    cert2 = dev.GetCertificate9d();
                }
            }

            if (cert == null)
            {
                MetroMessageBox.Show(this, "No Standard User certificate on device.", "No Certificate", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                X509Certificate2UI.DisplayCertificate(cert);
            }
            if (cert2 == null)
            {
                MetroMessageBox.Show(this, "No Admin User certificate on device.", "No Certificate", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                X509Certificate2UI.DisplayCertificate(cert2);
            }
        }
コード例 #2
0
ファイル: MainForm.cs プロジェクト: jnsgsbz/EnrollmentStation
        private void btnExportCert_Click(object sender, EventArgs e) //Änderungen vorgenommen, dass beide Zertifikate, wenn vorhanden exportiert werden können
        {
            X509Certificate2 cert  = null;                           //Standard Cert
            X509Certificate2 cert2 = null;                           //Admin Cert
            int deviceSerial       = 0;

            string devName = YubikeyPivManager.Instance.ListDevices().FirstOrDefault();

            if (!string.IsNullOrEmpty(devName))
            {
                using (YubikeyPivDevice dev = YubikeyPivManager.Instance.OpenDevice(devName))
                {
                    deviceSerial = (int)dev.GetSerialNumber();
                }

                using (YubikeyPivDevice dev = YubikeyPivManager.Instance.OpenDevice(devName))
                {
                    cert  = dev.GetCertificate9a();
                    cert2 = dev.GetCertificate9d();
                }
            }

            if (cert == null && cert2 == null)
            {
                MetroMessageBox.Show(this, "No certificate on device.", "No Certificate", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            SaveFileDialog saveFileDialog = new SaveFileDialog();

            saveFileDialog.FileName = deviceSerial + "-" + cert.SerialNumber + ".crt"; //TODO: GetSerialNumber() can possibly fail

            DialogResult dlgResult = saveFileDialog.ShowDialog();

            if (dlgResult != DialogResult.OK)
            {
                return;
            }

            if (cert != null)
            {
                using (Stream fs = saveFileDialog.OpenFile())
                {
                    byte[] data = cert.GetRawCertData();
                    fs.Write(data, 0, data.Length);
                }
            }

            if (cert2 != null)
            {
                using (Stream fs = saveFileDialog.OpenFile())
                {
                    byte[] data = cert2.GetRawCertData();
                    fs.Write(data, 0, data.Length);
                }
            }
        }
コード例 #3
0
        private void cmdEnroll_Click(object sender, EventArgs e)
        {
            string devName   = YubikeyPivManager.Instance.ListDevices().FirstOrDefault();
            bool   hasDevice = !string.IsNullOrEmpty(devName);

            if (!hasDevice)
            {
                return;
            }

            using (YubikeyPivDevice piv = YubikeyPivManager.Instance.OpenDevice(devName))
            {
                if (txtStdUser.Text != null && txtAdmUser.Text == null)
                {
                    if (piv.GetCertificate9a() != null)
                    {
                        // Already enrolled
                        DialogResult resp = MetroMessageBox.Show(this, "The inserted Yubikey has already been enrolled. Are you sure you wish to overwrite it?", "Already enrolled", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);

                        if (resp != DialogResult.Yes)
                        {
                            return;
                        }
                    }
                }
                else if (txtAdmUser.Text != null && txtStdUser.Text == null)
                {
                    if (piv.GetCertificate9d() != null)
                    {
                        // Already enrolled
                        DialogResult resp = MetroMessageBox.Show(this, "The inserted Yubikey has already been enrolled. Are you sure you wish to overwrite it?", "Already enrolled", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);

                        if (resp != DialogResult.Yes)
                        {
                            return;
                        }
                    }
                }
                else
                {
                    if (piv.GetCertificate9d() != null)
                    {
                        // Already enrolled
                        DialogResult resp = MetroMessageBox.Show(this, "The inserted Yubikey has already been enrolled. Are you sure you wish to overwrite Standard and Admin User?", "Already enrolled", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);

                        if (resp != DialogResult.Yes)
                        {
                            return;
                        }
                    }
                }
            }

            cmdEnroll.Enabled = false;

            foreach (Control control in groupBox1.Controls)
            {
                control.Enabled = false;
            }

            foreach (Control control in groupBox3.Controls)
            {
                control.Enabled = false;
            }

            drpAlgorithm.Enabled = false;

            _enrollWorker.RunWorkerAsync();
        }
コード例 #4
0
ファイル: MainForm.cs プロジェクト: jnsgsbz/EnrollmentStation
        private void RefreshInsertedKey()
        {
            List <string> listDevices = YubikeyPivManager.Instance.ListDevices().ToList();
            string        devName     = listDevices.FirstOrDefault();
            bool          hasDevice   = !string.IsNullOrEmpty(devName);

            foreach (Control control in gbInsertedKey.Controls)
            {
                if (control.Name.StartsWith("lbl"))
                {
                    control.Visible = (hasDevice);
                }
            }

            if (hasDevice)
            {
                using (YubikeyPivDevice dev = YubikeyPivManager.Instance.OpenDevice(devName))
                {
                    int serialNumber = (int)dev.GetSerialNumber();   // uint
                    var yi           = new YubikeyInfo();
                    yi.GetYubikeyInfo(serialNumber.ToString());
                    lblDevType.Text          = yi.devicetype;
                    lblInsertedSerial.Text   = yi.serial;
                    lblInsertedFirmware.Text = yi.firmware;
                    lblInsertedMode.Text     = yi.usbinterface;

                    X509Certificate2 cert  = null;  //Standard Cert
                    X509Certificate2 cert2 = null;  //Admin Cert



                    _hasBeenEnrolled = _dataStore.Search((int)dev.GetSerialNumber()).Any();


                    cert  = dev.GetCertificate9a();
                    cert2 = dev.GetCertificate9d();

                    if ((cert != null || cert2 != null) && _hasBeenEnrolled == true)
                    {
                        lblInsertedHasBeenEnrolled.Text      = "Enrolled!";
                        lblInsertedHasBeenEnrolled.ForeColor = Color.Green;
                    }
                    else if ((cert != null || cert2 != null) && _hasBeenEnrolled == false)
                    {
                        lblInsertedHasBeenEnrolled.Text      = "YubiKey is not empty!";
                        lblInsertedHasBeenEnrolled.ForeColor = Color.Red;
                    }
                    else if ((cert == null || cert2 == null) && _hasBeenEnrolled == true)
                    {
                        lblInsertedHasBeenEnrolled.Text      = "YubiKey is empty! Please revoke Certificate!";
                        lblInsertedHasBeenEnrolled.ForeColor = Color.Red;
                    }
                    else if ((cert == null || cert2 == null) && _hasBeenEnrolled == false)
                    {
                        lblInsertedHasBeenEnrolled.Text      = "YubiKey can be enrolled!";
                        lblInsertedHasBeenEnrolled.ForeColor = Color.DarkOrange;
                    }
                }
            }

            if (listDevices.Count > 1)
            {
                lblMultipleKeys.Text    = $"{listDevices.Count:N0} keys inserted";
                btnResetYubiKey.Enabled = false;
                btnViewCert.Enabled     = false;
                btnEnableCCID.Enabled   = false;
                btnExportCert.Enabled   = false;
                tsbEnroll.Enabled       = false;
                tsbAbout.Enabled        = false;
                tsbSettings.Enabled     = false;
            }
            else
            {
                lblMultipleKeys.Text = "";
            }
        }
コード例 #5
0
        private void RefreshInsertedKeyInfo()
        {
            string devName   = YubikeyPivManager.Instance.ListDevices().FirstOrDefault();
            bool   hasDevice = !string.IsNullOrEmpty(devName);

            foreach (Control control in gbInsertedYubikey.Controls)
            {
                if (control.Name.StartsWith("lbl"))
                {
                    control.Visible = hasDevice;
                }
            }

            if (!hasDevice)
            {
                return;
            }

            using (YubikeyPivDevice dev = YubikeyPivManager.Instance.OpenDevice(devName))
            {
                X509Certificate2 cert  = null;  //Standard Cert
                X509Certificate2 cert2 = null;  //Admin Cert

                cert  = dev.GetCertificate9a();
                cert2 = dev.GetCertificate9d();

                if ((cert != null || cert2 != null) && _hasBeenEnrolled == true)
                {
                    lblAlreadyEnrolled.Text      = "Enrolled!";
                    lblAlreadyEnrolled.ForeColor = Color.Green;
                }
                else if ((cert != null || cert2 != null) && _hasBeenEnrolled == false)
                {
                    lblAlreadyEnrolled.Text      = "YubiKey is not empty!";
                    lblAlreadyEnrolled.ForeColor = Color.Red;
                }
                else if ((cert == null || cert2 == null) && _hasBeenEnrolled == true)
                {
                    lblAlreadyEnrolled.Text      = "YubiKey is empty! Please revoke Certificate!";
                    lblAlreadyEnrolled.ForeColor = Color.Red;
                }
                else if ((cert == null || cert2 == null) && _hasBeenEnrolled == false)
                {
                    lblAlreadyEnrolled.Text      = "YubiKey can be enrolled!";
                    lblAlreadyEnrolled.ForeColor = Color.DarkOrange;
                }
            }

            using (YubikeyPivDevice dev = YubikeyPivManager.Instance.OpenDevice(devName))
            {
                string serial = dev.GetSerialNumber().ToString();

                var  yi      = new YubikeyInfo();
                bool success = yi.GetYubikeyInfo(serial);

                /* Get currently only CCID enabled Yubikeys
                 * if (HasCcid)
                 *  lblInsertedMode.ForeColor = Color.Black;
                 * else
                 *  lblInsertedMode.ForeColor = Color.Red;
                 */
                lblInsertedTyp.Text      = yi.devicetype;
                lblInsertedSerial.Text   = yi.serial;
                lblInsertedMode.Text     = yi.usbinterface;
                lblInsertedFirmware.Text = yi.firmware;
            }
        }