-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcert_manager.bat
352 lines (331 loc) · 10.4 KB
/
cert_manager.bat
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
:: Certificate Manager Script for WebCTRL
:: Version: 1.0.0
:: Repository: https://github.com/automatic-controls/cert-manager-for-webctrl
:: Contributors: Cameron Vogt (@cvogt729)
:: BSD 3-Clause License
::
:: Copyright (c) 2022, Automatic Controls Equipment Systems, Inc.
:: All rights reserved.
::
:: Redistribution and use in source and binary forms, with or without
:: modification, are permitted provided that the following conditions are met:
::
:: 1. Redistributions of source code must retain the above copyright notice, this
:: list of conditions and the following disclaimer.
::
:: 2. Redistributions in binary form must reproduce the above copyright notice,
:: this list of conditions and the following disclaimer in the documentation
:: and/or other materials provided with the distribution.
::
:: 3. Neither the name of the copyright holder nor the names of its
:: contributors may be used to endorse or promote products derived from
:: this software without specific prior written permission.
::
:: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
:: AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
:: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
:: DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
:: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
:: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
:: SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
:: CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
:: OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
:: OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@echo off
title Certificate Manager for WebCTRL
setlocal EnableDelayedExpansion
cd "%~dp0"
set "keystore=%~dp0certkeys"
set "obfuscate=%~dp0Obfuscate.js"
call :normalizePath WebCTRL "%~dp0..\.."
set "keytool=%WebCTRL%\bin\java\jre\bin\keytool.exe"
set "settings=%WebCTRL%\resources\properties\settings.properties"
set "config=%WebCTRL%\webserver\conf\server.xml"
%= Locate keytool utility =%
if not exist "%keytool%" (
echo.
echo Error - Unable to locate necessary files.
echo Please ensure this script is placed in the appropriate folder.
echo e.g. %HOMEDRIVE%\WebCTRL8.0\webserver\keystores
echo.
echo Press any key to exit...
pause >nul
exit
)
if exist "%keystore%" (
echo Recovering password...
call :recoverPassword
"%keytool%" -keystore "%keystore%" -storepass "!password!" -list >nul 2>nul
if %ERRORLEVEL% NEQ 0 (
call :passwordPrompt
)
echo Cleaning keystore...
call :clear
) else (
echo Enter a password for the new keystore.
echo DO NOT USE SPECIAL CHARACTERS ^"^&^^!%%;^?
set /p "password=>"
cls
call :save
)
%= Define various commands =%
set "commands[1]=help"
set "commands[2]=generate"
set "commands[3]=request"
set "commands[4]=import"
set "commands[5]=export"
set "commands=5"
%= Primary command loop =%
:main
cls
echo.
echo Certificate Manager for WebCTRL
echo.
echo Type 'help' for a list of commands.
echo.
:loop
set "cmd="
set /p "cmd=>"
for /f "tokens=1,* delims= " %%a in ("!cmd!") do (
if /i "%%a" EQU "cls" (
if "%%b" EQU "" (
goto :main
) else (
echo Unexpected parameter.
)
) else (
set "exists=0"
for /l %%i in (1,1,%commands%) do (
if /i "!commands[%%i]!" EQU "%%a" set "exists=1"
)
if "!exists!" EQU "1" (
call :!cmd!
) else (
"%keytool%" -keystore "%keystore%" -storepass "!password!" !cmd!
)
)
)
goto loop
:help
if "%*" NEQ "" (
echo Unexpected parameter.
exit /b
)
echo.
echo Online documentation can be found at
echo https://github.com/automatic-controls/cert-manager-for-webctrl/blob/main/README.md
echo.
echo CLS Clear the terminal.
echo HELP Display this message.
echo GENERATE Generate a new 2048bit RSA key-pair.
echo REQUEST Create a certificate signing request.
echo IMPORT [file] Import the certificate reply chain.
echo EXPORT Export the primary certificate for inspection.
echo.
echo All other commands are passed to keytool.
echo Type '--help' for a list of valid keytool switches.
echo Note the '-keystore' and '-storepass' switches are auto-populated.
echo.
exit /b
:import
setlocal
if "%~1" EQU "" (
call :normalizePath file "%~dp0."
) else (
call :normalizePath file "%~dp0%~1"
)
if not exist "%file%" (
if exist "%~1" (
set "file=%~1"
) else (
echo The specified file does not exist.
exit /b
)
)
if exist "%file%\*" (
set /a cert=0
for /r "%file%" %%i in (*.*) do (
set /a cert+=1
"%keytool%" -keystore "%keystore%" -storepass "!password!" -importcert -alias tmp!cert! -file "%%i" -noprompt >nul 2>nul
)
set success=0
for /r "%file%" %%i in (*.*) do (
if !success! EQU 0 (
"%keytool%" -keystore "%keystore%" -storepass "!password!" -importcert -alias webctrl -file "%%i" -noprompt >nul 2>nul
if !ERRORLEVEL! EQU 0 set success=1
)
)
call :clear
if !success! EQU 0 (
echo An error has occurred.
) else (
echo Certificate reply imported successfully.
)
) else (
"%keytool%" -keystore "%keystore%" -storepass "!password!" -importcert -alias webctrl -file "%file%" -noprompt
if %ERRORLEVEL% NEQ 0 echo An error has occurred.
)
exit /b
:generate
if "%*" NEQ "" (
echo Unexpected parameter.
exit /b
)
"%keytool%" -keystore "%keystore%" -storepass "!password!" -delete -alias webctrl >nul 2>nul
setlocal
echo.
echo Example: www.somename.net
echo Common Name ^(CN^):
set /p "CN=>"
echo.
echo Example: IT
echo Organizational Unit ^(OU^):
set /p "OU=>"
echo.
echo Example: Automatic Controls Equipment Systems
echo Organization ^(O^):
set /p "O=>"
echo.
echo Example: Fenton
echo Locality ^(L^):
set /p "L=>"
echo.
echo Example: MO
echo State or Province (ST):
set /p "ST=>"
echo.
echo Example: US
echo Country ^(C^):
set /p "C=>"
echo.
echo Example:
echo dns:www.somename.net,ip:1.1.1.1
echo If left blank, SAN will default to:
echo dns:!CN!
echo Subject Alternative Name ^(SAN^):
set /p "SAN=>"
echo.
if "!SAN!" EQU "" set "SAN=dns:!CN!"
set "CN=!CN:,=\,!"
set "OU=!OU:,=\,!"
set "O=!O:,=\,!"
set "L=!L:,=\,!"
set "ST=!ST:,=\,!"
set "C=!C:,=\,!"
"%keytool%" -keystore "%keystore%" -storepass "!password!" -genkeypair -alias webctrl -keyalg RSA -sigalg SHA256withRSA -keysize 2048 -keypass "!password!" -validity 3650 -ext "san=!SAN!" -dname "CN=!CN!, OU=!OU!, O=!O!, L=!L!, ST=!ST!, C=!C!"
if %ERRORLEVEL% EQU 0 (
echo Key-pair generated successfully.
) else (
echo An error has occurred.
)
echo.
endlocal
exit /b
:request
if "%*" NEQ "" (
echo Unexpected parameter.
exit /b
)
"%keytool%" -keystore "%keystore%" -storepass "!password!" -alias webctrl -certreq -file "%~dp0request.csr" >nul 2>nul
if %ERRORLEVEL% EQU 0 (
echo %~dp0request.csr
) else (
echo An error has occurred.
)
exit /b
:export
if "%*" NEQ "" (
echo Unexpected parameter.
exit /b
)
"%keytool%" -keystore "%keystore%" -storepass "!password!" -exportcert -alias webctrl -file "%~dp0WebCTRL.cer" >nul 2>nul
if %ERRORLEVEL% EQU 0 (
echo %~dp0WebCTRL.cer
) else (
echo An error has occurred.
)
exit /b
%= Delete extraneous aliases =%
:clear
for /f "tokens=2,* delims= " %%i in ('""%keytool%" -keystore "%keystore%" -storepass "!password!" -list -rfc ^| findstr /C:"Alias name: ""') do (
if "%%j" NEQ "webctrl" (
"%keytool%" -keystore "%keystore%" -storepass "!password!" -delete -alias "%%j" >nul 2>nul
)
)
exit /b
%= Resolves relative paths to fully qualified path names. =%
:normalizePath
set "%~1=%~f2"
exit /b
%= Prompt the user for the keystore password =%
:passwordPrompt
echo.
echo Enter the keystore password.
echo DO NOT USE SPECIAL CHARACTERS ^"^&^^!%%;^?
set /p "password=>"
cls
echo Checking password...
"%keytool%" -keystore "%keystore%" -storepass "!password!" -list >nul 2>nul
if %ERRORLEVEL% NEQ 0 (
cls
echo Incorrect password.
goto :passwordPrompt
)
call :save
exit /b
%= Obfuscates text =%
%= First parameter - name of variable to store result =%
%= Second parameter - name of variable which has text to obfuscate =%
:obfuscate
(
echo var x = WScript.Arguments^(0^)
echo var y = ^"^"
echo for ^(var i=x.length-1;i^>=0;--i^){
echo y+=String.fromCharCode^(x.charCodeAt^(i^)^^4^)
echo }
echo WScript.Echo^(y^)
) > "%obfuscate%"
for /f "tokens=* delims=" %%i in ('cscript //nologo //E:jscript "%obfuscate%" "!%~2!"') do (
setlocal DisableDelayedExpansion
set "tmpVar=%%i"
)
(
endlocal
set "%~1=%tmpVar%"
)
del /F "%obfuscate%" >nul 2>nul
exit /b
:save
echo Configuring WebCTRL...
setlocal
call :obfuscate ob password
set "password=!password:`=``!"
set "password=!password:$=`$!"
set "ob=!ob:`=``!"
set "ob=!ob:$=`$!"
call :echoPS > "%~dp0Utility.ps1"
PowerShell -File "%~dp0Utility.ps1"
del /F "%~dp0Utility.ps1" >nul 2>nul
endlocal
exit /b
:echoPS
echo $Data = (Get-Content "%config%") -replace '(?^<=keystorePass\=").*?(?=")', "!password!" -replace '(?^<=sslEnabledProtocols\=").*?(?=")', 'TLSv1.3' -replace '(?^<=ciphers\=").*?(?=")', 'TLS_AES_256_GCM_SHA384,TLS_AES_128_GCM_SHA256' -replace 'Disabled_Connector', 'Connector'
echo [IO.File]::WriteAllLines('%config%', $Data)
echo $Data = (Get-Content "%settings%") -replace '(?^<=\.keystorepassword\= ).*', "!ob!" -replace '(?^<=\.sslmode\= ).*', 'both' -replace '(?^<=\.tls_mode\= ).*', 'TLSv1_3' -replace '(?^<=\.sslredirect\= ).*', 'true'
echo [IO.File]::WriteAllLines('%settings%', $Data)
exit /b
:recoverPassword
call :echoPassPS > "%~dp0Utility.ps1"
setlocal DisableDelayedExpansion
for /f "tokens=* delims=" %%i in ('PowerShell -File "%~dp0Utility.ps1"') do (
set "pass=%%i"
)
(
endlocal
set "password=%pass%"
)
del /F "%~dp0Utility.ps1" >nul 2>nul
exit /b
:echoPassPS
echo echo([Regex]::Match((Get-Content "%config%"), '(?^<=keystorePass=").*?(?=")').Value)
exit /b