use of java.security.KeyManagementException in project xabber-android by redsolution.
the class ConnectionThread method onReady.
private void onReady(XMPPTCPConnectionConfiguration.Builder builder) {
builder.setSecurityMode(tlsMode.getSecurityMode());
builder.setCompressionEnabled(compression);
builder.setSendPresence(false);
try {
if (SettingsManager.securityCheckCertificate()) {
SSLContext sslContext = SSLContext.getInstance("TLS");
MemorizingTrustManager mtm = new MemorizingTrustManager(Application.getInstance());
sslContext.init(null, new X509TrustManager[] { mtm }, new java.security.SecureRandom());
builder.setCustomSSLContext(sslContext);
builder.setHostnameVerifier(mtm.wrapHostnameVerifier(new org.apache.http.conn.ssl.StrictHostnameVerifier()));
} else {
TLSUtils.acceptAllCertificates(builder);
TLSUtils.disableHostnameVerificationForTlsCertificicates(builder);
}
} catch (NoSuchAlgorithmException | KeyManagementException e) {
e.printStackTrace();
}
setUpSASL();
xmppConnection = new XMPPTCPConnection(builder.build());
xmppConnection.addAsyncStanzaListener(this, ACCEPT_ALL);
xmppConnection.addConnectionListener(this);
// by default Smack disconnects in case of parsing errors
xmppConnection.setParsingExceptionCallback(new ExceptionLoggingCallback());
AccountRosterListener rosterListener = new AccountRosterListener(((AccountItem) connectionItem).getAccount());
final Roster roster = Roster.getInstanceFor(xmppConnection);
roster.addRosterListener(rosterListener);
roster.addRosterLoadedListener(rosterListener);
roster.setSubscriptionMode(Roster.SubscriptionMode.manual);
org.jivesoftware.smackx.ping.PingManager.getInstanceFor(xmppConnection).registerPingFailedListener(this);
connectionItem.onSRVResolved(this);
final String password = OAuthManager.getInstance().getPassword(protocol, token);
if (password != null) {
runOnConnectionThread(new Runnable() {
@Override
public void run() {
connect(password);
}
});
} else {
runOnConnectionThread(new Runnable() {
@Override
public void run() {
passwordRequest();
}
});
}
}
use of java.security.KeyManagementException in project keywhiz by square.
the class ClientUtils method sslOkHttpClient.
/**
* Creates a {@link OkHttpClient} to start a TLS connection.
*
* @param devTrustStore if not null, uses the provided TrustStore instead of whatever is
* configured in the JVM. This is a convenient way to allow developers to
* start playing with Keywhiz right away. This option should not be used in
* production systems.
* @param cookies list of cookies to include in the client.
* @return new http client.
*/
public static OkHttpClient sslOkHttpClient(@Nullable KeyStore devTrustStore, List<HttpCookie> cookies) {
checkNotNull(cookies);
SSLContext sslContext;
try {
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(devTrustStore);
TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
sslContext = SSLContext.getInstance("TLSv1.2");
sslContext.init(new KeyManager[0], trustManagers, new SecureRandom());
} catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException e) {
throw Throwables.propagate(e);
}
SSLSocketFactory socketFactory = sslContext.getSocketFactory();
OkHttpClient.Builder client = new OkHttpClient().newBuilder().sslSocketFactory(socketFactory).connectionSpecs(Arrays.asList(ConnectionSpec.MODERN_TLS)).followSslRedirects(false);
client.retryOnConnectionFailure(false);
client.networkInterceptors().add(new XsrfTokenInterceptor("XSRF-TOKEN", "X-XSRF-TOKEN"));
cookies.forEach(c -> getCookieManager().getCookieStore().add(null, c));
client.cookieJar(new JavaNetCookieJar(getCookieManager()));
return client.build();
}
use of java.security.KeyManagementException in project QuickAndroid by ImKarl.
the class HttpsHelper method setCertificates.
public static void setCertificates(OkHttpClient client, InputStream[] certificates, InputStream bksFile, String password) {
if (client == null) {
return;
}
try {
TrustManager[] trustManagers = prepareTrustManager(certificates);
KeyManager[] keyManagers = prepareKeyManager(bksFile, password);
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(keyManagers, new TrustManager[] { new MyTrustManager(chooseTrustManager(trustManagers)) }, new SecureRandom());
client.setSslSocketFactory(sslContext.getSocketFactory());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (KeyStoreException e) {
e.printStackTrace();
}
}
use of java.security.KeyManagementException in project jodd by oblac.
the class SocketHttpConnectionProvider method getDefaultSSLSocketFactory.
/**
* Returns default SSL socket factory allowing setting trust managers.
*/
protected SSLSocketFactory getDefaultSSLSocketFactory(boolean trustAllCertificates) throws IOException {
if (trustAllCertificates) {
try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, TrustManagers.TRUST_ALL_CERTS, new java.security.SecureRandom());
return sc.getSocketFactory();
} catch (NoSuchAlgorithmException | KeyManagementException e) {
throw new IOException(e);
}
} else {
return (SSLSocketFactory) SSLSocketFactory.getDefault();
}
}
use of java.security.KeyManagementException in project openhab1-addons by openhab.
the class Util method getConnection.
public static Sardine getConnection(CalDavConfig config) {
if (config.isDisableCertificateVerification()) {
if (config.getUrl().startsWith(HTTP_URL_PREFIX)) {
log.error("do not use '{}' if no ssl is used", CalDavLoaderImpl.PROP_DISABLE_CERTIFICATE_VERIFICATION);
}
log.trace("connecting to caldav '{}' with disabled certificate verification (url={}, username={}, password={})", config.getKey(), config.getUrl(), config.getUsername(), config.getPassword());
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create().setHostnameVerifier(new AllowAllHostnameVerifier());
try {
httpClientBuilder.setSslcontext(new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
return true;
}
}).build());
} catch (KeyManagementException e) {
log.error("error verifying certificate", e);
} catch (NoSuchAlgorithmException e) {
log.error("error verifying certificate", e);
} catch (KeyStoreException e) {
log.error("error verifying certificate", e);
}
if (StringUtils.isEmpty(config.getUsername()) && StringUtils.isEmpty(config.getPassword())) {
log.trace("connecting without credentials for '{}'", config.getKey());
return new SardineImpl(httpClientBuilder);
} else {
return new SardineImpl(httpClientBuilder, config.getUsername(), config.getPassword());
}
} else {
log.trace("connecting to caldav '{}' (url={}, username={}, password={})", config.getKey(), config.getUrl(), config.getUsername(), config.getPassword());
if (StringUtils.isEmpty(config.getUsername()) && StringUtils.isEmpty(config.getPassword())) {
log.trace("connecting without credentials for '{}'", config.getKey());
return new SardineImpl();
} else {
return new SardineImpl(config.getUsername(), config.getPassword());
}
}
}
Aggregations