/* * 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.IO; namespace nsoftware.Demos { /// /// Summary description for frmMain. /// public class xmlsignerDemo : System.Windows.Forms.Form { private System.Windows.Forms.Label lbXMLFile; private System.Windows.Forms.TextBox edXMLFile; private System.Windows.Forms.Button sbBrowseXMLFile; private System.Windows.Forms.OpenFileDialog dlgOpen; /// /// Required designer variable. /// private System.ComponentModel.Container components = null; private System.Windows.Forms.Button btnSign; private Button btnReferences; private GroupBox gbKeyInfo; public CheckBox cbIncludeKey; private TextBox edKeyName; private Label lbKeyName; private TextBox edOutputFile; private Button sbBrowseOutputFile; private Label label2; private ReferencesForm frmReferences; private SaveFileDialog dlgSave; public CheckBox cbDetached; private ComboBox cmbHashAlgorithm; private Label lbSignatureMethod; private ComboBox cmbCanonMethod; private Label lbCanonMethod; private Label label3; private Label label6; private Button btnBrowseCert; private TextBox edCertPassword; private TextBox edSigningCertificate; private Label lCertPassword; private TextBox edSignatureNode; private Label lbSignatureLocation; private XMLSigner m_Signer; private CertificateManager m_CertManager; public xmlsignerDemo() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // cmbCanonMethod.SelectedIndex = 0; cmbHashAlgorithm.SelectedIndex = 2; m_Signer = new XMLSigner(); m_CertManager = new CertificateManager(); frmReferences = new ReferencesForm(m_Signer.References); } /// /// Clean up any resources being used. /// protected override void Dispose(bool disposing) { if (disposing) { if (components != null) { components.Dispose(); m_Signer.Dispose(); m_CertManager.Dispose(); frmReferences.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.lbXMLFile = new System.Windows.Forms.Label(); this.edXMLFile = new System.Windows.Forms.TextBox(); this.sbBrowseXMLFile = new System.Windows.Forms.Button(); this.btnSign = new System.Windows.Forms.Button(); this.dlgOpen = new System.Windows.Forms.OpenFileDialog(); this.btnReferences = new System.Windows.Forms.Button(); this.gbKeyInfo = new System.Windows.Forms.GroupBox(); this.label6 = new System.Windows.Forms.Label(); 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.cbIncludeKey = new System.Windows.Forms.CheckBox(); this.edKeyName = new System.Windows.Forms.TextBox(); this.lbKeyName = new System.Windows.Forms.Label(); 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.edSignatureNode = new System.Windows.Forms.TextBox(); this.lbSignatureLocation = new System.Windows.Forms.Label(); this.cbDetached = new System.Windows.Forms.CheckBox(); this.cmbHashAlgorithm = new System.Windows.Forms.ComboBox(); this.lbSignatureMethod = new System.Windows.Forms.Label(); this.cmbCanonMethod = new System.Windows.Forms.ComboBox(); this.lbCanonMethod = new System.Windows.Forms.Label(); this.label3 = new System.Windows.Forms.Label(); this.gbKeyInfo.SuspendLayout(); this.SuspendLayout(); // // lbXMLFile // this.lbXMLFile.AutoSize = true; this.lbXMLFile.Location = new System.Drawing.Point(15, 38); this.lbXMLFile.Name = "lbXMLFile"; this.lbXMLFile.Size = new System.Drawing.Size(50, 13); this.lbXMLFile.TabIndex = 0; this.lbXMLFile.Text = "Input file:"; // // edXMLFile // this.edXMLFile.Location = new System.Drawing.Point(71, 35); this.edXMLFile.Name = "edXMLFile"; this.edXMLFile.Size = new System.Drawing.Size(280, 20); this.edXMLFile.TabIndex = 1; // // sbBrowseXMLFile // this.sbBrowseXMLFile.Location = new System.Drawing.Point(357, 32); this.sbBrowseXMLFile.Name = "sbBrowseXMLFile"; this.sbBrowseXMLFile.Size = new System.Drawing.Size(75, 25); this.sbBrowseXMLFile.TabIndex = 2; this.sbBrowseXMLFile.Text = "Browse ..."; this.sbBrowseXMLFile.Click += new System.EventHandler(this.sbBrowseXMLFile_Click); // // btnSign // this.btnSign.Location = new System.Drawing.Point(607, 256); 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); // // btnReferences // this.btnReferences.Location = new System.Drawing.Point(8, 256); this.btnReferences.Name = "btnReferences"; this.btnReferences.Size = new System.Drawing.Size(75, 25); this.btnReferences.TabIndex = 6; this.btnReferences.Text = "References"; this.btnReferences.Click += new System.EventHandler(this.btnReferences_Click); // // gbKeyInfo // this.gbKeyInfo.Controls.Add(this.edSignatureNode); this.gbKeyInfo.Controls.Add(this.lbSignatureLocation); this.gbKeyInfo.Controls.Add(this.cbDetached); this.gbKeyInfo.Controls.Add(this.label6); this.gbKeyInfo.Controls.Add(this.cmbHashAlgorithm); this.gbKeyInfo.Controls.Add(this.lbSignatureMethod); this.gbKeyInfo.Controls.Add(this.btnBrowseCert); this.gbKeyInfo.Controls.Add(this.cmbCanonMethod); this.gbKeyInfo.Controls.Add(this.edCertPassword); this.gbKeyInfo.Controls.Add(this.lbCanonMethod); this.gbKeyInfo.Controls.Add(this.edSigningCertificate); this.gbKeyInfo.Controls.Add(this.lCertPassword); this.gbKeyInfo.Controls.Add(this.cbIncludeKey); this.gbKeyInfo.Controls.Add(this.edKeyName); this.gbKeyInfo.Controls.Add(this.lbKeyName); this.gbKeyInfo.Location = new System.Drawing.Point(8, 100); this.gbKeyInfo.Name = "gbKeyInfo"; this.gbKeyInfo.Size = new System.Drawing.Size(674, 148); this.gbKeyInfo.TabIndex = 5; this.gbKeyInfo.TabStop = false; this.gbKeyInfo.Text = "Signing options "; // // label6 // this.label6.AutoSize = true; this.label6.Location = new System.Drawing.Point(7, 28); this.label6.Name = "label6"; this.label6.Size = new System.Drawing.Size(94, 13); this.label6.TabIndex = 65; this.label6.Text = "Signing certificate:"; // // btnBrowseCert // this.btnBrowseCert.Location = new System.Drawing.Point(349, 22); 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(107, 51); this.edCertPassword.Name = "edCertPassword"; this.edCertPassword.PasswordChar = '*'; this.edCertPassword.Size = new System.Drawing.Size(181, 20); this.edCertPassword.TabIndex = 3; // // edSigningCertificate // this.edSigningCertificate.Location = new System.Drawing.Point(107, 25); this.edSigningCertificate.Name = "edSigningCertificate"; this.edSigningCertificate.Size = new System.Drawing.Size(236, 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 = 61; this.lCertPassword.Text = "Password:"; // // cbIncludeKey // this.cbIncludeKey.Checked = true; this.cbIncludeKey.CheckState = System.Windows.Forms.CheckState.Checked; this.cbIncludeKey.Location = new System.Drawing.Point(10, 90); this.cbIncludeKey.Name = "cbIncludeKey"; this.cbIncludeKey.Size = new System.Drawing.Size(184, 24); this.cbIncludeKey.TabIndex = 6; this.cbIncludeKey.Text = "Include Key (public part)"; // // edKeyName // this.edKeyName.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.edKeyName.Location = new System.Drawing.Point(107, 116); this.edKeyName.Name = "edKeyName"; this.edKeyName.Size = new System.Drawing.Size(236, 20); this.edKeyName.TabIndex = 7; // // lbKeyName // this.lbKeyName.AutoSize = true; this.lbKeyName.Location = new System.Drawing.Point(42, 119); this.lbKeyName.Name = "lbKeyName"; this.lbKeyName.Size = new System.Drawing.Size(59, 13); this.lbKeyName.TabIndex = 0; this.lbKeyName.Text = "Key Name:"; // // edOutputFile // this.edOutputFile.Location = new System.Drawing.Point(71, 65); this.edOutputFile.Name = "edOutputFile"; this.edOutputFile.Size = new System.Drawing.Size(280, 20); this.edOutputFile.TabIndex = 3; // // sbBrowseOutputFile // this.sbBrowseOutputFile.Location = new System.Drawing.Point(357, 62); this.sbBrowseOutputFile.Name = "sbBrowseOutputFile"; this.sbBrowseOutputFile.Size = new System.Drawing.Size(75, 25); this.sbBrowseOutputFile.TabIndex = 4; this.sbBrowseOutputFile.Text = "Browse ..."; this.sbBrowseOutputFile.Click += new System.EventHandler(this.sbBrowseOutputFile_Click); // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(7, 68); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(58, 13); this.label2.TabIndex = 18; this.label2.Text = "Output file:"; // // edSignatureNode // this.edSignatureNode.Location = new System.Drawing.Point(519, 116); this.edSignatureNode.Name = "edSignatureNode"; this.edSignatureNode.Size = new System.Drawing.Size(149, 20); this.edSignatureNode.TabIndex = 9; // // lbSignatureLocation // this.lbSignatureLocation.AutoSize = true; this.lbSignatureLocation.Location = new System.Drawing.Point(431, 119); this.lbSignatureLocation.Name = "lbSignatureLocation"; this.lbSignatureLocation.Size = new System.Drawing.Size(82, 13); this.lbSignatureLocation.TabIndex = 16; this.lbSignatureLocation.Text = "Signature node:"; // // cbDetached // this.cbDetached.AutoSize = true; this.cbDetached.Location = new System.Drawing.Point(416, 94); this.cbDetached.Name = "cbDetached"; this.cbDetached.Size = new System.Drawing.Size(151, 17); this.cbDetached.TabIndex = 8; this.cbDetached.Text = "Create detached signature"; // // cmbHashAlgorithm // this.cmbHashAlgorithm.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.cmbHashAlgorithm.Items.AddRange(new object[] { "SHA1", "MD5", "SHA256", "SHA384", "SHA512", "RIPEMD160"}); this.cmbHashAlgorithm.Location = new System.Drawing.Point(519, 24); this.cmbHashAlgorithm.Name = "cmbHashAlgorithm"; this.cmbHashAlgorithm.Size = new System.Drawing.Size(149, 21); this.cmbHashAlgorithm.TabIndex = 4; // // lbSignatureMethod // this.lbSignatureMethod.AutoSize = true; this.lbSignatureMethod.Location = new System.Drawing.Point(435, 27); this.lbSignatureMethod.Name = "lbSignatureMethod"; this.lbSignatureMethod.Size = new System.Drawing.Size(80, 13); this.lbSignatureMethod.TabIndex = 8; this.lbSignatureMethod.Text = "Hash algorithm:"; // // cmbCanonMethod // this.cmbCanonMethod.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.cmbCanonMethod.Items.AddRange(new object[] { "Canonical", "Canonical with comments", "Canonical v1.1", "Canonical with comments v1.1", "Exclusive canonical", "Exclusive canonical with comments", "Minimal canonical"}); this.cmbCanonMethod.Location = new System.Drawing.Point(519, 51); this.cmbCanonMethod.Name = "cmbCanonMethod"; this.cmbCanonMethod.Size = new System.Drawing.Size(149, 21); this.cmbCanonMethod.TabIndex = 5; // // lbCanonMethod // this.lbCanonMethod.AutoSize = true; this.lbCanonMethod.Location = new System.Drawing.Point(426, 54); this.lbCanonMethod.Name = "lbCanonMethod"; this.lbCanonMethod.Size = new System.Drawing.Size(87, 13); this.lbCanonMethod.TabIndex = 2; this.lbCanonMethod.Text = "Canonicalization:"; // // label3 // this.label3.AutoSize = true; this.label3.ForeColor = System.Drawing.SystemColors.Highlight; this.label3.Location = new System.Drawing.Point(5, 5); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(582, 13); this.label3.TabIndex = 23; this.label3.Text = "This sample shows how to create XML signatures. Please select an input file, tune" + " up the signing options, and click \'Sign\'. "; // // xmlsignerDemo // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(689, 291); this.Controls.Add(this.label3); this.Controls.Add(this.btnReferences); this.Controls.Add(this.edOutputFile); this.Controls.Add(this.sbBrowseOutputFile); this.Controls.Add(this.label2); this.Controls.Add(this.gbKeyInfo); this.Controls.Add(this.edXMLFile); this.Controls.Add(this.btnSign); this.Controls.Add(this.sbBrowseXMLFile); this.Controls.Add(this.lbXMLFile); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.MaximizeBox = false; this.Name = "xmlsignerDemo"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "XML Signer Demo"; this.gbKeyInfo.ResumeLayout(false); this.gbKeyInfo.PerformLayout(); this.ResumeLayout(false); this.PerformLayout(); } #endregion /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.Run(new xmlsignerDemo()); } private void sbBrowseXMLFile_Click(object sender, System.EventArgs e) { dlgOpen.Filter = "XML file (*.xml)|*.XML"; if (dlgOpen.ShowDialog() == DialogResult.OK) edXMLFile.Text = dlgOpen.FileName; } public Certificate LoadCertificate(string file, string password) { Certificate cert = null; if (file.Length > 0) { try { m_CertManager.ImportFromFile(file, password); cert = m_CertManager.Certificate; } catch (Exception ex) { MessageBox.Show("Failed to load certificate: " + ex.Message); } } return cert; } private void btnSign_Click(object sender, System.EventArgs e) { m_Signer.OutputFile = edOutputFile.Text; if (cbDetached.Checked) { m_Signer.DataFile = edXMLFile.Text; m_Signer.DataType = XMLSignerDataTypes.cxdtBinary; m_Signer.DataURI = Path.GetFileName(edXMLFile.Text); m_Signer.SignatureType = XMLSignerSignatureTypes.cxstDetached; } else { m_Signer.InputFile = edXMLFile.Text; m_Signer.SignatureType = XMLSignerSignatureTypes.cxstEnveloped; } m_Signer.SigningCertificate = LoadCertificate(edSigningCertificate.Text, edCertPassword.Text); switch (cmbCanonMethod.SelectedIndex) { case 0: { m_Signer.CanonicalizationMethod = XMLSignerCanonicalizationMethods.cxcmCanon; break; } case 1: { m_Signer.CanonicalizationMethod = XMLSignerCanonicalizationMethods.cxcmCanonComment; break; } case 2: { m_Signer.CanonicalizationMethod = XMLSignerCanonicalizationMethods.cxcmCanon_v1_1; break; } case 3: { m_Signer.CanonicalizationMethod = XMLSignerCanonicalizationMethods.cxcmCanonComment_v1_1; break; } case 4: { m_Signer.CanonicalizationMethod = XMLSignerCanonicalizationMethods.cxcmExclCanon; break; } case 5: { m_Signer.CanonicalizationMethod = XMLSignerCanonicalizationMethods.cxcmExclCanonComment; break; } case 6: { m_Signer.CanonicalizationMethod = XMLSignerCanonicalizationMethods.cxcmMinCanon; break; } default: { m_Signer.CanonicalizationMethod = XMLSignerCanonicalizationMethods.cxcmCanon; break; } } m_Signer.HashAlgorithm = cmbHashAlgorithm.Text; m_Signer.Config("KeyName=" + edKeyName.Text); if (cbIncludeKey.Checked) m_Signer.Config("IncludeKey=true"); else m_Signer.Config("IncludeKey=false"); // Enable automatic signature formatting m_Signer.Config("XMLFormatting=auto"); m_Signer.XMLElement = edSignatureNode.Text; try { m_Signer.Sign(); MessageBox.Show("XML document successfully signed"); } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void btnReferences_Click(object sender, EventArgs e) { frmReferences.ShowDialog(); } private string RequestPassword() { frmRequestPassword dlg = new frmRequestPassword(); string s = ""; if (dlg.ShowDialog() == DialogResult.OK) { s = dlg.tbInput.Text; } dlg.Dispose(); return s; } private void sbBrowseOutputFile_Click(object sender, EventArgs e) { dlgOpen.Filter = ""; 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; } } }