Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Certificate multi-matcher #95

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
180 changes: 73 additions & 107 deletions source/DSCResources/DSC_WSManListener/DSC_WSManListener.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ function Get-TargetResource
Listener if a thumbprint is not specified.

.PARAMETER DN
This is a Distinguished Name component that will be used to identify the certificate to use
for the HTTPS WS-Man Listener if a thumbprint is not specified.
This is the BaseDN (path part of the full Distinguished Name) used to identify the certificate
to use for the HTTPS WS-Man Listener if a thumbprint is not specified.

.PARAMETER CertificateThumbprint
The Thumbprint of the certificate to use for the HTTPS WS-Man Listener.
Expand Down Expand Up @@ -360,8 +360,8 @@ function Set-TargetResource
Listener if a thumbprint is not specified.

.PARAMETER DN
This is a Distinguished Name component that will be used to identify the certificate to use
for the HTTPS WS-Man Listener if a thumbprint is not specified.
This is the BaseDN (path part of the full Distinguished Name) used to identify the certificate
to use for the HTTPS WS-Man Listener if a thumbprint is not specified.

.PARAMETER CertificateThumbprint
The Thumbprint of the certificate to use for the HTTPS WS-Man Listener.
Expand Down Expand Up @@ -589,8 +589,8 @@ function Get-DefaultPort
Listener if a thumbprint is not specified.

.PARAMETER DN
This is a Distinguished Name component that will be used to identify the certificate to use
for the HTTPS WS-Man Listener if a thumbprint is not specified.
This is the BaseDN (path part of the full Distinguished Name) used to identify the certificate
to use for the HTTPS WS-Man Listener if a thumbprint is not specified.

.PARAMETER CertificateThumbprint
The Thumbprint of the certificate to use for the HTTPS WS-Man Listener.
Expand All @@ -602,7 +602,7 @@ function Find-Certificate
param
(
[Parameter()]
[System.String]
[System.String[]]
$Issuer,

[Parameter()]
Expand All @@ -615,7 +615,7 @@ function Find-Certificate
$MatchAlternate,

[Parameter()]
[System.String]
[System.String[]]
$DN,

[Parameter()]
Expand All @@ -624,122 +624,88 @@ function Find-Certificate

[Parameter()]
[System.String]
$Hostname
$Hostname = $env:ComputerName
)

[System.String] $thumbprint = ''
$private:PSDefaultParameterValues = @{
'Get-Childitem:Path' = 'Cert:\LocalMachine\My'
'Get-ChildItem:SSLServerAuthentication' = $true
}

