|
9 | 9 | import java.io.Closeable;
|
10 | 10 | import java.io.IOException;
|
11 | 11 | import java.io.InputStream;
|
| 12 | +import java.security.KeyStore; |
| 13 | +import java.security.SecureRandom; |
12 | 14 | import java.util.ArrayList;
|
13 | 15 | import java.util.Locale;
|
14 | 16 | import java.util.Stack;
|
|
17 | 19 | import java.util.concurrent.ThreadPoolExecutor;
|
18 | 20 | import java.util.concurrent.TimeUnit;
|
19 | 21 |
|
| 22 | +import javax.net.ssl.SSLContext; |
| 23 | +import javax.net.ssl.TrustManager; |
| 24 | +import javax.net.ssl.TrustManagerFactory; |
| 25 | +import javax.net.ssl.X509TrustManager; |
| 26 | + |
20 | 27 | import okhttp3.Call;
|
21 | 28 | import okhttp3.Callback;
|
22 | 29 | import okhttp3.CertificatePinner;
|
|
29 | 36 | import okhttp3.ResponseBody;
|
30 | 37 | import okhttp3.WebSocket;
|
31 | 38 | import okhttp3.WebSocketListener;
|
| 39 | +import okhttp3.internal.tls.OkHostnameVerifier; |
32 | 40 |
|
33 | 41 | public class Async {
|
34 | 42 | static final String TAG = "Async";
|
@@ -89,55 +97,26 @@ public static class Http {
|
89 | 97 | private static MemoryCookieJar cookieJar;
|
90 | 98 | private static CertificatePinner.Builder certificatePinnerBuilder;
|
91 | 99 | private static ImageParseMethod imageParseMethod = ImageParseMethod.CONTENTTYPE;
|
92 |
| - private static boolean allowSslErrors = false; |
| 100 | + |
| 101 | + private static TrustManager TRUST_ALL_CERTS = new X509TrustManager() { |
| 102 | + @Override |
| 103 | + public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) { } |
| 104 | + |
| 105 | + @Override |
| 106 | + public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) { } |
| 107 | + |
| 108 | + @Override |
| 109 | + public java.security.cert.X509Certificate[] getAcceptedIssuers() { |
| 110 | + return new java.security.cert.X509Certificate[] {}; |
| 111 | + } |
| 112 | + }; |
93 | 113 |
|
94 | 114 | public static void InitClient() {
|
95 | 115 | if (cookieJar == null) {
|
96 | 116 | cookieJar = new MemoryCookieJar();
|
97 | 117 | }
|
98 | 118 |
|
99 | 119 | if (client == null) {
|
100 |
| - if (allowSslErrors) { |
101 |
| - // Allow all ssl errors |
102 |
| - try { |
103 |
| - javax.net.ssl.TrustManager TRUST_ALL_CERTS = new javax.net.ssl.X509TrustManager() { |
104 |
| - @Override |
105 |
| - public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) { |
106 |
| - } |
107 |
| - |
108 |
| - @Override |
109 |
| - public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) { |
110 |
| - } |
111 |
| - |
112 |
| - @Override |
113 |
| - public java.security.cert.X509Certificate[] getAcceptedIssuers() { |
114 |
| - return new java.security.cert.X509Certificate[] {}; |
115 |
| - } |
116 |
| - }; |
117 |
| - |
118 |
| - javax.net.ssl.SSLContext sslContext = javax.net.ssl.SSLContext.getInstance("SSL"); |
119 |
| - sslContext.init(null, new javax.net.ssl.TrustManager[] { TRUST_ALL_CERTS }, new java.security.SecureRandom()); |
120 |
| - client = new OkHttpClient.Builder() |
121 |
| - .writeTimeout(60, TimeUnit.SECONDS) |
122 |
| - .readTimeout(60, TimeUnit.SECONDS) |
123 |
| - .connectTimeout(60, TimeUnit.SECONDS) |
124 |
| - .sslSocketFactory(sslContext.getSocketFactory(), (javax.net.ssl.X509TrustManager) TRUST_ALL_CERTS) |
125 |
| - .hostnameVerifier(new javax.net.ssl.HostnameVerifier() { |
126 |
| - @Override |
127 |
| - public boolean verify(String hostname, javax.net.ssl.SSLSession session) { |
128 |
| - return true; |
129 |
| - } |
130 |
| - }) |
131 |
| - .cookieJar(cookieJar) |
132 |
| - .build(); |
133 |
| - } catch (java.security.KeyManagementException e) { |
134 |
| - e.printStackTrace(); |
135 |
| - } catch (java.security.NoSuchAlgorithmException e) { |
136 |
| - e.printStackTrace(); |
137 |
| - } |
138 |
| - return; |
139 |
| - } |
140 |
| - |
141 | 120 | client = new OkHttpClient.Builder()
|
142 | 121 | .writeTimeout(60, TimeUnit.SECONDS)
|
143 | 122 | .readTimeout(60, TimeUnit.SECONDS)
|
@@ -230,10 +209,56 @@ public static void ClearCookies() {
|
230 | 209 | }
|
231 | 210 | }
|
232 | 211 |
|
233 |
| - public static void AllowSslErrors(boolean allow) { |
234 |
| - client = null; |
235 |
| - allowSslErrors = allow; |
| 212 | + public static void EnableSSLValidation() { |
| 213 | + InitClient(); |
| 214 | + |
| 215 | + try { |
| 216 | + TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); |
| 217 | + trustManagerFactory.init((KeyStore) null); |
| 218 | + TrustManager[] trustManagers = trustManagerFactory.getTrustManagers(); |
| 219 | + |
| 220 | + X509TrustManager trustManager = null; |
| 221 | + for (TrustManager tm : trustManagers) { |
| 222 | + if (tm instanceof X509TrustManager) { |
| 223 | + trustManager = (X509TrustManager) tm; |
| 224 | + break; |
| 225 | + } |
| 226 | + } |
| 227 | + if (trustManager != null) { |
| 228 | + SSLContext sslContext = SSLContext.getInstance("TLS"); |
| 229 | + sslContext.init(null, new TrustManager[] { trustManager }, new SecureRandom()); |
| 230 | + |
| 231 | + client = client.newBuilder() |
| 232 | + .sslSocketFactory(sslContext.getSocketFactory(), trustManager) |
| 233 | + .hostnameVerifier(OkHostnameVerifier.INSTANCE) |
| 234 | + .build(); |
| 235 | + } |
| 236 | + } catch (Exception e) { |
| 237 | + e.printStackTrace(); |
| 238 | + } |
| 239 | + } |
| 240 | + |
| 241 | + public static void DisableSSLValidation() { |
236 | 242 | InitClient();
|
| 243 | + |
| 244 | + try { |
| 245 | + SSLContext sslContext = SSLContext.getInstance("SSL"); |
| 246 | + sslContext.init(null, new TrustManager[] { TRUST_ALL_CERTS }, new SecureRandom()); |
| 247 | + |
| 248 | + client = client.newBuilder() |
| 249 | + .sslSocketFactory(sslContext.getSocketFactory(), (X509TrustManager) TRUST_ALL_CERTS) |
| 250 | + .hostnameVerifier(new javax.net.ssl.HostnameVerifier() { |
| 251 | + @Override |
| 252 | + public boolean verify(String hostname, javax.net.ssl.SSLSession session) { |
| 253 | + return true; |
| 254 | + } |
| 255 | + }) |
| 256 | + .build(); |
| 257 | + } catch (java.security.KeyManagementException e) { |
| 258 | + e.printStackTrace(); |
| 259 | + } catch (java.security.NoSuchAlgorithmException e) { |
| 260 | + e.printStackTrace(); |
| 261 | + } |
237 | 262 | }
|
238 | 263 |
|
239 | 264 | public static void SetImageParseMethod(ImageParseMethod newImageParseMethod) {
|
|
0 commit comments