SQL Server JDBC Error on Java 8: The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption

JavaSql ServerLinuxSslJdbc

Java Problem Overview


I am getting the following error when connecting to a SQL Server database using version the Microsoft JDBC Driver:

> com.microsoft.sqlserver.jdbc.SQLServerException: The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption. Error: "SQL Server returned an incomplete response. The connection has been closed. ClientConnectionId:98d0b6f4-f3ca-4683-939e-7c0a0fca5931".

We recently upgraded our applications from Java 6 & Java 7 to Java 8. All systems running Java are running SUSE Linux Enterprise Server 11 (x86_64), VERSION = 11, PATCHLEVEL = 3.

Here are the facts I have collected with a Java program that I wrote which simply sequentially opens and closes 1,000 database connections.

  • Connections are dropped with this error about 5%-10% of the time. The error DOES NOT occur on every connection.
  • The problem ONLY occurs with Java 8. I ran the same program on Java 7 and the problem is not reproducible. This is consistent with our experience in production prior to upgrading. We've had zero problems running under Java 7 in production.
  • The problem DOES NOT occur on all of our Linux servers running Java 8, it only occurs on some of them. This is perplexing to me, but when I run the same test program on the same version of the Linux JVM (1.8.0_60, 64 bit) on different Linux instances, the problem does not occur on one of the Linux instances, but the problem does occur on others. The Linux instances are running the same version of SUSE and they are at the same patch level.
  • The problem occurs when connecting to BOTH SQL Server 2008 and SQL Server 2014 servers/databases.
  • The problem occurs regardless if I am using the 4.0 version of the SQL Server JDBC driver or the newer 4.1 version of the driver.

The thing that makes my observations unique on this compared to others on the web is that although the problem happens ONLY on Java 8, I cannot get the problem to occur on one of the seemingly identical Linux servers that is running the same Java 8 JVM. Other folks have seen this problem on earlier versions of Java as well, but that has not been our experience.

Any input, suggestions, or observations you may have are appreciated.

Java Solutions


Solution 1 - Java

Your url should be like below and add sql sqljdbc42.jar. This will resolve your issue

url = "jdbc:sqlserver://" +serverName + ":1433;DatabaseName=" + dbName + ";encrypt=true;trustServerCertificate=true;

Solution 2 - Java

I turned on SSL logging in the Java 8 JVM on a Linux instance which reproduces the problem. SSL logging is turned on using -Djavax.net.debug=ssl:handshake:verbose. This revealed some useful information.

The workaround that we are using in production and has proven to work for us is to set this parameter on the JVM:

 -Djdk.tls.client.protocols=TLSv1

If you want more details, please read on.

On a server where the problem can be reproduced (again, only 5-10% of the time), I observed the following:

*** ClientHello, TLSv1.2
--- 8<-- SNIP -----
main, WRITE: TLSv1.2 Handshake, length = 195
main, READ: TLSv1.2 Handshake, length = 1130
*** ServerHello, TLSv1.2
--- 8<-- SNIP -----
%% Initialized:  [Session-79, TLS_DHE_RSA_WITH_AES_128_GCM_SHA256]
** TLS_DHE_RSA_WITH_AES_128_GCM_SHA256
--- 8<-- SNIP -----
Algorithm: [SHA1withRSA]
--- 8<-- SNIP -----
*** Diffie-Hellman ServerKeyExchange
--- 8<-- SNIP -----
*** ServerHelloDone
*** ClientKeyExchange, DH
--- 8<-- SNIP -----
main, WRITE: TLSv1.2 Handshake, length = 133
--- 8<-- SNIP -----
main, WRITE: TLSv1.2 Change Cipher Spec, length = 1
*** Finished
verify_data:  { 108, 116, 29, 115, 13, 26, 154, 198, 17, 125, 114, 166 }
***
main, WRITE: TLSv1.2 Handshake, length = 40
main, called close()
main, called closeInternal(true)
main, SEND TLSv1.2 ALERT:  warning, description = close_notify
main, WRITE: TLSv1.2 Alert, length = 26
main, called closeSocket(true)
main, waiting for close_notify or alert: state 5
main, received EOFException: ignored
main, called closeInternal(false)
main, close invoked again; state = 5
main, handling exception: java.io.IOException: SQL Server returned an incomplete response. The connection has been closed. ClientConnectionId:12a722b3-d61d-4ce4-8319-af049a0a4415

Notice that TLSv1.2 is selected by the database server and used in this exchange. I've observed that, when connections fail from the problematic linux service, TLSv1.2 is ALWAYS the level which was selected. However, connections do not ALWAYS fail when TLSv1.2 is used. They only fail 5-10% of the time.

Now here is an exchange from a server that does NOT have the problem. Everything else is equal. I.e., connecting to the same database, same version of the JVM (Java 1.8.0_60), same JDBC driver, etc. Notice that, here, TLSv1 is selected by the database server instead of TLSv1.2 as in the faulty server's case.

