Skip to content

Commit cfb007d

Browse files
benrr101edwardneal
andauthored
Port #3126 to release/6.0 (#3127)
* Fix down-level SSL/TLS version warnings (#3126) * Added test for downlevel connectivity warning * Correctly test bit flags for legacy SSL protocol warning * Corrected warning disablement/restore. (cherry picked from commit 198b906) * Test by rolling back changes to connection test matrix --------- Co-authored-by: Edward Neal <[email protected]>
1 parent 955ac52 commit cfb007d

File tree

10 files changed

+42
-27
lines changed

10 files changed

+42
-27
lines changed

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserHelperClasses.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -768,30 +768,30 @@ private static string ToFriendlyName(this SslProtocols protocol)
768768
name = "TLS 1.3";
769769
}*/
770770
#pragma warning disable CA5398 // Avoid hardcoded SslProtocols values
771-
if ((protocol & SslProtocols.Tls12) == SslProtocols.Tls12)
771+
if ((protocol & SslProtocols.Tls12) != SslProtocols.None)
772772
{
773773
name = "TLS 1.2";
774774
}
775775
#if NET
776776
#pragma warning disable SYSLIB0039 // Type or member is obsolete: TLS 1.0 & 1.1 are deprecated
777777
#endif
778-
else if ((protocol & SslProtocols.Tls11) == SslProtocols.Tls11)
778+
else if ((protocol & SslProtocols.Tls11) != SslProtocols.None)
779779
{
780780
name = "TLS 1.1";
781781
}
782-
else if ((protocol & SslProtocols.Tls) == SslProtocols.Tls)
782+
else if ((protocol & SslProtocols.Tls) != SslProtocols.None)
783783
{
784784
name = "TLS 1.0";
785785
}
786786
#if NET
787787
#pragma warning restore SYSLIB0039 // Type or member is obsolete: SSL and TLS 1.0 & 1.1 is deprecated
788788
#endif
789789
#pragma warning disable CS0618 // Type or member is obsolete: SSL is deprecated
790-
else if ((protocol & SslProtocols.Ssl3) == SslProtocols.Ssl3)
790+
else if ((protocol & SslProtocols.Ssl3) != SslProtocols.None)
791791
{
792792
name = "SSL 3.0";
793793
}
794-
else if ((protocol & SslProtocols.Ssl2) == SslProtocols.Ssl2)
794+
else if ((protocol & SslProtocols.Ssl2) != SslProtocols.None)
795795
#pragma warning restore CS0618 // Type or member is obsolete: SSL and TLS 1.0 & 1.1 is deprecated
796796
{
797797
name = "SSL 2.0";

src/Microsoft.Data.SqlClient/tests/ManualTests/DataCommon/ConnectionTestParameters.cs

+7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System;
66
using System.Collections.Generic;
77
using System.Linq;
8+
using System.Security.Authentication;
89
using System.Text;
910
using System.Threading.Tasks;
1011
using Microsoft.SqlServer.TDS.PreLogin;
@@ -26,15 +27,21 @@ public class ConnectionTestParameters
2627
public string HostNameInCertificate => _hnic;
2728
public bool TestResult => _result;
2829
public TDSPreLoginTokenEncryptionType TdsEncryptionType => _encryptionType;
30+
public SslProtocols EncryptionProtocols { get; }
2931

3032
public ConnectionTestParameters(TDSPreLoginTokenEncryptionType tdsEncryptionType, SqlConnectionEncryptOption encryptOption, bool trustServerCert, string cert, string hnic, bool result)
33+
: this(tdsEncryptionType, encryptOption, trustServerCert, cert, hnic, SslProtocols.Tls12, result)
34+
{ }
35+
36+
public ConnectionTestParameters(TDSPreLoginTokenEncryptionType tdsEncryptionType, SqlConnectionEncryptOption encryptOption, bool trustServerCert, string cert, string hnic, SslProtocols sslProtocols, bool result)
3137
{
3238
_encryptionOption = encryptOption;
3339
_trustServerCert = trustServerCert;
3440
_cert = cert;
3541
_hnic = hnic;
3642
_result = result;
3743
_encryptionType = tdsEncryptionType;
44+
EncryptionProtocols = sslProtocols;
3845
}
3946
}
4047
}

src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/ConnectionTestWithSSLCert/CertificateTestWithTdsServer.cs

+1
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ private void ConnectionTest(ConnectionTestParameters connectionTestParameters)
127127
#else
128128
new X509Certificate2(s_fullPathToPfx, "nopassword", X509KeyStorageFlags.UserKeySet),
129129
#endif
130+
encryptionProtocols: connectionTestParameters.EncryptionProtocols,
130131
encryptionType: connectionTestParameters.TdsEncryptionType);
131132

