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