Time to time TLS version changed in client application for same request and due to that SSLHandshakeException occurre - java-7

My client application running on tomcat server with java 1.7.0_79 version. It will get data from some third party server applications. But time to time server connection failed due to following exception.
javax.net.ssl.SSLHandshakeException: Remote host closed connection
during handshake at
sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:953) at
sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1332)
Most of the server applications supports TLSv1.2 and due to that my client application code is like this. When analyzed the packet identified that TLSv1.1 going for the failed connections from client side. Is there any possibility to get different TLS version other than TLSv1.2 with below code? Also this code execute by multiple threads in client application. Please advice.
TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
...
}};
SSLContext sc = SSLContext.getInstance("TLSv1.2");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
SSLContext.setDefault(sc);
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
URL mUrl = new URL(url);
URLConnection ucon = mUrl.openConnection();
HttpURLConnection con = (HttpURLConnection) ucon;
...
responseBody = con.getInputStream();

Use -Djavax.net.debug=ssl:all to get detail SSL connection log. It will help in getting more detail about error.

Related

How can I trust a self-signed certificate in an Oracle Wallet from .NET 5/6?

I have a console application that is trying to connect to a remote Oracle database using a wallet for authentication. Here is the code for the connection.
OracleConfiguration.WalletLocation = #"(SOURCE=(METHOD=FILE)(METHOD_DATA=(DIRECTORY=c:\Oracle\wallets\test)))";
OracleConfiguration.SqlNetWalletOverride = true;
OracleConfiguration.TnsAdmin = #"C:\Oracle";
OracleConfiguration.TraceLevel = 7;
OracleConfiguration.TraceFileLocation = #"C:\Oracle\trace";
OracleConfiguration.TraceOption = 7;
using (var dbConn = new OracleConnection(connectionString))
{
dbConn.Open();
Console.WriteLine("SUCCESS!");
dbConn.Close();
}
The certificate in the wallet is self-signed by the other party I am trying to connect to. The connection fails with the following error:
Oracle.ManagedDataAccess.Client.OracleException (0x80004005): ORA-00542: Failure during SSL handshake
---> OracleInternal.Network.NetworkException (0x80004005): ORA-00542: Failure during SSL handshake
---> System.Security.Authentication.AuthenticationException: The remote certificate was rejected by the provided RemoteCertificateValidationCallback.
In other .NET applications, I have been able to assign a callback to handle server certificate validation errors. Is there a way to do so in the context of the Oracle connection?

ActiveMQ Connection is not working

I created the ActiveMQ Connection with ActiveMQConnectionFactory,
connectionFactory = new ActiveMQConnectionFactory("nio://0.0.0.0:" + activeMqPort);
connection = connectionFactory.createConnection();
connection.start();
Later some point of time I am using the connection to get the session:
Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
While creating session from Connection it will fail with a NullPointerException (connection is null),
PS: Connection was working before, suddenly its null.
Can anyone explain it to me when this will happen and how to fix this ?
Your URI is incorrect for a client-side connection. nio:// is only relevant on the server-side, and specifying "0.0.0.0" is used by the server-side to instruct the server to listen on all ip interfaces.
Try this instead to connect to an ActiveMQ server running on the same computer as this client code:
connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:" + activeMqPort);
connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
Bonus recommendations:
Externalize the entire client url, not just the port. This will allow you to specify multiple servers for failover, reconnect timeout parameters and other client connectivity settings that would otherwise require a code change.
Do not use CLIENT_ACKNOWLEDGE unless you really know what it does. Its is a source of a lot of pain and suffering if you don't clearly have a handle on how it works. If you want per-message Acknowledgement, use ActiveMQSession.INDIVIDUAL_ACKNOWLEDGE. For non-transacted, use Session.AUTO_ACKNOWLEDGE.

Can't connect to ibm mq using jms

I had a problem with connecting to the IBM MQ queue from Java. I tried to change the passwords for the service of the IBM MQ, create a connection without specifying a login and password, but nothing happens. I wrote the code:
Properties props = new Properties();
props.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
props.setProperty(Context.PROVIDER_URL, "file:/D:/JNDI/");
try {
InitialContext initialContext = new InitialContext(props);
ConnectionFactory connectionFactory = (ConnectionFactory) initialContext.lookup(connectionFactoryName);
Destination destination = (Destination) initialContext.lookup(queueName);
initialContext.close();
Connection queueConnection = connectionFactory.createConnection("login", "password");// .createConnection();
which falls when I create a connection. I get an error:
JMSWMQ2013: Invalid credentials were passed to the queue manager QueueManager 'QM_LOCAL' in the 'Client' connection mode using the host 'localhost (1414)'.
Verify that the provided user name and password are correct in the queue administrator that you are connecting to.
can I turn off the authentication so that the method ".createConnection();" works? If not, where do I set the password? I Use the Windows.
Thanks.
p.s.: I get the completion code '2' ('MQCC_FAILED'), reason'2035' ('MQRC_NOT_AUTHORIZED').

Unity TCP socket connection not working on Mac OS X

I'm trying to publish my Unity multiplayer game to Mac App Store, but by some reason in production the socket fails to connect to server, throwing the following exception:
"Access denied System.Net.Sockets.Socket.Bind".
I have "com.apple.security.network.client" and "com.apple.security.app-sandbox" entitlement, but it does not work. HTTP requests work OK though.
If I test the app in debug build - it works..what could be blocking it ? The port I'm using is 16005
This is the client connection code:
var tcpClient = new TcpClient();
tcpClient.Connect(Host, Port);
networkStream = tcpClient.GetStream();
listenThread = new Thread(ListenToServer);
listenThread.Start();
Any ideas ?
I Solved the problem.
It turns out I had to add the server entitlement too.
(Although I'm not starting any server nor listening for incoming connections).
So adding "com.apple.security.network.server" entitlement solved the problem

Genymotion androvm web service error Cannot connect java.net.ConnectException

I developed a simple application, calls currency converter webservice from www.webservicex.net. and deployed it on GenyMotion AndroVM.
But I am getting below error,
"Cannot connect to www.webservicex.net on port 80:
java.net.ConnectException: Connection timed out"
We have proxy and I defined proxy settings as well. I am able to access internet using browser inside AndroVM.
Please help
In Android, the system's proxy settings is not applied to all Http requests you made inside your app. It is applied natively inside the browser only that's the reason why you can use it.
Each application has to handle it "manually".
My first and quick advise is to use OkHttp as Http client because it handles it for you.
Or you can get the values manually and configure yourself your requests like this (gathered from here):
String host = System.getProperty("http.proxyHost");
String port = System.getProperty("http.proxyPort");
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpHost proxy = new HttpHost(host, Integer.parseInt(port));
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

Resources