2022-01-17 05:39:02 -05:00
#!/bin/sh
#
# Copyright (C) Internet Systems Consortium, Inc. ("ISC")
#
# SPDX-License-Identifier: MPL-2.0
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, you can obtain one at https://mozilla.org/MPL/2.0/.
#
# See the COPYRIGHT file distributed with this work for additional
# information regarding copyright ownership.
# shellcheck source=conf.sh
. ../conf.sh
set -e
2023-11-16 06:10:50 -05:00
$SHELL clean.sh
2023-08-05 08:22:38 -04:00
OPENSSL_CONF = softhsm2-util --init-token --free --pin 1234 --so-pin 1234 --label "softhsm2-enginepkcs11" | awk '/^The token has been initialized and is reassigned to slot/ { print $NF }'
2022-01-17 05:39:02 -05:00
2022-02-28 07:51:47 -05:00
printf '%s' " ${ HSMPIN :- 1234 } " >ns1/pin
2022-01-17 05:39:02 -05:00
PWD = $( pwd )
keygen( ) {
type = " $1 "
bits = " $2 "
zone = " $3 "
id = " $4 "
2023-10-24 08:43:14 -04:00
2022-01-17 05:39:02 -05:00
label = " ${ id } - ${ zone } "
2022-09-08 13:31:58 -04:00
p11id = $( echo " ${ label } " | openssl sha1 -r | awk '{print $1}' )
2022-02-28 07:51:47 -05:00
OPENSSL_CONF = pkcs11-tool --module $SOFTHSM2_MODULE --token-label "softhsm2-enginepkcs11" -l -k --key-type $type :$bits --label " ${ label } " --id " ${ p11id } " --pin $( cat $PWD /ns1/pin) >pkcs11-tool.out.$zone .$id 2>pkcs11-tool.err.$zone .$id || return 1
2022-01-17 05:39:02 -05:00
}
keyfromlabel( ) {
alg = " $1 "
zone = " $2 "
id = " $3 "
dir = " $4 "
shift 4
2023-10-24 08:43:14 -04:00
2024-08-05 05:40:42 -04:00
$KEYFRLAB -K $dir -a $alg -l " pkcs11:token=softhsm2-enginepkcs11;object= ${ id } - ${ zone } ;pin-source= $PWD /ns1/pin " " $@ " $zone >>keyfromlabel.out.$zone .$id 2>keyfromlabel.err.$zone .$id || return 1
2022-01-17 05:39:02 -05:00
cat keyfromlabel.out.$zone .$id
}
# Setup ns1.
2023-11-16 09:37:34 -05:00
copy_setports ns1/named.conf.in ns1/named.conf
mkdir ns1/keys
2022-01-17 05:39:02 -05:00
dir = "ns1"
infile = " ${ dir } /template.db.in "
for algtypebits in rsasha256:rsa:2048 rsasha512:rsa:2048 \
ecdsap256sha256:EC:prime256v1 ecdsap384sha384:EC:prime384v1; do # Edwards curves are not yet supported by OpenSC
# ed25519:EC:edwards25519 ed448:EC:edwards448
alg = $( echo " $algtypebits " | cut -f 1 -d :)
type = $( echo " $algtypebits " | cut -f 2 -d :)
bits = $( echo " $algtypebits " | cut -f 3 -d :)
2024-01-24 09:38:55 -05:00
alg_upper = $( echo " $alg " | tr '[:lower:]' '[:upper:]' )
supported = $( eval " echo \$ ${ alg_upper } _SUPPORTED " )
2023-10-24 08:43:14 -04:00
2022-02-28 07:51:47 -05:00
tld = "example"
2024-01-24 09:38:55 -05:00
if [ " ${ supported } " = 1 ] ; then
2022-02-28 07:51:47 -05:00
zone = " $alg . $tld "
zonefile = " zone. $alg . $tld .db "
2022-01-17 05:39:02 -05:00
ret = 0
2023-10-24 08:43:14 -04:00
2022-01-17 05:39:02 -05:00
echo_i " Generate keys $alg $type : $bits for zone $zone "
keygen $type $bits $zone enginepkcs11-zsk || ret = 1
keygen $type $bits $zone enginepkcs11-ksk || ret = 1
test " $ret " -eq 0 || exit 1
2023-10-24 08:43:14 -04:00
2022-01-17 05:39:02 -05:00
echo_i " Get ZSK $alg $zone $type : $bits "
zsk1 = $( keyfromlabel $alg $zone enginepkcs11-zsk $dir )
test -z " $zsk1 " && exit 1
2023-10-24 08:43:14 -04:00
2022-01-17 05:39:02 -05:00
echo_i " Get KSK $alg $zone $type : $bits "
ksk1 = $( keyfromlabel $alg $zone enginepkcs11-ksk $dir -f KSK)
test -z " $ksk1 " && exit 1
2023-10-24 08:43:14 -04:00
2022-01-17 05:39:02 -05:00
(
cd $dir
zskid1 = $( keyfile_to_key_id $zsk1 )
kskid1 = $( keyfile_to_key_id $ksk1 )
echo " $zskid1 " >$zone .zskid1
echo " $kskid1 " >$zone .kskid1
)
2023-10-24 08:43:14 -04:00
2022-01-17 05:39:02 -05:00
echo_i " Sign zone with $ksk1 $zsk1 "
cat " $infile " " ${ dir } / ${ ksk1 } .key " " ${ dir } / ${ zsk1 } .key " >" ${ dir } / ${ zonefile } "
2024-08-05 05:40:42 -04:00
$SIGNER -K $dir -S -a -g -O full -o " $zone " " ${ dir } / ${ zonefile } " >signer.out.$zone || ret = 1
2022-01-17 05:39:02 -05:00
test " $ret " -eq 0 || exit 1
2023-10-24 08:43:14 -04:00
2022-01-17 05:39:02 -05:00
echo_i " Generate successor keys $alg $type : $bits for zone $zone "
keygen $type $bits $zone enginepkcs11-zsk2 || ret = 1
keygen $type $bits $zone enginepkcs11-ksk2 || ret = 1
test " $ret " -eq 0 || exit 1
2023-10-24 08:43:14 -04:00
2022-01-17 05:39:02 -05:00
echo_i " Get ZSK $alg $id - $zone $type : $bits "
zsk2 = $( keyfromlabel $alg $zone enginepkcs11-zsk2 $dir )
test -z " $zsk2 " && exit 1
2023-10-24 08:43:14 -04:00
2022-01-17 05:39:02 -05:00
echo_i " Get KSK $alg $id - $zone $type : $bits "
ksk2 = $( keyfromlabel $alg $zone enginepkcs11-ksk2 $dir -f KSK)
test -z " $ksk2 " && exit 1
2023-10-24 08:43:14 -04:00
2022-01-17 05:39:02 -05:00
(
cd $dir
zskid2 = $( keyfile_to_key_id $zsk2 )
kskid2 = $( keyfile_to_key_id $ksk2 )
echo " $zskid2 " >$zone .zskid2
echo " $kskid2 " >$zone .kskid2
cp " ${ zsk2 } .key " " ${ zsk2 } .zsk2 "
cp " ${ ksk2 } .key " " ${ ksk2 } .ksk2 "
)
2023-10-24 08:43:14 -04:00
2022-02-28 07:51:47 -05:00
echo_i " Add zone $alg .kasp to named.conf "
cp $infile ${ dir } /zone.${ alg } .kasp.db
2022-03-15 11:10:34 -04:00
echo_i " Add zone $alg .split to named.conf "
cp $infile ${ dir } /zone.${ alg } .split.db
2023-11-17 04:55:00 -05:00
echo_i "Add weird zone to named.conf"
cp $infile ${ dir } /zone.${ alg } .weird.db
2022-01-17 05:39:02 -05:00
echo_i " Add zone $zone to named.conf "
cat >>" ${ dir } /named.conf " <<EOF
zone " $zone " {
type primary;
file " ${ zonefile } .signed " ;
allow-update { any; } ;
} ;
2022-02-28 07:51:47 -05:00
dnssec-policy " $alg " {
keys {
ksk key-store "hsm" lifetime unlimited algorithm ${ alg } ;
zsk key-store "pin" lifetime unlimited algorithm ${ alg } ;
} ;
} ;
zone " ${ alg } .kasp " {
type primary;
file " zone. ${ alg } .kasp.db " ;
dnssec-policy " $alg " ;
allow-update { any; } ;
} ;
2023-11-17 04:55:00 -05:00
dnssec-policy " weird- ${ alg } -\"\:\;\?\&\[\]\@\!\$\*\+\,\|\=\.\(\) " {
keys {
ksk key-store "hsm" lifetime unlimited algorithm ${ alg } ;
zsk key-store "pin" lifetime unlimited algorithm ${ alg } ;
} ;
} ;
zone " ${ alg } .\"\:\;\?\&\[\]\@\!\$\*\+\,\|\=\.\(\)foo.weird " {
type primary;
file " zone. ${ alg } .weird.db " ;
check-names ignore;
dnssec-policy " weird- ${ alg } -\"\:\;\?\&\[\]\@\!\$\*\+\,\|\=\.\(\) " ;
allow-update { any; } ;
} ;
2022-03-15 11:10:34 -04:00
dnssec-policy " ${ alg } -split " {
keys {
ksk key-store "hsm" lifetime unlimited algorithm ${ alg } ;
zsk key-store "disk" lifetime unlimited algorithm ${ alg } ;
} ;
} ;
zone " ${ alg } .split " {
type primary;
file " zone. ${ alg } .split.db " ;
dnssec-policy " ${ alg } -split " ;
allow-update { any; } ;
} ;
2022-01-17 05:39:02 -05:00
EOF
fi
done
2023-11-16 09:37:34 -05:00
# Setup ns2 (with views).
copy_setports ns2/named.conf.in ns2/named.conf
mkdir ns2/keys
dir = "ns2"
infile = " ${ dir } /template.db.in "
algtypebits = "ecdsap256sha256:EC:prime256v1"
alg = $( echo " $algtypebits " | cut -f 1 -d :)
type = $( echo " $algtypebits " | cut -f 2 -d :)
bits = $( echo " $algtypebits " | cut -f 3 -d :)
2024-01-24 09:38:55 -05:00
alg_upper = $( echo " $alg " | tr '[:lower:]' '[:upper:]' )
supported = $( eval " echo \$ ${ alg_upper } _SUPPORTED " )
2023-11-16 09:37:34 -05:00
tld = "views"
2024-01-24 09:38:55 -05:00
if [ " ${ supported } " = 1 ] ; then
2023-11-16 09:37:34 -05:00
zone = " $alg . $tld "
zonefile1 = " zone. $alg . $tld .view1.db "
zonefile2 = " zone. $alg . $tld .view2.db "
ret = 0
echo_i " Generate keys $alg $type : $bits for zone $zone "
keygen $type $bits $zone enginepkcs11-zsk || ret = 1
keygen $type $bits $zone enginepkcs11-ksk || ret = 1
test " $ret " -eq 0 || exit 1
echo_i " Get ZSK $alg $zone $type : $bits "
zsk1 = $( keyfromlabel $alg $zone enginepkcs11-zsk $dir )
test -z " $zsk1 " && exit 1
echo_i " Get KSK $alg $zone $type : $bits "
ksk1 = $( keyfromlabel $alg $zone enginepkcs11-ksk $dir -f KSK)
test -z " $ksk1 " && exit 1
(
cd $dir
zskid1 = $( keyfile_to_key_id $zsk1 )
kskid1 = $( keyfile_to_key_id $ksk1 )
echo " $zskid1 " >$zone .zskid1
echo " $kskid1 " >$zone .kskid1
)
echo_i " Sign zone with $ksk1 $zsk1 "
cat " $infile " " ${ dir } / ${ ksk1 } .key " " ${ dir } / ${ zsk1 } .key " >" ${ dir } / ${ zonefile1 } "
2024-08-05 05:40:42 -04:00
$SIGNER -K $dir -S -a -g -O full -o " $zone " " ${ dir } / ${ zonefile1 } " >signer.out.view1.$zone || ret = 1
2023-11-16 09:37:34 -05:00
test " $ret " -eq 0 || exit 1
cat " $infile " " ${ dir } / ${ ksk1 } .key " " ${ dir } / ${ zsk1 } .key " >" ${ dir } / ${ zonefile2 } "
2024-08-05 05:40:42 -04:00
$SIGNER -K $dir -S -a -g -O full -o " $zone " " ${ dir } / ${ zonefile2 } " >signer.out.view2.$zone || ret = 1
2023-11-16 09:37:34 -05:00
test " $ret " -eq 0 || exit 1
echo_i " Generate successor keys $alg $type : $bits for zone $zone "
keygen $type $bits $zone enginepkcs11-zsk2 || ret = 1
keygen $type $bits $zone enginepkcs11-ksk2 || ret = 1
test " $ret " -eq 0 || exit 1
echo_i " Get ZSK $alg $id - $zone $type : $bits "
zsk2 = $( keyfromlabel $alg $zone enginepkcs11-zsk2 $dir )
test -z " $zsk2 " && exit 1
echo_i " Get KSK $alg $id - $zone $type : $bits "
ksk2 = $( keyfromlabel $alg $zone enginepkcs11-ksk2 $dir -f KSK)
test -z " $ksk2 " && exit 1
(
cd $dir
zskid2 = $( keyfile_to_key_id $zsk2 )
kskid2 = $( keyfile_to_key_id $ksk2 )
echo " $zskid2 " >$zone .zskid2
echo " $kskid2 " >$zone .kskid2
cp " ${ zsk2 } .key " " ${ zsk2 } .zsk2 "
cp " ${ ksk2 } .key " " ${ ksk2 } .ksk2 "
)
echo_i " Add zone $alg .same-policy. $tld to named.conf "
cp $infile ${ dir } /zone.${ alg } .same-policy.view1.db
cp $infile ${ dir } /zone.${ alg } .same-policy.view2.db
echo_i " Add zone zone-with.different-policy. $tld to named.conf "
cp $infile ${ dir } /zone.zone-with.different-policy.view1.db
cp $infile ${ dir } /zone.zone-with.different-policy.view2.db
echo_i " Add zone $zone to named.conf "
cat >>" ${ dir } /named.conf " <<EOF
dnssec-policy " $alg " {
keys {
csk key-store "hsm" lifetime unlimited algorithm ${ alg } ;
} ;
} ;
dnssec-policy "rsasha256" {
keys {
csk key-store "hsm2" lifetime unlimited algorithm rsasha256 2048;
} ;
} ;
view "view1" {
match-clients { key "keyforview1" ; } ;
zone " $zone " {
type primary;
file " ${ zonefile1 } .signed " ;
allow-update { any; } ;
} ;
zone " ${ alg } .same-policy. ${ tld } " {
type primary;
file " zone. ${ alg } .same-policy.view1.db " ;
dnssec-policy " $alg " ;
allow-update { any; } ;
} ;
zone " zone-with.different-policy. ${ tld } " {
type primary;
file "zone.zone-with.different-policy.view1.db" ;
dnssec-policy " $alg " ;
allow-update { any; } ;
} ;
} ;
view "view2" {
match-clients { key "keyforview2" ; } ;
zone " $zone " {
type primary;
file " ${ zonefile2 } .signed " ;
allow-update { any; } ;
} ;
zone " ${ alg } .same-policy. ${ tld } " {
type primary;
file " zone. ${ alg } .same-policy.view2.db " ;
dnssec-policy " $alg " ;
allow-update { any; } ;
} ;
zone " zone-with.different-policy. ${ tld } " {
type primary;
file "zone.zone-with.different-policy.view2.db" ;
dnssec-policy "rsasha256" ;
allow-update { any; } ;
} ;
} ;
EOF
fi