Skip to content

Instantly share code, notes, and snippets.

@erikjhordan-rey
Last active November 9, 2017 03:52
Show Gist options
  • Save erikjhordan-rey/10af1cc0b99a54f5b9a8a7614cca6f0a to your computer and use it in GitHub Desktop.
Save erikjhordan-rey/10af1cc0b99a54f5b9a8a7614cca6f0a to your computer and use it in GitHub Desktop.
dependencies {
compile 'com.squareup.okhttp3:okhttp:3.2.0'
compile 'com.squareup.okhttp3:okhttp-urlconnection:3.2.0'
compile 'com.squareup.retrofit2:retrofit:2.0.2
}
/**
* Copyright 2016 Erik Jhordan Rey.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import android.content.Context;
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.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManagerFactory;
import okhttp3.OkHttpClient;
public class SelfSignInClient {
private Context context;
public SelfSignInClient(Context context) {
this.context = context;
}
public OkHttpClient getOkHttpClient() {
OkHttpClient.Builder okHttpClient = new OkHttpClient.Builder();
Certificate certificate = getCertificate();
KeyStore keyStore = createKeyStoreTrustedCAs(certificate);
TrustManagerFactory managerFactory = createTrustManagerCAs(keyStore);
SSLContext sslContext = createSSLSocketFactory(managerFactory);
okHttpClient.sslSocketFactory(sslContext.getSocketFactory());
okHttpClient.hostnameVerifier(new HostnameVerifier() {
@Override public boolean verify(String hostname, SSLSession session) {
return hostname.equals("your_host_name");
}
});
// If you need an Interceptor to add some header
//okHttpClient.addInterceptor();
return okHttpClient.build();
}
// creating an SSLSocketFactory that uses our TrustManager
private SSLContext createSSLSocketFactory(TrustManagerFactory managerFactory) {
final String PROTOCOL = "TLS";
SSLContext sslContext = null;
try {
sslContext = SSLContext.getInstance(PROTOCOL);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
try {
assert sslContext != null;
sslContext.init(null, managerFactory.getTrustManagers(), null);
} catch (KeyManagementException e) {
e.printStackTrace();
}
return sslContext;
}
// creating a TrustManager that trusts the CAs in our KeyStore
private TrustManagerFactory createTrustManagerCAs(KeyStore keyStore) {
String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory managerFactory = null;
try {
managerFactory = TrustManagerFactory.getInstance(tmfAlgorithm);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
try {
assert managerFactory != null;
managerFactory.init(keyStore);
} catch (KeyStoreException e) {
e.printStackTrace();
}
return managerFactory;
}
// creating a KeyStore containing our trusted CAs
private KeyStore createKeyStoreTrustedCAs(Certificate certificate) {
final String ALIAS_CA = "ca";
String keyStoreType = KeyStore.getDefaultType();
KeyStore keyStore = null;
try {
keyStore = KeyStore.getInstance(keyStoreType);
} catch (KeyStoreException e) {
e.printStackTrace();
}
try {
assert keyStore != null;
keyStore.load(null, null);
} catch (IOException | NoSuchAlgorithmException | CertificateException e) {
e.printStackTrace();
}
try {
keyStore.setCertificateEntry(ALIAS_CA, certificate);
} catch (KeyStoreException e) {
e.printStackTrace();
}
return keyStore;
}
// creating a Certificate
private Certificate getCertificate() {
Certificate certificate = null;
CertificateFactory certificateFactory = loadCertificateAuthorityFromResources();
InputStream inputStream = getCAFromResources();
try {
certificate = certificateFactory.generateCertificate(inputStream);
} catch (CertificateException e) {
e.printStackTrace();
}
return certificate;
}
// loading CAs from an InputStream
private CertificateFactory loadCertificateAuthorityFromResources() {
final String CERT_TYPE = "X.509";
InputStream certificateAuthority = getCAFromResources();
CertificateFactory certificateFactory = null;
try {
certificateFactory = CertificateFactory.getInstance(CERT_TYPE);
} catch (CertificateException e) {
e.printStackTrace();
}
try {
assert certificateFactory != null;
certificateFactory.generateCertificate(certificateAuthority);
} catch (CertificateException e) {
e.printStackTrace();
} finally {
try {
certificateAuthority.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return certificateFactory;
}
// loading CAs from Resources
// save your certificate.crt on raw package in your resources
private InputStream getCAFromResources() {
return context.getResources().openRawResource(R.raw.certificate);
}
}
@erikjhordan-rey
Copy link
Author

SSL Android + Okhttp + Retrofit

@erikjhordan-rey
Copy link
Author

Now is included with more complexity in to my library BaseComponents https://github.com/erikcaffrey/Android-Base-Components

@5ran6
Copy link

5ran6 commented Oct 20, 2017

Please how do I call this class in my android code when I am also passing 3 query parameters? e.g https://host/name/?matNO=3434/34&username=lsdf&password=dff

@erikjhordan-rey
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment