securebb-win-demos/XAdES Signer (WinForms)/referenceform.cs

503 lines
22 KiB
C#
Raw Normal View History

2024-08-08 13:09:34 +06:00
using System;
using System.Windows.Forms;
using nsoftware.SecureBlackbox;
using System.Text;
namespace nsoftware.Demos
{
/// <summary>
/// Summary description for ReferenceForm.
/// </summary>
public class ReferenceForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Label lbReferenceId;
private System.Windows.Forms.TextBox edReferenceId;
private System.Windows.Forms.Label lbHashAlgorithm;
private System.Windows.Forms.ComboBox cbHashAlgorithm;
private System.Windows.Forms.TextBox edTargetXMLElement;
private System.Windows.Forms.TextBox mmData;
private System.Windows.Forms.Button btnOK;
private System.Windows.Forms.Button btnCancel;
private Label lbReferenceType;
private TextBox edReferenceType;
private CheckBox cbAutoGenerateElementId;
private Label lbCanonTransform;
private ComboBox cbCanonMethod;
private CheckBox cbUseEnvelopedSignatureTransform;
private TextBox edReferenceURI;
private Label lbReferenceURI;
private RadioButton rbTargetXMLElement;
private RadioButton rbTargetData;
private GroupBox groupBox1;
private GroupBox groupBox2;
private TextBox edCustomId;
private Label lbCustomId;
private Label label3;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public ReferenceForm()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.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()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ReferenceForm));
this.lbReferenceId = new System.Windows.Forms.Label();
this.edReferenceId = new System.Windows.Forms.TextBox();
this.lbHashAlgorithm = new System.Windows.Forms.Label();
this.cbHashAlgorithm = new System.Windows.Forms.ComboBox();
this.edTargetXMLElement = new System.Windows.Forms.TextBox();
this.mmData = new System.Windows.Forms.TextBox();
this.btnOK = new System.Windows.Forms.Button();
this.btnCancel = new System.Windows.Forms.Button();
this.lbReferenceType = new System.Windows.Forms.Label();
this.edReferenceType = new System.Windows.Forms.TextBox();
this.cbAutoGenerateElementId = new System.Windows.Forms.CheckBox();
this.lbCanonTransform = new System.Windows.Forms.Label();
this.cbCanonMethod = new System.Windows.Forms.ComboBox();
this.cbUseEnvelopedSignatureTransform = new System.Windows.Forms.CheckBox();
this.edReferenceURI = new System.Windows.Forms.TextBox();
this.lbReferenceURI = new System.Windows.Forms.Label();
this.rbTargetXMLElement = new System.Windows.Forms.RadioButton();
this.rbTargetData = new System.Windows.Forms.RadioButton();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.label3 = new System.Windows.Forms.Label();
this.edCustomId = new System.Windows.Forms.TextBox();
this.lbCustomId = new System.Windows.Forms.Label();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.SuspendLayout();
//
// lbReferenceId
//
this.lbReferenceId.AutoSize = true;
this.lbReferenceId.Location = new System.Drawing.Point(9, 15);
this.lbReferenceId.Name = "lbReferenceId";
this.lbReferenceId.Size = new System.Drawing.Size(72, 13);
this.lbReferenceId.TabIndex = 0;
this.lbReferenceId.Text = "Reference Id:";
//
// edReferenceId
//
this.edReferenceId.Location = new System.Drawing.Point(128, 12);
this.edReferenceId.Name = "edReferenceId";
this.edReferenceId.Size = new System.Drawing.Size(185, 20);
this.edReferenceId.TabIndex = 1;
//
// lbHashAlgorithm
//
this.lbHashAlgorithm.AutoSize = true;
this.lbHashAlgorithm.Location = new System.Drawing.Point(9, 103);
this.lbHashAlgorithm.Name = "lbHashAlgorithm";
this.lbHashAlgorithm.Size = new System.Drawing.Size(81, 13);
this.lbHashAlgorithm.TabIndex = 2;
this.lbHashAlgorithm.Text = "Hash Algorithm:";
//
// cbHashAlgorithm
//
this.cbHashAlgorithm.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cbHashAlgorithm.Items.AddRange(new object[] {
"MD5",
"SHA1",
"SHA224",
"SHA256",
"SHA384",
"SHA512",
"RIPEMD160"});
this.cbHashAlgorithm.Location = new System.Drawing.Point(128, 100);
this.cbHashAlgorithm.Name = "cbHashAlgorithm";
this.cbHashAlgorithm.Size = new System.Drawing.Size(185, 21);
this.cbHashAlgorithm.TabIndex = 3;
//
// edTargetXMLElement
//
this.edTargetXMLElement.Location = new System.Drawing.Point(12, 45);
this.edTargetXMLElement.Name = "edTargetXMLElement";
this.edTargetXMLElement.Size = new System.Drawing.Size(291, 20);
this.edTargetXMLElement.TabIndex = 5;
this.edTargetXMLElement.TextChanged += new System.EventHandler(this.edTargetXMLElement_TextChanged);
//
// mmData
//
this.mmData.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.mmData.Location = new System.Drawing.Point(11, 237);
this.mmData.Multiline = true;
this.mmData.Name = "mmData";
this.mmData.Size = new System.Drawing.Size(292, 85);
this.mmData.TabIndex = 11;
this.mmData.TextChanged += new System.EventHandler(this.mmData_TextChanged);
//
// btnOK
//
this.btnOK.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK;
this.btnOK.Location = new System.Drawing.Point(173, 567);
this.btnOK.Name = "btnOK";
this.btnOK.Size = new System.Drawing.Size(75, 25);
this.btnOK.TabIndex = 13;
this.btnOK.Text = "OK";
//
// btnCancel
//
this.btnCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.btnCancel.Location = new System.Drawing.Point(254, 567);
this.btnCancel.Name = "btnCancel";
this.btnCancel.Size = new System.Drawing.Size(75, 25);
this.btnCancel.TabIndex = 14;
this.btnCancel.Text = "Cancel";
//
// lbReferenceType
//
this.lbReferenceType.AutoSize = true;
this.lbReferenceType.Location = new System.Drawing.Point(9, 43);
this.lbReferenceType.Name = "lbReferenceType";
this.lbReferenceType.Size = new System.Drawing.Size(87, 13);
this.lbReferenceType.TabIndex = 17;
this.lbReferenceType.Text = "Reference Type:";
//
// edReferenceType
//
this.edReferenceType.Location = new System.Drawing.Point(128, 40);
this.edReferenceType.Name = "edReferenceType";
this.edReferenceType.Size = new System.Drawing.Size(185, 20);
this.edReferenceType.TabIndex = 18;
//
// cbAutoGenerateElementId
//
this.cbAutoGenerateElementId.AutoSize = true;
this.cbAutoGenerateElementId.Location = new System.Drawing.Point(16, 100);
this.cbAutoGenerateElementId.Name = "cbAutoGenerateElementId";
this.cbAutoGenerateElementId.Size = new System.Drawing.Size(175, 17);
this.cbAutoGenerateElementId.TabIndex = 22;
this.cbAutoGenerateElementId.Text = "Auto generate target element Id";
this.cbAutoGenerateElementId.UseVisualStyleBackColor = true;
//
// lbCanonTransform
//
this.lbCanonTransform.Location = new System.Drawing.Point(9, 28);
this.lbCanonTransform.Name = "lbCanonTransform";
this.lbCanonTransform.Size = new System.Drawing.Size(102, 27);
this.lbCanonTransform.TabIndex = 23;
this.lbCanonTransform.Text = "Canonicalization transform:";
//
// cbCanonMethod
//
this.cbCanonMethod.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cbCanonMethod.Items.AddRange(new object[] {
"Canonical",
"Canonical with comments",
"Canonical v1.1",
"Canonical with comments v1.1",
"Exclusive canonical",
"Exclusive canonical with comments",
"Minimal canonical",
"None"});
this.cbCanonMethod.Location = new System.Drawing.Point(117, 34);
this.cbCanonMethod.Name = "cbCanonMethod";
this.cbCanonMethod.Size = new System.Drawing.Size(185, 21);
this.cbCanonMethod.TabIndex = 24;
//
// cbUseEnvelopedSignatureTransform
//
this.cbUseEnvelopedSignatureTransform.AutoSize = true;
this.cbUseEnvelopedSignatureTransform.Location = new System.Drawing.Point(12, 65);
this.cbUseEnvelopedSignatureTransform.Name = "cbUseEnvelopedSignatureTransform";
this.cbUseEnvelopedSignatureTransform.Size = new System.Drawing.Size(190, 17);
this.cbUseEnvelopedSignatureTransform.TabIndex = 25;
this.cbUseEnvelopedSignatureTransform.Text = "Use enveloped signature transform";
this.cbUseEnvelopedSignatureTransform.UseVisualStyleBackColor = true;
//
// edReferenceURI
//
this.edReferenceURI.Location = new System.Drawing.Point(128, 70);
this.edReferenceURI.Name = "edReferenceURI";
this.edReferenceURI.Size = new System.Drawing.Size(185, 20);
this.edReferenceURI.TabIndex = 27;
//
// lbReferenceURI
//
this.lbReferenceURI.AutoSize = true;
this.lbReferenceURI.Location = new System.Drawing.Point(9, 73);
this.lbReferenceURI.Name = "lbReferenceURI";
this.lbReferenceURI.Size = new System.Drawing.Size(82, 13);
this.lbReferenceURI.TabIndex = 26;
this.lbReferenceURI.Text = "Reference URI:";
//
// rbTargetXMLElement
//
this.rbTargetXMLElement.AutoSize = true;
this.rbTargetXMLElement.Location = new System.Drawing.Point(11, 22);
this.rbTargetXMLElement.Name = "rbTargetXMLElement";
this.rbTargetXMLElement.Size = new System.Drawing.Size(122, 17);
this.rbTargetXMLElement.TabIndex = 28;
this.rbTargetXMLElement.TabStop = true;
this.rbTargetXMLElement.Text = "Target XML Element";
this.rbTargetXMLElement.UseVisualStyleBackColor = true;
//
// rbTargetData
//
this.rbTargetData.AutoSize = true;
this.rbTargetData.Location = new System.Drawing.Point(11, 214);
this.rbTargetData.Name = "rbTargetData";
this.rbTargetData.Size = new System.Drawing.Size(82, 17);
this.rbTargetData.TabIndex = 29;
this.rbTargetData.TabStop = true;
this.rbTargetData.Text = "Target Data";
this.rbTargetData.UseVisualStyleBackColor = true;
//
// groupBox1
//
this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)));
this.groupBox1.Controls.Add(this.label3);
this.groupBox1.Controls.Add(this.edCustomId);
this.groupBox1.Controls.Add(this.lbCustomId);
this.groupBox1.Controls.Add(this.rbTargetData);
this.groupBox1.Controls.Add(this.edTargetXMLElement);
this.groupBox1.Controls.Add(this.rbTargetXMLElement);
this.groupBox1.Controls.Add(this.mmData);
this.groupBox1.Controls.Add(this.cbAutoGenerateElementId);
this.groupBox1.Location = new System.Drawing.Point(10, 122);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(317, 332);
this.groupBox1.TabIndex = 30;
this.groupBox1.TabStop = false;
//
// label3
//
this.label3.ForeColor = System.Drawing.SystemColors.Highlight;
this.label3.Location = new System.Drawing.Point(9, 120);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(293, 91);
this.label3.TabIndex = 32;
this.label3.Text = resources.GetString("label3.Text");
//
// edCustomId
//
this.edCustomId.Location = new System.Drawing.Point(118, 74);
this.edCustomId.Name = "edCustomId";
this.edCustomId.Size = new System.Drawing.Size(185, 20);
this.edCustomId.TabIndex = 31;
//
// lbCustomId
//
this.lbCustomId.AutoSize = true;
this.lbCustomId.Location = new System.Drawing.Point(13, 77);
this.lbCustomId.Name = "lbCustomId";
this.lbCustomId.Size = new System.Drawing.Size(57, 13);
this.lbCustomId.TabIndex = 30;
this.lbCustomId.Text = "Custom Id:";
//
// groupBox2
//
this.groupBox2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.groupBox2.Controls.Add(this.cbCanonMethod);
this.groupBox2.Controls.Add(this.lbCanonTransform);
this.groupBox2.Controls.Add(this.cbUseEnvelopedSignatureTransform);
this.groupBox2.Location = new System.Drawing.Point(11, 460);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(316, 96);
this.groupBox2.TabIndex = 31;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "Transforms";
//
// ReferenceForm
//
this.AcceptButton = this.btnOK;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.CancelButton = this.btnCancel;
this.ClientSize = new System.Drawing.Size(339, 602);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.edReferenceURI);
this.Controls.Add(this.lbReferenceURI);
this.Controls.Add(this.edReferenceType);
this.Controls.Add(this.lbReferenceType);
this.Controls.Add(this.btnCancel);
this.Controls.Add(this.btnOK);
this.Controls.Add(this.edReferenceId);
this.Controls.Add(this.cbHashAlgorithm);
this.Controls.Add(this.lbHashAlgorithm);
this.Controls.Add(this.lbReferenceId);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "ReferenceForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "Reference Options";
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.groupBox2.ResumeLayout(false);
this.groupBox2.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
public void Initialize(XMLReference reference)
{
if (reference != null)
{
edReferenceId.Text = reference.ID;
edReferenceType.Text = reference.ReferenceType;
edReferenceURI.Text = reference.URI;
for (int i = 0; i < cbHashAlgorithm.Items.Count; i++)
{
if (cbHashAlgorithm.Items[i].Equals(reference.HashAlgorithm))
{
cbHashAlgorithm.SelectedIndex = i;
break;
}
}
edTargetXMLElement.Text = reference.TargetXMLElement;
edCustomId.Text = reference.CustomElementId;
cbAutoGenerateElementId.Checked = reference.AutoGenerateElementId;
mmData.Text = Encoding.UTF8.GetString(reference.TargetData);
if (!string.IsNullOrEmpty(reference.TargetXMLElement) || (reference.TargetData == null) || (reference.TargetData.Length == 0))
rbTargetXMLElement.Checked = true;
else
rbTargetData.Checked = true;
switch (reference.CanonicalizationMethod)
{
case XMLCanonicalizationMethods.cxcmCanon:
cbCanonMethod.SelectedIndex = 0;
break;
case XMLCanonicalizationMethods.cxcmCanonComment:
cbCanonMethod.SelectedIndex = 1;
break;
case XMLCanonicalizationMethods.cxcmCanon_v1_1:
cbCanonMethod.SelectedIndex = 2;
break;
case XMLCanonicalizationMethods.cxcmCanonComment_v1_1:
cbCanonMethod.SelectedIndex = 3;
break;
case XMLCanonicalizationMethods.cxcmExclCanon:
cbCanonMethod.SelectedIndex = 4;
break;
case XMLCanonicalizationMethods.cxcmExclCanonComment:
cbCanonMethod.SelectedIndex = 5;
break;
case XMLCanonicalizationMethods.cxcmMinCanon:
cbCanonMethod.SelectedIndex = 6;
break;
case XMLCanonicalizationMethods.cxcmNone:
cbCanonMethod.SelectedIndex = 7;
break;
default:
cbCanonMethod.SelectedIndex = 0;
break;
}
cbUseEnvelopedSignatureTransform.Checked = reference.UseEnvelopedSignatureTransform;
}
}
public void UpdateReference(XMLReference reference)
{
if (reference == null)
return;
reference.ID = edReferenceId.Text;
reference.ReferenceType = edReferenceType.Text;
reference.URI = edReferenceURI.Text;
reference.HashAlgorithm = cbHashAlgorithm.Text;
if (rbTargetXMLElement.Checked)
{
reference.TargetXMLElement = edTargetXMLElement.Text;
reference.CustomElementId = edCustomId.Text;
reference.AutoGenerateElementId = cbAutoGenerateElementId.Checked;
}
if (rbTargetData.Checked)
reference.TargetData = Encoding.UTF8.GetBytes(mmData.Text);
switch (cbCanonMethod.SelectedIndex)
{
case 0:
reference.CanonicalizationMethod = XMLCanonicalizationMethods.cxcmCanon;
break;
case 1:
reference.CanonicalizationMethod = XMLCanonicalizationMethods.cxcmCanonComment;
break;
case 2:
reference.CanonicalizationMethod = XMLCanonicalizationMethods.cxcmCanon_v1_1;
break;
case 3:
reference.CanonicalizationMethod = XMLCanonicalizationMethods.cxcmCanonComment_v1_1;
break;
case 4:
reference.CanonicalizationMethod = XMLCanonicalizationMethods.cxcmExclCanon;
break;
case 5:
reference.CanonicalizationMethod = XMLCanonicalizationMethods.cxcmExclCanonComment;
break;
case 6:
reference.CanonicalizationMethod = XMLCanonicalizationMethods.cxcmMinCanon;
break;
case 7:
reference.CanonicalizationMethod = XMLCanonicalizationMethods.cxcmNone;
break;
default:
reference.CanonicalizationMethod = XMLCanonicalizationMethods.cxcmCanon;
break;
}
reference.UseEnvelopedSignatureTransform = cbUseEnvelopedSignatureTransform.Checked;
}
private void mmData_TextChanged(object sender, EventArgs e)
{
rbTargetData.Checked = true;
}
private void edTargetXMLElement_TextChanged(object sender, EventArgs e)
{
rbTargetXMLElement.Checked = true;
}
}
}