132133
builder = new(server.ConnectionString)

src/Microsoft.Data.SqlClient/tests/ManualTests/TracingTests/TestTdsServer.cs

+5-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Net;
88
using System.Net.Sockets;
99
using System.Runtime.CompilerServices;
10+
using System.Security.Authentication;
1011
using System.Security.Cryptography.X509Certificates;
1112
using Microsoft.SqlServer.TDS.EndPoint;
1213
using Microsoft.SqlServer.TDS.PreLogin;
@@ -31,7 +32,7 @@ public TestTdsServer(QueryEngine engine, TDSServerArguments args) : base(args)
3132

3233
public static TestTdsServer StartServerWithQueryEngine(QueryEngine engine, bool enableFedAuth = false, bool enableLog = false,
3334
int connectionTimeout = DefaultConnectionTimeout, [CallerMemberName] string methodName = "",
34-
X509Certificate2 encryptionCertificate = null, TDSPreLoginTokenEncryptionType encryptionType = TDSPreLoginTokenEncryptionType.NotSupported)
35+
X509Certificate2 encryptionCertificate = null, SslProtocols encryptionProtocols = SslProtocols.Tls12, TDSPreLoginTokenEncryptionType encryptionType = TDSPreLoginTokenEncryptionType.NotSupported)
3536
{
3637
TDSServerArguments args = new TDSServerArguments()
3738
{
@@ -48,6 +49,7 @@ public static TestTdsServer StartServerWithQueryEngine(QueryEngine engine, bool
4849
args.EncryptionCertificate = encryptionCertificate;
4950
}
5051

52+
args.EncryptionProtocols = encryptionProtocols;
5153
args.Encryption = encryptionType;
5254

5355
TestTdsServer server = engine == null ? new TestTdsServer(args) : new TestTdsServer(engine, args);
@@ -83,9 +85,9 @@ public static TestTdsServer StartServerWithQueryEngine(QueryEngine engine, bool
8385

8486
public static TestTdsServer StartTestServer(bool enableFedAuth = false, bool enableLog = false,
8587
int connectionTimeout = DefaultConnectionTimeout, [CallerMemberName] string methodName = "",
86-
X509Certificate2 encryptionCertificate = null, TDSPreLoginTokenEncryptionType encryptionType = TDSPreLoginTokenEncryptionType.NotSupported)
88+
X509Certificate2 encryptionCertificate = null, SslProtocols encryptionProtocols = SslProtocols.Tls12, TDSPreLoginTokenEncryptionType encryptionType = TDSPreLoginTokenEncryptionType.NotSupported)
8789
{
88-
return StartServerWithQueryEngine(null, enableFedAuth, enableLog, connectionTimeout, methodName, encryptionCertificate, encryptionType);
90+
return StartServerWithQueryEngine(null, enableFedAuth, enableLog, connectionTimeout, methodName, encryptionCertificate, encryptionProtocols, encryptionType);
8991
}
9092

9193
public void Dispose() => _endpoint?.Stop();

src/Microsoft.Data.SqlClient/tests/tools/TDS/TDS.EndPoint/ITDSServerSession.cs

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System;
6+
using System.Security.Authentication;
67
using System.Security.Cryptography.X509Certificates;
78
using Microsoft.SqlServer.TDS.EndPoint.SSPI;
89

@@ -68,6 +69,11 @@ public interface ITDSServerSession
6869
/// </summary>
6970
X509Certificate EncryptionCertificate { get; }
7071

72+
/// <summary>
73+
/// SSL/TLS protocols to use for transport encryption
74+
/// </summary>
75+
SslProtocols EncryptionProtocols { get; }
76+
7177
/// <summary>
7278
/// Counter of connection reset requests for this session
7379
/// </summary>

src/Microsoft.Data.SqlClient/tests/tools/TDS/TDS.EndPoint/TDSParser.cs

+2-17
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@ public class TDSParser
2525
/// </summary>
2626
public TextWriter EventLog { get; set; }
2727

28-
/// <summary>
29-
/// Encryption protocol for server to use with AuthenticateAsServer
30-
/// </summary>
31-
public static SslProtocols ServerSslProtocol { get; set; }
32-
3328
/// <summary>
3429
/// Protocol stream between the client and the server
3530
/// </summary>
@@ -43,8 +38,6 @@ public TDSParser(Stream transport)
4338
// Save original transport
4439
_originalTransport = transport;
4540

46-
ServerSslProtocol = SslProtocols.Tls12;
47-
4841
// Wrap transport layer with TDS
4942
Transport = new TDSStream(transport, false);
5043
}
@@ -57,14 +50,6 @@ public void SetTDSStreamPreWriteCallback(Func<byte[], int, int, ushort> funcTDSS
5750
Transport.PreWriteCallBack = funcTDSStreamPreWriteCallBack;
5851
}
5952

60-
/// <summary>
61-
/// Resets the targeted encryption protocol for the server.
62-
/// </summary>
63-
public static void ResetTargetProtocol()
64-
{
65-
ServerSslProtocol = SslProtocols.Tls12;
66-
}
67-
6853
/// <summary>
6954
/// Enable transport encryption
7055
/// </summary>
@@ -105,7 +90,7 @@ protected void EnableClientTransportEncryption(string server)
10590
/// <summary>
10691
/// Enable transport encryption
10792
/// </summary>
108-
protected void EnableServerTransportEncryption(X509Certificate certificate)
93+
protected void EnableServerTransportEncryption(X509Certificate certificate, SslProtocols encryptionProtocols)
10994
{
11095
// Check if transport encryption is applied
11196
if (Transport.InnerStream is SslStream)
@@ -128,7 +113,7 @@ protected void EnableServerTransportEncryption(X509Certificate certificate)
128113
SslStream ssl = new SslStream(multiplexer, true);
129114

130115
// Secure the channel
131-
ssl.AuthenticateAsServer(certificate, false, ServerSslProtocol, false);
116+
ssl.AuthenticateAsServer(certificate, false, encryptionProtocols, false);
132117

133118
// Replace TDS stream with raw transport stream in multiplexer
134119
multiplexer.InnerStream = Transport.InnerStream;

src/Microsoft.Data.SqlClient/tests/tools/TDS/TDS.EndPoint/TDSServerParser.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public void Run()
146146
if (Session.Encryption == TDSEncryptionType.LoginOnly || Session.Encryption == TDSEncryptionType.Full)
147147
{
148148
// Enable server side encryption
149-
EnableServerTransportEncryption(Session.EncryptionCertificate);
149+
EnableServerTransportEncryption(Session.EncryptionCertificate, Session.EncryptionProtocols);
150150
}
151151
}
152152

src/Microsoft.Data.SqlClient/tests/tools/TDS/TDS.Servers/GenericTDSServer.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,9 @@ public virtual ITDSServerSession OpenSession()
8585
// Create a new session
8686
GenericTDSServerSession session = new GenericTDSServerSession(this, (uint)_sessionCount);
8787

88-
// Use configured encryption certificate
88+
// Use configured encryption certificate and protocols
8989
session.EncryptionCertificate = Arguments.EncryptionCertificate;
90+
session.EncryptionProtocols = Arguments.EncryptionProtocols;
9091

9192
return session;
9293
}

