From 5b0ace9cbbe301f68b283f101b29facf576756e9 Mon Sep 17 00:00:00 2001 From: Davoud Date: Wed, 14 Jun 2023 12:01:06 -0700 Subject: [PATCH 1/7] add Auth API --- .../SqlClient/TdsParserStateObjectManaged.cs | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) 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..c451b7d05e 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,25 @@ 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)!; + if (statusCode is not NegotiateAuthenticationStatusCode.Completed and not NegotiateAuthenticationStatusCode.ContinueNeeded) { - _sspiClientContextStatus = new SspiClientContextStatus(); + throw new InvalidOperationException(SQLMessage.SSPIGenerateError() + "\n" + 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; } From 7840fb89421d09a203a064b148208c9ed943a919 Mon Sep 17 00:00:00 2001 From: Davoud Date: Wed, 14 Jun 2023 13:36:22 -0700 Subject: [PATCH 2/7] Improve TdsParser --- .../src/Microsoft/Data/SqlClient/TdsParser.cs | 177 ++++++++-------- .../src/Microsoft/Data/SqlClient/TdsParser.cs | 198 ++++++++++-------- 2 files changed, 202 insertions(+), 173 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs index c83587ab4c..32a559a855 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs @@ -8159,43 +8159,54 @@ internal void TdsLogin(SqlLogin rec, TdsEnums.FeatureExtension requestedFeatures } int feOffset = length; + // calculate and reserve the required bytes for the featureEx + length = ApplyFeatureExData(requestedFeatures, recoverySessionData, fedAuthFeatureExtensionData, useFeatureExt, length); + + WriteLoginData(rec, + requestedFeatures, + recoverySessionData, + fedAuthFeatureExtensionData, + encrypt, + encryptedPassword, + encryptedChangePassword, + encryptedPasswordLengthInBytes, + encryptedChangePasswordLengthInBytes, + useFeatureExt, + userName, + length, + feOffset, + clientInterfaceName, + outSSPIBuff, + outSSPILength); - if (useFeatureExt) + if (rentedSSPIBuff != null) { - if ((requestedFeatures & TdsEnums.FeatureExtension.SessionRecovery) != 0) - { - length += WriteSessionRecoveryFeatureRequest(recoverySessionData, false); - } - if ((requestedFeatures & TdsEnums.FeatureExtension.FedAuth) != 0) - { - Debug.Assert(fedAuthFeatureExtensionData != null, "fedAuthFeatureExtensionData should not null."); - length += WriteFedAuthFeatureRequest(fedAuthFeatureExtensionData, write: false); - } - if ((requestedFeatures & TdsEnums.FeatureExtension.Tce) != 0) - { - length += WriteTceFeatureRequest(false); - } - if ((requestedFeatures & TdsEnums.FeatureExtension.GlobalTransactions) != 0) - { - length += WriteGlobalTransactionsFeatureRequest(false); - } - if ((requestedFeatures & TdsEnums.FeatureExtension.DataClassification) != 0) - { - length += WriteDataClassificationFeatureRequest(false); - } - if ((requestedFeatures & TdsEnums.FeatureExtension.UTF8Support) != 0) - { - length += WriteUTF8SupportFeatureRequest(false); - } - - if ((requestedFeatures & TdsEnums.FeatureExtension.SQLDNSCaching) != 0) - { - length += WriteSQLDNSCachingFeatureRequest(false); - } - - length++; // for terminator + ArrayPool.Shared.Return(rentedSSPIBuff, clearArray: true); } + _physicalStateObj.WritePacket(TdsEnums.HARDFLUSH); + _physicalStateObj.ResetSecurePasswordsInformation(); + _physicalStateObj.HasPendingData = true; + _physicalStateObj._messageStatus = 0; + }// tdsLogin + + private void WriteLoginData(SqlLogin rec, + TdsEnums.FeatureExtension requestedFeatures, + SessionData recoverySessionData, + FederatedAuthenticationFeatureExtensionData fedAuthFeatureExtensionData, + SqlConnectionEncryptOption encrypt, + byte[] encryptedPassword, + byte[] encryptedChangePassword, + int encryptedPasswordLengthInBytes, + int encryptedChangePasswordLengthInBytes, + bool useFeatureExt, + string userName, + int length, + int featureExOffset, + string clientInterfaceName, + byte[] outSSPIBuff, + uint outSSPILength) + { try { WriteInt(length, _physicalStateObj); @@ -8409,7 +8420,7 @@ internal void TdsLogin(SqlLogin rec, TdsEnums.FeatureExtension requestedFeatures SqlClientEventSource.Log.TryTraceEvent(" Sending federated authentication feature request"); } - WriteInt(feOffset, _physicalStateObj); + WriteInt(featureExOffset, _physicalStateObj); } WriteString(clientInterfaceName, _physicalStateObj); @@ -8433,42 +8444,7 @@ internal void TdsLogin(SqlLogin rec, TdsEnums.FeatureExtension requestedFeatures } } - if (useFeatureExt) - { - if ((requestedFeatures & TdsEnums.FeatureExtension.SessionRecovery) != 0) - { - WriteSessionRecoveryFeatureRequest(recoverySessionData, true); - } - if ((requestedFeatures & TdsEnums.FeatureExtension.FedAuth) != 0) - { - SqlClientEventSource.Log.TryTraceEvent(" Sending federated authentication feature request"); - Debug.Assert(fedAuthFeatureExtensionData != null, "fedAuthFeatureExtensionData should not null."); - WriteFedAuthFeatureRequest(fedAuthFeatureExtensionData, write: true); - } - if ((requestedFeatures & TdsEnums.FeatureExtension.Tce) != 0) - { - WriteTceFeatureRequest(true); - } - if ((requestedFeatures & TdsEnums.FeatureExtension.GlobalTransactions) != 0) - { - WriteGlobalTransactionsFeatureRequest(true); - } - if ((requestedFeatures & TdsEnums.FeatureExtension.DataClassification) != 0) - { - WriteDataClassificationFeatureRequest(true); - } - if ((requestedFeatures & TdsEnums.FeatureExtension.UTF8Support) != 0) - { - WriteUTF8SupportFeatureRequest(true); - } - - if ((requestedFeatures & TdsEnums.FeatureExtension.SQLDNSCaching) != 0) - { - WriteSQLDNSCachingFeatureRequest(true); - } - - _physicalStateObj.WriteByte(0xFF); // terminator - } + ApplyFeatureExData(requestedFeatures, recoverySessionData, fedAuthFeatureExtensionData, useFeatureExt, length, true); } catch (Exception e) { @@ -8481,17 +8457,58 @@ internal void TdsLogin(SqlLogin rec, TdsEnums.FeatureExtension requestedFeatures throw; } + } - if (rentedSSPIBuff != null) + private int ApplyFeatureExData(TdsEnums.FeatureExtension requestedFeatures, + SessionData recoverySessionData, + FederatedAuthenticationFeatureExtensionData fedAuthFeatureExtensionData, + bool useFeatureExt, + int length, + bool write = false) + { + if (useFeatureExt) { - ArrayPool.Shared.Return(rentedSSPIBuff, clearArray: true); + if ((requestedFeatures & TdsEnums.FeatureExtension.SessionRecovery) != 0) + { + length += WriteSessionRecoveryFeatureRequest(recoverySessionData, write); + } + if ((requestedFeatures & TdsEnums.FeatureExtension.FedAuth) != 0) + { + SqlClientEventSource.Log.TryTraceEvent(" Sending federated authentication feature request & wirte = {0}", write); + Debug.Assert(fedAuthFeatureExtensionData != null, "fedAuthFeatureExtensionData should not null."); + length += WriteFedAuthFeatureRequest(fedAuthFeatureExtensionData, write: write); + } + if ((requestedFeatures & TdsEnums.FeatureExtension.Tce) != 0) + { + length += WriteTceFeatureRequest(write); + } + if ((requestedFeatures & TdsEnums.FeatureExtension.GlobalTransactions) != 0) + { + length += WriteGlobalTransactionsFeatureRequest(write); + } + if ((requestedFeatures & TdsEnums.FeatureExtension.DataClassification) != 0) + { + length += WriteDataClassificationFeatureRequest(write); + } + if ((requestedFeatures & TdsEnums.FeatureExtension.UTF8Support) != 0) + { + length += WriteUTF8SupportFeatureRequest(write); + } + + if ((requestedFeatures & TdsEnums.FeatureExtension.SQLDNSCaching) != 0) + { + length += WriteSQLDNSCachingFeatureRequest(write); + } + + length++; // for terminator + if (write) + { + _physicalStateObj.WriteByte(0xFF); // terminator + } } - _physicalStateObj.WritePacket(TdsEnums.HARDFLUSH); - _physicalStateObj.ResetSecurePasswordsInformation(); - _physicalStateObj.HasPendingData = true; - _physicalStateObj._messageStatus = 0; - }// tdsLogin + return length; + } /// /// Send the access token to the server. @@ -8524,12 +8541,6 @@ internal void SendFedAuthToken(SqlFedAuthToken fedAuthToken) } private void SSPIData(byte[] receivedBuff, uint receivedLength, ref byte[] sendBuff, ref uint sendLength) - { - SNISSPIData(receivedBuff, receivedLength, ref sendBuff, ref sendLength); - } - - - private void SNISSPIData(byte[] receivedBuff, uint receivedLength, ref byte[] sendBuff, ref uint sendLength) { if (TdsParserStateObjectFactory.UseManagedSNI) { diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs index c407e1c6e9..d340be58c9 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs @@ -8992,7 +8992,7 @@ internal void TdsLogin(SqlLogin rec, Debug.Assert(SniContext.Snix_Login == _physicalStateObj.SniContext, $"Unexpected SniContext. Expecting Snix_Login, actual value is '{_physicalStateObj.SniContext}'"); _physicalStateObj.SniContext = SniContext.Snix_LoginSspi; SSPIData(null, 0, outSSPIBuff, ref outSSPILength); - if (outSSPILength > Int32.MaxValue) + if (outSSPILength > int.MaxValue) { throw SQL.InvalidSSPIPacketSize(); // SqlBu 332503 } @@ -9000,56 +9000,74 @@ internal void TdsLogin(SqlLogin rec, checked { - length += (Int32)outSSPILength; + length += (int)outSSPILength; } } } int feOffset = length; + // calculate and reserve the required bytes for the featureEx + length = ApplyFeatureExData(requestedFeatures, recoverySessionData, fedAuthFeatureExtensionData, useFeatureExt, length); + + WriteLoginData(rec, + requestedFeatures, + recoverySessionData, + fedAuthFeatureExtensionData, + encrypt, + encryptedPassword, + encryptedChangePassword, + encryptedPasswordLengthInBytes, + encryptedChangePasswordLengthInBytes, + useFeatureExt, + userName, + length, + feOffset, + clientInterfaceName, + outSSPIBuff, + outSSPILength); - if (useFeatureExt) + _physicalStateObj.WritePacket(TdsEnums.HARDFLUSH); + _physicalStateObj.ResetSecurePasswordsInfomation(); // Password information is needed only from Login process; done with writing login packet and should clear information + _physicalStateObj._pendingData = true; + _physicalStateObj._messageStatus = 0; + + // Remvove CTAIP Provider after login record is sent. + // + if (originalNetworkAddressInfo != null) { - checked + UInt32 error = SNINativeMethodWrapper.SNIRemoveProvider(_physicalStateObj.Handle, SNINativeMethodWrapper.ProviderEnum.CTAIP_PROV); + if (error != TdsEnums.SNI_SUCCESS) { - if ((requestedFeatures & TdsEnums.FeatureExtension.SessionRecovery) != 0) - { - length += WriteSessionRecoveryFeatureRequest(recoverySessionData, false); - }; - if ((requestedFeatures & TdsEnums.FeatureExtension.FedAuth) != 0) - { - Debug.Assert(fedAuthFeatureExtensionData != null, "fedAuthFeatureExtensionData should not null."); - length += WriteFedAuthFeatureRequest(fedAuthFeatureExtensionData, write: false); - } - if ((requestedFeatures & TdsEnums.FeatureExtension.Tce) != 0) - { - length += WriteTceFeatureRequest(false); - } - if ((requestedFeatures & TdsEnums.FeatureExtension.GlobalTransactions) != 0) - { - length += WriteGlobalTransactionsFeatureRequest(false); - } - if ((requestedFeatures & TdsEnums.FeatureExtension.AzureSQLSupport) != 0) - { - length += WriteAzureSQLSupportFeatureRequest(false); - } - if ((requestedFeatures & TdsEnums.FeatureExtension.DataClassification) != 0) - { - length += WriteDataClassificationFeatureRequest(false); - } - if ((requestedFeatures & TdsEnums.FeatureExtension.UTF8Support) != 0) - { - length += WriteUTF8SupportFeatureRequest(false); - } - - if ((requestedFeatures & TdsEnums.FeatureExtension.SQLDNSCaching) != 0) - { - length += WriteSQLDNSCachingFeatureRequest(false); - } + _physicalStateObj.AddError(ProcessSNIError(_physicalStateObj)); + ThrowExceptionAndWarning(_physicalStateObj); + } - length++; // for terminator + try + { } // EmptyTry/Finally to avoid FXCop violation + finally + { + _physicalStateObj.ClearAllWritePackets(); } } + }// tdsLogin + private void WriteLoginData(SqlLogin rec, + TdsEnums.FeatureExtension requestedFeatures, + SessionData recoverySessionData, + FederatedAuthenticationFeatureExtensionData fedAuthFeatureExtensionData, + SqlConnectionEncryptOption encrypt, + byte[] encryptedPassword, + byte[] encryptedChangePassword, + int encryptedPasswordLengthInBytes, + int encryptedChangePasswordLengthInBytes, + bool useFeatureExt, + string userName, + int length, + int featureExOffset, + string clientInterfaceName, + byte[] outSSPIBuff, + uint outSSPILength) + { try { WriteInt(length, _physicalStateObj); @@ -9265,7 +9283,12 @@ internal void TdsLogin(SqlLogin rec, // write ibFeatureExtLong if (useFeatureExt) { - WriteInt(feOffset, _physicalStateObj); + if ((requestedFeatures & TdsEnums.FeatureExtension.FedAuth) != 0) + { + SqlClientEventSource.Log.TryTraceEvent(" Sending federated authentication feature request"); + } + + WriteInt(featureExOffset, _physicalStateObj); } WriteString(clientInterfaceName, _physicalStateObj); @@ -9292,84 +9315,79 @@ internal void TdsLogin(SqlLogin rec, } } - if (useFeatureExt) + ApplyFeatureExData(requestedFeatures, recoverySessionData, fedAuthFeatureExtensionData, useFeatureExt, length, true); + } // try + catch (Exception e) + { + // UNDONE - should not be catching all exceptions!!! + if (ADP.IsCatchableExceptionType(e)) + { + // be sure to wipe out our buffer if we started sending stuff + _physicalStateObj.ResetPacketCounters(); + _physicalStateObj.ResetBuffer(); + } + + throw; + } + } + + private int ApplyFeatureExData(TdsEnums.FeatureExtension requestedFeatures, + SessionData recoverySessionData, + FederatedAuthenticationFeatureExtensionData fedAuthFeatureExtensionData, + bool useFeatureExt, + int length, + bool write = false) + { + if (useFeatureExt) + { + checked { if ((requestedFeatures & TdsEnums.FeatureExtension.SessionRecovery) != 0) { - WriteSessionRecoveryFeatureRequest(recoverySessionData, true); + length += WriteSessionRecoveryFeatureRequest(recoverySessionData, write); }; if ((requestedFeatures & TdsEnums.FeatureExtension.FedAuth) != 0) { - SqlClientEventSource.Log.TryTraceEvent(" Sending federated authentication feature request"); + SqlClientEventSource.Log.TryTraceEvent(" Sending federated authentication feature request & wirte = {0}", write); Debug.Assert(fedAuthFeatureExtensionData != null, "fedAuthFeatureExtensionData should not null."); - WriteFedAuthFeatureRequest(fedAuthFeatureExtensionData, write: true); - }; + length += WriteFedAuthFeatureRequest(fedAuthFeatureExtensionData, write: write); + } if ((requestedFeatures & TdsEnums.FeatureExtension.Tce) != 0) { - WriteTceFeatureRequest(true); - }; + length += WriteTceFeatureRequest(write); + } if ((requestedFeatures & TdsEnums.FeatureExtension.GlobalTransactions) != 0) { - WriteGlobalTransactionsFeatureRequest(true); - }; + length += WriteGlobalTransactionsFeatureRequest(write); + } if ((requestedFeatures & TdsEnums.FeatureExtension.AzureSQLSupport) != 0) { - WriteAzureSQLSupportFeatureRequest(true); + length += WriteAzureSQLSupportFeatureRequest(write); } if ((requestedFeatures & TdsEnums.FeatureExtension.DataClassification) != 0) { - WriteDataClassificationFeatureRequest(true); + length += WriteDataClassificationFeatureRequest(write); } if ((requestedFeatures & TdsEnums.FeatureExtension.UTF8Support) != 0) { - WriteUTF8SupportFeatureRequest(true); + length += WriteUTF8SupportFeatureRequest(write); } if ((requestedFeatures & TdsEnums.FeatureExtension.SQLDNSCaching) != 0) { - WriteSQLDNSCachingFeatureRequest(true); + length += WriteSQLDNSCachingFeatureRequest(write); } - _physicalStateObj.WriteByte(0xFF); // terminator - } - } // try - catch (Exception e) - { - // UNDONE - should not be catching all exceptions!!! - if (ADP.IsCatchableExceptionType(e)) - { - // be sure to wipe out our buffer if we started sending stuff - _physicalStateObj.ResetPacketCounters(); - _physicalStateObj.ResetBuffer(); + length++; // for terminator + if (write) + { + _physicalStateObj.WriteByte(0xFF); // terminator + } } - - throw; } - _physicalStateObj.WritePacket(TdsEnums.HARDFLUSH); - _physicalStateObj.ResetSecurePasswordsInfomation(); // Password information is needed only from Login process; done with writing login packet and should clear information - _physicalStateObj._pendingData = true; - _physicalStateObj._messageStatus = 0; - - // Remvove CTAIP Provider after login record is sent. - // - if (originalNetworkAddressInfo != null) - { - UInt32 error = SNINativeMethodWrapper.SNIRemoveProvider(_physicalStateObj.Handle, SNINativeMethodWrapper.ProviderEnum.CTAIP_PROV); - if (error != TdsEnums.SNI_SUCCESS) - { - _physicalStateObj.AddError(ProcessSNIError(_physicalStateObj)); - ThrowExceptionAndWarning(_physicalStateObj); - } - - try - { } // EmptyTry/Finally to avoid FXCop violation - finally - { - _physicalStateObj.ClearAllWritePackets(); - } - } - }// tdsLogin + return length; + } /// /// Send the access token to the server. From 7ca50a803639bdd3ccd819bfd85ad284fc51a76b Mon Sep 17 00:00:00 2001 From: Davoud Date: Thu, 15 Jun 2023 11:50:22 -0700 Subject: [PATCH 3/7] Add event source log --- .../src/Microsoft/Data/SqlClient/TdsParserStateObjectManaged.cs | 1 + 1 file changed, 1 insertion(+) 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 c451b7d05e..e2cd7ff419 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 @@ -398,6 +398,7 @@ internal override uint GenerateSspiClientContext(byte[] receivedBuff, #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) { throw new InvalidOperationException(SQLMessage.SSPIGenerateError() + "\n" + statusCode); From 185c4cb94944f1d5672f2f67096256062dbe4e60 Mon Sep 17 00:00:00 2001 From: Davoud Date: Fri, 16 Jun 2023 13:43:36 -0700 Subject: [PATCH 4/7] Address comments --- .../netcore/src/Microsoft/Data/SqlClient/SNI/SNIProxy.cs | 4 ++-- .../Microsoft/Data/SqlClient/TdsParserStateObjectManaged.cs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) 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 ac4d3599dd..b43903923e 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 @@ -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); } } } 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 e2cd7ff419..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 @@ -401,7 +401,7 @@ internal override uint GenerateSspiClientContext(byte[] receivedBuff, SqlClientEventSource.Log.TryTraceEvent("TdsParserStateObjectManaged.GenerateSspiClientContext | Info | Session Id {0}, StatusCode={1}", _sessionHandle?.ConnectionId, statusCode); if (statusCode is not NegotiateAuthenticationStatusCode.Completed and not NegotiateAuthenticationStatusCode.ContinueNeeded) { - throw new InvalidOperationException(SQLMessage.SSPIGenerateError() + "\n" + statusCode); + throw new InvalidOperationException(SQLMessage.SSPIGenerateError() + Environment.NewLine + statusCode); } #else _sspiClientContextStatus ??= new SspiClientContextStatus(); From 6cb8b4dfbabec08ed3e16b48713ee945ce625df8 Mon Sep 17 00:00:00 2001 From: Davoud Date: Mon, 19 Jun 2023 15:38:07 -0700 Subject: [PATCH 5/7] Take out dependency on .NET 6.0 --- .../src/Microsoft.Data.SqlClient.csproj | 143 ++++++++++-------- .../Microsoft/Data/SqlClient/SNI/SNIProxy.cs | 4 +- 2 files changed, 79 insertions(+), 68 deletions(-) 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..83756def13 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 @@ - @@ -791,69 +790,12 @@ Common\Interop\Windows\SChannel\SecPkgContext_ConnectionInfo.cs - - Common\Interop\Windows\sspicli\GlobalSSPI.cs - - - Common\Interop\Windows\sspicli\Interop.SSPI.cs - - - Common\Interop\Windows\sspicli\NegotiationInfoClass.cs - - - Common\Interop\Windows\sspicli\SafeDeleteContext.cs - - - Common\Interop\Windows\sspicli\SecPkgContext_Bindings.cs - - - Common\Interop\Windows\sspicli\SecPkgContext_NegotiationInfoW.cs - - - Common\Interop\Windows\sspicli\SecPkgContext_Sizes.cs - - - Common\Interop\Windows\sspicli\SecPkgContext_StreamSizes.cs - - - Common\Interop\Windows\sspicli\SecurityPackageInfo.cs - - - Common\Interop\Windows\sspicli\SecurityPackageInfoClass.cs - - - Common\Interop\Windows\sspicli\SecuritySafeHandles.cs - - - Common\Interop\Windows\sspicli\SSPIAuthType.cs - - - Common\Interop\Windows\sspicli\SSPIInterface.cs - - - Common\Interop\Windows\sspicli\SSPISecureChannelType.cs - - - 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 @@ -894,6 +836,7 @@ Common\System\Net\SecurityStatusPal.cs + Common\Interop\Unix\Interop.Libraries.cs @@ -910,12 +853,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 @@ -938,6 +875,80 @@ + + + + + Common\System\Net\ContextFlagsAdapterPal.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 + + + Common\Interop\Windows\sspicli\Interop.SSPI.cs + + + Common\Interop\Windows\sspicli\NegotiationInfoClass.cs + + + Common\Interop\Windows\sspicli\SafeDeleteContext.cs + + + Common\Interop\Windows\sspicli\SecPkgContext_Bindings.cs + + + Common\Interop\Windows\sspicli\SecPkgContext_NegotiationInfoW.cs + + + Common\Interop\Windows\sspicli\SecPkgContext_Sizes.cs + + + Common\Interop\Windows\sspicli\SecPkgContext_StreamSizes.cs + + + Common\Interop\Windows\sspicli\SecurityPackageInfo.cs + + + Common\Interop\Windows\sspicli\SecurityPackageInfoClass.cs + + + Common\Interop\Windows\sspicli\SecuritySafeHandles.cs + + + Common\Interop\Windows\sspicli\SSPIAuthType.cs + + + Common\Interop\Windows\sspicli\SSPIInterface.cs + + + Common\Interop\Windows\sspicli\SSPISecureChannelType.cs + + + Common\Interop\Windows\sspicli\SSPIWrapper.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 b43903923e..b2db128717 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 /// @@ -125,7 +125,7 @@ private static bool IsErrorStatus(SecurityStatusPalErrorCode errorCode) errorCode != SecurityStatusPalErrorCode.CredentialsNeeded && errorCode != SecurityStatusPalErrorCode.Renegotiate; } - +#endif /// /// Create a SNI connection handle /// From 36661307700c33017a5568d557c32926b2de12b9 Mon Sep 17 00:00:00 2001 From: Davoud Date: Mon, 19 Jun 2023 16:06:10 -0700 Subject: [PATCH 6/7] Take out more dependency --- .../src/Microsoft.Data.SqlClient.csproj | 55 +++++++++---------- 1 file changed, 27 insertions(+), 28 deletions(-) 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 83756def13..afa47334ed 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj @@ -838,33 +838,6 @@ - - Common\Interop\Unix\Interop.Libraries.cs - - - Common\Interop\Unix\System.Net.Security.Native\Interop.GssApiException.cs - - - Common\Interop\Unix\System.Net.Security.Native\Interop.GssBuffer.cs - - - Common\Interop\Unix\System.Net.Security.Native\Interop.NetSecurityNative.cs - - - Common\Microsoft\Win32\SafeHandles\GssSafeHandles.cs - - - Common\System\Net\Security\Unix\SafeDeleteContext.cs - - - Common\System\Net\Security\Unix\SafeDeleteNegoContext.cs - - - Common\System\Net\Security\Unix\SafeFreeCredentials.cs - - - Common\System\Net\Security\Unix\SafeFreeNegoCredentials.cs - @@ -877,7 +850,6 @@ - Common\System\Net\ContextFlagsAdapterPal.Windows.cs @@ -942,6 +914,33 @@ + + Common\Interop\Unix\Interop.Libraries.cs + + + Common\Interop\Unix\System.Net.Security.Native\Interop.GssApiException.cs + + + Common\Interop\Unix\System.Net.Security.Native\Interop.GssBuffer.cs + + + Common\Interop\Unix\System.Net.Security.Native\Interop.NetSecurityNative.cs + + + Common\Microsoft\Win32\SafeHandles\GssSafeHandles.cs + + + Common\System\Net\Security\Unix\SafeDeleteContext.cs + + + Common\System\Net\Security\Unix\SafeDeleteNegoContext.cs + + + Common\System\Net\Security\Unix\SafeFreeCredentials.cs + + + Common\System\Net\Security\Unix\SafeFreeNegoCredentials.cs + Common\System\Net\ContextFlagsAdapterPal.Unix.cs From 1c28de4248e577403695e8e7f3101533ff0c818c Mon Sep 17 00:00:00 2001 From: Davoud Date: Thu, 22 Jun 2023 17:34:39 -0700 Subject: [PATCH 7/7] Take out more dependency --- .../src/Interop/Windows/Interop.Libraries.cs | 2 + .../src/Microsoft.Data.SqlClient.csproj | 110 +++++++++--------- 2 files changed, 57 insertions(+), 55 deletions(-) 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 afa47334ed..2dfdbca264 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj @@ -767,74 +767,20 @@ - + - - Common\CoreLib\Interop\Windows\Kernel32\Interop.CloseHandle.cs - - - Common\Interop\Windows\Crypt32\Interop.certificates.cs - - - Common\Interop\Windows\Crypt32\Interop.certificates_types.cs - Common\Interop\Windows\Interop.Libraries.cs - - Common\Interop\Windows\SChannel\Interop.SecPkgContext_ApplicationProtocol.cs - - - Common\Interop\Windows\SChannel\Interop.SECURITY_STATUS.cs - - - Common\Interop\Windows\SChannel\SecPkgContext_ConnectionInfo.cs - - - Common\System\Collections\Generic\BidirectionalDictionary.cs - - - Common\System\Net\DebugCriticalHandleZeroOrMinusOneIsInvalid.cs - - - Common\System\Net\Security\SecurityContextTokenHandle.cs - - - Common\System\Net\SecurityStatusAdapterPal.Windows.cs - - - Common\System\Net\ContextFlagsPal.cs - - - Common\System\Net\DebugCriticalHandleMinusOneIsInvalid.cs - - - 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 - - - Common\System\Net\Security\SecurityBuffer.cs - - - Common\System\Net\Security\SecurityBufferType.cs - - - Common\System\Net\SecurityStatusPal.cs - @@ -850,9 +796,39 @@ + + Common\CoreLib\Interop\Windows\Kernel32\Interop.CloseHandle.cs + + + Common\Interop\Windows\Crypt32\Interop.certificates.cs + + + Common\Interop\Windows\Crypt32\Interop.certificates_types.cs + + + Common\Interop\Windows\SChannel\Interop.SecPkgContext_ApplicationProtocol.cs + + + Common\Interop\Windows\SChannel\Interop.SECURITY_STATUS.cs + + + 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 @@ -910,6 +886,30 @@ + + Common\System\Net\ContextFlagsPal.cs + + + Common\System\Net\DebugCriticalHandleMinusOneIsInvalid.cs + + + Common\System\Net\DebugSafeHandle.cs + + + Common\System\Net\Logging\DebugThreadTracking.cs + + + Common\System\Net\NegotiationInfoClass.cs + + + Common\System\Net\Security\SecurityBuffer.cs + + + Common\System\Net\Security\SecurityBufferType.cs + + + Common\System\Net\SecurityStatusPal.cs +