Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged common code base for AlwaysEncryptedKeyConverter #2538

Merged
merged 2 commits into from
Jun 27, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\AlwaysEncryptedEnclaveProviderUtils.cs">
<Link>Microsoft\Data\SqlClient\AlwaysEncryptedEnclaveProviderUtils.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\AlwaysEncryptedKeyConverter.cs">
<Link>Microsoft\Data\SqlClient\AlwaysEncryptedKeyConverter.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\ApplicationIntent.cs">
<Link>Microsoft\Data\SqlClient\ApplicationIntent.cs</Link>
</Compile>
Expand Down Expand Up @@ -622,7 +625,6 @@
<Compile Include="Microsoft\Data\ProviderBase\DbConnectionPoolIdentity.cs" />
<Compile Include="Microsoft\Data\SqlClient\AAsyncCallContext.cs" />
<Compile Include="Microsoft\Data\SqlClient\AlwaysEncryptedHelperClasses.cs" />
<Compile Include="Microsoft\Data\SqlClient\AlwaysEncryptedKeyConverter.CrossPlatform.cs" />
<Compile Include="Microsoft\Data\SqlClient\LocalDBAPI.cs" />
<Compile Include="Microsoft\Data\SqlClient\Reliability\SqlConfigurableRetryLogicManager.NetCoreApp.cs" />
<Compile Include="Microsoft\Data\SqlClient\SNI\ConcurrentQueueSemaphore.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,9 @@
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\AlwaysEncryptedEnclaveProviderUtils.cs">
<Link>Microsoft\Data\SqlClient\AlwaysEncryptedEnclaveProviderUtils.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\AlwaysEncryptedKeyConverter.cs">
<Link>Microsoft\Data\SqlClient\AlwaysEncryptedKeyConverter.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\AzureAttestationBasedEnclaveProvider.cs">
<Link>Microsoft\Data\SqlClient\AzureAttestationBasedEnclaveProvider.cs</Link>
</Compile>
Expand Down Expand Up @@ -652,7 +655,6 @@
<Compile Include="Microsoft\Data\ProviderBase\DbConnectionPoolCounters.cs" />
<Compile Include="Microsoft\Data\ProviderBase\DbConnectionPoolIdentity.cs" />
<Compile Include="Microsoft\Data\RelationshipConverter.cs" />
<Compile Include="Microsoft\Data\SqlClient\AlwaysEncryptedKeyConverter.Cng.cs" />
<Compile Include="Microsoft\Data\SqlClient\assemblycache.cs" />
<Compile Include="Microsoft\Data\SqlClient\LocalDBAPI.cs" />
<Compile Include="Microsoft\Data\SqlClient\LocalDBConfig.cs" />
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

Expand Down Expand Up @@ -39,6 +39,7 @@ private readonly struct ECCPublicKeyBlob
// => ECDiffieHellmanPublicKey.ToByteArray() is not supported in Unix
internal static byte[] GetECDiffieHellmanPublicKeyBlob(ECDiffieHellman ecDiffieHellman)
{
#if NET6_0_OR_GREATER
byte[] keyBlob = new byte[ECCPublicKeyBlob.Size];

// Set magic number
Expand All @@ -53,6 +54,16 @@ internal static byte[] GetECDiffieHellmanPublicKeyBlob(ECDiffieHellman ecDiffieH
Buffer.BlockCopy(ecPoint.X, 0, keyBlob, ECCPublicKeyBlob.HeaderSize, ECCPublicKeyBlob.KeySize);
Buffer.BlockCopy(ecPoint.Y, 0, keyBlob, ECCPublicKeyBlob.HeaderSize + ECCPublicKeyBlob.KeySize, ECCPublicKeyBlob.KeySize);
return keyBlob;
#else
if (ecDiffieHellman is ECDiffieHellmanCng cng)
{
return cng.Key.Export(CngKeyBlobFormat.EccPublicBlob);
}
else
{
throw new InvalidOperationException();
}
#endif
}

// The RSA public key blob is structured as follows:
Expand All @@ -75,6 +86,7 @@ private readonly struct RSAPublicKeyBlob

internal static RSA CreateRSAFromPublicKeyBlob(byte[] keyBlob)
{
#if NET6_0_OR_GREATER
Debug.Assert(keyBlob.Length == RSAPublicKeyBlob.Size, $"RSA public key blob was not the expected length. Actual: {keyBlob.Length}. Expected: {RSAPublicKeyBlob.Size}");

byte[] exponent = new byte[RSAPublicKeyBlob.ExponentSize];
Expand All @@ -87,10 +99,15 @@ internal static RSA CreateRSAFromPublicKeyBlob(byte[] keyBlob)
Modulus = modulus
};
return RSA.Create(rsaParameters);
#else
CngKey key = CngKey.Import(keyBlob, CngKeyBlobFormat.GenericPublicBlob);
return new RSACng(key);
#endif
}

