通过 smtp 发送邮件时出现证书问题(tream_socket_enable_crypto():SSL 操作失败,代码为 1)

通过 smtp 发送邮件时出现证书问题(tream_socket_enable_crypto():SSL 操作失败,代码为 1)

我是这个论坛的新手。我也在CraftCMS-board,但我认为它与这个更相关。我希望它适合这里。

我在这里找到了一些类似的帖子,但不幸的是,没有一个能解决我的问题。请放心,我搜索了很多次(几个小时),但无法解决。希望我没有忽略一个好的答案。

这篇文章有点长 - 很抱歉 - 但我确实想包含一些调试信息,因为我想它可能有助于找到解决方案。

非常感谢您花时间阅读并(希望)回复。

概括:我尝试通过 smtp 发送邮件(来自 php 脚本)。该网站使用外部邮件服务器(MX mail.domain.com → mailgate.otherdomain.com → A 到外部 IP)。尝试发送邮件时,我收到错误:SSL routines:tls_process_server_certificate:certificate verify failed。尝试通过 SSH 连接导致Verification error: self signed certificate这对于 Web 服务器来说是正确的,但对于外部邮件服务器则不正确。CheckTLS 状态:Cert Hostname DOES NOT VERIFY (mailgate.wwwshort.com != localhost.localdomain)。我负责 Web 服务器(托管服务器),但不负责邮件服务器(外部提供)。

设置

网站运行wwwdomain.com- 他们还有一个较短的版本,我们称之为wwwshort.com

邮件服务器由另一家公司提供,可在 上找到mailgate.wwwshort.com(这是所有(wwwdomain、wwwshort、其他域)域的 MX。在 wwwshort.com 的 DNS 中,有一个指向 IP 的 A 记录)。还有一个指向 的 A 记录 mail.* mailgate.wwwshort.com

我尝试通过 smtp 从 craftcms3 安装发送电子邮件。邮件服务器是mail.wwwdomain.com

所有域名都有自签名证书。背后的服务器mailgate.shortdomain.com有一个适当的 SSL 证书,其中还提到了所有可能的域名(例如 mail.wwwdomain.com、mail.wwwshort.com、mailgate.wwwshort.com 等)。

我的客户每天都使用这个邮件服务器(我猜它是 Microsoft Exchange Server)并且没有任何问题。

问题

当我尝试发送邮件时,我的日志中出现以下错误:

发送电子邮件时出错:stream_socket_enable_crypto():SSL 操作失败,代码为 1。OpenSSL 错误消息:错误:1416F086:SSL 例程:tls_process_server_certificate:证书验证失败

我能够(不久前)执行建议的解决方法这里但我不想再这样做了,因为它不太安全。

故障排除

我使用 PHPMailer 设置了一个脚本来获取更完整的日志。我得到的结果是:

2021-01-23 18:30:30 Connection: opening to mail.wwwdomain.com:587, timeout=300, options=array()
2021-01-23 18:30:30 Connection: opened
2021-01-23 18:30:30 SMTP INBOUND: "220 mailgate.wwwshort.com ESMTP - DSS"
2021-01-23 18:30:30 SERVER -> CLIENT: 220 mailgate.wwwshort.com ESMTP - DSS
2021-01-23 18:30:30 CLIENT -> SERVER: EHLO www.wwwdomain.com
2021-01-23 18:30:30 SMTP INBOUND: "250-mailgate.wwwshort.com"
2021-01-23 18:30:30 SMTP INBOUND: "250-PIPELINING"
2021-01-23 18:30:30 SMTP INBOUND: "250-SIZE 105080012"
2021-01-23 18:30:30 SMTP INBOUND: "250-ETRN"
2021-01-23 18:30:30 SMTP INBOUND: "250-STARTTLS"
2021-01-23 18:30:30 SMTP INBOUND: "250-ENHANCEDSTATUSCODES"
2021-01-23 18:30:30 SMTP INBOUND: "250-8BITMIME"
2021-01-23 18:30:30 SMTP INBOUND: "250 DSN"
2021-01-23 18:30:30 SERVER -> CLIENT: 250-mailgate.wwwshort.com250-PIPELINING250-SIZE 105080012250-ETRN250-STARTTLS250-ENHANCEDSTATUSCODES250-8BITMIME250 DSN
2021-01-23 18:30:30 CLIENT -> SERVER: STARTTLS
2021-01-23 18:30:30 SMTP INBOUND: "220 2.0.0 Ready to start TLS"
2021-01-23 18:30:30 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
2021-01-23 18:30:30 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 [/usr/www/users/client/test-mail/PHPMailer/SMTP.php line 467]
SMTP Error: Could not connect to SMTP host.
2021-01-23 18:30:30 CLIENT -> SERVER: QUIT
2021-01-23 18:30:30 
2021-01-23 18:30:30 
2021-01-23 18:30:30 
2021-01-23 18:30:30 
2021-01-23 18:30:30 
2021-01-23 18:30:30 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.

