diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Common/src/Interop/Windows/Interop.Libraries.cs b/src/Microsoft.Data.SqlClient/netcore/src/Common/src/Interop/Windows/Interop.Libraries.cs index 879bb0a38d..40308195e7 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Common/src/Interop/Windows/Interop.Libraries.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Common/src/Interop/Windows/Interop.Libraries.cs @@ -9,6 +9,8 @@ internal static partial class Libraries internal const string Crypt32 = "crypt32.dll"; internal const string Kernel32 = "kernel32.dll"; internal const string NtDll = "ntdll.dll"; +#if !NET7_0_OR_GREATER internal const string SspiCli = "sspicli.dll"; +#endif } } diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj index 66791366f3..2dfdbca264 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj @@ -89,7 +89,7 @@ Microsoft\Data\Sql\SqlDataSourceEnumeratorUtil.cs - + Microsoft\Data\Sql\SqlNotificationRequest.cs @@ -517,7 +517,7 @@ - + @@ -622,7 +622,6 @@ - @@ -768,8 +767,35 @@ - + + + Common\Interop\Windows\Interop.Libraries.cs + + + + + + Common\System\Net\InternalException.cs + + + Common\System\Net\Logging\NetEventSource.Common.cs + + + + + + + + + + + + + + + + Common\CoreLib\Interop\Windows\Kernel32\Interop.CloseHandle.cs @@ -779,9 +805,6 @@ Common\Interop\Windows\Crypt32\Interop.certificates_types.cs - - Common\Interop\Windows\Interop.Libraries.cs - Common\Interop\Windows\SChannel\Interop.SecPkgContext_ApplicationProtocol.cs @@ -791,6 +814,30 @@ Common\Interop\Windows\SChannel\SecPkgContext_ConnectionInfo.cs + + Common\System\Collections\Generic\BidirectionalDictionary.cs + + + Common\System\Net\ContextFlagsAdapterPal.Windows.cs + + + Common\System\Net\DebugCriticalHandleZeroOrMinusOneIsInvalid.cs + + + Common\System\Net\Security\SecurityContextTokenHandle.cs + + + Common\System\Net\SecurityStatusAdapterPal.Windows.cs + + + Common\System\Net\Security\NegotiateStreamPal.Windows.cs + + + Common\System\Net\Security\NetEventSource.Security.cs + + + Common\System\Net\Security\NetEventSource.Security.Windows.cs + Common\Interop\Windows\sspicli\GlobalSSPI.cs @@ -836,33 +883,9 @@ Common\Interop\Windows\sspicli\SSPIWrapper.cs - - Common\System\Collections\Generic\BidirectionalDictionary.cs - - - Common\System\Net\ContextFlagsAdapterPal.Windows.cs - - - Common\System\Net\DebugCriticalHandleZeroOrMinusOneIsInvalid.cs - - - Common\System\Net\Security\NegotiateStreamPal.Windows.cs - - - Common\System\Net\Security\NetEventSource.Security.cs - - - Common\System\Net\Security\NetEventSource.Security.Windows.cs - - - Common\System\Net\Security\SecurityContextTokenHandle.cs - - - Common\System\Net\SecurityStatusAdapterPal.Windows.cs - - - + + Common\System\Net\ContextFlagsPal.cs @@ -872,15 +895,9 @@ Common\System\Net\DebugSafeHandle.cs - - Common\System\Net\InternalException.cs - Common\System\Net\Logging\DebugThreadTracking.cs - - Common\System\Net\Logging\NetEventSource.Common.cs - Common\System\Net\NegotiationInfoClass.cs @@ -893,8 +910,10 @@ Common\System\Net\SecurityStatusPal.cs + - + + Common\Interop\Unix\Interop.Libraries.cs @@ -910,12 +929,6 @@ Common\Microsoft\Win32\SafeHandles\GssSafeHandles.cs - - Common\System\Net\ContextFlagsAdapterPal.Unix.cs - - - Common\System\Net\Security\NegotiateStreamPal.Unix.cs - Common\System\Net\Security\Unix\SafeDeleteContext.cs @@ -928,15 +941,12 @@ Common\System\Net\Security\Unix\SafeFreeNegoCredentials.cs - - - - - - - - - + + Common\System\Net\ContextFlagsAdapterPal.Unix.cs + + + Common\System\Net\Security\NegotiateStreamPal.Unix.cs + diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIProxy.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIProxy.cs index 0e8f2feeca..f1140fcded 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIProxy.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIProxy.cs @@ -24,7 +24,7 @@ internal class SNIProxy private static readonly SNIProxy s_singleton = new SNIProxy(); internal static SNIProxy Instance => s_singleton; - +#if !NET7_0_OR_GREATER /// /// Generate SSPI context /// @@ -105,11 +105,11 @@ internal static void GenSspiClientContext(SspiClientContextStatus sspiClientCont // so we don't need to check for a GssApiException here. if (statusCode.ErrorCode == SecurityStatusPalErrorCode.InternalError) { - throw new InvalidOperationException(SQLMessage.KerberosTicketMissingError() + "\n" + statusCode); + throw new InvalidOperationException(SQLMessage.KerberosTicketMissingError() + Environment.NewLine + statusCode); } else { - throw new InvalidOperationException(SQLMessage.SSPIGenerateError() + "\n" + statusCode); + throw new InvalidOperationException(SQLMessage.SSPIGenerateError() + Environment.NewLine + statusCode); } } } @@ -125,7 +125,7 @@ private static bool IsErrorStatus(SecurityStatusPalErrorCode errorCode) errorCode != SecurityStatusPalErrorCode.CredentialsNeeded && errorCode != SecurityStatusPalErrorCode.Renegotiate; } - +#endif /// /// Create a SNI connection handle /// diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObjectManaged.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObjectManaged.cs index f219e75406..6d56dbf491 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObjectManaged.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObjectManaged.cs @@ -7,7 +7,9 @@ using System; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; +using System.Net.Security; using System.Runtime.CompilerServices; +using System.Text; using System.Threading; using System.Threading.Tasks; using Microsoft.Data.Common; @@ -18,8 +20,11 @@ internal sealed class TdsParserStateObjectManaged : TdsParserStateObject { private SNIMarsConnection? _marsConnection; private SNIHandle? _sessionHandle; +#if NET7_0_OR_GREATER + private NegotiateAuthentication? _negotiateAuth = null; +#else private SspiClientContextStatus? _sspiClientContextStatus; - +#endif public TdsParserStateObjectManaged(TdsParser parser) : base(parser) { } internal TdsParserStateObjectManaged(TdsParser parser, TdsParserStateObject physicalConnection, bool async) : @@ -384,15 +389,26 @@ internal override uint SetConnectionBufferSize(ref uint unsignedPacketSize) return TdsEnums.SNI_SUCCESS; } - internal override uint GenerateSspiClientContext(byte[] receivedBuff, uint receivedLength, ref byte[] sendBuff, ref uint sendLength, byte[][] _sniSpnBuffer) + internal override uint GenerateSspiClientContext(byte[] receivedBuff, + uint receivedLength, + ref byte[] sendBuff, + ref uint sendLength, + byte[][] _sniSpnBuffer) { - if (_sspiClientContextStatus is null) +#if NET7_0_OR_GREATER + _negotiateAuth ??= new(new NegotiateAuthenticationClientOptions { Package = "Negotiate", TargetName = Encoding.Unicode.GetString(_sniSpnBuffer[0]) }); + sendBuff = _negotiateAuth.GetOutgoingBlob(receivedBuff, out NegotiateAuthenticationStatusCode statusCode)!; + SqlClientEventSource.Log.TryTraceEvent("TdsParserStateObjectManaged.GenerateSspiClientContext | Info | Session Id {0}, StatusCode={1}", _sessionHandle?.ConnectionId, statusCode); + if (statusCode is not NegotiateAuthenticationStatusCode.Completed and not NegotiateAuthenticationStatusCode.ContinueNeeded) { - _sspiClientContextStatus = new SspiClientContextStatus(); + throw new InvalidOperationException(SQLMessage.SSPIGenerateError() + Environment.NewLine + statusCode); } +#else + _sspiClientContextStatus ??= new SspiClientContextStatus(); SNIProxy.GenSspiClientContext(_sspiClientContextStatus, receivedBuff, ref sendBuff, _sniSpnBuffer); SqlClientEventSource.Log.TryTraceEvent("TdsParserStateObjectManaged.GenerateSspiClientContext | Info | Session Id {0}", _sessionHandle?.ConnectionId); +#endif sendLength = (uint)(sendBuff != null ? sendBuff.Length : 0); return 0; }