-
Notifications
You must be signed in to change notification settings - Fork 3
/
CASignCSR.bat
155 lines (141 loc) · 3.93 KB
/
CASignCSR.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
@echo off
setLocal EnableDelayedExpansion
Rem
Rem <b>CASignCSR</b> command file.
Rem @author Jack D. Pond
Rem @version 0.2 / Windows Batch Processor
Rem @see https://github.com/jdpond/WinCertUtilities/wiki and http://pki-tutorial.readthedocs.org/en/latest/index.html#
Rem @description Sign a CSR creating an x.509 (.crt) certificate .
Rem @param CA_SIGN_NAME - Name of the certificate corresponding to directory and CA_SIGN_NAMEs
call "etc/CertConfig.bat"
:PickCA_SIGN_NAME
if "%1" NEQ "" (
set CA_SIGN_NAME=%1
set CA_SIGN_NAME=%CA_SIGN_NAME:"=%
if exist "!CA_SIGN_NAME!\!CA_SIGN_NAME!\db" goto :ValidCAName
)
FOR /F "usebackq delims=" %%i in (`dir /B/AD`) do (
if exist "%%i\pending_rqsts\*.csr" (
set /a DirCount += 1
if !DirCount! GTR 1 Set DirNames=!DirNames!,
Set DirNames=!DirNames!%%i
)
)
if not defined DirCount (
echo.
echo You have no more pending CSR requests
echo To set up a CA, you may want to use the CA INfrastruction Creation Tool^(CreateCAInfrastructure^).
echo If you have a CA set up, you may need to copy your CSR into the appropriate "pending_rqsts" directory.
echo.
pause
goto :eof
)
if !DirCount! == 1 (
set Picked_Name=!DirNames!
goto :ValidCAName
) else (
call :parsenames "!DirNames!" 1
set /p CertID=With which Certificate Authority do you wish to sign a certificate ^(by number^)[or q to quit]?:
if "!CertID!" == "q" goto :eof
)
if !CertID! GTR 0 if !CertID! LEQ !DirCount! (
call :picklist "!DirNames!" !CertID! 1
) else (
echo.
echo Invalid Selection, must be 1-!DirCount!
echo.
goto :PickCA_SIGN_NAME
)
:ValidCAName
Set CA_SIGN_NAME=!Picked_Name!
:GetValidCertName
set /a DirCount = 0
Set DirNames=
FOR /F "usebackq delims=" %%i in (`dir /B "!CA_SIGN_NAME!\pending_rqsts\*.csr"`) do (
set /a DirCount += 1
if !DirCount! GTR 1 Set DirNames=!DirNames!,
Set DirNames=!DirNames!%%~ni
)
if !DirCount! == 1 (
set Picked_Name=!DirNames!
goto :ValidCertName
) else (
call :parsenames "!DirNames!" 1
set /p CertID=With which Certificate do you wish to sign ^(by number^)[or q to quit]?:
if "!CertID!" == "q" goto :eof
)
if !CertID! GTR 0 if !CertID! LEQ !DirCount! (
call :picklist "!DirNames!" !CertID! 1
) else (
echo.
echo Invalid Selection, must be 1-!DirCount!
echo.
goto :GetValidCertName
)
:ValidCertName
Set CertName=!Picked_Name!
:GetValidConfName
set /a DirCount = 0
Set DirNames=
FOR /F "usebackq delims=" %%i in (`dir /B "!CA_SIGN_NAME!\etc\CAConfigurations\*.conf"`) do (
set /a DirCount += 1
if !DirCount! GTR 1 Set DirNames=!DirNames!,
Set DirNames=!DirNames!%%i
)
if !DirCount! == 1 (
set Picked_Name=!DirNames!
goto :ValidConfName
) else (
call :parsenames "!DirNames!" 1
set /p CertID=With which CA Certificate Configuration do you wish to sign with ^(by number^)[or q to quit]?:
if "!CertID!" == "q" goto :eof
)
if !CertID! GTR 0 if !CertID! LEQ !DirCount! (
call :picklist "!DirNames!" !CertID! 1
) else (
echo.
echo Invalid Selection, must be 1-!DirCount!
echo.
goto :GetValidConfName
)
:ValidConfName
Rem
Rem Actually performs signature here
Rem
echo Certificate Authority !CA_SIGN_NAME! Certificate: !CertName! Conf: !Picked_Name!
set CA_NAME=!CA_SIGN_NAME!
"%OpenSSLExe%" ca -config "!CA_SIGN_NAME!/etc/CAConfigurations/!Picked_Name!" -in "!CA_SIGN_NAME!/pending_rqsts/!CertName!.csr" -out "!CA_SIGN_NAME!/certs/!CertName!.crt"
rem Then move from pending_rqsts to rqsts
move "!CA_SIGN_NAME!\pending_rqsts\!CertName!.csr" "!CA_SIGN_NAME!\rqsts\!CertName!.csr"
pause
goto :eof
:parsenames
set list=%1
set list=%list:"=%
set NextNum=%2
FOR /f "tokens=1* delims=," %%a IN ("%list%") DO (
if not "%%a" == "" echo %2^) %%a
if not "%%b" == "" (
set /a NextNum=%2+1
call :parsenames "%%b" !NextNum!
)
)
exit /b
:printname
echo %2^) %1
exit /b
:picklist
set list=%1
set list=%list:"=%
set NextNum=%3
FOR /f "tokens=1* delims=," %%a IN ("%list%") DO (
if !NextNum! == %2 (
Set Picked_Name=%%a
exit /b
)
if not "%%b" == "" (
set /a NextNum += 1
call :picklist "%%b" %2 !NextNum!
)
)
exit /b