securebb-win-demos/PDF Signer External (WinForms)/pdfsignerexternal.cs
2024-08-08 13:09:34 +06:00

515 lines
20 KiB
C#

/*
* 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;
using System.Text;
using System.Linq;
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 button1;
private Label label2;
private SaveFileDialog dlgSave;
public CheckBox cbVisible;
private Label label1;
private ComboBox cbLevel;
private Button btnBrowseCert;
private TextBox edCertPassword;
private TextBox edSigningCertificate;
private Label lCertPassword;
private Label label6;
private Label label7;
private GroupBox groupBox5;
private Label label3;
private Button btnBrowseKey;
private TextBox edSigningKey;
private Label label4;
private PDFSigner signer;
public MainForm()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
signer = new PDFSigner();
signer.OnExternalSign += new PDFSigner.OnExternalSignHandler(DoExternalSign);
cbLevel.SelectedIndex = 1;
}
/// <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);
}
public static byte[] HexStringToByteArray(string hex)
{
return Enumerable.Range(0, hex.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
.ToArray();
}
public static string ByteArrayToHexString(byte[] ba)
{
StringBuilder hex = new StringBuilder(ba.Length * 2);
foreach (byte b in ba)
hex.AppendFormat("{0:x2}", b);
return hex.ToString();
}
private void DoExternalSign(object sender, PDFSignerExternalSignEventArgs e)
{
PublicKeyCrypto crypto = new PublicKeyCrypto();
try
{
CryptoKeyManager keymanager = new CryptoKeyManager();
keymanager.ImportFromFile(edSigningKey.Text, 3, "", "", e.Pars, 0, "");
crypto.Key = keymanager.Key;
}
catch (Exception ex)
{
MessageBox.Show("Cannot load key!");
}
try
{
crypto.HashAlgorithm = e.HashAlgorithm;
crypto.InputIsHash = true;
crypto.SchemeParams = e.Pars;
var inBuf = HexStringToByteArray(e.Data);
var outBuf = crypto.Sign(inBuf, true);
e.SignedData = ByteArrayToHexString(outBuf);
}
catch (Exception ex)
{
MessageBox.Show("Cannot sign data!");
}
}
#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.button1 = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.dlgSave = new System.Windows.Forms.SaveFileDialog();
this.cbVisible = new System.Windows.Forms.CheckBox();
this.label1 = new System.Windows.Forms.Label();
this.cbLevel = new System.Windows.Forms.ComboBox();
this.btnBrowseCert = new System.Windows.Forms.Button();
this.edCertPassword = new System.Windows.Forms.TextBox();
this.edSigningCertificate = 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.label3 = new System.Windows.Forms.Label();
this.btnBrowseKey = new System.Windows.Forms.Button();
this.edSigningKey = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.groupBox5.SuspendLayout();
this.SuspendLayout();
//
// lbInputFile
//
this.lbInputFile.Location = new System.Drawing.Point(5, 52);
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, 49);
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, 46);
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(410, 321);
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, 79);
this.edOutputFile.Name = "edOutputFile";
this.edOutputFile.Size = new System.Drawing.Size(334, 20);
this.edOutputFile.TabIndex = 19;
//
// button1
//
this.button1.Location = new System.Drawing.Point(410, 76);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 25);
this.button1.TabIndex = 20;
this.button1.Text = "Browse...";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label2
//
this.label2.Location = new System.Drawing.Point(5, 82);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(60, 13);
this.label2.TabIndex = 18;
this.label2.Text = "Output file:";
//
// cbVisible
//
this.cbVisible.AutoSize = true;
this.cbVisible.Checked = true;
this.cbVisible.CheckState = System.Windows.Forms.CheckState.Checked;
this.cbVisible.Location = new System.Drawing.Point(203, 32);
this.cbVisible.Name = "cbVisible";
this.cbVisible.Size = new System.Drawing.Size(102, 17);
this.cbVisible.TabIndex = 25;
this.cbVisible.Text = "Visible signature";
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(7, 33);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(33, 13);
this.label1.TabIndex = 34;
this.label1.Text = "Level";
//
// cbLevel
//
this.cbLevel.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cbLevel.FormattingEnabled = true;
this.cbLevel.Items.AddRange(new object[] {
"Legacy",
"Baseline B",
"Baseline T",
"Baseline LT",
"Baseline LTA",
"BES",
"EPES",
"LTV"});
this.cbLevel.Location = new System.Drawing.Point(54, 30);
this.cbLevel.Name = "cbLevel";
this.cbLevel.Size = new System.Drawing.Size(121, 21);
this.cbLevel.TabIndex = 35;
//
// btnBrowseCert
//
this.btnBrowseCert.Location = new System.Drawing.Point(270, 73);
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(115, 110);
this.edCertPassword.Name = "edCertPassword";
this.edCertPassword.PasswordChar = '*';
this.edCertPassword.Size = new System.Drawing.Size(230, 20);
this.edCertPassword.TabIndex = 42;
//
// edSigningCertificate
//
this.edSigningCertificate.Location = new System.Drawing.Point(10, 76);
this.edSigningCertificate.Name = "edSigningCertificate";
this.edSigningCertificate.Size = new System.Drawing.Size(254, 20);
this.edSigningCertificate.TabIndex = 41;
//
// lCertPassword
//
this.lCertPassword.AutoSize = true;
this.lCertPassword.Location = new System.Drawing.Point(7, 113);
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, 60);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(94, 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(402, 13);
this.label7.TabIndex = 45;
this.label7.Text = "This sample illustrates the use of PDFSigner component for signing PDF documents." +
"";
//
// groupBox5
//
this.groupBox5.Controls.Add(this.label3);
this.groupBox5.Controls.Add(this.btnBrowseKey);
this.groupBox5.Controls.Add(this.edSigningKey);
this.groupBox5.Controls.Add(this.cbLevel);
this.groupBox5.Controls.Add(this.cbVisible);
this.groupBox5.Controls.Add(this.label6);
this.groupBox5.Controls.Add(this.btnBrowseCert);
this.groupBox5.Controls.Add(this.label1);
this.groupBox5.Controls.Add(this.edCertPassword);
this.groupBox5.Controls.Add(this.lCertPassword);
this.groupBox5.Controls.Add(this.edSigningCertificate);
this.groupBox5.Location = new System.Drawing.Point(5, 114);
this.groupBox5.Name = "groupBox5";
this.groupBox5.Size = new System.Drawing.Size(480, 197);
this.groupBox5.TabIndex = 46;
this.groupBox5.TabStop = false;
this.groupBox5.Text = "Signing options ";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(7, 150);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(135, 13);
this.label3.TabIndex = 47;
this.label3.Text = "Key file for external signing:";
//
// btnBrowseKey
//
this.btnBrowseKey.Location = new System.Drawing.Point(270, 163);
this.btnBrowseKey.Name = "btnBrowseKey";
this.btnBrowseKey.Size = new System.Drawing.Size(75, 25);
this.btnBrowseKey.TabIndex = 46;
this.btnBrowseKey.Text = "Browse...";
this.btnBrowseKey.Click += new System.EventHandler(this.btnBrowseKey_Click);
//
// edSigningKey
//
this.edSigningKey.Location = new System.Drawing.Point(10, 166);
this.edSigningKey.Name = "edSigningKey";
this.edSigningKey.Size = new System.Drawing.Size(254, 20);
this.edSigningKey.TabIndex = 45;
//
// label4
//
this.label4.AutoSize = true;
this.label4.ForeColor = System.Drawing.SystemColors.Highlight;
this.label4.Location = new System.Drawing.Point(5, 24);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(245, 13);
this.label4.TabIndex = 47;
this.label4.Text = "Please pick the signing certificate and click \'Sign\'. ";
//
// MainForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(494, 355);
this.Controls.Add(this.label4);
this.Controls.Add(this.groupBox5);
this.Controls.Add(this.label7);
this.Controls.Add(this.edOutputFile);
this.Controls.Add(this.button1);
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 = "PDF Signer Demo";
this.groupBox5.ResumeLayout(false);
this.groupBox5.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;
}
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)
{
signer.InputFile = edInputFile.Text;
signer.OutputFile = edOutputFile.Text;
signer.SigningCertificate = LoadCertificate(edSigningCertificate.Text, edCertPassword.Text);
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.SignatureType = PDFSignatureTypes.pstPAdES;
switch (cbLevel.SelectedIndex)
{
case 0: signer.NewSignature.Level = PAdESSignatureLevels.paslGeneric; break;
case 1: signer.NewSignature.Level = PAdESSignatureLevels.paslBaselineB; break;
case 2: signer.NewSignature.Level = PAdESSignatureLevels.paslBaselineT; break;
case 3: signer.NewSignature.Level = PAdESSignatureLevels.paslBaselineLT; break;
case 4: signer.NewSignature.Level = PAdESSignatureLevels.paslBaselineLTA; break;
case 5: signer.NewSignature.Level = PAdESSignatureLevels.paslBES; break;
case 6: signer.NewSignature.Level = PAdESSignatureLevels.paslEPES; break;
case 7: signer.NewSignature.Level = PAdESSignatureLevels.paslLTV; break;
}
signer.Widget.Invisible = !cbVisible.Checked;
signer.IgnoreChainValidationErrors = true;
signer.NewSignature.AuthorName = "test demo author";
signer.NewSignature.Reason = "test demo reason";
signer.ExternalCrypto.Mode = ExternalCryptoModes.ecmGeneric;
try
{
signer.SignExternal();
MessageBox.Show("PDF file successfully signed");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button1_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)
edSigningCertificate.Text = dlgOpen.FileName;
}
private void btnBrowseKey_Click(object sender, EventArgs e)
{
if (dlgOpen.ShowDialog() == DialogResult.OK)
edSigningKey.Text = dlgOpen.FileName;
}
}
}