Prompts the user to choose a certificate store.
Inheritance: System.Windows.Forms.Form
Ejemplo n.º 1
0
        private void BrowseBTN_Click(object sender, EventArgs e)
        {
            CertificateStoreIdentifier store = new CertificateStoreIdentifier();
            store.StoreType = CertificateStoreIdentifier.DetermineStoreType(CertificateStoreControl.Text);
            store.StorePath = CertificateStoreControl.Text;

            store = new CertificateStoreDlg().ShowDialog(store);

            if (store == null)
            {
                return;
            }

            CertificateStoreControl.Text = store.StorePath;

            if (m_CertificateStoreSelected != null)
            {
                m_CertificateStoreSelected(this, new EventArgs());
            }
        }
        private void BrowseBTN_Click(object sender, EventArgs e)
        {
            CertificateStoreIdentifier store = new CertificateStoreIdentifier();

            store.StoreType = CertificateStoreType.Directory;
            store.StorePath = CertificateStoreControl.Text;

            store = new CertificateStoreDlg().ShowDialog(store);
            if (store == null)
            {
                return;
            }

            CertificateStoreControl.Text = store.StorePath;

            if (m_CertificateStoreSelected != null)
            {
                m_CertificateStoreSelected(this, new EventArgs());
            }
        }
Ejemplo n.º 3
0
        private void MergeTrustListBTN_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);

                // chose trust list to import.
                CertificateStoreDlg dialog = new CertificateStoreDlg();
                dialog.Text = "Select Certificate Trust List to use as Source";
                CertificateStoreIdentifier id = dialog.ShowDialog(store);

                if (id == null)
                {
                    return;
                }

                if (String.Compare(application.TrustList.StorePath, id.StorePath, StringComparison.OrdinalIgnoreCase) == 0)
                {
                    MessageBox.Show("Selected Certificate Store is already the same as the Application Trust List", "Merge Trust List", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                
                // check for private keys.
                StringBuilder buffer = new StringBuilder();

                buffer.Append("This operation will add all of the certificates in the selected trust list to ");
                buffer.Append("the application trust list.");
                buffer.Append("\r\n");
                buffer.Append("\r\n");
                buffer.Append("Do you wish to proceed?\r\n");
                buffer.Append("\r\n");
                buffer.Append("Current Application Trust List = ");
                buffer.Append(application.TrustList.ToString());
                buffer.Append("\r\n");
                buffer.Append("Selected Trust List = ");
                buffer.Append(id.ToString());

                DialogResult result = new YesNoDlg().ShowDialog(buffer.ToString(), "Merge Trust List");

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

                // delete existing certificates.
                ICertificateStore targetStore = application.TrustList.OpenStore();

                // add the certificates.
                ICertificateStore sourceStore = id.OpenStore();

                foreach (X509Certificate2 certificate in sourceStore.Enumerate())
                {
                    if (targetStore.FindByThumbprint(certificate.Thumbprint) == null)
                    {
                        targetStore.Add(new X509Certificate2(certificate.RawData));
                    }
                }

                EditTrustListBTN_Click(sender, e);
            }
            catch (Exception exception)
            {
                GuiUtils.HandleException(this.Text, MethodBase.GetCurrentMethod(), exception);
            }
        }
