1# $OpenBSD: limit-keytype.sh,v 1.11 2025/05/06 06:05:48 djm Exp $ 2# Placed in the Public Domain. 3 4tid="restrict pubkey type" 5 6# XXX sk-* keys aren't actually tested ATM. 7 8rm -f $OBJ/authorized_keys_$USER $OBJ/user_ca_key* $OBJ/user_key* 9rm -f $OBJ/authorized_principals_$USER $OBJ/cert_user_key* 10 11mv $OBJ/sshd_proxy $OBJ/sshd_proxy.orig 12mv $OBJ/ssh_proxy $OBJ/ssh_proxy.orig 13 14ktype1=ed25519; ktype2=ed25519; ktype3=ed25519; 15ktype4=ed25519; ktype5=ed25519; ktype6=ed25519; 16for t in $SSH_KEYTYPES ; do 17 case "$t" in 18 ssh-rsa) ktype2=rsa ;; 19 ecdsa*) ktype3=ecdsa ;; # unused 20 sk-ssh-ed25519@openssh.com) ktype5=ed25519-sk ;; 21 sk-ecdsa-sha2-nistp256@openssh.com) ktype6=ecdsa-sk ;; 22 esac 23done 24 25# Create a CA key 26${SSHKEYGEN} -q -N '' -t $ktype1 -f $OBJ/user_ca_key ||\ 27 fatal "ssh-keygen failed" 28 29# Make some keys and a certificate. 30${SSHKEYGEN} -q -N '' -t $ktype1 -f $OBJ/user_key1 || \ 31 fatal "ssh-keygen failed" 32${SSHKEYGEN} -q -N '' -t $ktype2 -f $OBJ/user_key2 || \ 33 fatal "ssh-keygen failed" 34${SSHKEYGEN} -q -N '' -t $ktype2 -f $OBJ/user_key3 || \ 35 fatal "ssh-keygen failed" 36${SSHKEYGEN} -q -N '' -t $ktype4 -f $OBJ/user_key4 || \ 37 fatal "ssh-keygen failed" 38${SSHKEYGEN} -q -N '' -t $ktype5 -f $OBJ/user_key5 || \ 39 fatal "ssh-keygen failed" 40${SSHKEYGEN} -q -N '' -t $ktype6 -f $OBJ/user_key6 || \ 41 fatal "ssh-keygen failed" 42${SSHKEYGEN} -q -s $OBJ/user_ca_key -I "regress user key for $USER" \ 43 -z $$ -n ${USER},mekmitasdigoat $OBJ/user_key3 || 44 fatal "couldn't sign user_key1" 45# Copy the private key alongside the cert to allow better control of when 46# it is offered. 47mv $OBJ/user_key3-cert.pub $OBJ/cert_user_key3.pub 48 49grep -v IdentityFile $OBJ/ssh_proxy.orig > $OBJ/ssh_proxy 50 51opts="-oProtocol=2 -F $OBJ/ssh_proxy -oIdentitiesOnly=yes" 52certopts="$opts -i $OBJ/user_key3 -oCertificateFile=$OBJ/cert_user_key3.pub" 53 54echo mekmitasdigoat > $OBJ/authorized_principals_$USER 55cat $OBJ/user_key1.pub > $OBJ/authorized_keys_$USER 56cat $OBJ/user_key2.pub >> $OBJ/authorized_keys_$USER 57 58prepare_config() { 59 ( 60 grep -v "Protocol" $OBJ/sshd_proxy.orig 61 echo "Protocol 2" 62 echo "AuthenticationMethods publickey" 63 echo "TrustedUserCAKeys $OBJ/user_ca_key.pub" 64 echo "AuthorizedPrincipalsFile $OBJ/authorized_principals_%u" 65 for x in "$@" ; do 66 echo "$x" 67 done 68 ) > $OBJ/sshd_proxy 69} 70 71# Return the required parameter for PubkeyAcceptedAlgorithms corresponding to 72# the supplied key type. 73keytype() { 74 case "$1" in 75 ecdsa) printf "ecdsa-sha2-*" ;; 76 ed25519) printf "ssh-ed25519" ;; 77 rsa) printf "rsa-sha2-256,rsa-sha2-512,ssh-rsa" ;; 78 sk-ecdsa) printf "sk-ecdsa-*" ;; 79 sk-ssh-ed25519) printf "sk-ssh-ed25519-*" ;; 80 esac 81} 82 83prepare_config 84 85# Check we can log in with all key types. 86${SSH} $certopts proxy true || fatal "cert failed" 87${SSH} $opts -i $OBJ/user_key1 proxy true || fatal "key1 failed" 88${SSH} $opts -i $OBJ/user_key2 proxy true || fatal "key2 failed" 89 90# Allow plain Ed25519 and RSA. The certificate should fail. 91verbose "allow $ktype2,$ktype1" 92prepare_config \ 93 "PubkeyAcceptedAlgorithms `keytype $ktype2`,`keytype $ktype1`" 94${SSH} $certopts proxy true && fatal "cert succeeded" 95${SSH} $opts -i $OBJ/user_key1 proxy true || fatal "key1 failed" 96${SSH} $opts -i $OBJ/user_key2 proxy true || fatal "key2 failed" 97 98# Allow Ed25519 only. 99verbose "allow $ktype1" 100prepare_config "PubkeyAcceptedAlgorithms `keytype $ktype1`" 101${SSH} $certopts proxy true && fatal "cert succeeded" 102${SSH} $opts -i $OBJ/user_key1 proxy true || fatal "key1 failed" 103if [ "$ktype1" != "$ktype2" ]; then 104 ${SSH} $opts -i $OBJ/user_key2 proxy true && fatal "key2 succeeded" 105fi 106 107# Allow all certs. Plain keys should fail. 108verbose "allow cert only" 109prepare_config "PubkeyAcceptedAlgorithms *-cert-v01@openssh.com" 110${SSH} $certopts proxy true || fatal "cert failed" 111${SSH} $opts -i $OBJ/user_key1 proxy true && fatal "key1 succeeded" 112${SSH} $opts -i $OBJ/user_key2 proxy true && fatal "key2 succeeded" 113 114# Allow RSA in main config, Ed25519 for non-existent user. 115verbose "match w/ no match" 116prepare_config "PubkeyAcceptedAlgorithms `keytype $ktype2`" \ 117 "Match user x$USER" "PubkeyAcceptedAlgorithms +`keytype $ktype1`" 118${SSH} $certopts proxy true && fatal "cert succeeded" 119if [ "$ktype1" != "$ktype2" ]; then 120 ${SSH} $opts -i $OBJ/user_key1 proxy true && fatal "key1 succeeded" 121fi 122${SSH} $opts -i $OBJ/user_key2 proxy true || fatal "key2 failed" 123 124# Allow only Ed25519 in main config, Ed25519 for user. 125verbose "match w/ matching" 126prepare_config "PubkeyAcceptedAlgorithms `keytype $ktype4`" \ 127 "Match user $USER" "PubkeyAcceptedAlgorithms +`keytype $ktype1`" 128${SSH} $certopts proxy true || fatal "cert failed" 129${SSH} $opts -i $OBJ/user_key1 proxy true || fatal "key1 failed" 130${SSH} $opts -i $OBJ/user_key4 proxy true && fatal "key4 succeeded" 131 132