securebb-win-demos/XAdES Verifier (WinForms)/signform.cs
2024-08-08 13:09:34 +06:00

348 lines
14 KiB
C#

using System;
using System.Windows.Forms;
using nsoftware.SecureBlackbox;
/// <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;
private GroupBox gbKeyInfo;
private GroupBox gbSigningCertificates;
public ListView lvKnownCertificates;
private ColumnHeader chSerial;
private ColumnHeader chIssuer;
private Button btnRemove;
private Button btnAdd;
public TextBox edKeyName;
private Label lbKeyName;
private GroupBox gbGeneralEnc;
private Label lbSignatureMethod;
private Label lbCanonMethod;
public TextBox edHashAlgorithm;
public TextBox edCanonMethod;
private XAdESVerifier FVerifier = null;
public SignForm(XAdESVerifier 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.gbKeyInfo = new System.Windows.Forms.GroupBox();
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.edKeyName = new System.Windows.Forms.TextBox();
this.lbKeyName = new System.Windows.Forms.Label();
this.gbGeneralEnc = new System.Windows.Forms.GroupBox();
this.edHashAlgorithm = new System.Windows.Forms.TextBox();
this.edCanonMethod = new System.Windows.Forms.TextBox();
this.lbSignatureMethod = new System.Windows.Forms.Label();
this.lbCanonMethod = new System.Windows.Forms.Label();
this.gbKeyInfo.SuspendLayout();
this.gbSigningCertificates.SuspendLayout();
this.gbGeneralEnc.SuspendLayout();
this.SuspendLayout();
//
// btnCancel
//
this.btnCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.btnCancel.Location = new System.Drawing.Point(240, 314);
this.btnCancel.Name = "btnCancel";
this.btnCancel.Size = new System.Drawing.Size(75, 25);
this.btnCancel.TabIndex = 7;
this.btnCancel.Text = "Cancel";
//
// btnOK
//
this.btnOK.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK;
this.btnOK.Location = new System.Drawing.Point(150, 314);
this.btnOK.Name = "btnOK";
this.btnOK.Size = new System.Drawing.Size(75, 25);
this.btnOK.TabIndex = 6;
this.btnOK.Text = "OK";
//
// gbKeyInfo
//
this.gbKeyInfo.Controls.Add(this.gbSigningCertificates);
this.gbKeyInfo.Controls.Add(this.edKeyName);
this.gbKeyInfo.Controls.Add(this.lbKeyName);
this.gbKeyInfo.Location = new System.Drawing.Point(5, 145);
this.gbKeyInfo.Name = "gbKeyInfo";
this.gbKeyInfo.Size = new System.Drawing.Size(310, 160);
this.gbKeyInfo.TabIndex = 17;
this.gbKeyInfo.TabStop = false;
this.gbKeyInfo.Text = "Key Info";
//
// 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, 57);
this.gbSigningCertificates.Name = "gbSigningCertificates";
this.gbSigningCertificates.Size = new System.Drawing.Size(300, 100);
this.gbSigningCertificates.TabIndex = 16;
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(210, 70);
this.lvKnownCertificates.TabIndex = 4;
this.lvKnownCertificates.UseCompatibleStateImageBehavior = false;
this.lvKnownCertificates.View = System.Windows.Forms.View.Details;
//
// chSerial
//
this.chSerial.Text = "Serial";
this.chSerial.Width = 85;
//
// chIssuer
//
this.chIssuer.Text = "Issuer";
this.chIssuer.Width = 120;
//
// 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(220, 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(220, 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);
//
// edKeyName
//
this.edKeyName.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.edKeyName.Location = new System.Drawing.Point(81, 19);
this.edKeyName.Name = "edKeyName";
this.edKeyName.ReadOnly = true;
this.edKeyName.Size = new System.Drawing.Size(218, 20);
this.edKeyName.TabIndex = 1;
//
// lbKeyName
//
this.lbKeyName.Location = new System.Drawing.Point(17, 23);
this.lbKeyName.Name = "lbKeyName";
this.lbKeyName.Size = new System.Drawing.Size(64, 16);
this.lbKeyName.TabIndex = 0;
this.lbKeyName.Text = "Key Name:";
//
// gbGeneralEnc
//
this.gbGeneralEnc.Controls.Add(this.edHashAlgorithm);
this.gbGeneralEnc.Controls.Add(this.edCanonMethod);
this.gbGeneralEnc.Controls.Add(this.lbSignatureMethod);
this.gbGeneralEnc.Controls.Add(this.lbCanonMethod);
this.gbGeneralEnc.Location = new System.Drawing.Point(5, 10);
this.gbGeneralEnc.Name = "gbGeneralEnc";
this.gbGeneralEnc.Size = new System.Drawing.Size(310, 120);
this.gbGeneralEnc.TabIndex = 18;
this.gbGeneralEnc.TabStop = false;
this.gbGeneralEnc.Text = "General";
//
// edHashAlgorithm
//
this.edHashAlgorithm.Enabled = false;
this.edHashAlgorithm.Location = new System.Drawing.Point(98, 64);
this.edHashAlgorithm.Name = "edHashAlgorithm";
this.edHashAlgorithm.ReadOnly = true;
this.edHashAlgorithm.Size = new System.Drawing.Size(160, 20);
this.edHashAlgorithm.TabIndex = 18;
//
// edCanonMethod
//
this.edCanonMethod.Enabled = false;
this.edCanonMethod.Location = new System.Drawing.Point(98, 33);
this.edCanonMethod.Name = "edCanonMethod";
this.edCanonMethod.ReadOnly = true;
this.edCanonMethod.Size = new System.Drawing.Size(186, 20);
this.edCanonMethod.TabIndex = 17;
//
// lbSignatureMethod
//
this.lbSignatureMethod.Location = new System.Drawing.Point(7, 67);
this.lbSignatureMethod.Name = "lbSignatureMethod";
this.lbSignatureMethod.Size = new System.Drawing.Size(85, 16);
this.lbSignatureMethod.TabIndex = 8;
this.lbSignatureMethod.Text = "Hash algorithm:";
//
// lbCanonMethod
//
this.lbCanonMethod.Location = new System.Drawing.Point(7, 28);
this.lbCanonMethod.Name = "lbCanonMethod";
this.lbCanonMethod.Size = new System.Drawing.Size(85, 32);
this.lbCanonMethod.TabIndex = 2;
this.lbCanonMethod.Text = "Canonicalization method";
//
// SignForm
//
this.AcceptButton = this.btnOK;
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.CancelButton = this.btnCancel;
this.ClientSize = new System.Drawing.Size(319, 346);
this.Controls.Add(this.gbGeneralEnc);
this.Controls.Add(this.gbKeyInfo);
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.gbKeyInfo.ResumeLayout(false);
this.gbKeyInfo.PerformLayout();
this.gbSigningCertificates.ResumeLayout(false);
this.gbGeneralEnc.ResumeLayout(false);
this.gbGeneralEnc.PerformLayout();
this.ResumeLayout(false);
}
#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();
}
}
}