Ho un'applicazione Android che si sta connettendo a un servizio Web SSL che ospitiamo. Il server Web è apache e ha una propria CA che abbiamo creato e un certificato SSL autofirmato.Richiesta HTTP SSL HTTP con certificato autofirmato e CA
Ho importato il certificato CA sul tablet Android nella sezione Certificati attendibili dell'utente in Sicurezza.
Ho testato l'accesso al server web e posso confermare che il certificato di servizio web mostra come valido (screenshot qui sotto)
Ecco il certificato nelle impostazioni di sicurezza:
Ora quando provo ad accedere al servizio web nella mia applicazione ottengo l'eccezione "Nessun certificato peer" attivata.
Questa è l'implementazione SSL semplificata:
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);
}
}
}
Il ciclo while appena sputa fuori i certificati e posso vedere il mio CA nei registri. Ma ottengo ancora l'eccezione "No Peer Certificate".
10-17 18: 29: 01,234: I/System.out (4006): Nessun certificato pari
Devo caricare manualmente il mio certificato CA in qualche modo in questa implementazione?
Hai provato manualmente il caricamento del certificato CA in questa implementazione? – BON
Puoi ottenere un certificato SSL gratuito e affidabile per il tuo dominio da http://www.startssl.com/ (li sto utilizzando per alcune app), quindi non devi occuparti di aggiungere la CA in ogni dispositivo che vuole usare la tua app. –