【问题标题】:The SSL connection could not be established: System.Security.Authentication.AuthenticationException无法建立 SSL 连接:System.Security.Authentication.AuthenticationException
【发布时间】:2021-10-12 21:57:32
【问题描述】:

我的服务器代码(在 AWS Lambda .net Core 3.1 上运行)突然停止使用我的 API 服务器进行身份验证。它已经工作了多年,但今天它停止在生产和开发环境中同时工作。我得到的错误是

System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception.
System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure.

相关源码为

WebRequest httpWebRequest2 = WebRequest.Create(APIurl);
httpWebRequest2.Method = "GET";
httpWebRequest2.Accept = "application/json"
httpWebRequest2.Timeout = 60 * 1000
httpWebRequest2.Headers.Add("Authorization", _APIToken);
WebResponse response = httpWebRequest2.GetResponseAsync().Result;

我正在使用letsencrypt 来生成证书,并且我的开发api url 或生产api url 的证书都没有过期。 HTTPS 查询在我尝试过的所有其他平台上都能成功。

在这一点上,我什至不知道还有什么要解决的问题。我可以提供尽可能多的信息。

【问题讨论】:

标签: c# .net https aws-lambda


【解决方案1】:

这是 Amazon Linux 2 的一个问题,Lambda 函数利用它来运行其运行时环境。截至本文发布时,Amazon Linux 2 使用 OpenSSL 1.0(特别是软件包 openssl-1.0.2k-19.amzn2.0.6.x86_64)。 As of today, September 30th 2021, Let's Encrypt's older certificate from DST Root CA X3 expired 已过期,其中一个怪癖是 Let's Encrypt 的默认首选证书链不再适用于 OpenSSL 1.0。

您可以按照 this post 中的说明解决此问题,具体取决于您生成 Let's Encrypt 证书的方式。例如,我们使用CertBot 并需要传递--preferred-chain 'ISRG Root X1' 参数来为OpenSSL 1.0 生成兼容的证书。

希望底层运行时环境能尽快与默认链首选项兼容。

【讨论】:

    【解决方案2】:

    我还找到了一个临时解决方法来启动和运行 Lambda 函数。我只设法让它在 netcoreapp3.1 运行时运行 - 而不是 2.1 运行时。

    基本上,我去Let's Encrypt下载了“正确”的中间R3证书

    我将它添加到我的函数的项目文件夹中,将文件设置为“复制到输出目录”:

    <ItemGroup>
      <None Update="lets-encrypt-r3.pem">
        <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      </None>
    </ItemGroup>
    

    在此之后,我将以下环境变量添加到我的函数中:

    "SSL_CERT_FILE" = "/var/task/lets-encrypt-r3.pem"
    

    进行此更改后,SSL 错误消失了。据我了解,SSL_CERT_FILE 环境变量将指示 OpenSSL 使用给定证书进行验证 - 尽管它可能使用给定证书,因此在将其推送到生产之前在您自己的函数上对其进行测试 :-)话虽如此,我的一个功能连接到 MailGun,它起作用了

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-24
      • 1970-01-01
      • 1970-01-01
      • 2017-12-24
      • 1970-01-01
      相关资源
      最近更新 更多