59
59
* certificate validation on every device, use with caution
60
60
*/
61
61
public class MySSLSocketFactory extends SSLSocketFactory {
62
- SSLContext sslContext ;
62
+ final SSLContext sslContext = SSLContext . getInstance ( "TLS" ) ;
63
63
64
64
/**
65
65
* Creates a new SSL Socket Factory with the given KeyStore.
@@ -72,18 +72,6 @@ public class MySSLSocketFactory extends SSLSocketFactory {
72
72
*/
73
73
public MySSLSocketFactory (KeyStore truststore ) throws NoSuchAlgorithmException , KeyManagementException , KeyStoreException , UnrecoverableKeyException {
74
74
super (truststore );
75
-
76
- // Define sslContext
77
- try {
78
- sslContext = SSLContext .getInstance ("TLSv1.2" );
79
- Log .w ("SSLSocketFactory" , "TLSv1.2 is supported" );
80
- } catch (NoSuchAlgorithmException e ) {
81
- // TODO fallback v1.1 if needed
82
- Log .w ("SSLSocketFactory" , "TLSv1.2 is not supported in this device; falling through TLSv1.0" );
83
- sslContext = SSLContext .getInstance ("TLSv1" );
84
- // should be available in any device; see reference of supported protocols in
85
- // http://developer.android.com/reference/javax/net/ssl/SSLSocket.html
86
- }
87
75
88
76
X509TrustManager tm = new X509TrustManager () {
89
77
public void checkClientTrusted (X509Certificate [] chain , String authType ) throws CertificateException {
@@ -212,28 +200,17 @@ public Socket createSocket() throws IOException {
212
200
enableSecureProtocols (socket );
213
201
return socket ;
214
202
}
215
-
203
+
216
204
/**
217
205
* Activate supported protocols on the socket.
218
- *
206
+ *
219
207
* @param socket The socket on which to activate secure protocols.
220
208
*/
221
209
private void enableSecureProtocols (Socket socket ) {
222
- // get supported params
210
+ // set all supported protocols
223
211
SSLParameters params = sslContext .getSupportedSSLParameters ();
224
- String [] supportedProtocols = params .getProtocols ();
225
-
226
- // activate supported protocols on the socket
227
- try {
228
- Socket localSocket = sslContext .getSocketFactory ().createSocket ();
229
- ((SSLSocket ) localSocket ).setEnabledProtocols (supportedProtocols );
230
- }catch (Exception e )
231
- {
232
-
233
- }
234
-
235
- //((SSLSocket) socket).setEnabledProtocols(new String[] {"TLSv1.2"} );
236
- }
212
+ ((SSLSocket ) socket ).setEnabledProtocols (params .getProtocols ());
213
+ }
237
214
238
215
/**
239
216
* Makes HttpsURLConnection trusts a set of certificates specified by the KeyStore
@@ -242,4 +219,4 @@ public void fixHttpsURLConnection() {
242
219
HttpsURLConnection .setDefaultSSLSocketFactory (sslContext .getSocketFactory ());
243
220
}
244
221
245
- }
222
+ }
0 commit comments