414 lines
16 KiB
C#
414 lines
16 KiB
C#
|
/*
|
||
|
* SecureBlackbox 2024 .NET Edition - Sample Project
|
||
|
*
|
||
|
* This sample project demonstrates the usage of SecureBlackbox in a
|
||
|
* simple, straightforward way. It is not intended to be a complete
|
||
|
* application. Error handling and other checks are simplified for clarity.
|
||
|
*
|
||
|
* www.nsoftware.com/secureblackbox
|
||
|
*
|
||
|
* This code is subject to the terms and conditions specified in the
|
||
|
* corresponding product license agreement which outlines the authorized
|
||
|
* usage and restrictions.
|
||
|
*
|
||
|
*/
|
||
|
|
||
|
using System;
|
||
|
using System.Windows.Forms;
|
||
|
using System.Collections.Generic;
|
||
|
using nsoftware.SecureBlackbox;
|
||
|
using System.IO;
|
||
|
|
||
|
namespace XMLVerifierDemo
|
||
|
{
|
||
|
/// <summary>
|
||
|
/// Summary description for frmMain.
|
||
|
/// </summary>
|
||
|
public class MainForm : System.Windows.Forms.Form
|
||
|
{
|
||
|
private System.Windows.Forms.OpenFileDialog dlgOpen;
|
||
|
/// <summary>
|
||
|
/// Required designer variable.
|
||
|
/// </summary>
|
||
|
private System.ComponentModel.Container components = null;
|
||
|
|
||
|
private SignForm frmSign = null;
|
||
|
private ReferencesForm frmReferences;
|
||
|
private XMLVerifier verifier;
|
||
|
private Label label1;
|
||
|
private CheckBox cbDetached;
|
||
|
private TextBox edDataFile;
|
||
|
private Button sbBrowseDataFile;
|
||
|
private Label lDataFile;
|
||
|
private TextBox edXMLFile;
|
||
|
private Button btnVerify;
|
||
|
private Button sbBrowseXMLFile;
|
||
|
private Label lbXMLFile;
|
||
|
private Label label2;
|
||
|
private List<ReferenceRes> ReferenceResult = new List<ReferenceRes>();
|
||
|
|
||
|
public MainForm()
|
||
|
{
|
||
|
//
|
||
|
// Required for Windows Form Designer support
|
||
|
//
|
||
|
InitializeComponent();
|
||
|
|
||
|
//
|
||
|
// TODO: Add any constructor code after InitializeComponent call
|
||
|
//
|
||
|
verifier = new XMLVerifier();
|
||
|
verifier.OnReferenceValidated += new XMLVerifier.OnReferenceValidatedHandler(ReferenceValidated);
|
||
|
verifier.OnSignatureFound += new XMLVerifier.OnSignatureFoundHandler(SignatureFound);
|
||
|
|
||
|
frmSign = new SignForm(verifier);
|
||
|
frmReferences = new ReferencesForm();
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Clean up any resources being used.
|
||
|
/// </summary>
|
||
|
protected override void Dispose(bool disposing)
|
||
|
{
|
||
|
if (disposing)
|
||
|
{
|
||
|
if (components != null)
|
||
|
{
|
||
|
components.Dispose();
|
||
|
frmSign.Dispose();
|
||
|
frmReferences.Dispose();
|
||
|
verifier.Dispose();
|
||
|
}
|
||
|
}
|
||
|
base.Dispose(disposing);
|
||
|
}
|
||
|
|
||
|
private void ReferenceValidated(object s, XMLVerifierReferenceValidatedEventArgs e)
|
||
|
{
|
||
|
ReferenceResult.Add(new ReferenceRes() { Id = e.ID, Uri = e.URI, RefType = e.RefType, DigestValid = e.DigestValid });
|
||
|
}
|
||
|
|
||
|
private void SignatureFound(object s, XMLVerifierSignatureFoundEventArgs e)
|
||
|
{
|
||
|
if (e.CertFound)
|
||
|
{
|
||
|
e.ValidateSignature = true;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
XMLSignature sig = verifier.Signatures[e.Index];
|
||
|
switch (sig.CanonicalizationMethod)
|
||
|
{
|
||
|
case XMLCanonicalizationMethods.cxcmCanon:
|
||
|
{
|
||
|
frmSign.edCanonMethod.Text = "Canonical";
|
||
|
break;
|
||
|
}
|
||
|
case XMLCanonicalizationMethods.cxcmCanonComment:
|
||
|
{
|
||
|
frmSign.edCanonMethod.Text = "Canonical with comments";
|
||
|
break;
|
||
|
}
|
||
|
case XMLCanonicalizationMethods.cxcmCanon_v1_1:
|
||
|
{
|
||
|
frmSign.edCanonMethod.Text = "Canonical v1.1";
|
||
|
break;
|
||
|
}
|
||
|
case XMLCanonicalizationMethods.cxcmCanonComment_v1_1:
|
||
|
{
|
||
|
frmSign.edCanonMethod.Text = "Canonical with comments v1.1";
|
||
|
break;
|
||
|
}
|
||
|
case XMLCanonicalizationMethods.cxcmExclCanon:
|
||
|
{
|
||
|
frmSign.edCanonMethod.Text = "Exclusive canonical";
|
||
|
break;
|
||
|
}
|
||
|
case XMLCanonicalizationMethods.cxcmExclCanonComment:
|
||
|
{
|
||
|
frmSign.edCanonMethod.Text = "Exclusive canonical with comments";
|
||
|
break;
|
||
|
}
|
||
|
case XMLCanonicalizationMethods.cxcmMinCanon:
|
||
|
{
|
||
|
frmSign.edCanonMethod.Text = "Minimal canonical";
|
||
|
break;
|
||
|
}
|
||
|
default:
|
||
|
{
|
||
|
frmSign.edCanonMethod.Text = "Unknown";
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
frmSign.edHashAlgorithm.Text = sig.HashAlgorithm;
|
||
|
|
||
|
frmSign.edKeyName.Text = verifier.Config("KeyName");
|
||
|
frmSign.UpdateCertificates();
|
||
|
|
||
|
if (frmSign.ShowDialog() == DialogResult.OK)
|
||
|
{
|
||
|
e.ValidateSignature = true;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
e.ValidateSignature = false;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#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.label1 = new System.Windows.Forms.Label();
|
||
|
this.cbDetached = new System.Windows.Forms.CheckBox();
|
||
|
this.edDataFile = new System.Windows.Forms.TextBox();
|
||
|
this.sbBrowseDataFile = new System.Windows.Forms.Button();
|
||
|
this.lDataFile = new System.Windows.Forms.Label();
|
||
|
this.edXMLFile = new System.Windows.Forms.TextBox();
|
||
|
this.btnVerify = new System.Windows.Forms.Button();
|
||
|
this.sbBrowseXMLFile = new System.Windows.Forms.Button();
|
||
|
this.lbXMLFile = new System.Windows.Forms.Label();
|
||
|
this.label2 = new System.Windows.Forms.Label();
|
||
|
this.SuspendLayout();
|
||
|
//
|
||
|
// label1
|
||
|
//
|
||
|
this.label1.AutoSize = true;
|
||
|
this.label1.ForeColor = System.Drawing.SystemColors.Highlight;
|
||
|
this.label1.Location = new System.Drawing.Point(8, 6);
|
||
|
this.label1.Name = "label1";
|
||
|
this.label1.Size = new System.Drawing.Size(389, 13);
|
||
|
this.label1.TabIndex = 30;
|
||
|
this.label1.Text = "This sample illustrates processing of XML-SIG signatures with XMLVerifier control" +
|
||
|
".";
|
||
|
//
|
||
|
// cbDetached
|
||
|
//
|
||
|
this.cbDetached.AutoSize = true;
|
||
|
this.cbDetached.Location = new System.Drawing.Point(13, 81);
|
||
|
this.cbDetached.Name = "cbDetached";
|
||
|
this.cbDetached.Size = new System.Drawing.Size(73, 17);
|
||
|
this.cbDetached.TabIndex = 29;
|
||
|
this.cbDetached.Text = "Detached";
|
||
|
this.cbDetached.UseVisualStyleBackColor = true;
|
||
|
this.cbDetached.CheckedChanged += new System.EventHandler(this.cbDetached_CheckedChanged);
|
||
|
//
|
||
|
// edDataFile
|
||
|
//
|
||
|
this.edDataFile.Enabled = false;
|
||
|
this.edDataFile.Location = new System.Drawing.Point(66, 103);
|
||
|
this.edDataFile.Name = "edDataFile";
|
||
|
this.edDataFile.Size = new System.Drawing.Size(284, 20);
|
||
|
this.edDataFile.TabIndex = 27;
|
||
|
//
|
||
|
// sbBrowseDataFile
|
||
|
//
|
||
|
this.sbBrowseDataFile.Enabled = false;
|
||
|
this.sbBrowseDataFile.Location = new System.Drawing.Point(356, 100);
|
||
|
this.sbBrowseDataFile.Name = "sbBrowseDataFile";
|
||
|
this.sbBrowseDataFile.Size = new System.Drawing.Size(75, 25);
|
||
|
this.sbBrowseDataFile.TabIndex = 28;
|
||
|
this.sbBrowseDataFile.Text = "Browse ...";
|
||
|
this.sbBrowseDataFile.Click += new System.EventHandler(this.sbBrowseDataFile_Click);
|
||
|
//
|
||
|
// lDataFile
|
||
|
//
|
||
|
this.lDataFile.AutoSize = true;
|
||
|
this.lDataFile.Enabled = false;
|
||
|
this.lDataFile.Location = new System.Drawing.Point(11, 106);
|
||
|
this.lDataFile.Name = "lDataFile";
|
||
|
this.lDataFile.Size = new System.Drawing.Size(49, 13);
|
||
|
this.lDataFile.TabIndex = 26;
|
||
|
this.lDataFile.Text = "Data file:";
|
||
|
//
|
||
|
// edXMLFile
|
||
|
//
|
||
|
this.edXMLFile.Location = new System.Drawing.Point(66, 51);
|
||
|
this.edXMLFile.Name = "edXMLFile";
|
||
|
this.edXMLFile.Size = new System.Drawing.Size(284, 20);
|
||
|
this.edXMLFile.TabIndex = 23;
|
||
|
//
|
||
|
// btnVerify
|
||
|
//
|
||
|
this.btnVerify.Location = new System.Drawing.Point(356, 149);
|
||
|
this.btnVerify.Name = "btnVerify";
|
||
|
this.btnVerify.Size = new System.Drawing.Size(75, 25);
|
||
|
this.btnVerify.TabIndex = 25;
|
||
|
this.btnVerify.Text = "Verify";
|
||
|
this.btnVerify.Click += new System.EventHandler(this.btnVerify_Click);
|
||
|
//
|
||
|
// sbBrowseXMLFile
|
||
|
//
|
||
|
this.sbBrowseXMLFile.Location = new System.Drawing.Point(356, 48);
|
||
|
this.sbBrowseXMLFile.Name = "sbBrowseXMLFile";
|
||
|
this.sbBrowseXMLFile.Size = new System.Drawing.Size(75, 25);
|
||
|
this.sbBrowseXMLFile.TabIndex = 24;
|
||
|
this.sbBrowseXMLFile.Text = "Browse ...";
|
||
|
this.sbBrowseXMLFile.Click += new System.EventHandler(this.sbBrowseXMLFile_Click);
|
||
|
//
|
||
|
// lbXMLFile
|
||
|
//
|
||
|
this.lbXMLFile.AutoSize = true;
|
||
|
this.lbXMLFile.Location = new System.Drawing.Point(10, 54);
|
||
|
this.lbXMLFile.Name = "lbXMLFile";
|
||
|
this.lbXMLFile.Size = new System.Drawing.Size(50, 13);
|
||
|
this.lbXMLFile.TabIndex = 22;
|
||
|
this.lbXMLFile.Text = "Input file:";
|
||
|
//
|
||
|
// label2
|
||
|
//
|
||
|
this.label2.AutoSize = true;
|
||
|
this.label2.ForeColor = System.Drawing.SystemColors.Highlight;
|
||
|
this.label2.Location = new System.Drawing.Point(8, 22);
|
||
|
this.label2.Name = "label2";
|
||
|
this.label2.Size = new System.Drawing.Size(236, 13);
|
||
|
this.label2.TabIndex = 31;
|
||
|
this.label2.Text = "Please select a signed XML file and click \'Verify\'.";
|
||
|
//
|
||
|
// MainForm
|
||
|
//
|
||
|
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
|
||
|
this.ClientSize = new System.Drawing.Size(440, 185);
|
||
|
this.Controls.Add(this.label2);
|
||
|
this.Controls.Add(this.label1);
|
||
|
this.Controls.Add(this.cbDetached);
|
||
|
this.Controls.Add(this.edDataFile);
|
||
|
this.Controls.Add(this.sbBrowseDataFile);
|
||
|
this.Controls.Add(this.lDataFile);
|
||
|
this.Controls.Add(this.edXMLFile);
|
||
|
this.Controls.Add(this.btnVerify);
|
||
|
this.Controls.Add(this.sbBrowseXMLFile);
|
||
|
this.Controls.Add(this.lbXMLFile);
|
||
|
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
|
||
|
this.MaximizeBox = false;
|
||
|
this.Name = "MainForm";
|
||
|
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
|
||
|
this.Text = "XML Verifier Demo";
|
||
|
this.ResumeLayout(false);
|
||
|
this.PerformLayout();
|
||
|
|
||
|
}
|
||
|
#endregion
|
||
|
|
||
|
/// <summary>
|
||
|
/// The main entry point for the application.
|
||
|
/// </summary>
|
||
|
[STAThread]
|
||
|
static void Main()
|
||
|
{
|
||
|
Application.Run(new MainForm());
|
||
|
}
|
||
|
|
||
|
private void sbBrowseXMLFile_Click(object sender, System.EventArgs e)
|
||
|
{
|
||
|
dlgOpen.InitialDirectory = Application.StartupPath;
|
||
|
dlgOpen.FileName = edXMLFile.Text;
|
||
|
if (dlgOpen.ShowDialog() == DialogResult.OK)
|
||
|
edXMLFile.Text = dlgOpen.FileName;
|
||
|
}
|
||
|
|
||
|
private void btnVerify_Click(object sender, EventArgs e)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
ReferenceResult.Clear();
|
||
|
|
||
|
verifier.KnownCertificates.Clear();
|
||
|
verifier.InputFile = edXMLFile.Text;
|
||
|
|
||
|
if (cbDetached.Checked)
|
||
|
{
|
||
|
verifier.DataFile = edDataFile.Text;
|
||
|
verifier.DataType = XMLVerifierDataTypes.cxdtBinary;
|
||
|
verifier.DataURI = Path.GetFileName(edDataFile.Text);
|
||
|
verifier.VerifyDetached();
|
||
|
}
|
||
|
else
|
||
|
verifier.Verify();
|
||
|
|
||
|
if (verifier.Signatures.Count == 0)
|
||
|
MessageBox.Show("Signature not found", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
|
|
||
|
for (int i = 0; i < verifier.Signatures.Count; i++)
|
||
|
{
|
||
|
XMLSignature sig = verifier.Signatures[i];
|
||
|
switch (sig.SignatureValidationResult)
|
||
|
{
|
||
|
case SignatureValidities.svtSignerNotFound:
|
||
|
{
|
||
|
MessageBox.Show("Signer not found", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
|
break;
|
||
|
}
|
||
|
case SignatureValidities.svtFailure:
|
||
|
{
|
||
|
MessageBox.Show("Signature verification failed", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
|
break;
|
||
|
}
|
||
|
case SignatureValidities.svtCorrupted:
|
||
|
{
|
||
|
MessageBox.Show("Signature is invalid", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
|
break;
|
||
|
}
|
||
|
case SignatureValidities.svtReferenceCorrupted:
|
||
|
{
|
||
|
if (MessageBox.Show("Signature has invalid references.\r\nDo you want to see the result of verifying references?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Error) == DialogResult.Yes)
|
||
|
{
|
||
|
frmReferences.Update(ReferenceResult);
|
||
|
frmReferences.ShowDialog();
|
||
|
}
|
||
|
break;
|
||
|
}
|
||
|
case SignatureValidities.svtValid:
|
||
|
{
|
||
|
if (MessageBox.Show("Signature validated successfully.\r\nDo you want to see the result of verifying references?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
|
||
|
{
|
||
|
frmReferences.Update(ReferenceResult);
|
||
|
frmReferences.ShowDialog();
|
||
|
}
|
||
|
break;
|
||
|
}
|
||
|
default:
|
||
|
MessageBox.Show("Unknown signature validation result", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
MessageBox.Show(ex.Message);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private void cbDetached_CheckedChanged(object sender, EventArgs e)
|
||
|
{
|
||
|
edDataFile.Enabled = cbDetached.Checked;
|
||
|
sbBrowseDataFile.Enabled = cbDetached.Checked;
|
||
|
lDataFile.Enabled = cbDetached.Checked;
|
||
|
}
|
||
|
|
||
|
private void sbBrowseDataFile_Click(object sender, EventArgs e)
|
||
|
{
|
||
|
dlgOpen.InitialDirectory = Application.StartupPath;
|
||
|
dlgOpen.FileName = edDataFile.Text;
|
||
|
if (dlgOpen.ShowDialog() == DialogResult.OK)
|
||
|
edDataFile.Text = dlgOpen.FileName;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public class ReferenceRes
|
||
|
{
|
||
|
public string Id { get; set; }
|
||
|
public string Uri { get; set; }
|
||
|
public string RefType { get; set; }
|
||
|
public bool DigestValid { get; set; }
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|