现在我搜索了网络并做了很多调查。我将在这里列出结果。

检查 CA 证书

我听从了phpmailer 插件并通过

echo QUIT | openssl s_client -crlf -starttls smtp -CAfile /etc/ssl/cacert.pem -connect smtp.gmail.com:587

这有效非常好

现在我尝试了同样的方法mail.wwwdomain.com

echo QUIT | openssl s_client -crlf -starttls smtp -CAfile /etc/ssl/cacert.pem -connect mail.wwwdomain.com:587

问题又来了:

CONNECTED(00000003)
depth=0 O = NA, CN = localhost.localdomain
verify error:num=18:self signed certificate
verify return:1
depth=0 O = NA, CN = localhost.localdomain
verify return:1
---
Certificate chain
 0 s:O = NA, CN = localhost.localdomain
   i:O = NA, CN = localhost.localdomain
---
Server certificate
-----BEGIN CERTIFICATE-----
MIIFMDCCAxigJANm1uWBG8V5qMA0GIb3DQEBDAUAMC0xCzAJBgNV
BAoMAk5BMR4wHAYDDBVQub2WjAtMQswCQYDVQAJOQMBwGA1UEAww
bG9jYWxob3N0LmxvY2FsZG9tYWluMIICIjANBgkqhk0BAg8AMIIC (shortened)
CgKCAgEAyXZvXtT7f
-----END CERTIFICATE-----
subject=O = NA, CN = localhost.localdomain

issuer=O = NA, CN = localhost.localdomain

---
No client certificate CA names sent
Peer signing digest: SHA256
Peer signature type: RSA
Server Temp Key: ECDH, P-256, 256 bits
---
SSL handshake has read 2481 bytes and written 483 bytes
Verification error: self signed certificate
---
New, TLSv1.2, Cipher is ECDHE-RSA-AES256-GCM-SHA384
Server public key is 4096 bit
Secure Renegotiation IS supported
Compression: NONE
Expansion: NONE
No ALPN negotiated
SSL-Session:
    Protocol  : TLSv1.2
    Cipher    : ECDHE-RSA-AES256-GCM-SHA384
    Session-ID: 0225B94178B4DE0B4499DAFB0C0D3AD4BE5519CCFBA4458E1333FF56B56D700D
    Session-ID-ctx: 
    Master-Key: 103159001B6597C40E8C35A31B5DC240AE52D081BEE153A0B904A71C618D235AE5DD21192A784FBD35084130A1A36688
    PSK identity: None
    PSK identity hint: None
    SRP username: None
    TLS session ticket lifetime hint: 7200 (seconds)
    TLS session ticket:
    0000 - 71 f2 0b 8f 85 4b e2 9a-cb bc 21 1f 5a c6 a7 b4   q....K....!.Z...
    (shortened)

    Start Time: 1611427542
    Timeout   : 7200 (sec)
    Verify return code: 18 (self signed certificate)
    Extended master secret: yes
---
250 DSN
DONE

输出等于mailgate.wwwshort.com和邮件服务器的 IP 地址。

有两件事引起了我的注意:

SSL handshake has read 2481 bytes and written 483 bytes
Verification error: self signed certificate

并提到 localhost.local

0 s:O = NA, CN = localhost.localdomain
  i:O = NA, CN = localhost.localdomain

SSL 报告

我用的是SSL 报告来自 SSL Labs 来测试域名。

  • wwwlong.com被正确识别为“自签名”并且仅列出主域和 www。* 它获得了 B 级评级。
  • wwwshort.com
  • mail.wwwdomain.com被正确识别为非自签名,并将指向此服务器的所有其他可能域列为“替代名称”。它获得了 A+ 评级。

