简体   繁体   English

SSLHandshakeException:证书中的主机名不匹配

[英]SSLHandshakeException: hostname in certificate didn't match

I am writing a system that must make a multipart post to a server (a third-party program called ARX that is currently running on localhost during the development) that has a self-signed certificate. 我正在编写一个系统,必须将一个多部分发布到服务器(一个名为ARX的第三方程序,当前在开发期间在localhost上运行),该服务器具有自签名证书。

I tried to find its certificate, but can only find three different jks files; 我试图找到它的证书,但只能找到三个不同的jks文件; server.jks , servertrust.jks and serverca.jks . server.jks,servertrust.jksserverca.jks。

I have tried to use the System.setProperty("javax.net.ssl.trustStore", "Program Files\\\\<path>\\\\jksfile") with each of the jks files. 我试图使用每个jks文件的System.setProperty("javax.net.ssl.trustStore", "Program Files\\\\<path>\\\\jksfile") However; 然而; when I do, I get the following error: hostname in certificate didn't match: < localhost> != <9200416 arx sa cert> . 当我这样做时,我收到以下错误: 证书中的主机名不匹配:<localhost>!= <9200416 arx sa cert>

I have browsed plenty of similar questions here on stackoverflow to try and get an idea on how to fix this, but I have not been able to resolve my problem. 我在stackoverflow上浏览了大量类似的问题,试图了解如何解决这个问题,但我无法解决我的问题。

Any suggestions? 有什么建议么? All help is very much appreciated. 非常感谢所有帮助。

The certificate itself seems trusted, so your javax.net.ssl.trustStore setting worked, but the host name doesn't match. 证书本身似乎是可信的,因此您的javax.net.ssl.trustStore设置有效,但主机名不匹配。

Host name matching is done according to how the client identifies the host it's trying to access. 根据客户端如何识别它尝试访问的主机来完成主机名匹配。 If it's trying to access https://localhost/ , then the certificate must be valid for localhost . 如果它尝试访问https://localhost/ ,则证书必须对localhost有效。 If it's trying to access https://something-else.example , then the certificate must be valid for something-else.example , even if localhost and something-else.example are one and the same machine. 如果它试图访问https://something-else.example ,那么证书必须对something-else.example有效something-else.example ,即使localhostsomething-else.example是同一台机器也是如此。

The identifier in the certificate should be in a Subject Alternative Name extension or, failing that, in the Common Name (CN) of the Subject Distinguished Name. 证书中的标识符应该在主题备用名称扩展名中,或者在主题可分辨名称的公用名(CN)中失败。

Here, it looks like your certificate only has a CN and that this CN is for " 9200416 arx sa cert ". 在这里,您的证书看起来只有CN,并且此CN用于“ 9200416 arx sa cert ”。

In principle, you could address that problem by having that name point to your localhost using your hosts file on your development machine. 原则上,您可以使用开发计算机上的hosts文件将该名称指向localhost来解决该问题。 However, that name contains spaces, so it's not even a valid host name. 但是,该名称包含空格,因此它甚至不是有效的主机名。

You get a couple of options: 你有几个选择:

  1. Re-generate the certificate for that application, so that it uses a proper host name (and if required, adapt your hosts file). 为该应用程序重新生成证书,以便它使用正确的主机名(如果需要,可以调整hosts文件)。 This might just be a mistake when it was set up. 这可能只是在设置时出错。 Maybe someone just filled in that name with spaces, without realising it would be used like that in the cert (OpenSSL sometimes calls this "Your Name", for example). 也许有人只是用空格填充该名称,但没有意识到它会像证书中那样被使用(例如,OpenSSL有时称之为“你的名字”)。

  2. A bad option would be to change your application to ignore host name validation. 一个不好的选择是更改您的应用程序以忽略主机名验证。 This is a bad option because this leaves your code open to MITM attacks. 这是一个糟糕的选择,因为这会使您的代码对MITM攻击开放。 Of course, this barely matters from localhost to localhost, but that's the kind of code that stays in the code. 当然,这从localhost到localhost几乎不重要,但这是代码中保留的那种代码。 Because it will prevent an error (that would otherwise be an intended error) from happening, it's likely that removing this from production code will be forgotten. 因为它可以防止发生错误(否则会是一个预期的错误),所以很可能会忘记从生产代码中删除它。 Even in places with good development practices, it's easy to miss. 即使在具有良好开发实践的地方,也很容易错过。 That's a bad option (just to stress this point). 这是一个糟糕的选择(只是为了强调这一点)。

    A slightly better variant for this would be to have a custom host name verifier that checks the name it finds is the name you know to be in the certificate. 稍微好一点的变体是拥有一个自定义主机名验证程序,用于检查它找到的名称是否是您知道在证书中的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM