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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace Microsoft.Data.SqlClient
{
Expand Down Expand Up @@ -67,6 +69,74 @@ internal void GetEnclaveSession(SqlConnectionAttestationProtocol attestationProt
GetEnclaveSession(attestationProtocol, enclaveType, enclaveSessionParameters, generateCustomData, isRetry, out sqlEnclaveSession, out _, out customData, out customDataLength, throwIfNull: false);
}

/// <summary>
/// Creates a session asynchronously, unless another caller has already cached one.
/// </summary>
internal async Task CreateEnclaveSessionAsync(
SqlConnectionAttestationProtocol attestationProtocol,
string enclaveType,
EnclaveSessionParameters enclaveSessionParameters,
byte[] attestationInfo,
SqlEnclaveAttestationParameters attestationParameters,
byte[] customData,
int customDataLength,
bool isRetry,
CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
SqlColumnEncryptionEnclaveProvider provider = GetEnclaveProvider(attestationProtocol, enclaveType);
var cached = await provider.GetEnclaveSessionAsync(
enclaveSessionParameters,
generateCustomData: false,
isRetry,
cancellationToken)
.ConfigureAwait(false);

if (cached.SqlEnclaveSession != null)
{
return;
}

// The provider owns the async attestation gate and rechecks its cache under that gate.
var created = await provider.CreateEnclaveSessionAsync(
attestationInfo,
attestationParameters.ClientDiffieHellmanKey,
enclaveSessionParameters,
customData,
customDataLength,
cancellationToken)
.ConfigureAwait(false);

if (created.SqlEnclaveSession == null)
{
throw SQL.NullEnclaveSessionReturnedFromProvider(
enclaveType,
enclaveSessionParameters.AttestationUrl);
}
}

/// <summary>
/// Looks up a session asynchronously, preserving its replay counter and attestation data.
/// </summary>
internal async Task<(SqlEnclaveSession SqlEnclaveSession, long Counter, byte[] CustomData, int CustomDataLength)>
GetEnclaveSessionAsync(
SqlConnectionAttestationProtocol attestationProtocol,
string enclaveType,
EnclaveSessionParameters enclaveSessionParameters,
bool generateCustomData,
bool isRetry,
CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
SqlColumnEncryptionEnclaveProvider provider = GetEnclaveProvider(attestationProtocol, enclaveType);
Comment thread
cheenamalhotra marked this conversation as resolved.
Resolved
return await provider.GetEnclaveSessionAsync(
enclaveSessionParameters,
generateCustomData,
isRetry,
cancellationToken)
.ConfigureAwait(false);
}

private void GetEnclaveSession(SqlConnectionAttestationProtocol attestationProtocol, string enclaveType, EnclaveSessionParameters enclaveSessionParameters, bool generateCustomData, bool isRetry, out SqlEnclaveSession sqlEnclaveSession, out long counter, out byte[] customData, out int customDataLength, bool throwIfNull)
{
SqlColumnEncryptionEnclaveProvider sqlColumnEncryptionEnclaveProvider = GetEnclaveProvider(attestationProtocol, enclaveType);
Expand All @@ -84,6 +154,23 @@ internal void InvalidateEnclaveSession(SqlConnectionAttestationProtocol attestat
sqlColumnEncryptionEnclaveProvider.InvalidateEnclaveSession(enclaveSessionParameters, enclaveSession);
}

/// <summary>
/// Invalidates a session through the provider's asynchronous API.
/// </summary>
internal async Task InvalidateEnclaveSessionAsync(
SqlConnectionAttestationProtocol attestationProtocol,
string enclaveType,
EnclaveSessionParameters enclaveSessionParameters,
SqlEnclaveSession enclaveSession,
CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
SqlColumnEncryptionEnclaveProvider provider = GetEnclaveProvider(attestationProtocol, enclaveType);
await provider.InvalidateEnclaveSessionAsync(
enclaveSessionParameters,
enclaveSession,
cancellationToken).ConfigureAwait(false);
}

private SqlColumnEncryptionEnclaveProvider GetEnclaveProvider(SqlConnectionAttestationProtocol attestationProtocol, string enclaveType)
{
Expand Down Expand Up @@ -178,6 +265,26 @@ internal SqlEnclaveAttestationParameters GetAttestationParameters(SqlConnectionA
return sqlColumnEncryptionEnclaveProvider.GetAttestationParameters(attestationUrl, customData, customDataLength);
}

/// <summary>
/// Obtains attestation parameters through the provider's asynchronous API.
/// </summary>
internal async Task<SqlEnclaveAttestationParameters> GetAttestationParametersAsync(
SqlConnectionAttestationProtocol attestationProtocol,
string enclaveType,
string attestationUrl,
byte[] customData,
int customDataLength,
CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
SqlColumnEncryptionEnclaveProvider provider = GetEnclaveProvider(attestationProtocol, enclaveType);
return await provider.GetAttestationParametersAsync(
attestationUrl,
customData,
customDataLength,
cancellationToken).ConfigureAwait(false);
}

internal byte[] GetSerializedAttestationParameters(SqlEnclaveAttestationParameters sqlEnclaveAttestationParameters, string enclaveType)
{
byte[] attestationProtocolBytes = null;
Expand Down
Loading
Loading