我正在嘗試在服務器和Android客戶端之間創建TLS v1.2通信。 我與任何問題建立了TLS v1.0連接,但我無法獲得v1.2。 這是服務器代碼:TLS v1.2上的Android客戶端/服務器
char[] passphrase = "myComplexPass1".toCharArray();
KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
keystore.load(new FileInputStream("cacerts"), passphrase);
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509");
keyManagerFactory.init(keystore, passphrase);
SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
KeyManager[] keyManagers = keyManagerFactory.getKeyManagers();
SSLContext sslContext.init(keyManagers, null, null);
SSLServerSocketFactory sslServerSocketFactory = sslContext.getServerSocketFactory();
SSLServerSocket sslServerSocket = (SSLServerSocket) sslServerSocketFactory.createServerSocket(port);
sslServerSocket.setEnabledProtocols(new String [] { "TLSv1", "TLSv1.1", "TLSv1.2" });
sslServerSocket.setUseClientMode(false);
sslServerSocket.setWantClientAuth(false);
sslServerSocket.setNeedClientAuth(false);
sslSocket = (SSLSocket)sslServerSocket.accept();
,而這是客戶端代碼:
char[] passphrase = "myComplexPass1".toCharArray();
KeyStore keystore = KeyStore.getInstance("BKS");
keystore.load(this.getApplicationContext().getResources().openRawResource(R.raw.jb), passphrase);
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
keyManagerFactory.init(keystore, passphrase);
SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
Log.d("Context Protocol",sslContext.getProtocol());//this prints correctly TLS v1.2!
KeyManager[] keyManagers = keyManagerFactory.getKeyManagers();
TrustManager[] trustManagers = new TrustManager[]{
new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers()
{
return null;
}
public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType)
{
}
public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType)
{
}
}
};
sslContext.init(keyManagers, trustManagers, new SecureRandom());
SSLSocketFactory sslSocketFactory = (SSLSocketFactory) sslContext.getSocketFactory();
SSLSocket skt = (SSLSocket) sslSocketFactory.createSocket(HOST, PORT);
skt.setKeepAlive(true);
客戶端代碼,寫在我的電腦上JRE7運行的Java客戶端,完美的作品和我的getProtocol(服務器見端)帶有正確密碼的TLSv1.2,由tlsv1.2支持。 在android上的相同的代碼使tlsv1.0連接! 我真的不明白。 在Java客戶端JRE7上工作,在android ONLY tlsv1.0 任何建議?
這是我的第一個問題,我搜查了很多。也許我的格式不正確:(
TLS 1.2在Android 4.1上可用。請參閱http://stackoverflow.com/questions/5950178/執行tls-1-2-on-android-2-3-3瞭解更多詳情。 – 2013-05-14 06:03:20
謝謝,但根據此[鏈接](http://source.android.com/tech/security/android_4_2_security_enhancements.html)它應該被支持。我正在運行android 4.2.2 – 2013-05-14 07:04:00