跳到主要内容
ecdsa.com
.NET SslStream / HttpClient.NET

错误信息

The remote certificate is invalid because of errors in the certificate chain: NotTimeValid

HttpClient 会把它报告为“The SSL connection could not be established, see inner exception.”的内部异常。多个标志可能会一起列出,例如“NotTimeValid, UntrustedRoot”。

.NET SslStream / HttpClient — X509ChainStatusFlags.NotTimeValid

含义

按照这台机器的时钟,.NET 构建的证书链中有一张证书不在有效期内:已过期或尚未生效。NotTimeValid 不会指出是哪一张证书;验证回调中的证书链元素会指出来。根证书和名称都没有问题,否则还会同时出现其他标志。

原因

修复方法

  1. 1.

    检查服务器实际发送的证书日期

    将 notBefore 和 notAfter 与当前 UTC 时间比较。

    bash
    echo | openssl s_client -connect api.example.com:443 -servername api.example.com 2>/dev/null \
      | openssl x509 -noout -subject -startdate -enddate
  2. 2.

    记录出错的证书链元素

    用一个验证回调记录 ChainElementStatus 并返回默认判定结果,就能看出是哪张证书不在有效期内,而不会削弱验证。

    csharp
    // Log which certificate failed and why, without accepting it
    var handler = new HttpClientHandler
    {
        ServerCertificateCustomValidationCallback = (msg, cert, chain, errors) =>
        {
            foreach (var element in chain!.ChainElements)
                foreach (var status in element.ChainElementStatus)
                    Console.WriteLine($"{element.Certificate.Subject}: {status.Status} {element.Certificate.NotAfter:u}");
            return errors == System.Net.Security.SslPolicyErrors.None;
        },
    };
  3. 3.

    续期并重新绑定

    续期证书,并更新所有提供该证书的绑定。

    bash
    sudo certbot renew && sudo systemctl reload nginx     # or renew in your CA / key vault and redeploy

相关错误

浏览完整的签名错误库