*** ClientHello, TLSv1.2
--- 8<-- SNIP -----
main, WRITE: TLSv1.2 Handshake, length = 207
main, READ: TLSv1 Handshake, length = 604
*** ServerHello, TLSv1
--- 8<-- SNIP -----
Cipher Suite: TLS_RSA_WITH_AES_128_CBC_SHA
--- 8<-- SNIP -----
%% Initialized:  [Session-79, TLS_RSA_WITH_AES_128_CBC_SHA]
** TLS_RSA_WITH_AES_128_CBC_SHA
--- 8<-- SNIP -----
Algorithm: [SHA1withRSA]
--- 8<-- SNIP -----
***
*** ServerHelloDone
*** ClientKeyExchange, RSA PreMasterSecret, TLSv1
--- 8<-- SNIP -----
main, WRITE: TLSv1 Handshake, length = 134
main, WRITE: TLSv1 Change Cipher Spec, length = 1
*** Finished
verify_data:  { 26, 155, 166, 89, 229, 193, 126, 39, 103, 206, 126, 21 }
***
main, WRITE: TLSv1 Handshake, length = 48
main, READ: TLSv1 Change Cipher Spec, length = 1
main, READ: TLSv1 Handshake, length = 48
*** Finished

So, when TLSv1 is negotiated between the Linux JVM and the SQL Server, connections are ALWAYS successful. When TLSv1.2 is negotiated, we get sporadic connection failures.

(Note: Java 7 (1.7.0_51) always negotiates TLSv1, which is why the problem never occurred for us with a Java 7 JVM.)

The open questions we still have are:

  1. WHY is that the same Java 8 JVM run from 2 different Linux servers will always negotiate TLSv1, but when connecting from another Linux server it always negotiates TLSv1.2.
  2. And also why are TLSv1.2 negotiated connections successful most, but not all, of the time on that server?

Update 6/10/2017: This posting from Microsoft describes the problem and their proposed solution.

Resources:

http://www.infoworld.com/article/2849292/operating-systems/more-patch-problems-reported-with-the-ms14-066-kb-2992611-winshock-mess.html

http://www.infoworld.com/article/2849292/operating-systems/more-patch-problems-reported-with-the-ms14-066-kb-2992611-winshock-mess.html

http://blogs.msdn.com/b/jdbcteam/archive/2008/09/09/the-driver-could-not-establish-a-secure-connection-to-sql-server-by-using-secure-sockets-layer-ssl-encryption.aspx

https://stackoverflow.com/questions/30792156/java-8-jce-unlimited-strength-policy-and-ssl-handshake-over-tls

http://blogs.msdn.com/b/saponsqlserver/archive/2013/05/10/analyzing-jdbc-connection-issues.aspx

https://docs.oracle.com/javase/8/docs/technotes/guides/security/jsse/JSSERefGuide.html#descPhase2

https://blogs.oracle.com/java-platform-group/entry/java_8_will_use_tls

Solution 3 - Java

This appears to have been fixed in version 4.2 of the MS SQL JDBC driver. I created a program where I connected to the server 1000 times, pausing 100ms between each attempt. With version 4.1 I was able to reproduce the problem every time, although it happened only sporadically. With version 4.2 I was unable to reproduce the problem.

Solution 4 - Java

Before you upgrade SQL JDBC Driver, check the compatibility first:

  • Sqljdbc.jar requires a JRE of 5 and supports the JDBC 3.0 API
  • Sqljdbc4.jar requires a JRE of 6 and supports the JDBC 4.0 API
  • Sqljdbc41.jar requires a JRE of 7 and supports the JDBC 4.1 API
  • Sqljdbc42.jar requires a JRE of 8 and supports the JDBC 4.2 API

Source: https://www.microsoft.com/en-us/download/details.aspx?id=11774

Solution 5 - Java

In my case i had a sql server using the 3DES_EDE_CBC algorithm, this is disabled by default on jdk 1.8 , so checking the

> /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home/jre/lib/security/java.security

And eliminating the algorithm from:

> jdk.tls.disabledAlgorithms=SSLv3, RC4, DES, MD5withRSA, DH keySize < > 1024,
> EC keySize < 224, 3DES_EDE_CBC, anon, NULL

Worked for me.

Solution 6 - Java

Like @2Aguy wrote, you can change the JVM parameter. In my case I couldn't change it, and used the connection string "sslProtocol" parameter, lowering the connection to TLSV1.

> Connection String: jdbc:sqlserver://<HOST>:<PORT>;encrypt=true;trustServerCertificate=true;sslProtocol=TLSv1;database=<DB NAME>

Solution 7 - Java

