Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit a4cdab7

Browse files
Added tls protocol selection config
1 parent 771f5ba commit a4cdab7

File tree

4 files changed

+18
-1
lines changed

4 files changed

+18
-1
lines changed

src/ServiceStack.Redis/RedisEndpoint.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.ComponentModel;
4+
using System.Security.Authentication;
45
using System.Text;
56
using ServiceStack.IO;
67
using ServiceStack.Text;
@@ -34,6 +35,7 @@ public RedisEndpoint(string host, int port, string password = null, long db = Re
3435
public string Host { get; set; }
3536
public int Port { get; set; }
3637
public bool Ssl { get; set; }
38+
public SslProtocols? SslProtocol {get; set;}
3739
public int ConnectTimeout { get; set; }
3840
public int SendTimeout { get; set; }
3941
public int ReceiveTimeout { get; set; }
@@ -59,6 +61,8 @@ public override string ToString()
5961
args.Add("Db=" + Db);
6062
if (Ssl)
6163
args.Add("Ssl=true");
64+
if (SslProtocol != null)
65+
args.Add("SslProtocols=" + SslProtocol.ToString());
6266
if (ConnectTimeout != RedisConfig.DefaultConnectTimeout)
6367
args.Add("ConnectTimeout=" + ConnectTimeout);
6468
if (SendTimeout != RedisConfig.DefaultSendTimeout)
@@ -83,6 +87,7 @@ protected bool Equals(RedisEndpoint other)
8387
return string.Equals(Host, other.Host)
8488
&& Port == other.Port
8589
&& Ssl.Equals(other.Ssl)
90+
&& SslProtocol.Equals(other.SslProtocol)
8691
&& ConnectTimeout == other.ConnectTimeout
8792
&& SendTimeout == other.SendTimeout
8893
&& ReceiveTimeout == other.ReceiveTimeout
@@ -109,6 +114,7 @@ public override int GetHashCode()
109114
var hashCode = (Host != null ? Host.GetHashCode() : 0);
110115
hashCode = (hashCode * 397) ^ Port;
111116
hashCode = (hashCode * 397) ^ Ssl.GetHashCode();
117+
hashCode = (hashCode * 397) ^ SslProtocol.GetHashCode();
112118
hashCode = (hashCode * 397) ^ ConnectTimeout;
113119
hashCode = (hashCode * 397) ^ SendTimeout;
114120
hashCode = (hashCode * 397) ^ ReceiveTimeout;

src/ServiceStack.Redis/RedisExtensions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using System.Globalization;
1616
using System.Linq;
1717
using System.Net.Sockets;
18+
using System.Security.Authentication;
1819
using ServiceStack.Model;
1920
using ServiceStack.Text;
2021

@@ -76,6 +77,11 @@ public static RedisEndpoint ToRedisEndpoint(this string connectionString, int? d
7677
if (useDefaultPort)
7778
endpoint.Port = RedisConfig.DefaultPortSsl;
7879
break;
80+
case "sslProtocols":
81+
SslProtocols protocols;
82+
if (!Enum.TryParse(value, true, out protocols)) throw new ArgumentOutOfRangeException("Keyword '" + name + "' requires an SslProtocol value (multiple values separated by '|').");
83+
endpoint.SslProtocol = protocols;
84+
break;
7985
case "client":
8086
endpoint.Client = value;
8187
break;

src/ServiceStack.Redis/RedisNativeClient.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
using ServiceStack.Logging;
2222
using ServiceStack.Redis.Pipeline;
2323
using ServiceStack.Text;
24+
using System.Security.Authentication;
2425

2526
namespace ServiceStack.Redis
2627
{
@@ -91,6 +92,7 @@ internal bool Active
9192
public string Host { get; private set; }
9293
public int Port { get; private set; }
9394
public bool Ssl { get; private set; }
95+
public SslProtocols? SslProtocol { get; private set; }
9496

9597
/// <summary>
9698
/// Gets or sets object key prefix.
@@ -177,6 +179,7 @@ private void Init(RedisEndpoint config)
177179
Client = config.Client;
178180
Db = config.Db;
179181
Ssl = config.Ssl;
182+
SslProtocol = config.SslProtocol;
180183
IdleTimeOutSecs = config.IdleTimeOutSecs;
181184
ServerVersionNumber = RedisConfig.AssumeServerVersion.GetValueOrDefault();
182185
LogPrefix = "#" + ClientId + " ";

src/ServiceStack.Redis/RedisNativeClient_Utils.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
using System.Net;
1919
using System.Net.Security;
2020
using System.Net.Sockets;
21+
using System.Security.Authentication;
2122
using System.Security.Cryptography;
23+
using System.Security.Cryptography.X509Certificates;
2224
using System.Text;
2325
using System.Threading;
2426
using System.Threading.Tasks;
@@ -167,7 +169,7 @@ private void Connect()
167169
#if NETSTANDARD2_0
168170
sslStream.AuthenticateAsClientAsync(Host).Wait();
169171
#else
170-
sslStream.AuthenticateAsClient(Host);
172+
sslStream.AuthenticateAsClient(Host, new X509CertificateCollection(), SslProtocol ?? SslProtocols.Default, checkCertificateRevocation: true);
171173
#endif
172174

173175
if (!sslStream.IsEncrypted)

0 commit comments

Comments
 (0)