Skip to content

Commit 86e9a0f

Browse files
Get-DbaNetworkConfiguration: Add SuitableCertificate property to output (#10143)
1 parent c583c61 commit 86e9a0f

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

public/Get-DbaNetworkConfiguration.ps1

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ function Get-DbaNetworkConfiguration {
5959
- TcpIpProperties: Nested object containing Enabled, KeepAlive, and ListenAll properties for TCP/IP configuration
6060
- TcpIpAddresses: Array of objects representing IP address configurations with properties like Name, Active, Enabled, IpAddress, TcpDynamicPorts, and TcpPort
6161
- Certificate: Nested object containing SSL certificate information (FriendlyName, DnsNameList, Thumbprint, Generated, Expires, IssuedTo, IssuedBy, Certificate object)
62+
- SuitableCertificate: Array of certificates from the local machine store that are suitable for SQL Server encryption based on key usage, signature algorithm, validity, and DNS names
6263
- Advanced: Nested object containing advanced settings (ForceEncryption, HideInstance, AcceptedSPNs, ExtendedProtection)
6364
6465
When -OutputType ServerProtocols is specified:
@@ -231,6 +232,35 @@ function Get-DbaNetworkConfiguration {
231232
$outputCertificate = $outputAdvanced = "Failed to get information from registry: Path not found"
232233
}
233234

235+
# Get a list of suitable certificates.
236+
# For details on the requirements see https://learn.microsoft.com/en-us/sql/database-engine/configure-windows/certificate-requirements
237+
$requiredKeyUsages = [System.Security.Cryptography.X509Certificates.X509KeyUsageFlags]::DigitalSignature -bor [System.Security.Cryptography.X509Certificates.X509KeyUsageFlags]::KeyEncipherment
238+
$networkName = if ($vsname) { $vsname } else { hostname }
239+
$suitableCertificate = Get-ChildItem -Path Cert:\LocalMachine\My -ErrorAction SilentlyContinue | Where-Object {
240+
try {
241+
$keyUsages = ($_.Extensions | Where-Object { $_ -is [System.Security.Cryptography.X509Certificates.X509KeyUsageExtension] }).KeyUsages
242+
$keyUsagesOk = ($keyUsages -band $requiredKeyUsages) -eq $requiredKeyUsages
243+
244+
$dnsNames = $_.DnsNameList.Unicode
245+
if (-not $dnsNames -and $_.Subject -match 'CN=([^,]+)') { $dnsNames = @( $Matches[1] ) }
246+
$dnsNamesOk = $dnsNames -contains $networkName -or $dnsNames -contains "$networkName.$env:USERDNSDOMAIN"
247+
248+
$_.PrivateKey -is [System.Security.Cryptography.RSACryptoServiceProvider] -and
249+
$_.PrivateKey.CspKeyContainerInfo.KeyNumber -eq [System.Security.Cryptography.KeyNumber]::Exchange -and
250+
$_.PublicKey.Key.KeySize -ge 2048 -and
251+
$_.PublicKey.Oid.FriendlyName -match 'RSA' -and
252+
$_.SignatureAlgorithm.FriendlyName -match 'sha256|sha384|sha512' -and
253+
$_.EnhancedKeyUsageList.FriendlyName -contains 'Server Authentication' -and
254+
$_.NotBefore -lt (Get-Date) -and
255+
$_.NotAfter -gt (Get-Date) -and
256+
$keyUsagesOk -and
257+
$dnsNamesOk
258+
} catch {
259+
$verbose += "Failed to test certificate '$($_.Thumbprint)' for suitability: $_"
260+
$false
261+
}
262+
}
263+
234264
[PSCustomObject]@{
235265
ComputerName = $instance.ComputerName
236266
InstanceName = $instance.InstanceName
@@ -241,6 +271,7 @@ function Get-DbaNetworkConfiguration {
241271
TcpIpProperties = $outputTcpIpProperties
242272
TcpIpAddresses = $outputTcpIpAddressesIPn + $outputTcpIpAddressesIPAll
243273
Certificate = $outputCertificate
274+
SuitableCertificate = $suitableCertificate
244275
Advanced = $outputAdvanced
245276
Verbose = $verbose
246277
}

0 commit comments

Comments
 (0)