Skip to content
Draft
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 @@ -248,11 +248,89 @@ internal EnclavePackage GenerateEnclavePackage(SqlConnectionAttestationProtocol
throw new RetryableEnclaveQueryExecutionException(e.Message, e);
}

List<ColumnEncryptionKeyInfo> decryptedKeysToBeSentToEnclave = GetDecryptedKeysToBeSentToEnclave(keysToBeSentToEnclave, enclaveSessionParameters.ServerName, connection, command);
List<ColumnEncryptionKeyInfo> decryptedKeysToBeSentToEnclave = GetDecryptedKeysToBeSentToEnclave(keysToBeSentToEnclave, connection, command);
return BuildEnclavePackage(decryptedKeysToBeSentToEnclave, queryText, counter, sqlEnclaveSession, enclaveSessionParameters.ServerName);
}

/// <summary>
/// Generates an enclave package with asynchronous column encryption key decryption.
/// </summary>
/// <remarks>
/// Async counterpart of <see cref="GenerateEnclavePackage"/>. Column encryption key decryption is
/// awaited. The enclave session lookup stays synchronous: it reads the session cached by attestation
/// and does not attest, but a cache miss can wait on the provider's session lock. Package
/// cryptography is performed inline.
/// </remarks>
/// <param name="attestationProtocol">attestation protocol</param>
/// <param name="keysToBeSentToEnclave">Keys to be sent to enclave</param>
/// <param name="queryText">Text of the query being executed</param>
/// <param name="enclaveType">enclave type</param>
/// <param name="enclaveSessionParameters">The set of parameters required for enclave session.</param>
/// <param name="connection">connection executing the query</param>
/// <param name="command">command executing the query</param>
/// <param name="cancellationToken">Token used to request cancellation of the operation</param>
internal async Task<EnclavePackage> GenerateEnclavePackageAsync(
SqlConnectionAttestationProtocol attestationProtocol,
ConcurrentDictionary<int, SqlTceCipherInfoEntry> keysToBeSentToEnclave,
string queryText,
string enclaveType,
EnclaveSessionParameters enclaveSessionParameters,
SqlConnection connection,
SqlCommand command,
CancellationToken cancellationToken)
{
SqlEnclaveSession sqlEnclaveSession;
long counter;

try
{
// Reads the session cached by attestation; a miss can wait on the provider's session lock.
GetEnclaveSession(
attestationProtocol,
enclaveType,
enclaveSessionParameters,
generateCustomData: false,
isRetry: false,
sqlEnclaveSession: out sqlEnclaveSession,
counter: out counter,
customData: out _,
customDataLength: out _,
throwIfNull: true
);
}
catch (Exception e)
{
throw new RetryableEnclaveQueryExecutionException(e.Message, e);
}

List<ColumnEncryptionKeyInfo> decryptedKeysToBeSentToEnclave = await GetDecryptedKeysToBeSentToEnclaveAsync(
keysToBeSentToEnclave,
connection,
command,
cancellationToken)
.ConfigureAwait(false);

return BuildEnclavePackage(decryptedKeysToBeSentToEnclave, queryText, counter, sqlEnclaveSession, enclaveSessionParameters.ServerName);
}