internal static ECDiffieHellman CreateECDiffieHellmanFromPublicKeyBlob(byte[] keyBlob)
{
#if NET6_0_OR_GREATER
Debug.Assert(keyBlob.Length == ECCPublicKeyBlob.Size, $"ECC public key blob was not the expected length. Actual: {keyBlob.Length}. Expected: {ECCPublicKeyBlob.Size}");

byte[] x = new byte[ECCPublicKeyBlob.KeySize];
Expand All @@ -109,27 +126,61 @@ internal static ECDiffieHellman CreateECDiffieHellmanFromPublicKeyBlob(byte[] ke
};

return ECDiffieHellman.Create(parameters);
#else
CngKey key = CngKey.Import(keyBlob, CngKeyBlobFormat.GenericPublicBlob);
return new ECDiffieHellmanCng(key);
#endif
}

internal static ECDiffieHellman CreateECDiffieHellman(int keySize)
{
#if NET6_0_OR_GREATER
// platform agnostic creates a key of the correct size but does not
// set the key derivation type or algorithm, these must be set by calling
// DeriveKeyFromHash later in DeriveKey
ECDiffieHellman clientDHKey = ECDiffieHellman.Create();
clientDHKey.KeySize = keySize;
#else
// Cng sets the key size and hash algorithm at creation time and these
// parameters are then used later when DeriveKeyMaterial is called
ECDiffieHellmanCng clientDHKey = new ECDiffieHellmanCng(keySize);
clientDHKey.KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash;
clientDHKey.HashAlgorithm = CngAlgorithm.Sha256;
#endif
return clientDHKey;
}

internal static byte[] DeriveKey(ECDiffieHellman ecd, ECDiffieHellmanPublicKey publicKey)
internal static byte[] DeriveKey(ECDiffieHellman ecDiffieHellman, ECDiffieHellmanPublicKey publicKey)
{
#if NET6_0_OR_GREATER
// see notes in CreateECDDiffieHellman
return ecd.DeriveKeyFromHash(publicKey, HashAlgorithmName.SHA256);
return ecDiffieHellman.DeriveKeyFromHash(publicKey, HashAlgorithmName.SHA256);
#else
if (ecDiffieHellman is ECDiffieHellmanCng cng)
{
return cng.DeriveKeyMaterial(publicKey);
}
else
{
throw new InvalidOperationException();
}
#endif
}

internal static RSA GetRSAFromCertificate(X509Certificate2 certificate)
{
#if NET6_0_OR_GREATER
return certificate.GetRSAPublicKey();
#else
RSAParameters parameters;
using (RSA rsaCsp = certificate.GetRSAPublicKey())
{
parameters = rsaCsp.ExportParameters(includePrivateParameters: false);
}
RSACng rsaCng = new RSACng();
rsaCng.ImportParameters(parameters);
return rsaCng;
#endif
}
}
}
Loading