Skip to content

Commit

Permalink
first stab at supporting SSL certificate pinning
Browse files Browse the repository at this point in the history
  • Loading branch information
slogan621 committed Jun 14, 2018
1 parent 9bc77ab commit ba8166f
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package org.thousandsmiles.tscharts_lib;

import android.content.Context;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.security.KeyStore;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManagerFactory;

public class TrustedSSLSocketFactory {

private static SSLSocketFactory m_sf = null;
private static TrustedSSLSocketFactory m_instance = null;

public static TrustedSSLSocketFactory getInstance() {
if (m_instance == null) {
m_instance = new TrustedSSLSocketFactory();
}
return m_instance;
}

public SSLSocketFactory getSocketFactory(Context aCtx) {

CertificateFactory cf = null;

if (m_sf != null) {
return m_sf;
}
try {
cf = CertificateFactory.getInstance("X.509");
} catch (CertificateException e) {
return null;
}

Certificate ca = null;
// Generate the certificate using the certificate file under res/raw/cert.cer
InputStream caInput = new BufferedInputStream(aCtx.getResources().openRawResource(R.raw.cert));
try {
ca = cf.generateCertificate(caInput);
try {
caInput.close();
} catch(java.io.IOException e) {
}
} catch (CertificateException e) {
return null;
}

// Create a KeyStore containing our trusted CAs
String keyStoreType = KeyStore.getDefaultType();
KeyStore trusted = null;
try {
trusted = KeyStore.getInstance(keyStoreType);
trusted.load(null, null);
trusted.setCertificateEntry("ca", ca);
} catch (java.security.NoSuchAlgorithmException | java.security.KeyStoreException | java.io.IOException | java.security.cert.CertificateException e ) {
return null;
}

// Create a TrustManager that trusts the CAs in our KeyStore
String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory tmf = null;
try {
tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
tmf.init(trusted);
} catch (java.security.KeyStoreException | java.security.NoSuchAlgorithmException e) {
return null;
}

// Create an SSLContext that uses our TrustManager
SSLContext context = null;

try {
context = SSLContext.getInstance("TLS");
context.init(null, tmf.getTrustManagers(), null);
} catch (java.security.KeyManagementException | java.security.NoSuchAlgorithmException e) {
return null;
}

m_sf = context.getSocketFactory();
return m_sf;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,32 @@
import android.content.Context;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.HurlStack;
import com.android.volley.toolbox.Volley;

import javax.net.ssl.SSLSocketFactory;

public class VolleySingleton {
private static VolleySingleton instance;
private static RequestQueue queue;
private static VolleySingleton m_instance;
private static RequestQueue m_queue;

public void initQueueIf(Context context) {
if (queue == null) {
queue = Volley.newRequestQueue(context);
if (m_queue == null) {
TrustedSSLSocketFactory tssf = TrustedSSLSocketFactory.getInstance();
SSLSocketFactory sf = tssf.getSocketFactory(context);
m_queue = Volley.newRequestQueue(context, new HurlStack(null, sf));
}
}

public RequestQueue getQueue() {
return queue;
return m_queue;
}

public static VolleySingleton getInstance() {
if (instance == null) {
instance = new VolleySingleton();
if (m_instance == null) {
m_instance = new VolleySingleton();
}
return instance;
return m_instance;
}
}

Empty file added app/src/main/res/raw/cert
Empty file.

0 comments on commit ba8166f

Please sign in to comment.