Skip to content

Commit c0feeeb

Browse files
authored
Merge pull request #159 from Keyfactor/78697-SSL_Flags_Not_Updating
Fixed an issue with SSL flags
2 parents 7d294d3 + 984d342 commit c0feeeb

22 files changed

+149
-30
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
2.6.4
2+
* Fixed an issue with SSL Flags greater than 3 were not being applied correctly to newer IIS servers.
13
2.6.3
24
* Fixed re-enrollment or ODKG job when RDN Components contained escaped commas.
35
* Updated renewal job for IIS Certs to delete the old cert if not bound or used by other web sites.

IISU/PowerShellScripts/WinCertScripts.ps1

Lines changed: 76 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# 08/29/25 Fixed the add cert to store function to return the correct thumbprint
1212
# Made changes to the IIS Binding logic, breaking it into manageable pieces to aid in debugging issues
1313
# 09/16/25 Updated the Get CSP function to handle null values when reading hybrid certificates
14+
# 11/17/25 Fixed issue with SSL Flags not being applied correctly to IIS bindings (2.6.4)
1415

1516
# Set preferences globally at the script level
1617
$DebugPreference = "Continue"
@@ -388,74 +389,110 @@ function New-KFIISSiteBinding {
388389
param (
389390
[Parameter(Mandatory = $true)]
390391
[string]$SiteName,
391-
392392
[string]$IPAddress = "*",
393-
394393
[int]$Port = 443,
395-
396394
[AllowEmptyString()]
397395
[string]$Hostname = "",
398-
399396
[ValidateSet("http", "https")]
400397
[string]$Protocol = "https",
401-
402398
[ValidateScript({
403399
if ($Protocol -eq 'https' -and [string]::IsNullOrEmpty($_)) {
404400
throw "Thumbprint is required when Protocol is 'https'"
405401
}
406402
$true
407403
})]
408404
[string]$Thumbprint,
409-
410405
[string]$StoreName = "My",
411-
412406
[int]$SslFlags = 0
413407
)
414408

415409
Write-Information "Entering PowerShell Script: New-KFIISSiteBinding" -InformationAction SilentlyContinue
416-
Write-Verbose "Function: New-KFIISSiteBinding"
417410
Write-Verbose "Parameters: $(($PSBoundParameters.GetEnumerator() | ForEach-Object { "$($_.Key): '$($_.Value)'" }) -join ', ')"
418411

419412
try {
420-
# This function mimics IIS Manager behavior:
421-
# - Replaces exact binding matches (same IP:Port:Hostname)
422-
# - Allows multiple bindings with different hostnames (SNI)
423-
# - Lets IIS handle true conflicts rather than pre-checking
424-
425-
# Step 1: Verify site exists and get management approach
413+
# Step 1: Perform verifications and get management info
414+
# Check SslFlags
415+
if (-not (Test-ValidSslFlags -SslFlags $SslFlags)) {
416+
return New-ResultObject -Status Error 400 -Step "Validation" -ErrorMessage "Invalid SSL Flag bit configuration ($SslFlags)"
417+
}
418+
426419
$managementInfo = Get-IISManagementInfo -SiteName $SiteName
427420
if (-not $managementInfo.Success) {
428421
return $managementInfo.Result
429422
}
430423

431-
# Step 2: Remove existing HTTPS bindings for this exact binding information
432-
# This mimics IIS behavior: replace exact matches, allow different hostnames
424+
# Step 2: Remove existing HTTPS bindings for this binding info
433425
$searchBindings = "${IPAddress}:${Port}:${Hostname}"
434426
Write-Verbose "Removing existing HTTPS bindings for: $searchBindings"
435-
427+
436428
$removalResult = Remove-ExistingIISBinding -SiteName $SiteName -BindingInfo $searchBindings -UseIISDrive $managementInfo.UseIISDrive
437429
if ($removalResult.Status -eq 'Error') {
438430
return $removalResult
439431
}
440432

441-
# Step 3: Add new binding with SSL certificate
442-
Write-Verbose "Adding new binding with SSL certificate"
443-
433+
# Step 3: Determine SslFlags supported by Microsoft.Web.Administration
434+
if ($SslFlags -gt 3) {
435+
Write-Verbose "SslFlags value $SslFlags exceeds managed API range (0–3). Applying reduced flags for creation."
436+
$SslFlagsApplied = ($SslFlags -band 3)
437+
} else {
438+
$SslFlagsApplied = $SslFlags
439+
}
440+
441+
# Step 4: Add the new binding with the reduced flag set
442+
Write-Verbose "Adding new binding with SSL certificate (SslFlagsApplied=$SslFlagsApplied)"
443+
444444
$addParams = @{
445-
SiteName = $SiteName
446-
Protocol = $Protocol
447-
IPAddress = $IPAddress
448-
Port = $Port
449-
Hostname = $Hostname
450-
Thumbprint = $Thumbprint
451-
StoreName = $StoreName
452-
SslFlags = $SslFlags
445+
SiteName = $SiteName
446+
Protocol = $Protocol
447+
IPAddress = $IPAddress
448+
Port = $Port
449+
Hostname = $Hostname
450+
Thumbprint = $Thumbprint
451+
StoreName = $StoreName
452+
SslFlags = $SslFlagsApplied
453453
UseIISDrive = $managementInfo.UseIISDrive
454454
}
455-
455+
456456
$addResult = Add-IISBindingWithSSL @addParams
457-
return $addResult
458457

458+
if ($addResult.Status -eq 'Error') {
459+
return $addResult
460+
}
461+
462+
# Step 5: If extended flags, update via appcmd.exe
463+
if ($SslFlags -gt 3) {
464+
Write-Verbose "Applying full SslFlags=$SslFlags via appcmd"
465+
466+
$appcmd = Join-Path $env:windir "System32\inetsrv\appcmd.exe"
467+
468+
# Escape any single quotes in hostname
469+
$safeHostname = $Hostname -replace "'", "''"
470+
$bindingInfo = "${IPAddress}:${Port}:${safeHostname}"
471+
472+
# Quote site name only if it contains spaces
473+
if ($SiteName -match '\s') {
474+
$siteArg = "/site.name:`"$SiteName`""
475+
} else {
476+
$siteArg = "/site.name:$SiteName"
477+
}
478+
479+
# Build binding argument for appcmd
480+
$bindingArg = "/bindings.[protocol='https',bindingInformation='$bindingInfo'].sslFlags:$SslFlags"
481+
482+
Write-Verbose "Running appcmd: $appcmd $siteArg $bindingArg"
483+
$appcmdOutput = & $appcmd set site $siteArg $bindingArg 2>&1
484+
Write-Verbose "appcmd output: $appcmdOutput"
485+
486+
#& $appcmd set site $siteArg $bindingArg | Out-Null
487+
488+
if ($LASTEXITCODE -ne 0) {
489+
Write-Warning "appcmd failed to set extended SslFlags ($SslFlags) for binding $bindingInfo."
490+
} else {
491+
Write-Verbose "Successfully updated SslFlags to $SslFlags via appcmd."
492+
}
493+
}
494+
495+
return $addResult
459496
}
460497
catch {
461498
$errorMessage = "Unexpected error in New-KFIISSiteBinding: $($_.Exception.Message)"
@@ -1464,6 +1501,15 @@ function Parse-DNSubject {
14641501
return $subjectString
14651502
}
14661503

1504+
function Test-ValidSslFlags {
1505+
param([int]$SslFlags)
1506+
1507+
$validBits = 1,2,4,8,32,64,128
1508+
$invalidBits = $SslFlags -bxor ($SslFlags -band ($validBits | Measure-Object -Sum).Sum)
1509+
1510+
return ($invalidBits -eq 0)
1511+
}
1512+
14671513
# Note: Removed Test-IISBindingConflict function - we now mimic IIS behavior
14681514
# IIS replaces exact matches and allows multiple hostnames (SNI) on same IP:Port
14691515
function Get-IISManagementInfo {

README.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,18 @@ the Keyfactor Command Portal
291291

292292
![WinCert Entry Parameters Tab](docsource/images/WinCert-entry-parameters-store-type-dialog.png)
293293

294+
295+
##### ProviderName
296+
297+
![WinCert Entry Parameter - ProviderName](docsource/images/WinCert-entry-parameters-store-type-dialog-ProviderName.png)
298+
299+
300+
##### SAN
301+
302+
![WinCert Entry Parameter - SAN](docsource/images/WinCert-entry-parameters-store-type-dialog-SAN.png)
303+
304+
305+
294306
</details>
295307
</details>
296308

@@ -426,6 +438,48 @@ the Keyfactor Command Portal
426438

427439
![IISU Entry Parameters Tab](docsource/images/IISU-entry-parameters-store-type-dialog.png)
428440

441+
442+
##### Port
443+
444+
![IISU Entry Parameter - Port](docsource/images/IISU-entry-parameters-store-type-dialog-Port.png)
445+
446+
447+
##### IPAddress
448+
449+
![IISU Entry Parameter - IPAddress](docsource/images/IISU-entry-parameters-store-type-dialog-IPAddress.png)
450+
451+
452+
##### HostName
453+
454+
![IISU Entry Parameter - HostName](docsource/images/IISU-entry-parameters-store-type-dialog-HostName.png)
455+
456+
457+
##### SiteName
458+
459+
![IISU Entry Parameter - SiteName](docsource/images/IISU-entry-parameters-store-type-dialog-SiteName.png)
460+
461+
462+
##### SniFlag
463+
464+
![IISU Entry Parameter - SniFlag](docsource/images/IISU-entry-parameters-store-type-dialog-SniFlag.png)
465+
466+
467+
##### Protocol
468+
469+
![IISU Entry Parameter - Protocol](docsource/images/IISU-entry-parameters-store-type-dialog-Protocol.png)
470+
471+
472+
##### ProviderName
473+
474+
![IISU Entry Parameter - ProviderName](docsource/images/IISU-entry-parameters-store-type-dialog-ProviderName.png)
475+
476+
477+
##### SAN
478+
479+
![IISU Entry Parameter - SAN](docsource/images/IISU-entry-parameters-store-type-dialog-SAN.png)
480+
481+
482+
429483
</details>
430484
</details>
431485

@@ -549,6 +603,23 @@ the Keyfactor Command Portal
549603

550604
![WinSql Entry Parameters Tab](docsource/images/WinSql-entry-parameters-store-type-dialog.png)
551605

606+
607+
##### InstanceName
608+
609+
![WinSql Entry Parameter - InstanceName](docsource/images/WinSql-entry-parameters-store-type-dialog-InstanceName.png)
610+
611+
612+
##### ProviderName
613+
614+
![WinSql Entry Parameter - ProviderName](docsource/images/WinSql-entry-parameters-store-type-dialog-ProviderName.png)
615+
616+
617+
##### SAN
618+
619+
![WinSql Entry Parameter - SAN](docsource/images/WinSql-entry-parameters-store-type-dialog-SAN.png)
620+
621+
622+
552623
</details>
553624
</details>
554625

-86 Bytes
Loading
2 Bytes
Loading
26.6 KB
Loading
27.7 KB
Loading
23.5 KB
Loading
27.5 KB
Loading
27.8 KB
Loading

0 commit comments

Comments
 (0)