]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - winrc/unbound-control-setup.cmd
Vendor import of Unbound 1.6.2.
[FreeBSD/FreeBSD.git] / winrc / unbound-control-setup.cmd
1 @Echo off\r
2 rem\r
3 rem unbound-control-setup.cmd - set up SSL certificates for unbound-control\r
4 rem\r
5 rem Copyright (c) 2008, NLnet Labs. All rights reserved.\r
6 rem Modified for Windows by Y.Voinov (c) 2014\r
7 rem\r
8 rem This software is open source.\r
9 rem \r
10 rem Redistribution and use in source and binary forms, with or without\r
11 rem modification, are permitted provided that the following conditions\r
12 rem are met:\r
13 rem \r
14 rem Redistributions of source code must retain the above copyright notice,\r
15 rem this list of conditions and the following disclaimer.\r
16 rem \r
17 rem Redistributions in binary form must reproduce the above copyright notice,\r
18 rem this list of conditions and the following disclaimer in the documentation\r
19 rem and/or other materials provided with the distribution.\r
20 rem \r
21 rem Neither the name of the NLNET LABS nor the names of its contributors may\r
22 rem be used to endorse or promote products derived from this software without\r
23 rem specific prior written permission.\r
24 rem \r
25 rem THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\r
26 rem "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\r
27 rem LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\r
28 rem A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\r
29 rem HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\r
30 rem SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED\r
31 rem TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\r
32 rem PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\r
33 rem LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\r
34 rem NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r
35 rem SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
36 \r
37 rem settings:\r
38 \r
39 rem directory for files\r
40 set prefix="C:\Program Files"\r
41 set DESTDIR=%prefix%\Unbound\r
42 \r
43 rem issuer and subject name for certificates\r
44 set SERVERNAME=unbound\r
45 set CLIENTNAME=unbound-control\r
46 \r
47 rem validity period for certificates\r
48 set DAYS=7200\r
49 \r
50 rem size of keys in bits\r
51 set BITS=1536\r
52 \r
53 rem hash algorithm\r
54 set HASH=sha256\r
55 \r
56 rem base name for unbound server keys\r
57 set SVR_BASE=unbound_server\r
58 \r
59 rem base name for unbound-control keys\r
60 set CTL_BASE=unbound_control\r
61 \r
62 rem end of options\r
63 \r
64 rem Check OpenSSL installed\r
65 for /f "delims=" %%a in ('where openssl') do @set SSL_PROGRAM=%%a\r
66 if /I "%SSL_PROGRAM%"=="" echo SSL not found. If installed, add path to PATH environment variable. & exit 1\r
67 echo SSL found: %SSL_PROGRAM%\r
68 \r
69 set arg=%1\r
70 if /I "%arg%" == "-h" goto help\r
71 if /I "%arg%"=="-d" set DESTDIR=%2\r
72 \r
73 rem go!:\r
74 echo setup in directory %DESTDIR%\r
75 cd %DESTDIR%\r
76 \r
77 rem create certificate keys; do not recreate if they already exist.\r
78 if exist %SVR_BASE%.key (\r
79 echo %SVR_BASE%.key exists\r
80 goto next\r
81 )\r
82 echo generating %SVR_BASE%.key\r
83 "%SSL_PROGRAM%" genrsa -out %SVR_BASE%.key %BITS% || echo could not genrsa && exit 1\r
84 \r
85 :next\r
86 if exist %CTL_BASE%.key (\r
87 echo %CTL_BASE%.key exists\r
88 goto next2\r
89 )\r
90 echo generating %CTL_BASE%.key\r
91 "%SSL_PROGRAM%" genrsa -out %CTL_BASE%.key %BITS% || echo could not genrsa && exit 1\r
92 \r
93 :next2\r
94 rem create self-signed cert for server\r
95 if exist request.cfg (del /F /Q /S request.cfg)\r
96 echo [req]>>request.cfg\r
97 echo default_bits=%BITS%>>request.cfg\r
98 echo default_md=%HASH%>>request.cfg\r
99 echo prompt=no>>request.cfg\r
100 echo distinguished_name=req_distinguished_name>>request.cfg\r
101 echo.>>request.cfg\r
102 echo [req_distinguished_name]>>request.cfg\r
103 echo commonName=%SERVERNAME%>>request.cfg\r
104 \r
105 if not exist request.cfg (\r
106 echo could not create request.cfg\r
107 exit 1\r
108 )\r
109 \r
110 echo create %SVR_BASE%.pem (self signed certificate)\r
111 "%SSL_PROGRAM%" req -key %SVR_BASE%.key -config request.cfg  -new -x509 -days %DAYS% -out %SVR_BASE%.pem || echo could not create %SVR_BASE%.pem && exit 1\r
112 rem create trusted usage pem\r
113 "%SSL_PROGRAM%" x509 -in %SVR_BASE%.pem -addtrust serverAuth -out %SVR_BASE%_trust.pem\r
114 \r
115 rem create client request and sign it\r
116 if exist request.cfg (del /F /Q /S request.cfg)\r
117 echo [req]>>request.cfg\r
118 echo default_bits=%BITS%>>request.cfg\r
119 echo default_md=%HASH%>>request.cfg\r
120 echo prompt=no>>request.cfg\r
121 echo distinguished_name=req_distinguished_name>>request.cfg\r
122 echo.>>request.cfg\r
123 echo [req_distinguished_name]>>request.cfg\r
124 echo commonName=%CLIENTNAME%>>request.cfg\r
125 \r
126 if not exist request.cfg (\r
127 echo could not create request.cfg\r
128 exit 1\r
129 )\r
130 \r
131 echo create %CTL_BASE%.pem (signed client certificate)\r
132 "%SSL_PROGRAM%" req -key %CTL_BASE%.key -config request.cfg -new | "%SSL_PROGRAM%" x509 -req -days %DAYS% -CA %SVR_BASE%_trust.pem -CAkey %SVR_BASE%.key -CAcreateserial -%HASH% -out %CTL_BASE%.pem\r
133 \r
134 if not exist %CTL_BASE%.pem (\r
135 echo could not create %CTL_BASE%.pem\r
136 exit 1\r
137 )\r
138 rem create trusted usage pem\r
139 rem "%SSL_PROGRAM%" x509 -in %CTL_BASE%.pem -addtrust clientAuth -out %CTL_BASE%_trust.pem\r
140 \r
141 rem see details with "%SSL_PROGRAM%" x509 -noout -text < %SVR_BASE%.pem\r
142 rem echo "create %CTL_BASE%_browser.pfx (web client certificate)"\r
143 rem echo "create webbrowser PKCSrem12 .PFX certificate file. In Firefox import in:"\r
144 rem echo "preferences - advanced - encryption - view certificates - your certs"\r
145 rem echo "empty password is used, simply click OK on the password dialog box."\r
146 rem "%SSL_PROGRAM%" pkcs12 -export -in %CTL_BASE%_trust.pem -inkey %CTL_BASE%.key -name "unbound remote control client cert" -out %CTL_BASE%_browser.pfx -password "pass:" || echo could not create browser certificate && exit 1\r
147 \r
148 rem remove crap\r
149 del /F /Q /S request.cfg\r
150 del /F /Q /S %CTL_BASE%_trust.pem \r
151 del /F /Q /S %SVR_BASE%_trust.pem \r
152 del /F /Q /S %SVR_BASE%_trust.srl\r
153 \r
154 echo Setup success. Certificates created. Enable in unbound.conf file to use\r
155 \r
156 exit 0\r
157 \r
158 :help\r
159 echo unbound-control-setup.cmd - setup SSL keys for unbound-control\r
160 echo    -d dir  use directory to store keys and certificates.\r
161 echo            default: %DESTDIR%\r
162 echo please run this command using the same user id that the \r
163 echo unbound daemon uses, it needs read privileges.\r
164 exit 1\r