如何修复 SSL 证书验证失败

Posted

技术标签:

【中文标题】如何修复 SSL 证书验证失败【英文标题】:how to fix SSL certificate verification failure 【发布时间】:2018-03-12 17:02:45 【问题描述】:

我在本地使用 phpMailer,以及 Apache 和 PHP。 当我测试我的 SSL 配置和我的 cacerts 时,我得到了

default_cert_file = C:\Program Files\Common Files\SSL/cert.pem
default_cert_file_env = SSL_CERT_FILE
default_cert_dir = C:\Program Files\Common Files\SSL/certs
default_cert_dir_env = SSL_CERT_DIR
default_private_dir = C:\Program Files\Common Files\SSL/private
default_default_cert_area = C:\Program Files\Common Files\SSL
ini_cafile = 
ini_capath = 

然后,我会这样做

 var_dump(fsockopen("smtp.gmail.com", 465, $errno, $errstr, 3.0));
 var_dump($errno);
 var_dump($errstr);

我明白了

fsockopen resource(2) of type (stream) int(0) string(0) "" 

在我的php.ini 中,我在curl 部分中有curl.cainfo = C:/php/cacert.pem,它可以工作。

但是当我尝试使用 PHPMailer 从本地主机发送邮件时,我不断收到

SSL operation failed with code 1. OpenSSL Error messages:error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed 
Failed to enable crypto
unable to connect to ssl://smtp.gmail.com:465 (Unknown error)

我还访问了 SMTP 中使用的帐户 https://accounts.google.com/DisplayUnlockCaptcha 并启用它。和https://myaccount.google.com/lesssecureapps?pli=1 并启用安全性较低的应用程序。 我猜这是一个 SSL 错误,而不是 PHPMailer。坦率地说,我很困惑不知道如何解决它。原谅我的无知,任何关于哪里出了问题以及如何解决它的帮助,都会很棒。

PS,这是我发送邮件的 php 文件。谢谢

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;    
require 'C:/php/PHPMailer/src/Exception.php';
require 'C:/php/PHPMailer/src/PHPMailer.php';
require 'C:/php/PHPMailer/src/SMTP.php';

$mail = new PHPMailer(true);                             
try 

    $mail->SMTPDebug = 3;
    $mail->isSMTP();                                      
    $mail->Host = 'smtp.gmail.com'; 
    $mail->SMTPAuth = true;                               
    $mail->Username = 'slevin@gmail.com';                 
    $mail->Password = 'secret';                           
    $mail->SMTPSecure = 'ssl';                            
    $mail->Port = 465;                                    

    //Recipients
    $mail->setFrom('slevin@gmail.com');
    $mail->addAddress('jacob@gmail.com');     


    //Content
    $mail->ishtml(true);                                 
    $mail->Subject = 'subject';
    $mail->Body    = 'HTML message body <b>in bold!</b>';
    $mail->AltBody = 'body in plain text';

    $mail->send();
    echo 'Message has been sent';
 catch (Exception $e) 
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
            
?>

更新

当我尝试使用 $mail-&gt;SMTPSecure = 'ssl';$mail-&gt;Port = 465; 时,我得到了

2017-10-01 13:04:25 Connection: opening to ssl://smtp.gmail.com:465, timeout=300, options=array()
2017-10-01 13:04:26 Connection failed. Error #2: stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages:error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed [C:\php\PHPMailer\src\SMTP.php line 324]
2017-10-01 13:04:26 Connection failed. Error #2: stream_socket_client(): Failed to enable crypto [C:\php\PHPMailer\src\SMTP.php line 324]
2017-10-01 13:04:26 Connection failed. Error #2: stream_socket_client(): unable to connect to ssl://smtp.gmail.com:465 (Unknown error) [C:\php\PHPMailer\src\SMTP.php line 324]
2017-10-01 13:04:26 SMTP ERROR: Failed to connect to server: (0)
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

当我尝试使用 $mail-&gt;SMTPSecure = 'tls';$mail-&gt;Port = 587; 时,我得到了

