Skip to content

Commit

Permalink
Abstract the SSPI context generation
Browse files Browse the repository at this point in the history
This change introduces SSPIContextProvider that can generate payloads for SSPI. Specifically, this change plumbs the current SSPI context generation into this object, while later changes will continue to update the shape to be a more general purpose, public API.
  • Loading branch information
twsouthwick committed Jan 18, 2024
1 parent b12b15d commit cad62b9
Show file tree
Hide file tree
Showing 14 changed files with 516 additions and 613 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,18 @@
<Compile Include="..\..\src\System\Diagnostics\CodeAnalysis.cs">
<Link>Common\System\Diagnostics\CodeAnalysis.cs</Link>
</Compile>
<Compile Include="..\..\src\Microsoft\Data\SqlClient\SSPI\ManagedSSPIContextProvider.cs">
<Link>Microsoft\Data\SqlClient\SSPI\ManagedSSPIContextProvider.cs</Link>
</Compile>
<Compile Include="..\..\src\Microsoft\Data\SqlClient\SSPI\NegotiateSSPIContextProvider.cs">
<Link>Microsoft\Data\SqlClient\SSPI\NegotiateSSPIContextProvider.cs</Link>
</Compile>
<Compile Include="..\..\src\Microsoft\Data\SqlClient\SSPI\SSPIContextProvider.cs">
<Link>Microsoft\Data\SqlClient\SSPI\SSPIContextProvider.cs</Link>
</Compile>
<Compile Include="..\..\src\Microsoft\Data\SqlClient\TdsParser.Shared.cs">
<Link>Microsoft\Data\SqlClient\TdsParser.Shared.cs</Link>
</Compile>
</ItemGroup>
<ItemGroup Condition="'$(TargetGroup)' == 'netstandard' OR '$(TargetGroup)' == 'netcoreapp' OR '$(IsUAPAssembly)' == 'true'">
<Compile Include="Microsoft.Data.SqlClient.TypeForwards.cs" />
Expand Down Expand Up @@ -580,7 +592,7 @@
<Compile Include="Microsoft\Data\SqlClient\SqlConnectionFactory.AssemblyLoadContext.cs" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\src\Resources\StringsHelper.cs">
<Compile Include="..\..\src\Resources\StringsHelper.cs">
<Link>Resources\StringsHelper.cs</Link>
</Compile>
<Compile Include="..\..\src\Resources\Strings.Designer.cs">
Expand Down Expand Up @@ -767,7 +779,8 @@
<ItemGroup Condition="'$(TargetsWindows)' == 'true' and '$(IsUAPAssembly)' != 'true'">
<Compile Include="$(CommonPath)\Interop\Windows\kernel32\Interop.LoadLibraryEx.cs">
<Link>Common\Interop\Windows\kernel32\Interop.LoadLibraryEx.cs</Link>
</Compile>
</Compile>
<Compile Include="..\..\src\Microsoft\Data\SqlClient\SSPI\NativeSSPIContextProvider.cs" Link="Microsoft\Data\SqlClient\SSPI\NativeSSPIContextProvider.cs" />
<Compile Include="Interop\SNINativeMethodWrapper.Windows.cs" />
<Compile Include="Microsoft\Data\ProviderBase\DbConnectionPoolIdentity.Windows.cs" />
<Compile Include="Microsoft\Data\SqlClient\LocalDBAPI.Windows.cs" />
Expand Down Expand Up @@ -1007,6 +1020,9 @@
<PackageReference Condition="$(TargetGroup) == 'netstandard'" Include="System.Security.Cryptography.Cng" Version="$(SystemSecurityCryptographyCngVersion)" />
<PackageReference Condition="$(BuildForRelease) == 'true'" Include="Microsoft.SourceLink.GitHub" Version="$(MicrosoftSourceLinkGitHubVersion)" PrivateAssets="All" />
</ItemGroup>
<ItemGroup>
<Folder Include="Microsoft\Data\SqlClient\SSPI\" />
</ItemGroup>
<Import Project="$(ToolsDir)targets\GenerateThisAssemblyCs.targets" />
<Import Project="$(ToolsDir)targets\ResolveContract.targets" Condition="'$(OSGroup)' == 'AnyOS'" />
<Import Project="$(ToolsDir)targets\NotSupported.targets" Condition="'$(OSGroup)' == 'AnyOS'" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ namespace Microsoft.Data.SqlClient
{
internal sealed partial class TdsParser
{
private static volatile bool s_fSSPILoaded = false; // bool to indicate whether library has been loaded

internal void PostReadAsyncForMars()
{
if (TdsParserStateObjectFactory.UseManagedSNI)
Expand Down Expand Up @@ -43,37 +41,7 @@ internal void PostReadAsyncForMars()
_physicalStateObj.AddError(ProcessSNIError(_physicalStateObj));
ThrowExceptionAndWarning(_physicalStateObj);
}
}

private void LoadSSPILibrary()
{
if (TdsParserStateObjectFactory.UseManagedSNI)
return;
// Outer check so we don't acquire lock once it's loaded.
if (!s_fSSPILoaded)
{
lock (s_tdsParserLock)
{
// re-check inside lock
if (!s_fSSPILoaded)
{
// use local for ref param to defer setting s_maxSSPILength until we know the call succeeded.
uint maxLength = 0;

if (0 != SNINativeMethodWrapper.SNISecInitPackage(ref maxLength))
SSPIError(SQLMessage.SSPIInitializeError(), TdsEnums.INIT_SSPI_PACKAGE);

s_maxSSPILength = maxLength;
s_fSSPILoaded = true;
}
}
}

if (s_maxSSPILength > int.MaxValue)
{
throw SQL.InvalidSSPIPacketSize(); // SqlBu 332503
}
}
}

private void WaitForSSLHandShakeToComplete(ref uint error, ref int protocolVersion)
{
Expand Down
Loading

0 comments on commit cad62b9

Please sign in to comment.