Java SSLException:证书中的主机名不匹配.非常错误的证书?

Dza*_*ata 1 apache ssl https android

我正在为Android写简单的记事本,通过互联网与我的服务器同步数据.我使用了来自startssl的https和免费证书,一切顺利.后来我用它自己的ssl证书添加了新的虚拟主机,我的应用程序突然停止了对服务器的连接.它抛出:

javax.net.ssl.SSLException: hostname in certificate didn't match: <www.rapidnote.emendus.ovh> != <othersite.emendus.ovh> OR <othersite.emendus.ovh> OR <emendus.ovh>
Run Code Online (Sandbox Code Playgroud)

它尝试从不同的apache虚拟主机验证完全不同的证书.我的用于rapidnote虚拟主机的apache配置是:

<VirtualHost *:80>
  ServerName                rapidnote.emendus.ovh
  ServerAlias               www.rapidnote.emendus.ovh
  Redirect Permanent        /   https://www.rapidnote.emendus.ovh
</VirtualHost>

<VirtualHost *:443>
  ServerName                rapidnote.emendus.ovh
  DocumentRoot              /var/www/rapidnote
  SSLEngine                 on
  SSLCertificateFile        cert_location/rapidnote.emendus.ovh.crt
  SSLCertificateKeyFile     key_location/rapidnote.emendus.ovh.key
  SSLCertificateChainFile   cert_location/sub.class1.server.ca.pem
</VirtualHost>

<VirtualHost *:443>
  ServerName                www.rapidnote.emendus.ovh
  DocumentRoot              /var/www/rapidnote
  SSLEngine                 on
  SSLCertificateFile        cert_location/www.rapidnote.emendus.ovh.crt
  SSLCertificateKeyFile     key_location/www.rapidnote.emendus.ovh.key
  SSLCertificateChainFile   cert_location/sub.class1.server.ca.pem
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

我的代码如下:

HttpClient   httpClient = new DefaultHttpClient();
HttpPost     httpPost   = new HttpPost("https://www.rapidnote.emendus.ovh/" + paramScriptName);
HttpResponse response   = httpClient.execute(httpPost);
Run Code Online (Sandbox Code Playgroud)

如果我othersite.emendus.ovh在apache中关闭虚拟主机,则SSLException消息说:

<www.rapidnote.emendus.ovh> != <rapidnote.emendus.ovh> OR <rapidnote.emendus.ovh> OR <emendus.ovh> 
Run Code Online (Sandbox Code Playgroud)

如果然后我改"https://www.rapidnote.emendus.ovh/"到"https://rapidnote.emendus.ovh/",它开始工作.我检查了两个(www.和非www)证书,两者中的通用名称都是正确的.我认为它应该验证我在HttpPost构造函数中给他的域证书,而不是我的服务器中的其他随机证书...

我使用了一些测试网站来验证我的ssl配置,它表示一切都很好.

一切都在发生android 4.2.2 (API 17).我用min SDK 14和构建应用程序target SDK API 20 Android 4.4 (KitKat Wear)

如何告诉他验证为此域名指定的正确证书?是应用程序或Apache的网站上的问题?

Bru*_*uno 5

您的服务器配置为使用服务器名称指示(SNI)呈现不同的证书.

据我所知,与Android捆绑在一起的Apache HTTP Client版本不支持SNI.您可以尝试使用HttpsURLConnection而不是HttpClient,这应该支持不太旧的Android版本的SNI.这个问题的答案中有更多细节.

请注意,您仍然需要为主机名提供的证书对该主机名有效,即它需要该名称的主题备用名称条目(或者如果主题DN的CN需要是该名称,如果没有DNS SAN).(如果您有这样的证书,也可以使用通配符.)