1 # $OpenBSD: limit-keytype.sh,v 1.4 2015/10/29 08:05:17 djm Exp $ 2 # Placed in the Public Domain. 3 4 tid="restrict pubkey type" 5 6 rm -f $OBJ/authorized_keys_$USER $OBJ/user_ca_key* $OBJ/user_key* 7 rm -f $OBJ/authorized_principals_$USER $OBJ/cert_user_key* 8 9 mv $OBJ/sshd_proxy $OBJ/sshd_proxy.orig 10 mv $OBJ/ssh_proxy $OBJ/ssh_proxy.orig 11 12 # Create a CA key 13 ${SSHKEYGEN} -q -N '' -t ed25519 -f $OBJ/user_ca_key ||\ 14 fatal "ssh-keygen failed" 15 16 # Make some keys and a certificate. 17 ${SSHKEYGEN} -q -N '' -t ed25519 -f $OBJ/user_key1 || \ 18 fatal "ssh-keygen failed" 19 ${SSHKEYGEN} -q -N '' -t rsa -f $OBJ/user_key2 || \ 20 fatal "ssh-keygen failed" 21 ${SSHKEYGEN} -q -N '' -t rsa -f $OBJ/user_key3 || \ 22 fatal "ssh-keygen failed" 23 ${SSHKEYGEN} -q -N '' -t dsa -f $OBJ/user_key4 || \ 24 fatal "ssh-keygen failed" 25 ${SSHKEYGEN} -q -s $OBJ/user_ca_key -I "regress user key for $USER" \ 26 -z $$ -n ${USER},mekmitasdigoat $OBJ/user_key3 || 27 fatal "couldn't sign user_key1" 28 # Copy the private key alongside the cert to allow better control of when 29 # it is offered. 30 mv $OBJ/user_key3-cert.pub $OBJ/cert_user_key3.pub 31 32 grep -v IdentityFile $OBJ/ssh_proxy.orig > $OBJ/ssh_proxy 33 34 opts="-oProtocol=2 -F $OBJ/ssh_proxy -oIdentitiesOnly=yes" 35 certopts="$opts -i $OBJ/user_key3 -oCertificateFile=$OBJ/cert_user_key3.pub" 36 37 echo mekmitasdigoat > $OBJ/authorized_principals_$USER 38 cat $OBJ/user_key1.pub > $OBJ/authorized_keys_$USER 39 cat $OBJ/user_key2.pub >> $OBJ/authorized_keys_$USER 40 41 prepare_config() { 42 ( 43 grep -v "Protocol" $OBJ/sshd_proxy.orig 44 echo "Protocol 2" 45 echo "AuthenticationMethods publickey" 46 echo "TrustedUserCAKeys $OBJ/user_ca_key.pub" 47 echo "AuthorizedPrincipalsFile $OBJ/authorized_principals_%u" 48 for x in "$@" ; do 49 echo "$x" 50 done 51 ) > $OBJ/sshd_proxy 52 } 53 54 prepare_config 55 56 # Check we can log in with all key types. 57 ${SSH} $certopts proxy true || fatal "cert failed" 58 ${SSH} $opts -i $OBJ/user_key1 proxy true || fatal "key1 failed" 59 ${SSH} $opts -i $OBJ/user_key2 proxy true || fatal "key2 failed" 60 61 # Allow plain Ed25519 and RSA. The certificate should fail. 62 verbose "allow rsa,ed25519" 63 prepare_config "PubkeyAcceptedKeyTypes ssh-rsa,ssh-ed25519" 64 ${SSH} $certopts proxy true && fatal "cert succeeded" 65 ${SSH} $opts -i $OBJ/user_key1 proxy true || fatal "key1 failed" 66 ${SSH} $opts -i $OBJ/user_key2 proxy true || fatal "key2 failed" 67 68 # Allow Ed25519 only. 69 verbose "allow ed25519" 70 prepare_config "PubkeyAcceptedKeyTypes ssh-ed25519" 71 ${SSH} $certopts proxy true && fatal "cert succeeded" 72 ${SSH} $opts -i $OBJ/user_key1 proxy true || fatal "key1 failed" 73 ${SSH} $opts -i $OBJ/user_key2 proxy true && fatal "key2 succeeded" 74 75 # Allow all certs. Plain keys should fail. 76 verbose "allow cert only" 77 prepare_config "PubkeyAcceptedKeyTypes ssh-*-cert-v01 (at] openssh.com" 78 ${SSH} $certopts proxy true || fatal "cert failed" 79 ${SSH} $opts -i $OBJ/user_key1 proxy true && fatal "key1 succeeded" 80 ${SSH} $opts -i $OBJ/user_key2 proxy true && fatal "key2 succeeded" 81 82 # Allow RSA in main config, Ed25519 for non-existent user. 83 verbose "match w/ no match" 84 prepare_config "PubkeyAcceptedKeyTypes ssh-rsa" \ 85 "Match user x$USER" "PubkeyAcceptedKeyTypes +ssh-ed25519" 86 ${SSH} $certopts proxy true && fatal "cert succeeded" 87 ${SSH} $opts -i $OBJ/user_key1 proxy true && fatal "key1 succeeded" 88 ${SSH} $opts -i $OBJ/user_key2 proxy true || fatal "key2 failed" 89 90 # Allow only DSA in main config, Ed25519 for user. 91 verbose "match w/ matching" 92 prepare_config "PubkeyAcceptedKeyTypes ssh-dss" \ 93 "Match user $USER" "PubkeyAcceptedKeyTypes +ssh-ed25519" 94 ${SSH} $certopts proxy true || fatal "cert failed" 95 ${SSH} $opts -i $OBJ/user_key1 proxy true || fatal "key1 failed" 96 ${SSH} $opts -i $OBJ/user_key4 proxy true && fatal "key4 succeeded" 97 98