Skip to content

Commit ff511ed

Browse files
authored
Merge | TdsParserStateObjectNative (#3631)
* netfx, netcore: move duplicate WritePacketCache class Duplicated in netcore by a class in TdsParserSafeHandles.Windows.cs. Remove the duplicate and add to netfx. * netfx, netcore: sync comments and whitespace * netcore: sync tracing * netcore: remove unused methods ReadAsyncCallback and WriteAsyncCallback only recursed. Also sync whitespace in netfx. * netfx: sync null coalescing operator usage * netfx, netcore: cleanup SQLFallbackDNSCache call Also, netfx: sync of AuthProviderInfo population to match netcore. We were assigning default values. * netfx: remove unnecessary default values for CreatePhysicalSNIHandle parameters * netcore, netfx: sync SNIHandle construction Also sync debug assertion for SessionHandle * netcore: remove hardcoded SSL protocol flags NativeProtocols enum values are aligned with the SslProtocol values. The output is only ever used to return the SSL warning, which checks whether any of each relevant SSL protocol's bits are set. Normalization of the client and server bits are irrelevant for this purpose - the SSL warning tests for both. * netcore: sync timeouts of ReadSyncOverAsync This is only ever called with a timeoutRemaining parameter value of GetTimeoutRemaining(). * netcore: sync server certificate filename marshalling Send a null value to the native SNI by default, not an empty string * Merge TdsParserStateObjectNative * First code review response Maintained comment in Dispose method post-merge * Add explicit handling of SNI-layer SSL protocols This guards against future changes to the SslProtocols enum
1 parent d7a71f6 commit ff511ed

14 files changed

+192
-661
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,9 @@
931931
<Compile Include="$(CommonSourceRoot)\Interop\Windows\Sni\SniNativeWrapper.cs">
932932
<Link>Interop\Windows\Sni\SniNativeWrapper.cs</Link>
933933
</Compile>
934+
<Compile Include="$(CommonSourceRoot)\Interop\Windows\Sni\SniSslProtocols.cs">
935+
<Link>Interop\Windows\Sni\SniSslProtocols.cs</Link>
936+
</Compile>
934937
<Compile Include="$(CommonSourceRoot)\Interop\Windows\Sni\TransparentNetworkResolutionMode.cs">
935938
<Link>Interop\Windows\Sni\TransparentNetworkResolutionMode.cs</Link>
936939
</Compile>
@@ -976,12 +979,13 @@
976979
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\TdsParserStateObjectFactory.Windows.cs">
977980
<Link>Microsoft\Data\SqlClient\TdsParserStateObjectFactory.Windows.cs</Link>
978981
</Compile>
982+
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\TdsParserStateObjectNative.Windows.cs">
983+
<Link>Microsoft\Data\SqlClient\TdsParserStateObjectNative.Windows.cs</Link>
984+
</Compile>
979985
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlTypes\SqlFileStream.Windows.cs">
980986
<Link>Microsoft\Data\SqlTypes\SqlFileStream.Windows.cs</Link>
981987
</Compile>
982988

983-
<Compile Include="Microsoft\Data\SqlClient\TdsParserStateObjectNative.cs" />
984-
985989
<EmbeddedResource Include="$(CommonSourceRoot)Resources\ILLink.Substitutions.Windows.xml">
986990
<LogicalName>ILLink.Substitutions.xml</LogicalName>
987991
<Link>Resources\ILLink.Substitutions.Windows.xml</Link>

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObjectNative.cs

Lines changed: 0 additions & 554 deletions
This file was deleted.

src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,9 @@
219219
<Compile Include="$(CommonSourceRoot)\Interop\Windows\Sni\SqlDependencyProcessDispatcherStorage.netfx.cs">
220220
<Link>Interop\Windows\Sni\SqlDependencyProcessDispatcherStorage.netfx.cs</Link>
221221
</Compile>
222+
<Compile Include="$(CommonSourceRoot)\Interop\Windows\Sni\SniSslProtocols.cs">
223+
<Link>Interop\Windows\Sni\SniSslProtocols.cs</Link>
224+
</Compile>
222225
<Compile Include="$(CommonSourceRoot)\Interop\Windows\Sni\TransparentNetworkResolutionMode.cs">
223226
<Link>Interop\Windows\Sni\TransparentNetworkResolutionMode.cs</Link>
224227
</Compile>
@@ -927,6 +930,9 @@
927930
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\TdsParserStateObjectFactory.Windows.cs">
928931
<Link>Microsoft\Data\SqlClient\TdsParserStateObjectFactory.Windows.cs</Link>
929932
</Compile>
933+
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\TdsParserStateObjectNative.Windows.cs">
934+
<Link>Microsoft\Data\SqlClient\TdsParserStateObjectNative.Windows.cs</Link>
935+
</Compile>
930936
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\TdsParserStaticMethods.cs">
931937
<Link>Microsoft\Data\SqlClient\TdsParserStaticMethods.cs</Link>
932938
</Compile>
@@ -999,7 +1005,6 @@
9991005
<Compile Include="Microsoft\Data\SqlClient\SqlInternalConnectionTds.cs" />
10001006
<Compile Include="Microsoft\Data\SqlClient\TdsParser.cs" />
10011007
<Compile Include="Microsoft\Data\SqlClient\TdsParserStateObject.netfx.cs" />
1002-
<Compile Include="Microsoft\Data\SqlClient\TdsParserStateObjectNative.cs" />
10031008
</ItemGroup>
10041009
<!-- Resources -->
10051010
<ItemGroup>

src/Microsoft.Data.SqlClient/src/Interop/Windows/Sni/ISniNativeMethods.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ unsafe uint SniSecGenClientContextWrapper(
9292

9393
uint SniTerminate();
9494

95-
uint SniWaitForSslHandshakeToComplete(SNIHandle pConn, int dwMilliseconds, out uint pProtocolVersion);
95+
uint SniWaitForSslHandshakeToComplete(SNIHandle pConn, int dwMilliseconds, out SniSslProtocols pProtocolVersion);
9696

9797
uint SniWriteAsyncWrapper(SNIHandle pConn, SNIPacket pPacket);
9898

src/Microsoft.Data.SqlClient/src/Interop/Windows/Sni/SniNativeMethods.netcore.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public uint SniSetInfo(SNIHandle pConn, QueryType queryType, ref uint pbQueryInf
148148
public uint SniTerminate() =>
149149
SNITerminate();
150150

151-
public uint SniWaitForSslHandshakeToComplete(SNIHandle pConn, int dwMilliseconds, out uint pProtocolVersion) =>
151+
public uint SniWaitForSslHandshakeToComplete(SNIHandle pConn, int dwMilliseconds, out SniSslProtocols pProtocolVersion) =>
152152
SNIWaitForSSLHandshakeToCompleteWrapper(pConn, dwMilliseconds, out pProtocolVersion);
153153

154154
public uint SniWriteAsyncWrapper(SNIHandle pConn, SNIPacket pPacket) =>
@@ -299,7 +299,7 @@ private static extern int SNIServerEnumReadWrapper(
299299
private static extern uint SNIWaitForSSLHandshakeToCompleteWrapper(
300300
[In] SNIHandle pConn,
301301
int dwMilliseconds,
302-
out uint pProtocolVersion);
302+
out SniSslProtocols pProtocolVersion);
303303

304304
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
305305
private static extern uint SNIWriteAsyncWrapper(SNIHandle pConn, [In] SNIPacket pPacket);

src/Microsoft.Data.SqlClient/src/Interop/Windows/Sni/SniNativeMethodsArm64.netfx.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public uint SniSetInfo(SNIHandle pConn, QueryType queryType, ref uint pbQueryInf
148148
public uint SniTerminate() =>
149149
SNITerminate();
150150

151-
public uint SniWaitForSslHandshakeToComplete(SNIHandle pConn, int dwMilliseconds, out uint pProtocolVersion) =>
151+
public uint SniWaitForSslHandshakeToComplete(SNIHandle pConn, int dwMilliseconds, out SniSslProtocols pProtocolVersion) =>
152152
SNIWaitForSSLHandshakeToCompleteWrapper(pConn, dwMilliseconds, out pProtocolVersion);
153153

154154
public uint SniWriteAsyncWrapper(SNIHandle pConn, SNIPacket pPacket) =>
@@ -299,7 +299,7 @@ private static extern int SNIServerEnumReadWrapper(
299299
private static extern uint SNIWaitForSSLHandshakeToCompleteWrapper(
300300
[In] SNIHandle pConn,
301301
int dwMilliseconds,
302-
out uint pProtocolVersion);
302+
out SniSslProtocols pProtocolVersion);
303303

304304
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
305305
private static extern uint SNIWriteAsyncWrapper(SNIHandle pConn, [In] SNIPacket pPacket);

src/Microsoft.Data.SqlClient/src/Interop/Windows/Sni/SniNativeMethodsNotSupported.netfx.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public uint SniSetInfo(SNIHandle pConn, QueryType queryType, ref uint pbQueryInf
142142
public uint SniTerminate() =>
143143
throw ADP.SNIPlatformNotSupported(_architecture);
144144

145-
public uint SniWaitForSslHandshakeToComplete(SNIHandle pConn, int dwMilliseconds, out uint pProtocolVersion) =>
145+
public uint SniWaitForSslHandshakeToComplete(SNIHandle pConn, int dwMilliseconds, out SniSslProtocols pProtocolVersion) =>
146146
throw ADP.SNIPlatformNotSupported(_architecture);
147147

148148
public uint SniWriteAsyncWrapper(SNIHandle pConn, SNIPacket pPacket) =>

src/Microsoft.Data.SqlClient/src/Interop/Windows/Sni/SniNativeMethodsX64.netfx.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public uint SniSetInfo(SNIHandle pConn, QueryType queryType, ref uint pbQueryInf
148148
public uint SniTerminate() =>
149149
SNITerminate();
150150

151-
public uint SniWaitForSslHandshakeToComplete(SNIHandle pConn, int dwMilliseconds, out uint pProtocolVersion) =>
151+
public uint SniWaitForSslHandshakeToComplete(SNIHandle pConn, int dwMilliseconds, out SniSslProtocols pProtocolVersion) =>
152152
SNIWaitForSSLHandshakeToCompleteWrapper(pConn, dwMilliseconds, out pProtocolVersion);
153153

154154
public uint SniWriteAsyncWrapper(SNIHandle pConn, SNIPacket pPacket) =>
@@ -299,7 +299,7 @@ private static extern int SNIServerEnumReadWrapper(
299299
private static extern uint SNIWaitForSSLHandshakeToCompleteWrapper(
300300
[In] SNIHandle pConn,
301301
int dwMilliseconds,
302-
out uint pProtocolVersion);
302+
out SniSslProtocols pProtocolVersion);
303303

304304
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
305305
private static extern uint SNIWriteAsyncWrapper(SNIHandle pConn, [In] SNIPacket pPacket);

src/Microsoft.Data.SqlClient/src/Interop/Windows/Sni/SniNativeMethodsX86.netfx.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public uint SniSetInfo(SNIHandle pConn, QueryType queryType, ref uint pbQueryInf
148148
public uint SniTerminate() =>
149149
SNITerminate();
150150

151-
public uint SniWaitForSslHandshakeToComplete(SNIHandle pConn, int dwMilliseconds, out uint pProtocolVersion) =>
151+
public uint SniWaitForSslHandshakeToComplete(SNIHandle pConn, int dwMilliseconds, out SniSslProtocols pProtocolVersion) =>
152152
SNIWaitForSSLHandshakeToCompleteWrapper(pConn, dwMilliseconds, out pProtocolVersion);
153153

154154
public uint SniWriteAsyncWrapper(SNIHandle pConn, SNIPacket pPacket) =>
@@ -299,7 +299,7 @@ private static extern int SNIServerEnumReadWrapper(
299299
private static extern uint SNIWaitForSSLHandshakeToCompleteWrapper(
300300
[In] SNIHandle pConn,
301301
int dwMilliseconds,
302-
out uint pProtocolVersion);
302+
out SniSslProtocols pProtocolVersion);
303303

304304
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
305305
private static extern uint SNIWriteAsyncWrapper(SNIHandle pConn, [In] SNIPacket pPacket);

src/Microsoft.Data.SqlClient/src/Interop/Windows/Sni/SniNativeWrapper.cs

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -376,19 +376,67 @@ internal static uint SniSetInfo(SNIHandle pConn, QueryType qType, ref uint pbQIn
376376

377377
internal static uint SniTerminate() =>
378378
s_nativeMethods.SniTerminate();
379-
379+
380380
internal static uint SniWaitForSslHandshakeToComplete(
381381
SNIHandle pConn,
382382
int dwMilliseconds,
383-
out uint pProtocolVersion) =>
384-
s_nativeMethods.SniWaitForSslHandshakeToComplete(pConn, dwMilliseconds, out pProtocolVersion);
383+
out System.Security.Authentication.SslProtocols pProtocolVersion)
384+
{
385+
uint returnValue = s_nativeMethods.SniWaitForSslHandshakeToComplete(pConn, dwMilliseconds, out SniSslProtocols nativeProtocolVersion);
386+
387+
#pragma warning disable CA5398 // Avoid hardcoded SslProtocols values
388+
if ((nativeProtocolVersion & SniSslProtocols.SP_PROT_TLS1_2) != 0)
389+
{
390+
pProtocolVersion = System.Security.Authentication.SslProtocols.Tls12;
391+
}
392+
else if ((nativeProtocolVersion & SniSslProtocols.SP_PROT_TLS1_3) != 0)
393+
{
394+
#if NET
395+
pProtocolVersion = System.Security.Authentication.SslProtocols.Tls13;
396+
#else
397+
// Only .NET Core supports SslProtocols.Tls13
398+
pProtocolVersion = (System.Security.Authentication.SslProtocols)0x3000;
399+
#endif
400+
}
401+
else if ((nativeProtocolVersion & SniSslProtocols.SP_PROT_TLS1_1) != 0)
402+
{
403+
#if NET8_0_OR_GREATER
404+
#pragma warning disable SYSLIB0039 // Type or member is obsolete: TLS 1.0 & 1.1 are deprecated
405+
#endif
406+
pProtocolVersion = System.Security.Authentication.SslProtocols.Tls11;
407+
}
408+
else if ((nativeProtocolVersion & SniSslProtocols.SP_PROT_TLS1_0) != 0)
409+
{
410+
pProtocolVersion = System.Security.Authentication.SslProtocols.Tls;
411+
#if NET8_0_OR_GREATER
412+
#pragma warning restore SYSLIB0039 // Type or member is obsolete: SSL and TLS 1.0 & 1.1 is deprecated
413+
#endif
414+
}
415+
else if ((nativeProtocolVersion & SniSslProtocols.SP_PROT_SSL3) != 0)
416+
{
417+
// SSL 2.0 and 3.0 are only referenced to log a warning, not explicitly used for connections
418+
#pragma warning disable CS0618, CA5397
419+
pProtocolVersion = System.Security.Authentication.SslProtocols.Ssl3;
420+
}
421+
else if ((nativeProtocolVersion & SniSslProtocols.SP_PROT_SSL2) != 0)
422+
{
423+
pProtocolVersion = System.Security.Authentication.SslProtocols.Ssl2;
424+
#pragma warning restore CS0618, CA5397
425+
}
426+
else
427+
{
428+
pProtocolVersion = System.Security.Authentication.SslProtocols.None;
429+
}
430+
#pragma warning restore CA5398 // Avoid hardcoded SslProtocols values
431+
return returnValue;
432+
}
385433

386434
internal static uint SniWritePacket(SNIHandle pConn, SNIPacket packet, bool sync) =>
387435
sync
388436
? s_nativeMethods.SniWriteSyncOverAsync(pConn, packet)
389437
: s_nativeMethods.SniWriteAsyncWrapper(pConn, packet);
390438

391-
#endregion
439+
#endregion
392440

393441
#region Private Methods
394442

0 commit comments

Comments
 (0)