1238106Sdes#!/bin/sh 2238106Sdes# 3238106Sdes# unbound-control-setup.sh - set up SSL certificates for unbound-control 4238106Sdes# 5238106Sdes# Copyright (c) 2008, NLnet Labs. All rights reserved. 6238106Sdes# 7238106Sdes# This software is open source. 8238106Sdes# 9238106Sdes# Redistribution and use in source and binary forms, with or without 10238106Sdes# modification, are permitted provided that the following conditions 11238106Sdes# are met: 12238106Sdes# 13238106Sdes# Redistributions of source code must retain the above copyright notice, 14238106Sdes# this list of conditions and the following disclaimer. 15238106Sdes# 16238106Sdes# Redistributions in binary form must reproduce the above copyright notice, 17238106Sdes# this list of conditions and the following disclaimer in the documentation 18238106Sdes# and/or other materials provided with the distribution. 19238106Sdes# 20238106Sdes# Neither the name of the NLNET LABS nor the names of its contributors may 21238106Sdes# be used to endorse or promote products derived from this software without 22238106Sdes# specific prior written permission. 23238106Sdes# 24238106Sdes# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 25269257Sdes# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 26269257Sdes# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 27269257Sdes# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 28269257Sdes# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 29269257Sdes# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 30269257Sdes# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 31269257Sdes# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 32269257Sdes# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 33269257Sdes# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 34269257Sdes# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35238106Sdes 36238106Sdes# settings: 37238106Sdes 38238106Sdes# directory for files 39255593Sdesprefix= 40255593SdesDESTDIR=${prefix}/etc/unbound 41238106Sdes 42238106Sdes# issuer and subject name for certificates 43238106SdesSERVERNAME=unbound 44238106SdesCLIENTNAME=unbound-control 45238106Sdes 46238106Sdes# validity period for certificates 47238106SdesDAYS=7200 48238106Sdes 49238106Sdes# size of keys in bits 50238106SdesBITS=1536 51238106Sdes 52238106Sdes# hash algorithm 53238106SdesHASH=sha256 54238106Sdes 55238106Sdes# base name for unbound server keys 56238106SdesSVR_BASE=unbound_server 57238106Sdes 58238106Sdes# base name for unbound-control keys 59238106SdesCTL_BASE=unbound_control 60238106Sdes 61255595Sdes# we want -rw-r----- access (say you run this as root: grp=yes (server), all=no). 62255595Sdesumask 0027 63238106Sdes 64238106Sdes# end of options 65238106Sdes 66238106Sdes# functions: 67238106Sdeserror ( ) { 68238106Sdes echo "$0 fatal error: $1" 69238106Sdes exit 1 70238106Sdes} 71238106Sdes 72238106Sdes# check arguments: 73238106Sdeswhile test $# -ne 0; do 74238106Sdes case $1 in 75238106Sdes -d) 76238106Sdes if test $# -eq 1; then error "need argument for -d"; fi 77238106Sdes DESTDIR="$2" 78238106Sdes shift 79238106Sdes ;; 80238106Sdes *) 81238106Sdes echo "unbound-control-setup.sh - setup SSL keys for unbound-control" 82238106Sdes echo " -d dir use directory to store keys and certificates." 83238106Sdes echo " default: $DESTDIR" 84238106Sdes echo "please run this command using the same user id that the " 85238106Sdes echo "unbound daemon uses, it needs read privileges." 86238106Sdes exit 1 87238106Sdes ;; 88238106Sdes esac 89238106Sdes shift 90238106Sdesdone 91238106Sdes 92238106Sdes# go!: 93238106Sdesecho "setup in directory $DESTDIR" 94238106Sdescd "$DESTDIR" || error "could not cd to $DESTDIR" 95238106Sdes 96238106Sdes# create certificate keys; do not recreate if they already exist. 97238106Sdesif test -f $SVR_BASE.key; then 98238106Sdes echo "$SVR_BASE.key exists" 99238106Sdeselse 100238106Sdes echo "generating $SVR_BASE.key" 101238106Sdes openssl genrsa -out $SVR_BASE.key $BITS || error "could not genrsa" 102238106Sdesfi 103238106Sdesif test -f $CTL_BASE.key; then 104238106Sdes echo "$CTL_BASE.key exists" 105238106Sdeselse 106238106Sdes echo "generating $CTL_BASE.key" 107238106Sdes openssl genrsa -out $CTL_BASE.key $BITS || error "could not genrsa" 108238106Sdesfi 109238106Sdes 110238106Sdes# create self-signed cert for server 111238106Sdescat >request.cfg <<EOF 112238106Sdes[req] 113238106Sdesdefault_bits=$BITS 114238106Sdesdefault_md=$HASH 115238106Sdesprompt=no 116238106Sdesdistinguished_name=req_distinguished_name 117238106Sdes 118238106Sdes[req_distinguished_name] 119238106SdescommonName=$SERVERNAME 120238106SdesEOF 121238106Sdestest -f request.cfg || error "could not create request.cfg" 122238106Sdes 123238106Sdesecho "create $SVR_BASE.pem (self signed certificate)" 124238106Sdesopenssl req -key $SVR_BASE.key -config request.cfg -new -x509 -days $DAYS -out $SVR_BASE.pem || error "could not create $SVR_BASE.pem" 125238106Sdes# create trusted usage pem 126238106Sdesopenssl x509 -in $SVR_BASE.pem -addtrust serverAuth -out $SVR_BASE"_trust.pem" 127238106Sdes 128238106Sdes# create client request and sign it, piped 129238106Sdescat >request.cfg <<EOF 130238106Sdes[req] 131238106Sdesdefault_bits=$BITS 132238106Sdesdefault_md=$HASH 133238106Sdesprompt=no 134238106Sdesdistinguished_name=req_distinguished_name 135238106Sdes 136238106Sdes[req_distinguished_name] 137238106SdescommonName=$CLIENTNAME 138238106SdesEOF 139238106Sdestest -f request.cfg || error "could not create request.cfg" 140238106Sdes 141238106Sdesecho "create $CTL_BASE.pem (signed client certificate)" 142238106Sdesopenssl req -key $CTL_BASE.key -config request.cfg -new | openssl x509 -req -days $DAYS -CA $SVR_BASE"_trust.pem" -CAkey $SVR_BASE.key -CAcreateserial -$HASH -out $CTL_BASE.pem 143238106Sdestest -f $CTL_BASE.pem || error "could not create $CTL_BASE.pem" 144238106Sdes# create trusted usage pem 145238106Sdes# openssl x509 -in $CTL_BASE.pem -addtrust clientAuth -out $CTL_BASE"_trust.pem" 146238106Sdes 147238106Sdes# see details with openssl x509 -noout -text < $SVR_BASE.pem 148238106Sdes# echo "create $CTL_BASE""_browser.pfx (web client certificate)" 149238106Sdes# echo "create webbrowser PKCS#12 .PFX certificate file. In Firefox import in:" 150238106Sdes# echo "preferences - advanced - encryption - view certificates - your certs" 151238106Sdes# echo "empty password is used, simply click OK on the password dialog box." 152238106Sdes# openssl pkcs12 -export -in $CTL_BASE"_trust.pem" -inkey $CTL_BASE.key -name "unbound remote control client cert" -out $CTL_BASE"_browser.pfx" -password "pass:" || error "could not create browser certificate" 153238106Sdes 154238106Sdes# remove unused permissions 155238106Sdeschmod o-rw $SVR_BASE.pem $SVR_BASE.key $CTL_BASE.pem $CTL_BASE.key 156238106Sdes 157238106Sdes# remove crap 158238106Sdesrm -f request.cfg 159238106Sdesrm -f $CTL_BASE"_trust.pem" $SVR_BASE"_trust.pem" $SVR_BASE"_trust.srl" 160238106Sdes 161238106Sdesecho "Setup success. Certificates created. Enable in unbound.conf file to use" 162238106Sdes 163238106Sdesexit 0 164