-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSet-SANExtension.ps1
96 lines (74 loc) · 3.83 KB
/
Set-SANExtension.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
[cmdletbinding()]
param(
[Parameter(Mandatory = $True)]
[ValidateNotNullOrEmpty()]
[String[]]
$ConfigStrings,
[Parameter(Mandatory = $True)]
[ValidateNotNullOrEmpty()]
[String[]]
$CertificateTemplates
)
begin {
$Script:BaseDirectory = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
# Loading all Libary Scripts we depend on
Get-ChildItem -Path "$Script:BaseDirectory\lib" -Filter *.ps1 | ForEach-Object {
. ($_.FullName)
}
New-Variable -Option Constant -Name XCN_CRYPT_STRING_BASE64_ANY -Value 0x6
New-Variable -Option Constant -Name szOID_SUBJECT_ALT_NAME2 -Value 2.5.29.17
New-Variable -Option Constant -Name REQUESTTYPE_PKCS10 -Value 0x40100
New-Variable -Option Constant -Name REQUESTTYPE_PKCS7 -Value 0x40300
New-Variable -Option Constant -Name REQUESTTYPE_CMC -Value 0x40400
}
process {
ForEach ($ConfigString in $ConfigStrings) {
ForEach ($CertificateTemplate in $CertificateTemplates) {
Get-CADatabaseRecord `
-ConfigString $ConfigString `
-Disposition Pending `
-CertificateTemplate $CertificateTemplate `
-Properties RequestId,Request.RequestType,Request.RawRequest | ForEach-Object -Process {
$CurrentRow = $_
switch ($CurrentRow."Request.Requesttype") {
$REQUESTTYPE_PKCS7 { $RequestObject = New-Object -ComObject X509Enrollment.CX509CertificateRequestPkcs7 }
$REQUESTTYPE_CMC { $RequestObject = New-Object -ComObject X509Enrollment.CX509CertificateRequestCmc }
default { $RequestObject = New-Object -ComObject X509Enrollment.CX509CertificateRequestPkcs10 }
}
Try {
$RequestObject.InitializeDecode(
$CurrentRow."Request.RawRequest",
$XCN_CRYPT_STRING_BASE64_ANY
)
}
Catch {
Write-Warning -Message "Unable to decode Request $($CurrentRow.RequestId). Skipping."
return
}
switch ($CurrentRow."Request.Requesttype") {
$REQUESTTYPE_PKCS7 { $Pkcs10Object = $RequestObject.GetInnerRequest(1) }
$REQUESTTYPE_CMC { $Pkcs10Object = $RequestObject.GetInnerRequest(1) }
default { $Pkcs10Object = $RequestObject }
}
If (-not ($Pkcs10Object.x509extensions | Where-Object {$_.Objectid.Value -eq $szOID_SUBJECT_ALT_NAME2})) {
Write-Verbose -Message "Request $($CurrentRow.RequestId) on $ConfigString does not have a SAN Extension."
Try {
$DistinguishedName = $Pkcs10Object.Subject.Name
}
Catch {
Write-Warning -Message "Request $($CurrentRow.RequestId) on $ConfigString seems to neither have a Subject nor a SAN Extension. Skipping."
return
}
# Extracting the Common Name from the Distinguished Name
$RegEx = '(?<=CN=)([^,]+)'
If ($DistinguishedName -match $RegEx) {
$DnsName = $Matches[0]
Write-Output "Adding DnsName SAN Extension for $DnsName to Request $($CurrentRow.RequestId) on $ConfigString."
Add-SANCertificateExtension -ConfigString $ConfigString -RequestId $CurrentRow.RequestID -DnsName $DnsName
}
}
}
}
}
}
end {}