2017-10-01 13:07:20 Connection: opening to smtp.gmail.com:587, timeout=300, options=array()
2017-10-01 13:07:21 Connection: opened
2017-10-01 13:07:21 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP l4sm5217189wrb.74 - gsmtp
2017-10-01 13:07:21 CLIENT -> SERVER: EHLO localhost
2017-10-01 13:07:21 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [85.75.196.114]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250 SMTPUTF8
2017-10-01 13:07:21 CLIENT -> SERVER: STARTTLS
2017-10-01 13:07:21 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
2017-10-01 13:07:21 Connection failed. Error #2: stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages:error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed [C:\php\PHPMailer\src\SMTP.php line 403]
SMTP Error: Could not connect to SMTP host.
2017-10-01 13:07:21 CLIENT -> SERVER: QUIT
2017-10-01 13:07:21 SERVER -> CLIENT: 
2017-10-01 13:07:21 SMTP ERROR: QUIT command failed: 
2017-10-01 13:07:21 Connection: closed
SMTP Error: Could not connect to SMTP host.
Message could not be sent.Mailer Error: SMTP Error: Could not connect to SMTP host.

【问题讨论】:

fsockopen 只是打开一个 TCP 连接,它不进行 TLS 握手,所以这并不能告诉你任何事情。使用SMTPSecure = 'tls' 和端口 587 可能会在调试输出中显示更多反馈。 @Synchro 我按照你说的做了,现在我得到了Error #2: stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages:error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed ,然后是SMTP Error: Could not connect to SMTP host.。我是初学者。让我感到困惑的是,我的 SSL 配置和证书看起来不错,但我仍然遇到错误。也许我可以更新我的证书?谢谢。 显示调试输出中的内容,而不仅仅是最终的错误消息。 @Synchro 检查问题中的更新。 好的,这至少表明您没有被重定向到其他地方。我建议使用 openssl(如故障排除指南中所述)检查您的 CA 证书文件是否正常工作 - Google 不会发布无效证书。 【参考方案1】:

刚刚收到同样的错误信息,也许你遇到了同样的问题:

我的证书很好(我可以毫无问题地访问 HTTPS 域) 无法通过 PHP 的 stream_context_createstream_socket_client 运行相同的请求 我想要检查 SSL 证书

通过返回的 cURL 测试同一个域:

curl: (60) SSL certificate problem: certificate is not yet valid

原来我在其中运行 PHP 和 cURL 的虚拟机出现了 11 天的错误(12 月 11 日而不是 12 月 22 日)。

通过修复计算机/虚拟机的日期(使用ntpntpdate-debian 等),证书测试现在可以在 cURL 命令行或 PHP 中正常运行。 p>

【讨论】:

是的,这就是我刚才的问题。 Homestead 的这个问题每天都让我发疯:github.com/laravel/homestead/issues/799 通常症状是不同的。我以前没有遇到过这个特别的错误,但你说得对,同步时钟修复了它。【参考方案2】:

我最近在一个新的 Windows IIS 服务器上遇到了这个问题。 cURL 调用是从批处理脚本到我自己的域的,这两个脚本都在同一台服务器上运行。

我已经添加了

127.0.0.1  mydomain.com

到 hosts 文件,我在更改公共 DNS 时忘记将其删除。

服务器位于 Cloudflare 后面,因此我的 cURL 获得的是 CF 原始证书,而不是 Cloudflare 提供的真正证书。

删除 hosts 条目即可解决问题。

【讨论】:

以上是关于如何修复 SSL 证书验证失败的主要内容,如果未能解决你的问题,请参考以下文章

Windows 10 SSL证书验证失败

为啥我的SSL证书验证失败

证书验证失败怎么回事?

使用 Kohana 验证 SSL 证书失败

Mandrill webhooks,SSL证书验证失败,验证CA证书没问题

如何让 python 信任我服务器的 TLS 自签名证书:ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] 证书验证失败