securebb-win-demos/PKCS11 Certificate Storage (WinForms)/pdfprocessor.cs

262 lines
9.2 KiB
C#
Raw Permalink Normal View History

2024-08-08 13:09:34 +06:00
using System;
using System.Windows.Forms;
using System.IO;
using nsoftware.SecureBlackbox;
namespace PKCS11CertificateStorage
{
/// <summary>
/// Summary description for ProcessorForm.
/// </summary>
public class frmProcessor : System.Windows.Forms.Form
{
private System.Windows.Forms.Label lbInputFile;
private System.Windows.Forms.TextBox edInputFile;
private System.Windows.Forms.Button btnSelectInput;
private System.Windows.Forms.Button btnSelectOutput;
private System.Windows.Forms.TextBox edOutputFile;
private System.Windows.Forms.Label lbOutputFile;
private System.Windows.Forms.Button btnSign;
private System.Windows.Forms.Button btnVerify;
private System.Windows.Forms.OpenFileDialog dlgOpen;
private System.Windows.Forms.SaveFileDialog dlgSave;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Certificate Cert;
private PDFSigner signer;
private Button btnClose;
private PDFVerifier verifier;
public frmProcessor()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
signer = new PDFSigner();
verifier = new PDFVerifier();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
signer.Dispose();
verifier.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.lbInputFile = new System.Windows.Forms.Label();
this.edInputFile = new System.Windows.Forms.TextBox();
this.btnSelectInput = new System.Windows.Forms.Button();
this.btnSelectOutput = new System.Windows.Forms.Button();
this.edOutputFile = new System.Windows.Forms.TextBox();
this.lbOutputFile = new System.Windows.Forms.Label();
this.btnSign = new System.Windows.Forms.Button();
this.btnVerify = new System.Windows.Forms.Button();
this.dlgOpen = new System.Windows.Forms.OpenFileDialog();
this.dlgSave = new System.Windows.Forms.SaveFileDialog();
this.btnClose = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// lbInputFile
//
this.lbInputFile.Location = new System.Drawing.Point(16, 16);
this.lbInputFile.Name = "lbInputFile";
this.lbInputFile.Size = new System.Drawing.Size(100, 16);
this.lbInputFile.TabIndex = 0;
this.lbInputFile.Text = "Input File:";
//
// edInputFile
//
this.edInputFile.Location = new System.Drawing.Point(16, 32);
this.edInputFile.Name = "edInputFile";
this.edInputFile.Size = new System.Drawing.Size(261, 20);
this.edInputFile.TabIndex = 1;
//
// btnSelectInput
//
this.btnSelectInput.Location = new System.Drawing.Point(283, 30);
this.btnSelectInput.Name = "btnSelectInput";
this.btnSelectInput.Size = new System.Drawing.Size(75, 23);
this.btnSelectInput.TabIndex = 2;
this.btnSelectInput.Text = "Browse...";
this.btnSelectInput.Click += new System.EventHandler(this.btnSelectInput_Click);
//
// btnSelectOutput
//
this.btnSelectOutput.Location = new System.Drawing.Point(283, 78);
this.btnSelectOutput.Name = "btnSelectOutput";
this.btnSelectOutput.Size = new System.Drawing.Size(75, 23);
this.btnSelectOutput.TabIndex = 5;
this.btnSelectOutput.Text = "Browse...";
this.btnSelectOutput.Click += new System.EventHandler(this.btnSelectOutput_Click);
//
// edOutputFile
//
this.edOutputFile.Location = new System.Drawing.Point(16, 80);
this.edOutputFile.Name = "edOutputFile";
this.edOutputFile.Size = new System.Drawing.Size(261, 20);
this.edOutputFile.TabIndex = 4;
//
// lbOutputFile
//
this.lbOutputFile.Location = new System.Drawing.Point(16, 64);
this.lbOutputFile.Name = "lbOutputFile";
this.lbOutputFile.Size = new System.Drawing.Size(100, 16);
this.lbOutputFile.TabIndex = 3;
this.lbOutputFile.Text = "Output File:";
//
// btnSign
//
this.btnSign.Location = new System.Drawing.Point(202, 125);
this.btnSign.Name = "btnSign";
this.btnSign.Size = new System.Drawing.Size(75, 23);
this.btnSign.TabIndex = 6;
this.btnSign.Text = "Sign";
this.btnSign.Click += new System.EventHandler(this.btnSign_Click);
//
// btnVerify
//
this.btnVerify.Location = new System.Drawing.Point(283, 125);
this.btnVerify.Name = "btnVerify";
this.btnVerify.Size = new System.Drawing.Size(75, 23);
this.btnVerify.TabIndex = 7;
this.btnVerify.Text = "Verify";
this.btnVerify.Click += new System.EventHandler(this.btnVerify_Click);
//
// dlgOpen
//
this.dlgOpen.Filter = "All files (*.*)|*.*";
//
// dlgSave
//
this.dlgSave.DefaultExt = "All files (*.*)|*.*";
//
// btnClose
//
this.btnClose.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.btnClose.Location = new System.Drawing.Point(283, 166);
this.btnClose.Name = "btnClose";
this.btnClose.Size = new System.Drawing.Size(75, 23);
this.btnClose.TabIndex = 10;
this.btnClose.Text = "Close";
//
// frmProcessor
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(375, 201);
this.Controls.Add(this.btnClose);
this.Controls.Add(this.btnVerify);
this.Controls.Add(this.btnSign);
this.Controls.Add(this.btnSelectOutput);
this.Controls.Add(this.edOutputFile);
this.Controls.Add(this.lbOutputFile);
this.Controls.Add(this.btnSelectInput);
this.Controls.Add(this.edInputFile);
this.Controls.Add(this.lbInputFile);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "frmProcessor";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "File processor";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private void btnSelectInput_Click(object sender, System.EventArgs e)
{
if (dlgOpen.ShowDialog() == DialogResult.OK)
edInputFile.Text = dlgOpen.FileName;
}
private void btnSelectOutput_Click(object sender, System.EventArgs e)
{
if (dlgSave.ShowDialog() == DialogResult.OK)
edOutputFile.Text = dlgSave.FileName;
}
private void btnSign_Click(object sender, System.EventArgs e)
{
signer.InputFile = edInputFile.Text;
signer.OutputFile = edOutputFile.Text;
signer.SigningCertificate = Cert;
if (signer.SigningCertificate.KeyAlgorithm.Contains("id-dsa"))
{
MessageBox.Show("The certificate was found to contain a DSA key. The hash algorithm has been switched to SHA1.");
signer.NewSignature.HashAlgorithm = "SHA1";
}
signer.NewSignature.Level = PAdESSignatureLevels.paslBES;
signer.Widget.Invisible = false;
signer.IgnoreChainValidationErrors = true;
try
{
signer.Sign();
MessageBox.Show("PDF file successfully signed");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
signer.SigningCertificate = null;
}
private void btnVerify_Click(object sender, System.EventArgs e)
{
try
{
verifier.InputFile = edInputFile.Text;
verifier.IgnoreChainValidationErrors = true;
verifier.KnownCertificates.Clear();
verifier.KnownCertificates.Add(Cert);
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();
}
}
}