use of javax.net.ssl.TrustManager 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.TrustManager in project opennms by OpenNMS.
the class VmwareConfigBuilder method main.
public static void main(String[] args) throws ParseException {
String hostname = null;
String username = null;
String password = null;
String rrdRepository = null;
final Options options = new Options();
options.addOption("rrdRepository", true, "set rrdRepository path for generated config files, default: '/opt/opennms/share/rrd/snmp/'");
final CommandLineParser parser = new PosixParser();
final CommandLine cmd = parser.parse(options, args);
@SuppressWarnings("unchecked") List<String> arguments = (List<String>) cmd.getArgList();
if (arguments.size() < 3) {
usage(options, cmd);
System.exit(1);
}
hostname = arguments.remove(0);
username = arguments.remove(0);
password = arguments.remove(0);
if (cmd.hasOption("rrdRepository")) {
rrdRepository = cmd.getOptionValue("rrdRepository");
} else {
rrdRepository = "/opt/opennms/share/rrd/snmp/";
}
TrustManager[] trustAllCerts = new TrustManager[] { new AnyServerX509TrustManager() };
SSLContext sc = null;
try {
sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, null);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
HostnameVerifier hv = new HostnameVerifier() {
@Override
public boolean verify(String urlHostName, SSLSession session) {
return true;
}
};
HttpsURLConnection.setDefaultHostnameVerifier(hv);
VmwareConfigBuilder vmwareConfigBuilder;
vmwareConfigBuilder = new VmwareConfigBuilder(hostname, username, password);
try {
vmwareConfigBuilder.generateData(rrdRepository);
} catch (Exception e) {
e.printStackTrace();
}
}
use of javax.net.ssl.TrustManager 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.TrustManager in project voltdb by VoltDB.
the class SSLConfiguration method createSslContext.
public static SSLContext createSslContext(SslConfig sslConfig) {
if (sslConfig == null) {
throw new IllegalArgumentException("sslConfig is null");
}
KeyManager[] keyManagers = null;
TrustManager[] trustManagers = null;
try {
SSLContext sslContext = SSLContext.getInstance("TLS");
if (sslConfig.keyStorePath != null && sslConfig.keyStorePassword != null) {
keyManagers = createKeyManagers(sslConfig.keyStorePath, sslConfig.keyStorePassword, sslConfig.keyStorePassword);
}
if (sslConfig.trustStorePath != null && sslConfig.trustStorePassword != null) {
trustManagers = createTrustManagers(sslConfig.trustStorePath, sslConfig.trustStorePassword);
}
sslContext.init(keyManagers, trustManagers, new SecureRandom());
return sslContext;
} catch (IOException | NoSuchAlgorithmException | KeyStoreException | CertificateException | UnrecoverableKeyException | KeyManagementException ex) {
throw new IllegalArgumentException("Failed to initialize SSL using " + sslConfig, ex);
}
}
use of javax.net.ssl.TrustManager in project java-chassis by ServiceComb.
the class SSLManager method createSSLContext.
public static SSLContext createSSLContext(SSLOption option, SSLCustom custom) {
try {
String keyStoreName = custom.getFullPath(option.getKeyStore());
KeyManager[] keymanager;
if (keyStoreName != null && new File(keyStoreName).exists()) {
char[] keyStoreValue = custom.decode(option.getKeyStoreValue().toCharArray());
KeyStore keyStore = KeyStoreUtil.createKeyStore(keyStoreName, option.getKeyStoreType(), keyStoreValue);
keymanager = KeyStoreUtil.createKeyManagers(keyStore, keyStoreValue);
} else {
keymanager = null;
}
String trustStoreName = custom.getFullPath(option.getTrustStore());
TrustManager[] trustManager;
if (trustStoreName != null && new File(trustStoreName).exists()) {
char[] trustStoreValue = custom.decode(option.getTrustStoreValue().toCharArray());
KeyStore trustStore = KeyStoreUtil.createKeyStore(trustStoreName, option.getTrustStoreType(), trustStoreValue);
trustManager = KeyStoreUtil.createTrustManagers(trustStore);
} else {
trustManager = new TrustManager[] { new TrustAllManager() };
}
TrustManager[] wrapped = new TrustManager[trustManager.length];
for (int i = 0; i < trustManager.length; i++) {
wrapped[i] = new TrustManagerExt((X509ExtendedTrustManager) trustManager[i], option, custom);
}
// ?: ssl context version
SSLContext context = SSLContext.getInstance("TLS");
context.init(keymanager, wrapped, new SecureRandom());
return context;
} catch (NoSuchAlgorithmException e) {
throw new IllegalArgumentException("NoSuchAlgorithmException." + e.getMessage());
} catch (KeyManagementException e) {
throw new IllegalArgumentException("KeyManagementException." + e.getMessage());
}
}
Aggregations