securebb-win-demos/Simple PDF Signer (WinForms)/simplepdfsigner.cs

572 lines
23 KiB
C#
Raw Normal View History

2024-08-08 13:09:34 +06:00
/*
* 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 PDFSignerDemo
{
/// <summary>
/// Summary description for frmMain.
/// </summary>
public class MainForm : 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;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.Button btnSign;
private TextBox edOutputFile;
private Button sbBrowseOutputFile;
private Label label2;
private SaveFileDialog dlgSave;
public TextBox edTimestampServer;
public CheckBox cbTimestamp;
private ComboBox cbReason;
private Label lReason;
private TextBox edAuthor;
private Label lAuthor;
private Button btnBrowseCert;
private TextBox edCertPassword;
private TextBox edCertFile;
private Label lCertPassword;
private Label label6;
private Label label7;
private GroupBox groupBox5;
private GroupBox groupBox7;
private RadioButton rbWin32;
private TextBox edWin32Store;
private RadioButton rbPKCS11;
private Button btnBrowsePKCS11;
private TextBox edPKCS11PIN;
private Label label1;
private TextBox edPKCS11File;
private RadioButton rbCertFile;
private PDFSigner signer;
public MainForm()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
signer = new PDFSigner();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
signer.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.sbBrowseInputFile = new System.Windows.Forms.Button();
this.btnSign = new System.Windows.Forms.Button();
this.dlgOpen = new System.Windows.Forms.OpenFileDialog();
this.edOutputFile = new System.Windows.Forms.TextBox();
this.sbBrowseOutputFile = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.dlgSave = new System.Windows.Forms.SaveFileDialog();
this.edTimestampServer = new System.Windows.Forms.TextBox();
this.cbTimestamp = new System.Windows.Forms.CheckBox();
this.cbReason = new System.Windows.Forms.ComboBox();
this.lReason = new System.Windows.Forms.Label();
this.edAuthor = new System.Windows.Forms.TextBox();
this.lAuthor = new System.Windows.Forms.Label();
this.btnBrowseCert = new System.Windows.Forms.Button();
this.edCertPassword = new System.Windows.Forms.TextBox();
this.edCertFile = new System.Windows.Forms.TextBox();
this.lCertPassword = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.label7 = new System.Windows.Forms.Label();
this.groupBox5 = new System.Windows.Forms.GroupBox();
this.rbWin32 = new System.Windows.Forms.RadioButton();
this.edWin32Store = new System.Windows.Forms.TextBox();
this.rbPKCS11 = new System.Windows.Forms.RadioButton();
this.btnBrowsePKCS11 = new System.Windows.Forms.Button();
this.edPKCS11PIN = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.edPKCS11File = new System.Windows.Forms.TextBox();
this.rbCertFile = new System.Windows.Forms.RadioButton();
this.groupBox7 = new System.Windows.Forms.GroupBox();
this.groupBox5.SuspendLayout();
this.groupBox7.SuspendLayout();
this.SuspendLayout();
//
// lbInputFile
//
this.lbInputFile.Location = new System.Drawing.Point(5, 33);
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(70, 30);
this.edInputFile.Name = "edInputFile";
this.edInputFile.Size = new System.Drawing.Size(334, 20);
this.edInputFile.TabIndex = 1;
//
// sbBrowseInputFile
//
this.sbBrowseInputFile.Location = new System.Drawing.Point(410, 27);
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.sbBrowseInputFile_Click);
//
// btnSign
//
this.btnSign.Location = new System.Drawing.Point(620, 438);
this.btnSign.Name = "btnSign";
this.btnSign.Size = new System.Drawing.Size(75, 25);
this.btnSign.TabIndex = 8;
this.btnSign.Text = "Sign";
this.btnSign.Click += new System.EventHandler(this.btnSign_Click);
//
// edOutputFile
//
this.edOutputFile.Location = new System.Drawing.Point(70, 60);
this.edOutputFile.Name = "edOutputFile";
this.edOutputFile.Size = new System.Drawing.Size(334, 20);
this.edOutputFile.TabIndex = 19;
//
// sbBrowseOutputFile
//
this.sbBrowseOutputFile.Location = new System.Drawing.Point(410, 57);
this.sbBrowseOutputFile.Name = "sbBrowseOutputFile";
this.sbBrowseOutputFile.Size = new System.Drawing.Size(75, 25);
this.sbBrowseOutputFile.TabIndex = 20;
this.sbBrowseOutputFile.Text = "Browse...";
this.sbBrowseOutputFile.Click += new System.EventHandler(this.sbBrowseOutputFile_Click);
//
// label2
//
this.label2.Location = new System.Drawing.Point(5, 63);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(60, 13);
this.label2.TabIndex = 18;
this.label2.Text = "Output file:";
//
// edTimestampServer
//
this.edTimestampServer.Location = new System.Drawing.Point(385, 70);
this.edTimestampServer.Name = "edTimestampServer";
this.edTimestampServer.Size = new System.Drawing.Size(295, 20);
this.edTimestampServer.TabIndex = 27;
this.edTimestampServer.Text = "http://";
//
// cbTimestamp
//
this.cbTimestamp.AutoSize = true;
this.cbTimestamp.Location = new System.Drawing.Point(375, 50);
this.cbTimestamp.Name = "cbTimestamp";
this.cbTimestamp.Size = new System.Drawing.Size(207, 17);
this.cbTimestamp.TabIndex = 26;
this.cbTimestamp.Text = "Request a timestamp from TSA server:";
//
// cbReason
//
this.cbReason.Items.AddRange(new object[] {
"I am the author of this document",
"I agree to the terms defined by placement of my signature on this document",
"I have reviewed this document",
"I attest to the accuracy and integrity of this document",
"I am approving this document"});
this.cbReason.Location = new System.Drawing.Point(10, 69);
this.cbReason.Name = "cbReason";
this.cbReason.Size = new System.Drawing.Size(346, 21);
this.cbReason.TabIndex = 31;
this.cbReason.Text = "<none>";
//
// lReason
//
this.lReason.AutoSize = true;
this.lReason.Location = new System.Drawing.Point(7, 48);
this.lReason.Name = "lReason";
this.lReason.Size = new System.Drawing.Size(98, 13);
this.lReason.TabIndex = 30;
this.lReason.Text = "Reason for signing:";
//
// edAuthor
//
this.edAuthor.Location = new System.Drawing.Point(88, 25);
this.edAuthor.Name = "edAuthor";
this.edAuthor.Size = new System.Drawing.Size(268, 20);
this.edAuthor.TabIndex = 29;
//
// lAuthor
//
this.lAuthor.AutoSize = true;
this.lAuthor.Location = new System.Drawing.Point(7, 28);
this.lAuthor.Name = "lAuthor";
this.lAuthor.Size = new System.Drawing.Size(77, 13);
this.lAuthor.TabIndex = 28;
this.lAuthor.Text = "Author\'s name:";
//
// btnBrowseCert
//
this.btnBrowseCert.Location = new System.Drawing.Point(285, 67);
this.btnBrowseCert.Name = "btnBrowseCert";
this.btnBrowseCert.Size = new System.Drawing.Size(75, 25);
this.btnBrowseCert.TabIndex = 43;
this.btnBrowseCert.Text = "Browse...";
this.btnBrowseCert.Click += new System.EventHandler(this.btnBrowseCert_Click);
//
// edCertPassword
//
this.edCertPassword.Location = new System.Drawing.Point(480, 70);
this.edCertPassword.Name = "edCertPassword";
this.edCertPassword.PasswordChar = '*';
this.edCertPassword.Size = new System.Drawing.Size(200, 20);
this.edCertPassword.TabIndex = 42;
//
// edCertFile
//
this.edCertFile.Location = new System.Drawing.Point(20, 70);
this.edCertFile.Name = "edCertFile";
this.edCertFile.Size = new System.Drawing.Size(260, 20);
this.edCertFile.TabIndex = 41;
//
// lCertPassword
//
this.lCertPassword.AutoSize = true;
this.lCertPassword.Location = new System.Drawing.Point(372, 73);
this.lCertPassword.Name = "lCertPassword";
this.lCertPassword.Size = new System.Drawing.Size(105, 13);
this.lCertPassword.TabIndex = 40;
this.lCertPassword.Text = "Certificate password:";
//
// label6
//
this.label6.AutoSize = true;
this.label6.Location = new System.Drawing.Point(7, 26);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(91, 13);
this.label6.TabIndex = 44;
this.label6.Text = "Signing certificate";
//
// label7
//
this.label7.AutoSize = true;
this.label7.ForeColor = System.Drawing.SystemColors.Highlight;
this.label7.Location = new System.Drawing.Point(5, 5);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(643, 13);
this.label7.TabIndex = 45;
this.label7.Text = "This sample illustrates the use of PDFSigner component for signing PDF documents." +
" Please pick the signing certificate and click \'Sign\'. ";
//
// groupBox5
//
this.groupBox5.Controls.Add(this.rbWin32);
this.groupBox5.Controls.Add(this.edWin32Store);
this.groupBox5.Controls.Add(this.rbPKCS11);
this.groupBox5.Controls.Add(this.btnBrowsePKCS11);
this.groupBox5.Controls.Add(this.edPKCS11PIN);
this.groupBox5.Controls.Add(this.label1);
this.groupBox5.Controls.Add(this.edPKCS11File);
this.groupBox5.Controls.Add(this.rbCertFile);
this.groupBox5.Controls.Add(this.label6);
this.groupBox5.Controls.Add(this.btnBrowseCert);
this.groupBox5.Controls.Add(this.edCertPassword);
this.groupBox5.Controls.Add(this.lCertPassword);
this.groupBox5.Controls.Add(this.edCertFile);
this.groupBox5.Location = new System.Drawing.Point(5, 95);
this.groupBox5.Name = "groupBox5";
this.groupBox5.Size = new System.Drawing.Size(690, 210);
this.groupBox5.TabIndex = 46;
this.groupBox5.TabStop = false;
this.groupBox5.Text = "Signing options ";
//
// rbWin32
//
this.rbWin32.AutoSize = true;
this.rbWin32.Location = new System.Drawing.Point(20, 150);
this.rbWin32.Name = "rbWin32";
this.rbWin32.Size = new System.Drawing.Size(97, 17);
this.rbWin32.TabIndex = 55;
this.rbWin32.Text = "Win32 storage:";
this.rbWin32.UseVisualStyleBackColor = true;
//
// edWin32Store
//
this.edWin32Store.Location = new System.Drawing.Point(20, 170);
this.edWin32Store.Name = "edWin32Store";
this.edWin32Store.Size = new System.Drawing.Size(260, 20);
this.edWin32Store.TabIndex = 52;
this.edWin32Store.Text = "My";
//
// rbPKCS11
//
this.rbPKCS11.AutoSize = true;
this.rbPKCS11.Location = new System.Drawing.Point(20, 100);
this.rbPKCS11.Name = "rbPKCS11";
this.rbPKCS11.Size = new System.Drawing.Size(122, 17);
this.rbPKCS11.TabIndex = 50;
this.rbPKCS11.Text = "PKCS11 storage file:";
this.rbPKCS11.UseVisualStyleBackColor = true;
//
// btnBrowsePKCS11
//
this.btnBrowsePKCS11.Location = new System.Drawing.Point(285, 117);
this.btnBrowsePKCS11.Name = "btnBrowsePKCS11";
this.btnBrowsePKCS11.Size = new System.Drawing.Size(75, 25);
this.btnBrowsePKCS11.TabIndex = 49;
this.btnBrowsePKCS11.Text = "Browse...";
this.btnBrowsePKCS11.Click += new System.EventHandler(this.btnBrowsePKCS11_Click);
//
// edPKCS11PIN
//
this.edPKCS11PIN.Location = new System.Drawing.Point(480, 120);
this.edPKCS11PIN.Name = "edPKCS11PIN";
this.edPKCS11PIN.PasswordChar = '*';
this.edPKCS11PIN.Size = new System.Drawing.Size(200, 20);
this.edPKCS11PIN.TabIndex = 48;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(372, 123);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(28, 13);
this.label1.TabIndex = 46;
this.label1.Text = "PIN:";
//
// edPKCS11File
//
this.edPKCS11File.Location = new System.Drawing.Point(20, 120);
this.edPKCS11File.Name = "edPKCS11File";
this.edPKCS11File.Size = new System.Drawing.Size(260, 20);
this.edPKCS11File.TabIndex = 47;
//
// rbCertFile
//
this.rbCertFile.AutoSize = true;
this.rbCertFile.Checked = true;
this.rbCertFile.Location = new System.Drawing.Point(20, 50);
this.rbCertFile.Name = "rbCertFile";
this.rbCertFile.Size = new System.Drawing.Size(91, 17);
this.rbCertFile.TabIndex = 45;
this.rbCertFile.TabStop = true;
this.rbCertFile.Text = "Certificate file:";
this.rbCertFile.UseVisualStyleBackColor = true;
//
// groupBox7
//
this.groupBox7.Controls.Add(this.lAuthor);
this.groupBox7.Controls.Add(this.edAuthor);
this.groupBox7.Controls.Add(this.lReason);
this.groupBox7.Controls.Add(this.cbReason);
this.groupBox7.Controls.Add(this.edTimestampServer);
this.groupBox7.Controls.Add(this.cbTimestamp);
this.groupBox7.Location = new System.Drawing.Point(5, 320);
this.groupBox7.Name = "groupBox7";
this.groupBox7.Size = new System.Drawing.Size(690, 101);
this.groupBox7.TabIndex = 47;
this.groupBox7.TabStop = false;
this.groupBox7.Text = "Additional options ";
//
// MainForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(699, 471);
this.Controls.Add(this.groupBox7);
this.Controls.Add(this.groupBox5);
this.Controls.Add(this.label7);
this.Controls.Add(this.edOutputFile);
this.Controls.Add(this.sbBrowseOutputFile);
this.Controls.Add(this.label2);
this.Controls.Add(this.edInputFile);
this.Controls.Add(this.btnSign);
this.Controls.Add(this.sbBrowseInputFile);
this.Controls.Add(this.lbInputFile);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "MainForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Simple PDF Signer demo";
this.groupBox5.ResumeLayout(false);
this.groupBox5.PerformLayout();
this.groupBox7.ResumeLayout(false);
this.groupBox7.PerformLayout();
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 sbBrowseInputFile_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 sbBrowseOutputFile_Click(object sender, EventArgs e)
{
dlgSave.InitialDirectory = Application.StartupPath;
dlgSave.FileName = edInputFile.Text;
if (dlgSave.ShowDialog() == DialogResult.OK)
edOutputFile.Text = dlgSave.FileName;
}
private void btnBrowseCert_Click(object sender, EventArgs e)
{
if (dlgOpen.ShowDialog() == DialogResult.OK)
edCertFile.Text = dlgOpen.FileName;
}
private void btnBrowsePKCS11_Click(object sender, EventArgs e)
{
if (dlgOpen.ShowDialog() == DialogResult.OK)
edPKCS11File.Text = dlgOpen.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 void btnSign_Click(object sender, System.EventArgs e)
{
CertificateStorage certstorage = new CertificateStorage();
signer.InputFile = edInputFile.Text;
signer.OutputFile = edOutputFile.Text;
if (rbCertFile.Checked)
{
signer.SigningCertificate = LoadCertificate(edCertFile.Text, edCertPassword.Text);
}
else
{
try
{
if (rbPKCS11.Checked)
{
certstorage.Open("pkcs11://user:" + edPKCS11PIN.Text + "@/" + edPKCS11File.Text);
}
else
{
certstorage.Open("system://?store=" + edWin32Store.Text);
}
signer.SigningCertificate = certstorage.Certificates[0];
}
catch (Exception ex)
{
MessageBox.Show("Cannot load certificate!");
return;
}
}
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;
signer.NewSignature.AuthorName = edAuthor.Text;
if (String.Compare(cbReason.Text, "<none>") != 0)
signer.NewSignature.Reason = cbReason.Text;
else
signer.NewSignature.Reason = "";
if (cbTimestamp.Checked)
signer.TimestampServer = edTimestampServer.Text;
else
signer.TimestampServer = "";
try
{
signer.Sign();
MessageBox.Show("PDF file successfully signed");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}