Home | History | Annotate | Line # | Download | only in winrc
unbound-control-setup.cmd revision 1.1.1.2
      1 @Echo off
      2 rem
      3 rem unbound-control-setup.cmd - set up SSL certificates for unbound-control
      4 rem
      5 rem Copyright (c) 2008, NLnet Labs. All rights reserved.
      6 rem Modified for Windows by Y.Voinov (c) 2014
      7 rem
      8 rem This software is open source.
      9 rem 
     10 rem Redistribution and use in source and binary forms, with or without
     11 rem modification, are permitted provided that the following conditions
     12 rem are met:
     13 rem 
     14 rem Redistributions of source code must retain the above copyright notice,
     15 rem this list of conditions and the following disclaimer.
     16 rem 
     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.
     20 rem 
     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.
     24 rem 
     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.
     36 
     37 rem settings:
     38 
     39 rem directory for files
     40 set prefix="C:\Program Files"
     41 set DESTDIR=%prefix%\Unbound
     42 
     43 rem issuer and subject name for certificates
     44 set SERVERNAME=unbound
     45 set CLIENTNAME=unbound-control
     46 
     47 rem validity period for certificates
     48 set DAYS=7200
     49 
     50 rem size of keys in bits
     51 set BITS=1536
     52 
     53 rem hash algorithm
     54 set HASH=sha256
     55 
     56 rem base name for unbound server keys
     57 set SVR_BASE=unbound_server
     58 
     59 rem base name for unbound-control keys
     60 set CTL_BASE=unbound_control
     61 
     62 rem end of options
     63 
     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%
     68 
     69 set arg=%1
     70 if /I "%arg%" == "-h" goto help
     71 if /I "%arg%"=="-d" set DESTDIR=%2
     72 
     73 rem go!:
     74 echo setup in directory %DESTDIR%
     75 cd %DESTDIR%
     76 
     77 rem create certificate keys; do not recreate if they already exist.
     78 if exist %SVR_BASE%.key (
     79 echo %SVR_BASE%.key exists
     80 goto next
     81 )
     82 echo generating %SVR_BASE%.key
     83 "%SSL_PROGRAM%" genrsa -out %SVR_BASE%.key %BITS% || echo could not genrsa && exit 1
     84 
     85 :next
     86 if exist %CTL_BASE%.key (
     87 echo %CTL_BASE%.key exists
     88 goto next2
     89 )
     90 echo generating %CTL_BASE%.key
     91 "%SSL_PROGRAM%" genrsa -out %CTL_BASE%.key %BITS% || echo could not genrsa && exit 1
     92 
     93 :next2
     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
    101 echo.>>request.cfg
    102 echo [req_distinguished_name]>>request.cfg
    103 echo commonName=%SERVERNAME%>>request.cfg
    104 
    105 if not exist request.cfg (
    106 echo could not create request.cfg
    107 exit 1
    108 )
    109 
    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
    114 
    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
    122 echo.>>request.cfg
    123 echo [req_distinguished_name]>>request.cfg
    124 echo commonName=%CLIENTNAME%>>request.cfg
    125 
    126 if not exist request.cfg (
    127 echo could not create request.cfg
    128 exit 1
    129 )
    130 
    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
    133 
    134 if not exist %CTL_BASE%.pem (
    135 echo could not create %CTL_BASE%.pem
    136 exit 1
    137 )
    138 rem create trusted usage pem
    139 rem "%SSL_PROGRAM%" x509 -in %CTL_BASE%.pem -addtrust clientAuth -out %CTL_BASE%_trust.pem
    140 
    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
    147 
    148 rem remove crap
    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
    153 
    154 echo Setup success. Certificates created. Enable in unbound.conf file to use
    155 
    156 exit 0
    157 
    158 :help
    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.
    164 exit 1
    165