How to fix SSL auth issue in HttpClient: Azure Linux App Service

Viewed 843

I'm having a difficult time finding out how to fix this issue as it only happens in a Azure Linux app service. Locally (win10) and in Azure Windows app service, no problems.

The app is ASP.NET Core 3.1 and I've created a custom service as an HttpClient:

        private readonly HttpClient _client;

        public NPIApiService(HttpClient client)
        {
            _client = client;
            _client.BaseAddress = new Uri("https://npiregistry.cms.hhs.gov/api/");
        }

        public class AllowCertsMessageHandler : HttpClientHandler
        {
            public AllowCertsMessageHandler()
            {
                this.ClientCertificateOptions = ClientCertificateOption.Manual;
                this.ServerCertificateCustomValidationCallback = (requestMessage, cert, certChain, policyErrors) =>
                {
                    return true;
                };
            }
        }

        public async Task<NPIResult> LoadNPI(string npi)
        {
            var response = await _client.GetAsync(new Uri($"?version=2.1&number={npi}", UriKind.Relative), HttpCompletionOption.ResponseContentRead);
            if (response.IsSuccessStatusCode)
            {
                var rawstring = await response.Content.ReadAsStringAsync();
                return System.Text.Json.JsonSerializer.Deserialize<NPIResult>(rawstring);
            }

            return null;
        }

Note the AllowCertsMessageHandler: I've added this as a hopeful workaround but to no avail.

            services.AddHttpClient<Services.NPIApiService>()
                .ConfigurePrimaryHttpMessageHandler(() =>
                {
                    return new HttpClientHandler()
                    {
                        ClientCertificateOptions = ClientCertificateOption.Manual,
                        ServerCertificateCustomValidationCallback = (requestMessage, cert, certChain, policyErrors) =>
                        {
                            return true;
                        }
                    };
                });

The code above works well everywhere I've tried, except specifically in Azure Linux App Service.

Stack trace of exception:

2020-06-10T19:18:31.232053228Z: [INFO]  [40m[32minfo[39m[22m[49m: System.Net.Http.HttpClient.NPIApiService.LogicalHandler[100]
2020-06-10T19:18:31.232121130Z: [INFO]        Start processing HTTP request GET https://npiregistry.cms.hhs.gov/api/?version=2.1&number=1316923212
2020-06-10T19:18:31.233564068Z: [INFO]  [40m[32minfo[39m[22m[49m: System.Net.Http.HttpClient.NPIApiService.ClientHandler[100]
2020-06-10T19:18:31.233584268Z: [INFO]        Sending HTTP request GET https://npiregistry.cms.hhs.gov/api/?version=2.1&number=1316923212
2020-06-10T19:18:31.387040818Z: [INFO]  [41m[30mfail[39m[22m[49m: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[1]
2020-06-10T19:18:31.387088119Z: [INFO]        An unhandled exception has occurred while executing the request.
2020-06-10T19:18:31.388164847Z: [INFO]  System.Net.Http.HttpRequestException: **The SSL connection could not be established, see inner exception.**
2020-06-10T19:18:31.388184048Z: [INFO]   ---> System.Security.Authentication.AuthenticationException: **Authentication failed, see inner exception.**
2020-06-10T19:18:31.388194148Z: [INFO]   ---> Interop+OpenSsl+SslException: **SSL Handshake failed with OpenSSL error - SSL_ERROR_SSL.**
2020-06-10T19:18:31.389275477Z: [INFO]   ---> Interop+Crypto+OpenSslCryptographicException: **error:141A318A:SSL routines:tls_process_ske_dhe:dh key too small**
2020-06-10T19:18:31.389293777Z: [INFO]     --- End of inner exception stack trace ---
2020-06-10T19:18:31.394673519Z: [INFO]     at Interop.OpenSsl.DoSslHandshake(SafeSslHandle context, Byte[] recvBuf, Int32 recvOffset, Int32 recvCount, Byte[]& sendBuf, Int32& sendCount)
2020-06-10T19:18:31.394697020Z: [INFO]     at System.Net.Security.SslStreamPal.HandshakeInternal(SafeFreeCredentials credential, SafeDeleteContext& context, ArraySegment`1 inputBuffer, Byte[]& outputBuffer, SslAuthenticationOptions sslAuthenticationOptions)
2020-06-10T19:18:31.394708120Z: [INFO]     --- End of inner exception stack trace ---

I've tried making some changes to openssl.conf as well, but didn't seem to make any difference.

This is my first go at an app that was presumably x-platform compatible, so I'm still learning. I'm pretty sure this is environment related, but I welcome any suggestions.

Thanks in advance...

0 Answers
Related