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);
            }
        }
        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);
                }
            }
        }
Exemple #3
0
        static void PrintPiv(string name)
        {
            Console.WriteLine($"[PIV] Device: {name}");

            if (!YubikeyPivManager.Instance.IsValidDevice(name))
            {
                Console.WriteLine("      Not a valid PIV device");
            }
            else
            {
                using (YubikeyPivDevice device = YubikeyPivManager.Instance.OpenDevice(name))
                {
                    Console.WriteLine($"      Version : {device.GetVersion()}");

                    byte[] chuid;
                    if (device.GetCHUID(out chuid))
                    {
                        Console.WriteLine($"      CHUID   : {BitConverter.ToString(chuid).Replace("-", "")}");
                    }
                    else
                    {
                        Console.WriteLine("      CHUID   : N/A");
                    }

                    Console.WriteLine($"      PinTries: {device.GetPinTriesLeft():N0}");

                    X509Certificate2 cert = device.GetCertificate9a();

                    if (cert != null)
                    {
                        Console.WriteLine($"      Cert 9A, Subject: {cert.SubjectName}");
                        Console.WriteLine($"               Issuer : {cert.IssuerName}");
                        Console.WriteLine($"               Start  : {cert.NotBefore.ToUniversalTime():O}");
                        Console.WriteLine($"               Expiry : {cert.NotAfter.ToUniversalTime():O}");
                        Console.WriteLine($"               Serial : {cert.SerialNumber}");
                        Console.WriteLine($"               Finger : {cert.Thumbprint}");
                    }
                    else
                    {
                        Console.WriteLine("      Cert 9A : N/A");
                    }
                }
            }

            Console.WriteLine();
        }
        private void btnExportCert_Click(object sender, EventArgs e)
        {
            X509Certificate2 cert = null;
            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();
                }
            }

            if (cert == null)
            {
                MessageBox.Show("No certificate on device.", "No Certificate", MessageBoxButtons.OK);
                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;
            }

            using (Stream fs = saveFileDialog.OpenFile())
            {
                byte[] data = cert.GetRawCertData();
                fs.Write(data, 0, data.Length);
            }
        }
Exemple #5
0
        private void cmdEnroll_Click(object sender, EventArgs e)
        {
            string devName   = YubikeyNeoManager.Instance.ListDevices().FirstOrDefault();
            bool   hasDevice = !string.IsNullOrEmpty(devName);

            if (!hasDevice)
            {
                return;
            }

            using (YubikeyPivDevice piv = YubikeyPivManager.Instance.OpenDevice(devName))
            {
                if (piv.GetCertificate9a() != null)
                {
                    // Already enrolled
                    DialogResult resp = MessageBox.Show("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;
                    }
                }
            }

            cmdEnroll.Enabled = false;

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

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

            _enrollWorker.RunWorkerAsync();
        }
        private void btnViewCert_Click(object sender, EventArgs e)
        {
            X509Certificate2 cert = null;

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

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

            if (cert == null)
            {
                MessageBox.Show("No certificate on device.", "No Certificate", MessageBoxButtons.OK);
            }
            else
            {
                X509Certificate2UI.DisplayCertificate(cert);
            }
        }
        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 = "";
            }
        }
Exemple #8
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;
            }
        }