-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from phobeous/master
New validatesSecureCertificate to bypass SSL validation
- Loading branch information
Showing
3 changed files
with
167 additions
and
11 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package av.imageview.utils; | ||
|
||
import okhttp3.OkHttpClient; | ||
|
||
import javax.net.ssl.X509TrustManager; | ||
import javax.net.ssl.HostnameVerifier; | ||
import javax.net.ssl.SSLSession; | ||
import javax.net.ssl.TrustManager; | ||
import javax.net.ssl.SSLContext; | ||
import javax.net.ssl.SSLSocketFactory; | ||
import java.security.cert.CertificateException; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.security.KeyManagementException; | ||
|
||
/** | ||
* Helper to avoid oversizing AvImageView class | ||
*/ | ||
public class SSLHelper | ||
{ | ||
private static final String LCAT = "SSLHelper"; | ||
|
||
public static OkHttpClient trustAllSslClient(OkHttpClient client) { | ||
okhttp3.OkHttpClient.Builder builder = client.newBuilder(); | ||
builder.sslSocketFactory(trustAllSslSocketFactory, (X509TrustManager)trustAllCerts[0]); | ||
builder.hostnameVerifier(new HostnameVerifier() { | ||
@Override | ||
public boolean verify(String hostname, SSLSession session) { | ||
return true; | ||
} | ||
}); | ||
return builder.build(); | ||
} | ||
|
||
public static final TrustManager[] trustAllCerts = new TrustManager[] { | ||
new X509TrustManager() { | ||
@Override | ||
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException { | ||
} | ||
|
||
@Override | ||
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException { | ||
} | ||
|
||
@Override | ||
public java.security.cert.X509Certificate[] getAcceptedIssuers() { | ||
return new java.security.cert.X509Certificate[]{}; | ||
} | ||
} | ||
}; | ||
|
||
private static final SSLContext trustAllSslContext; | ||
|
||
static { | ||
try { | ||
trustAllSslContext = SSLContext.getInstance("SSL"); | ||
trustAllSslContext.init(null, trustAllCerts, new java.security.SecureRandom()); | ||
} catch (NoSuchAlgorithmException | KeyManagementException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public static final SSLSocketFactory trustAllSslSocketFactory = trustAllSslContext.getSocketFactory(); | ||
} |