src/Microsoft.Data.SqlClient/tests/tools/TDS/TDS.Servers/GenericTDSServerSession.cs

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using System;
66
using System.Collections.Generic;
7+
using System.Security.Authentication;
78
using System.Security.Cryptography.X509Certificates;
89
using Microsoft.SqlServer.TDS.EndPoint;
910
using Microsoft.SqlServer.TDS.EndPoint.SSPI;
@@ -78,6 +79,11 @@ public class GenericTDSServerSession : ITDSServerSession
7879
/// </summary>
7980
public X509Certificate EncryptionCertificate { get; set; }
8081

82+
/// <summary>
83+
/// SSL/TLS protocols to use for transport encryption
84+
/// </summary>
85+
public SslProtocols EncryptionProtocols { get; set; }
86+
8187
/// <summary>
8288
/// Nonce option sent by client
8389
/// </summary>

src/Microsoft.Data.SqlClient/tests/tools/TDS/TDS.Servers/TDSServerArguments.cs

+7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using System;
66
using System.IO;
7+
using System.Security.Authentication;
78
using System.Security.Cryptography.X509Certificates;
89
using Microsoft.SqlServer.TDS.PreLogin;
910

@@ -69,6 +70,11 @@ public class TDSServerArguments
6970
/// </summary>
7071
public X509Certificate EncryptionCertificate { get; set; }
7172

73+
/// <summary>
74+
/// SSL/TLS protocols to use for transport encryption
75+
/// </summary>
76+
public SslProtocols EncryptionProtocols { get; set; }
77+
7278
/// <summary>
7379
/// Initialization constructor
7480
/// </summary>
@@ -88,6 +94,7 @@ public TDSServerArguments()
8894
FedAuthRequiredPreLoginOption = TdsPreLoginFedAuthRequiredOption.FedAuthNotRequired;
8995

9096
EncryptionCertificate = new X509Certificate2("TdsServerCertificate.pfx", "SecretPassword123456");
97+
EncryptionProtocols = SslProtocols.Tls12;
9198

9299
ServerPrincipalName = AzureADServicePrincipalName;
93100
StsUrl = AzureADProductionTokenEndpoint;

0 commit comments

Comments
 (0)