Displays a list of certificates.
Inheritance: System.Windows.Forms.Form
示例#1
0
        private void AddCertificateToTrustListBTN_Click(object sender, EventArgs e)
        {
            try
            {
                // get application.
                ManagedApplication application = ApplicationToManageCTRL.GetSelectedApplication();;

                if (application == null)
                {
                    return;
                }
                
                // load the configuration.
                application.Reload();

                CertificateStoreIdentifier store = GetDefaultStore(application, false);

                // show the list of rejected certificates.
                CertificateIdentifier id = new CertificateListDlg().ShowDialog(store, true);

                if (id == null)
                {
                    return;
                }

                store = new CertificateStoreIdentifier();
                store.StoreType = id.StoreType;
                store.StorePath = id.StorePath;
                m_currentStore = store;

                X509Certificate2 certificate = id.Find();
                ValidateAndImport(application.TrustList, certificate);
            }
            catch (Exception exception)
            {
                GuiUtils.HandleException(this.Text, MethodBase.GetCurrentMethod(), exception);
            }
        }
        private void CertificateBTN_Click(object sender, EventArgs e)
        {
            try
            {
                CertificateStoreIdentifier store = new CertificateStoreIdentifier();
                store.StoreType = m_certificate.StoreType;
                store.StorePath = m_certificate.StorePath;
               
                CertificateIdentifier certificate = new CertificateListDlg().ShowDialog(store, true);

                if (certificate != null)
                {
                    m_certificate = certificate;
                    CertificateTB.Text = m_certificate.Thumbprint;
                }
            }
            catch (Exception exception)
            {
                GuiUtils.HandleException(this.Text, MethodBase.GetCurrentMethod(), exception);
            }
        }
示例#3
0
        /// <summary>
        /// Assigns a certificate to the application.
        /// </summary>
        private void AssignApplicationCertificateBTN_Click(object sender, EventArgs e)
        {
            try
            {
                // get application.
                ManagedApplication application = ApplicationToManageCTRL.GetSelectedApplication();;

                if (application == null)
                {
                    return;
                }

                // load the configuration.
                application.Reload();

                // can't set application certificate for non-sdk apps.
                if (!application.IsSdkCompatible)
                {
                    return;
                }

                CertificateStoreIdentifier store = GetDefaultStore(application, true);

                // select the certificate.
                CertificateIdentifier certificate = new CertificateListDlg().ShowDialog(store, true);

                if (certificate == null)
                {
                    return;
                }

                store = new CertificateStoreIdentifier();
                store.StoreType = certificate.StoreType;
                store.StorePath = certificate.StorePath;
                m_currentStore = store;

                // update the certificate.
                UpdateApplicationCertificate(application.Application, store, certificate.Certificate);
                application.Certificate = certificate;
            }
            catch (Exception exception)
            {
                GuiUtils.HandleException(this.Text, MethodBase.GetCurrentMethod(), exception);
            }
        }
