2021-02-25 04:27:34 +01:00
|
|
|
# $OpenBSD: limit-keytype.sh,v 1.10 2021/02/25 03:27:34 djm Exp $
|
2015-01-13 08:49:49 +01:00
|
|
|
# Placed in the Public Domain.
|
|
|
|
|
|
|
|
tid="restrict pubkey type"
|
|
|
|
|
2019-11-27 00:43:10 +01:00
|
|
|
# XXX sk-* keys aren't actually tested ATM.
|
|
|
|
|
2015-01-13 08:49:49 +01:00
|
|
|
rm -f $OBJ/authorized_keys_$USER $OBJ/user_ca_key* $OBJ/user_key*
|
|
|
|
rm -f $OBJ/authorized_principals_$USER $OBJ/cert_user_key*
|
|
|
|
|
|
|
|
mv $OBJ/sshd_proxy $OBJ/sshd_proxy.orig
|
|
|
|
mv $OBJ/ssh_proxy $OBJ/ssh_proxy.orig
|
|
|
|
|
2019-11-27 00:43:10 +01:00
|
|
|
ktype1=ed25519; ktype2=ed25519; ktype3=ed25519;
|
|
|
|
ktype4=ed25519; ktype5=ed25519; ktype6=ed25519;
|
2019-12-16 03:39:05 +01:00
|
|
|
for t in $SSH_KEYTYPES ; do
|
2019-07-26 06:22:21 +02:00
|
|
|
case "$t" in
|
|
|
|
ssh-rsa) ktype2=rsa ;;
|
|
|
|
ecdsa*) ktype3=ecdsa ;; # unused
|
|
|
|
ssh-dss) ktype4=dsa ;;
|
2019-11-27 00:43:10 +01:00
|
|
|
sk-ssh-ed25519@openssh.com) ktype5=ed25519-sk ;;
|
|
|
|
sk-ecdsa-sha2-nistp256@openssh.com) ktype6=ecdsa-sk ;;
|
2019-07-26 06:22:21 +02:00
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2015-01-13 08:49:49 +01:00
|
|
|
# Create a CA key
|
2019-07-26 06:22:21 +02:00
|
|
|
${SSHKEYGEN} -q -N '' -t $ktype1 -f $OBJ/user_ca_key ||\
|
2015-01-13 08:49:49 +01:00
|
|
|
fatal "ssh-keygen failed"
|
|
|
|
|
|
|
|
# Make some keys and a certificate.
|
2019-07-26 06:22:21 +02:00
|
|
|
${SSHKEYGEN} -q -N '' -t $ktype1 -f $OBJ/user_key1 || \
|
2015-01-13 08:49:49 +01:00
|
|
|
fatal "ssh-keygen failed"
|
2019-07-26 06:22:21 +02:00
|
|
|
${SSHKEYGEN} -q -N '' -t $ktype2 -f $OBJ/user_key2 || \
|
2015-01-13 08:49:49 +01:00
|
|
|
fatal "ssh-keygen failed"
|
2019-07-26 06:22:21 +02:00
|
|
|
${SSHKEYGEN} -q -N '' -t $ktype2 -f $OBJ/user_key3 || \
|
2015-01-13 08:49:49 +01:00
|
|
|
fatal "ssh-keygen failed"
|
2019-07-26 06:22:21 +02:00
|
|
|
${SSHKEYGEN} -q -N '' -t $ktype4 -f $OBJ/user_key4 || \
|
2015-10-29 09:05:17 +01:00
|
|
|
fatal "ssh-keygen failed"
|
2019-11-27 00:43:10 +01:00
|
|
|
${SSHKEYGEN} -q -N '' -t $ktype5 -f $OBJ/user_key5 || \
|
|
|
|
fatal "ssh-keygen failed"
|
|
|
|
${SSHKEYGEN} -q -N '' -t $ktype6 -f $OBJ/user_key6 || \
|
|
|
|
fatal "ssh-keygen failed"
|
2015-01-13 08:49:49 +01:00
|
|
|
${SSHKEYGEN} -q -s $OBJ/user_ca_key -I "regress user key for $USER" \
|
|
|
|
-z $$ -n ${USER},mekmitasdigoat $OBJ/user_key3 ||
|
|
|
|
fatal "couldn't sign user_key1"
|
|
|
|
# Copy the private key alongside the cert to allow better control of when
|
|
|
|
# it is offered.
|
|
|
|
mv $OBJ/user_key3-cert.pub $OBJ/cert_user_key3.pub
|
|
|
|
|
|
|
|
grep -v IdentityFile $OBJ/ssh_proxy.orig > $OBJ/ssh_proxy
|
|
|
|
|
|
|
|
opts="-oProtocol=2 -F $OBJ/ssh_proxy -oIdentitiesOnly=yes"
|
2015-09-24 08:16:53 +02:00
|
|
|
certopts="$opts -i $OBJ/user_key3 -oCertificateFile=$OBJ/cert_user_key3.pub"
|
2015-01-13 08:49:49 +01:00
|
|
|
|
|
|
|
echo mekmitasdigoat > $OBJ/authorized_principals_$USER
|
|
|
|
cat $OBJ/user_key1.pub > $OBJ/authorized_keys_$USER
|
|
|
|
cat $OBJ/user_key2.pub >> $OBJ/authorized_keys_$USER
|
|
|
|
|
|
|
|
prepare_config() {
|
|
|
|
(
|
|
|
|
grep -v "Protocol" $OBJ/sshd_proxy.orig
|
|
|
|
echo "Protocol 2"
|
|
|
|
echo "AuthenticationMethods publickey"
|
|
|
|
echo "TrustedUserCAKeys $OBJ/user_ca_key.pub"
|
|
|
|
echo "AuthorizedPrincipalsFile $OBJ/authorized_principals_%u"
|
|
|
|
for x in "$@" ; do
|
|
|
|
echo "$x"
|
|
|
|
done
|
|
|
|
) > $OBJ/sshd_proxy
|
|
|
|
}
|
|
|
|
|
2021-02-25 04:27:34 +01:00
|
|
|
# Return the required parameter for PubkeyAcceptedAlgorithms corresponding to
|
2019-07-26 06:22:21 +02:00
|
|
|
# the supplied key type.
|
|
|
|
keytype() {
|
|
|
|
case "$1" in
|
|
|
|
ecdsa) printf "ecdsa-sha2-*" ;;
|
|
|
|
ed25519) printf "ssh-ed25519" ;;
|
|
|
|
dsa) printf "ssh-dss" ;;
|
|
|
|
rsa) printf "rsa-sha2-256,rsa-sha2-512,ssh-rsa" ;;
|
2019-11-27 00:43:10 +01:00
|
|
|
sk-ecdsa) printf "sk-ecdsa-*" ;;
|
|
|
|
sk-ssh-ed25519) printf "sk-ssh-ed25519-*" ;;
|
2019-07-26 06:22:21 +02:00
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
2015-01-13 08:49:49 +01:00
|
|
|
prepare_config
|
|
|
|
|
|
|
|
# Check we can log in with all key types.
|
2015-09-24 08:16:53 +02:00
|
|
|
${SSH} $certopts proxy true || fatal "cert failed"
|
2015-01-13 08:49:49 +01:00
|
|
|
${SSH} $opts -i $OBJ/user_key1 proxy true || fatal "key1 failed"
|
|
|
|
${SSH} $opts -i $OBJ/user_key2 proxy true || fatal "key2 failed"
|
|
|
|
|
|
|
|
# Allow plain Ed25519 and RSA. The certificate should fail.
|
2019-07-26 06:22:21 +02:00
|
|
|
verbose "allow $ktype2,$ktype1"
|
2018-03-12 01:52:57 +01:00
|
|
|
prepare_config \
|
2021-02-25 04:27:34 +01:00
|
|
|
"PubkeyAcceptedAlgorithms `keytype $ktype2`,`keytype $ktype1`"
|
2015-10-26 03:50:58 +01:00
|
|
|
${SSH} $certopts proxy true && fatal "cert succeeded"
|
2015-01-13 08:49:49 +01:00
|
|
|
${SSH} $opts -i $OBJ/user_key1 proxy true || fatal "key1 failed"
|
|
|
|
${SSH} $opts -i $OBJ/user_key2 proxy true || fatal "key2 failed"
|
|
|
|
|
|
|
|
# Allow Ed25519 only.
|
2019-07-26 06:22:21 +02:00
|
|
|
verbose "allow $ktype1"
|
2021-02-25 04:27:34 +01:00
|
|
|
prepare_config "PubkeyAcceptedAlgorithms `keytype $ktype1`"
|
2015-09-24 08:16:53 +02:00
|
|
|
${SSH} $certopts proxy true && fatal "cert succeeded"
|
2015-01-13 08:49:49 +01:00
|
|
|
${SSH} $opts -i $OBJ/user_key1 proxy true || fatal "key1 failed"
|
2019-07-26 06:22:21 +02:00
|
|
|
if [ "$ktype1" != "$ktype2" ]; then
|
|
|
|
${SSH} $opts -i $OBJ/user_key2 proxy true && fatal "key2 succeeded"
|
|
|
|
fi
|
2015-01-13 08:49:49 +01:00
|
|
|
|
|
|
|
# Allow all certs. Plain keys should fail.
|
2015-09-24 08:16:53 +02:00
|
|
|
verbose "allow cert only"
|
2021-02-25 04:27:34 +01:00
|
|
|
prepare_config "PubkeyAcceptedAlgorithms *-cert-v01@openssh.com"
|
2015-09-24 08:16:53 +02:00
|
|
|
${SSH} $certopts proxy true || fatal "cert failed"
|
2015-01-13 08:49:49 +01:00
|
|
|
${SSH} $opts -i $OBJ/user_key1 proxy true && fatal "key1 succeeded"
|
|
|
|
${SSH} $opts -i $OBJ/user_key2 proxy true && fatal "key2 succeeded"
|
|
|
|
|
2015-10-29 09:05:17 +01:00
|
|
|
# Allow RSA in main config, Ed25519 for non-existent user.
|
|
|
|
verbose "match w/ no match"
|
2021-02-25 04:27:34 +01:00
|
|
|
prepare_config "PubkeyAcceptedAlgorithms `keytype $ktype2`" \
|
|
|
|
"Match user x$USER" "PubkeyAcceptedAlgorithms +`keytype $ktype1`"
|
2015-10-29 09:05:17 +01:00
|
|
|
${SSH} $certopts proxy true && fatal "cert succeeded"
|
2019-07-26 06:22:21 +02:00
|
|
|
if [ "$ktype1" != "$ktype2" ]; then
|
|
|
|
${SSH} $opts -i $OBJ/user_key1 proxy true && fatal "key1 succeeded"
|
|
|
|
fi
|
2015-10-29 09:05:17 +01:00
|
|
|
${SSH} $opts -i $OBJ/user_key2 proxy true || fatal "key2 failed"
|
|
|
|
|
|
|
|
# Allow only DSA in main config, Ed25519 for user.
|
|
|
|
verbose "match w/ matching"
|
2021-02-25 04:27:34 +01:00
|
|
|
prepare_config "PubkeyAcceptedAlgorithms `keytype $ktype4`" \
|
|
|
|
"Match user $USER" "PubkeyAcceptedAlgorithms +`keytype $ktype1`"
|
2015-10-29 09:05:17 +01:00
|
|
|
${SSH} $certopts proxy true || fatal "cert failed"
|
|
|
|
${SSH} $opts -i $OBJ/user_key1 proxy true || fatal "key1 failed"
|
|
|
|
${SSH} $opts -i $OBJ/user_key4 proxy true && fatal "key4 succeeded"
|
|
|
|
|