xref: /openbsd-src/usr.sbin/nsd/nsd-control-setup.sh.in (revision bc6311d77cd5d30ac9d52b9dbc59ea6cf4b9c7d3)
1d3fecca9Ssthen#!/bin/sh
2d3fecca9Ssthen#
3d3fecca9Ssthen# nsd-control-setup.sh - set up SSL certificates for nsd-control
4d3fecca9Ssthen#
5d3fecca9Ssthen# Copyright (c) 2011, NLnet Labs. All rights reserved.
6d3fecca9Ssthen#
7d3fecca9Ssthen# This software is open source.
8d3fecca9Ssthen#
9d3fecca9Ssthen# Redistribution and use in source and binary forms, with or without
10d3fecca9Ssthen# modification, are permitted provided that the following conditions
11d3fecca9Ssthen# are met:
12d3fecca9Ssthen#
13d3fecca9Ssthen# Redistributions of source code must retain the above copyright notice,
14d3fecca9Ssthen# this list of conditions and the following disclaimer.
15d3fecca9Ssthen#
16d3fecca9Ssthen# Redistributions in binary form must reproduce the above copyright notice,
17d3fecca9Ssthen# this list of conditions and the following disclaimer in the documentation
18d3fecca9Ssthen# and/or other materials provided with the distribution.
19d3fecca9Ssthen#
20d3fecca9Ssthen# Neither the name of the NLNET LABS nor the names of its contributors may
21d3fecca9Ssthen# be used to endorse or promote products derived from this software without
22d3fecca9Ssthen# specific prior written permission.
23d3fecca9Ssthen#
24d3fecca9Ssthen# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25cbbc2d6cSbrad# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26cbbc2d6cSbrad# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27cbbc2d6cSbrad# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28cbbc2d6cSbrad# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29cbbc2d6cSbrad# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
30cbbc2d6cSbrad# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31cbbc2d6cSbrad# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32cbbc2d6cSbrad# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33cbbc2d6cSbrad# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34cbbc2d6cSbrad# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35d3fecca9Ssthen
36d3fecca9Ssthen# settings:
37d3fecca9Ssthen
38d3fecca9Ssthen# directory for files
39d3fecca9SsthenDESTDIR=@configdir@
40d3fecca9Ssthen
41d3fecca9Ssthen# issuer and subject name for certificates
42d3fecca9SsthenSERVERNAME=nsd
43d3fecca9SsthenCLIENTNAME=nsd-control
44d3fecca9Ssthen
45d3fecca9Ssthen# validity period for certificates
46*bc6311d7SflorianDAYS=7200
47d3fecca9Ssthen
48d3fecca9Ssthen# size of keys in bits
49c939baa4SsthenBITS=3072
50d3fecca9Ssthen
51d3fecca9Ssthen# hash algorithm
52d3fecca9SsthenHASH=sha256
53d3fecca9Ssthen
54d3fecca9Ssthen# base name for nsd server keys
55d3fecca9SsthenSVR_BASE=nsd_server
56d3fecca9Ssthen
57d3fecca9Ssthen# base name for nsd-control keys
58d3fecca9SsthenCTL_BASE=nsd_control
59d3fecca9Ssthen
60308d2509Sflorian# flag to recreate generated certificates
61308d2509SflorianRECREATE=0
62308d2509Sflorian
63d3fecca9Ssthen# we want -rw-r--- access (say you run this as root: grp=yes (server), all=no).
64d3fecca9Ssthenumask 0026
65d3fecca9Ssthen
66d3fecca9Ssthen# end of options
67d3fecca9Ssthen
68308d2509Sflorianset -eu
69308d2509Sflorian
70308d2509Sfloriancleanup() {
71308d2509Sflorian    echo "removing artifacts"
72308d2509Sflorian
73308d2509Sflorian    rm -rf \
74308d2509Sflorian       server.cnf \
75308d2509Sflorian       client.cnf \
76308d2509Sflorian       "${SVR_BASE}_trust.pem" \
77308d2509Sflorian       "${CTL_BASE}_trust.pem" \
78308d2509Sflorian       "${SVR_BASE}_trust.srl"
79308d2509Sflorian}
80308d2509Sflorian
81308d2509Sflorianfatal() {
82308d2509Sflorian    printf "fatal error: $*\n" >/dev/stderr
83d3fecca9Ssthen    exit 1
84d3fecca9Ssthen}
85d3fecca9Ssthen
86308d2509Sflorianusage() {
87308d2509Sflorian    cat <<EOF
88308d2509Sflorianusage: $0 OPTIONS
89308d2509SflorianOPTIONS
90308d2509Sflorian-d <dir>  used directory to store keys and certificates (default: $DESTDIR)
91308d2509Sflorian-h        show help notice
92308d2509Sflorian-r        recreate certificates
93308d2509SflorianEOF
94308d2509Sflorian}
95308d2509Sflorian
96308d2509SflorianOPTIND=1
97308d2509Sflorianwhile getopts 'd:hr' arg; do
98308d2509Sflorian    case "$arg" in
99308d2509Sflorian      d) DESTDIR="$OPTARG" ;;
100*bc6311d7Sflorian      h) usage; exit 1 ;;
101308d2509Sflorian      r) RECREATE=1 ;;
102308d2509Sflorian      ?) fatal "'$arg' unknown option" ;;
103d3fecca9Ssthen    esac
104d3fecca9Ssthendone
105308d2509Sflorianshift $((OPTIND - 1))
106d3fecca9Ssthen
107308d2509Sflorian
108d3fecca9Ssthenecho "setup in directory $DESTDIR"
109308d2509Sfloriancd "$DESTDIR"
110d3fecca9Ssthen
111ac5517e4Sfloriantrap cleanup INT
112308d2509Sflorian
113308d2509Sflorian# ===
114308d2509Sflorian# Generate server certificate
115308d2509Sflorian# ===
116308d2509Sflorian
117308d2509Sflorian# generate private key; do no recreate it if they already exist.
118308d2509Sflorianif [ ! -f "$SVR_BASE.key" ]; then
119308d2509Sflorian    openssl genrsa -out "$SVR_BASE.key" "$BITS"
120d3fecca9Ssthenfi
121d3fecca9Ssthen
122308d2509Sfloriancat >server.cnf <<EOF
123*bc6311d7Sflorian[req]
124d3fecca9Ssthendefault_bits=$BITS
125d3fecca9Ssthendefault_md=$HASH
126d3fecca9Ssthenprompt=no
127d3fecca9Ssthendistinguished_name=req_distinguished_name
128*bc6311d7Sflorianx509_extensions=v3_ca
129d3fecca9Ssthen[req_distinguished_name]
130d3fecca9SsthencommonName=$SERVERNAME
131*bc6311d7Sflorian[v3_ca]
132*bc6311d7SfloriansubjectKeyIdentifier=hash
133*bc6311d7SflorianauthorityKeyIdentifier=keyid:always,issuer:always
134*bc6311d7SflorianbasicConstraints=critical,CA:TRUE,pathlen:0
135*bc6311d7SfloriansubjectAltName=DNS:$SERVERNAME
136d3fecca9SsthenEOF
137d3fecca9Ssthen
138308d2509Sflorian[ -f server.cnf ] || fatal "cannot create openssl configuration"
139d3fecca9Ssthen
140308d2509Sflorianif [ ! -f "$SVR_BASE.pem" -o $RECREATE -eq 1 ]; then
141308d2509Sflorian    openssl req \
142308d2509Sflorian            -new -x509 \
143308d2509Sflorian            -key "$SVR_BASE.key" \
144308d2509Sflorian            -config server.cnf  \
145308d2509Sflorian            -days "$DAYS" \
146308d2509Sflorian            -out "$SVR_BASE.pem"
147308d2509Sflorian
148308d2509Sflorian    [ ! -f "SVR_BASE.pem" ] || fatal "cannot create server certificate"
149308d2509Sflorianfi
150308d2509Sflorian
151308d2509Sflorian# ===
152308d2509Sflorian# Generate client certificate
153308d2509Sflorian# ===
154308d2509Sflorian
155308d2509Sflorian# generate private key; do no recreate it if they already exist.
156308d2509Sflorianif [ ! -f "$CTL_BASE.key" ]; then
157308d2509Sflorian    openssl genrsa -out "$CTL_BASE.key" "$BITS"
158308d2509Sflorianfi
159308d2509Sflorian
160308d2509Sfloriancat >client.cnf <<EOF
161d3fecca9Ssthen[req]
162d3fecca9Ssthendefault_bits=$BITS
163d3fecca9Ssthendefault_md=$HASH
164d3fecca9Ssthenprompt=no
165d3fecca9Ssthendistinguished_name=req_distinguished_name
166*bc6311d7Sflorianreq_extensions=v3_req
167d3fecca9Ssthen[req_distinguished_name]
168d3fecca9SsthencommonName=$CLIENTNAME
169*bc6311d7Sflorian[v3_req]
170*bc6311d7SflorianbasicConstraints=critical,CA:FALSE
171*bc6311d7SfloriansubjectAltName=DNS:$CLIENTNAME
172d3fecca9SsthenEOF
173d3fecca9Ssthen
174308d2509Sflorian[ -f client.cnf ] || fatal "cannot create openssl configuration"
175308d2509Sflorian
176308d2509Sflorianif [ ! -f "$CTL_BASE.pem" -o $RECREATE -eq 1 ]; then
177308d2509Sflorian    openssl x509 \
178308d2509Sflorian        -addtrust serverAuth \
179308d2509Sflorian        -in "$SVR_BASE.pem" \
180308d2509Sflorian        -out "${SVR_BASE}_trust.pem"
181308d2509Sflorian
182308d2509Sflorian    openssl req \
183308d2509Sflorian            -new \
184308d2509Sflorian            -config client.cnf \
185308d2509Sflorian            -key "$CTL_BASE.key" \
186308d2509Sflorian        | openssl x509 \
187308d2509Sflorian                  -req \
188308d2509Sflorian                  -days "$DAYS" \
189308d2509Sflorian                  -CA "${SVR_BASE}_trust.pem" \
190308d2509Sflorian                  -CAkey "$SVR_BASE.key" \
191308d2509Sflorian                  -CAcreateserial \
192308d2509Sflorian                  -$HASH \
193*bc6311d7Sflorian                  -extfile client.cnf \
194*bc6311d7Sflorian                  -extensions v3_req \
195308d2509Sflorian                  -out "$CTL_BASE.pem"
196308d2509Sflorian
197308d2509Sflorian    [ ! -f "CTL_BASE.pem" ] || fatal "cannot create signed client certificate"
198308d2509Sflorianfi
199308d2509Sflorian
200308d2509Sflorian# remove unused permissions
201308d2509Sflorianchmod o-rw \
202308d2509Sflorian      "$SVR_BASE.pem" \
203308d2509Sflorian      "$SVR_BASE.key" \
204308d2509Sflorian      "$CTL_BASE.pem" \
205308d2509Sflorian      "$CTL_BASE.key"
206308d2509Sflorian
207308d2509Sfloriancleanup
208308d2509Sflorian
209308d2509Sflorianecho "Setup success. Certificates created. Enable in nsd.conf file to use"
210308d2509Sflorian
211d3fecca9Ssthen# create trusted usage pem
212d3fecca9Ssthen# openssl x509 -in $CTL_BASE.pem -addtrust clientAuth -out $CTL_BASE"_trust.pem"
213d3fecca9Ssthen
214d3fecca9Ssthen# see details with openssl x509 -noout -text < $SVR_BASE.pem
215d3fecca9Ssthen# echo "create $CTL_BASE""_browser.pfx (web client certificate)"
216d3fecca9Ssthen# echo "create webbrowser PKCS#12 .PFX certificate file. In Firefox import in:"
217d3fecca9Ssthen# echo "preferences - advanced - encryption - view certificates - your certs"
218d3fecca9Ssthen# echo "empty password is used, simply click OK on the password dialog box."
219d3fecca9Ssthen# openssl pkcs12 -export -in $CTL_BASE"_trust.pem" -inkey $CTL_BASE.key -name "nsd remote control client cert" -out $CTL_BASE"_browser.pfx" -password "pass:" || error "could not create browser certificate"
220