LLI Wiki — My Digital Knowledge Base

SQL Server : certificat TLS avec PowerShell et certutil

Créer, vérifier, convertir et importer un certificat TLS pour SQL Server sous Windows.

Créer, vérifier, convertir et importer un certificat TLS pour SQL Server sous Windows : certificat auto-signé avec PowerShell, certificat émis par une autorité de certification (AC) d'entreprise, contrôles, conversions de formats et import.

1. Certificat auto-signé avec PowerShell

Le certificat est créé dans le magasin de la machine, avec le FQDN et le nom court du serveur comme noms DNS.

$Fqdn = [System.Net.Dns]::GetHostEntry('').HostName

$cert = New-SelfSignedCertificate `
 -Type SSLServerAuthentication `
 -Subject "CN=$Fqdn" `
 -DnsName @(
 $env:COMPUTERNAME,
 $Fqdn,
 "localhost"
 ) `
 -KeyAlgorithm RSA `
 -KeyLength 2048 `
 -HashAlgorithm SHA256 `
 -KeySpec KeyExchange `
 -Provider "Microsoft RSA SChannel Cryptographic Provider" `
 -CertStoreLocation "Cert:\LocalMachine\My" `
 -NotAfter (Get-Date).AddYears(3)

$cert

2. Certificat émis par une AC d'entreprise

Lister les modèles de certificat disponibles, puis demander un certificat avec le modèle MSSQLServer (nom commun du modèle « MS-SQL Server »).

certutil -Template
certreq -enroll -machine MSSQLServer

3. Trouver le certificat à utiliser

Get-ChildItem Cert:\LocalMachine\My |
Select-Object Subject, Thumbprint, NotAfter, HasPrivateKey
$cert = Get-ChildItem Cert:\LocalMachine\My |
Where-Object Thumbprint -eq "THUMBPRINT"

Vérifier aussi le compte de service SQL Server : le compte doit pouvoir lire la clé privée du certificat.

Get-CimInstance Win32_Service |
Where-Object Name -like 'MSSQL*' |
Select-Object Name, StartName

4. Contrôler le certificat

Contrôle PowerShell / Windows OpenSSL
Tout afficher certutil -v -store My THUMBPRINT openssl x509 -in cert.pem -text -noout
CN / Subject $cert.Subject openssl x509 -in cert.pem -subject -noout
SAN $cert.Extensions | ? {$_.Oid.Value -eq "2.5.29.17"} | % {$_.Format($true)} openssl x509 -in cert.pem -ext subjectAltName -noout
EKU $cert.EnhancedKeyUsageList openssl x509 -in cert.pem -ext extendedKeyUsage -noout
Dates $cert | Select NotBefore,NotAfter openssl x509 -in cert.pem -dates -noout
SHA1 $cert.Thumbprint openssl x509 -in cert.pem -fingerprint -sha1 -noout
SHA256 $cert.GetCertHashString("SHA256") openssl x509 -in cert.pem -fingerprint -sha256 -noout
Clé privée $cert.HasPrivateKey openssl pkey -in key.pem -check
Taille RSA $cert.GetRSAPublicKey().KeySize openssl x509 -in cert.pem -text -noout
Provider / KeySpec certutil -v -store My THUMBPRINT Windows uniquement

Pour SQL Server, les valeurs attendues sont :

CN / SAN    = nom d'hôte / FQDN
EKU         = Server Authentication
Private Key = True
RSA         = 2048 bits minimum
KeySpec     = AT_KEYEXCHANGE
Provider    = Microsoft RSA SChannel Cryptographic Provider

Vérifier la chaîne de certification depuis Windows, ou la présentation TLS d'un serveur :

certutil -f -urlfetch -verify C:\temp\server.crt
openssl s_client -showcerts -connect sqlserver01.example.com:443

5. Formats

Format Contenu Clé privée
.cer / .crt Certificat public Non
.pem Certificat, chaîne ou clé Possible
.key Clé privée Oui
.pfx / .p12 Certificat, clé privée et chaîne Oui
.der Certificat binaire Non
.csr Demande de certificat Non

Conversions OpenSSL :

# PFX -> certificat
openssl pkcs12 -in cert.pfx -clcerts -nokeys -out cert.pem

# PFX -> clé privée
openssl pkcs12 -in cert.pfx -nocerts -out key.pem

# PEM -> PFX
openssl pkcs12 -export -out cert.pfx -inkey key.pem -in cert.pem

# DER -> PEM
openssl x509 -inform DER -in cert.cer -out cert.pem

# PEM -> DER
openssl x509 -outform DER -in cert.pem -out cert.der

6. Importer dans Windows

# Certificat seul
Import-Certificate `
  -FilePath ".\cert.cer" `
  -CertStoreLocation "Cert:\LocalMachine\My"

# Certificat avec clé privée
Import-PfxCertificate `
  -FilePath ".\cert.pfx" `
  -CertStoreLocation "Cert:\LocalMachine\My"

Par l'interface graphique : certlm.msc, puis Personnel > Certificats > Toutes les tâches > Importer.

7. Renouveler un certificat expiré

certreq -enroll -machine -q -cert "<SN>" renew reusekeys
certreq -enroll -machine -q -cert "<SN>" renew

La première commande réutilise la clé privée, la seconde en génère une nouvelle. <SN> est le numéro de série du certificat.

8. Référence des OID

OID Nom Usage
2.5.29.17 Subject Alternative Name (SAN) DNS, IP ou autres noms associés au certificat
2.5.29.15 Key Usage Signature, chiffrement, key encipherment, etc.
2.5.29.37 Extended Key Usage (EKU) Liste les usages applicatifs autorisés
1.3.6.1.5.5.7.3.1 Server Authentication Authentification TLS d'un serveur
1.3.6.1.5.5.7.3.2 Client Authentication Authentification TLS d'un client
1.3.6.1.5.5.7.3.3 Code Signing Signature de code
1.3.6.1.5.5.7.3.4 Email Protection S/MIME, signature et chiffrement de courriels
2.5.29.19 Basic Constraints Indique si le certificat est une AC ou un certificat final
2.5.29.14 Subject Key Identifier Identifie la clé publique du certificat
2.5.29.35 Authority Key Identifier Identifie la clé de l'AC qui a signé le certificat

Remarques

Mastodon LLIMastodon SocialFosstodon