【发布时间】:2015-11-25 01:27:59
【问题描述】:
编辑: cmets 中的 BNK 已链接到找到的解决方案 here。
我通过 REST 将 POST 请求发送到后端服务器(通过 LAN),所有这些都通过 HTTPS 完成。此服务器有一个 .pem 文件形式的自签名证书,一切正常。
我现在正尝试连接到不同的网络服务器(通过 WAN,通过 DNS),一个自签名证书也是一个 .crt 文件(标准,BER/DER 格式)。但是现在,虽然代码相同,但我收到以下异常:
java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
我不确定为什么一台服务器可以连接,而另一台则不能。我不想信任所有证书,因为这将通过公共互联网进行。
我的网络代码:
public HttpsURLConnection setUpHttpsConnection(String urlString)
{
try
{
// Load CAs from an InputStream
CertificateFactory cf = CertificateFactory.getInstance("X.509");
InputStream caInput = new BufferedInputStream(context.getAssets().open("server.crt"));
Certificate ca = cf.generateCertificate(caInput);
System.out.println("ca=" + ((java.security.cert.X509Certificate) ca).getSubjectDN());
// Create a KeyStore containing our trusted CAs
String keyStoreType = KeyStore.getDefaultType();
KeyStore keyStore = KeyStore.getInstance(keyStoreType);
keyStore.load(null, null);
keyStore.setCertificateEntry("ca", ca);
// Create a TrustManager that trusts the CAs in our KeyStore
String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
tmf.init(keyStore);
// Create an SSLContext that uses our TrustManager
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, tmf.getTrustManagers(), null);
// Create all-trusting host name verifier
// to avoid the following :
// java.security.cert.CertificateException: No name matching
// This is because Java by default verifies that the certificate CN (Common Name) is
// the same as host name in the URL. If they are not, the web service client fails.
HostnameVerifier allHostsValid = new HostnameVerifier() {
@Override
public boolean verify(String arg0, SSLSession arg1) {
return true;
}
};
// Install it
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
// Tell the URLConnection to use a SocketFactory from our SSLContext
URL url = new URL(urlString);
HttpsURLConnection urlConnection = null;
urlConnection = (HttpsURLConnection)url.openConnection();
urlConnection.setSSLSocketFactory(sslContext.getSocketFactory());
return urlConnection;
}
catch (Exception ex)
{
Log.e("NetworkManager", "Failed to establish SSL connection to server: " + ex.toString());
return null;
}
}
/**
* Represents an asynchronous login/registration task used to authenticate
* the user.
*/
public class POSTTask extends AsyncTask<POSTRequest, Void, StringBuilder>
{
POSTTask()
{
}
@Override
protected void onPreExecute() {}
@Override
protected StringBuilder doInBackground(POSTRequest... params)
{
OutputStream os = null;
try {
HttpsURLConnection urlConnection = setUpHttpsConnection(params[0].url);
//Sets the maximum time to wait for an input stream read to complete before giving up.
urlConnection.setReadTimeout(30000);
//Sets the maximum time in milliseconds to wait while connecting.
urlConnection.setConnectTimeout(20000);
urlConnection.setRequestMethod("POST");
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(params[0].nameValuePairs);
os = urlConnection.getOutputStream();
formEntity.writeTo(os);
InputStream in = urlConnection.getInputStream();
StringBuilder ret = inputStreamToString(in);
return ret;
} catch (IOException e) {
Log.i("NetworkError", e.toString());
} catch (Exception e) {
} finally {
if (os != null) {
try {
os.close();
} catch (IOException ex) {
}
}
}
return null;
}
@Override
protected void onPostExecute(StringBuilder result) {
}
@Override
protected void onCancelled() {
}
}
【问题讨论】:
-
与问题无关,但与您的代码有关:像您一样禁用主机名检查有效地攻击者使用其由受信任的 CA 签名的 any 证书进行人工入侵-中间攻击。由于这样的证书很容易获得(黑客需要拥有一个域名),因此您可以通过这种方式有效地禁用任何类型的证书检查。
-
@LBran:正确的方法当然是让证书与名称匹配。处理自签名证书的最佳方法是根本不使用系统信任库,而只信任特定证书。有关更多详细信息和示例代码,请访问OWASP: certificate and public key pinning。
-
好吧,如果它是您的服务器,请让 CA 对其进行签名。从长远来看,它更便宜。如果不是您的服务器,请投诉。在短期内,您可以将他们的证书导入您的信任库,但这不是生产的方式。
-
嗨! IMO,您可以在this question 尝试我的回答,以检查它是否适用于您的情况。注意“getWrappedTrustManagers”。
-
@BNK 嘿!感谢您的回复 - 我可以确认这是可行的,并且适合临时修复。这只是一个演示,所以就足够了,但我肯定需要解决一些问题,所以我不相信一切。 :)
标签: android ssl https self-signed