using System; using System.Windows.Forms; using nsoftware.SecureBlackbox; namespace CAdESVerifierDemo { /// /// Summary description for SignForm. /// public class SignForm : System.Windows.Forms.Form { private System.Windows.Forms.Button btnCancel; private System.Windows.Forms.Button btnOK; private System.Windows.Forms.OpenFileDialog dlgOpen; /// /// Required designer variable. /// private System.ComponentModel.Container components = null; public TextBox edSerialNumber; private Label label1; public TextBox edSubjectKeyID; private Label label2; public TextBox edIssuerRDN; private Label label3; private GroupBox gbSigningCertificates; public ListView lvSigningCertificates; private ColumnHeader chSerial; private ColumnHeader chIssuer; private Button btnRemove; private Button btnAdd; private CAdESVerifier FVerifier = null; public SignForm(CAdESVerifier verifier) { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // FVerifier = verifier; } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.btnCancel = new System.Windows.Forms.Button(); this.btnOK = new System.Windows.Forms.Button(); this.dlgOpen = new System.Windows.Forms.OpenFileDialog(); this.edSerialNumber = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label(); this.edSubjectKeyID = new System.Windows.Forms.TextBox(); this.label2 = new System.Windows.Forms.Label(); this.edIssuerRDN = new System.Windows.Forms.TextBox(); this.label3 = new System.Windows.Forms.Label(); this.gbSigningCertificates = new System.Windows.Forms.GroupBox(); this.lvSigningCertificates = new System.Windows.Forms.ListView(); this.chSerial = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); this.chIssuer = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); this.btnRemove = new System.Windows.Forms.Button(); this.btnAdd = new System.Windows.Forms.Button(); this.gbSigningCertificates.SuspendLayout(); this.SuspendLayout(); // // btnCancel // this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel; this.btnCancel.Location = new System.Drawing.Point(270, 217); this.btnCancel.Name = "btnCancel"; this.btnCancel.Size = new System.Drawing.Size(75, 25); this.btnCancel.TabIndex = 7; this.btnCancel.Text = "Cancel"; // // btnOK // this.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK; this.btnOK.Location = new System.Drawing.Point(182, 217); this.btnOK.Name = "btnOK"; this.btnOK.Size = new System.Drawing.Size(75, 25); this.btnOK.TabIndex = 6; this.btnOK.Text = "OK"; // // edSerialNumber // this.edSerialNumber.Enabled = false; this.edSerialNumber.Location = new System.Drawing.Point(90, 41); this.edSerialNumber.Name = "edSerialNumber"; this.edSerialNumber.ReadOnly = true; this.edSerialNumber.Size = new System.Drawing.Size(250, 20); this.edSerialNumber.TabIndex = 22; // // label1 // this.label1.Location = new System.Drawing.Point(5, 45); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(85, 16); this.label1.TabIndex = 21; this.label1.Text = "Serial Number:"; // // edSubjectKeyID // this.edSubjectKeyID.Enabled = false; this.edSubjectKeyID.Location = new System.Drawing.Point(90, 71); this.edSubjectKeyID.Name = "edSubjectKeyID"; this.edSubjectKeyID.ReadOnly = true; this.edSubjectKeyID.Size = new System.Drawing.Size(250, 20); this.edSubjectKeyID.TabIndex = 24; // // label2 // this.label2.Location = new System.Drawing.Point(5, 75); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(85, 16); this.label2.TabIndex = 23; this.label2.Text = "Subject KeyID:"; // // edIssuerRDN // this.edIssuerRDN.Enabled = false; this.edIssuerRDN.Location = new System.Drawing.Point(90, 11); this.edIssuerRDN.Name = "edIssuerRDN"; this.edIssuerRDN.ReadOnly = true; this.edIssuerRDN.Size = new System.Drawing.Size(250, 20); this.edIssuerRDN.TabIndex = 26; // // label3 // this.label3.Location = new System.Drawing.Point(5, 15); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(85, 16); this.label3.TabIndex = 25; this.label3.Text = "Issuer RDN:"; // // gbSigningCertificates // this.gbSigningCertificates.Controls.Add(this.lvSigningCertificates); this.gbSigningCertificates.Controls.Add(this.btnRemove); this.gbSigningCertificates.Controls.Add(this.btnAdd); this.gbSigningCertificates.Location = new System.Drawing.Point(5, 105); this.gbSigningCertificates.Name = "gbSigningCertificates"; this.gbSigningCertificates.Size = new System.Drawing.Size(340, 100); this.gbSigningCertificates.TabIndex = 27; this.gbSigningCertificates.TabStop = false; this.gbSigningCertificates.Text = "Known Certificates"; // // lvSigningCertificates // this.lvSigningCertificates.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.lvSigningCertificates.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] { this.chSerial, this.chIssuer}); this.lvSigningCertificates.HideSelection = false; this.lvSigningCertificates.Location = new System.Drawing.Point(5, 20); this.lvSigningCertificates.Name = "lvSigningCertificates"; this.lvSigningCertificates.Size = new System.Drawing.Size(250, 75); this.lvSigningCertificates.TabIndex = 4; this.lvSigningCertificates.UseCompatibleStateImageBehavior = false; this.lvSigningCertificates.View = System.Windows.Forms.View.Details; // // chSerial // this.chSerial.Text = "Serial"; this.chSerial.Width = 90; // // chIssuer // this.chIssuer.Text = "Issuer"; this.chIssuer.Width = 150; // // btnRemove // this.btnRemove.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.btnRemove.Location = new System.Drawing.Point(260, 50); this.btnRemove.Name = "btnRemove"; this.btnRemove.Size = new System.Drawing.Size(75, 25); this.btnRemove.TabIndex = 3; this.btnRemove.Text = "Remove"; this.btnRemove.Click += new System.EventHandler(this.btnRemove_Click); // // btnAdd // this.btnAdd.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.btnAdd.Location = new System.Drawing.Point(260, 20); this.btnAdd.Name = "btnAdd"; this.btnAdd.Size = new System.Drawing.Size(75, 25); this.btnAdd.TabIndex = 2; this.btnAdd.Text = "Add"; this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click); // // SignForm // this.AcceptButton = this.btnOK; this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.CancelButton = this.btnCancel; this.ClientSize = new System.Drawing.Size(349, 251); this.Controls.Add(this.gbSigningCertificates); this.Controls.Add(this.edIssuerRDN); this.Controls.Add(this.label3); this.Controls.Add(this.edSubjectKeyID); this.Controls.Add(this.label2); this.Controls.Add(this.edSerialNumber); this.Controls.Add(this.label1); this.Controls.Add(this.btnCancel); this.Controls.Add(this.btnOK); this.Cursor = System.Windows.Forms.Cursors.Default; this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.MaximizeBox = false; this.Name = "SignForm"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent; this.Text = "Signature Options"; this.gbSigningCertificates.ResumeLayout(false); this.ResumeLayout(false); this.PerformLayout(); } #endregion public Certificate LoadCertificate(string file, string password) { Certificate cert = null; if (file.Length > 0) { try { CertificateManager certmanager = new CertificateManager(); certmanager.ImportFromFile(file, password); cert = certmanager.Certificate; } catch (Exception e) { MessageBox.Show("Cannot load certificate!"); } } return cert; } private string RequestPassword() { frmRequestPassword dlg = new frmRequestPassword(); string s = ""; if (dlg.ShowDialog() == DialogResult.OK) { s = dlg.tbInput.Text; } dlg.Dispose(); return s; } public void UpdateCertificates() { lvSigningCertificates.BeginUpdate(); lvSigningCertificates.Items.Clear(); for (int i = 0; i < FVerifier.KnownCertificates.Count; i++) { string s = FVerifier.KnownCertificates[i].Issuer; if (s == "") s = ""; ListViewItem Item = lvSigningCertificates.Items.Add(BitConverter.ToString(FVerifier.KnownCertificates[i].SerialNumber)); Item.SubItems.Add(s); } lvSigningCertificates.EndUpdate(); } private void btnAdd_Click(object sender, EventArgs e) { dlgOpen.Title = "Select certificate file"; dlgOpen.Filter = "PEM-encoded certificate (*.pem)|*.PEM|DER-encoded certificate (*.cer)|*.CER|PFX-encoded certificate (*.pfx)|*.PFX"; if (dlgOpen.ShowDialog() == DialogResult.OK) { Certificate cert = LoadCertificate(dlgOpen.FileName, RequestPassword()); FVerifier.KnownCertificates.Add(cert); UpdateCertificates(); } } private void btnRemove_Click(object sender, EventArgs e) { if (lvSigningCertificates.SelectedItems.Count > 0) { FVerifier.KnownCertificates.RemoveAt(lvSigningCertificates.SelectedItems[0].Index); UpdateCertificates(); } } } }