/* * 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 PDFVerifierDemo { /// /// Summary description for Form1. /// public class frmMain : System.Windows.Forms.Form { private System.Windows.Forms.Label lSelectSource; private System.Windows.Forms.TextBox edPDFFile; private System.Windows.Forms.Button btnBrowse; private System.Windows.Forms.Button btnVerify; private System.Windows.Forms.OpenFileDialog openDialogPDF; private System.Windows.Forms.OpenFileDialog openDialogCert; /// /// Required designer variable. /// private System.ComponentModel.Container components = null; private PDFVerifier verifier; private GroupBox groupBox1; private GroupBox groupBox3; public ListView lvTrustedCertificates; private ColumnHeader columnHeader3; private ColumnHeader columnHeader4; private Button btnRemoveTrusted; private Button btnAddTrusted; private GroupBox groupBox2; public ListView lvKnownCertificates; private ColumnHeader columnHeader1; private ColumnHeader columnHeader2; private Button btnRemoveKnown; private Label label1; public CheckBox cbOfflineMode; public CheckBox cbPerformRevocationCheck; public CheckBox cbIgnoreChainValidationErrors; public CheckBox cbForceCompleteChainValidation; private Button bthAddKnown; public frmMain() { // // Required for Windows Form Designer support // InitializeComponent(); verifier = new PDFVerifier(); verifier.OnSignatureFound += new PDFVerifier.OnSignatureFoundHandler(SignatureFound); } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); verifier.Dispose(); } } base.Dispose( disposing ); } private void SignatureFound(object s, PDFVerifierSignatureFoundEventArgs e) { if (e.CertFound) { e.ValidateSignature = true; e.ValidateChain = true; } else { SignForm dlg = new SignForm(verifier); dlg.edSignatureName.Text = verifier.Signatures[e.Index].SignatureName; 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.lSelectSource = new System.Windows.Forms.Label(); this.edPDFFile = new System.Windows.Forms.TextBox(); this.btnBrowse = new System.Windows.Forms.Button(); this.btnVerify = new System.Windows.Forms.Button(); this.openDialogPDF = new System.Windows.Forms.OpenFileDialog(); this.openDialogCert = new System.Windows.Forms.OpenFileDialog(); this.groupBox1 = 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.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.label1 = new System.Windows.Forms.Label(); this.cbOfflineMode = new System.Windows.Forms.CheckBox(); this.cbPerformRevocationCheck = new System.Windows.Forms.CheckBox(); this.cbIgnoreChainValidationErrors = new System.Windows.Forms.CheckBox(); this.cbForceCompleteChainValidation = new System.Windows.Forms.CheckBox(); this.groupBox1.SuspendLayout(); this.groupBox3.SuspendLayout(); this.groupBox2.SuspendLayout(); this.SuspendLayout(); // // lSelectSource // this.lSelectSource.AutoSize = true; this.lSelectSource.Location = new System.Drawing.Point(5, 32); this.lSelectSource.Name = "lSelectSource"; this.lSelectSource.Size = new System.Drawing.Size(164, 13); this.lSelectSource.TabIndex = 0; this.lSelectSource.Text = "Please select the PDF document:"; // // edPDFFile // this.edPDFFile.Location = new System.Drawing.Point(8, 48); this.edPDFFile.Name = "edPDFFile"; this.edPDFFile.Size = new System.Drawing.Size(373, 21); this.edPDFFile.TabIndex = 1; // // btnBrowse // this.btnBrowse.Location = new System.Drawing.Point(387, 45); this.btnBrowse.Name = "btnBrowse"; this.btnBrowse.Size = new System.Drawing.Size(75, 25); this.btnBrowse.TabIndex = 2; this.btnBrowse.Text = "Browse..."; this.btnBrowse.Click += new System.EventHandler(this.btnBrowse_Click); // // btnVerify // this.btnVerify.Location = new System.Drawing.Point(560, 250); this.btnVerify.Name = "btnVerify"; this.btnVerify.Size = new System.Drawing.Size(75, 25); this.btnVerify.TabIndex = 3; this.btnVerify.Text = "Verify"; this.btnVerify.Click += new System.EventHandler(this.btnVerify_Click); // // openDialogPDF // this.openDialogPDF.Filter = "PDF documents (*.pdf)|*.pdf|All files (*.*)|*.*"; this.openDialogPDF.InitialDirectory = "."; // // 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"; // // groupBox1 // this.groupBox1.Controls.Add(this.cbOfflineMode); this.groupBox1.Controls.Add(this.cbPerformRevocationCheck); this.groupBox1.Controls.Add(this.cbIgnoreChainValidationErrors); this.groupBox1.Controls.Add(this.cbForceCompleteChainValidation); this.groupBox1.Controls.Add(this.groupBox3); this.groupBox1.Controls.Add(this.groupBox2); this.groupBox1.Location = new System.Drawing.Point(5, 80); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(630, 160); this.groupBox1.TabIndex = 39; this.groupBox1.TabStop = false; this.groupBox1.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(325, 55); this.groupBox3.Name = "groupBox3"; this.groupBox3.Size = new System.Drawing.Size(300, 100); 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(210, 75); this.lvTrustedCertificates.TabIndex = 4; this.lvTrustedCertificates.UseCompatibleStateImageBehavior = false; this.lvTrustedCertificates.View = System.Windows.Forms.View.Details; // // columnHeader3 // this.columnHeader3.Text = "Serial"; this.columnHeader3.Width = 85; // // columnHeader4 // this.columnHeader4.Text = "Issuer"; this.columnHeader4.Width = 120; // // 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(220, 50); 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(220, 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); // // 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, 55); this.groupBox2.Name = "groupBox2"; this.groupBox2.Size = new System.Drawing.Size(300, 100); 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(210, 75); this.lvKnownCertificates.TabIndex = 4; this.lvKnownCertificates.UseCompatibleStateImageBehavior = false; this.lvKnownCertificates.View = System.Windows.Forms.View.Details; // // columnHeader1 // this.columnHeader1.Text = "Serial"; this.columnHeader1.Width = 85; // // columnHeader2 // this.columnHeader2.Text = "Issuer"; this.columnHeader2.Width = 120; // // 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(220, 50); 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(220, 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); // // 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(423, 13); this.label1.TabIndex = 44; this.label1.Text = "This sample illustrates the use of PDFVerifier component for validating PDF signa" + "tures. "; // // cbOfflineMode // this.cbOfflineMode.AutoSize = true; this.cbOfflineMode.Location = new System.Drawing.Point(538, 27); this.cbOfflineMode.Name = "cbOfflineMode"; this.cbOfflineMode.Size = new System.Drawing.Size(87, 17); this.cbOfflineMode.TabIndex = 54; this.cbOfflineMode.Text = "Offline Mode"; // // cbPerformRevocationCheck // this.cbPerformRevocationCheck.AutoSize = true; this.cbPerformRevocationCheck.Location = new System.Drawing.Point(382, 27); this.cbPerformRevocationCheck.Name = "cbPerformRevocationCheck"; this.cbPerformRevocationCheck.Size = new System.Drawing.Size(148, 17); this.cbPerformRevocationCheck.TabIndex = 51; this.cbPerformRevocationCheck.Text = "Perform revocation check"; // // cbIgnoreChainValidationErrors // this.cbIgnoreChainValidationErrors.AutoSize = true; this.cbIgnoreChainValidationErrors.Location = new System.Drawing.Point(8, 27); this.cbIgnoreChainValidationErrors.Name = "cbIgnoreChainValidationErrors"; this.cbIgnoreChainValidationErrors.Size = new System.Drawing.Size(167, 17); this.cbIgnoreChainValidationErrors.TabIndex = 52; this.cbIgnoreChainValidationErrors.Text = "Ignore chain validation errors"; // // cbForceCompleteChainValidation // this.cbForceCompleteChainValidation.AutoSize = true; this.cbForceCompleteChainValidation.Location = new System.Drawing.Point(191, 27); this.cbForceCompleteChainValidation.Name = "cbForceCompleteChainValidation"; this.cbForceCompleteChainValidation.Size = new System.Drawing.Size(176, 17); this.cbForceCompleteChainValidation.TabIndex = 53; this.cbForceCompleteChainValidation.Text = "Force complete chain validation"; // // frmMain // this.AutoScaleBaseSize = new System.Drawing.Size(5, 14); this.ClientSize = new System.Drawing.Size(639, 281); this.Controls.Add(this.label1); this.Controls.Add(this.groupBox1); this.Controls.Add(this.btnVerify); this.Controls.Add(this.btnBrowse); this.Controls.Add(this.edPDFFile); this.Controls.Add(this.lSelectSource); this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204))); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog; this.MaximizeBox = false; this.Name = "frmMain"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "PDF Verifier demo"; this.groupBox1.ResumeLayout(false); this.groupBox1.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 btnBrowse_Click(object sender, System.EventArgs e) { if (openDialogPDF.ShowDialog() == DialogResult.OK) edPDFFile.Text = openDialogPDF.FileName; } private void btnVerify_Click(object sender, System.EventArgs e) { try { verifier.InputFile = edPDFFile.Text; if (cbPerformRevocationCheck.Checked) { verifier.RevocationCheck = PDFVerifierRevocationChecks.crcAuto; } else { verifier.RevocationCheck = PDFVerifierRevocationChecks.crcNone; } verifier.IgnoreChainValidationErrors = cbIgnoreChainValidationErrors.Checked; verifier.OfflineMode = cbOfflineMode.Checked; if (cbForceCompleteChainValidation.Checked) verifier.Config("ForceCompleteChainValidation=true"); else verifier.Config("ForceCompleteChainValidation=False"); verifier.Verify(); DisplaySignaturesInfo(); } catch(Exception ex) { MessageBox.Show("Error: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void DisplaySignaturesInfo() { ValidationResultForm dlg = new ValidationResultForm(); dlg.Init(verifier); dlg.ShowDialog(); } 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(); } } } public class ValidationRes { public string SignatureName { get; set; } public string AuthorName { get; set; } public string Reason { get; set; } public string SigningTime { get; set; } public int Result { get; set; } } public class CertValidationRes { public string SignatureName { get; set; } public string SerialNumber { get; set; } public string IssuerRDN { get; set; } public string SubjectKeyID { get; set; } public int Result { get; set; } public int Reason { get; set; } } }