Ejemplo n.º 4
0
        private void ReplaceTrustListBTN_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);

                // chose trust list to import.
                CertificateStoreDlg dialog = new CertificateStoreDlg();
                dialog.Text = "Select Certificate Trust List to use as Source";
                CertificateStoreIdentifier id = dialog.ShowDialog(store);

                if (id == null)
                {
                    return;
                }

                if (String.Compare(application.TrustList.StorePath, id.StorePath, StringComparison.OrdinalIgnoreCase) == 0)
                {
                    MessageBox.Show("Selected Certificate Store is already the same as the Application Trust List", "Replace Trust List", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                // check for private keys.
                ICertificateStore targetStore = application.TrustList.OpenStore();
                X509Certificate2Collection certificates = targetStore.Enumerate();

                bool hasPrivateKeys = true;
                StringBuilder buffer = null;
                DialogResult result = DialogResult.None;

                while (hasPrivateKeys)
                {
                    hasPrivateKeys = false;

                    foreach (X509Certificate2 certificate in certificates)
                    {
                        if (certificate.HasPrivateKey)
                        {
                            hasPrivateKeys = true;

                            buffer = new StringBuilder();

                            buffer.Append("The application's current trust list contains certificates with private keys.\r\n");
                            buffer.Append("Automatically deleting these certificates could break other applications. ");
                            buffer.Append("\r\n");
                            buffer.Append("\r\n");
                            buffer.Append("Would you like to remove these certificates manually?\r\n");
                            buffer.Append("\r\n");
                            buffer.Append("Current Application Trust List = ");
                            buffer.Append(application.TrustList.ToString());
                            buffer.Append("\r\n");
                            buffer.Append("Certificate with Private Key = ");
                            buffer.Append(certificate.Subject);

                            result = new YesNoDlg().ShowDialog(buffer.ToString(), "Warning Private Keys Found");

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

                            new CertificateListDlg().ShowDialog(application.TrustList, false);
                            certificates = targetStore.Enumerate();
                            break;
                        }
                    }
                }

                buffer = new StringBuilder();

                buffer.Append("This operation will delete all of the certificates in the current application trust list and ");
                buffer.Append("replace them with the certificates in the selected trust list.");
                buffer.Append("\r\n");
                buffer.Append("\r\n");
                buffer.Append("Do you wish to proceed?\r\n");
                buffer.Append("\r\n");
                buffer.Append("Current Application Trust List = ");
                buffer.Append(application.TrustList.ToString());
                buffer.Append("\r\n");
                buffer.Append("Selected Trust List = ");
                buffer.Append(id.ToString());
               
                result = new YesNoDlg().ShowDialog(buffer.ToString(), "Replace Trust List");

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

                // delete existing certificates.
                certificates = targetStore.Enumerate();

                foreach (X509Certificate2 certificate in certificates)
                {
                    if (!certificate.HasPrivateKey)
                    {
                        targetStore.Delete(certificate.Thumbprint);
                    }
                }

                // copy the certificates.
                ICertificateStore sourceStore = id.OpenStore();

                foreach (X509Certificate2 certificate in sourceStore.Enumerate())
                {
                    targetStore.Add(new X509Certificate2(certificate.RawData));
                }

                EditTrustListBTN_Click(sender, e);
            }
            catch (Exception exception)
            {
                GuiUtils.HandleException(this.Text, MethodBase.GetCurrentMethod(), exception);
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Assigns a certificate to the application.
        /// </summary>
        private void AssignTrustListBTN_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 = application.TrustList;

                if (store == null)
                {
                    store = GetDefaultStore(application, false);
                }

                // prompt for the store to open.
                store = new CertificateStoreDlg().ShowDialog(store);

                if (store == null)
                {
                    return;
                }

                // update the trust list.
                application.Application.TrustedCertificateStore = new Opc.Ua.Security.CertificateStoreIdentifier();
                application.Application.TrustedCertificateStore.StorePath = store.StorePath;
                application.Application.TrustedCertificateStore.StoreType = store.StoreType;
                application.Application.TrustedCertificates = new Opc.Ua.Security.CertificateList();

                m_currentStore = store;

                // save the configuration.
                new Opc.Ua.Security.SecurityConfigurationManager().WriteConfiguration(application.Application.ConfigurationFile, application.Application);
            }
            catch (Exception exception)
            {
                GuiUtils.HandleException(this.Text, MethodBase.GetCurrentMethod(), exception);
            }
        }
Ejemplo n.º 6
0
        private void ImportCertificateListToStoreBTN_Click(object sender, EventArgs e)
        {
            try
            {
                const string caption = "Import Certificate List";
                
                CertificateStoreIdentifier list1 = new CertificateStoreIdentifier();
                list1.StoreType = ManagedStoreCTRL.StoreType;
                list1.StorePath = ManagedStoreCTRL.StorePath;

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

                CertificateStoreIdentifier list2 = new CertificateStoreDlg().ShowDialog(m_currentStore);

                if (list2 == null)
                {
                    return;
                }

                m_currentStore = list2;

                int count = 0;
                ICertificateStore store1 = list1.OpenStore();
                ICertificateStore store2 = list2.OpenStore();

                try
                {
                    foreach (X509Certificate2 certificate in store2.Enumerate())
                    {
                        if (store1.FindByThumbprint(certificate.Thumbprint) == null)
                        {
                            store1.Add(certificate);
                            count++;
                        }
                    }
                }
                finally
                {
                    store1.Close();
                    store2.Close();
                }

                MessageBox.Show(
                    this,
                    count.ToString() + " certificates added.",
                    caption,
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Information);
            }
            catch (Exception exception)
            {
                GuiUtils.HandleException(this.Text, MethodBase.GetCurrentMethod(), exception);
            }
        }