/// <summary>
/// Assembles the enclave package from already-decrypted column encryption keys.
/// </summary>
/// <remarks>
/// Shared by the synchronous and asynchronous package generation paths. All work here is local
/// cryptography, so there is no asynchronous counterpart.
/// </remarks>
private EnclavePackage BuildEnclavePackage(
List<ColumnEncryptionKeyInfo> decryptedKeysToBeSentToEnclave,
string queryText,
long counter,
SqlEnclaveSession sqlEnclaveSession,
string serverName)
{
byte[] queryStringHashBytes = ComputeQueryStringHash(queryText);
byte[] keyBytePackage = GenerateBytePackageForKeys(counter, queryStringHashBytes, decryptedKeysToBeSentToEnclave);
byte[] sessionKey = sqlEnclaveSession.GetSessionKey();
byte[] encryptedBytePackage = EncryptBytePackage(keyBytePackage, sessionKey, enclaveSessionParameters.ServerName);
byte[] encryptedBytePackage = EncryptBytePackage(keyBytePackage, sessionKey, serverName);
byte[] enclaveSessionHandle = BitConverter.GetBytes(sqlEnclaveSession.SessionId);
byte[] byteArrayToBeSentToEnclave = CombineByteArrays(enclaveSessionHandle, encryptedBytePackage);
return new EnclavePackage(byteArrayToBeSentToEnclave, sqlEnclaveSession);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Microsoft.Data.SqlClient
{
Expand Down Expand Up @@ -46,46 +48,90 @@ private byte[] GetUintBytes(string enclaveType, int intValue, string variableNam
/// Decrypt the keys that need to be sent to the enclave
/// </summary>
/// <param name="keysTobeSentToEnclave">Keys that need to sent to the enclave</param>
/// <param name="serverName"></param>
/// <param name="connection"></param>
/// <param name="command"></param>
/// <param name="connection">Connection executing the query</param>
/// <param name="command">Command executing the query</param>
/// <returns></returns>
private List<ColumnEncryptionKeyInfo> GetDecryptedKeysToBeSentToEnclave(ConcurrentDictionary<int, SqlTceCipherInfoEntry> keysTobeSentToEnclave, string serverName, SqlConnection connection, SqlCommand command)
internal List<ColumnEncryptionKeyInfo> GetDecryptedKeysToBeSentToEnclave(ConcurrentDictionary<int, SqlTceCipherInfoEntry> keysTobeSentToEnclave, SqlConnection connection, SqlCommand command)
{
List<ColumnEncryptionKeyInfo> decryptedKeysToBeSentToEnclave = new List<ColumnEncryptionKeyInfo>();
List<ColumnEncryptionKeyInfo> decryptedKeysToBeSentToEnclave = new List<ColumnEncryptionKeyInfo>(keysTobeSentToEnclave.Count);

foreach (SqlTceCipherInfoEntry cipherInfo in keysTobeSentToEnclave.Values)
{
SqlSecurityUtility.DecryptSymmetricKey(cipherInfo, out SymmetricKey sqlClientSymmetricKey, out SqlEncryptionKeyInfo encryptionkeyInfoChosen, connection, command);

if (sqlClientSymmetricKey == null)
{
throw SQL.NullArgumentInternal(nameof(sqlClientSymmetricKey), nameof(EnclaveDelegate), nameof(GetDecryptedKeysToBeSentToEnclave));
}
if (cipherInfo.ColumnEncryptionKeyValues == null)
{
throw SQL.NullArgumentInternal(nameof(cipherInfo.ColumnEncryptionKeyValues), nameof(EnclaveDelegate), nameof(GetDecryptedKeysToBeSentToEnclave));
}
if (!(cipherInfo.ColumnEncryptionKeyValues.Count > 0))
{
throw SQL.ColumnEncryptionKeysNotFound();
}
decryptedKeysToBeSentToEnclave.Add(CreateColumnEncryptionKeyInfo(cipherInfo, sqlClientSymmetricKey));
}
return decryptedKeysToBeSentToEnclave;
}

//cipherInfo.CekId is always 0, hence used cipherInfo.ColumnEncryptionKeyValues[0].cekId. Even when cek has multiple ColumnEncryptionKeyValues
//the cekid and the plaintext value will remain the same, what varies is the encrypted cek value, since the cek can be encrypted by
//multiple CMKs
decryptedKeysToBeSentToEnclave.Add(
new ColumnEncryptionKeyInfo(
sqlClientSymmetricKey.RootKey,
cipherInfo.ColumnEncryptionKeyValues[0].databaseId,
cipherInfo.ColumnEncryptionKeyValues[0].cekMdVersion,
cipherInfo.ColumnEncryptionKeyValues[0].cekId
)
);
/// <summary>
/// Asynchronously decrypts the keys that need to be sent to the enclave.
/// </summary>
/// <remarks>
/// Async counterpart of <see cref="GetDecryptedKeysToBeSentToEnclave"/>. Each column encryption key is
/// resolved through <see cref="SqlSecurityUtility.DecryptSymmetricKeyAsync(SqlTceCipherInfoEntry, SqlConnection, SqlCommand, CancellationToken)"/>
/// so that key store providers performing network I/O (for example Azure Key Vault) do not block a
/// thread while the enclave package is being assembled.
/// </remarks>
/// <param name="keysTobeSentToEnclave">Keys that need to sent to the enclave</param>
/// <param name="connection">Connection executing the query</param>
/// <param name="command">Command executing the query</param>
/// <param name="cancellationToken">Token used to request cancellation of the operation</param>
internal async Task<List<ColumnEncryptionKeyInfo>> GetDecryptedKeysToBeSentToEnclaveAsync(
ConcurrentDictionary<int, SqlTceCipherInfoEntry> keysTobeSentToEnclave,
SqlConnection connection,
SqlCommand command,
CancellationToken cancellationToken)
{
List<ColumnEncryptionKeyInfo> decryptedKeysToBeSentToEnclave = new List<ColumnEncryptionKeyInfo>(keysTobeSentToEnclave.Count);

foreach (SqlTceCipherInfoEntry cipherInfo in keysTobeSentToEnclave.Values)
{
(SymmetricKey sqlClientSymmetricKey, SqlEncryptionKeyInfo _) =
await SqlSecurityUtility.DecryptSymmetricKeyAsync(cipherInfo, connection, command, cancellationToken)
.ConfigureAwait(false);

decryptedKeysToBeSentToEnclave.Add(CreateColumnEncryptionKeyInfo(cipherInfo, sqlClientSymmetricKey));
}

return decryptedKeysToBeSentToEnclave;
}

/// <summary>
/// Validates a decrypted column encryption key and projects it into the shape the enclave expects.
/// </summary>
/// <remarks>
/// Shared by the synchronous and asynchronous decryption paths so that both apply identical validation
/// and produce identical <see cref="ColumnEncryptionKeyInfo"/> instances.
/// </remarks>
private static ColumnEncryptionKeyInfo CreateColumnEncryptionKeyInfo(
SqlTceCipherInfoEntry cipherInfo,
SymmetricKey sqlClientSymmetricKey)
{
if (sqlClientSymmetricKey == null)
{
throw SQL.NullArgumentInternal(nameof(sqlClientSymmetricKey), nameof(EnclaveDelegate), nameof(CreateColumnEncryptionKeyInfo));
}
if (cipherInfo.ColumnEncryptionKeyValues == null)
{
throw SQL.NullArgumentInternal(nameof(cipherInfo.ColumnEncryptionKeyValues), nameof(EnclaveDelegate), nameof(CreateColumnEncryptionKeyInfo));
}
if (!(cipherInfo.ColumnEncryptionKeyValues.Count > 0))
{
throw SQL.ColumnEncryptionKeysNotFound();
}

//cipherInfo.CekId is always 0, hence used cipherInfo.ColumnEncryptionKeyValues[0].cekId. Even when cek has multiple ColumnEncryptionKeyValues
//the cekid and the plaintext value will remain the same, what varies is the encrypted cek value, since the cek can be encrypted by
//multiple CMKs
return new ColumnEncryptionKeyInfo(
sqlClientSymmetricKey.RootKey,
cipherInfo.ColumnEncryptionKeyValues[0].databaseId,
cipherInfo.ColumnEncryptionKeyValues[0].cekMdVersion,
cipherInfo.ColumnEncryptionKeyValues[0].cekId
);
}

/// <summary>
/// Generate a byte package consisting of decrypted keys and some headers expected by the enclave
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,7 @@ private void PrepareForTransparentEncryptionCore(
"completion should be null if and only if mode is async.");

returnTask = null;
bool inProgressFlagWasSet = _enclaveAttestationInProgress;

if (isAsync && ShouldUseEnclaveBasedWorkflow)
{
Expand All @@ -706,6 +707,12 @@ private void PrepareForTransparentEncryptionCore(
if (returnTask?.Status == TaskStatus.RanToCompletion)
{
returnTask = null;

// No reader continuation will run, so release a flag that preparation handed off.
if (!inProgressFlagWasSet)
{
_enclaveAttestationInProgress = false;
}
}
}

Expand Down Expand Up @@ -844,7 +851,10 @@ private async Task PrepareForTransparentEncryptionAsync(
{
SqlConnectionInternal retryConnection = _enclaveRetryConnection;
// A stale cached-metadata fallback already holds the flag until execution hand-off.
// Otherwise, on success, the reader continuation clears it after reserving the
// connection.
bool ownsInProgressFlag = !_enclaveAttestationInProgress;
bool handedOff = false;
_enclaveAttestationInProgress = true;
try
{
Expand All @@ -859,6 +869,8 @@ private async Task PrepareForTransparentEncryptionAsync(
{
await task.ConfigureAwait(false);
}

handedOff = true;
}
catch (Exception e) when (ADP.IsCatchableExceptionType(e))
{
Expand All @@ -868,7 +880,7 @@ private async Task PrepareForTransparentEncryptionAsync(
}
finally
{
if (ownsInProgressFlag)
if (ownsInProgressFlag && !handedOff)
{
_enclaveAttestationInProgress = false;
}
Expand Down
Loading
Loading