Connecting with SSL Encryption

DownloadDownload JDBC Driver

The examples in this article describe how to use connection string properties that allow applications to use Secure Sockets Layer (SSL) encryption in a Java application. For more information about these new connection string properties such as encrypt, trustServerCertificate, trustStore, trustStorePassword, and hostNameInCertificate, see Setting the Connection Properties.

When the encrypt property is set to true and the trustServerCertificate property is set to true, the Microsoft JDBC Driver for SQL Server won't validate the SQL Server SSL certificate. This is usually required for allowing connections in test environments, such as where the SQL Server instance has only a self signed certificate.

The following code example demonstrates how to set the trustServerCertificate property in a connection string:

String connectionUrl =   
    "jdbc:sqlserver://localhost:1433;" +  
     "databaseName=AdventureWorks;integratedSecurity=true;" +  
     "encrypt=true;trustServerCertificate=true";  

When the encrypt property is set to true and the trustServerCertificate property is set to false, the Microsoft JDBC Driver for SQL Server will validate the SQL Server SSL certificate. Validating the server certificate is a part of the SSL handshake and ensures that the server is the correct server to connect to. To validate the server certificate, the trust material must be supplied at connection time either by using trustStore and trustStorePassword connection properties explicitly, or by using the underlying Java Virtual Machine (JVM)'s default trust store implicitly.

The trustStore property specifies the path (including filename) to the certificate trustStore file, which contains the list of certificates that the client trusts. The trustStorePassword property specifies the password used to check the integrity of the trustStore data. For more information on using the JVM's default trust store, see the Configuring the Client for SSL Encryption.

The following code example demonstrates how to set the trustStore and trustStorePassword properties in a connection string:

String connectionUrl =   
    "jdbc:sqlserver://localhost:1433;" +  
     "databaseName=AdventureWorks;integratedSecurity=true;" +  
     "encrypt=true; trustServerCertificate=false;" +  
     "trustStore=storeName;trustStorePassword=storePassword";  

The JDBC Driver provides an additional property, hostNameInCertificate, which specifies the host name of the server. The value of this property must match the subject property of the certificate.

The following code example demonstrates how to use the hostNameInCertificate property in a connection string:

String connectionUrl =   
    "jdbc:sqlserver://localhost:1433;" +  
     "databaseName=AdventureWorks;integratedSecurity=true;" +  
     "encrypt=true; trustServerCertificate=false;" +  
     "trustStore=storeName;trustStorePassword=storePassword" +  
     "hostNameInCertificate=hostName";  

Note

Alternatively, you can set the value of connection properties by using the appropriate setter methods provided by the SQLServerDataSource class.

If the encrypt property is set to true and the trustServerCertificate property is set to false and if the server name in the connection string doesn't match the server name in the SSL certificate, the following error will be issued: The driver couldn't 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.". As of version 7.2, the driver supports wildcard pattern matching in the left-most label of the server name in the SSL certificate.

See Also

Using SSL Encryption
Securing JDBC Driver Applications