RestSharp SSL HandshakeFailure

Viewed 185

I'm trying to send an http request with an ssl certificate using RestSharp but for some reason I'm getting this exception:

I'm using dotnet 6, windows 11 and RestSharp 108.0.1.

System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception. ---> System.Security.Authentication.AuthenticationException: Authentication failed because the remote party sent a TLS alert: 'HandshakeFailure'. ---> System.ComponentModel.Win32Exception (0x80090326): Mensagem recebida inesperada ou formatada incorretamente. --- End of inner exception stack trace --- at System.Net.Security.SslStream.ForceAuthenticationAsync[TIOAdapter](TIOAdapter adapter, Boolean receiveFirst, Byte[] reAuthenticationData, Boolean isApm) at System.Net.Http.ConnectHelper.EstablishSslConnectionAsync(SslClientAuthenticationOptions sslOptions, HttpRequestMessage request, Boolean async, Stream stream, CancellationToken cancellationToken) --- End of inner exception stack trace --- at Program.<$>g__WithRestSharp|0_1() in E:\dir\Program.cs:line 58

The code I'm trying to run looks like this:

async Task WithRestSharp()
{
    RestResponse? result = null;
    try
    {
        var certificate = GetCertificate();

        var options = new RestClientOptions(@"");
        options.ConfigureMessageHandler = handler =>
        {
            if (handler is not HttpClientHandler clientHandler) return handler;
            clientHandler.SslProtocols = SslProtocols.Tls12;
            return clientHandler;
        };

        var client = new RestClient(options);
        var certificates = new X509CertificateCollection {certificate};


        client.Options.ClientCertificates = certificates;


        var request = new RestRequest(@"");

        result = await client.ExecuteGetAsync(request);
        if (result.ErrorException is not null)
            throw result.ErrorException;

        Console.WriteLine("Status Code: {0}", result.StatusCode);
    }
    catch (Exception e)
    {
        Console.WriteLine("Status Code: {0}", result?.StatusCode);
        Console.WriteLine(e.ToString());
    }
}

I've also written a version without RestSharp that works fine but it's not as easy to use.

async Task WithOutRestSharp()
{
    try
    {
        var handler = new HttpClientHandler();
        handler.ClientCertificateOptions = ClientCertificateOption.Manual;
        handler.SslProtocols = SslProtocols.Tls12;
        handler.ClientCertificates.Add(GetCertificate());

        var client = new HttpClient(handler);

        client.BaseAddress = new Uri(@"");

        var result = await client.GetAsync(@"");

        Console.WriteLine(await result.Content.ReadAsStringAsync());
    }
    catch (Exception e)
    {
        Console.WriteLine(e.ToString());
    }
}

The GetCertificate method is simple:

X509Certificate2 GetCertificate()
{
    var p12Filename = @"";
    var password = "";

    return new X509Certificate2(p12Filename, password);
}

I already tried writing this line before the ExecuteGetAsync method.

System.Net.ServicePointManager.SecurityProtocol =  SecurityProtocolType.Tls12;

If there is any information missing please let me know.

0 Answers
Related