Connecting with SSL Encryption

Starting with the Microsoft SQL Server 2005 JDBC Driver version 1.2, new connection properties and new getter and setter methods is introduced to allow applications to use the Secure Sockets Layer (SSL) encryption.

The examples in this topic describe how to use the new connection string properties 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 SQL Server JDBC Driver will not 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 SQL Server JDBC Driver 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. In order 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.

See Also

Concepts

Using SSL Encryption

Other Resources

Securing JDBC Driver Applications