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