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