检查TLS

我用了检查TLS在 上运行一些测试wwwdomain.com。它正确地将 MX 服务器识别为mailgate.wwwshort.com(具有正确的 IP),并显示以下输出。

CheckTLS 的结果

[000.000]       Trying TLS on mailgate.wwwshort.com[217.86.144.15:25] (10)
[000.099]       Server answered
[000.595]   <‑‑ 220 mailgate.wwwshort.com ESMTP - DSS
[000.595]       We are allowed to connect
[000.595]   ‑‑> EHLO www12-do.checktls.com
[000.691]   <‑‑ 250-mailgate.wwwshort.com
                250-PIPELINING
                250-SIZE 105080012
                250-ETRN
                250-STARTTLS
                250-AUTH PLAIN LOGIN
                250-ENHANCEDSTATUSCODES
                250-8BITMIME
                250 DSN
[000.691]       We can use this server
[000.691]       TLS is an option on this server
[000.692]   ‑‑> STARTTLS
[000.788]   <‑‑ 220 2.0.0 Ready to start TLS
[000.788]       STARTTLS command works on this server
[001.010]       Connection converted to SSL
SSLVersion in use: TLSv1_2
Cipher in use: ECDHE-RSA-AES256-GCM-SHA384
Perfect Forward Secrecy: yes
Certificate #1 of 1 (sent by MX):
Cert VALIDATION ERROR(S): self signed certificate
So email is encrypted but the recipient domain is not verified
Cert Hostname DOES NOT VERIFY (mailgate.wwwshort.com != localhost.localdomain)
So email is encrypted but the host is not verified
Not Valid Before: Jan  9 20:31:46 2021 GMT
Not Valid After: Oct  6 20:31:46 2023 GMT
subject= /O=NA/CN=localhost.localdomain
issuer= /O=NA/CN=localhost.localdomain
[001.013]   ~~> EHLO www12-do.checktls.com
[001.111]   <~~ 250-mailgate.wwwshort.com
250-PIPELINING
250-SIZE 105080012
250-ETRN
250-AUTH PLAIN LOGIN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
[001.111]       TLS successfully started on this server
[001.111]   ~~> MAIL FROM:<[email protected]>
[001.212]   <~~ 250 2.1.0 Ok
[001.212]       Sender is OK
[001.212]   ~~> QUIT
[001.311]   <~~ 221 2.0.0 Bye

重点突出的是这一部分:

Certificate #1 of 1 (sent by MX):
Cert VALIDATION ERROR(S): self signed certificate
So email is encrypted but the recipient domain is not verified
Cert Hostname DOES NOT VERIFY (mailgate.wwwshort.com != localhost.localdomain)
So email is encrypted but the host is not verified

我想知道网络服务器的自签名证书是否导致了这个问题(因为它最初是一个通配符 *.wwwdomain.com),所以我用仅包含主域的自签名证书替换了所有证书。然而,问题仍然存在(虽然我不确定我是否必须等待更长时间)。

我的“不理解”

据我(不)理解,有两个问题的报告:1)证书自签名(这对于网络服务器来说是正确的,但对于邮件服务器则不然)2)域名不匹配

我绝对不是这方面的专家。谁能告诉我问题出在我这边(网络服务器、craft 等)还是“他们那边”(邮件服务器)。

答案1

简而言之:您尝试使用的邮件服务器未正确设置为供公众使用。它使用的是自签名证书 - 默认情况下不受任何客户端信任。此外,证书中的名称与服务器的主机名不匹配。

我的客户每天都使用这个邮件服务器(我猜它是 Microsoft Exchange Server)并且没有任何问题。

造成这种情况的原因可能有多种:

  • 客户端没有意识到问题,因为它只是忽略了证书错误
  • 客户端实际上并未完全使用此服务器和服务(主机和端口),而是使用不同的服务和此服务器,如端口 465(smtps)或根本不使用 SMTP,而是使用 Exchange 特定服务。这些可能已正确设置。

我(不久前)能够执行这里建议的解决方法,但我不想再这样做了,因为它不是很安全。

不幸的是,如果服务器设置有问题,就没有很好的解决方案了。但请向服务器提供商咨询其设置中应如何进行安全访问,因为您可能没有按照其设置所预期的方式使用系统。

相关内容