3 rem unbound-control-setup.cmd - set up SSL certificates for unbound-control
5 rem Copyright (c) 2008, NLnet Labs. All rights reserved.
6 rem Modified for Windows by Y.Voinov (c) 2014
8 rem This software is open source.
10 rem Redistribution and use in source and binary forms, with or without
11 rem modification, are permitted provided that the following conditions
14 rem Redistributions of source code must retain the above copyright notice,
15 rem this list of conditions and the following disclaimer.
17 rem Redistributions in binary form must reproduce the above copyright notice,
18 rem this list of conditions and the following disclaimer in the documentation
19 rem and/or other materials provided with the distribution.
21 rem Neither the name of the NLNET LABS nor the names of its contributors may
22 rem be used to endorse or promote products derived from this software without
23 rem specific prior written permission.
25 rem THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 rem "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 rem LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 rem A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 rem HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 rem SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
31 rem TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 rem PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 rem LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 rem NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 rem SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 rem directory for files
40 set prefix="C:\Program Files (x86)"
41 set DESTDIR=%prefix%\Unbound
43 rem issuer and subject name for certificates
44 set SERVERNAME=unbound
45 set CLIENTNAME=unbound-control
47 rem validity period for certificates
50 rem size of keys in bits
56 rem base name for unbound server keys
57 set SVR_BASE=unbound_server
59 rem base name for unbound-control keys
60 set CTL_BASE=unbound_control
64 rem Check OpenSSL installed
65 for /f "delims=" %%a in ('where openssl') do @set SSL_PROGRAM=%%a
66 if /I "%SSL_PROGRAM%"=="" echo SSL not found. If installed, add path to PATH environment variable. & exit 1
67 echo SSL found: %SSL_PROGRAM%
70 if /I "%arg%" == "-h" goto help
71 if /I "%arg%"=="-d" set DESTDIR=%2
74 echo setup in directory %DESTDIR%
77 rem create certificate keys; do not recreate if they already exist.
78 if exist $SVR_BASE.key (
79 echo %SVR_BASE%.key exists
82 echo generating %SVR_BASE%.key
83 "%SSL_PROGRAM%" genrsa -out %SVR_BASE%.key %BITS% || echo could not genrsa && exit 1
86 if exist %CTL_BASE%.key (
87 echo %CTL_BASE%.key exists
90 echo generating %CTL_BASE%.key
91 "%SSL_PROGRAM%" genrsa -out %CTL_BASE%.key %BITS% || echo could not genrsa && exit 1
94 rem create self-signed cert for server
95 if exist request.cfg (del /F /Q /S request.cfg)
96 echo [req]>>request.cfg
97 echo default_bits=%BITS%>>request.cfg
98 echo default_md=%HASH%>>request.cfg
99 echo prompt=no>>request.cfg
100 echo distinguished_name=req_distinguished_name>>request.cfg
102 echo [req_distinguished_name]>>request.cfg
103 echo commonName=%SERVERNAME%>>request.cfg
105 if not exist request.cfg (
106 echo could not create request.cfg
110 echo create %SVR_BASE%.pem (self signed certificate)
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
112 rem create trusted usage pem
113 "%SSL_PROGRAM%" x509 -in %SVR_BASE%.pem -addtrust serverAuth -out %SVR_BASE%_trust.pem
115 rem create client request and sign it
116 if exist request.cfg (del /F /Q /S request.cfg)
117 echo [req]>>request.cfg
118 echo default_bits=%BITS%>>request.cfg
119 echo default_md=%HASH%>>request.cfg
120 echo prompt=no>>request.cfg
121 echo distinguished_name=req_distinguished_name>>request.cfg
123 echo [req_distinguished_name]>>request.cfg
124 echo commonName=%CLIENTNAME%>>request.cfg
126 if not exist request.cfg (
127 echo could not create request.cfg
131 echo create %CTL_BASE%.pem (signed client certificate)
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
134 if not exist %CTL_BASE%.pem (
135 echo could not create %CTL_BASE%.pem
138 rem create trusted usage pem
139 rem "%SSL_PROGRAM%" x509 -in %CTL_BASE%.pem -addtrust clientAuth -out %CTL_BASE%_trust.pem
141 rem see details with "%SSL_PROGRAM%" x509 -noout -text < %SVR_BASE%.pem
142 rem echo "create %CTL_BASE%_browser.pfx (web client certificate)"
143 rem echo "create webbrowser PKCSrem12 .PFX certificate file. In Firefox import in:"
144 rem echo "preferences - advanced - encryption - view certificates - your certs"
145 rem echo "empty password is used, simply click OK on the password dialog box."
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
149 del /F /Q /S request.cfg
150 del /F /Q /S %CTL_BASE%_trust.pem
151 del /F /Q /S %SVR_BASE%_trust.pem
152 del /F /Q /S %SVR_BASE%_trust.srl
154 echo Setup success. Certificates created. Enable in unbound.conf file to use
159 echo unbound-control-setup.cmd - setup SSL keys for unbound-control
160 echo -d dir use directory to store keys and certificates.
161 echo default: %DESTDIR%
162 echo please run this command using the same user id that the
163 echo unbound daemon uses, it needs read privileges.