Skip to content

Commit 56127b1

Browse files
author
Bob Pokorny
committed
#ab73289 Resolved issues parsing Distinguished Name subject string and properly quotes the RDN values containing escaped commas.
1 parent 813133f commit 56127b1

File tree

2 files changed

+111
-1
lines changed

2 files changed

+111
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2.6.3
2+
* Fixed reenrollment job when RDN Components contained escaped commas
3+
14
2.6.2
25
* Fixed error when attempting to connect to remote computer using UO service account
36
* Fixed error when connecting to remote computer using HTTPS; was defaulting to HTTP

IISU/PowerShellScripts/WinCertScripts.ps1

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1166,6 +1166,9 @@ function New-CSREnrollment {
11661166
# Validate the Crypto Service Provider
11671167
Validate-CryptoProvider -ProviderName $ProviderName
11681168

1169+
# Parse Subject for any escaped commas
1170+
$parsedSubject = Parse-DNSubject $SubjectText
1171+
11691172
# Build the SAN entries if provided
11701173
$sanContent = ""
11711174
if ($SAN) {
@@ -1184,7 +1187,7 @@ $($sanDirectives -join "`n")
11841187
Signature=`"$`Windows NT$`"
11851188
11861189
[NewRequest]
1187-
Subject = "$SubjectText"
1190+
Subject = "$parsedSubject"
11881191
ProviderName = "$ProviderName"
11891192
MachineKeySet = True
11901193
HashAlgorithm = SHA256
@@ -1402,4 +1405,108 @@ function Validate-CryptoProvider {
14021405
}
14031406

14041407
Write-Verbose "Crypto Service Provider '$ProviderName' is valid."
1408+
}
1409+
1410+
function Parse-DNSubject {
1411+
<#
1412+
.SYNOPSIS
1413+
Parses a Distinguished Name (DN) subject string and properly quotes RDN values containing escaped commas.
1414+
1415+
.DESCRIPTION
1416+
This function takes a DN subject string and parses the Relative Distinguished Name (RDN) components,
1417+
adding proper quotes around values that contain escaped commas and escaping quotes for use in
1418+
PowerShell here-strings. Only RDN values with escaped commas get quoted.
1419+
1420+
.PARAMETER Subject
1421+
The DN subject string to parse (e.g., "CN=Keyfactor,O=Keyfactor\, Inc")
1422+
1423+
.EXAMPLE
1424+
Parse-DNSubject -Subject "CN=Keyfactor,O=Keyfactor\, Inc"
1425+
Returns: CN=Keyfactor,O=""Keyfactor, Inc""
1426+
1427+
.EXAMPLE
1428+
Parse-DNSubject -Subject "CN=Test User,O=Company\, LLC,OU=IT Department\, Security"
1429+
Returns: CN=Test User,O=""Company, LLC"",OU=""IT Department, Security""
1430+
#>
1431+
1432+
[CmdletBinding()]
1433+
param(
1434+
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
1435+
[string]$Subject
1436+
)
1437+
1438+
# Initialize variables
1439+
$parsedComponents = @()
1440+
$currentComponent = ""
1441+
$i = 0
1442+
1443+
# Convert string to character array for easier parsing
1444+
$chars = $Subject.ToCharArray()
1445+
1446+
while ($i -lt $chars.Length) {
1447+
$char = $chars[$i]
1448+
1449+
# Check if we hit a comma
1450+
if ($char -eq ',') {
1451+
# Look back to see if it's escaped
1452+
$isEscaped = $false
1453+
if ($i -gt 0 -and $chars[$i-1] -eq '\') {
1454+
$isEscaped = $true
1455+
}
1456+
1457+
if ($isEscaped) {
1458+
# This is an escaped comma, add it to current component
1459+
$currentComponent += $char
1460+
} else {
1461+
# This is a separator comma, finish current component
1462+
if ($currentComponent.Trim() -ne "") {
1463+
$parsedComponents += $currentComponent.Trim()
1464+
$currentComponent = ""
1465+
}
1466+
}
1467+
} else {
1468+
# Regular character, add to current component
1469+
$currentComponent += $char
1470+
}
1471+
1472+
$i++
1473+
}
1474+
1475+
# Add the last component
1476+
if ($currentComponent.Trim() -ne "") {
1477+
$parsedComponents += $currentComponent.Trim()
1478+
}
1479+
1480+
# Process each component to add quotes where needed
1481+
$processedComponents = @()
1482+
1483+
foreach ($component in $parsedComponents) {
1484+
# Split on first equals sign to get attribute and value
1485+
$equalIndex = $component.IndexOf('=')
1486+
if ($equalIndex -gt 0) {
1487+
$attribute = $component.Substring(0, $equalIndex).Trim()
1488+
$value = $component.Substring($equalIndex + 1).Trim()
1489+
1490+
# Clean up escaped commas first
1491+
$cleanValue = $value -replace '\\,', ','
1492+
1493+
# Check if original value had escaped commas (needs quotes)
1494+
if ($value -match '\\,') {
1495+
# This RDN value had escaped commas, so wrap in doubled quotes and escape quotes
1496+
$escapedValue = $cleanValue -replace '"', '""'
1497+
$processedComponents += "$attribute=`"`"$escapedValue`"`""
1498+
} else {
1499+
# No escaped commas, keep as simple value but escape any existing quotes
1500+
$escapedValue = $cleanValue -replace '"', '""'
1501+
$processedComponents += "$attribute=$escapedValue"
1502+
}
1503+
} else {
1504+
# Invalid component format, keep as is
1505+
$processedComponents += $component
1506+
}
1507+
}
1508+
1509+
# Join components back together (no outer quotes needed since it goes in PowerShell string)
1510+
$subjectString = ($processedComponents -join ',')
1511+
return $subjectString
14051512
}

0 commit comments

Comments
 (0)