HttpClient with Client Certificate on CentOS 7
HttpClient does not seem to work with a client certificate on linux (CentOS 7). Using dotnet 1.1 with System.Net.Http 4.3.0 in a console app and asp.net site.
var handler = new HttpClientHandler() { ClientCertificateOptions = ClientCertificateOption.Manual };
handler.ClientCertificates.Add(settings.ClientCertificate);
var httpClient = new HttpClient(handler);
var responseMsg = await httpClient.SendAsync(requestMsg);
It works fine on Windows 8.1/10 but fails on CentOS 7 with the following error...
Exception: System.PlatformNotSupportedException: The libcurl library in use (7.29.0) and its SSL backend ("NSS/3.19.1 Basic ECC") do not support custom handling of certificates. A libcurl built with OpenSSL is required.
I tried using libcurl-openssl, http://linuxsoft.cern.ch/cern/centos/7/cern/x86_64/repoview/libcurl-openssl.html, and exporting LD_LIBRARY_PATH as well and get the following error
Exception: System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.Http.CurlException: SSL peer certificate or SSH remote key was not OK
We're also having this issue. CentOS is pretty popular. We are having a lot of trouble locating an RPM that will work with Cent7. We also couldn't get it working by compiling from source.
cc @Priya91 @stephentoub
@justin-lavelle Can you try sending the request through curl without .net,
curl --cacert <client-cerficate> url
Well we seem to have been able to work around it by importing the certificate into nss on CentOS and changing our code to remove the HttpClientHandler, letting curl handle the cert entirely.
$ sudo pk12util -d sql:/etc/pki/nssdb -i prod.pfx
With the default curl
curl 7.29.0 (x86_64-redhat-linux-gnu) libcurl/7.29.0 NSS/3.19.1 Basic ECC zlib/1.2.7 libidn/1.28 libssh2/1.4.3
$ curl --cacert prod.pem
curl: (77) Problem with the SSL CA cert (path? access rights?)
$ curl --cacert ./prod.pem
curl: (77) Problem with the SSL CA cert (path? access rights?)
This it turns out is because the default curl using nss is wants the cert imported to nss.
With curl-openssl from http://download.opensuse.org/repositories/security:shibboleth/CentOS_7/security:shibboleth.repo
curl 7.29.0 (x86_64-redhat-linux-gnu) libcurl/7.51.0 OpenSSL/1.0.1e zlib/1.2.7
$ curl --cacert prod.pem
curl: (60) SSL certificate problem: self signed certificate in certificate chain
$ curl --cert prod.pem --cacert prod.pem
curl: (60) SSL certificate problem: self signed certificate in certificate chain
The cert verifies via openssl with itself specified as the CAfile (it contains the CA cert)
$ openssl verify -CAfile prod.pem prod.pem
prod.pem: OK
I would be nice to have consistent code across all platforms of course and not have to have this work around for CentOS.
@justin-lavelle .NET Core requires curl with openssl. From the curl error with openssl it seems like your certificate is self-signed and is not placed in the correct location as required by openssl. You can follow the instructions from here and then the .net code should work.
I would be nice to have consistent code across all platforms of course and not have to have this work around for CentOS.
The API surface area is consistent across platforms, it is the case that different OSes require different configuration. .NET is ultimately a managed wrapper over the native OS APIs, and we require platform specific configuration as demanded by the underlying OS.
@Priya91
The API surface area is consistent across platforms, it is the case that different OSes require different configuration. .NET is ultimately a managed wrapper over the native OS APIs, and we require platform specific configuration as demanded by the underlying OS.
You're correct I see this isn't a .NET issue, but a platform specific implementation issue.
I was thinking the issue maybe with the version of openssl linked in the curl rpm, OpenSSL/1.0.1e, since I came across some threads on the self signed cert issue, http://stackoverflow.com/questions/27599985/verify-errorcode-20-unable-to-get-local-issuer-certificate
I was able to get it working by building the lastest curl and openssl from source. Using ldd to find the libraries needed by System.Net.Http.Native.so (libssl.so.1.0.0, libcrypto.so.1.0.0, libcurl.so.4) and setting the LD_CONFIG_PATH and linking them to the newly built ones.
curl 7.52.1 (x86_64-pc-linux-gnu) libcurl/7.52.1 OpenSSL/1.0.2k
$curl --cert prod.pem https://url
<html>
<head><title>Error 404 Not Found</title></head>
...
$ dotnet bin/Debug/netcoreapp1.1/ConsoleApp1.dll echoinputvalue
{"serviceEntity":null,"data":"echoinputvalue","Response":{"code":"0","description":"Success"}}
other info for reference...
It's not a self signed cert, the cert is signed by a private CA of the company whose service we're connecting to, not a public CA. So I added the CA cert to the ca-trust and then it verified without specifiying itself as the CAfile
$ sudo cp ca.pem /etc/pki/ca-trust/source/anchors/ca.pem
$ sudo update-ca-trust enable
$ openssl verify prod.pem
prod.pem: OK
But curl with openssl from the rpm, and our dotnet app, was still failing with unless --insecure is used
$ curl --cert prod.pem https://url
curl: (60) SSL certificate problem: self signed certificate in certificate chain
So I rebuilt curl and openssl from the latest source and that worked, for curl.
@justin-lavelle Thanks for explaining the solution here, it will be helpful for others who hit the same issue. :)
In the above case Justin has control to the operating system and was able to update the system to get around the cert issue. I am having the same issue, but my code is running in AWS as a Lambda function, so I don't have control to the underlying operating system. Do you have any recommendations on how I can present a cert signed by a private CA in the web request? Thanks!
@wfurt @tmds any idea?
This is difficult one as crypto was always problematic with nss. The original problem was reported on 1.1 but if upgrade is possibility 2.1 there is new HTTP implementation without dependency on curl. It may also be possible to use custom X509 store instead of manipulating OS directly. I would need to do some research. Do you have at least some control over the system @TomYip? It seems like nss would at least honor some variables like SSL_DIR.
The package that is built for RHEL is also built for CentOS. This package includes a supported backend (OpenSSL). Installation should be similar to: https://www.microsoft.com/net/download/linux-package-manager/rhel/sdk-current. With this package it is possible to use ClientCertificates.
With 2.1, it is possible to specify a certificate bundle using SSL_CERT_FILE (https://github.com/dotnet/corefx/pull/25219).