/*
* 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 nsoftware.SecureBlackbox;
namespace CAdESVerifierDemo
{
///
/// Summary description for frmMain.
///
public class frmMain : System.Windows.Forms.Form
{
private System.Windows.Forms.Label lbInputFile;
private System.Windows.Forms.TextBox edInputFile;
private System.Windows.Forms.Button sbBrowseInputFile;
private System.Windows.Forms.OpenFileDialog dlgOpen;
///
/// Required designer variable.
///
private System.ComponentModel.Container components = null;
private System.Windows.Forms.Button btnVerify;
private SignForm frmSign = null;
private CAdESVerifier verifier;
private TextBox edDataFile;
private Button sbBrowseDataFile;
private Label lDataFile;
private SaveFileDialog dlgSave;
private Label label5;
private GroupBox groupBox4;
private GroupBox groupBox3;
public ListView lvTrustedCertificates;
private ColumnHeader columnHeader3;
private ColumnHeader columnHeader4;
private Button btnRemoveTrusted;
private Button btnAddTrusted;
public CheckBox cbPerformRevocationCheck;
private GroupBox groupBox2;
public ListView lvKnownCertificates;
private ColumnHeader columnHeader1;
private ColumnHeader columnHeader2;
private Button btnRemoveKnown;
private Button bthAddKnown;
public CheckBox cbIgnoreChainValidationErrors;
public CheckBox cbForceCompleteChainValidation;
private OpenFileDialog openDialogCert;
public CheckBox cbOfflineMode;
private CheckBox cbDetached;
public frmMain()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
verifier = new CAdESVerifier();
verifier.OnSignatureFound += new CAdESVerifier.OnSignatureFoundHandler(SignatureFound);
frmSign = new SignForm(verifier);
}
///
/// Clean up any resources being used.
///
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
frmSign.Dispose();
verifier.Dispose();
}
}
base.Dispose(disposing);
}
private void SignatureFound(object s, CAdESVerifierSignatureFoundEventArgs e)
{
if (e.CertFound)
{
e.ValidateSignature = true;
e.ValidateChain = true;
}
else
{
SignForm dlg = new SignForm(verifier);
dlg.edIssuerRDN.Text = e.IssuerRDN;
dlg.edSerialNumber.Text = BitConverter.ToString(e.SerialNumber);
dlg.edSubjectKeyID.Text = BitConverter.ToString(e.SubjectKeyID);
dlg.UpdateCertificates();
if (dlg.ShowDialog() == DialogResult.OK)
{
e.ValidateSignature = true;
e.ValidateChain = true;
}
else
{
e.ValidateSignature = false;
e.ValidateChain = false;
}
}
}
#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.lbInputFile = new System.Windows.Forms.Label();
this.edInputFile = new System.Windows.Forms.TextBox();
this.sbBrowseInputFile = new System.Windows.Forms.Button();
this.btnVerify = new System.Windows.Forms.Button();
this.dlgOpen = new System.Windows.Forms.OpenFileDialog();
this.edDataFile = new System.Windows.Forms.TextBox();
this.sbBrowseDataFile = new System.Windows.Forms.Button();
this.lDataFile = new System.Windows.Forms.Label();
this.cbDetached = new System.Windows.Forms.CheckBox();
this.dlgSave = new System.Windows.Forms.SaveFileDialog();
this.label5 = new System.Windows.Forms.Label();
this.groupBox4 = new System.Windows.Forms.GroupBox();
this.groupBox3 = new System.Windows.Forms.GroupBox();
this.lvTrustedCertificates = new System.Windows.Forms.ListView();
this.columnHeader3 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.columnHeader4 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.btnRemoveTrusted = new System.Windows.Forms.Button();
this.btnAddTrusted = new System.Windows.Forms.Button();
this.cbPerformRevocationCheck = new System.Windows.Forms.CheckBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.lvKnownCertificates = new System.Windows.Forms.ListView();
this.columnHeader1 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.columnHeader2 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.btnRemoveKnown = new System.Windows.Forms.Button();
this.bthAddKnown = new System.Windows.Forms.Button();
this.cbIgnoreChainValidationErrors = new System.Windows.Forms.CheckBox();
this.cbForceCompleteChainValidation = new System.Windows.Forms.CheckBox();
this.openDialogCert = new System.Windows.Forms.OpenFileDialog();
this.cbOfflineMode = new System.Windows.Forms.CheckBox();
this.groupBox4.SuspendLayout();
this.groupBox3.SuspendLayout();
this.groupBox2.SuspendLayout();
this.SuspendLayout();
//
// lbInputFile
//
this.lbInputFile.Location = new System.Drawing.Point(8, 36);
this.lbInputFile.Name = "lbInputFile";
this.lbInputFile.Size = new System.Drawing.Size(60, 13);
this.lbInputFile.TabIndex = 0;
this.lbInputFile.Text = "Input file:";
//
// edInputFile
//
this.edInputFile.Location = new System.Drawing.Point(73, 33);
this.edInputFile.Name = "edInputFile";
this.edInputFile.Size = new System.Drawing.Size(347, 20);
this.edInputFile.TabIndex = 1;
//
// sbBrowseInputFile
//
this.sbBrowseInputFile.Location = new System.Drawing.Point(426, 30);
this.sbBrowseInputFile.Name = "sbBrowseInputFile";
this.sbBrowseInputFile.Size = new System.Drawing.Size(75, 25);
this.sbBrowseInputFile.TabIndex = 2;
this.sbBrowseInputFile.Text = "Browse ...";
this.sbBrowseInputFile.Click += new System.EventHandler(this.sbBrowseXMLFile_Click);
//
// btnVerify
//
this.btnVerify.Location = new System.Drawing.Point(655, 300);
this.btnVerify.Name = "btnVerify";
this.btnVerify.Size = new System.Drawing.Size(75, 25);
this.btnVerify.TabIndex = 8;
this.btnVerify.Text = "Verify";
this.btnVerify.Click += new System.EventHandler(this.btnVerify_Click);
//
// edDataFile
//
this.edDataFile.Location = new System.Drawing.Point(73, 97);
this.edDataFile.Name = "edDataFile";
this.edDataFile.Size = new System.Drawing.Size(347, 20);
this.edDataFile.TabIndex = 10;
//
// sbBrowseDataFile
//
this.sbBrowseDataFile.Location = new System.Drawing.Point(426, 94);
this.sbBrowseDataFile.Name = "sbBrowseDataFile";
this.sbBrowseDataFile.Size = new System.Drawing.Size(75, 25);
this.sbBrowseDataFile.TabIndex = 11;
this.sbBrowseDataFile.Text = "Browse ...";
this.sbBrowseDataFile.Click += new System.EventHandler(this.sbBrowseDataFile_Click);
//
// lDataFile
//
this.lDataFile.Location = new System.Drawing.Point(8, 100);
this.lDataFile.Name = "lDataFile";
this.lDataFile.Size = new System.Drawing.Size(60, 13);
this.lDataFile.TabIndex = 9;
this.lDataFile.Text = "Output file:";
//
// cbDetached
//
this.cbDetached.AutoSize = true;
this.cbDetached.Location = new System.Drawing.Point(11, 68);
this.cbDetached.Name = "cbDetached";
this.cbDetached.Size = new System.Drawing.Size(73, 17);
this.cbDetached.TabIndex = 12;
this.cbDetached.Text = "Detached";
this.cbDetached.UseVisualStyleBackColor = true;
this.cbDetached.CheckedChanged += new System.EventHandler(this.cbDetached_CheckedChanged);
//
// label5
//
this.label5.ForeColor = System.Drawing.SystemColors.Highlight;
this.label5.Location = new System.Drawing.Point(5, 5);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(725, 13);
this.label5.TabIndex = 71;
this.label5.Text = "This sample shows processing of CAdES signatures. Please select a .p7s or .p7m fi" +
"le, tune up validation settings, and click \'Verify\' when ready.";
//
// groupBox4
//
this.groupBox4.Controls.Add(this.cbOfflineMode);
this.groupBox4.Controls.Add(this.groupBox3);
this.groupBox4.Controls.Add(this.cbPerformRevocationCheck);
this.groupBox4.Controls.Add(this.groupBox2);
this.groupBox4.Controls.Add(this.cbIgnoreChainValidationErrors);
this.groupBox4.Controls.Add(this.cbForceCompleteChainValidation);
this.groupBox4.Location = new System.Drawing.Point(5, 125);
this.groupBox4.Name = "groupBox4";
this.groupBox4.Size = new System.Drawing.Size(725, 160);
this.groupBox4.TabIndex = 72;
this.groupBox4.TabStop = false;
this.groupBox4.Text = "Validation settings ";
//
// groupBox3
//
this.groupBox3.Controls.Add(this.lvTrustedCertificates);
this.groupBox3.Controls.Add(this.btnRemoveTrusted);
this.groupBox3.Controls.Add(this.btnAddTrusted);
this.groupBox3.Location = new System.Drawing.Point(370, 60);
this.groupBox3.Name = "groupBox3";
this.groupBox3.Size = new System.Drawing.Size(350, 95);
this.groupBox3.TabIndex = 17;
this.groupBox3.TabStop = false;
this.groupBox3.Text = "Trusted Certificates";
//
// lvTrustedCertificates
//
this.lvTrustedCertificates.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.columnHeader3,
this.columnHeader4});
this.lvTrustedCertificates.HideSelection = false;
this.lvTrustedCertificates.Location = new System.Drawing.Point(5, 20);
this.lvTrustedCertificates.Name = "lvTrustedCertificates";
this.lvTrustedCertificates.Size = new System.Drawing.Size(260, 70);
this.lvTrustedCertificates.TabIndex = 4;
this.lvTrustedCertificates.UseCompatibleStateImageBehavior = false;
this.lvTrustedCertificates.View = System.Windows.Forms.View.Details;
//
// columnHeader3
//
this.columnHeader3.Text = "Serial";
this.columnHeader3.Width = 90;
//
// columnHeader4
//
this.columnHeader4.Text = "Issuer";
this.columnHeader4.Width = 160;
//
// btnRemoveTrusted
//
this.btnRemoveTrusted.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.btnRemoveTrusted.Location = new System.Drawing.Point(270, 51);
this.btnRemoveTrusted.Name = "btnRemoveTrusted";
this.btnRemoveTrusted.Size = new System.Drawing.Size(75, 25);
this.btnRemoveTrusted.TabIndex = 3;
this.btnRemoveTrusted.Text = "Remove";
this.btnRemoveTrusted.Click += new System.EventHandler(this.btnRemoveTrusted_Click);
//
// btnAddTrusted
//
this.btnAddTrusted.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.btnAddTrusted.Location = new System.Drawing.Point(270, 20);
this.btnAddTrusted.Name = "btnAddTrusted";
this.btnAddTrusted.Size = new System.Drawing.Size(75, 25);
this.btnAddTrusted.TabIndex = 2;
this.btnAddTrusted.Text = "Add";
this.btnAddTrusted.Click += new System.EventHandler(this.btnAddTrusted_Click);
//
// cbPerformRevocationCheck
//
this.cbPerformRevocationCheck.AutoSize = true;
this.cbPerformRevocationCheck.Location = new System.Drawing.Point(411, 29);
this.cbPerformRevocationCheck.Name = "cbPerformRevocationCheck";
this.cbPerformRevocationCheck.Size = new System.Drawing.Size(148, 17);
this.cbPerformRevocationCheck.TabIndex = 40;
this.cbPerformRevocationCheck.Text = "Perform revocation check";
//
// groupBox2
//
this.groupBox2.Controls.Add(this.lvKnownCertificates);
this.groupBox2.Controls.Add(this.btnRemoveKnown);
this.groupBox2.Controls.Add(this.bthAddKnown);
this.groupBox2.Location = new System.Drawing.Point(5, 60);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(350, 95);
this.groupBox2.TabIndex = 16;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "Known Certificates";
//
// lvKnownCertificates
//
this.lvKnownCertificates.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.columnHeader1,
this.columnHeader2});
this.lvKnownCertificates.HideSelection = false;
this.lvKnownCertificates.Location = new System.Drawing.Point(5, 20);
this.lvKnownCertificates.Name = "lvKnownCertificates";
this.lvKnownCertificates.Size = new System.Drawing.Size(260, 70);
this.lvKnownCertificates.TabIndex = 4;
this.lvKnownCertificates.UseCompatibleStateImageBehavior = false;
this.lvKnownCertificates.View = System.Windows.Forms.View.Details;
//
// columnHeader1
//
this.columnHeader1.Text = "Serial";
this.columnHeader1.Width = 90;
//
// columnHeader2
//
this.columnHeader2.Text = "Issuer";
this.columnHeader2.Width = 160;
//
// btnRemoveKnown
//
this.btnRemoveKnown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.btnRemoveKnown.Location = new System.Drawing.Point(270, 51);
this.btnRemoveKnown.Name = "btnRemoveKnown";
this.btnRemoveKnown.Size = new System.Drawing.Size(75, 25);
this.btnRemoveKnown.TabIndex = 3;
this.btnRemoveKnown.Text = "Remove";
this.btnRemoveKnown.Click += new System.EventHandler(this.btnRemoveKnown_Click);
//
// bthAddKnown
//
this.bthAddKnown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.bthAddKnown.Location = new System.Drawing.Point(270, 20);
this.bthAddKnown.Name = "bthAddKnown";
this.bthAddKnown.Size = new System.Drawing.Size(75, 25);
this.bthAddKnown.TabIndex = 2;
this.bthAddKnown.Text = "Add";
this.bthAddKnown.Click += new System.EventHandler(this.bthAddKnown_Click);
//
// cbIgnoreChainValidationErrors
//
this.cbIgnoreChainValidationErrors.AutoSize = true;
this.cbIgnoreChainValidationErrors.Location = new System.Drawing.Point(10, 29);
this.cbIgnoreChainValidationErrors.Name = "cbIgnoreChainValidationErrors";
this.cbIgnoreChainValidationErrors.Size = new System.Drawing.Size(162, 17);
this.cbIgnoreChainValidationErrors.TabIndex = 41;
this.cbIgnoreChainValidationErrors.Text = "Ignore chain validation errors";
//
// cbForceCompleteChainValidation
//
this.cbForceCompleteChainValidation.AutoSize = true;
this.cbForceCompleteChainValidation.Location = new System.Drawing.Point(208, 29);
this.cbForceCompleteChainValidation.Name = "cbForceCompleteChainValidation";
this.cbForceCompleteChainValidation.Size = new System.Drawing.Size(176, 17);
this.cbForceCompleteChainValidation.TabIndex = 43;
this.cbForceCompleteChainValidation.Text = "Force complete chain validation";
//
// openDialogCert
//
this.openDialogCert.Filter = "PKCS#12 files (*.pfx)|*.pfx|All files (*.*)|*.*";
this.openDialogCert.InitialDirectory = ".";
this.openDialogCert.Title = "Please select a certificate to decrypt the document";
//
// cbOfflineMode
//
this.cbOfflineMode.AutoSize = true;
this.cbOfflineMode.Location = new System.Drawing.Point(584, 29);
this.cbOfflineMode.Name = "cbOfflineMode";
this.cbOfflineMode.Size = new System.Drawing.Size(86, 17);
this.cbOfflineMode.TabIndex = 50;
this.cbOfflineMode.Text = "Offline Mode";
//
// frmMain
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(734, 331);
this.Controls.Add(this.groupBox4);
this.Controls.Add(this.label5);
this.Controls.Add(this.cbDetached);
this.Controls.Add(this.edDataFile);
this.Controls.Add(this.sbBrowseDataFile);
this.Controls.Add(this.lDataFile);
this.Controls.Add(this.edInputFile);
this.Controls.Add(this.btnVerify);
this.Controls.Add(this.sbBrowseInputFile);
this.Controls.Add(this.lbInputFile);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "frmMain";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "CAdES Verifier Demo";
this.groupBox4.ResumeLayout(false);
this.groupBox4.PerformLayout();
this.groupBox3.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Application.Run(new frmMain());
}
private void sbBrowseXMLFile_Click(object sender, System.EventArgs e)
{
dlgOpen.InitialDirectory = Application.StartupPath;
dlgOpen.FileName = edInputFile.Text;
if (dlgOpen.ShowDialog() == DialogResult.OK)
edInputFile.Text = dlgOpen.FileName;
}
private void btnVerify_Click(object sender, EventArgs e)
{
try
{
verifier.InputFile = edInputFile.Text;
if (cbPerformRevocationCheck.Checked)
{
verifier.RevocationCheck = CAdESVerifierRevocationChecks.crcAuto;
}
else
{
verifier.RevocationCheck = CAdESVerifierRevocationChecks.crcNone;
}
verifier.IgnoreChainValidationErrors = cbIgnoreChainValidationErrors.Checked;
if (cbForceCompleteChainValidation.Checked)
verifier.Config("ForceCompleteChainValidation=true");
else
verifier.Config("ForceCompleteChainValidation=false");
verifier.OfflineMode = cbOfflineMode.Checked;
verifier.Detached = cbDetached.Checked;
if (cbDetached.Checked)
{
verifier.DataFile = edDataFile.Text;
}
else
{
verifier.OutputFile = edDataFile.Text;
}
verifier.Verify();
switch (verifier.Signatures[0].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;
}
default:
{
MessageBox.Show("Signature validated successfully", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
break;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void cbDetached_CheckedChanged(object sender, EventArgs e)
{
if (cbDetached.Checked)
{
lDataFile.Text = "Data file:";
}
else
{
lDataFile.Text = "Output file:";
}
}
private void sbBrowseDataFile_Click(object sender, EventArgs e)
{
if (cbDetached.Checked)
{
dlgOpen.FileName = edDataFile.Text;
if (dlgOpen.ShowDialog() == DialogResult.OK)
edDataFile.Text = dlgOpen.FileName;
}
else
{
dlgSave.FileName = edDataFile.Text;
if (dlgSave.ShowDialog() == DialogResult.OK)
edDataFile.Text = dlgSave.FileName;
}
}
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 UpdateKnownCertificates()
{
lvKnownCertificates.BeginUpdate();
lvKnownCertificates.Items.Clear();
for (int i = 0; i < verifier.KnownCertificates.Count; i++)
{
string s = verifier.KnownCertificates[i].Issuer;
if (s == "")
s = "";
ListViewItem Item = lvKnownCertificates.Items.Add(BitConverter.ToString(verifier.KnownCertificates[i].SerialNumber));
Item.SubItems.Add(s);
}
lvKnownCertificates.EndUpdate();
}
public void UpdateTrustedCertificates()
{
lvTrustedCertificates.BeginUpdate();
lvTrustedCertificates.Items.Clear();
for (int i = 0; i < verifier.TrustedCertificates.Count; i++)
{
string s = verifier.TrustedCertificates[i].Issuer;
if (s == "")
s = "";
ListViewItem Item = lvTrustedCertificates.Items.Add(BitConverter.ToString(verifier.TrustedCertificates[i].SerialNumber));
Item.SubItems.Add(s);
}
lvTrustedCertificates.EndUpdate();
}
private void bthAddKnown_Click(object sender, EventArgs e)
{
openDialogCert.Title = "Select certificate file";
openDialogCert.Filter = "PEM-encoded certificate (*.pem)|*.PEM|DER-encoded certificate (*.cer)|*.CER|PFX-encoded certificate (*.pfx)|*.PFX";
if (openDialogCert.ShowDialog() == DialogResult.OK)
{
Certificate cert = LoadCertificate(openDialogCert.FileName, RequestPassword());
verifier.KnownCertificates.Add(cert);
UpdateKnownCertificates();
}
}
private void btnRemoveKnown_Click(object sender, EventArgs e)
{
if (lvKnownCertificates.SelectedItems.Count > 0)
{
verifier.KnownCertificates.RemoveAt(lvKnownCertificates.SelectedItems[0].Index);
UpdateKnownCertificates();
}
}
private void btnAddTrusted_Click(object sender, EventArgs e)
{
openDialogCert.Title = "Select certificate file";
openDialogCert.Filter = "PEM-encoded certificate (*.pem)|*.PEM|DER-encoded certificate (*.cer)|*.CER|PFX-encoded certificate (*.pfx)|*.PFX";
if (openDialogCert.ShowDialog() == DialogResult.OK)
{
Certificate cert = LoadCertificate(openDialogCert.FileName, RequestPassword());
verifier.TrustedCertificates.Add(cert);
UpdateTrustedCertificates();
}
}
private void btnRemoveTrusted_Click(object sender, EventArgs e)
{
if (lvTrustedCertificates.SelectedItems.Count > 0)
{
verifier.TrustedCertificates.RemoveAt(lvTrustedCertificates.SelectedItems[0].Index);
UpdateTrustedCertificates();
}
}
}
}