if ($PSBoundParameters.ContainsKey('CertificateThumbprint'))
{
Write-Verbose -Message ( @(
"$($MyInvocation.MyCommand): "
$($script:localizedData.FindCertificateByThumbprintMessage) `
-f $CertificateThumbprint
) -join '' )
# This hashtable will represent the search criteria. It is later compiled
# into a FilterScript
[Hashtable] $X509Properties = @{}

$certificate = Get-ChildItem -Path Cert:\localmachine\my | Where-Object -FilterScript {
($_.Thumbprint -eq $CertificateThumbprint)
} | Select-Object -First 1
# If CertificateThumbprint is specified, that's all we need or care about.
# Else, convert the input parameters into matcher arrays in $X509Properties.
if ($CertificateThumbprint) {
$X509Properties.Thumbprint = $CertificateThumbprint
}
else
{
# First try and find a certificate that is used to the FQDN of the machine
if ($SubjectFormat -in 'Both', 'FQDNOnly')
else {
[System.String] $FQDN = [System.Net.Dns]::GetHostByName($Hostname).HostName

# SubjectFormat
switch ($SubjectFormat)
{
# Lookup the certificate using the FQDN of the machine
if ([System.String]::IsNullOrEmpty($Hostname))
{
$Hostname = [System.Net.Dns]::GetHostByName($ENV:computerName).Hostname
'FQDNOnly' { [System.String] $CNs = $FQDN }
'NameOnly' { [System.String] $CNs = $Hostname }
'Both' { [System.String[]] $CNs = @($FQDN, $Hostname) }
}

# DNSNameList via MatchAlternate
if ($MatchAlternate) { [System.String[]] $X509Properties.DNSNameList = @($CNs) }

# BaseDNs via DN
if ($PSBoundParameters.ContainsKey('DN')) {
[System.String[]] $BaseDNs = foreach ($baseDN in $DN) { ", ${baseDN}" }
} else {
[System.String[]] $BaseDNs = @('')
}

# Put it all together for a list of possible Subject names
[System.String[]] $X509Properties.Subject = foreach ($hostname in $CNs) {
foreach ($baseDN in $BaseDNs) {
"CN=${hostname}${baseDN}"
}
$Subject = "CN=$Hostname"
}

if ($PSBoundParameters.ContainsKey('DN'))
{
$Subject = "$Subject, $DN"
} # if
if ($Issuer) { [System.String[]] $X509Properties.Issuer = @($Issuer) }
}

if ($MatchAlternate)
{
# Try and lookup the certificate using the subject and the alternate name
Write-Verbose -Message ( @(
"$($MyInvocation.MyCommand): "
$($script:localizedData.FindCertificateAlternateMessage) `
-f $Subject, $Issuer, $Hostname
) -join '' )
# Compile the X509Properties object into a list of expressions
[System.String[]] $filterExpressions = foreach ($property in $X509Properties.GetEnumerator()) {
if ($property.Value.Length -GT 0) {
[System.String] $valueString = "'{0}'" -f ($property.Value -join "','")

$certificate = (Get-ChildItem -Path Cert:\localmachine\my | Where-Object -FilterScript {
($_.Extensions.EnhancedKeyUsages.FriendlyName `
-contains 'Server Authentication') -and
($_.Issuer -eq $Issuer) -and
($Hostname -in $_.DNSNameList.Unicode) -and
($_.Subject -eq $Subject)
} | Select-Object -First 1)
switch ($property.Key) {
'DNSNameList' {
'(Compare-Object $_.DNSNameList.Unicode ({0}) -PassThru -IncludeEqual -ExcludeDifferent) -gt 0' -f $valueString
}
default {
'$_.{0} -in {1}' -f $property.Key, $valueString
}
}
else
{
# Try and lookup the certificate using the subject name
Write-Verbose -Message ( @(
"$($MyInvocation.MyCommand): "
$($script:localizedData.FindCertificateMessage) `
-f $Subject, $Issuer
) -join '' )

$certificate = Get-ChildItem -Path Cert:\localmachine\my | Where-Object -FilterScript {
($_.Extensions.EnhancedKeyUsages.FriendlyName `
-contains 'Server Authentication') -and
($_.Issuer -eq $Issuer) -and
($_.Subject -eq $Subject)
} | Select-Object -First 1
} # if
}
}

if (-not $certificate `
-and ($SubjectFormat -in 'Both', 'NameOnly'))
{
# If could not find an FQDN cert, try for one issued to the computer name
[System.String] $Hostname = $ENV:ComputerName
[System.String] $Subject = "CN=$Hostname"

if ($PSBoundParameters.ContainsKey('DN'))
{
$Subject = "$Subject, $DN"
} # if
# Link the expressions
[System.String] $filterScriptString = $filterExpressions -join ' -and '

if ($MatchAlternate)
{
# Try and lookup the certificate using the subject and the alternate name
Write-Verbose -Message ( @(
"$($MyInvocation.MyCommand): "
$($script:localizedData.FindCertificateAlternateMessage) `
-f $Subject, $Issuer, $Hostname
) -join '' )
# Create the ScriptBlock
$filterScript = [System.Management.Automation.ScriptBlock]::Create($filterScriptString)

$certificate = Get-ChildItem -Path Cert:\localmachine\my | Where-Object -FilterScript {
($_.Extensions.EnhancedKeyUsages.FriendlyName `
-contains 'Server Authentication') -and
($_.Issuer -eq $Issuer) -and
($Hostname -in $_.DNSNameList.Unicode) -and
($_.Subject -eq $Subject)
} | Select-Object -First 1
}
else
{
# Try and lookup the certificate using the subject name
Write-Verbose -Message ( @(
"$($MyInvocation.MyCommand): "
$($script:localizedData.FindCertificateMessage) `
-f $Subject, $Issuer
) -join '' )
# Execute our search
$certificateList = Get-ChildItem | Where-Object -FilterScript $filterScript

$certificate = Get-ChildItem -Path Cert:\localmachine\my | Where-Object -FilterScript {
($_.Extensions.EnhancedKeyUsages.FriendlyName `
-contains 'Server Authentication') -and
($_.Issuer -eq $Issuer) -and
($_.Subject -eq $Subject)
} | Select-Object -First 1
} # if
} # if
} # if
# Sort and select to ensure deterministic behavior
if ($certificateList)
{
$certificate = $certificateList | Sort-Object @(
@{ Expression = { $X509Properties['Subject'].IndexOf($_.Subject) }; Descending = $true }
@{ Expression = { $X509Properties['Issuer'].IndexOf($_.Issuer) }; Descending = $false }
) | Select-Object -First 1
}

if ($certificate)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class DSC_WSManListener : OMI_BaseResource
[Write, Description("The Issuer of the certificate to use for the HTTPS WS-Man Listener if a thumbprint is not specified.")] String Issuer;
[Write, Description("The format used to match the certificate subject to use for an HTTPS WS-Man Listener if a thumbprint is not specified."), ValueMap{"Both","FQDNOnly","NameOnly"}, Values{"Both","FQDNOnly","NameOnly"}] String SubjectFormat;
[Write, Description("Should the FQDN/Name be used to also match the certificate alternate subject for an HTTPS WS-Man Listener if a thumbprint is not specified.")] Boolean MatchAlternate;
[Write, Description("This is a Distinguished Name component that will be used to identify the certificate to use for the HTTPS WS-Man Listener if a thumbprint is not specified.")] String DN;
[Write, Description("This is the BaseDN (base of the full Distinguished Name) used to identify the certificate to use for the HTTPS WS-Man Listener if a thumbprint is not specified.")] String DN;
[Write, Description("The host name that a HTTPS WS-Man Listener will be bound to. If not specified it will default to the computer name of the node.")] String Hostname;
[Read, Description("Returns true if the existing WS-Man Listener is enabled.")] Boolean Enabled;
[Read, Description("The URL Prefix of the existing WS-Man Listener.")] String URLPrefix;
Expand Down
6 changes: 3 additions & 3 deletions tests/Integration/DSC_WSManListener.Integration.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ try
Remove-Item -Force

$Hostname = ([System.Net.Dns]::GetHostByName($ENV:computerName).Hostname)
$DN = 'O=Contoso Inc, S=Pennsylvania, C=US'
$Issuer = "CN=$Hostname, $DN"
$BaseDN = 'O=Contoso Inc, S=Pennsylvania, C=US'
$Issuer = "CN=$Hostname, $BaseDN"

# Create the certificate
if ([System.Environment]::OSVersion.Version.Major -ge 10)
Expand Down Expand Up @@ -146,7 +146,7 @@ try
Issuer = $Issuer
SubjectFormat = 'Both'
MatchAlternate = $False
DN = $DN
BaseDN = $BaseDN
Hostname = $Hostname
}
)
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/DSC_WSManListener_Add_HTTPS.config.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Configuration DSC_WSManListener_Config_Add_HTTPS {
Issuer = $Node.Issuer
SubjectFormat = $Node.SubjectFormat
MatchAlternate = $Node.MatchAlternate
DN = $Node.DN
DN = $Node.BaseDN
}
}
}
Loading