From 6c3e3eaf5e985830a49d2c1250a5a12b1debd001 Mon Sep 17 00:00:00 2001 From: alcexhim Date: Fri, 17 Apr 2015 13:03:17 -0400 Subject: [PATCH] Begin to implement RSA key DataFormat/ObjectModel --- .../Dialogs/DocumentPropertiesDialog.cs | 11 +- .../Security/Key/RSA/RSAKeyAlgorithm.cs | 13 ++ .../Security/Key/RSA/RSAKeyDataFormat.cs | 128 ++++++++++++++++++ .../Security/Key/RSA/RSAKeyType.cs | 18 +++ .../Security/Key/RSA/RSAKeyObjectModel.cs | 82 +++++++++++ .../UniversalEditor.Essential.csproj | 4 + .../Include/Objects/000-Objectify.inc.php | 0 .../Include/Objects/DataCenter.inc.php | 0 .../Include/Objects/DataType.inc.php | 0 .../Include/Objects/Language.inc.php | 0 .../Include/Objects/Module.inc.php | 0 .../Include/Objects/ModulePage.inc.php | 0 .../Objects/MultipleInstanceProperty.inc.php | 0 .../Include/Objects/PaymentPlan.inc.php | 0 .../Objects/SingleInstanceProperty.inc.php | 0 .../Include/Objects/Tenant.inc.php | 0 .../Include/Objects/TenantObject.inc.php | 0 .../Objects/TenantObjectInstance.inc.php | 0 .../Objects/TenantObjectMethod.inc.php | 0 .../Objects/TenantObjectProperty.inc.php | 0 .../Include/Objects/TenantProperty.inc.php | 0 .../Objects/TenantQueryParameter.inc.php | 0 .../Objects/TenantStringTableEntry.inc.php | 0 .../Include/Objects/TenantType.inc.php | 0 24 files changed, 253 insertions(+), 3 deletions(-) create mode 100644 CSharp/Plugins/UniversalEditor.Essential/DataFormats/Security/Key/RSA/RSAKeyAlgorithm.cs create mode 100644 CSharp/Plugins/UniversalEditor.Essential/DataFormats/Security/Key/RSA/RSAKeyDataFormat.cs create mode 100644 CSharp/Plugins/UniversalEditor.Essential/DataFormats/Security/Key/RSA/RSAKeyType.cs create mode 100644 CSharp/Plugins/UniversalEditor.Essential/ObjectModels/Security/Key/RSA/RSAKeyObjectModel.cs rename PHP/{Common => }/Include/Objects/000-Objectify.inc.php (100%) rename PHP/{Common => }/Include/Objects/DataCenter.inc.php (100%) rename PHP/{Common => }/Include/Objects/DataType.inc.php (100%) rename PHP/{Common => }/Include/Objects/Language.inc.php (100%) rename PHP/{Common => }/Include/Objects/Module.inc.php (100%) rename PHP/{Common => }/Include/Objects/ModulePage.inc.php (100%) rename PHP/{Common => }/Include/Objects/MultipleInstanceProperty.inc.php (100%) rename PHP/{Common => }/Include/Objects/PaymentPlan.inc.php (100%) rename PHP/{Common => }/Include/Objects/SingleInstanceProperty.inc.php (100%) rename PHP/{Common => }/Include/Objects/Tenant.inc.php (100%) rename PHP/{Common => }/Include/Objects/TenantObject.inc.php (100%) rename PHP/{Common => }/Include/Objects/TenantObjectInstance.inc.php (100%) rename PHP/{Common => }/Include/Objects/TenantObjectMethod.inc.php (100%) rename PHP/{Common => }/Include/Objects/TenantObjectProperty.inc.php (100%) rename PHP/{Common => }/Include/Objects/TenantProperty.inc.php (100%) rename PHP/{Common => }/Include/Objects/TenantQueryParameter.inc.php (100%) rename PHP/{Common => }/Include/Objects/TenantStringTableEntry.inc.php (100%) rename PHP/{Common => }/Include/Objects/TenantType.inc.php (100%) diff --git a/CSharp/Engines/WindowsForms/Engines/UniversalEditor.UserInterface.WindowsForms.DesktopApplication/Dialogs/DocumentPropertiesDialog.cs b/CSharp/Engines/WindowsForms/Engines/UniversalEditor.UserInterface.WindowsForms.DesktopApplication/Dialogs/DocumentPropertiesDialog.cs index 4165ef92..ea637bd6 100644 --- a/CSharp/Engines/WindowsForms/Engines/UniversalEditor.UserInterface.WindowsForms.DesktopApplication/Dialogs/DocumentPropertiesDialog.cs +++ b/CSharp/Engines/WindowsForms/Engines/UniversalEditor.UserInterface.WindowsForms.DesktopApplication/Dialogs/DocumentPropertiesDialog.cs @@ -177,14 +177,14 @@ namespace UniversalEditor.UserInterface.WindowsForms.Dialogs private void dlgAccessor_SelectionChanged(object sender, EventArgs e) { GenericBrowserPopup dlg = (sender as GenericBrowserPopup); - mvarAccessor = dlg.SelectedObject; + Accessor acc = dlg.SelectedObject; dlg.AutoClose = false; switch (mvarMode) { case DocumentPropertiesDialogMode.Open: { - if (!Engine.CurrentEngine.ShowCustomOptionDialog(ref mvarAccessor, CustomOptionDialogType.Import)) + if (!Engine.CurrentEngine.ShowCustomOptionDialog(ref acc, CustomOptionDialogType.Import)) { return; } @@ -192,7 +192,7 @@ namespace UniversalEditor.UserInterface.WindowsForms.Dialogs } case DocumentPropertiesDialogMode.Save: { - if (!Engine.CurrentEngine.ShowCustomOptionDialog(ref mvarAccessor, CustomOptionDialogType.Export)) + if (!Engine.CurrentEngine.ShowCustomOptionDialog(ref acc, CustomOptionDialogType.Export)) { return; } @@ -200,6 +200,7 @@ namespace UniversalEditor.UserInterface.WindowsForms.Dialogs } } + mvarAccessor = acc; RefreshButtons(); dlg.AutoClose = true; @@ -308,6 +309,10 @@ namespace UniversalEditor.UserInterface.WindowsForms.Dialogs { txtAccessor.Text = mvarAccessor.ToString(); } + else + { + txtAccessor.Text = String.Empty; + } if (mvarDataFormat != null) { DataFormatReference dfr = mvarDataFormat.MakeReference(); diff --git a/CSharp/Plugins/UniversalEditor.Essential/DataFormats/Security/Key/RSA/RSAKeyAlgorithm.cs b/CSharp/Plugins/UniversalEditor.Essential/DataFormats/Security/Key/RSA/RSAKeyAlgorithm.cs new file mode 100644 index 00000000..e2e23953 --- /dev/null +++ b/CSharp/Plugins/UniversalEditor.Essential/DataFormats/Security/Key/RSA/RSAKeyAlgorithm.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace UniversalEditor.DataFormats.Security.Key.RSA +{ + public enum RSAKeyAlgorithm : uint + { + KeyX = 0x0000A400, + Sign = 0x00002400 + } +} diff --git a/CSharp/Plugins/UniversalEditor.Essential/DataFormats/Security/Key/RSA/RSAKeyDataFormat.cs b/CSharp/Plugins/UniversalEditor.Essential/DataFormats/Security/Key/RSA/RSAKeyDataFormat.cs new file mode 100644 index 00000000..a7e23fde --- /dev/null +++ b/CSharp/Plugins/UniversalEditor.Essential/DataFormats/Security/Key/RSA/RSAKeyDataFormat.cs @@ -0,0 +1,128 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using UniversalEditor.IO; +using UniversalEditor.ObjectModels.Security.Key.RSA; + +namespace UniversalEditor.DataFormats.Security.Key.RSA +{ + public class RSAKeyDataFormat : DataFormat + { + private static DataFormatReference _dfr = null; + protected override DataFormatReference MakeReferenceInternal() + { + if (_dfr == null) + { + _dfr = base.MakeReferenceInternal(); + _dfr.Capabilities.Add(typeof(RSAKeyObjectModel), DataFormatCapabilities.All); + } + return _dfr; + } + + private byte mvarFormatVersion = 0x02; + public byte FormatVersion { get { return mvarFormatVersion; } set { mvarFormatVersion = value; } } + + protected override void LoadInternal(ref ObjectModel objectModel) + { + RSAKeyObjectModel key = (objectModel as RSAKeyObjectModel); + if (key == null) throw new ObjectModelNotSupportedException(); + + Reader reader = base.Accessor.Reader; + + // ---- read the BLOBHEADER struct ------ + bool dotnetkey = false; + RSAKeyType btype = (RSAKeyType)reader.ReadByte(); + if (btype != RSAKeyType.PublicKeyBlob && btype != RSAKeyType.PrivateKeyBlob) + { + // possibly a .NET publickey + reader.Accessor.Seek(11, SeekOrigin.Current); // advance past 3 int headers, minus 1 byte read + btype = (RSAKeyType)reader.ReadByte(); + if (btype != RSAKeyType.PublicKeyBlob && btype != RSAKeyType.PrivateKeyBlob) + { + throw new InvalidDataFormatException("Key is neither a PublicKeyBlob nor a PrivateKeyBlob"); + } + dotnetkey = true; + } + + byte version = reader.ReadByte(); + if (version != mvarFormatVersion) + { + throw new InvalidDataFormatException("Invalid version for format " + version.ToString()); + } + + ushort reserved = reader.ReadUInt16(); + RSAKeyAlgorithm algorithm = (RSAKeyAlgorithm)reader.ReadUInt32(); + if (algorithm != RSAKeyAlgorithm.KeyX && algorithm != RSAKeyAlgorithm.Sign) + { + throw new InvalidDataFormatException("Unknown algorithm " + algorithm.ToString()); + } + + //---- read the RSAPUBKEY struct -------- + string magic = reader.ReadFixedLengthString(4); + if (magic != "RSA1" && magic != "RSA2") + { + throw new InvalidDataFormatException("File does not begin with 'RSA1' or 'RSA2'"); + } + int bitlen = reader.ReadInt32(); //get RSA bit length + key.BitLength = bitlen; + + //---- read RSA public exponent ------ + reader.Endianness = Endianness.BigEndian; + uint pubexp = reader.ReadUInt32(); //get public exponent + key.PublicExponent = pubexp; + + //---- read RSA modulus ----------- + //Reverse byte array for little-endian to big-endian conversion + byte[] RSAmodulus = reader.ReadBytes(bitlen / 8); + Array.Reverse(RSAmodulus); + key.Modulus = RSAmodulus; + + //-- if this is a valid unencrypted PRIVATEKEYBLOB, read RSA private key properties + if (btype == RSAKeyType.PrivateKeyBlob) + { + int bitlen16 = bitlen / 16; + byte[] P = reader.ReadBytes(bitlen16); + Array.Reverse(P); + if (P.Length != bitlen16) + throw new InvalidDataFormatException("Invalid bit length for P"); + key.P = P; + + byte[] Q = reader.ReadBytes(bitlen16); + Array.Reverse(Q); + if (Q.Length != bitlen16) + throw new InvalidDataFormatException("Invalid bit length for Q"); + key.Q = Q; + + byte[] DP = reader.ReadBytes(bitlen16); + Array.Reverse(DP); + if (DP.Length != bitlen16) + throw new InvalidDataFormatException("Invalid bit length for DP"); + key.DP = DP; + + byte[] DQ = reader.ReadBytes(bitlen16); + Array.Reverse(DQ); + if (DQ.Length != bitlen16) + throw new InvalidDataFormatException("Invalid bit length for DQ"); + key.DQ = DQ; + + byte[] IQ = reader.ReadBytes(bitlen16); + Array.Reverse(IQ); + if (IQ.Length != bitlen16) + throw new InvalidDataFormatException("Invalid bit length for IQ"); + key.IQ = IQ; + + byte[] D = reader.ReadBytes(bitlen / 8); + Array.Reverse(D); + if (D.Length != bitlen / 8) + throw new InvalidDataFormatException("Invalid bit length for D"); + key.D = D; + } + } + + protected override void SaveInternal(ObjectModel objectModel) + { + throw new NotImplementedException(); + } + } +} diff --git a/CSharp/Plugins/UniversalEditor.Essential/DataFormats/Security/Key/RSA/RSAKeyType.cs b/CSharp/Plugins/UniversalEditor.Essential/DataFormats/Security/Key/RSA/RSAKeyType.cs new file mode 100644 index 00000000..83ec9f09 --- /dev/null +++ b/CSharp/Plugins/UniversalEditor.Essential/DataFormats/Security/Key/RSA/RSAKeyType.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace UniversalEditor.DataFormats.Security.Key.RSA +{ + public enum RSAKeyType : byte + { + Simple = 0x01, + PublicKeyBlob = 0x06, + PrivateKeyBlob = 0x07, + PlainTextKeyBlob = 0x08, + OpaqueKeyBlob = 0x09, + PublicKeyBlobEx = 0x0A, + SymmetricWrapKeyBlob = 0x0B + } +} diff --git a/CSharp/Plugins/UniversalEditor.Essential/ObjectModels/Security/Key/RSA/RSAKeyObjectModel.cs b/CSharp/Plugins/UniversalEditor.Essential/ObjectModels/Security/Key/RSA/RSAKeyObjectModel.cs new file mode 100644 index 00000000..9c328c85 --- /dev/null +++ b/CSharp/Plugins/UniversalEditor.Essential/ObjectModels/Security/Key/RSA/RSAKeyObjectModel.cs @@ -0,0 +1,82 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace UniversalEditor.ObjectModels.Security.Key.RSA +{ + public class RSAKeyObjectModel : ObjectModel + { + private static ObjectModelReference _omr = null; + protected override ObjectModelReference MakeReferenceInternal() + { + if (_omr == null) + { + _omr = base.MakeReferenceInternal(); + _omr.Title = "RSA cryptography key"; + _omr.Path = new string[] { "Security", "Key" }; + } + return _omr; + } + + private byte[] mvarP = new byte[0]; + public byte[] P { get { return mvarP; } set { mvarP = value; } } + private byte[] mvarQ = new byte[0]; + public byte[] Q { get { return mvarQ; } set { mvarQ = value; } } + private byte[] mvarDP = new byte[0]; + public byte[] DP { get { return mvarDP; } set { mvarDP = value; } } + private byte[] mvarDQ = new byte[0]; + public byte[] DQ { get { return mvarDQ; } set { mvarDQ = value; } } + private byte[] mvarIQ = new byte[0]; + public byte[] IQ { get { return mvarIQ; } set { mvarIQ = value; } } + private byte[] mvarD = new byte[0]; + public byte[] D { get { return mvarD; } set { mvarD = value; } } + + private int mvarBitLength = 0; + /// + /// The bit length of the private key components. + /// + public int BitLength { get { return mvarBitLength; } set { mvarBitLength = value; } } + + private uint mvarPublicExponent = 0; + /// + /// The exponent of the public key. + /// + public uint PublicExponent { get { return mvarPublicExponent; } set { mvarPublicExponent = value; } } + + private byte[] mvarModulus = new byte[0]; + /// + /// The RSA modulus. + /// + public byte[] Modulus { get { return mvarModulus; } set { mvarModulus = value; } } + + public override void Clear() + { + mvarBitLength = 0; + mvarD = new byte[0]; + mvarDP = new byte[0]; + mvarDQ = new byte[0]; + mvarIQ = new byte[0]; + mvarModulus = new byte[0]; + mvarP = new byte[0]; + mvarPublicExponent = 0; + mvarQ = new byte[0]; + } + + public override void CopyTo(ObjectModel where) + { + RSAKeyObjectModel clone = (where as RSAKeyObjectModel); + if (clone == null) throw new ObjectModelNotSupportedException(); + + clone.BitLength = mvarBitLength; + clone.D = mvarD; + clone.DP = mvarDP; + clone.DQ = mvarDQ; + clone.IQ = mvarIQ; + clone.Modulus = mvarModulus; + clone.P = mvarP; + clone.PublicExponent = mvarPublicExponent; + clone.Q = mvarQ; + } + } +} diff --git a/CSharp/Plugins/UniversalEditor.Essential/UniversalEditor.Essential.csproj b/CSharp/Plugins/UniversalEditor.Essential/UniversalEditor.Essential.csproj index 4b07af31..56537537 100644 --- a/CSharp/Plugins/UniversalEditor.Essential/UniversalEditor.Essential.csproj +++ b/CSharp/Plugins/UniversalEditor.Essential/UniversalEditor.Essential.csproj @@ -62,6 +62,9 @@ + + + @@ -109,6 +112,7 @@ + diff --git a/PHP/Common/Include/Objects/000-Objectify.inc.php b/PHP/Include/Objects/000-Objectify.inc.php similarity index 100% rename from PHP/Common/Include/Objects/000-Objectify.inc.php rename to PHP/Include/Objects/000-Objectify.inc.php diff --git a/PHP/Common/Include/Objects/DataCenter.inc.php b/PHP/Include/Objects/DataCenter.inc.php similarity index 100% rename from PHP/Common/Include/Objects/DataCenter.inc.php rename to PHP/Include/Objects/DataCenter.inc.php diff --git a/PHP/Common/Include/Objects/DataType.inc.php b/PHP/Include/Objects/DataType.inc.php similarity index 100% rename from PHP/Common/Include/Objects/DataType.inc.php rename to PHP/Include/Objects/DataType.inc.php diff --git a/PHP/Common/Include/Objects/Language.inc.php b/PHP/Include/Objects/Language.inc.php similarity index 100% rename from PHP/Common/Include/Objects/Language.inc.php rename to PHP/Include/Objects/Language.inc.php diff --git a/PHP/Common/Include/Objects/Module.inc.php b/PHP/Include/Objects/Module.inc.php similarity index 100% rename from PHP/Common/Include/Objects/Module.inc.php rename to PHP/Include/Objects/Module.inc.php diff --git a/PHP/Common/Include/Objects/ModulePage.inc.php b/PHP/Include/Objects/ModulePage.inc.php similarity index 100% rename from PHP/Common/Include/Objects/ModulePage.inc.php rename to PHP/Include/Objects/ModulePage.inc.php diff --git a/PHP/Common/Include/Objects/MultipleInstanceProperty.inc.php b/PHP/Include/Objects/MultipleInstanceProperty.inc.php similarity index 100% rename from PHP/Common/Include/Objects/MultipleInstanceProperty.inc.php rename to PHP/Include/Objects/MultipleInstanceProperty.inc.php diff --git a/PHP/Common/Include/Objects/PaymentPlan.inc.php b/PHP/Include/Objects/PaymentPlan.inc.php similarity index 100% rename from PHP/Common/Include/Objects/PaymentPlan.inc.php rename to PHP/Include/Objects/PaymentPlan.inc.php diff --git a/PHP/Common/Include/Objects/SingleInstanceProperty.inc.php b/PHP/Include/Objects/SingleInstanceProperty.inc.php similarity index 100% rename from PHP/Common/Include/Objects/SingleInstanceProperty.inc.php rename to PHP/Include/Objects/SingleInstanceProperty.inc.php diff --git a/PHP/Common/Include/Objects/Tenant.inc.php b/PHP/Include/Objects/Tenant.inc.php similarity index 100% rename from PHP/Common/Include/Objects/Tenant.inc.php rename to PHP/Include/Objects/Tenant.inc.php diff --git a/PHP/Common/Include/Objects/TenantObject.inc.php b/PHP/Include/Objects/TenantObject.inc.php similarity index 100% rename from PHP/Common/Include/Objects/TenantObject.inc.php rename to PHP/Include/Objects/TenantObject.inc.php diff --git a/PHP/Common/Include/Objects/TenantObjectInstance.inc.php b/PHP/Include/Objects/TenantObjectInstance.inc.php similarity index 100% rename from PHP/Common/Include/Objects/TenantObjectInstance.inc.php rename to PHP/Include/Objects/TenantObjectInstance.inc.php diff --git a/PHP/Common/Include/Objects/TenantObjectMethod.inc.php b/PHP/Include/Objects/TenantObjectMethod.inc.php similarity index 100% rename from PHP/Common/Include/Objects/TenantObjectMethod.inc.php rename to PHP/Include/Objects/TenantObjectMethod.inc.php diff --git a/PHP/Common/Include/Objects/TenantObjectProperty.inc.php b/PHP/Include/Objects/TenantObjectProperty.inc.php similarity index 100% rename from PHP/Common/Include/Objects/TenantObjectProperty.inc.php rename to PHP/Include/Objects/TenantObjectProperty.inc.php diff --git a/PHP/Common/Include/Objects/TenantProperty.inc.php b/PHP/Include/Objects/TenantProperty.inc.php similarity index 100% rename from PHP/Common/Include/Objects/TenantProperty.inc.php rename to PHP/Include/Objects/TenantProperty.inc.php diff --git a/PHP/Common/Include/Objects/TenantQueryParameter.inc.php b/PHP/Include/Objects/TenantQueryParameter.inc.php similarity index 100% rename from PHP/Common/Include/Objects/TenantQueryParameter.inc.php rename to PHP/Include/Objects/TenantQueryParameter.inc.php diff --git a/PHP/Common/Include/Objects/TenantStringTableEntry.inc.php b/PHP/Include/Objects/TenantStringTableEntry.inc.php similarity index 100% rename from PHP/Common/Include/Objects/TenantStringTableEntry.inc.php rename to PHP/Include/Objects/TenantStringTableEntry.inc.php diff --git a/PHP/Common/Include/Objects/TenantType.inc.php b/PHP/Include/Objects/TenantType.inc.php similarity index 100% rename from PHP/Common/Include/Objects/TenantType.inc.php rename to PHP/Include/Objects/TenantType.inc.php