private void uxLoadCertificateFile_Click(object sender, EventArgs e) { var dlg = uxOpenFileDialog; dlg.Title = "Load Certificate"; dlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); dlg.Filter = "Certificate Files (*.cer)|*.cer|All Files (*.*)|*.*"; dlg.FilterIndex = 0; if (_lastCertificateFile.Length > 0) { dlg.FileName = Path.GetFileName(_lastCertificateFile); dlg.InitialDirectory = Path.GetDirectoryName(_lastCertificateFile); } if (dlg.ShowDialog(this) == System.Windows.Forms.DialogResult.OK) { var errMsg = string.Empty; try { errMsg = "Couldn't load InfFile from certificate"; _lastCertificateFile = dlg.FileName; Folder = _lastCertificateFile; var f = new InfFile(); f.LoadFromCertificate(dlg.FileName); errMsg = "Couldn't get main domain."; uxMainDomainNameTextBox.Text = f.MainDomain; errMsg = "Couldn't get organization unit"; uxOrganizationalUnitTextBox.Text = f.OrganizationalUnit; errMsg = "Couldn't get organization"; uxOrganizationTextBox.Text = f.Organization; errMsg = "Couldn't get street"; uxStreetAddressTextBox.Text = f.Street; errMsg = "Couldn't city"; uxCityTextBox.Text = f.City; errMsg = "Couldn't state"; uxStateComboBox.Text = f.State; errMsg = "Couldn't get postal code"; uxPostalCodeTextBox.Text = f.PostalCode; errMsg = "Couldn't set country dropdown. Country = '" + (f.Country ?? string.Empty) + "'"; uxCountryComboBox.SelectedValue = f.Country; errMsg = "Couldn't set key length. KeyLength = '" + f.KeyLength.ToString() + "'"; var cbo = uxKeyLengthComboBox; cbo.SelectedItem = f.KeyLength.ToString(); errMsg = "Couldn't clear alternate domain names listbox"; uxAlternateDomainNamesListBox.Items.Clear(); errMsg = "Couldn't add subject alternative names"; uxAlternateDomainNamesListBox.Items.AddRange(f.SubjectAlternativeNames.ToArray()); } catch (Exception ex) { MessageBox.Show("Error\n\n" + errMsg, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning); //throw; } } }
private void uxGenerateInfButton_Click(object sender, EventArgs e) { try { var f = new InfFile { MainDomain = uxMainDomainNameTextBox.Text, OrganizationalUnit = uxOrganizationalUnitTextBox.Text, Organization = uxOrganizationTextBox.Text, Street = uxStreetAddressTextBox.Text, City = uxCityTextBox.Text, State = uxStateComboBox.Text, PostalCode = uxPostalCodeTextBox.Text, Country = SelectedCountryCode, KeyLength = Convert.ToInt32(uxKeyLengthComboBox.Text), OS = (uxWinSvr2003XpCheckBox.Checked ? InfFile.OperatingSystem.WindowsServer2003OrXp : InfFile.OperatingSystem.Other), IsPrivateKeyExportable = uxPrivateKeyExportableCheckBox.Checked }; foreach (var item in uxAlternateDomainNamesListBox.Items) { var s = item.ToString(); if (!string.IsNullOrWhiteSpace(s)) f.SubjectAlternativeNames.Add(s); } var createInfFile = true; if (!f.ValidateSubjectAlternativeNames()) { var msg = "The following domain(s) seem(s) to be missing from the list of\n" + " Alternate Domain Names. Would you like to add them?\n\n" + string.Join("\n", f.MissingSans); var dlgResult = MessageBox.Show(msg, Text, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1); if (dlgResult == DialogResult.Yes) foreach (var item in f.MissingSans) uxAlternateDomainNamesListBox.Items.Add(item); createInfFile = (dlgResult == DialogResult.No); } if (createInfFile) { var fileName = f.GetFileName() + DateTime.UtcNow.ToString("--yyyy-MM-dd--HH-mm"); var fullPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); var dlg = uxSaveFileDialog; dlg.Title = "Save INF File"; dlg.InitialDirectory = fullPath; dlg.FileName = fileName + ".inf"; dlg.Filter = "CSR Information File (*.inf)|*.inf|All Files (*.*)|*.*"; dlg.FilterIndex = 0; if (DialogResult.OK == dlg.ShowDialog()) { fullPath = dlg.FileName; Folder = fullPath; File.WriteAllText(fullPath, f.GetFileContents()); var fullPathWithoutExt = fullPath.Substring(0, fullPath.Length - 3); var cmd = "certreq -new \"" + fullPathWithoutExt + "inf\" \"" + fullPathWithoutExt + "csr\""; new CommandForm { Command = cmd }.ShowDialog(this); } } } catch (Exception ex) { MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); //throw; } }