Microsoft Recently open sourced their driver. One can see mssql-jdbc driver activity on GitHub. I guess latest preview version is 6.1.5.

Also you can find all preview versions on maven too. Supporting both JDK7 & JDK 8.

Solution 8 - Java

I've also hit this issue on Windows Server 2012 R2, using JDBC driver 4.0 & 4.1 with Java 7. This Microsoft article pins the blame on DHE ciphersuites, and recommends disabling them or reducing their priority if you cannot upgrade to JDBC driver 4.2

Solution 9 - Java

In my case, the issue was because the app was set to use spring-boot-ext-security-starter-credhub-credential and there were some issues with that setup.

So I removed credhub from the manifest file and pom and fetched credentials in a different way; then the error was gone.

Solution 10 - Java

If the server name in the connection string does not match the server name in the SQL Server SSL certificate, the following error will be issued: The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption. Error: "java.security.cert.CertificateException: Failed to validate the server name in a certificate during Secure Sockets Layer (SSL) initialization."

This helped me resolve the issue. Was using the localhost in servername, finally changing in jdbc connection string to the same name as the CN was able to connect.

Refer: https://wiki.deepnetsecurity.com/pages/viewpage.action?pageId=1410867 for more info

Solution 11 - Java

Thanks to @Sunil Kumar and @Joce. I used the jar and below syntax:

String myDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";

String myURL = "jdbc:sqlserver://DB_ipaddress\DB_instance;databaseName=DB_name;user=myusername; password=mypass;encrypt=true;trustServerCertificate=true;"; /==> here the semicolon will be twice like shown/

Connection con = DriverManager.getConnection(myURL);

Solution 12 - Java

I had this issue recently after doing a yum update in a client's server running RedHat 7. Since the above thread did not help me resolve my issue, I am posting this answer.

Issue:- Yum update in RedHat automatically reinstalls OpenJDK , my applications use oracle JDK. Verifying Default JDK: > java version Switch the default version:

update-alternatives --config java

There are 2 programs which provide 'java'.

Selection Command

  • 1 java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.302.b08-0.el7_9.x86_64/jre/bin/java)
  • 2 /usr/java/jdk1.8.0_181-amd64/jre/bin/java

Enter to keep the current selection[+], or type selection number:

Enter the number alongside the appropriate version you want to us and hit enter.

Solution 13 - Java

I have fixed the issue on my local enviroment, it mainly contains two steps below.

  1. Switch the JDBC Driver dependency like below if your project is built by maven:

<dependency>
  <groupId>net.sourceforge.jtds</groupId>
  <artifactId>jtds</artifactId>
  <version>1.3.1</version>
</dependency>

2.Replace the Driver Class Name additionally:

ru.yandex.clickhouse.ClickHouseDriver

3.Modify the JDBC URL like this:

jdbc:jtds:sqlserver://xxxx:1433;databaseName=xxx

Solution 14 - Java

I have also faced same issue while creating connection to the database with below connection string.

DriverManager.getConnection("jdbc:sqlserver://" + host + ":" + port + ";databaseName=" + dbName + ";user=" + userName + ";password=" + password);

After updating connection string as below, it worked.

Connection con = DriverManager.getConnection("jdbc:sqlserver://" + host + ":" + port + ";databaseName=" + dbName + ";encrypt=true;trustServerCertificate=true;user=" + userName + ";password=" + password);

here i have added encrypt=true;trustServerCertificate=true after the database name.

Solution 15 - Java

Issue got resolved for me when I changed the sqljdbc-4.2.0 jar with mssql-jdbc-8.4.1 jar

<dependency>
	  <groupId>com.microsoft.sqlserver</groupId>
	  <artifactId>mssql-jdbc</artifactId>
	  <version>8.4.1.jre8</version>
</dependency>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Question2AguyView Question on Stackoverflow
Solution 1 - JavaSunil KumarView Answer on Stackoverflow
Solution 2 - Java2AguyView Answer on Stackoverflow
Solution 3 - JavaTore RefsnesView Answer on Stackoverflow
Solution 4 - JavabbhagatView Answer on Stackoverflow
Solution 5 - Javacabaji99View Answer on Stackoverflow
Solution 6 - JavanbyView Answer on Stackoverflow
Solution 7 - JavaNikhilView Answer on Stackoverflow
Solution 8 - JavacoolhandView Answer on Stackoverflow
Solution 9 - JavaebyView Answer on Stackoverflow
Solution 10 - Javayolob 21View Answer on Stackoverflow
Solution 11 - JavaApplication AutomationView Answer on Stackoverflow
Solution 12 - JavaMartin KarariView Answer on Stackoverflow
Solution 13 - JavaLeoView Answer on Stackoverflow
Solution 14 - JavaSandeep PatelView Answer on Stackoverflow
Solution 15 - JavaRuhul HussainView Answer on Stackoverflow