/* * 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; namespace ASiCSigner { /// /// Summary description for frmMain. /// public class MainForm : System.Windows.Forms.Form { 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 btnOutputFile; private Label label2; private SaveFileDialog dlgSave; 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 ComboBox cbHashAlgorithm; private Label label4; private TextBox edInputFile; private Button btnInputFile; private Label label3; public CheckBox cbDetached; private Label label5; private GroupBox groupBox1; private CAdESSigner signer; public MainForm() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // signer = new CAdESSigner(); cbLevel.SelectedIndex = 0; 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.btnSign = new System.Windows.Forms.Button(); this.dlgOpen = new System.Windows.Forms.OpenFileDialog(); this.edOutputFile = new System.Windows.Forms.TextBox(); this.btnOutputFile = 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.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.cbHashAlgorithm = new System.Windows.Forms.ComboBox(); this.label4 = new System.Windows.Forms.Label(); this.edInputFile = new System.Windows.Forms.TextBox(); this.btnInputFile = new System.Windows.Forms.Button(); this.label3 = new System.Windows.Forms.Label(); this.cbDetached = new System.Windows.Forms.CheckBox(); this.label5 = new System.Windows.Forms.Label(); this.groupBox1 = new System.Windows.Forms.GroupBox(); this.groupBox1.SuspendLayout(); this.SuspendLayout(); // // btnSign // this.btnSign.Location = new System.Drawing.Point(567, 235); 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, 65); this.edOutputFile.Name = "edOutputFile"; this.edOutputFile.Size = new System.Drawing.Size(266, 20); this.edOutputFile.TabIndex = 3; // // btnOutputFile // this.btnOutputFile.Location = new System.Drawing.Point(342, 62); this.btnOutputFile.Name = "btnOutputFile"; this.btnOutputFile.Size = new System.Drawing.Size(75, 25); this.btnOutputFile.TabIndex = 4; this.btnOutputFile.Text = "Browse ..."; this.btnOutputFile.Click += new System.EventHandler(this.btnOutputFile_Click); // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(8, 68); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(58, 13); this.label2.TabIndex = 18; this.label2.Text = "Output file:"; // // edTimestampServer // this.edTimestampServer.Location = new System.Drawing.Point(13, 101); this.edTimestampServer.Name = "edTimestampServer"; this.edTimestampServer.Size = new System.Drawing.Size(318, 20); this.edTimestampServer.TabIndex = 5; this.edTimestampServer.Text = "http://"; // // cbTimestamp // this.cbTimestamp.Location = new System.Drawing.Point(13, 78); this.cbTimestamp.Name = "cbTimestamp"; this.cbTimestamp.Size = new System.Drawing.Size(244, 17); this.cbTimestamp.TabIndex = 4; this.cbTimestamp.Text = "Request a timestamp from TSA server:"; // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(431, 27); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(36, 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[] { "BaselineB", "BaselineT", "BaselineLT", "BaselineLTA", "BES", "EPES", "T", "C", "X", "XType1", "XType2", "XL", "XLType1", "XLType2", "A", "ExtendedBES", "ExtendedEPES", "ExtendedT", "ExtendedC", "ExtendedX", "ExtendedXType1", "ExtendedXType2", "ExtendedXLong", "ExtendedXL", "ExtendedXLType1", "ExtendedXLType2 ", "ExtendedA"}); this.cbLevel.Location = new System.Drawing.Point(473, 24); this.cbLevel.Name = "cbLevel"; this.cbLevel.Size = new System.Drawing.Size(154, 21); this.cbLevel.TabIndex = 6; // // btnBrowseCert // this.btnBrowseCert.Location = new System.Drawing.Point(337, 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.Location = new System.Drawing.Point(110, 51); this.edCertPassword.Name = "edCertPassword"; this.edCertPassword.PasswordChar = '*'; this.edCertPassword.Size = new System.Drawing.Size(191, 20); this.edCertPassword.TabIndex = 3; // // edSigningCertificate // this.edSigningCertificate.Location = new System.Drawing.Point(110, 24); this.edSigningCertificate.Name = "edSigningCertificate"; this.edSigningCertificate.Size = new System.Drawing.Size(221, 20); this.edSigningCertificate.TabIndex = 1; // // lCertPassword // this.lCertPassword.AutoSize = true; this.lCertPassword.Location = new System.Drawing.Point(48, 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(10, 27); this.label6.Name = "label6"; this.label6.Size = new System.Drawing.Size(94, 13); this.label6.TabIndex = 44; this.label6.Text = "Signing certificate:"; // // cbHashAlgorithm // 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(473, 51); this.cbHashAlgorithm.Name = "cbHashAlgorithm"; this.cbHashAlgorithm.Size = new System.Drawing.Size(154, 21); this.cbHashAlgorithm.TabIndex = 7; // // label4 // this.label4.AutoSize = true; this.label4.Location = new System.Drawing.Point(387, 54); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(80, 13); this.label4.TabIndex = 54; this.label4.Text = "Hash algorithm:"; // // edInputFile // this.edInputFile.Location = new System.Drawing.Point(70, 36); this.edInputFile.Name = "edInputFile"; this.edInputFile.Size = new System.Drawing.Size(266, 20); this.edInputFile.TabIndex = 1; // // btnInputFile // this.btnInputFile.Location = new System.Drawing.Point(342, 33); this.btnInputFile.Name = "btnInputFile"; this.btnInputFile.Size = new System.Drawing.Size(75, 25); this.btnInputFile.TabIndex = 2; this.btnInputFile.Text = "Browse ..."; this.btnInputFile.Click += new System.EventHandler(this.btnInputFile_Click); // // label3 // this.label3.AutoSize = true; this.label3.Location = new System.Drawing.Point(16, 39); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(50, 13); this.label3.TabIndex = 56; this.label3.Text = "Input file:"; // // cbDetached // this.cbDetached.Location = new System.Drawing.Point(473, 78); this.cbDetached.Name = "cbDetached"; this.cbDetached.Size = new System.Drawing.Size(89, 17); this.cbDetached.TabIndex = 8; this.cbDetached.Text = "Detached"; // // label5 // this.label5.ForeColor = System.Drawing.SystemColors.Highlight; this.label5.Location = new System.Drawing.Point(5, 5); this.label5.Name = "label5"; this.label5.Size = new System.Drawing.Size(637, 13); this.label5.TabIndex = 60; this.label5.Text = "This sample shows how to create CAdES signatures. Please select an input file, tu" + "ne up the signing options, and click \'Sign\'."; // // groupBox1 // this.groupBox1.Controls.Add(this.cbTimestamp); this.groupBox1.Controls.Add(this.edTimestampServer); this.groupBox1.Controls.Add(this.cbLevel); this.groupBox1.Controls.Add(this.label1); this.groupBox1.Controls.Add(this.label4); this.groupBox1.Controls.Add(this.cbDetached); this.groupBox1.Controls.Add(this.cbHashAlgorithm); this.groupBox1.Controls.Add(this.label6); this.groupBox1.Controls.Add(this.lCertPassword); this.groupBox1.Controls.Add(this.edSigningCertificate); this.groupBox1.Controls.Add(this.btnBrowseCert); this.groupBox1.Controls.Add(this.edCertPassword); this.groupBox1.Location = new System.Drawing.Point(5, 95); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(635, 134); this.groupBox1.TabIndex = 5; this.groupBox1.TabStop = false; this.groupBox1.Text = "Signing options "; // // MainForm // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(644, 265); this.Controls.Add(this.groupBox1); this.Controls.Add(this.label5); this.Controls.Add(this.edInputFile); this.Controls.Add(this.btnInputFile); this.Controls.Add(this.label3); this.Controls.Add(this.edOutputFile); this.Controls.Add(this.btnOutputFile); this.Controls.Add(this.label2); this.Controls.Add(this.btnSign); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.MaximizeBox = false; this.Name = "MainForm"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "CAdES Signer Demo"; this.groupBox1.ResumeLayout(false); this.groupBox1.PerformLayout(); this.ResumeLayout(false); this.PerformLayout(); } #endregion /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.Run(new MainForm()); } 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.Detached = cbDetached.Checked; signer.NewSignature.HashAlgorithm = cbHashAlgorithm.Text; signer.NewSignature.Level = (AdESSignatureLevels)(cbLevel.SelectedIndex + 2); signer.SigningCertificate = LoadCertificate(edSigningCertificate.Text, edCertPassword.Text); if (cbTimestamp.Checked) signer.TimestampServer = edTimestampServer.Text; else signer.TimestampServer = ""; signer.IgnoreChainValidationErrors = true; try { signer.Sign(); MessageBox.Show("The file(s) successfully signed"); } catch (Exception ex) { MessageBox.Show(ex.Message); } } 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) { if (dlgOpen.ShowDialog() == DialogResult.OK) edSigningCertificate.Text = dlgOpen.FileName; } private void btnOutputFile_Click(object sender, EventArgs e) { dlgSave.FileName = edOutputFile.Text; if (dlgSave.ShowDialog() == DialogResult.OK) edOutputFile.Text = dlgSave.FileName; } private void btnInputFile_Click(object sender, EventArgs e) { if (dlgOpen.ShowDialog() == DialogResult.OK) edInputFile.Text = dlgOpen.FileName; } } }