我有一个连接到我们托管的SSL Web服务的Android应用程序。Web服务器是Apache,具有我们创建的自己的CA和自签名的SSL证书。
我已将我们的CA证书导入Android平板电脑的"安全性"中的用户受信任证书部分。
我已测试访问Web服务器,并可以确认Web服务证书显示为有效(如下所示的屏幕截图)。
以下是安全设置中的证书:
现在当我尝试在我的应用程序中访问Web服务时,触发了"No Peer Certificate"异常。
这是简化的SSL实现:
我已将我们的CA证书导入Android平板电脑的"安全性"中的用户受信任证书部分。
我已测试访问Web服务器,并可以确认Web服务证书显示为有效(如下所示的屏幕截图)。
以下是安全设置中的证书:
现在当我尝试在我的应用程序中访问Web服务时,触发了"No Peer Certificate"异常。
这是简化的SSL实现:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// allows network on main thread (temp hack)
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
SchemeRegistry schemeRegistry = new SchemeRegistry();
//schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
schemeRegistry.register(new Scheme("https", newSSLSocketFactory(), 443));
HttpParams params = new BasicHttpParams();
SingleClientConnManager mgr = new SingleClientConnManager(params, schemeRegistry);
HttpClient client = new DefaultHttpClient(mgr, params);
HttpPost httpRequest = new HttpPost("https://our-web-service.com");
try {
client.execute(httpRequest);
} catch (Exception e) {
e.printStackTrace(); //
}
}
/*
* Standard SSL CA Store Setup //
*/
private SSLSocketFactory newSSLSocketFactory() {
KeyStore trusted;
try {
trusted = KeyStore.getInstance("AndroidCAStore");
trusted.load(null, null);
Enumeration<String> aliases = trusted.aliases();
while (aliases.hasMoreElements()) {
String alias = aliases.nextElement();
X509Certificate cert = (X509Certificate) trusted.getCertificate(alias);
Log.d("", "Alias="+alias);
Log.d("", "Subject DN: " + cert.getSubjectDN().getName());
Log.d("", "Issuer DN: " + cert.getIssuerDN().getName());
}
SSLSocketFactory sf = new SSLSocketFactory(trusted);
sf.setHostnameVerifier(SSLSocketFactory.STRICT_HOSTNAME_VERIFIER);
return sf;
} catch (Exception e) {
// TODO Auto-generated catch block
throw new AssertionError(e);
}
}
}
while循环只是输出证书,我可以在日志中看到我的CA。但是我仍然收到“没有对等证书”的异常。
10-17 18:29:01.234:I / System.out(4006):没有对等证书
在此实现中,我是否需要手动加载我的CA证书?