Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,68 +27,10 @@ private CertificateUtility()
/// System.Data assembly.
/// </summary>
public static Assembly systemData = Assembly.GetAssembly(typeof(SqlConnection));
public static Type sqlClientSymmetricKey = systemData.GetType("Microsoft.Data.SqlClient.SqlClientSymmetricKey");
public static ConstructorInfo sqlColumnEncryptionKeyConstructor = sqlClientSymmetricKey.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { typeof(byte[]) }, null);
public static Type sqlAeadAes256CbcHmac256Factory = systemData.GetType("Microsoft.Data.SqlClient.SqlAeadAes256CbcHmac256Factory");
public static MethodInfo sqlAeadAes256CbcHmac256FactoryCreate = sqlAeadAes256CbcHmac256Factory.GetMethod("Create", BindingFlags.Instance | BindingFlags.NonPublic);
public static Type sqlClientEncryptionAlgorithm = systemData.GetType("Microsoft.Data.SqlClient.SqlClientEncryptionAlgorithm");
public static MethodInfo sqlClientEncryptionAlgorithmEncryptData = sqlClientEncryptionAlgorithm.GetMethod("EncryptData", BindingFlags.Instance | BindingFlags.NonPublic);
public static MethodInfo sqlClientEncryptionAlgorithmDecryptData = sqlClientEncryptionAlgorithm.GetMethod("DecryptData", BindingFlags.Instance | BindingFlags.NonPublic);
public static Type SqlSymmetricKeyCache = systemData.GetType("Microsoft.Data.SqlClient.SqlSymmetricKeyCache");
public static MethodInfo SqlSymmetricKeyCacheGetInstance = SqlSymmetricKeyCache.GetMethod("GetInstance", BindingFlags.Static | BindingFlags.NonPublic);
public static FieldInfo SqlSymmetricKeyCacheFieldCache = SqlSymmetricKeyCache.GetField("_cache", BindingFlags.Instance | BindingFlags.NonPublic);

/// <summary>
/// ECEK Corruption types (useful for testing)
/// </summary>
internal enum ECEKCorruption
{
ALGORITHM_VERSION,
CEK_LENGTH,
SIGNATURE,
SIGNATURE_LENGTH
}

/// <summary>
/// Encryption Type as per the test code. Different than product code's enumeration.
/// </summary>
internal enum CColumnEncryptionType
{
PlainText = 0,
Deterministic,
Randomized
}

/// <summary>
/// Encrypt Data using AED
/// </summary>
/// <param name="plainTextData"></param>
/// <returns></returns>
internal static byte[] EncryptDataUsingAED(byte[] plainTextData, byte[] key, CColumnEncryptionType encryptionType)
{
Debug.Assert(plainTextData != null);
Debug.Assert(key != null && key.Length > 0);
byte[] encryptedData = null;

Object columnEncryptionKey = sqlColumnEncryptionKeyConstructor.Invoke(new object[] { key });
Debug.Assert(columnEncryptionKey != null);

Object aesFactory = Activator.CreateInstance(sqlAeadAes256CbcHmac256Factory);
Debug.Assert(aesFactory != null);

object[] parameters = new object[] { columnEncryptionKey, encryptionType, SQLSetupStrategy.ColumnEncryptionAlgorithmName };
Object authenticatedAES = sqlAeadAes256CbcHmac256FactoryCreate.Invoke(aesFactory, parameters);
Debug.Assert(authenticatedAES != null);

parameters = new object[] { plainTextData };
Object finalCellBlob = sqlClientEncryptionAlgorithmEncryptData.Invoke(authenticatedAES, parameters);
Debug.Assert(finalCellBlob != null);

encryptedData = (byte[])finalCellBlob;

return encryptedData;
}

/// <summary>
/// Through reflection, clear the SqlClient cache
/// </summary>
Expand All @@ -99,35 +41,6 @@ internal static void CleanSqlClientCache()
ClearCache(cache);
}

/// <summary>
/// Decrypt Data using AEAD
/// </summary>
internal static byte[] DecryptDataUsingAED(byte[] encryptedCellBlob, byte[] key, CColumnEncryptionType encryptionType)
{
Debug.Assert(encryptedCellBlob != null && encryptedCellBlob.Length > 0);
Debug.Assert(key != null && key.Length > 0);

byte[] decryptedData = null;

Object columnEncryptionKey = sqlColumnEncryptionKeyConstructor.Invoke(new object[] { key });
Debug.Assert(columnEncryptionKey != null);

Object aesFactory = Activator.CreateInstance(sqlAeadAes256CbcHmac256Factory);
Debug.Assert(aesFactory != null);

object[] parameters = new object[] { columnEncryptionKey, encryptionType, SQLSetupStrategy.ColumnEncryptionAlgorithmName };
Object authenticatedAES = sqlAeadAes256CbcHmac256FactoryCreate.Invoke(aesFactory, parameters);
Debug.Assert(authenticatedAES != null);

parameters = new object[] { encryptedCellBlob };
Object decryptedValue = sqlClientEncryptionAlgorithmDecryptData.Invoke(authenticatedAES, parameters);
Debug.Assert(decryptedValue != null);

decryptedData = (byte[])decryptedValue;

return decryptedData;
}

internal static SqlConnection GetOpenConnection(bool fTceEnabled, SqlConnectionStringBuilder sb, bool fSuppressAttestation = false)
{
SqlConnection conn = new SqlConnection(GetConnectionString(fTceEnabled, sb, fSuppressAttestation));
Expand Down
Loading
Loading