Skip to content

Commit a497697

Browse files
benrr101edwardneal
andauthored
Update src/Microsoft.Data.SqlClient/tests/ManualTests/AlwaysEncrypted/TestFixtures/SQLSetupStrategyAzureKeyVault.cs
Co-authored-by: Edward Neal <55035479+edwardneal@users.noreply.github.com>
1 parent 3564f63 commit a497697

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/Microsoft.Data.SqlClient/tests/ManualTests/AlwaysEncrypted/TestFixtures/SQLSetupStrategyAzureKeyVault.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,31 @@ public static void RegisterGlobalProviders(SqlColumnEncryptionAzureKeyVaultProvi
5353
IsAKVProviderRegistered = true;
5454
}
5555

56+
private static RSA CopyKey(RSA rsa)
57+
{
58+
#if NET8_0
59+
// In .NET Framework, the key is exportable in plaintext. In .NET 9.0+, we use X509CertificateLoader2 to maintain this functionality.
60+
// We need to manually handle this in .NET 8.0 with an non-plaintext export.
61+
RSA replacementKey = RSA.Create(rsa.KeySize);
62+
Span<byte> passwordBytes = stackalloc byte[32];
63+
PbeParameters pbeParameters = new PbeParameters(PbeEncryptionAlgorithm.Aes256Cbc, HashAlgorithmName.SHA256, 10000);
64+
65+
Random.Shared.NextBytes(passwordBytes);
66+
67+
replacementKey.ImportEncryptedPkcs8PrivateKey(
68+
passwordBytes,
69+
rsa.ExportEncryptedPkcs8PrivateKey(passwordBytes, pbeParameters),
70+
out _);
71+
return replacementKey;
72+
#else
73+
return rsa;
74+
#endif
75+
}
76+
5677
private void SetupAzureKeyVault()
5778
{
58-
JsonWebKey rsaImport = new JsonWebKey(ColumnMasterKeyCertificate.GetRSAPrivateKey(), true);
79+
RSA rsaCopy = CopyKey(ColumnMasterKeyCertificate.GetRSAPrivateKey());
80+
JsonWebKey rsaImport = new JsonWebKey(rsaCopy, true);
5981
string akvKeyName = $"AE-{ColumnMasterKeyCertificate.Thumbprint}";
6082

6183
_keyClient.ImportKey(akvKeyName, rsaImport);

0 commit comments

Comments
 (0)