lasernick
lasernick

Reputation: 45

Connecting to TSL Redis Cloud using ServiceStack.Redis

I'm attempting to use ServiceStack.Redis to connect to a cloud based Redis instance using SSL Certificates. The ServiceStack documentation provides information on how to connect to an Azure based Redis using SSL, but has no information or examples on how to connect to a non Azure Redis instance.

I've got a pem, crt, and key file but nothing I do with them seems to actually pass them across to the service. I've set ssl=true&sslprotocols=tls12 in the connectionstring which seems like the first step, but beyond that I'm not sure what I should be doing to correctly pass across the certificate for verification

EDIT: Current Code

RedisConfig.CertificateSelectionCallback = BuildCertificateSelectionCallback;
RedisConfig.CertificateValidationCallback = Config_CertificateValidation;

var hosts = redCon.Split(',');
var sentinel = new RedisSentinel(hosts, masterName)
{
  RedisManagerFactory = CreateRedisManager,
.....
};
container.Register(c => sentinel.Start());



private static X509Certificate BuildCertificateSelectionCallback(object sender,string targetHost,X509CertificateCollection, X509Certificate remoteCertificate, string[] acceptableIssuers)
{
    return new X509Certificate(@"[filepath]\rl.pfx");
}

private static bool Config_CertificateValidation(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
    //Validation not fully fleshed out yet
    if(sslPolicyErrors == SslPolicyErrors.None;)
    {
        return true;
    }
    return false;
}

Upvotes: 2

Views: 610

Answers (1)

mythz
mythz

Reputation: 143319

ServiceStack.Redis uses .NET's SslStream to establish its SSL connection where you can configure its RemoteCertificateValidationCallback to validate whether to accept the specified certificate for authentication:

RedisConfig.CertificateValidationCallback = (object sender,
    X509Certificate certificate,
    X509Chain chain,
    SslPolicyErrors sslPolicyErrors) => {
    //...
};

And a LocalCertificateSelectionCallback to select the local SSL certificate used for authentication:

RedisConfig.CertificateSelectionCallback = (object sender,
    string targetHost,
    X509CertificateCollection localCertificates,
    X509Certificate remoteCertificate,
    string[] acceptableIssuers) => {
    //...
}

Upvotes: 1

Related Questions