Microsoft SQL Server External (EKM)
Eviden KMS integrates with Microsoft SQL Server External key management using a Windows DLL implementing the SQL Server Extensible Key Management (EKM).
The Windows DLL is a SQL Server EKM provider that forwards key operations to the Eviden KMS over mutual TLS. This allows SQL Server features like column-level encryption and Transparent Data Encryption (TDE) to use keys managed by the Eviden KMS, without exposing key material to the SQL Server host.
The Windows DLL is available in a separate project on GitHub; pre-built signed DLLs are available for download.
Architecture & Workflow
The sequence diagram below shows how SQL Server interacts with the Cosmian EKM Provider DLL (cosmian_ekm_sql_server.dll), which forwards KMIP/REST operations over TLS to the Eviden KMS, persisting key state in the configured KMS database backend:
Deployment Guide
This document explains how to install and configure the Cosmian EKM SQL Server provider, starting from a signed DLL. Follow the steps in order.
Prerequisites
| Requirement | Notes |
|---|---|
| Windows Server 2016+ or Windows 10/11 | x64 |
| SQL Server 2016+ (any edition) with EKM support | Standard or Enterprise; Express does not support EKM |
| Eviden KMS 5.x | Must run on the same machine (or be network-reachable) |
Signed cosmian_ekm_sql_server.dll | Download the pre-built signed DLL, or build and sign it following BUILD.md |
| EKM provider ≥ 0.2.0 | Required for Auth Verifier login/password and for KMIP key names |
| PowerShell 5.1 (elevated) | For the setup scripts |
sqlcmd on the system PATH | Part of SQL Server Tools or installable standalone |
The PowerShell scripts used below (scripts\Initialize-EkmEnvironment.ps1, New-EkmCertificate.ps1, Sign-EkmDll.ps1, Deploy-EkmDll.ps1, Prepare-Integration-Tests.ps1) live in the ekm_sql_server repository. Run them from a checkout of that repository: the DLL path defaults to target\release\... relative to it; override it with -DllPath.
1. Install SQL Server
Install SQL Server with default settings. The instance name used throughout
this guide is the default instance (MSSQLSERVER). For a named instance,
substitute MSSQL$<InstanceName> wherever a service name is needed.
Ensure the SQL Server Database Engine service account is noted
(default: NT SERVICE\MSSQLSERVER).
2. Start the Eviden KMS
The KMS must be running and accessible before SQL Server loads the EKM provider.
# Example — adjust paths and config file as needed
cosmian_kms_server.exe --config kms_server.toml
The KMS must be configured with:
- A TLS certificate trusted by the SQL Server machine, installed in
LocalMachine\Root. The provider has no CA-file setting;accept_invalid_certs = trueinconfig.tomldisables the verification and is for development only. - For mTLS logins: verification of client certificates, with
clients_ca_cert_filein the[tls]section (see TLS). - For Auth Verifier logins: an
[auth_verifier]section (see Authentication).
Verify the KMS is reachable:
Test-NetConnection -ComputerName localhost -Port 9998
3. Prepare the machine (one-time)
# Creates directories and grants ACLs to the SQL Server service account
scripts\Initialize-EkmEnvironment.ps1
# Creates and trusts the self-signed code-signing certificate "CN=Cosmian EKM Dev".
# Skip it when you sign with a CA-issued code-signing certificate.
scripts\New-EkmCertificate.ps1
For a named instance, pass the service account to the first script: -SqlServiceAccount "NT SERVICE\MSSQL$MyInstance"`.
Initialize-EkmEnvironment.ps1 creates C:\Program Files\Cosmian\EKM\, C:\ProgramData\Cosmian\EKM\ and its logs\ subdirectory, and grants the service account Modify on the data directory. This is a one-time operation per machine.
4. Install the provider configuration
Create C:\ProgramData\Cosmian\EKM\config.toml (see
config.toml.example for the full schema):
max_age_seconds = 1800
stale_collector_period_seconds = 120
[kms]
server_url = "https://localhost:9998"
accept_invalid_certs = false # set true only for local dev
[[kms.certificates]]
username = "admin"
client_cert = 'C:\ProgramData\Cosmian\EKM\admin.cert.pem'
client_key = 'C:\ProgramData\Cosmian\EKM\admin.key.pem'
To authenticate with an Auth Verifier user and password instead of a client certificate (see section 8), add:
[kms.auth_verifier]
server_url = "https://auth.example.com" # https, except for localhost
realm = "kms"
Copy the mTLS client certificate and key files to the paths referenced above, and install the KMS CA certificate in the Windows trust store (LocalMachine\Root) so TLS verification succeeds.
| Field | Default | Meaning |
|---|---|---|
max_age_seconds | 1800 | Idle lifetime of an EKM session; every access resets it |
stale_collector_period_seconds | 120 | Period of the sweep that evicts expired sessions |
[kms] server_url | URL of the KMS | |
[kms] accept_invalid_certs | false | Disables KMS certificate verification; development only |
[[kms.certificates]] username | Credential IDENTITY this entry applies to (matched case-insensitively) | |
[[kms.certificates]] client_cert, client_key | PEM client certificate and key. The certificate Subject CN or SAN must equal username, otherwise the provider reports certificate CN/SAN mismatch | |
[kms.auth_verifier] server_url, realm | Auth Verifier used for logins that have no certificate entry |
The session settings (max_age_seconds, stale_collector_period_seconds) are read once, when the first session opens. The [kms] settings are re-read for each operation, but KMS clients are cached per login for the lifetime of the DLL: restart SQL Server after changing certificate or credential paths.
If config.toml cannot be parsed, the provider silently falls back to its defaults (no KMS configured) and logs a WARNING in C:\ProgramData\Cosmian\EKM\logs\.
5. Enable EKM in SQL Server
Connect to SQL Server (e.g. via SSMS or sqlcmd) and run:
EXEC sp_configure 'show advanced options', 1;
RECONFIGURE;
EXEC sp_configure 'EKM provider enabled', 1;
RECONFIGURE;
6. Deploy the DLL
# Must be run as Administrator
scripts\Deploy-EkmDll.ps1
This script, in order:
- Verifies the DLL's Authenticode signature.
- Checks the installation directory.
- Stops the SQL Server service (
MSSQLSERVERby default). - Copies the DLL to
C:\Program Files\Cosmian\EKM\cosmian_ekm_sql_server.dll. - Starts the SQL Server service.
- Drops and re-creates the
CosmianEKMcryptographic provider. Registration runs last and is hard-wired tosqlcmd -S localhost, i.e. the default instance only.
| Flag | Effect |
|---|---|
-DllPath | DLL to deploy (default: target\release\cosmian_ekm_sql_server.dll of the repository checkout) |
-SqlServiceName | SQL Server service to stop and start, for example "MSSQL$MyInstance"` |
-SkipServiceRestart | Neither stops nor starts SQL Server; the copy fails if the DLL is locked by a running instance |
-SkipRegistration | Skips step 6 |
scripts\Deploy-EkmDll.ps1 -SqlServiceName "MSSQL`$MyInstance" -SkipRegistration
scripts\Deploy-EkmDll.ps1 -SkipRegistration
Once credentials, logins or keys depend on the provider, always use -SkipRegistration: re-registration drops the provider, which SQL Server refuses while dependents exist. For a named instance use -SkipRegistration as well and register by hand (section 7).
Replacing the DLL needs a SQL Server restart (the file is locked while SQL Server runs); no re-registration is needed.
7. Register the provider
If Deploy-EkmDll.ps1 was run with -SkipRegistration, register manually:
CREATE CRYPTOGRAPHIC PROVIDER CosmianEKM
FROM FILE = 'C:\Program Files\Cosmian\EKM\cosmian_ekm_sql_server.dll';
Verify (dll_path shows the registered DLL):
SELECT name, dll_path, is_enabled FROM sys.cryptographic_providers WHERE name = 'CosmianEKM';
A failed CREATE CRYPTOGRAPHIC PROVIDER (Msg 33029) leaves no catalog entry; see Troubleshooting.
8. Create a credential and map it to a login
Each SQL Server login that uses EKM keys must have a credential. The credential tells the provider how to authenticate to the KMS; two modes exist.
mTLS (the IDENTITY has a [[kms.certificates]] entry): the provider presents the client
certificate. SECRET is ignored.
CREATE CREDENTIAL Cosmian_EKM_Cred
WITH IDENTITY = 'admin', -- must match [[kms.certificates]] username
SECRET = 'unused'
FOR CRYPTOGRAPHIC PROVIDER CosmianEKM;
Auth Verifier (the IDENTITY has no certificate entry and [kms.auth_verifier] is set):
IDENTITY and SECRET are the user name and password of an Auth Verifier user in the configured
realm. The provider checks them at every EKM session open and uses the JWT it receives toward the
KMS; a wrong password makes SQL Server report an authentication failure. Keep the realm session
lifetime at least equal to max_age_seconds. The KMS must be started with an [auth_verifier]
section for the same realm (see Authentication).
CREATE CREDENTIAL Cosmian_EKM_Cred
WITH IDENTITY = 'kms-user', -- Auth Verifier user
SECRET = '<PASSWORD>' -- Auth Verifier password
FOR CRYPTOGRAPHIC PROVIDER CosmianEKM;
Then map the credential to a Windows login (adjust the login name):
ALTER LOGIN [HOSTNAME\YourLogin] ADD CREDENTIAL Cosmian_EKM_Cred;
The same value must be used for:
- the
IDENTITYof the SQL Server credential (CREATE CREDENTIAL ... WITH IDENTITY), - the
usernameof the[[kms.certificates]]entry inconfig.tomlon the SQL Server host, - the Common Name (CN) of the client mTLS certificate presented to the KMS.
If any of the three differ, authentication to the KMS fails. This does not apply to the Auth Verifier mode.
Further rules:
- Auth Verifier mode does not support TOTP-enabled users (the provider returns
TOTP not supported). - A login whose credential
IDENTITYhas no certificate entry, while[kms.auth_verifier]is not configured, is refused.
9. Create keys
USE master;
-- Asymmetric key (RSA 2048)
CREATE ASYMMETRIC KEY MyRsaKey
FROM PROVIDER CosmianEKM
WITH ALGORITHM = RSA_2048,
PROVIDER_KEY_NAME = 'my-rsa-key',
CREATION_DISPOSITION = CREATE_NEW;
-- Symmetric key (AES 256)
CREATE SYMMETRIC KEY MyAesKey
FROM PROVIDER CosmianEKM
WITH ALGORITHM = AES_256,
PROVIDER_KEY_NAME = 'my-aes-key',
CREATION_DISPOSITION = CREATE_NEW;
The provider (version 0.2.0 or later) gives each KMS object the KMIP Name attribute PROVIDER_KEY_NAME, and the same value as a tag. For an asymmetric key the private key is named my-rsa-key and the public key my-rsa-key_pk; both carry the tag my-rsa-key. Keys created by older providers carry only the tag.
The thumbprint SQL Server shows for a key is the UID of the KMS object (for an RSA key, of the private key; the public key UID is <private UID>_pk), not the name.
Supported algorithms
ALGORITHM | KMS object |
|---|---|
RSA_2048, RSA_3072, RSA_4096 | RSA key pair |
AES_128, AES_192, AES_256 | AES symmetric key |
Key material never leaves the KMS: RSA keys wrap and unwrap with RSA-OAEP-SHA256 on the KMS (public key <private UID>_pk encrypts, private key decrypts), and AES keys encrypt with AES-CBC (the provider prepends the 16-byte IV to the ciphertext).
DROP ASYMMETRIC KEY and DROP SYMMETRIC KEY revoke, then destroy, the KMS object(s) of the key.
Activate the keys in the KMS
Keys created through the EKM provider start in the KMIP Pre-Active state (the KMS default after Create) and must be activated before use; until then, SQL Server operations on the key fail with an encryption error from the provider. Find the UID the KMS assigned to each key with ckms locate --tag <PROVIDER_KEY_NAME>, or by name in the Web UI, then activate it. For an asymmetric key the tag matches both the private and the public key; locating by KMIP Name (my-rsa-key for the private key, my-rsa-key_pk for the public key) tells them apart. A name is not a UID: the activation commands below need the UID.
# Symmetric key
ckms sym keys activate --key-id <AES_KEY_UID>
# Asymmetric key: activate the private key and the public key separately
ckms rsa keys activate --key-id <RSA_PRIVATE_KEY_UID>
ckms rsa keys activate --key-id <RSA_PUBLIC_KEY_UID>
Check the state with ckms attributes get --id <KEY_UID>.
10. Column-level encryption (quick check)
USE master;
-- Symmetric key protected by the EKM asymmetric key
CREATE SYMMETRIC KEY ColKey
WITH ALGORITHM = AES_256
ENCRYPTION BY ASYMMETRIC KEY MyRsaKey;
-- Encrypt
OPEN SYMMETRIC KEY ColKey DECRYPTION BY ASYMMETRIC KEY MyRsaKey;
SELECT ENCRYPTBYKEY(KEY_GUID('ColKey'), N'Hello EKM!');
CLOSE SYMMETRIC KEY ColKey;
11. Transparent Data Encryption (TDE)
-- Create a login backed by the EKM asymmetric key
CREATE LOGIN Cosmian_TDE_Login FROM ASYMMETRIC KEY MyRsaKey;
-- Credential for unattended access
CREATE CREDENTIAL Cosmian_TDE_Cred
WITH IDENTITY = 'admin', SECRET = 'unused' -- mTLS mode; Auth Verifier mode: its user and password
FOR CRYPTOGRAPHIC PROVIDER CosmianEKM;
ALTER LOGIN Cosmian_TDE_Login ADD CREDENTIAL Cosmian_TDE_Cred;
-- Enable TDE on a database
USE MyDatabase;
CREATE DATABASE ENCRYPTION KEY
WITH ALGORITHM = AES_256
ENCRYPTION BY SERVER ASYMMETRIC KEY MyRsaKey;
ALTER DATABASE MyDatabase SET ENCRYPTION ON;
The credential identity rules of section 8 apply unchanged: use either the mTLS identity or the Auth Verifier user and password.
Monitor encryption progress:
SELECT db.name, dek.encryption_state, dek.percent_complete
FROM sys.dm_database_encryption_keys dek
JOIN sys.databases db ON dek.database_id = db.database_id;
State 3 means fully encrypted.
12. Run integration tests
The integration tests live in the ekm_sql_server repository and run against a real SQL Server and KMS. Prerequisites:
- The KMS runs on
https://localhost:9998with mTLS (aclients_ca_cert_filethat signs the test client certificate). scripts\New-EkmCertificate.ps1andscripts\Initialize-EkmEnvironment.ps1have been run once.- The
WIN_LOGINconstant intests/integration.rsis set to your Windows login (DOMAIN\user). config.test.tomlhas been edited: its certificate paths point at the author's checkout.Prepare-Integration-Tests.ps1installs it as the active configuration and overwritesconfig.toml.
# All-in-one: build + sign + deploy + copy test config
scripts\Prepare-Integration-Tests.ps1
# Then run the tests, sequentially
cargo test --test integration -- --test-threads=1
The tests are t00 to t13 followed by t99; t99_cleanup drops every object the suite created. See tests.md for the description of each test.
13. Uninstall
Remove the objects in dependency order; SQL Server refuses to drop a provider, credential or key that is still referenced. Adapt the names to the ones you used (this page uses MyRsaKey, MyAesKey, ColKey, Cosmian_TDE_Login, Cosmian_TDE_Cred and Cosmian_EKM_Cred).
-- 1. Decrypt and remove TDE
ALTER DATABASE MyDatabase SET ENCRYPTION OFF;
-- wait until sys.dm_database_encryption_keys no longer reports the database as encrypted
USE MyDatabase;
DROP DATABASE ENCRYPTION KEY;
USE master;
-- 2. Symmetric keys protected by the EKM asymmetric key
DROP SYMMETRIC KEY ColKey;
-- 3. Credential mappings, then the logins created from the EKM key
ALTER LOGIN [HOSTNAME\YourLogin] DROP CREDENTIAL Cosmian_EKM_Cred;
ALTER LOGIN Cosmian_TDE_Login DROP CREDENTIAL Cosmian_TDE_Cred;
DROP LOGIN Cosmian_TDE_Login;
-- 4. Credentials
DROP CREDENTIAL Cosmian_TDE_Cred;
DROP CREDENTIAL Cosmian_EKM_Cred;
-- 5. EKM keys (revokes and destroys the objects on the KMS)
DROP ASYMMETRIC KEY MyRsaKey;
DROP SYMMETRIC KEY MyAesKey;
-- 6. The provider
DROP CRYPTOGRAPHIC PROVIDER CosmianEKM;
Then remove the DLL:
Stop-Service MSSQLSERVER -Force
Remove-Item 'C:\Program Files\Cosmian\EKM\cosmian_ekm_sql_server.dll'
Start-Service MSSQLSERVER
Troubleshooting
Provider not loading
Check the EKM log file:
C:\ProgramData\Cosmian\EKM\logs\cosmian_ekm.log.<date>
"Cannot open database / invalid object name"
The SQL Server service account needs Modify rights on
C:\ProgramData\Cosmian\EKM\. Re-run Initialize-EkmEnvironment.ps1.
KMS connection errors
- Verify
config.tomlhas the correctserver_url. - Confirm the client certificate CN or SAN matches the SQL credential identity.
- Check the KMS server is running:
Test-NetConnection localhost -Port 9998.
Key operations fail after redeployment
If the DLL is replaced, SQL Server must be restarted to reload it. Run
Deploy-EkmDll.ps1 (which handles the restart automatically).
Msg 33029 when running CREATE CRYPTOGRAPHIC PROVIDER
SQL Server reports Msg 33029 with a provider error code (3, 0 or 1) when the provider information structure returned by the DLL is invalid, or when the DLL is unsigned or its signature is not trusted. A failed CREATE CRYPTOGRAPHIC PROVIDER leaves no entry in sys.cryptographic_providers. Use a provider 0.2.0 or later, sign the DLL, and check:
(Get-AuthenticodeSignature 'C:\Program Files\Cosmian\EKM\cosmian_ekm_sql_server.dll').Status # must be Valid
PowerShell refuses to run the scripts
Allow locally created scripts to run:
Set-ExecutionPolicy RemoteSigned -Scope LocalMachine
No log file is written
The SQL Server service account lacks write access to C:\ProgramData\Cosmian\EKM\logs\. Re-run scripts\Initialize-EkmEnvironment.ps1 (add -SqlServiceAccount for a named instance).
Finding which call failed
Every SqlCrypt* entry point of the provider logs an INFO line when SQL Server calls it. The last function logged before an error is the failing step.
certificate CN/SAN mismatch
The Subject CN or a SAN of the client certificate does not equal the credential IDENTITY (compared with the username of the [[kms.certificates]] entry). See the three-place match in section 8.
no client certificate configured for user
The credential IDENTITY has no [[kms.certificates]] entry (username is matched case-insensitively) and [kms.auth_verifier] is not configured. Add one of the two to config.toml.
Authentication failure in Auth Verifier mode
Check, in this order: the password in the credential SECRET, the realm of [kms.auth_verifier], that the KMS runs with an [auth_verifier] section for the same realm, and that the user is not TOTP-enabled (TOTP not supported).
Provider ignores config.toml
An unparsable config.toml makes the provider silently fall back to its defaults, which have no KMS configured, and log a WARNING in the EKM log file. Fix the TOML syntax and restart SQL Server.