C# .NET Core 3.1 SSL连接无法建立

3
我使用一个带有自签名证书的SAP docker容器中的C# ASP.NET Core 3.1服务来使用OData服务。与此同时,我尝试了数千种方法,但错误仍然存在。
系统.InvalidOperationException: 在处理此请求时出现错误。mdt2oowvsap_1 | --->System.Data.Services.Client.DataServiceTransportException: SSL连接无法建立,请参见内部异常。根据验证程序,远程证书无效。
即使使用HttpClientHandler.ServerCertificateCustomValidationCallback并直接返回true等不安全的解决方案,也没有改变任何事情。
 public MyService()
 {
        :
        handler = new HttpClientHandler()
        {
            Credentials = new NetworkCredential(Username, Password, Domain),
            PreAuthenticate = true,                
            ServerCertificateCustomValidationCallback = (message, cert, chain, sslPolicyErrors) =>
            {
                if (sslPolicyErrors == SslPolicyErrors.None)
                {
                    logger.LogDebug($"No SSL policy errors!");
                    return true;   //Is valid
                }

                logger.LogDebug($"Get certificate hash: {cert.GetCertHashString()}");
                // From: https://dev59.com/EnE85IYBdhLWcg3wpFSu
                if (!String.IsNullOrEmpty(certificateHash) && cert.GetCertHashString().Equals(certificateHash))
                {
                    // Get hash string via: openssl s_client -connect <host>:<port> < /dev/null 2>/dev/null | openssl x509 -fingerprint -noout -in /dev/stdin
                    // see: https://dev59.com/-2435IYBdhLWcg3w3EIi
                    logger.LogDebug($"Using certificate hash: {certificateHash}");
                    return true;
                }
                return false;
            },
            UseCookies = true,
            CookieContainer = cookieContainer
        };
        String[] files = Directory.GetFiles("./certs/", "*.*", SearchOption.TopDirectoryOnly);
        logger.LogInformation($"Found {files.Length} certificate files");
        // see: https://dev59.com/f1kS5IYBdhLWcg3wVlXC
        foreach (string cfile in files)
        {
            try
            {
                logger.LogDebug($"Try adding {cfile} as trusted client certificate...");
                handler.ClientCertificates.Add(new X509Certificate2(Path.GetFullPath(cfile)));
            }catch(Exception e)
            {
                logger.LogInformation($"Exception while adding certificate file {cfile} to 'ClientCertificates'");
                logger.LogError(e.ToString());
            }
        }
        httpClient = new HttpClient(handler);
        :
 }

最后一次尝试是将证书下载并使用ClientCertificates.Add提供给HttpClientHandler,但没有成功。
使用curl时,传递这个证书文件是有效的。
 $> curl --location --request GET 'https://customer.de:1234/sap/opu/odata/something/useful_srv' \
 --header 'Authorization: Basic ABCDEFGHIJKLMNOPQRST='
 curl: (60) SSL certificate problem: self signed certificate
 More details here: https://curl.haxx.se/docs/sslcerts.html
 :
 $> echo -n | openssl s_client -connect customer.de:1234 -servername customer.de | \
 openssl x509 > /full/path/to/customer.cert
 depth=0 CN = customer.de
 verify error:num=18:self signed certificate
 verify return:1
 depth=0 CN = customer.de
 verify return:1
 DONE
 $> 
 $> curl --location --cacert '/full/path/to/customer.cert' --request GET \
 'https://customer.de:1234/sap/opu/odata/something/useful_srv' \
 --header 'Authorization: Basic ABCDEFGHIJKLMNOPQRST='
 <?xml version="1.0" encoding="utf-8"?><app:service xml:lang="de" xml:base="https:blablabla.../></app:service>
 $> _

大家还有其他的想法吗?

已查看的解决方案(不完整):

提前感谢。


可能服务器要求使用TLS 1.3,但你运行C#代码的操作系统不支持TLS 1.3。 - Ian Kemp
3个回答

1
我想在Buckaroo Banzai的答案中补充一点,对于在docker容器中运行.NET应用程序的其他用户来说,更新ca证书包(update-ca-certificates)可能并没有默认安装。
通过执行以下命令获取shell访问权限:
sudo docker exec -it containerName /bin/bash

执行以下命令来更新软件包列表,安装并运行update-ca-certificates:

apt update && \
apt install -y ca-certificates && \ 
update-ca-certificates

现在我可以从我的.NET应用程序内部的容器重新与我的(以及其他人的)Web API进行通信。
为了使此更改永久有效,您应该将以下代码片段添加到ENTRYPOINT之前使用的映像的dockerfile中。
RUN \ 
  apt update && \
  apt install -y ca-certificates && \
  update-ca-certificates

在这里找到一个很好的解释,为什么会出现这个问题,并更好地理解证书颁发机构的工作方式:

什么是CA证书,为什么我们需要它?


非常好的提示!谢谢! - Buckaroo Banzai

1
问题的解决方案是将“自签名”证书提供给Docker容器或其中包含的操作系统(Ubuntu),并为其提供以下内容:

cp certs/customer.cert /usr/local/share/ca-certificates/customer.crt &&
update-ca-certificates

现在通信可以通过SSL进行。


0

问题:使用MinIO SDK时,我遇到了错误:

MinIO API响应消息=连接错误:SSL连接无法建立,请参见内部异常。状态码=0,响应=SSL连接无法建立,请参见内部异常。内容=

我的回答: 我注册了自签名证书:

Import-Certificate -FilePath “public.crt” -CertStoreLocation 'Cert:\LocalMachine\Root'

系统回显: PSParentPath:Microsoft.PowerShell.Security\Certificate::LocalMachine\Root

指纹 主题


E021FFABABAF32318D31BB18867F3BFAB788FFC0 OU=BETVSYS\andy@andy-tp (andy), O=Certgen Development

问题已解决。不需要更改任何代码。 参考链接: 1 https://github.com/minio/certgen 2 https://github.com/minio/minio-go/issues/1000#issuecomment-550262204


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接