/* * 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 description for frmMain. /// 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; /// /// Required designer variable. /// 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; public TextBox edTimestampServer; public CheckBox cbTimestamp; 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 ComboBox cbHashAlgorithm; private Label label4; private ComboBox cbSignatureType; private Label label3; private PDFSigner signer; public MainForm() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // signer = new PDFSigner(); cbSignatureType.SelectedIndex = 1; cbLevel.SelectedIndex = 5; cbHashAlgorithm.SelectedIndex = 2; } /// /// Clean up any resources being used. /// protected override void Dispose(bool disposing) { if (disposing) { if (components != null) { components.Dispose(); signer.Dispose(); } } base.Dispose(disposing); } #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.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.edTimestampServer = new System.Windows.Forms.TextBox(); this.cbTimestamp = 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.cbSignatureType = new System.Windows.Forms.ComboBox(); this.label3 = new System.Windows.Forms.Label(); this.cbHashAlgorithm = new System.Windows.Forms.ComboBox(); this.label4 = new System.Windows.Forms.Label(); this.groupBox5.SuspendLayout(); this.SuspendLayout(); // // lbInputFile // this.lbInputFile.AutoSize = true; this.lbInputFile.Location = new System.Drawing.Point(14, 33); this.lbInputFile.Name = "lbInputFile"; this.lbInputFile.Size = new System.Drawing.Size(50, 13); this.lbInputFile.TabIndex = 0; this.lbInputFile.Text = "Input file:"; // // edInputFile // this.edInputFile.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.edInputFile.Location = new System.Drawing.Point(70, 30); this.edInputFile.Name = "edInputFile"; this.edInputFile.Size = new System.Drawing.Size(296, 20); this.edInputFile.TabIndex = 1; // // sbBrowseInputFile // this.sbBrowseInputFile.Location = new System.Drawing.Point(372, 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(586, 231); 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.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.edOutputFile.Location = new System.Drawing.Point(70, 60); this.edOutputFile.Name = "edOutputFile"; this.edOutputFile.Size = new System.Drawing.Size(296, 20); this.edOutputFile.TabIndex = 3; // // button1 // this.button1.Location = new System.Drawing.Point(372, 57); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 25); this.button1.TabIndex = 4; this.button1.Text = "Browse..."; this.button1.Click += new System.EventHandler(this.button1_Click); // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(6, 63); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(58, 13); this.label2.TabIndex = 18; this.label2.Text = "Output file:"; // // cbVisible // this.cbVisible.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.cbVisible.AutoSize = true; this.cbVisible.Location = new System.Drawing.Point(536, 103); this.cbVisible.Name = "cbVisible"; this.cbVisible.Size = new System.Drawing.Size(102, 17); this.cbVisible.TabIndex = 8; this.cbVisible.Text = "Visible signature"; // // edTimestampServer // this.edTimestampServer.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.edTimestampServer.Location = new System.Drawing.Point(10, 98); this.edTimestampServer.Name = "edTimestampServer"; this.edTimestampServer.Size = new System.Drawing.Size(351, 20); this.edTimestampServer.TabIndex = 5; this.edTimestampServer.Text = "http://"; // // cbTimestamp // this.cbTimestamp.AutoSize = true; this.cbTimestamp.Location = new System.Drawing.Point(10, 78); this.cbTimestamp.Name = "cbTimestamp"; this.cbTimestamp.Size = new System.Drawing.Size(207, 17); this.cbTimestamp.TabIndex = 4; this.cbTimestamp.Text = "Request a timestamp from TSA server:"; // // label1 // this.label1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(494, 52); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(36, 13); this.label1.TabIndex = 34; this.label1.Text = "Level:"; // // cbLevel // this.cbLevel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.cbLevel.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.cbLevel.FormattingEnabled = true; this.cbLevel.Items.AddRange(new object[] { "Generic/Legacy", "Baseline B", "Baseline T", "Baseline LT", "Baseline LTA", "BES", "EPES", "LTV"}); this.cbLevel.Location = new System.Drawing.Point(536, 49); this.cbLevel.Name = "cbLevel"; this.cbLevel.Size = new System.Drawing.Size(128, 21); this.cbLevel.TabIndex = 6; // // btnBrowseCert // this.btnBrowseCert.Location = new System.Drawing.Point(367, 21); this.btnBrowseCert.Name = "btnBrowseCert"; this.btnBrowseCert.Size = new System.Drawing.Size(75, 25); this.btnBrowseCert.TabIndex = 2; this.btnBrowseCert.Text = "Browse..."; this.btnBrowseCert.Click += new System.EventHandler(this.btnBrowseCert_Click); // // edCertPassword // this.edCertPassword.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.edCertPassword.Location = new System.Drawing.Point(107, 51); this.edCertPassword.Name = "edCertPassword"; this.edCertPassword.PasswordChar = '*'; this.edCertPassword.Size = new System.Drawing.Size(230, 20); this.edCertPassword.TabIndex = 3; // // edSigningCertificate // this.edSigningCertificate.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.edSigningCertificate.Location = new System.Drawing.Point(107, 24); this.edSigningCertificate.Name = "edSigningCertificate"; this.edSigningCertificate.Size = new System.Drawing.Size(254, 20); this.edSigningCertificate.TabIndex = 1; // // lCertPassword // this.lCertPassword.AutoSize = true; this.lCertPassword.Location = new System.Drawing.Point(45, 54); this.lCertPassword.Name = "lCertPassword"; this.lCertPassword.Size = new System.Drawing.Size(56, 13); this.lCertPassword.TabIndex = 40; this.lCertPassword.Text = "Password:"; // // label6 // this.label6.AutoSize = true; this.label6.Location = new System.Drawing.Point(7, 27); 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(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.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.groupBox5.Controls.Add(this.cbSignatureType); this.groupBox5.Controls.Add(this.label3); this.groupBox5.Controls.Add(this.edTimestampServer); this.groupBox5.Controls.Add(this.cbHashAlgorithm); this.groupBox5.Controls.Add(this.cbTimestamp); this.groupBox5.Controls.Add(this.label4); 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, 95); this.groupBox5.Name = "groupBox5"; this.groupBox5.Size = new System.Drawing.Size(670, 130); this.groupBox5.TabIndex = 5; this.groupBox5.TabStop = false; this.groupBox5.Text = "Signing options "; // // cbSignatureType // this.cbSignatureType.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.cbSignatureType.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.cbSignatureType.FormattingEnabled = true; this.cbSignatureType.Items.AddRange(new object[] { "Legacy", "PAdES", "DocumentTimestamp"}); this.cbSignatureType.Location = new System.Drawing.Point(536, 24); this.cbSignatureType.Name = "cbSignatureType"; this.cbSignatureType.Size = new System.Drawing.Size(128, 21); this.cbSignatureType.TabIndex = 47; this.cbSignatureType.SelectionChangeCommitted += new System.EventHandler(this.cbSignatureType_SelectionChangeCommitted); // // label3 // this.label3.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.label3.AutoSize = true; this.label3.Location = new System.Drawing.Point(448, 27); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(82, 13); this.label3.TabIndex = 48; this.label3.Text = "Signature Type:"; // // cbHashAlgorithm // this.cbHashAlgorithm.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.cbHashAlgorithm.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.cbHashAlgorithm.Items.AddRange(new object[] { "SHA1", "MD5", "SHA256", "SHA384", "SHA512", "RIPEMD160"}); this.cbHashAlgorithm.Location = new System.Drawing.Point(536, 76); this.cbHashAlgorithm.Name = "cbHashAlgorithm"; this.cbHashAlgorithm.Size = new System.Drawing.Size(128, 21); this.cbHashAlgorithm.TabIndex = 7; // // label4 // this.label4.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.label4.AutoSize = true; this.label4.Location = new System.Drawing.Point(450, 79); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(80, 13); this.label4.TabIndex = 46; this.label4.Text = "Hash algorithm:"; // // MainForm // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(681, 263); 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.FixedDialog; 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 /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.Run(new MainForm()); } private void sbBrowseInputFile_Click(object sender, System.EventArgs e) { dlgOpen.InitialDirectory = Application.StartupPath; dlgOpen.Filter = "PDF files (*.pdf)|*.pdf|All files (*.*)|*.*"; 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"; } if (cbSignatureType.SelectedIndex == 0) signer.NewSignature.SignatureType = PDFSignatureTypes.pstLegacy; else if (cbSignatureType.SelectedIndex == 2) signer.NewSignature.SignatureType = PDFSignatureTypes.pstDocumentTimestamp; else { 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.NewSignature.HashAlgorithm = cbHashAlgorithm.Text; signer.Widget.Invisible = !cbVisible.Checked; signer.NewSignature.PolicyID = "1.2.3.4.5"; 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); } } private void button1_Click(object sender, EventArgs e) { dlgSave.InitialDirectory = Application.StartupPath; dlgSave.Filter = "PDF files (*.pdf)|*.pdf|All files (*.*)|*.*"; dlgSave.FileName = edInputFile.Text; if (dlgSave.ShowDialog() == DialogResult.OK) edOutputFile.Text = dlgSave.FileName; } private string RequestPassword() { frmRequestPassword dlg = new frmRequestPassword(); string s = ""; if (dlg.ShowDialog() == DialogResult.OK) { s = dlg.tbInput.Text; } dlg.Dispose(); return s; } private void btnBrowseCert_Click(object sender, EventArgs e) { dlgOpen.Filter = "PEM-encoded certificate (*.pem)|*.PEM|DER-encoded certificate (*.cer)|*.CER|PFX-encoded certificate (*.pfx)|*.PFX"; if (dlgOpen.ShowDialog() == DialogResult.OK) edSigningCertificate.Text = dlgOpen.FileName; } private void cbSignatureType_SelectionChangeCommitted(object sender, EventArgs e) { cbLevel.Enabled = cbSignatureType.SelectedIndex == 1; } } }