namespace com.hitrust.Security.Cryptography { using com.hitrust.Security; using System; using System.Runtime.InteropServices; using System.Security.Cryptography; using System.Text; internal sealed class SymmetricKey : IDisposable { private static readonly byte[] ExponentOfOne = new byte[] { 7, 2, 0, 0, 0, 0xa4, 0, 0, 0x52, 0x53, 0x41, 50, 0, 4, 0, 0, 1, 0, 0, 0, 0x4b, 0x59, 0x4e, 0x26, 0xd0, 90, 0x33, 11, 0xbd, 0x5d, 0x44, 0x53, 0x19, 0xa3, 0x74, 0x8a, 0x1c, 0x90, 0x71, 0x75, 8, 0x41, 0x19, 0xf4, 0xbc, 0x92, 0x23, 4, 0x26, 0x67, 0x8d, 190, 0xe4, 0x6f, 0x74, 0x71, 0x47, 0x60, 0x60, 0x55, 0x1f, 0x72, 0x20, 0x79, 0xf2, 0x21, 0xab, 0x91, 0xc4, 0xc9, 0x5c, 180, 0x89, 0x67, 0x52, 0x10, 0x9c, 0x71, 0x52, 0x7b, 0xd4, 0x42, 0xae, 14, 0x93, 0xa6, 0xaf, 0x8d, 0x3a, 0x61, 0x70, 0x41, 0x98, 0xc3, 0x58, 220, 0xcf, 0x4c, 0xef, 0x3e, 0xc6, 0xf3, 0xe0, 180, 0xcd, 0xfb, 0xec, 0x81, 11, 0x7a, 0x75, 0x29, 0x7a, 190, 0x40, 0xf6, 0x4a, 0x3f, 0x40, 0xb7, 0x43, 240, 0x45, 0x3f, 150, 0xf1, 0x73, 0x2f, 0x71, 0xee, 0xa7, 0x70, 0x4d, 0xf9, 0x63, 0xb8, 0x52, 0x4c, 0xf1, 0x18, 0xf3, 60, 0x21, 0x13, 0x6a, 0x9a, 0x85, 0xb7, 0xa1, 0xfd, 0xb6, 0xa4, 0xf1, 0xeb, 3, 0xd6, 0x86, 5, 0x6a, 0x63, 0x93, 0xb2, 0xe7, 0xf9, 0x2a, 0x77, 9, 0xe4, 12, 0x90, 0x2d, 0x6a, 0xa2, 0xcd, 0x37, 11, 0xc0, 0xb6, 0x1c, 150, 0xc3, 0xa7, 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, 0x5c, 0xb5, 0x16, 4, 0xc4, 0x4e, 0x9d, 0x89, 0xef, 0xf1, 0x15, 0x26, 0x19, 0x16, 0x3e, 0xdd, 0xac, 0x4f, 0xe1, 170, 0x44, 0x7b, 160, 0xc5, 0xe9, 0x93, 0xc1, 0x34, 0x15, 0x67, 0x69, 0x2d, 0xc3, 0x83, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; private int m_ExponentOfOne; private int m_Handle; private bool m_OwnsProvider; private PaddingMode m_PaddingMode; private int m_Provider; private SymmetricKey(CryptoProvider provider) : this(false) { this.m_Provider = 0; if (((SspiProvider.CryptAcquireContext(ref this.m_Provider, IntPtr.Zero, null, (int) provider, 8) == 0) && (Marshal.GetLastWin32Error() == -2146893809)) && (SspiProvider.CryptAcquireContext(ref this.m_Provider, IntPtr.Zero, null, (int) provider, 0) == 0)) { throw new SecurityException("Cannot acquire crypto service provider."); } this.m_ExponentOfOne = this.CreateExponentOfOneKey(); this.m_PaddingMode = PaddingMode.None; } private SymmetricKey(bool ownsProvider) { this.m_OwnsProvider = ownsProvider; } public SymmetricKey(CryptoProvider provider, CryptoAlgorithm algorithm) : this(provider) { this.m_Handle = 0; if (SspiProvider.CryptGenKey(this.m_Provider, new IntPtr((int) algorithm), 1, ref this.m_Handle) == 0) { throw new SecurityException("Cannot generate session key."); } } public SymmetricKey(CryptoProvider provider, CryptoAlgorithm algorithm, byte[] buffer) : this(provider) { if (buffer == null) { throw new ArgumentNullException(); } this.m_Handle = this.KeyFromBytes(this.m_Provider, algorithm, buffer); } internal SymmetricKey(int provider, int key, bool ownsProvider) : this(ownsProvider) { if ((key == 0) || (provider == 0)) { throw new ArgumentNullException(); } this.m_Provider = provider; this.m_Handle = key; this.m_PaddingMode = PaddingMode.None; } private int CreateDynamicExponentOfOneKey() { int hPrivateKey = 0; int dwKeyBlob = 0; IntPtr keyblob = IntPtr.Zero; try { int i; if (SspiProvider.CryptGenKey(this.m_Provider, new IntPtr(1), 1, ref hPrivateKey) == 0) { throw new SecurityException("Cannot generate key pair."); } if (SspiProvider.CryptExportKey(hPrivateKey, 0, 7, 0, IntPtr.Zero, ref dwKeyBlob) == 0) { throw new SecurityException("Cannot export generated key."); } keyblob = Marshal.AllocHGlobal(dwKeyBlob); if (SspiProvider.CryptExportKey(hPrivateKey, 0, 7, 0, keyblob, ref dwKeyBlob) == 0) { throw new SecurityException("Cannot export generated key."); } SspiProvider.CryptDestroyKey(hPrivateKey); hPrivateKey = 0; int dwBitLen = Marshal.ReadInt32(keyblob, 12); int offset = 0x10; for (i = 0; i < 4; i++) { if (i == 0) { Marshal.WriteByte(keyblob, offset, 1); } else { Marshal.WriteByte(keyblob, offset + i, 0); } } offset += 4; offset += dwBitLen / 8; offset += dwBitLen / 0x10; offset += dwBitLen / 0x10; for (i = 0; i < (dwBitLen / 0x10); i++) { if (i == 0) { Marshal.WriteByte(keyblob, offset, 1); } else { Marshal.WriteByte(keyblob, offset + i, 0); } } offset += dwBitLen / 0x10; for (i = 0; i < (dwBitLen / 0x10); i++) { if (i == 0) { Marshal.WriteByte(keyblob, offset, 1); } else { Marshal.WriteByte(keyblob, offset + i, 0); } } offset += dwBitLen / 0x10; offset += dwBitLen / 0x10; for (i = 0; i < (dwBitLen / 8); i++) { if (i == 0) { Marshal.WriteByte(keyblob, offset, 1); } else { Marshal.WriteByte(keyblob, offset + i, 0); } } if (SspiProvider.CryptImportKey(this.m_Provider, keyblob, dwKeyBlob, 0, 1, ref hPrivateKey) == 0) { throw new SecurityException("Could not import modified key."); } } catch (Exception e) { if (hPrivateKey != 0) { SspiProvider.CryptDestroyKey(hPrivateKey); } throw e; } finally { if (keyblob != IntPtr.Zero) { Marshal.FreeHGlobal(keyblob); } } return hPrivateKey; } private int CreateExponentOfOneKey() { try { return this.CreateStaticExponentOfOneKey(); } catch { return this.CreateDynamicExponentOfOneKey(); } } private int CreateStaticExponentOfOneKey() { int hPrivateKey = 0; if (SspiProvider.CryptImportKey(this.m_Provider, ExponentOfOne, ExponentOfOne.Length, 0, 1, ref hPrivateKey) == 0) { throw new SecurityException("Could not import modified key."); } return hPrivateKey; } public void Dispose() { if (this.m_Handle != 0) { SspiProvider.CryptDestroyKey(this.m_Handle); } if (this.m_ExponentOfOne != 0) { SspiProvider.CryptDestroyKey(this.m_ExponentOfOne); } if (this.m_OwnsProvider && (this.m_Provider != 0)) { SspiProvider.CryptReleaseContext(this.m_Provider, 0); } this.m_Handle = this.m_ExponentOfOne = this.m_Provider = 0; try { GC.SuppressFinalize(this); } catch { } } ~SymmetricKey() { this.Dispose(); } private int GetPaddingMode(PaddingMode mode) { if (mode == PaddingMode.PKCS7) { return 1; } return 3; } private int KeyFromBytes(int provider, CryptoAlgorithm algorithm, byte[] key) { int dwFlags = 1; int dwProvSessionKeySize = 0; int dwPublicKeySize = 0; int offset = 0; int hTempKey = 0; int hSessionKey = 0; IntPtr algo = new IntPtr((int) algorithm); IntPtr dwPrivKeyAlg = IntPtr.Zero; IntPtr pbSessionBlob = IntPtr.Zero; IntPtr provEnum = IntPtr.Zero; try { int dwSize; int i; bool found = false; provEnum = Marshal.AllocHGlobal((int) (0x54 + IntPtr.Size)); do { dwSize = 0x54 + IntPtr.Size; if (SspiProvider.CryptGetProvParam(provider, 0x16, provEnum, ref dwSize, dwFlags) == 0) { break; } dwFlags = 0; if (Marshal.ReadIntPtr(provEnum) == algo) { found = true; } } while (!found); if (!found) { throw new SecurityException("CSP does not support selected algorithm."); } if (SspiProvider.CryptGenKey(provider, algo, 0, ref hTempKey) == 0) { throw new SecurityException("Cannot generate temporary key."); } dwSize = 4; if (SspiProvider.CryptGetKeyParam(hTempKey, 9, ref dwProvSessionKeySize, ref dwSize, 0) == 0) { throw new SecurityException("Cannot retrieve key parameters."); } if ((key.Length * 8) > dwProvSessionKeySize) { throw new SecurityException("Key too big."); } dwSize = IntPtr.Size; if (SspiProvider.CryptGetKeyParam(this.m_ExponentOfOne, 7, ref dwPrivKeyAlg, ref dwSize, 0) == 0) { throw new SecurityException("Unable to get the private key's algorithm."); } dwSize = 4; if (SspiProvider.CryptGetKeyParam(this.m_ExponentOfOne, 9, ref dwPublicKeySize, ref dwSize, 0) == 0) { throw new SecurityException("Unable to get the key length."); } int dwSessionBlob = (((dwPublicKeySize / 8) + IntPtr.Size) + 4) + IntPtr.Size; pbSessionBlob = Marshal.AllocHGlobal(dwSessionBlob); PUBLICKEYSTRUC pks = new PUBLICKEYSTRUC { bType = 1, bVersion = 2, reserved = 0, aiKeyAlg = algo }; Marshal.StructureToPtr(pks, pbSessionBlob, false); Marshal.WriteIntPtr(pbSessionBlob, offset = Marshal.SizeOf(pks), dwPrivKeyAlg); offset += IntPtr.Size; for (i = 0; i < key.Length; i++) { Marshal.WriteByte(pbSessionBlob, ((offset + key.Length) - i) - 1, key[i]); } dwSize = dwSessionBlob - ((((IntPtr.Size + IntPtr.Size) + 4) + key.Length) + 3); offset += key.Length + 1; if (SspiProvider.CryptGenRandom(provider, dwSize, new IntPtr(pbSessionBlob.ToInt64() + offset)) == 0) { throw new SecurityException("Could not generate random data."); } for (i = 0; i < dwSize; i++) { if (Marshal.ReadByte(pbSessionBlob, offset) == 0) { Marshal.WriteByte(pbSessionBlob, offset, 1); } offset++; } Marshal.WriteByte(pbSessionBlob, dwSessionBlob - 2, 2); if (SspiProvider.CryptImportKey(provider, pbSessionBlob, dwSessionBlob, this.m_ExponentOfOne, 1, ref hSessionKey) == 0) { throw new SecurityException("Cannot import key [key has right size?]."); } } finally { if (provEnum != IntPtr.Zero) { Marshal.FreeHGlobal(provEnum); } if (hTempKey != 0) { SspiProvider.CryptDestroyKey(hTempKey); } if (pbSessionBlob != IntPtr.Zero) { Marshal.FreeHGlobal(pbSessionBlob); } } return hSessionKey; } public byte[] ToBytes() { byte[] CS10000; if (this.m_Handle == 0) { throw new ObjectDisposedException(base.GetType().FullName); } IntPtr pbSessionBlob = IntPtr.Zero; try { int dwSessionBlob = 0; if (SspiProvider.CryptExportKey(this.m_Handle, this.m_ExponentOfOne, 1, 0, IntPtr.Zero, ref dwSessionBlob) == 0) { throw new SecurityException("Cannot export key."); } pbSessionBlob = Marshal.AllocHGlobal(dwSessionBlob); if (SspiProvider.CryptExportKey(this.m_Handle, this.m_ExponentOfOne, 1, 0, pbSessionBlob, ref dwSessionBlob) == 0) { throw new SecurityException("Cannot export key."); } int dwSize = 4; int dwKeyMaterial = 0; if (SspiProvider.CryptGetKeyParam(this.m_Handle, 9, ref dwKeyMaterial, ref dwSize, 0) == 0) { throw new SecurityException("Cannot retrieve key parameters."); } dwKeyMaterial /= 8; byte[] pbKeyMaterial = new byte[dwKeyMaterial]; int offset = 4 + IntPtr.Size; offset += IntPtr.Size; Marshal.Copy(new IntPtr(pbSessionBlob.ToInt64() + offset), pbKeyMaterial, 0, pbKeyMaterial.Length); Array.Reverse(pbKeyMaterial); CS10000 = pbKeyMaterial; } finally { if (pbSessionBlob != IntPtr.Zero) { Marshal.FreeHGlobal(pbSessionBlob); } } return CS10000; } public override string ToString() { if (this.m_Handle == 0) { throw new ObjectDisposedException(base.GetType().FullName); } byte[] bytes = this.ToBytes(); StringBuilder sb = new StringBuilder(bytes.Length * 2); for (int i = 0; i < bytes.Length; i++) { sb.Append(bytes[i].ToString("X2")); } return sb.ToString(); } public int FeedbackSize { get { if (this.m_Handle == 0) { throw new ObjectDisposedException(base.GetType().FullName); } int ret = 0; int length = 4; if (SspiProvider.CryptGetKeyParam(this.m_Handle, 5, ref ret, ref length, 0) == 0) { throw new CryptographicException("Could not get the feedback size."); } return ret; } set { if (this.m_Handle == 0) { throw new ObjectDisposedException(base.GetType().FullName); } if (SspiProvider.CryptSetKeyParam(this.m_Handle, 5, ref value, 0) == 0) { throw new CryptographicException("Unable to set the feedback size."); } } } public int Handle { get { if (this.m_Handle == 0) { throw new ObjectDisposedException(base.GetType().FullName); } return this.m_Handle; } } public byte[] IV { get { if (this.m_Handle == 0) { throw new ObjectDisposedException(base.GetType().FullName); } int length = 0; if (SspiProvider.CryptGetKeyParam(this.m_Handle, 1, (byte[]) null, ref length, 0) == 0) { throw new CryptographicException("Could not get the IV."); } byte[] buffer = new byte[length]; if (SspiProvider.CryptGetKeyParam(this.m_Handle, 1, buffer, ref length, 0) == 0) { throw new CryptographicException("Could not get the IV."); } return buffer; } set { if (this.m_Handle == 0) { throw new ObjectDisposedException(base.GetType().FullName); } if (value == null) { throw new ArgumentNullException(); } if (SspiProvider.CryptSetKeyParam(this.m_Handle, 1, value, 0) == 0) { throw new CryptographicException("Unable to set the initialization vector."); } } } public CipherMode Mode { get { if (this.m_Handle == 0) { throw new ObjectDisposedException(base.GetType().FullName); } int ret = 0; int length = 4; if (SspiProvider.CryptGetKeyParam(this.m_Handle, 4, ref ret, ref length, 0) == 0) { throw new CryptographicException("Could not get the cipher mode."); } return (CipherMode) ret; } set { if (this.m_Handle == 0) { throw new ObjectDisposedException(base.GetType().FullName); } int mode = (int) value; if (SspiProvider.CryptSetKeyParam(this.m_Handle, 4, ref mode, 0) == 0) { throw new CryptographicException("Unable to set the cipher mode."); } } } public PaddingMode Padding { get { return this.m_PaddingMode; } set { if (this.m_Handle == 0) { throw new ObjectDisposedException(base.GetType().FullName); } int val = this.GetPaddingMode(value); if (SspiProvider.CryptSetKeyParam(this.m_Handle, 3, ref val, 0) == 0) { throw new CryptographicException("Unable to set the padding type."); } this.m_PaddingMode = value; } } public int Provider { get { if (this.m_Handle == 0) { throw new ObjectDisposedException(base.GetType().FullName); } return this.m_Provider; } } } }