1255592Sdes#!/bin/sh
2255592Sdes#
3255592Sdes# unbound-control-setup.sh - set up SSL certificates for unbound-control
4255592Sdes#
5255592Sdes# Copyright (c) 2008, NLnet Labs. All rights reserved.
6255592Sdes#
7255592Sdes# This software is open source.
8255592Sdes# 
9255592Sdes# Redistribution and use in source and binary forms, with or without
10255592Sdes# modification, are permitted provided that the following conditions
11255592Sdes# are met:
12255592Sdes# 
13255592Sdes# Redistributions of source code must retain the above copyright notice,
14255592Sdes# this list of conditions and the following disclaimer.
15255592Sdes# 
16255592Sdes# Redistributions in binary form must reproduce the above copyright notice,
17255592Sdes# this list of conditions and the following disclaimer in the documentation
18255592Sdes# and/or other materials provided with the distribution.
19255592Sdes# 
20255592Sdes# Neither the name of the NLNET LABS nor the names of its contributors may
21255592Sdes# be used to endorse or promote products derived from this software without
22255592Sdes# specific prior written permission.
23255592Sdes# 
24255592Sdes# 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.
35255592Sdes
36255592Sdes# settings:
37255592Sdes
38255592Sdes# directory for files
39285206SdesDESTDIR=@ub_conf_dir@
40255592Sdes
41255592Sdes# issuer and subject name for certificates
42255592SdesSERVERNAME=unbound
43255592SdesCLIENTNAME=unbound-control
44255592Sdes
45255592Sdes# validity period for certificates
46255592SdesDAYS=7200
47255592Sdes
48255592Sdes# size of keys in bits
49255592SdesBITS=1536
50255592Sdes
51255592Sdes# hash algorithm
52255592SdesHASH=sha256
53255592Sdes
54255592Sdes# base name for unbound server keys
55255592SdesSVR_BASE=unbound_server
56255592Sdes
57255592Sdes# base name for unbound-control keys
58255592SdesCTL_BASE=unbound_control
59255592Sdes
60255595Sdes# we want -rw-r----- access (say you run this as root: grp=yes (server), all=no).
61255595Sdesumask 0027
62255592Sdes
63255592Sdes# end of options
64255592Sdes
65255592Sdes# functions:
66255592Sdeserror ( ) {
67255592Sdes	echo "$0 fatal error: $1"
68255592Sdes	exit 1
69255592Sdes}
70255592Sdes
71255592Sdes# check arguments:
72255592Sdeswhile test $# -ne 0; do
73255592Sdes	case $1 in
74255592Sdes	-d)
75255592Sdes	if test $# -eq 1; then error "need argument for -d"; fi
76255592Sdes	DESTDIR="$2"
77255592Sdes	shift
78255592Sdes	;;
79255592Sdes	*)
80255592Sdes	echo "unbound-control-setup.sh - setup SSL keys for unbound-control"
81255592Sdes	echo "	-d dir	use directory to store keys and certificates."
82255592Sdes	echo "		default: $DESTDIR"
83255592Sdes	echo "please run this command using the same user id that the "
84255592Sdes	echo "unbound daemon uses, it needs read privileges."
85255592Sdes	exit 1
86255592Sdes	;;
87255592Sdes	esac
88255592Sdes	shift
89255592Sdesdone
90255592Sdes
91255592Sdes# go!:
92255592Sdesecho "setup in directory $DESTDIR"
93255592Sdescd "$DESTDIR" || error "could not cd to $DESTDIR"
94255592Sdes
95255592Sdes# create certificate keys; do not recreate if they already exist.
96255592Sdesif test -f $SVR_BASE.key; then
97255592Sdes	echo "$SVR_BASE.key exists"
98255592Sdeselse
99255592Sdes	echo "generating $SVR_BASE.key"
100255592Sdes	openssl genrsa -out $SVR_BASE.key $BITS || error "could not genrsa"
101255592Sdesfi
102255592Sdesif test -f $CTL_BASE.key; then
103255592Sdes	echo "$CTL_BASE.key exists"
104255592Sdeselse
105255592Sdes	echo "generating $CTL_BASE.key"
106255592Sdes	openssl genrsa -out $CTL_BASE.key $BITS || error "could not genrsa"
107255592Sdesfi
108255592Sdes
109255592Sdes# create self-signed cert for server
110255592Sdescat >request.cfg <<EOF
111255592Sdes[req]
112255592Sdesdefault_bits=$BITS
113255592Sdesdefault_md=$HASH
114255592Sdesprompt=no
115255592Sdesdistinguished_name=req_distinguished_name
116255592Sdes
117255592Sdes[req_distinguished_name]
118255592SdescommonName=$SERVERNAME
119255592SdesEOF
120255592Sdestest -f request.cfg || error "could not create request.cfg"
121255592Sdes
122255592Sdesecho "create $SVR_BASE.pem (self signed certificate)"
123255592Sdesopenssl req -key $SVR_BASE.key -config request.cfg  -new -x509 -days $DAYS -out $SVR_BASE.pem || error "could not create $SVR_BASE.pem"
124255592Sdes# create trusted usage pem
125255592Sdesopenssl x509 -in $SVR_BASE.pem -addtrust serverAuth -out $SVR_BASE"_trust.pem"
126255592Sdes
127255592Sdes# create client request and sign it, piped
128255592Sdescat >request.cfg <<EOF
129255592Sdes[req]
130255592Sdesdefault_bits=$BITS
131255592Sdesdefault_md=$HASH
132255592Sdesprompt=no
133255592Sdesdistinguished_name=req_distinguished_name
134255592Sdes
135255592Sdes[req_distinguished_name]
136255592SdescommonName=$CLIENTNAME
137255592SdesEOF
138255592Sdestest -f request.cfg || error "could not create request.cfg"
139255592Sdes
140255592Sdesecho "create $CTL_BASE.pem (signed client certificate)"
141255592Sdesopenssl 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
142255592Sdestest -f $CTL_BASE.pem || error "could not create $CTL_BASE.pem"
143255592Sdes# create trusted usage pem
144255592Sdes# openssl x509 -in $CTL_BASE.pem -addtrust clientAuth -out $CTL_BASE"_trust.pem"
145255592Sdes
146255592Sdes# see details with openssl x509 -noout -text < $SVR_BASE.pem
147255592Sdes# echo "create $CTL_BASE""_browser.pfx (web client certificate)"
148255592Sdes# echo "create webbrowser PKCS#12 .PFX certificate file. In Firefox import in:"
149255592Sdes# echo "preferences - advanced - encryption - view certificates - your certs"
150255592Sdes# echo "empty password is used, simply click OK on the password dialog box."
151255592Sdes# 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"
152255592Sdes
153255592Sdes# remove unused permissions
154255592Sdeschmod o-rw $SVR_BASE.pem $SVR_BASE.key $CTL_BASE.pem $CTL_BASE.key
155255592Sdes
156255592Sdes# remove crap
157255592Sdesrm -f request.cfg
158255592Sdesrm -f $CTL_BASE"_trust.pem" $SVR_BASE"_trust.pem" $SVR_BASE"_trust.srl"
159255592Sdes
160255592Sdesecho "Setup success. Certificates created. Enable in unbound.conf file to use"
161255592Sdes
162255592Sdesexit 0
163