Skip to content

Commit cad62b9

Browse files
committed
Abstract the SSPI context generation
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.
1 parent b12b15d commit cad62b9

14 files changed

+516
-613
lines changed

src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj

+18-2
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,18 @@
516516
<Compile Include="..\..\src\System\Diagnostics\CodeAnalysis.cs">
517517
<Link>Common\System\Diagnostics\CodeAnalysis.cs</Link>
518518
</Compile>
519+
<Compile Include="..\..\src\Microsoft\Data\SqlClient\SSPI\ManagedSSPIContextProvider.cs">
520+
<Link>Microsoft\Data\SqlClient\SSPI\ManagedSSPIContextProvider.cs</Link>
521+
</Compile>
522+
<Compile Include="..\..\src\Microsoft\Data\SqlClient\SSPI\NegotiateSSPIContextProvider.cs">
523+
<Link>Microsoft\Data\SqlClient\SSPI\NegotiateSSPIContextProvider.cs</Link>
524+
</Compile>
525+
<Compile Include="..\..\src\Microsoft\Data\SqlClient\SSPI\SSPIContextProvider.cs">
526+
<Link>Microsoft\Data\SqlClient\SSPI\SSPIContextProvider.cs</Link>
527+
</Compile>
528+
<Compile Include="..\..\src\Microsoft\Data\SqlClient\TdsParser.Shared.cs">
529+
<Link>Microsoft\Data\SqlClient\TdsParser.Shared.cs</Link>
530+
</Compile>
519531
</ItemGroup>
520532
<ItemGroup Condition="'$(TargetGroup)' == 'netstandard' OR '$(TargetGroup)' == 'netcoreapp' OR '$(IsUAPAssembly)' == 'true'">
521533
<Compile Include="Microsoft.Data.SqlClient.TypeForwards.cs" />
@@ -580,7 +592,7 @@
580592
<Compile Include="Microsoft\Data\SqlClient\SqlConnectionFactory.AssemblyLoadContext.cs" />
581593
</ItemGroup>
582594
<ItemGroup>
583-
<Compile Include="..\..\src\Resources\StringsHelper.cs">
595+
<Compile Include="..\..\src\Resources\StringsHelper.cs">
584596
<Link>Resources\StringsHelper.cs</Link>
585597
</Compile>
586598
<Compile Include="..\..\src\Resources\Strings.Designer.cs">
@@ -767,7 +779,8 @@
767779
<ItemGroup Condition="'$(TargetsWindows)' == 'true' and '$(IsUAPAssembly)' != 'true'">
768780
<Compile Include="$(CommonPath)\Interop\Windows\kernel32\Interop.LoadLibraryEx.cs">
769781
<Link>Common\Interop\Windows\kernel32\Interop.LoadLibraryEx.cs</Link>
770-
</Compile>
782+
</Compile>
783+
<Compile Include="..\..\src\Microsoft\Data\SqlClient\SSPI\NativeSSPIContextProvider.cs" Link="Microsoft\Data\SqlClient\SSPI\NativeSSPIContextProvider.cs" />
771784
<Compile Include="Interop\SNINativeMethodWrapper.Windows.cs" />
772785
<Compile Include="Microsoft\Data\ProviderBase\DbConnectionPoolIdentity.Windows.cs" />
773786
<Compile Include="Microsoft\Data\SqlClient\LocalDBAPI.Windows.cs" />
@@ -1007,6 +1020,9 @@
10071020
<PackageReference Condition="$(TargetGroup) == 'netstandard'" Include="System.Security.Cryptography.Cng" Version="$(SystemSecurityCryptographyCngVersion)" />
10081021
<PackageReference Condition="$(BuildForRelease) == 'true'" Include="Microsoft.SourceLink.GitHub" Version="$(MicrosoftSourceLinkGitHubVersion)" PrivateAssets="All" />
10091022
</ItemGroup>
1023+
<ItemGroup>
1024+
<Folder Include="Microsoft\Data\SqlClient\SSPI\" />
1025+
</ItemGroup>
10101026
<Import Project="$(ToolsDir)targets\GenerateThisAssemblyCs.targets" />
10111027
<Import Project="$(ToolsDir)targets\ResolveContract.targets" Condition="'$(OSGroup)' == 'AnyOS'" />
10121028
<Import Project="$(ToolsDir)targets\NotSupported.targets" Condition="'$(OSGroup)' == 'AnyOS'" />

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.Windows.cs

+1-33
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ namespace Microsoft.Data.SqlClient
1010
{
1111
internal sealed partial class TdsParser
1212
{
13-
private static volatile bool s_fSSPILoaded = false; // bool to indicate whether library has been loaded
14-
1513
internal void PostReadAsyncForMars()
1614
{
1715
if (TdsParserStateObjectFactory.UseManagedSNI)
@@ -43,37 +41,7 @@ internal void PostReadAsyncForMars()
4341
_physicalStateObj.AddError(ProcessSNIError(_physicalStateObj));
4442
ThrowExceptionAndWarning(_physicalStateObj);
4543
}
46-
}
47-
48-
private void LoadSSPILibrary()
49-
{
50-
if (TdsParserStateObjectFactory.UseManagedSNI)
51-
return;
52-
// Outer check so we don't acquire lock once it's loaded.
53-
if (!s_fSSPILoaded)
54-
{
55-
lock (s_tdsParserLock)
56-
{
57-
// re-check inside lock
58-
if (!s_fSSPILoaded)
59-
{
60-
// use local for ref param to defer setting s_maxSSPILength until we know the call succeeded.
61-
uint maxLength = 0;
62-
63-
if (0 != SNINativeMethodWrapper.SNISecInitPackage(ref maxLength))
64-
SSPIError(SQLMessage.SSPIInitializeError(), TdsEnums.INIT_SSPI_PACKAGE);
65-
66-
s_maxSSPILength = maxLength;
67-
s_fSSPILoaded = true;
68-
}
69-
}
70-
}
71-
72-
if (s_maxSSPILength > int.MaxValue)
73-
{
74-
throw SQL.InvalidSSPIPacketSize(); // SqlBu 332503
75-
}
76-
}
44+
}
7745

7846
private void WaitForSSLHandShakeToComplete(ref uint error, ref int protocolVersion)
7947
{

0 commit comments

Comments
 (0)