示例#4
0
        private void SelectCertificateToTrustBTN_Click(object sender, EventArgs e)
        {
            try
            {
                const string caption = "Select Certificate to Trust";

                ManagedApplication application = ManageApplicationSecurityCTRL.GetSelectedApplication();

                if (application == null)
                {
                    return;
                }

                if (application.TrustList == null)
                {
                    MessageBox.Show(application.ToString() + " does not have a trust list defined.", caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                if (m_currentStore == null)
                {
                    m_currentStore = new CertificateStoreIdentifier();
                    m_currentStore.StoreType = Utils.DefaultStoreType;
                    m_currentStore.StorePath = Utils.DefaultStorePath;
                }

                CertificateIdentifier id = new CertificateListDlg().ShowDialog(m_currentStore, true);

                if (id == null)
                {
                    return;
                }

                m_currentStore.StoreType = id.StoreType;
                m_currentStore.StorePath = id.StorePath;

                X509Certificate2 certificate = id.Find();

                if (certificate == null)
                {
                    return;
                }

                ICertificateStore store = application.TrustList.OpenStore();

                try
                {
                    if (store.FindByThumbprint(certificate.Thumbprint) == null)
                    {
                        store.Add(new X509Certificate2(certificate.RawData));
                    }
                }
                finally
                {
                    store.Close();
                }

                MessageBox.Show(
                    this,
                    certificate.Subject + " now trusted.",
                    caption,
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Information);
            }
            catch (Exception exception)
            {
                GuiUtils.HandleException(this.Text, MethodBase.GetCurrentMethod(), exception);
            }
        }
示例#5
0
        private void ExportPrivateKeyBTN_Click(object sender, EventArgs e)
        {
            try
            {
                const string caption = "Select Certificate to Export";

                CertificateStoreIdentifier store = new CertificateStoreIdentifier();
                store.StoreType = ManagedStoreCTRL.StoreType;
                store.StorePath = ManagedStoreCTRL.StorePath;

                CertificateIdentifier id = new CertificateListDlg().ShowDialog(store, true);

                if (id == null)
                {
                    return;
                }

                X509Certificate2 certificate = id.Find(false);

                if (certificate == null)
                {
                    MessageBox.Show(
                        this,
                        "Certificate does not exist or its private key cannot be accessed.",
                        caption,
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Information);

                    return;
                }

                string displayName = null;

                foreach (string element in Utils.ParseDistinguishedName(certificate.Subject))
                {
                    if (element.StartsWith("CN="))
                    {
                        displayName = element.Substring(3);
                        break;
                    }
                }

                StringBuilder filePath = new StringBuilder();

                if (!String.IsNullOrEmpty(displayName))
                {
                    filePath.Append(displayName);
                    filePath.Append(" ");
                }

                filePath.Append("[");
                filePath.Append(certificate.Thumbprint);
                filePath.Append("].pfx");

                SaveFileDialog dialog = new SaveFileDialog();

                dialog.CheckFileExists = false;
                dialog.CheckPathExists = true;
                dialog.DefaultExt = ".pfx";
                dialog.Filter = "PKCS#12 Files (*.pfx)|*.pfx|All Files (*.*)|*.*";
                dialog.ValidateNames = true;
                dialog.Title = "Save Private File";
                dialog.FileName = filePath.ToString();
                dialog.InitialDirectory = m_currentDirectory;

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

                string password = new PasswordDlg().ShowDialog(null, "Password recommended");

                FileInfo fileInfo = new FileInfo(dialog.FileName);
                m_currentDirectory = fileInfo.DirectoryName;

                // save the file.
                using (Stream ostrm = fileInfo.Open(FileMode.Create, FileAccess.ReadWrite, FileShare.None))
                {
                    byte[] data = certificate.Export(X509ContentType.Pkcs12, password);
                    ostrm.Write(data, 0, data.Length);
                }

                // save the public key.
                string fileRoot = fileInfo.FullName.Substring(0, fileInfo.FullName.Length - fileInfo.Extension.Length);
                fileRoot += ".der";

                using (Stream ostrm = File.Open(fileRoot, FileMode.Create, FileAccess.ReadWrite, FileShare.None))
                {
                    byte[] data = certificate.RawData;
                    ostrm.Write(data, 0, data.Length);
                }

                // check if original certificate should be deleted.
                if (new YesNoDlg().ShowDialog("Delete original certificate?", caption) == DialogResult.Yes)
                {                    
                    ICertificateStore physicalStore = id.OpenStore();

                    try
                    {
                        physicalStore.Delete(certificate.Thumbprint);
                    }
                    finally
                    {
                        physicalStore.Close();
                    }
                }
            }
            catch (Exception exception)
            {
                GuiUtils.HandleException(this.Text, MethodBase.GetCurrentMethod(), exception);
            }
        }
示例#6
0
        private void SelectAndIssueCertificateBTN_Click(object sender, EventArgs e)
        {
            try
            {
                const string caption = "Select Certificate to Issue";

                if (m_currentStore == null)
                {
                    m_currentStore = new CertificateStoreIdentifier();
                    m_currentStore.StoreType = Utils.DefaultStoreType;
                    m_currentStore.StorePath = Utils.DefaultStorePath;
                }

                CertificateIdentifier id = new CertificateListDlg().ShowDialog(m_currentStore, true);

                if (id == null)
                {
                    return;
                }

                m_currentStore.StoreType = id.StoreType;
                m_currentStore.StorePath = id.StorePath;

                X509Certificate2 certificate = id.Find();

                if (certificate == null)
                {
                    return;
                }

                CertificateIdentifier newId = new CreateCertificateDlg().ShowDialog(m_currentStore, IssuerKeyFilePathTB.Text, certificate);

                if (newId == null)
                {
                    return;
                }

                X509Certificate2 newCertificate = id.Find();

                MessageBox.Show(
                    this,
                    newCertificate.Subject + " issued.",
                    caption,
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Information);

                // check if original certificate should be deleted.
                if (new YesNoDlg().ShowDialog("Delete orginal certificate?", caption) == DialogResult.Yes)
                {
                    ICertificateStore physicalStore = id.OpenStore();

                    try
                    {
                        physicalStore.Delete(certificate.Thumbprint);
                    }
                    finally
                    {
                        physicalStore.Close();
                    }
                }
            }
            catch (Exception exception)
            {
                GuiUtils.HandleException(this.Text, MethodBase.GetCurrentMethod(), exception);
            }
        }
        private void CertificateBTN_Click(object sender, EventArgs e)
        {
            try
            {
                // determine default store.
                CertificateStoreIdentifier store = new CertificateStoreIdentifier();

                if (m_certificate != null)
                {
                    store.StoreType = m_certificate.StoreType;
                    store.StorePath = m_certificate.StorePath;
                }
                else
                {
                    store.StoreType = Utils.DefaultStoreType;
                    store.StorePath = Utils.DefaultStorePath;
                }

                // select the certificate.
                CertificateIdentifier certificate = new CertificateListDlg().ShowDialog(store, true);

                if (certificate != null)
                {
                    m_certificate = certificate;
                    X509Certificate2 certificate2 = m_certificate.Find();

                    if (certificate2 != null)
                    {
                        CertificateTB.Text = certificate2.Subject;
                    }
                    else
                    {
                        CertificateTB.Text = m_certificate.ToString();
                    }
                }
            }
            catch (Exception exception)
            {
                GuiUtils.HandleException(this.Text, System.Reflection.MethodBase.GetCurrentMethod(), exception);
            }
        }