-
-
Save ricdex/2933e8276f6f6634c1dbff6bf07bf79c to your computer and use it in GitHub Desktop.
This file contains 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
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 | |
} |
This file contains 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
/** | |
* 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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment