use of javax.net.ssl.X509TrustManager in project nutz by nutzam.
the class Http method nopSSLSocketFactory.
public static SSLSocketFactory nopSSLSocketFactory() throws Exception {
SSLContext sc = SSLContext.getInstance("SSL");
TrustManager[] tmArr = { new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] paramArrayOfX509Certificate, String paramString) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] paramArrayOfX509Certificate, String paramString) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
} };
sc.init(null, tmArr, new SecureRandom());
return sc.getSocketFactory();
}
use of javax.net.ssl.X509TrustManager in project openhab1-addons by openhab.
the class AirConditioner method connect.
private void connect() throws Exception {
if (isConnected()) {
return;
} else {
logger.debug("Disconnected so we'll try again");
disconnect();
}
if (CERTIFICATE_FILE_NAME != null && new File(CERTIFICATE_FILE_NAME).isFile()) {
if (CERTIFICATE_PASSWORD == null) {
CERTIFICATE_PASSWORD = "";
}
try {
SSLClient client = new SSLClient();
client.addTrustMaterial(TrustMaterial.DEFAULT);
client.setCheckHostname(false);
client.setKeyMaterial(new KeyMaterial(CERTIFICATE_FILE_NAME, CERTIFICATE_PASSWORD.toCharArray()));
client.setConnectTimeout(10000);
socket = (SSLSocket) client.createSocket(IP, PORT);
socket.setSoTimeout(2000);
socket.startHandshake();
} catch (Exception e) {
throw new Exception("Could not connect using certificate: " + CERTIFICATE_FILE_NAME, e);
}
} else {
try {
SSLContext ctx = SSLContext.getInstance("TLS");
final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
}
} };
ctx.init(null, trustAllCerts, null);
socket = (SSLSocket) ctx.getSocketFactory().createSocket(IP, PORT);
socket.setSoTimeout(2000);
socket.startHandshake();
} catch (Exception e) {
throw new Exception("Cannot connect to " + IP + ":" + PORT, e);
}
}
handleResponse();
}
use of javax.net.ssl.X509TrustManager in project CloudReader by youlookwhat.
the class HttpUtils method getUnsafeOkHttpClient.
public OkHttpClient getUnsafeOkHttpClient() {
try {
final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[] {};
}
} };
// Install the all-trusting trust manager
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustAllCerts, new SecureRandom());
// Create an ssl socket factory with our all-trusting manager
SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
OkHttpClient.Builder okBuilder = new OkHttpClient.Builder();
okBuilder.readTimeout(20, TimeUnit.SECONDS);
okBuilder.connectTimeout(10, TimeUnit.SECONDS);
okBuilder.writeTimeout(20, TimeUnit.SECONDS);
okBuilder.addInterceptor(new HttpHeadInterceptor());
okBuilder.addInterceptor(getInterceptor());
okBuilder.sslSocketFactory(sslSocketFactory);
okBuilder.hostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
// Log.d("HttpUtils", "==come");
return true;
}
});
return okBuilder.build();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of javax.net.ssl.X509TrustManager in project Tusky by Vavassor.
the class OkHttpUtils method enableHigherTlsOnPreLollipop.
private static OkHttpClient.Builder enableHigherTlsOnPreLollipop(OkHttpClient.Builder builder) {
if (Build.VERSION.SDK_INT >= 16 && Build.VERSION.SDK_INT < 22) {
try {
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init((KeyStore) null);
TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
if (trustManagers.length != 1 || !(trustManagers[0] instanceof X509TrustManager)) {
throw new IllegalStateException("Unexpected default trust managers:" + Arrays.toString(trustManagers));
}
X509TrustManager trustManager = (X509TrustManager) trustManagers[0];
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, new TrustManager[] { trustManager }, null);
SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
builder.sslSocketFactory(new SSLSocketFactoryCompat(sslSocketFactory), trustManager);
} catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e) {
Log.e(TAG, "Failed enabling TLS 1.1 & 1.2. " + e.getMessage());
}
}
return builder;
}
use of javax.net.ssl.X509TrustManager in project ride-read-android by Ride-Read.
the class RetrofitUtils method configClient.
private static OkHttpClient configClient() {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.connectTimeout(TIMEOUT_SECONDS, TimeUnit.SECONDS).readTimeout(TIMEOUT_SECONDS, TimeUnit.SECONDS).writeTimeout(TIMEOUT_SECONDS, TimeUnit.SECONDS);
// .retryOnConnectionFailure(true);
try {
final X509TrustManager tm = new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
@Override
public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
// Not implemented
}
@Override
public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
// Not implemented
}
};
final SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, new TrustManager[] { tm }, new java.security.SecureRandom());
builder.sslSocketFactory(sc.getSocketFactory(), tm).hostnameVerifier((hostname, session) -> hostname.contains(".195."));
if (BuildConfig.DEBUG) {
// Log信息拦截器
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
//设置 Debug Log 模式
builder.addInterceptor(loggingInterceptor);
}
} catch (Exception e) {
e.printStackTrace();
}
return builder.build();
}
Aggregations