Ejemplo n.º 7
0
        private void ImportCertificateListToTrustBTN_Click(object sender, EventArgs e)
        {
            try
            {
                const string caption = "Import Certificate List";

                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;
                }

                CertificateStoreIdentifier store = new CertificateStoreDlg().ShowDialog(m_currentStore);

                if (store == null)
                {
                    return;
                }

                m_currentStore = store;

                int count = 0;
                ICertificateStore store1 = application.TrustList.OpenStore();
                ICertificateStore store2 = store.OpenStore();

                try
                {
                    foreach (X509Certificate2 certificate in store2.Enumerate())
                    {
                        if (store1.FindByThumbprint(certificate.Thumbprint) == null)
                        {
                            store1.Add(certificate);
                            count++;
                        }
                    }
                }
                finally
                {
                    store1.Close();
                    store2.Close();
                }

                MessageBox.Show(
                    this,
                    count.ToString() + " certificates added.",
                    caption,
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Information);
            }
            catch (Exception exception)
            {
                GuiUtils.HandleException(this.Text, MethodBase.GetCurrentMethod(), exception);
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Browses for a certificate to import.
        /// </summary>
        private void ImportApplicationCertificateBTN_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;
                }

                // set current directory.
                if (m_currentDirectory == null)
                {
                    m_currentDirectory = Utils.GetAbsoluteDirectoryPath("%CommonApplicationData%\\OPC Foundation\\CertificateStores\\MachineDefault", false, false);
                }

                if (m_currentDirectory == null)
                {
                    m_currentDirectory = new FileInfo(Application.ExecutablePath).DirectoryName;
                }

                // open file dialog.
                OpenFileDialog dialog = new OpenFileDialog();

                dialog.CheckFileExists = true;
                dialog.CheckPathExists = true;
                dialog.DefaultExt = ".pfx";
                dialog.Filter = "PKCS#12 Files (*.pfx)|*.pfx|All Files (*.*)|*.*";
                dialog.Multiselect = false;
                dialog.ValidateNames = true;
                dialog.Title = "Open Application Certificate File";
                dialog.FileName = null;
                dialog.InitialDirectory = m_currentDirectory;
                dialog.RestoreDirectory = true;

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

                FileInfo fileInfo = new FileInfo(dialog.FileName);
                m_currentDirectory = fileInfo.Directory.FullName;

                CertificateStoreIdentifier store = GetDefaultStore(application, true);

                // prompt for the store to import into.
                store = new CertificateStoreDlg().ShowDialog(store);

                if (store == null)
                {
                    return;
                }

                m_currentStore = store;
                string password = String.Empty;
                X509Certificate2 certificate = null;

                do
                {
                    try
                    {
                        // load the certificate.
                        certificate = new X509Certificate2(
                            fileInfo.FullName,
                            password,
                            X509KeyStorageFlags.Exportable | X509KeyStorageFlags.MachineKeySet);

                        if (!certificate.HasPrivateKey)
                        {
                            MessageBox.Show("Certificate does not have a private key.", "Import Certificate", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }

                        // import certificate.
                        ICertificateStore physicalStore = store.OpenStore();
                        physicalStore.Add(certificate);
                        physicalStore.Close();
                        break;
                    }
                    catch (System.Security.Cryptography.CryptographicException exception)
                    {
                        // prompt for password.
                        password = new PasswordDlg().ShowDialog(password, exception.Message);

                        if (password == null)
                        {
                            return;
                        }
                    }
                }
                while (true);

                UpdateApplicationCertificate(application.Application, store, certificate);
            }
            catch (Exception exception)
            {
                GuiUtils.HandleException(this.Text, MethodBase.GetCurrentMethod(), exception);
            }
        }
Ejemplo n.º 9
0
        private void TrustListBTN_Click(object sender, EventArgs e)
        {
            try
            {
                // determine default store.
                CertificateStoreIdentifier store = new CertificateStoreIdentifier();

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

                // select store.
                CertificateStoreIdentifier trustList = new CertificateStoreDlg().ShowDialog(store);

                if (trustList != null)
                {
                    m_trustList = trustList;
                    TrustListTB.Text = m_trustList.ToString();
                }
            }
            catch (Exception exception)
            {
                GuiUtils.HandleException(this.Text, System.Reflection.MethodBase.GetCurrentMethod(), exception);
            }
        }