347 lines
14 KiB
C#
347 lines
14 KiB
C#
|
using System;
|
||
|
using System.Windows.Forms;
|
||
|
using nsoftware.SecureBlackbox;
|
||
|
|
||
|
namespace XMLVerifierDemo
|
||
|
{
|
||
|
/// <summary>
|
||
|
/// Summary description for SignForm.
|
||
|
/// </summary>
|
||
|
public class SignForm : System.Windows.Forms.Form
|
||
|
{
|
||
|
private System.Windows.Forms.OpenFileDialog dlgOpen;
|
||
|
/// <summary>
|
||
|
/// Required designer variable.
|
||
|
/// </summary>
|
||
|
private System.ComponentModel.Container components = null;
|
||
|
private GroupBox gbGeneralEnc;
|
||
|
public TextBox edHashAlgorithm;
|
||
|
public TextBox edCanonMethod;
|
||
|
private Label lbSignatureMethod;
|
||
|
private Label lbCanonMethod;
|
||
|
private GroupBox gbKeyInfo;
|
||
|
private GroupBox gbSigningCertificates;
|
||
|
public ListView lvSigningCertificates;
|
||
|
private ColumnHeader chSerial;
|
||
|
private ColumnHeader chIssuer;
|
||
|
private Button btnRemove;
|
||
|
private Button btnAdd;
|
||
|
public TextBox edKeyName;
|
||
|
private Label lbKeyName;
|
||
|
private Button btnCancel;
|
||
|
private Button btnOK;
|
||
|
private XMLVerifier FVerifier = null;
|
||
|
|
||
|
public SignForm(XMLVerifier 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.dlgOpen = new System.Windows.Forms.OpenFileDialog();
|
||
|
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 = new System.Windows.Forms.GroupBox();
|
||
|
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.edKeyName = new System.Windows.Forms.TextBox();
|
||
|
this.lbKeyName = new System.Windows.Forms.Label();
|
||
|
this.btnCancel = new System.Windows.Forms.Button();
|
||
|
this.btnOK = new System.Windows.Forms.Button();
|
||
|
this.gbGeneralEnc.SuspendLayout();
|
||
|
this.gbKeyInfo.SuspendLayout();
|
||
|
this.gbSigningCertificates.SuspendLayout();
|
||
|
this.SuspendLayout();
|
||
|
//
|
||
|
// 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(4, 9);
|
||
|
this.gbGeneralEnc.Name = "gbGeneralEnc";
|
||
|
this.gbGeneralEnc.Size = new System.Drawing.Size(310, 120);
|
||
|
this.gbGeneralEnc.TabIndex = 22;
|
||
|
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";
|
||
|
//
|
||
|
// 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(4, 144);
|
||
|
this.gbKeyInfo.Name = "gbKeyInfo";
|
||
|
this.gbKeyInfo.Size = new System.Drawing.Size(310, 160);
|
||
|
this.gbKeyInfo.TabIndex = 21;
|
||
|
this.gbKeyInfo.TabStop = false;
|
||
|
this.gbKeyInfo.Text = "Key Info";
|
||
|
//
|
||
|
// 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, 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 = "Signing 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(210, 70);
|
||
|
this.lvSigningCertificates.TabIndex = 4;
|
||
|
this.lvSigningCertificates.UseCompatibleStateImageBehavior = false;
|
||
|
this.lvSigningCertificates.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";
|
||
|
//
|
||
|
// 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";
|
||
|
//
|
||
|
// 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:";
|
||
|
//
|
||
|
// 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(239, 313);
|
||
|
this.btnCancel.Name = "btnCancel";
|
||
|
this.btnCancel.Size = new System.Drawing.Size(75, 25);
|
||
|
this.btnCancel.TabIndex = 20;
|
||
|
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(149, 313);
|
||
|
this.btnOK.Name = "btnOK";
|
||
|
this.btnOK.Size = new System.Drawing.Size(75, 25);
|
||
|
this.btnOK.TabIndex = 19;
|
||
|
this.btnOK.Text = "OK";
|
||
|
//
|
||
|
// SignForm
|
||
|
//
|
||
|
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
|
||
|
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.gbGeneralEnc.ResumeLayout(false);
|
||
|
this.gbGeneralEnc.PerformLayout();
|
||
|
this.gbKeyInfo.ResumeLayout(false);
|
||
|
this.gbKeyInfo.PerformLayout();
|
||
|
this.gbSigningCertificates.ResumeLayout(false);
|
||
|
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()
|
||
|
{
|
||
|
lvSigningCertificates.BeginUpdate();
|
||
|
lvSigningCertificates.Items.Clear();
|
||
|
|
||
|
for (int i = 0; i < FVerifier.KnownCertificates.Count; i++)
|
||
|
{
|
||
|
string s = FVerifier.KnownCertificates[i].Issuer;
|
||
|
if (s == "")
|
||
|
s = "<unknown>";
|
||
|
|
||
|
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();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|