https 证书校验异常 Trust anchor for certification path not found

经过合法证书机构签发的证书,在android中无法通过系统验证,出现以下异常

java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.


1.问题原因:

服务器证书链配置错误,缺少中间证书设置  


2. stackoverflow 解决方案:

 服务器修改证书链配置


3. 可以通过openssl 测试证书脸是否配置正确 ,下面是支付宝的证书链,通常错误的证书链只包含你服务器自己的证书。

通过修改https跳过证书验证,将存在安全风险,导致不安全得连接。


 openssl s_client -debug -connect www.alipay.com:443


---
Certificate chain
 0 s:/C=CN/ST=ZHEJIANG/L=HANGZHOU/O=Alipay.com Co.,Ltd/OU=Operations Department/CN=*.alipay.com
   i:/C=US/O=Symantec Corporation/OU=Symantec Trust Network/CN=Symantec Class 3 Secure Server CA - G4
 1 s:/C=US/O=Symantec Corporation/OU=Symantec Trust Network/CN=Symantec Class 3 Secure Server CA - G4
   i:/C=US/O=VeriSign, Inc./OU=VeriSign Trust Network/OU=(c) 2006 VeriSign, Inc. - For authorized use only/CN=VeriSign Class 3 Public Primary Certification Authority - G5
 2 s:/C=US/O=VeriSign, Inc./OU=VeriSign Trust Network/OU=(c) 2006 VeriSign, Inc. - For authorized use only/CN=VeriSign Class 3 Public Primary Certification Authority - G5
   i:/C=US/O=VeriSign, Inc./OU=Class 3 Public Primary Certification Authority
---




原文: http://stackoverflow.com/questions/6825226/trust-anchor-not-found-for-android-ssl-connection


Contrary to the accepted answer you do not need a custom trust manager, you need to fix your server configuration!


I hit the same problem while connecting to an Apache server with an incorrectly installed dynadot/alphassl certificate. I'm connecting using HttpsUrlConnection (Java/Android), which was throwing -


javax.net.ssl.SSLHandshakeException: 
  java.security.cert.CertPathValidatorException: 
    Trust anchor for certification path not found.
The actual problem is a server misconfiguration - test it with http://www.digicert.com/help/ or similar, and it will even tell you the solution:


"The certificate is not signed by a trusted authority (checking against Mozilla's root store). If you bought the certificate from a trusted authority, you probably just need to install one or more Intermediate certificates. Contact your certificate provider for assistance doing this for your server platform."


You can also check the certificate with openssl:


openssl s_client -debug -connect www.thedomaintocheck.com:443


You'll probably see:


Verify return code: 21 (unable to verify the first certificate)


and, earlier in the output:


depth=0 OU = Domain Control Validated, CN = www.thedomaintocheck.com
verify error:num=20:unable to get local issuer certificate
verify return:1
depth=0 OU = Domain Control Validated, CN = www.thedomaintocheck.com
verify error:num=27:certificate not trusted
verify return:1
depth=0 OU = Domain Control Validated, CN = www.thedomaintocheck.com
verify error:num=21:unable to verify the first certificate`
The certificate chain will only contain 1 element (your certificate):


Certificate chain
 0 s:/OU=Domain Control Validated/CN=www.thedomaintocheck.com
  i:/O=AlphaSSL/CN=AlphaSSL CA - G2
... but should reference the signing authorities in a chain back to one which is trusted by Android (Verisign, GlobalSign, etc):


Certificate chain
 0 s:/OU=Domain Control Validated/CN=www.thedomaintocheck.com
   i:/O=AlphaSSL/CN=AlphaSSL CA - G2
 1 s:/O=AlphaSSL/CN=AlphaSSL CA - G2
   i:/C=BE/O=GlobalSign nv-sa/OU=Root CA/CN=GlobalSign Root CA
 2 s:/C=BE/O=GlobalSign nv-sa/OU=Root CA/CN=GlobalSign Root CA
   i:/C=BE/O=GlobalSign nv-sa/OU=Root CA/CN=GlobalSign Root CA
Instructions (and the intermediate certificates) for configuring your server are usually provided by the authority that issued your certificate, for example: http://www.alphassl.com/support/install-root-certificate.html


After installing the intermediate certificates provided by my certificate issuer I now have no errors when connecting using HttpsUrlConnection.

你可能感兴趣的:(https)