/* * 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; /// /// Summary description for frmMain. /// public class frmMain : System.Windows.Forms.Form { private System.Windows.Forms.OpenFileDialog dlgOpen; /// /// Required designer variable. /// private System.ComponentModel.Container components = null; private SignForm frmSign = null; private ReferencesForm frmReferences; private XAdESForm frmXAdES; private XAdESVerifier verifier; 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 label1; private Label label2; private List ReferenceResult = new List(); public frmMain() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // verifier = new XAdESVerifier(); verifier.OnReferenceValidated += new XAdESVerifier.OnReferenceValidatedHandler(ReferenceValidated); verifier.OnSignatureFound += new XAdESVerifier.OnSignatureFoundHandler(SignatureFound); verifier.OnTimestampValidated += new XAdESVerifier.OnTimestampValidatedHandler(TimestampValidated); frmSign = new SignForm(verifier); frmReferences = new ReferencesForm(); frmXAdES = new XAdESForm(); } /// /// Clean up any resources being used. /// protected override void Dispose(bool disposing) { if (disposing) { if (components != null) { components.Dispose(); frmSign.Dispose(); frmReferences.Dispose(); frmXAdES.Dispose(); verifier.Dispose(); } } base.Dispose(disposing); } private void ReferenceValidated(object s, XAdESVerifierReferenceValidatedEventArgs e) { ReferenceResult.Add(new ReferenceRes() { Id = e.ID, Uri = e.URI, RefType = e.RefType, DigestValid = e.DigestValid }); } private void SignatureFound(object s, XAdESVerifierSignatureFoundEventArgs e) { if (e.CertFound) { e.ValidateSignature = true; e.ValidateChain = true; } else { switch (verifier.Signatures[e.Index].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 = verifier.Signatures[e.Index].HashAlgorithm; frmSign.edKeyName.Text = verifier.Config("KeyName"); frmSign.UpdateCertificates(); if (frmSign.ShowDialog() == DialogResult.OK) { e.ValidateSignature = true; e.ValidateChain = true; } else { e.ValidateSignature = false; e.ValidateChain = false; } } } private void TimestampValidated(object s, XAdESVerifierTimestampValidatedEventArgs e) { if (e.ValidationResult == 0)//cvtValid { frmXAdES.lbTimestamp.Text = "Timestamp: " + e.Time; frmXAdES.lbTimestampSerial.Text = "Timestamp Serial: " + BitConverter.ToString(e.SerialNumber); } else { frmXAdES.lbTimestamp.Text = "Timestamp signature is not valid"; frmXAdES.lbTimestampSerial.Text = ""; } } #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.dlgOpen = new System.Windows.Forms.OpenFileDialog(); 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.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.SuspendLayout(); // // dlgOpen // this.dlgOpen.Filter = "XML file (*.xml)|*.XML"; // // cbDetached // this.cbDetached.AutoSize = true; this.cbDetached.Location = new System.Drawing.Point(8, 79); this.cbDetached.Name = "cbDetached"; this.cbDetached.Size = new System.Drawing.Size(73, 17); this.cbDetached.TabIndex = 20; 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(61, 101); this.edDataFile.Name = "edDataFile"; this.edDataFile.Size = new System.Drawing.Size(239, 20); this.edDataFile.TabIndex = 18; // // sbBrowseDataFile // this.sbBrowseDataFile.Enabled = false; this.sbBrowseDataFile.Location = new System.Drawing.Point(304, 98); this.sbBrowseDataFile.Name = "sbBrowseDataFile"; this.sbBrowseDataFile.Size = new System.Drawing.Size(75, 25); this.sbBrowseDataFile.TabIndex = 19; 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(6, 104); this.lDataFile.Name = "lDataFile"; this.lDataFile.Size = new System.Drawing.Size(49, 13); this.lDataFile.TabIndex = 17; this.lDataFile.Text = "Data file:"; // // edXMLFile // this.edXMLFile.Location = new System.Drawing.Point(61, 49); this.edXMLFile.Name = "edXMLFile"; this.edXMLFile.Size = new System.Drawing.Size(239, 20); this.edXMLFile.TabIndex = 14; // // btnVerify // this.btnVerify.Location = new System.Drawing.Point(304, 145); this.btnVerify.Name = "btnVerify"; this.btnVerify.Size = new System.Drawing.Size(75, 25); this.btnVerify.TabIndex = 16; this.btnVerify.Text = "Verify"; this.btnVerify.Click += new System.EventHandler(this.btnVerify_Click); // // sbBrowseXMLFile // this.sbBrowseXMLFile.Location = new System.Drawing.Point(304, 46); this.sbBrowseXMLFile.Name = "sbBrowseXMLFile"; this.sbBrowseXMLFile.Size = new System.Drawing.Size(75, 25); this.sbBrowseXMLFile.TabIndex = 15; 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(5, 52); this.lbXMLFile.Name = "lbXMLFile"; this.lbXMLFile.Size = new System.Drawing.Size(50, 13); this.lbXMLFile.TabIndex = 13; this.lbXMLFile.Text = "Input file:"; // // label1 // this.label1.AutoSize = true; this.label1.ForeColor = System.Drawing.SystemColors.Highlight; this.label1.Location = new System.Drawing.Point(5, 5); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(253, 13); this.label1.TabIndex = 21; this.label1.Text = "This sample shows processing of XAdES signatures."; // // label2 // this.label2.AutoSize = true; this.label2.ForeColor = System.Drawing.SystemColors.Highlight; this.label2.Location = new System.Drawing.Point(6, 23); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(294, 13); this.label2.TabIndex = 22; this.label2.Text = "Please select a signed XML file and click \'Verify\' when ready."; // // frmMain // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(387, 178); 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 = "frmMain"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "XAdES Verifier Demo"; 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 = edXMLFile.Text; if (dlgOpen.ShowDialog() == DialogResult.OK) edXMLFile.Text = dlgOpen.FileName; } private void btnVerify_Click(object sender, EventArgs e) { try { ReferenceResult.Clear(); frmXAdES.lbTimestamp.Text = ""; frmXAdES.lbTimestampSerial.Text = ""; verifier.KnownCertificates.Clear(); verifier.InputFile = edXMLFile.Text; if (cbDetached.Checked) { verifier.DataFile = edDataFile.Text; verifier.DataURI = System.IO.Path.GetFileName(edDataFile.Text); verifier.DataType = XAdESVerifierDataTypes.cxdtBinary; verifier.VerifyDetached(); } else verifier.Verify(); if (verifier.Signatures.Count == 0) MessageBox.Show("No signatures found", "", MessageBoxButtons.OK, MessageBoxIcon.Error); else { for (int i = 0; i < verifier.Signatures.Count; i++) { XAdESSignature 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 is invalid (reference corrupted)\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: { Boolean SigOK = true; if (sig.ChainValidationResult == ChainValidities.cvtValidButUntrusted) MessageBox.Show("The selected signature is signed by self-signed certificate which was not previously trusted", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); else if (sig.ChainValidationResult != ChainValidities.cvtValid) { //SigOK = false; if (sig.ChainValidationResult == ChainValidities.cvtCantBeEstablished) MessageBox.Show("Signing certificate chain could not be validated completely.", "", MessageBoxButtons.OK, MessageBoxIcon.Error); else MessageBox.Show("Signing certificate is not valid.", "", MessageBoxButtons.OK, MessageBoxIcon.Error); } if (SigOK) { 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(); } if (sig.Level != AdESSignatureLevels.aslGeneric) { if (MessageBox.Show("Do you want to view XAdES information?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) { switch (sig.XAdESVersion) { case XAdESVersions.xav111: { frmXAdES.cmbVersion.SelectedIndex = 0; break; } case XAdESVersions.xav122: { frmXAdES.cmbVersion.SelectedIndex = 1; break; } case XAdESVersions.xav132: { frmXAdES.cmbVersion.SelectedIndex = 2; break; } case XAdESVersions.xav141: { frmXAdES.cmbVersion.SelectedIndex = 3; break; } default: { frmXAdES.cmbVersion.SelectedIndex = -1; break; } } switch (sig.Level) { case AdESSignatureLevels.aslGeneric: { frmXAdES.cmbForm.SelectedIndex = 0; break; } case AdESSignatureLevels.aslBaselineB: { frmXAdES.cmbForm.SelectedIndex = 1; break; } case AdESSignatureLevels.aslBaselineT: { frmXAdES.cmbForm.SelectedIndex = 2; break; } case AdESSignatureLevels.aslBaselineLT: { frmXAdES.cmbForm.SelectedIndex = 3; break; } case AdESSignatureLevels.aslBaselineLTA: { frmXAdES.cmbForm.SelectedIndex = 4; break; } case AdESSignatureLevels.aslBES: { frmXAdES.cmbForm.SelectedIndex = 5; break; } case AdESSignatureLevels.aslEPES: { frmXAdES.cmbForm.SelectedIndex = 6; break; } case AdESSignatureLevels.aslT: { frmXAdES.cmbForm.SelectedIndex = 7; break; } case AdESSignatureLevels.aslC: { frmXAdES.cmbForm.SelectedIndex = 8; break; } case AdESSignatureLevels.aslX: { frmXAdES.cmbForm.SelectedIndex = 9; break; } case AdESSignatureLevels.aslXL: { frmXAdES.cmbForm.SelectedIndex = 10; break; } case AdESSignatureLevels.aslA: { frmXAdES.cmbForm.SelectedIndex = 11; break; } case AdESSignatureLevels.aslExtendedBES: { frmXAdES.cmbForm.SelectedIndex = 12; break; } case AdESSignatureLevels.aslExtendedEPES: { frmXAdES.cmbForm.SelectedIndex = 13; break; } case AdESSignatureLevels.aslExtendedT: { frmXAdES.cmbForm.SelectedIndex = 14; break; } case AdESSignatureLevels.aslExtendedC: { frmXAdES.cmbForm.SelectedIndex = 15; break; } case AdESSignatureLevels.aslExtendedX: { frmXAdES.cmbForm.SelectedIndex = 16; break; } case AdESSignatureLevels.aslExtendedXLong: { frmXAdES.cmbForm.SelectedIndex = 17; break; } case AdESSignatureLevels.aslExtendedXL: { frmXAdES.cmbForm.SelectedIndex = 18; break; } case AdESSignatureLevels.aslExtendedA: { frmXAdES.cmbForm.SelectedIndex = 19; break; } default: { frmXAdES.cmbForm.SelectedIndex = -1; break; } } if (sig.ValidatedSigningTime != "") frmXAdES.lbSignedTime.Text = "Validated Signing Time: " + sig.ValidatedSigningTime + " UTC"; else if (sig.ClaimedSigningTime != "") frmXAdES.lbSignedTime.Text = "Claimed Signing Time: " + sig.ClaimedSigningTime + " UTC"; else frmXAdES.lbSignedTime.Text = ""; frmXAdES.ShowDialog(); } } } break; } default: MessageBox.Show("Signature is invalid (unknown 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; } }