/// <summary> /// Generate Certificate Signing Request (CSR) from private key. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void CSRBtn_Click(object sender, EventArgs e) { try { if (SystemTitleTb.Text == "") { throw new Exception("Invalid system title."); } byte[] st = GXDLMSTranslator.HexToBytes(SystemTitleTb.Text); if (st.Length != 8) { throw new Exception("Invalid system title."); } OpenFileDialog dlg = new OpenFileDialog(); dlg.Multiselect = false; if (string.IsNullOrEmpty(_path)) { dlg.InitialDirectory = Directory.GetCurrentDirectory(); } else { System.IO.FileInfo fi = new System.IO.FileInfo(_path); dlg.InitialDirectory = fi.DirectoryName; dlg.FileName = fi.Name; } dlg.Filter = Properties.Resources.PemFilterTxt; dlg.DefaultExt = ".pem"; dlg.ValidateNames = true; if (dlg.ShowDialog(Parent) == DialogResult.OK) { string path = dlg.FileName; GXPkcs8 pk = GXPkcs8.Load(path); KeyValuePair <GXPublicKey, GXPrivateKey> kp = new KeyValuePair <GXPublicKey, GXPrivateKey>(pk.PublicKey, pk.PrivateKey); //Generate certificate request and ask new x509Certificate. GXPkcs10 pkc10 = GXPkcs10.CreateCertificateSigningRequest(kp, GXAsn1Converter.SystemTitleToSubject(st)); Pkcs10Tb.Text = pkc10.ToPem(); Pkcs10Tb.AppendText(Environment.NewLine); Pkcs10Tb.AppendText(pkc10.ToString()); } } catch (Exception ex) { MessageBox.Show(Parent, ex.Message); } }
public GXCertifigateGenerationForm(string address, GXPkcs10 pkc10, CertificateType type, byte[] systemTitle) { InitializeComponent(); _address = address; SystemTitleTb.Text = GXDLMSTranslator.ToHex(systemTitle, false); LoadBtn.Enabled = CSRBtn.Enabled = false; Pkcs10Tb.Text = pkc10.ToPem(); Pkcs10Tb.AppendText(Environment.NewLine); Pkcs10Tb.AppendText(pkc10.ToString()); if (type == CertificateType.DigitalSignature) { DigitalSignatureCb.Checked = true; } else if (type == CertificateType.KeyAgreement) { KeyAgreementCb.Checked = true; } else { ServerTlsCb.Checked = true; } ServerTlsCb.Enabled = KeyAgreementCb.Enabled = DigitalSignatureCb.Enabled = false; }