securebb-win-demos/XML Decryptor (WinForms)/decryptprops.cs

387 lines
15 KiB
C#
Raw Normal View History

2024-08-08 13:09:34 +06:00
using System;
using System.Windows.Forms;
using nsoftware.SecureBlackbox;
/// <summary>
/// Summary description for SignForm.
/// </summary>
public class DecryptPropsForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btnCancel;
private System.Windows.Forms.Button btnOK;
private System.Windows.Forms.OpenFileDialog dlgOpen;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private GroupBox gbGeneralEnc;
private GroupBox gbKeyInfo;
private Label lbCertificate;
private Button btnBrowseCert;
private TextBox edCertPassword;
private TextBox edCert;
private Label lbCertPassword;
private TextBox edPassphrase;
private Label lbPassphrase;
private Label label1;
private TextBox textInfo;
private XMLDecryptor decryptor = null;
public DecryptPropsForm(XMLDecryptor _decryptor)
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
decryptor = _decryptor;
String t = "";
if (_decryptor.UseGCM)
t = "-GCM";
String s = "Encryption method: " + _decryptor.EncryptionMethod + t + Environment.NewLine;
if (_decryptor.EncryptedDataType == XMLDecryptorEncryptedDataTypes.cxedtElement)
t = "Element";
else if (_decryptor.EncryptedDataType == XMLDecryptorEncryptedDataTypes.cxedtContent)
t = "Content";
else
t = "External";
s = s + "Encrypted data type: " + t + Environment.NewLine;
bool certRequired = false;
if (_decryptor.EncryptKey)
{
s = s + "EncryptKey: true" + Environment.NewLine;
if (_decryptor.KeyEncryptionType == XMLDecryptorKeyEncryptionTypes.cxetKeyTransport)
{
s = s + "Key encryption type: transport" + Environment.NewLine;
if (_decryptor.KeyTransportMethod == XMLDecryptorKeyTransportMethods.cxktRSA15)
t = "RSA v1.5";
else
t = "RSA-OAEP";
s = s + "Key transport method: " + t + Environment.NewLine;
certRequired = true;
}
else
{
s = s + "Key encryption type: wrap" + Environment.NewLine;
s = s + "Key wrap method: " + _decryptor.KeyWrapMethod + Environment.NewLine;
}
}
else
s = s + "EncryptKey: false" + Environment.NewLine;
t = _decryptor.Config("KeyName");
if (t.Length > 0)
s = s + "Key name: " + t + Environment.NewLine;
t = _decryptor.Config("MimeType");
if (t.Length > 0)
s = s + "Mime type: " + t + Environment.NewLine;
textInfo.Text = s;
edPassphrase.Enabled = !certRequired;
lbPassphrase.Enabled = !certRequired;
edCert.Enabled = certRequired;
lbCertificate.Enabled = certRequired;
edCertPassword.Enabled = certRequired;
lbCertPassword.Enabled = certRequired;
btnBrowseCert.Enabled = certRequired;
}
/// <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()
{
this.btnCancel = new System.Windows.Forms.Button();
this.btnOK = new System.Windows.Forms.Button();
this.dlgOpen = new System.Windows.Forms.OpenFileDialog();
this.gbGeneralEnc = new System.Windows.Forms.GroupBox();
this.textInfo = new System.Windows.Forms.TextBox();
this.gbKeyInfo = new System.Windows.Forms.GroupBox();
this.lbCertificate = new System.Windows.Forms.Label();
this.btnBrowseCert = new System.Windows.Forms.Button();
this.edCertPassword = new System.Windows.Forms.TextBox();
this.edCert = new System.Windows.Forms.TextBox();
this.lbCertPassword = new System.Windows.Forms.Label();
this.edPassphrase = new System.Windows.Forms.TextBox();
this.lbPassphrase = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.gbGeneralEnc.SuspendLayout();
this.gbKeyInfo.SuspendLayout();
this.SuspendLayout();
//
// 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(272, 329);
this.btnCancel.Name = "btnCancel";
this.btnCancel.Size = new System.Drawing.Size(75, 25);
this.btnCancel.TabIndex = 7;
this.btnCancel.Text = "Cancel";
//
// btnOK
//
this.btnOK.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.btnOK.Location = new System.Drawing.Point(185, 329);
this.btnOK.Name = "btnOK";
this.btnOK.Size = new System.Drawing.Size(75, 25);
this.btnOK.TabIndex = 6;
this.btnOK.Text = "OK";
this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
//
// gbGeneralEnc
//
this.gbGeneralEnc.Controls.Add(this.textInfo);
this.gbGeneralEnc.Controls.Add(this.gbKeyInfo);
this.gbGeneralEnc.Location = new System.Drawing.Point(7, 55);
this.gbGeneralEnc.Name = "gbGeneralEnc";
this.gbGeneralEnc.Size = new System.Drawing.Size(340, 265);
this.gbGeneralEnc.TabIndex = 18;
this.gbGeneralEnc.TabStop = false;
this.gbGeneralEnc.Text = "General";
//
// textInfo
//
this.textInfo.Location = new System.Drawing.Point(8, 16);
this.textInfo.Multiline = true;
this.textInfo.Name = "textInfo";
this.textInfo.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.textInfo.Size = new System.Drawing.Size(325, 83);
this.textInfo.TabIndex = 19;
//
// gbKeyInfo
//
this.gbKeyInfo.Controls.Add(this.lbCertificate);
this.gbKeyInfo.Controls.Add(this.btnBrowseCert);
this.gbKeyInfo.Controls.Add(this.edCertPassword);
this.gbKeyInfo.Controls.Add(this.edCert);
this.gbKeyInfo.Controls.Add(this.lbCertPassword);
this.gbKeyInfo.Controls.Add(this.edPassphrase);
this.gbKeyInfo.Controls.Add(this.lbPassphrase);
this.gbKeyInfo.Location = new System.Drawing.Point(8, 105);
this.gbKeyInfo.Name = "gbKeyInfo";
this.gbKeyInfo.Size = new System.Drawing.Size(325, 150);
this.gbKeyInfo.TabIndex = 16;
this.gbKeyInfo.TabStop = false;
this.gbKeyInfo.Text = "Key/certificate properties ";
//
// lbCertificate
//
this.lbCertificate.AutoSize = true;
this.lbCertificate.Location = new System.Drawing.Point(7, 75);
this.lbCertificate.Name = "lbCertificate";
this.lbCertificate.Size = new System.Drawing.Size(109, 13);
this.lbCertificate.TabIndex = 65;
this.lbCertificate.Text = "Encryption certificate:";
//
// btnBrowseCert
//
this.btnBrowseCert.Location = new System.Drawing.Point(244, 89);
this.btnBrowseCert.Name = "btnBrowseCert";
this.btnBrowseCert.Size = new System.Drawing.Size(75, 25);
this.btnBrowseCert.TabIndex = 64;
this.btnBrowseCert.Text = "Browse ...";
this.btnBrowseCert.Click += new System.EventHandler(this.btnBrowseCert_Click);
//
// edCertPassword
//
this.edCertPassword.Location = new System.Drawing.Point(118, 123);
this.edCertPassword.Name = "edCertPassword";
this.edCertPassword.PasswordChar = '*';
this.edCertPassword.Size = new System.Drawing.Size(201, 20);
this.edCertPassword.TabIndex = 63;
//
// edCert
//
this.edCert.Location = new System.Drawing.Point(10, 92);
this.edCert.Name = "edCert";
this.edCert.Size = new System.Drawing.Size(228, 20);
this.edCert.TabIndex = 62;
//
// lbCertPassword
//
this.lbCertPassword.AutoSize = true;
this.lbCertPassword.Location = new System.Drawing.Point(7, 126);
this.lbCertPassword.Name = "lbCertPassword";
this.lbCertPassword.Size = new System.Drawing.Size(105, 13);
this.lbCertPassword.TabIndex = 61;
this.lbCertPassword.Text = "Certificate password:";
//
// edPassphrase
//
this.edPassphrase.Location = new System.Drawing.Point(10, 41);
this.edPassphrase.Name = "edPassphrase";
this.edPassphrase.Size = new System.Drawing.Size(309, 20);
this.edPassphrase.TabIndex = 1;
//
// lbPassphrase
//
this.lbPassphrase.AutoSize = true;
this.lbPassphrase.Location = new System.Drawing.Point(7, 25);
this.lbPassphrase.Name = "lbPassphrase";
this.lbPassphrase.Size = new System.Drawing.Size(79, 13);
this.lbPassphrase.TabIndex = 0;
this.lbPassphrase.Text = "Key/Password:";
//
// label1
//
this.label1.AutoSize = true;
this.label1.ForeColor = System.Drawing.SystemColors.Highlight;
this.label1.Location = new System.Drawing.Point(4, 9);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(319, 39);
this.label1.TabIndex = 22;
this.label1.Text = "The XML document is encrypted. Depending on the document\'s \r\nencryption type you " +
"would need to provide a decryption password \r\nor decryption certificate.";
//
// DecryptPropsForm
//
this.AcceptButton = this.btnOK;
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.CancelButton = this.btnCancel;
this.ClientSize = new System.Drawing.Size(354, 361);
this.Controls.Add(this.label1);
this.Controls.Add(this.gbGeneralEnc);
this.Controls.Add(this.btnCancel);
this.Controls.Add(this.btnOK);
this.Cursor = System.Windows.Forms.Cursors.Default;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "DecryptPropsForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "Encryption properties";
this.gbGeneralEnc.ResumeLayout(false);
this.gbGeneralEnc.PerformLayout();
this.gbKeyInfo.ResumeLayout(false);
this.gbKeyInfo.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
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 byte[] getKey(string algorithm)
{
int len = 0;
if (algorithm.Equals("AES128", StringComparison.OrdinalIgnoreCase))
len = 16;
else if (algorithm.Equals("AES192", StringComparison.OrdinalIgnoreCase))
len = 24;
else if (algorithm.Equals("AES256", StringComparison.OrdinalIgnoreCase))
len = 32;
else if (algorithm.Equals("Camellia128", StringComparison.OrdinalIgnoreCase))
len = 16;
else if (algorithm.Equals("Camellia192", StringComparison.OrdinalIgnoreCase))
len = 24;
else if (algorithm.Equals("Camellia256", StringComparison.OrdinalIgnoreCase))
len = 32;
else if (algorithm.Equals("DES", StringComparison.OrdinalIgnoreCase))
len = 8;
else if (algorithm.Equals("3DES", StringComparison.OrdinalIgnoreCase))
len = 24;
else if (algorithm.Equals("RC4", StringComparison.OrdinalIgnoreCase))
len = 16;
else if (algorithm.Equals("SEED", StringComparison.OrdinalIgnoreCase))
len = 16;
// simple key derivation function from a Passphrase
// TODO: replace with SHA256 hash or KDF
string s = edPassphrase.Text;
while (s.Length < len)
s = s + "/" + edPassphrase.Text;
byte[] res = new byte[len];
for (int i = 0; i < len; i++)
res[i] = (byte)s[i];
return res;
}
private void btnBrowseCert_Click(object sender, EventArgs e)
{
dlgOpen.Title = "Select certificate file";
dlgOpen.Filter = "PEM-encoded certificate (*.pem)|*.PEM|DER-encoded certificate (*.cer)|*.CER|PFX-encoded certificate (*.pfx)|*.PFX";
if (dlgOpen.ShowDialog() == DialogResult.OK)
edCert.Text = dlgOpen.FileName;
}
private void btnOK_Click(object sender, EventArgs e)
{
if (decryptor.EncryptKey)
{
if (decryptor.KeyEncryptionType == XMLDecryptorKeyEncryptionTypes.cxetKeyTransport)
{
try
{
decryptor.KeyDecryptionCertificate = LoadCertificate(edCert.Text, edCertPassword.Text);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else
{
decryptor.KeyDecryptionKey = getKey(decryptor.KeyWrapMethod);
}
}
else
{
decryptor.DecryptionKey = getKey(decryptor.EncryptionMethod);
}
this.DialogResult = DialogResult.OK;
}
}