Is there any way to connect to SQL Server 2022 using Client Certificate Authentication using an X.509 cert? From everything I can see it looks like this is not possible but where I am confused is it looks like there is an option in the ODBC 18 Driver here to specify a client cert and key? What scenario is this supported? Perhaps I am missing something in the documentation?
Just for fun I did try creating a certificate in SQL Server and authenticating to it using the ODBC Driver in C# like so.
- Make a Cert and map to a login
USE MASTER;
--create cert
CREATE CERTIFICATE coolCert
ENCRYPTION BY PASSWORD = ''
WITH SUBJECT = 'coolLogin',
EXPIRY_DATE = '12/31/2026';
--create login from cert
CREATE LOGIN coolLogin FROM CERTIFICATE coolCert;
go
--backup cert to server
BACKUP CERTIFICATE coolCert TO FILE = 'c:\certs\coolCert.pfx'
WITH FORMAT = 'PFX', PRIVATE KEY (
ENCRYPTION BY PASSWORD = '',
DECRYPTION BY PASSWORD = '',
ALGORITHM = 'AES_256');
GO
- Client Auth Test
string connectionString = "" +
"Driver={ODBC Driver 18 for SQL Server};" +
"Server=SomeCoolServer;" +
"Database=master;" +
"TrustServerCertificate=yes;" + //tried both yes and no
"Encrypt=yes;" +
"ClientCertificate=file:C:\\certs\\coolCert.pfx,password:thePassword;";
"ClientKey=file:C:\\certs\\sqlcert.key;";
using (OdbcConnection connection = new OdbcConnection(connectionString))
{
try
{
connection.Open();
Console.WriteLine("Connection successful!");
string query = "select SUSER_SNAME()";
OdbcCommand command = new OdbcCommand(query, connection);
using (OdbcDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
Console.WriteLine(reader[0].ToString());
}
}
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}
It actually seems like it accepts the cert and I even tried using an incorrect password and it threw an exception that the password was wrong but even with all this I am getting the following error:
Error: ERROR [28000] [Microsoft][ODBC Driver 18 for SQL Server][SQL Server]Login failed for user ''
CREATE LOGIN's Remarks: "Logins created from certificates or asymmetric keys are used only for code signing. They can't be used to connect to SQL Server." SQL Server does not support connections via certificates.