use of java.security.KeyManagementException in project cloudstack by apache.
the class RawHTTP method _getSocket.
private Socket _getSocket() throws IOException {
if (useSSL) {
SSLContext context = null;
try {
context = SSLUtils.getSSLContext("SunJSSE");
} catch (NoSuchAlgorithmException e) {
s_logger.error("Unexpected exception ", e);
} catch (NoSuchProviderException e) {
s_logger.error("Unexpected exception ", e);
}
if (context == null)
throw new IOException("Unable to setup SSL context");
SSLSocket ssl = null;
try {
context.init(null, trustAllCerts, new SecureRandom());
SocketFactory factory = new SecureSSLSocketFactory(context);
ssl = (SSLSocket) factory.createSocket(host, port);
ssl.setEnabledProtocols(SSLUtils.getSupportedProtocols(ssl.getEnabledProtocols()));
/* ssl.setSSLParameters(context.getDefaultSSLParameters()); */
} catch (IOException e) {
s_logger.error("IOException: " + e.getMessage(), e);
throw e;
} catch (KeyManagementException e) {
s_logger.error("KeyManagementException: " + e.getMessage(), e);
} catch (NoSuchAlgorithmException e) {
s_logger.error("NoSuchAlgorithmException: " + e.getMessage(), e);
}
return ssl;
} else {
return new Socket(host, port);
}
}
use of java.security.KeyManagementException in project opennms by OpenNMS.
the class AsyncBasicDetectorMinaImpl method isServiceDetected.
/**
* {@inheritDoc}
*/
@Override
public final DetectFuture isServiceDetected(final InetAddress address) {
final DetectFutureMinaImpl detectFuture = new DetectFutureMinaImpl(this);
try {
// Set this up here because it can throw an Exception, which we want
// to throw now, not in initializeSession
final SSLContext c = createClientSSLContext();
// Create an IoSessionInitializer that will configure this individual
// session. Previously, all this was done on a new Connector each time
// but that was leaking file handles all over the place. This way gives
// us per-connection settings without the overhead of creating new
// Connectors each time
IoSessionInitializer<ConnectFuture> init = new IoSessionInitializer<ConnectFuture>() {
@Override
public void initializeSession(IoSession session, ConnectFuture future) {
// Add filters to the session
if (isUseSSLFilter()) {
final SslFilter filter = new SslFilter(c);
filter.setUseClientMode(true);
session.getFilterChain().addFirst("SSL", filter);
}
session.getFilterChain().addLast("logger", getLoggingFilter() != null ? getLoggingFilter() : new SlightlyMoreVerboseLoggingFilter());
session.getFilterChain().addLast("codec", getProtocolCodecFilter());
// Make the minimum idle timeout 1 second
int idleTimeInSeconds = Math.max(1, Math.round(getIdleTime() / 1000.0f));
// Set all of the idle time limits. Make sure to specify values in
// seconds!!!
session.getConfig().setReaderIdleTime(idleTimeInSeconds);
session.getConfig().setWriterIdleTime(idleTimeInSeconds);
session.getConfig().setBothIdleTime(idleTimeInSeconds);
}
};
// Start communication
final InetSocketAddress socketAddress = new InetSocketAddress(address, getPort());
final ConnectFuture cf = m_connectionFactory.connect(socketAddress, init, createDetectorHandler(detectFuture));
cf.addListener(retryAttemptListener(detectFuture, socketAddress, init, getRetries()));
} catch (KeyManagementException e) {
detectFuture.setException(e);
} catch (NoSuchAlgorithmException e) {
detectFuture.setException(e);
} catch (Throwable e) {
detectFuture.setException(e);
}
return detectFuture;
}
use of java.security.KeyManagementException in project android_frameworks_base by AOSPA.
the class SSLCertificateSocketFactory method makeSocketFactory.
private SSLSocketFactory makeSocketFactory(KeyManager[] keyManagers, TrustManager[] trustManagers) {
try {
OpenSSLContextImpl sslContext = OpenSSLContextImpl.getPreferred();
sslContext.engineInit(keyManagers, trustManagers, null);
sslContext.engineGetClientSessionContext().setPersistentCache(mSessionCache);
return sslContext.engineGetSocketFactory();
} catch (KeyManagementException e) {
Log.wtf(TAG, e);
// Fallback
return (SSLSocketFactory) SSLSocketFactory.getDefault();
}
}
use of java.security.KeyManagementException in project K6nele by Kaljurand.
the class WebSocketRecognitionService method startSocket.
/**
* Opens the socket and starts recording/sending.
*
* @param url Webservice URL
*/
void startSocket(String url) {
mIsEosSent = false;
Log.i(url);
AsyncHttpClient client = AsyncHttpClient.getDefaultInstance();
if (false) {
// http://stackoverflow.com/questions/37804816/androidasync-how-to-create-ssl-client-in-websocket-connection
AsyncSSLSocketMiddleware sslSocketMiddleware = new AsyncSSLSocketMiddleware(client);
SSLContext sslContext = null;
try {
sslContext = getSSLContext();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
sslSocketMiddleware.setSSLContext(sslContext);
client.insertMiddleware(sslSocketMiddleware);
}
client.websocket(url, PROTOCOL, new AsyncHttpClient.WebSocketConnectCallback() {
@Override
public void onCompleted(Exception ex, final WebSocket webSocket) {
mWebSocket = webSocket;
if (ex != null) {
handleException(ex);
return;
}
webSocket.setStringCallback(new WebSocket.StringCallback() {
public void onStringAvailable(String s) {
Log.i(s);
handleResult(s);
}
});
webSocket.setClosedCallback(new CompletedCallback() {
@Override
public void onCompleted(Exception ex) {
if (ex == null) {
Log.e("ClosedCallback");
handleFinish(mIsEosSent);
} else {
Log.e("ClosedCallback: ", ex);
handleException(ex);
}
}
});
webSocket.setEndCallback(new CompletedCallback() {
@Override
public void onCompleted(Exception ex) {
if (ex == null) {
Log.e("EndCallback");
handleFinish(mIsEosSent);
} else {
Log.e("EndCallback: ", ex);
handleException(ex);
}
}
});
startSending(webSocket);
}
});
}
use of java.security.KeyManagementException in project J2ME-Loader by nikita36078.
the class Connection method openConnection.
@Override
public javax.microedition.io.Connection openConnection(String name, int mode, boolean timeouts) throws IOException {
if (!org.microemu.cldc.http.Connection.isAllowNetworkConnection()) {
throw new IOException("No network");
}
int portSepIndex = name.lastIndexOf(':');
int port = Integer.parseInt(name.substring(portSepIndex + 1));
String host = name.substring("ssl://".length(), portSepIndex);
// TODO validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
@Override
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
} };
try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new SecureRandom());
SSLSocketFactory factory = sc.getSocketFactory();
socket = factory.createSocket(host, port);
} catch (NoSuchAlgorithmException ex) {
throw new IOException(ex.toString());
} catch (KeyManagementException ex) {
throw new IOException(ex.toString());
}
return this;
}
Aggregations