-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPowerCSR_V1.ps1
More file actions
245 lines (209 loc) · 10.5 KB
/
PowerCSR_V1.ps1
File metadata and controls
245 lines (209 loc) · 10.5 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
######################################################################################
# PowerCSR V1.0 - Released 23/01/2024 #
# #
# Script Created by ReproDev: https://https://github.com/reprodev/PowerCSR/ #
# Released Under MIT Licence #
# Check out other projects : https://github.com/reprodev/ #
# Why not buy me a coffee? : https://ko-fi.com/reprodev #
# #
######################################################################################
# Load Windows Forms
Add-Type -AssemblyName System.Windows.Forms
# Create the form
$form = New-Object System.Windows.Forms.Form
$form.Text = 'PowerCSR V1.0'
$form.Size = New-Object System.Drawing.Size(650,700) # Increased size for SAN field
# Function to add a label and textbox
function Add-InputField($form, $labelText, $position, $isPassword) {
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(15, $position)
$label.Size = New-Object System.Drawing.Size(180, 40) # Increased size
$label.Text = $labelText
$label.Font = New-Object System.Drawing.Font("Arial", 12) # Increased font size for label
$form.Controls.Add($label)
$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(195, $position)
$textBox.Size = New-Object System.Drawing.Size(395, 30) # Increased size
$textBox.Font = New-Object System.Drawing.Font("Arial", 12) # Increased font size for input box
if ($isPassword) { $textBox.UseSystemPasswordChar = $true }
$form.Controls.Add($textBox)
return $textBox
}
$commonName = Add-InputField $form 'Common Name (CN):' 30 $false
$organization = Add-InputField $form 'Organization (O):' 75 $false
$organizationalUnit = Add-InputField $form 'Organizational Unit (OU):' 120 $false
$country = Add-InputField $form 'Country (C):' 170 $false
$state = Add-InputField $form 'State (ST):' 215 $false
$locality = Add-InputField $form 'Locality (L):' 260 $false
# $email = Add-InputField $form 'Email (Email):' 305 $false
$sanLabel = New-Object System.Windows.Forms.Label
$sanLabel.Location = New-Object System.Drawing.Point(15, 305)
$sanLabel.Size = New-Object System.Drawing.Size(180, 40)
$sanLabel.Text = 'SAN (Optional):'
$sanLabel.Font = New-Object System.Drawing.Font("Arial", 12)
$form.Controls.Add($sanLabel)
$sanTextBox = New-Object System.Windows.Forms.TextBox
$sanTextBox.Location = New-Object System.Drawing.Point(195, 305)
$sanTextBox.Size = New-Object System.Drawing.Size(395, 30)
$sanTextBox.Font = New-Object System.Drawing.Font("Arial", 12)
$sanTextBox.Text = ''
$form.Controls.Add($sanTextBox)
$sanHelpLabel = New-Object System.Windows.Forms.Label
$sanHelpLabel.Location = New-Object System.Drawing.Point(195, 335)
$sanHelpLabel.Size = New-Object System.Drawing.Size(395, 40)
$sanHelpLabel.Text = 'Format: DNS:example.com,DNS:www.example.com,IP:1.2.3.4'
$sanHelpLabel.Font = New-Object System.Drawing.Font("Arial", 8, [System.Drawing.FontStyle]::Italic)
$sanHelpLabel.ForeColor = [System.Drawing.Color]::Gray
$form.Controls.Add($sanHelpLabel)
$password1 = Add-InputField $form 'Password:' 395 $true
$password2 = Add-InputField $form 'Confirm Password:' 440 $true
# Auto-Fill Function
function Auto-FillDomainInfo {
try {
# Get Windows geographic region country code from registry (only if country field is empty)
if ([string]::IsNullOrWhiteSpace($country.Text)) {
try {
$geoKey = Get-ItemProperty -Path "HKCU:\Control Panel\International\Geo" -ErrorAction SilentlyContinue
if ($geoKey -and $geoKey.Nation) {
# Get the GeoId and convert it using .NET
$geoId = $geoKey.Nation
$cultures = [System.Globalization.CultureInfo]::GetCultures([System.Globalization.CultureTypes]::SpecificCultures)
foreach ($culture in $cultures) {
$region = New-Object System.Globalization.RegionInfo($culture.Name)
if ($region.GeoId -eq $geoId) {
$country.Text = $region.TwoLetterISORegionName
break
}
}
}
} catch {
# Fallback to current region if registry lookup fails
try {
$regionInfo = [System.Globalization.RegionInfo]::CurrentRegion
$country.Text = $regionInfo.TwoLetterISORegionName
} catch {
# Silent fail for country
}
}
}
# Get domain information (only populate if fields are empty)
$hostname = [System.Net.Dns]::GetHostName()
$domainName = [System.Net.NetworkInformation.IPGlobalProperties]::GetIPGlobalProperties().DomainName
if ($domainName) {
$fqdn = "$hostname.$domainName"
if ([string]::IsNullOrWhiteSpace($commonName.Text)) {
$commonName.Text = $fqdn
}
# Build SAN entries
if ([string]::IsNullOrWhiteSpace($sanTextBox.Text)) {
$sanEntries = @()
$sanEntries += "DNS:$fqdn"
$sanEntries += "DNS:$hostname"
if ($domainName) {
$sanEntries += "DNS:$domainName"
}
# Get local IP addresses (excluding loopback)
$ipAddresses = [System.Net.Dns]::GetHostAddresses($hostname) |
Where-Object { $_.AddressFamily -eq 'InterNetwork' -and $_.IPAddressToString -ne '127.0.0.1' }
foreach ($ip in $ipAddresses) {
$sanEntries += "IP:$($ip.IPAddressToString)"
}
$sanTextBox.Text = $sanEntries -join ','
}
} else {
if ([string]::IsNullOrWhiteSpace($commonName.Text)) {
$commonName.Text = $hostname
}
if ([string]::IsNullOrWhiteSpace($sanTextBox.Text)) {
$sanTextBox.Text = "DNS:$hostname"
}
}
} catch {
[System.Windows.Forms.MessageBox]::Show("Error auto-filling: $_", 'Error', [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
}
}
# SAN Validation Function
function Validate-SAN {
param([string]$sanInput)
if ([string]::IsNullOrWhiteSpace($sanInput)) {
return @{ Valid = $true; Message = '' }
}
$sanEntries = $sanInput -split ','
$validPrefixes = @('DNS:', 'IP:', 'email:', 'URI:')
foreach ($entry in $sanEntries) {
$entry = $entry.Trim()
if ([string]::IsNullOrWhiteSpace($entry)) {
return @{ Valid = $false; Message = 'Empty SAN entry detected. Remove extra commas.' }
}
$hasValidPrefix = $false
foreach ($prefix in $validPrefixes) {
if ($entry.StartsWith($prefix, [System.StringComparison]::OrdinalIgnoreCase)) {
$hasValidPrefix = $true
$value = $entry.Substring($prefix.Length)
if ([string]::IsNullOrWhiteSpace($value)) {
return @{ Valid = $false; Message = "SAN entry '$entry' has no value after prefix." }
}
break
}
}
if (-not $hasValidPrefix) {
return @{ Valid = $false; Message = "Invalid SAN entry: '$entry'. Must start with DNS:, IP:, email:, or URI:" }
}
}
return @{ Valid = $true; Message = '' }
}
# Create Auto-Fill button
$autoFillButton = New-Object System.Windows.Forms.Button
$autoFillButton.Location = New-Object System.Drawing.Point(50,595)
$autoFillButton.Size = New-Object System.Drawing.Size(150,30)
$autoFillButton.Text = 'Auto-Fill'
$autoFillButton.Font = New-Object System.Drawing.Font("Arial", 12)
# Add click event for Auto-Fill
$autoFillButton.Add_Click({
Auto-FillDomainInfo
})
$form.Controls.Add($autoFillButton)
# Create Generate CSR button
$button = New-Object System.Windows.Forms.Button
$button.Location = New-Object System.Drawing.Point(225,595)
$button.Size = New-Object System.Drawing.Size(150,30)
$button.Text = 'Generate CSR'
$button.Font = New-Object System.Drawing.Font("Arial", 12)
# Add click event for CSR generation
$button.Add_Click({
if ($password1.Text -ne $password2.Text) {
[System.Windows.Forms.MessageBox]::Show('Passwords do not match', 'Error', [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
return
}
# Validate SAN if provided
$sanValidation = Validate-SAN -sanInput $sanTextBox.Text
if (-not $sanValidation.Valid) {
[System.Windows.Forms.MessageBox]::Show($sanValidation.Message, 'SAN Validation Error', [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
return
}
$subjectParts = @()
if ($commonName.Text) { $subjectParts += "CN=$($commonName.Text)" }
if ($organization.Text) { $subjectParts += "O=$($organization.Text)" }
if ($organizationalUnit.Text) { $subjectParts += "OU=$($organizationalUnit.Text)" }
if ($country.Text) { $subjectParts += "C=$($country.Text)" }
if ($state.Text) { $subjectParts += "ST=$($state.Text)" }
if ($locality.Text) { $subjectParts += "L=$($locality.Text)" }
$subjectString = $subjectParts -join '/'
# Constructing the OpenSSL command
$cmd = "openssl req -new -newkey rsa:2048 -nodes -keyout mykey.key -out mycsr.csr " +
"-passout pass:$($password1.Text) -subj '/$subjectString'"
# Add SAN extension if provided
if (-not [string]::IsNullOrWhiteSpace($sanTextBox.Text)) {
$sanValue = $sanTextBox.Text.Trim()
$cmd += " -addext 'subjectAltName=$sanValue'"
}
try {
Invoke-Expression $cmd
[System.Windows.Forms.MessageBox]::Show('CSR Generation Complete', 'Success', [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Information)
} catch {
[System.Windows.Forms.MessageBox]::Show("An error occurred: $_", 'Error', [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
}
})
$form.Controls.Add($button)
#Show the form
$form.ShowDialog()