public static bool Show(Window owner, string title, string prompt, out string password) { var dialog = new PasswordDialog() { Owner = owner, Title = title, }; dialog.label.Text = prompt; if (dialog.ShowDialog() == true) { password = dialog.passwordBox.Password; return(true); } else { password = null; return(false); } }
private void buttonGenerate_Click(object sender, RoutedEventArgs e) { // TODO Validate X509Certificate2 issuerCertificate; if (false == string.IsNullOrWhiteSpace(textBoxIssuer_CertificatePath.Text)) { string issuerPassword; if (false == PasswordDialog.Show( this, "Issuer Certificate Password", "Please enter the password of the issuer certificate:", out issuerPassword )) { return; } try { issuerCertificate = new X509Certificate2(textBoxIssuer_CertificatePath.Text.Trim(), issuerPassword); } catch (CryptographicException exc) { MessageBox.Show( this, "Could not load the certificate of the issuer:\n" + exc.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error ); return; } } else { issuerCertificate = null; } string outputPassword; if (false == PasswordDialog.Show( this, "New Certificate Password", "Please enter a password for the NEW certificate:", out outputPassword )) { return; } var outputPath = textBoxSaveTo_Path.Text.Trim(); var subjectName = new X501DistinguishedName() { CommonName = textBoxCommonName.Text.TrimToNull(), Organization = textBoxOrganization.Text.TrimToNull(), OrganizationalUnit = textBoxOrganizationalUnit.Text.TrimToNull(), Locality = textBoxLocality.Text.TrimToNull(), StateOrProvince = textBoxState.Text.TrimToNull(), Country = (dropDownListCountry.SelectedValue as string ?? dropDownListCountry.Text).TrimToNull() // "SelectedValue" is null if text is entered manually }; var subjectAlternativeNames = textBoxSubjectAltNames.Text .Split(new char[] { '\n', '\r' }) .Select(x => x.Trim()) .Where(x => x.Length > 0) .ToList(); if (subjectAlternativeNames.Count > 0 && false == subjectAlternativeNames.Contains(subjectName.CommonName, StringComparer.InvariantCultureIgnoreCase)) { // Check if the common name seems like a DNS name if (Regex.IsMatch(subjectName.CommonName, @"^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$")) { subjectAlternativeNames.Insert(0, subjectName.CommonName); } } var options = new Options() { subjectName = subjectName.ToString(), subjectAlternativeNames = subjectAlternativeNames.ToArray(), keySize = int.Parse(dropDownListKeySize.SelectedValue as string), serialNumber = Guid.NewGuid().ToByteArray(), basicKeyUsages = FindLogicalChildren <CheckBox>(checkBoxListBasicKeyUsages) .Where(x => x.IsChecked == true) .Select(x => (BasicKeyUsages)x.Tag) .DefaultIfEmpty() // The "Aggregate" method requires at least one value .Aggregate((x, y) => x | y), basicKeyUsagesCritical = checkBoxBasicKeyUsages_Critical.IsChecked == true, extendedUsages = FindLogicalChildren <CheckBox>(checkBoxListExtendedKeyUsages) .Where(x => x.IsChecked == true) .Select(x => (string)x.Tag) .ToList(), extendedUsagesCritical = checkBoxExtendedKeyUsages_Critical.IsChecked == true, fromDate = DateTime.SpecifyKind(textBoxPeriod_From.SelectedDate.Value, DateTimeKind.Utc), toDate = DateTime.SpecifyKind(textBoxPeriod_To.SelectedDate.Value, DateTimeKind.Utc), isCA = checkBoxCA.IsChecked == true, caLength = int.Parse(dropDownListCA_MaxPathLength.SelectedValue as string), issuerCertificate = issuerCertificate, outputFile = System.IO.Path.HasExtension(outputPath) ? outputPath : System.IO.Path.ChangeExtension(outputPath, ".pfx"), outputPassword = outputPassword, outputCertFile = System.IO.Path.ChangeExtension(outputPath, ".cer"), }; // Check if some of the output files exist if (File.Exists(options.outputFile)) { var result = MessageBox.Show( this, string.Format("'{0}' already exists. Overwrite?", options.outputFile), "Overwrite PKCS#12 File", MessageBoxButton.YesNo, MessageBoxImage.Question ); if (result != MessageBoxResult.Yes) { return; } } if (File.Exists(options.outputCertFile)) { var result = MessageBox.Show( this, string.Format("'{0}' already exists. Overwrite?", options.outputCertFile), "Overwrite X.509 Certificate File", MessageBoxButton.YesNo, MessageBoxImage.Question ); if (result != MessageBoxResult.Yes) { return; } } panelMain.IsEnabled = false; Task.Factory .StartNew(() => GenerateCertificate(options)) .ContinueWith(GenerateCompleted, TaskScheduler.FromCurrentSynchronizationContext()); }