主机名与对等方提供的证书主题不匹配,但完全匹配

Posted

技术标签:

【中文标题】主机名与对等方提供的证书主题不匹配,但完全匹配【英文标题】:Host name does not match the certificate subject provided by the peer, but it's a perfect match 【发布时间】:2016-09-16 13:56:50 【问题描述】:

我有两台服务器需要使用 HTTPS 相互通信。

在这种情况下,我们将它们称为“服务器”和“客户端”,其中“客户端正在对“服务器”进行 https 调用。

在生产中,服务器将具有有效的 CA 证书,但在测试时我们将使用自签名证书。

据我了解,这是我们必须做的:

    创建证书 将其添加到服务器上的密钥库中 将其添加到客户端上受信任的 cacerts 密钥库中(以便在尝试进行 https 调用时接受此自签名证书)

这一切都完成了,但是在拨打电话时我收到了这个错误:

Caused by: javax.net.ssl.SSLPeerUnverifiedException: Host name 'docker-abc-123' does not match the certificate subject provided by the peer (CN=docker-abc-123, OU=unit, O=org, L=city, ST=area, C=xx)
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.verifyHostname(SSLConnectionSocketFactory.java:465) [httpclient-4.5.jar:4.5]
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.createLayeredSocket(SSLConnectionSocketFactory.java:395) [httpclient-4.5.jar:4.5]
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:353) [httpclient-4.5.jar:4.5]
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:134) [httpclient-4.5.jar:4.5]
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353) [httpclient-4.5.jar:4.5]
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380) [httpclient-4.5.jar:4.5]
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236) [httpclient-4.5.jar:4.5]
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184) [httpclient-4.5.jar:4.5]
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88) [httpclient-4.5.jar:4.5]
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110) [httpclient-4.5.jar:4.5]
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184) [httpclient-4.5.jar:4.5]
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82) [httpclient-4.5.jar:4.5]
at org.springframework.http.client.HttpComponentsClientHttpRequest.executeInternal(HttpComponentsClientHttpRequest.java:91) [spring-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48) [spring-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53) [spring-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:568) [spring-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
... 10 more

即使主机名与证书中的“公用名”完全匹配。什么可能导致这种情况?欢迎任何想法!

【问题讨论】:

【参考方案1】:

如果证书中有主题备用名称扩展,则通用名称将被忽略,并且 SAN 必须包含与您的主机匹配的标识符。

【讨论】:

刚好找到同样的东西,回来写,就是为了找到你的答案,嘿嘿。我们那里只有替代名称,但那里没有实际的 CN。当我确认它解决了问题时,我会尝试并标记为正确。 (更多信息:wiki.cacert.org/FAQ/subjectAltName)

以上是关于主机名与对等方提供的证书主题不匹配,但完全匹配的主要内容,如果未能解决你的问题,请参考以下文章

Rails 3:OpenSSL::SSL::SSLError:主机名与服务器证书不匹配

卷曲,证书的所有者与主机名不匹配 [关闭]

主机名/IP 与证书的替代名称不匹配

Java SSLException:证书中的主机名不匹配

NPM 没有安装包。主机名/IP 地址与证书的替代名称不匹配:

Node.js 主机名/IP 与证书的替代名称不匹配