-
Notifications
You must be signed in to change notification settings - Fork 17
/
ps-certreq.ps1
113 lines (95 loc) · 4.08 KB
/
ps-certreq.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<#
script to generate a test self-signed certificate for use with testing rds deployments
to enable script execution, you may need to Set-ExecutionPolicy Bypass -Force
From <https://blog.kloud.com.au/2013/07/30/ssl-san-certificate-request-and-import-from-powershell/>
New-CertificateRequest -subject mail1.showcase.kloud.com.au
New-CertificateRequest -subject *.contoso.com
New-CertificateRequest -subject remote.contoso.com -sans @("broker.contoso.com","broker.contoso.lab")
#>
param(
[string]$pfxPassword = "",
[string]$subject = "", #"*.contoso.com",
[string[]]$sans = @(),
[string]$onlineCa = "",
[string]$outputDir = (get-location)
)
function New-CertificateRequest
{
param (
[ValidatePattern("CN=")][string]$subject,
[string[]]$SANs,
[string]$outputDir,
[string]$pfxPassword,
[string]$OnlineCA = "",
[string]$CATemplate = "WebServer"
)
### Preparation
$subjectDomain = $subject.split(',')[0].split('=')[1]
if ($subjectDomain -match "\*.")
{
$subjectDomain = $subjectDomain -replace "\*", "star"
}
$CertificateINI = "$($outputDir)\$($subjectDomain).ini"
$CertificateREQ = "$($outputDir)\$($subjectDomain).req"
$CertificateRSP = "$($outputDir)\$($subjectDomain).rsp"
$CertificateCER = "$($outputDir)\$($subjectDomain).cer"
$CertificatePFX = "$($outputDir)\$($subjectDomain).pfx"
### INI file generation
new-item -type file $CertificateINI -force
add-content $CertificateINI '[Version]'
add-content $CertificateINI 'Signature="$Windows NT$"'
add-content $CertificateINI ''
add-content $CertificateINI '[NewRequest]'
add-content $CertificateINI ('Subject="' + $subject + '"')
add-content $CertificateINI 'exportable=TRUE'
add-content $CertificateINI 'KeyLength=2048'
add-content $CertificateINI 'KeySpec=1'
add-content $CertificateINI 'KeyUsage=0x30'
add-content $CertificateINI 'MachineKeySet=True'
add-content $CertificateINI 'ProviderName="Microsoft RSA SChannel Cryptographic Provider"'
add-content $CertificateINI 'ProviderType=12'
add-content $CertificateINI 'SMIME=FALSE'
### Date Ranges
add-content $CertificateINI ('NotBefore="' + (get-date).ToShortDateString() + '"')
### Expire in 5 years
add-content $CertificateINI ('NotAfter="' + (get-date).AddYears(5).ToShortDateString() + '"')
add-content $CertificateINI 'RequestType=Cert'
add-content $CertificateINI 'HashAlgorithm=sha256'
add-content $CertificateINI '[EnhancedKeyUsageExtension]'
add-content $CertificateINI 'OID=1.3.6.1.5.5.7.3.1 ; this is for Server Authentication / Token Signing'
if ($SANs)
{
add-content $CertificateINI '[Extensions]'
add-content $CertificateINI '2.5.29.17 = "{text}"'
foreach ($SAN in $SANs)
{
add-content $CertificateINI ('_continue_ = "dns=' + $SAN + '&"')
}
}
### Certificate request generation
if (test-path $CertificateREQ) {del $CertificateREQ}
certreq -new $CertificateINI $CertificateREQ
### Online certificate request and import
if ($OnlineCA)
{
if (test-path $CertificateCER) {del $CertificateCER}
if (test-path $CertificateRSP) {del $CertificateRSP}
certreq -submit -attrib "CertificateTemplate:$CATemplate" -config $OnlineCA $CertificateREQ $CertificateCER
certreq -accept $CertificateCER
}
if($pfxPassword)
{
$cleanSubject = $subject.Replace("=","\=").Replace("*","\*")
$SecurePassword = $pfxPassword | ConvertTo-SecureString -AsPlainText -Force
Get-ChildItem -Path cert:\LocalMachine\My -Recurse | where-object Subject -imatch $cleansubject | export-PfxCertificate -Password $securePassword -FilePath $CertificatePFX -Force
$CertificatePFX
}
$CertificateINI
$CertificateREQ
}
if([string]::IsNullOrEmpty($subject))
{
write-host "supply subject name. can be wildcard. ex: *.contoso.com. exiting"
return
}
New-CertificateRequest -subject "CN=$($subject)" -SANs $sans -outputDir $outputDir -pfxpassword $pfxPassword -OnlineCA $onlineCa