limit-keytype.sh revision 1.3
1#	$OpenBSD: limit-keytype.sh,v 1.3 2015/10/26 02:50:58 dtucker Exp $
2#	Placed in the Public Domain.
3
4tid="restrict pubkey type"
5
6rm -f $OBJ/authorized_keys_$USER $OBJ/user_ca_key* $OBJ/user_key*
7rm -f $OBJ/authorized_principals_$USER $OBJ/cert_user_key*
8
9mv $OBJ/sshd_proxy $OBJ/sshd_proxy.orig
10mv $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 -s $OBJ/user_ca_key -I "regress user key for $USER" \
24	-z $$ -n ${USER},mekmitasdigoat $OBJ/user_key3 ||
25		fatal "couldn't sign user_key1"
26# Copy the private key alongside the cert to allow better control of when
27# it is offered.
28mv $OBJ/user_key3-cert.pub $OBJ/cert_user_key3.pub
29
30grep -v IdentityFile $OBJ/ssh_proxy.orig > $OBJ/ssh_proxy
31
32opts="-oProtocol=2 -F $OBJ/ssh_proxy -oIdentitiesOnly=yes"
33certopts="$opts -i $OBJ/user_key3 -oCertificateFile=$OBJ/cert_user_key3.pub"
34
35echo mekmitasdigoat > $OBJ/authorized_principals_$USER
36cat $OBJ/user_key1.pub > $OBJ/authorized_keys_$USER
37cat $OBJ/user_key2.pub >> $OBJ/authorized_keys_$USER
38
39prepare_config() {
40	(
41		grep -v "Protocol"  $OBJ/sshd_proxy.orig
42		echo "Protocol 2"
43		echo "AuthenticationMethods publickey"
44		echo "TrustedUserCAKeys $OBJ/user_ca_key.pub"
45		echo "AuthorizedPrincipalsFile $OBJ/authorized_principals_%u"
46		for x in "$@" ; do
47			echo "$x"
48		done
49 	) > $OBJ/sshd_proxy
50}
51
52prepare_config
53
54# Check we can log in with all key types.
55${SSH} $certopts proxy true || fatal "cert failed"
56${SSH} $opts -i $OBJ/user_key1 proxy true || fatal "key1 failed"
57${SSH} $opts -i $OBJ/user_key2 proxy true || fatal "key2 failed"
58
59# Allow plain Ed25519 and RSA. The certificate should fail.
60verbose "allow rsa,ed25519"
61prepare_config "PubkeyAcceptedKeyTypes ssh-rsa,ssh-ed25519"
62${SSH} $certopts proxy true && fatal "cert succeeded"
63${SSH} $opts -i $OBJ/user_key1 proxy true || fatal "key1 failed"
64${SSH} $opts -i $OBJ/user_key2 proxy true || fatal "key2 failed"
65
66# Allow Ed25519 only.
67verbose "allow ed25519"
68prepare_config "PubkeyAcceptedKeyTypes ssh-ed25519"
69${SSH} $certopts proxy true && fatal "cert succeeded"
70${SSH} $opts -i $OBJ/user_key1 proxy true || fatal "key1 failed"
71${SSH} $opts -i $OBJ/user_key2 proxy true && fatal "key2 succeeded"
72
73# Allow all certs. Plain keys should fail.
74verbose "allow cert only"
75prepare_config "PubkeyAcceptedKeyTypes ssh-*-cert-v01@openssh.com"
76${SSH} $certopts proxy true || fatal "cert failed"
77${SSH} $opts -i $OBJ/user_key1 proxy true && fatal "key1 succeeded"
78${SSH} $opts -i $OBJ/user_key2 proxy true && fatal "key2 succeeded"
79
80