Created
June 19, 2018 22:35
-
-
Save jonatasmaxi/25022d4ea9bd4fdc78af336d7d8b8c94 to your computer and use it in GitHub Desktop.
TLSSocketFactory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.example.leonardo.tlsexample; | |
| import android.os.AsyncTask; | |
| import android.os.NetworkOnMainThreadException; | |
| import android.support.v7.app.AppCompatActivity; | |
| import android.os.Bundle; | |
| import android.util.Log; | |
| import android.view.View; | |
| import android.widget.TextView; | |
| import com.fasterxml.jackson.databind.ObjectMapper; | |
| import java.io.BufferedReader; | |
| import java.io.InputStreamReader; | |
| import java.net.HttpURLConnection; | |
| import java.net.Socket; | |
| import java.net.URL; | |
| import java.net.URLConnection; | |
| import java.security.KeyStore; | |
| import java.security.KeyStoreException; | |
| import java.security.NoSuchAlgorithmException; | |
| import java.util.HashMap; | |
| import javax.net.ssl.TrustManager; | |
| import javax.net.ssl.TrustManagerFactory; | |
| import javax.net.ssl.X509TrustManager; | |
| import okhttp3.Call; | |
| import okhttp3.OkHttpClient; | |
| import okhttp3.Request; | |
| import okhttp3.Response; | |
| public class MainActivity extends AppCompatActivity { | |
| public TextView content; | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_main); | |
| content = findViewById(R.id.content); | |
| } | |
| public void buttonOnClick(View view) { | |
| new TLSConnection().execute(); | |
| } | |
| public class TLSConnection extends AsyncTask<Void, Void, String> { | |
| @Override | |
| protected String doInBackground(Void... voids) { | |
| try { | |
| TrustManagerFactory trustManagerFactory = null; | |
| try { | |
| trustManagerFactory = | |
| TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); | |
| trustManagerFactory.init((KeyStore) null); | |
| TrustManager[] trustManagers = trustManagerFactory.getTrustManagers(); | |
| if (trustManagers.length != 1 || !(trustManagers[0] instanceof X509TrustManager)) { | |
| //throw new IllegalStateException( | |
| // "Unexpected default trust managers:" + Arrays.toString(trustManagers)); | |
| } | |
| TLSSocketFactory tlsSocketFactory = new TLSSocketFactory(); | |
| OkHttpClient.Builder builder = new OkHttpClient.Builder(); | |
| builder.sslSocketFactory(tlsSocketFactory,(X509TrustManager) trustManagers[0]); | |
| OkHttpClient client = builder.build(); | |
| String endpoint = "https://tls12.pagar.me"; | |
| Request.Builder bld = new Request.Builder(); | |
| bld.url(endpoint); | |
| Request request = bld.build(); | |
| Call call = client.newCall(request); | |
| Response response = call.execute(); | |
| return response.body().string(); | |
| } catch (NoSuchAlgorithmException e) { | |
| e.printStackTrace(); | |
| } catch (KeyStoreException e) { | |
| e.printStackTrace(); | |
| } | |
| } catch(NetworkOnMainThreadException e) { | |
| Log.e("Erro", e.getMessage(), e); | |
| } catch(Exception e) { | |
| Log.e("Erro", e.getMessage(), e); | |
| } | |
| return null; | |
| } | |
| @Override | |
| protected void onPostExecute(String olar) { | |
| super.onPostExecute(olar); | |
| String msg = olar != null ? olar : "deu meh"; | |
| Log.i("Mensagem", msg); | |
| } | |
| } | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.example.leonardo.tlsexample; | |
| import javax.net.ssl.SSLSocketFactory; | |
| import java.io.IOException; | |
| import java.net.InetAddress; | |
| import java.net.Socket; | |
| import java.net.UnknownHostException; | |
| import java.security.KeyManagementException; | |
| import java.security.NoSuchAlgorithmException; | |
| import javax.net.ssl.SSLContext; | |
| import javax.net.ssl.SSLSocket; | |
| public class TLSSocketFactory extends SSLSocketFactory{ | |
| private SSLSocketFactory internalSSLSocketFactory; | |
| public TLSSocketFactory() throws KeyManagementException, NoSuchAlgorithmException { | |
| SSLContext context = SSLContext.getInstance("TLS"); | |
| context.init(null, null, null); | |
| internalSSLSocketFactory = context.getSocketFactory(); | |
| } | |
| @Override | |
| public String[] getDefaultCipherSuites() { | |
| return internalSSLSocketFactory.getDefaultCipherSuites(); | |
| } | |
| @Override | |
| public String[] getSupportedCipherSuites() { | |
| return internalSSLSocketFactory.getSupportedCipherSuites(); | |
| } | |
| @Override | |
| public Socket createSocket() throws IOException { | |
| return enableTLSOnSocket(internalSSLSocketFactory.createSocket()); | |
| } | |
| @Override | |
| public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException { | |
| return enableTLSOnSocket(internalSSLSocketFactory.createSocket(s, host, port, autoClose)); | |
| } | |
| @Override | |
| public Socket createSocket(String host, int port) throws IOException, UnknownHostException { | |
| return enableTLSOnSocket(internalSSLSocketFactory.createSocket(host, port)); | |
| } | |
| @Override | |
| public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException, UnknownHostException { | |
| return enableTLSOnSocket(internalSSLSocketFactory.createSocket(host, port, localHost, localPort)); | |
| } | |
| @Override | |
| public Socket createSocket(InetAddress host, int port) throws IOException { | |
| return enableTLSOnSocket(internalSSLSocketFactory.createSocket(host, port)); | |
| } | |
| @Override | |
| public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException { | |
| return enableTLSOnSocket(internalSSLSocketFactory.createSocket(address, port, localAddress, localPort)); | |
| } | |
| private Socket enableTLSOnSocket(Socket socket) { | |
| if(socket != null && (socket instanceof SSLSocket)) { | |
| ((SSLSocket)socket).setEnabledProtocols(new String[] {"TLSv1.2"}); | |
| } | |
| return socket; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment