Skip to content

Commit

Permalink
THRIFT-5743 add TLS1.3 to default protocols where available
Browse files Browse the repository at this point in the history
Client: netstd
Patch: Jens Geyer
  • Loading branch information
Jens-G committed Nov 17, 2023
1 parent fa123f9 commit a06eedc
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
23 changes: 17 additions & 6 deletions lib/netstd/Thrift/Transport/Client/TTlsSocketTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// under the License.

using System;
using System.Diagnostics;
using System.Net;
using System.Net.Security;
using System.Net.Sockets;
Expand Down Expand Up @@ -43,11 +44,19 @@ public class TTlsSocketTransport : TStreamTransport
private SslStream _secureStream;
private int _timeout;

#if NET7_0_OR_GREATER
public const SslProtocols DefaultSslProtocols = SslProtocols.Tls12 | SslProtocols.Tls13;
#else
public const SslProtocols DefaultSslProtocols = SslProtocols.Tls12;
#endif



public TTlsSocketTransport(TcpClient client, TConfiguration config,
X509Certificate2 certificate, bool isServer = false,
RemoteCertificateValidationCallback certValidator = null,
LocalCertificateSelectionCallback localCertificateSelectionCallback = null,
SslProtocols sslProtocols = SslProtocols.Tls12)
SslProtocols sslProtocols = DefaultSslProtocols)
: base(config)
{
_client = client;
Expand All @@ -74,7 +83,7 @@ public TTlsSocketTransport(IPAddress host, int port, TConfiguration config,
string certificatePath,
RemoteCertificateValidationCallback certValidator = null,
LocalCertificateSelectionCallback localCertificateSelectionCallback = null,
SslProtocols sslProtocols = SslProtocols.Tls12)
SslProtocols sslProtocols = DefaultSslProtocols)
: this(host, port, config, 0,
new X509Certificate2(certificatePath),
certValidator,
Expand All @@ -87,7 +96,7 @@ public TTlsSocketTransport(IPAddress host, int port, TConfiguration config,
X509Certificate2 certificate = null,
RemoteCertificateValidationCallback certValidator = null,
LocalCertificateSelectionCallback localCertificateSelectionCallback = null,
SslProtocols sslProtocols = SslProtocols.Tls12)
SslProtocols sslProtocols = DefaultSslProtocols)
: this(host, port, config, 0,
certificate,
certValidator,
Expand All @@ -100,7 +109,7 @@ public TTlsSocketTransport(IPAddress host, int port, TConfiguration config, int
X509Certificate2 certificate,
RemoteCertificateValidationCallback certValidator = null,
LocalCertificateSelectionCallback localCertificateSelectionCallback = null,
SslProtocols sslProtocols = SslProtocols.Tls12)
SslProtocols sslProtocols = DefaultSslProtocols)
: base(config)
{
_host = host;
Expand All @@ -118,7 +127,7 @@ public TTlsSocketTransport(string host, int port, TConfiguration config, int tim
X509Certificate2 certificate,
RemoteCertificateValidationCallback certValidator = null,
LocalCertificateSelectionCallback localCertificateSelectionCallback = null,
SslProtocols sslProtocols = SslProtocols.Tls12)
SslProtocols sslProtocols = DefaultSslProtocols)
: base(config)
{
try
Expand Down Expand Up @@ -237,7 +246,7 @@ public async Task SetupTlsAsync()
{
// Client authentication
var certs = _certificate != null
? new X509CertificateCollection {_certificate}
? new X509CertificateCollection { _certificate }
: new X509CertificateCollection();

var targetHost = _targetHost ?? _host.ToString();
Expand Down Expand Up @@ -269,5 +278,7 @@ public override void Close()
_secureStream = null;
}
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public TTlsServerSocketTransport(
X509Certificate2 certificate,
RemoteCertificateValidationCallback clientCertValidator = null,
LocalCertificateSelectionCallback localCertificateSelectionCallback = null,
SslProtocols sslProtocols = SslProtocols.Tls12)
SslProtocols sslProtocols = TTlsSocketTransport.DefaultSslProtocols)
: base(config)
{
if (!certificate.HasPrivateKey)
Expand All @@ -65,7 +65,7 @@ public TTlsServerSocketTransport(
X509Certificate2 certificate,
RemoteCertificateValidationCallback clientCertValidator = null,
LocalCertificateSelectionCallback localCertificateSelectionCallback = null,
SslProtocols sslProtocols = SslProtocols.Tls12)
SslProtocols sslProtocols = TTlsSocketTransport.DefaultSslProtocols)
: this(null, config, certificate, clientCertValidator, localCertificateSelectionCallback, sslProtocols)
{
try
Expand Down
2 changes: 1 addition & 1 deletion test/netstd/Client/TestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ public TTransport CreateTransport()
trans = new TTlsSocketTransport(host, port, Configuration, 0,
cert,
(sender, certificate, chain, errors) => true,
null, SslProtocols.Tls12);
null);
break;

case TransportChoice.Socket:
Expand Down
2 changes: 1 addition & 1 deletion test/netstd/Server/TestServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ public static async Task<int> Execute(List<string> args)
trans = new TTlsServerSocketTransport(param.port, Configuration,
cert,
(sender, certificate, chain, errors) => true,
null, SslProtocols.Tls12);
null);
break;

case TransportChoice.Socket:
Expand Down

0 comments on commit a06eedc

Please sign in to comment.