1616
1717package com .vorlonsoft .android .http ;
1818
19+ import android .util .Log ;
20+
1921import java .io .BufferedInputStream ;
2022import java .io .IOException ;
2123import java .io .InputStream ;
5658 * certificate validation on every device, use with caution
5759 */
5860public class MySSLSocketFactory extends SSLSocketFactory {
59- final SSLContext sslContext = SSLContext . getInstance ( "TLS" ) ;
61+ SSLContext sslContext ;
6062
6163 /**
6264 * Creates a new SSL Socket Factory with the given KeyStore.
@@ -70,6 +72,21 @@ public class MySSLSocketFactory extends SSLSocketFactory {
7072 public MySSLSocketFactory (KeyStore truststore ) throws NoSuchAlgorithmException , KeyManagementException , KeyStoreException , UnrecoverableKeyException {
7173 super (truststore );
7274
75+ try {
76+ sslContext = SSLContext .getInstance ("TLSv1.2" );
77+ Log .w ("SSLSocketFactory" , "TLSv1.2 is supported" );
78+ } catch (NoSuchAlgorithmException e2 ) {
79+ try {
80+ Log .w ("SSLSocketFactory" , "TLSv1.2 is not supported in this device; falling through TLSv1.1" );
81+ sslContext = SSLContext .getInstance ("TLSv1.1" );
82+ } catch (NoSuchAlgorithmException e1 ) {
83+ Log .w ("SSLSocketFactory" , "TLSv1.2 and TLSv1.1 is not supported in this device; falling through TLSv1.0" );
84+ sslContext = SSLContext .getInstance ("TLSv1" );
85+ // should be available in any device; see reference of supported protocols in
86+ // http://developer.android.com/reference/javax/net/ssl/SSLSocket.html
87+ }
88+ }
89+
7390 X509TrustManager tm = new X509TrustManager () {
7491 public void checkClientTrusted (X509Certificate [] chain , String authType ) throws CertificateException {
7592 try {
@@ -214,8 +231,13 @@ public Socket createSocket() throws IOException {
214231 */
215232 private void enableSecureProtocols (Socket socket ) {
216233 // set all supported protocols
217- SSLParameters params = sslContext .getSupportedSSLParameters ();
218- ((SSLSocket ) socket ).setEnabledProtocols (params .getProtocols ());
234+ try {
235+ SSLParameters params = sslContext .getSupportedSSLParameters ();
236+ ((SSLSocket ) socket ).setEnabledProtocols (params .getProtocols ());
237+ }catch (Exception e )
238+ {
239+ Log .w ("SSLSocketFactory" , e );
240+ }
219241 }
220242
221243 /**
0 commit comments