securebb-win-demos/PDF Verifier (WinForms)/signform.cs

346 lines
14 KiB
C#
Raw Normal View History

2024-08-08 13:09:34 +06:00
using System;
using System.Windows.Forms;
using nsoftware.SecureBlackbox;
namespace PDFVerifierDemo
{
/// <summary>
/// Summary description for SignForm.
/// </summary>
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;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public TextBox edSignatureName;
private Label lbSignatureName;
public TextBox edSerialNumber;
private Label label1;
public TextBox edSubjectKeyID;
private Label label2;
public TextBox edIssuerRDN;
private Label label3;
private GroupBox gbSigningCertificates;
public ListView lvKnownCertificates;
private ColumnHeader chSerial;
private ColumnHeader chIssuer;
private Button btnRemove;
private Button btnAdd;
private PDFVerifier FVerifier = null;
public SignForm(PDFVerifier verifier)
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
FVerifier = verifier;
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
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.edSignatureName = new System.Windows.Forms.TextBox();
this.lbSignatureName = new System.Windows.Forms.Label();
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.lvKnownCertificates = 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(250, 255);
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(165, 255);
this.btnOK.Name = "btnOK";
this.btnOK.Size = new System.Drawing.Size(75, 25);
this.btnOK.TabIndex = 6;
this.btnOK.Text = "OK";
//
// edSignatureName
//
this.edSignatureName.Enabled = false;
this.edSignatureName.Location = new System.Drawing.Point(100, 20);
this.edSignatureName.Name = "edSignatureName";
this.edSignatureName.ReadOnly = true;
this.edSignatureName.Size = new System.Drawing.Size(220, 20);
this.edSignatureName.TabIndex = 20;
//
// lbSignatureName
//
this.lbSignatureName.AutoSize = true;
this.lbSignatureName.Location = new System.Drawing.Point(7, 23);
this.lbSignatureName.Name = "lbSignatureName";
this.lbSignatureName.Size = new System.Drawing.Size(84, 13);
this.lbSignatureName.TabIndex = 19;
this.lbSignatureName.Text = "Signature name:";
//
// edSerialNumber
//
this.edSerialNumber.Enabled = false;
this.edSerialNumber.Location = new System.Drawing.Point(100, 80);
this.edSerialNumber.Name = "edSerialNumber";
this.edSerialNumber.ReadOnly = true;
this.edSerialNumber.Size = new System.Drawing.Size(220, 20);
this.edSerialNumber.TabIndex = 22;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(7, 83);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(76, 13);
this.label1.TabIndex = 21;
this.label1.Text = "Serial Number:";
//
// edSubjectKeyID
//
this.edSubjectKeyID.Enabled = false;
this.edSubjectKeyID.Location = new System.Drawing.Point(100, 110);
this.edSubjectKeyID.Name = "edSubjectKeyID";
this.edSubjectKeyID.ReadOnly = true;
this.edSubjectKeyID.Size = new System.Drawing.Size(220, 20);
this.edSubjectKeyID.TabIndex = 24;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(7, 113);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(78, 13);
this.label2.TabIndex = 23;
this.label2.Text = "Subject KeyID:";
//
// edIssuerRDN
//
this.edIssuerRDN.Enabled = false;
this.edIssuerRDN.Location = new System.Drawing.Point(100, 50);
this.edIssuerRDN.Name = "edIssuerRDN";
this.edIssuerRDN.ReadOnly = true;
this.edIssuerRDN.Size = new System.Drawing.Size(220, 20);
this.edIssuerRDN.TabIndex = 26;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(7, 53);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(65, 13);
this.label3.TabIndex = 25;
this.label3.Text = "Issuer RDN:";
//
// gbSigningCertificates
//
this.gbSigningCertificates.Controls.Add(this.lvKnownCertificates);
this.gbSigningCertificates.Controls.Add(this.btnRemove);
this.gbSigningCertificates.Controls.Add(this.btnAdd);
this.gbSigningCertificates.Location = new System.Drawing.Point(5, 145);
this.gbSigningCertificates.Name = "gbSigningCertificates";
this.gbSigningCertificates.Size = new System.Drawing.Size(320, 100);
this.gbSigningCertificates.TabIndex = 27;
this.gbSigningCertificates.TabStop = false;
this.gbSigningCertificates.Text = "Known Certificates";
//
// lvKnownCertificates
//
this.lvKnownCertificates.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.lvKnownCertificates.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.chSerial,
this.chIssuer});
this.lvKnownCertificates.HideSelection = false;
this.lvKnownCertificates.Location = new System.Drawing.Point(5, 20);
this.lvKnownCertificates.Name = "lvKnownCertificates";
this.lvKnownCertificates.Size = new System.Drawing.Size(230, 75);
this.lvKnownCertificates.TabIndex = 4;
this.lvKnownCertificates.UseCompatibleStateImageBehavior = false;
this.lvKnownCertificates.View = System.Windows.Forms.View.Details;
//
// chSerial
//
this.chSerial.Text = "Serial";
this.chSerial.Width = 90;
//
// chIssuer
//
this.chIssuer.Text = "Issuer";
this.chIssuer.Width = 130;
//
// 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(240, 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(240, 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(329, 286);
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.edSignatureName);
this.Controls.Add(this.lbSignatureName);
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()
{
lvKnownCertificates.BeginUpdate();
lvKnownCertificates.Items.Clear();
for (int i = 0; i < FVerifier.KnownCertificates.Count; i++)
{
string s = FVerifier.KnownCertificates[i].Issuer;
if (s == "")
s = "<unknown>";
ListViewItem Item = lvKnownCertificates.Items.Add(BitConverter.ToString(FVerifier.KnownCertificates[i].SerialNumber));
Item.SubItems.Add(s);
}
lvKnownCertificates.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 (lvKnownCertificates.SelectedItems.Count > 0)
{
FVerifier.KnownCertificates.RemoveAt(lvKnownCertificates.SelectedItems[0].Index);
UpdateCertificates();
}
}
}
}