private void btnMakeCert_Click(object sender, EventArgs e) { if (btnMakeCert.Text == c_make) { MakeCertArgs args = new MakeCertArgs(int.Parse(cbKeyStrength.SelectedItem.ToString()), dateFrom.Value, dateUntil.Value, GetCleanDomainArray(), txtCertPassword.Text, cbCerAndKey.Checked, cbIssuerSelect.SelectedItem.ToString()); if (args.domains.Length == 0) { MessageBox.Show("No domain names were entered!" + Environment.NewLine + Environment.NewLine + "You should at least enter localhost if you don't want any other domain names."); return; } btnMakeCert.Text = c_cancel; StartProgress(); worker = new Thread(MakeCertificate); worker.IsBackground = true; worker.Name = "Make Certificate"; worker.Start(args); } else { btnMakeCert.Enabled = false; worker.Abort(); } }
private void MakeCertificate(object Argument) { try { MakeCertArgs args = (MakeCertArgs)Argument; // Verify that the files do not already exist string safeFileName = SafeFileName(args.domains[0]); if (args.saveCerAndKey) { if (File.Exists(safeFileName + ".cer")) { ShowFileExistsError(safeFileName + ".cer"); return; } else if (File.Exists(safeFileName + ".key")) { ShowFileExistsError(safeFileName + ".key"); return; } } else { if (File.Exists(safeFileName + ".pfx")) { ShowFileExistsError(safeFileName + ".pfx"); return; } } CertificateBundle certBundle; if (args.issuer == "*Self-Signed*") { certBundle = CertMaker.GetCertificateSignedBySelf(args.domains, args.keyStrength, args.validFrom, args.validTo); } else { CertificateBundle issuerBundle = null; if (args.issuer.EndsWith(".pfx", StringComparison.OrdinalIgnoreCase)) { string password = null; while (issuerBundle == null) { issuerBundle = CertificateBundle.LoadFromPfxFile(args.issuer, password); if (issuerBundle == null) { PasswordPrompt pp = new PasswordPrompt("CA file is protected", "The .pfx file requires a password:"******".key", StringComparison.OrdinalIgnoreCase) ? args.issuer.Remove(args.issuer.Length - 4) + ".cer" : null; issuerBundle = CertificateBundle.LoadFromCerAndKeyFiles(cerFile, args.issuer); } certBundle = CertMaker.GetCertificateSignedByCA(args.domains, args.keyStrength, args.validFrom, args.validTo, issuerBundle); } if (args.saveCerAndKey) { File.WriteAllBytes(safeFileName + ".cer", certBundle.GetPublicCertAsCerFile()); File.WriteAllBytes(safeFileName + ".key", certBundle.GetPrivateKeyAsKeyFile()); } else { if (args.password == "") { args.password = null; } File.WriteAllBytes(safeFileName + ".pfx", certBundle.GetPfx(true, args.password)); } } catch (ThreadAbortException) { } catch (Exception ex) { MessageBox.Show(ex.ToString()); } finally { StopProgress(); } }