Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Handler para conexión SSL y Pinning
<resources>
<string name="file_name_cer" translatable="false">name_pem.cer</string>
<string name="seed_cer" translatable="false">password</string>
<string name="file_name_cer_private" translatable="false">name_private_cer.p12</string>
</resources>
/**
* Ejemplo para Dagger 2
*/
public class NetworkModule{
@Provides
@Singleton
public OkHttpClient provideOkHttpClient(final HttpLoggingInterceptor loggingInterceptor,
final HeaderInterceptor headerInterceptor,
final TokenInterceptor tokenInterceptor,
final SSLCertificateHandler sslCertificateHandler,
final HostSelectionInterceptor hostSelectionInterceptor) {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.sslSocketFactory(sslCertificateHandler.getSSLSocketFactory(), sslCertificateHandler.getX509TrustManager());
builder.addInterceptor(headerInterceptor);
builder.addInterceptor(tokenInterceptor);
builder.addInterceptor(hostSelectionInterceptor);
if (BuildConfig.DEBUG) {
builder.addInterceptor(loggingInterceptor);
}
return builder.build();
}
}
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;
public class SSLCertificateHandler {
private Context context;
private SSLContext sslContext;
private TrustManager[] certs;
private Certificate cert = null;
private KeyManager[] keyManagers;
private KeyStore keyStore;
public SSLCertificateHandler(Context context) {
this.context = context;
buildSSLConfig();
}
private void buildSSLConfig(){
buildCertificateFromPem();
buildKeyManager();
buildTrustManager();
buildSSLContext();
}
private void buildCertificateFromPem() {
try {
String pemNameFile = context.getResources().getString(R.string.file_name_cer);
CertificateFactory cf = CertificateFactory.getInstance("X.509");
cert = cf.generateCertificate(getFileAssets(pemNameFile));
} catch (CertificateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void buildKeyManager() {
try {
String clientPassword = context.getResources().getString(R.string.seed_cer);
String certP12 = context.getResources().getString(R.string.file_name_cer_private);
keyStore = KeyStore.getInstance("PKCS12");
keyStore.load(new BufferedInputStream(getFileAssets(certP12)), clientPassword.toCharArray());
keyStore.setCertificateEntry("ca", this.cert);
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
kmf.init(keyStore, clientPassword.toCharArray());
keyManagers = kmf.getKeyManagers();
} catch (KeyStoreException | IOException | NoSuchAlgorithmException | CertificateException | UnrecoverableKeyException configureClientCertException) {
configureClientCertException.printStackTrace();
}
}
private void buildTrustManager(){
try {
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(keyStore);
certs = trustManagerFactory.getTrustManagers();
}catch (Exception ex){
ex.printStackTrace();
}
}
private void buildSSLContext() {
try {
sslContext = SSLContext.getInstance("TLS");
sslContext.init(keyManagers, certs, null);
} catch (NoSuchAlgorithmException | KeyManagementException sslContextException) {
sslContextException.printStackTrace();
}
}
public SSLSocketFactory getSSLSocketFactory(){
return sslContext.getSocketFactory();
}
public X509TrustManager getX509TrustManager() {
return (X509TrustManager)certs[0];
}
private InputStream getFileAssets(String fileName) throws IOException {
return context.getAssets().open(fileName);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.