xref: /freebsd/crypto/openssh/regress/multipubkey.sh (revision 19261079b74319502c6ffa1249920079f0f69a72)
1*19261079SEd Maste#	$OpenBSD: multipubkey.sh,v 1.4 2021/06/07 01:16:34 djm Exp $
2bc5531deSDag-Erling Smørgrav#	Placed in the Public Domain.
3bc5531deSDag-Erling Smørgrav
4bc5531deSDag-Erling Smørgravtid="multiple pubkey"
5bc5531deSDag-Erling Smørgrav
6bc5531deSDag-Erling Smørgravrm -f $OBJ/authorized_keys_$USER $OBJ/user_ca_key* $OBJ/user_key*
7bc5531deSDag-Erling Smørgravrm -f $OBJ/authorized_principals_$USER $OBJ/cert_user_key*
8bc5531deSDag-Erling Smørgrav
9bc5531deSDag-Erling Smørgravmv $OBJ/sshd_proxy $OBJ/sshd_proxy.orig
10bc5531deSDag-Erling Smørgravmv $OBJ/ssh_proxy $OBJ/ssh_proxy.orig
11bc5531deSDag-Erling Smørgrav
12bc5531deSDag-Erling Smørgrav# Create a CA key
13bc5531deSDag-Erling Smørgrav${SSHKEYGEN} -q -N '' -t ed25519  -f $OBJ/user_ca_key ||\
14bc5531deSDag-Erling Smørgrav	fatal "ssh-keygen failed"
15bc5531deSDag-Erling Smørgrav
16bc5531deSDag-Erling Smørgrav# Make some keys and a certificate.
17bc5531deSDag-Erling Smørgrav${SSHKEYGEN} -q -N '' -t ed25519 -f $OBJ/user_key1 || \
18bc5531deSDag-Erling Smørgrav	fatal "ssh-keygen failed"
19bc5531deSDag-Erling Smørgrav${SSHKEYGEN} -q -N '' -t ed25519 -f $OBJ/user_key2 || \
20bc5531deSDag-Erling Smørgrav	fatal "ssh-keygen failed"
21bc5531deSDag-Erling Smørgrav${SSHKEYGEN} -q -s $OBJ/user_ca_key -I "regress user key for $USER" \
22bc5531deSDag-Erling Smørgrav	-z $$ -n ${USER},mekmitasdigoat $OBJ/user_key1 ||
23bc5531deSDag-Erling Smørgrav		fail "couldn't sign user_key1"
24bc5531deSDag-Erling Smørgrav# Copy the private key alongside the cert to allow better control of when
25bc5531deSDag-Erling Smørgrav# it is offered.
26bc5531deSDag-Erling Smørgravmv $OBJ/user_key1-cert.pub $OBJ/cert_user_key1.pub
27bc5531deSDag-Erling Smørgravcp -p $OBJ/user_key1 $OBJ/cert_user_key1
28bc5531deSDag-Erling Smørgrav
29bc5531deSDag-Erling Smørgravgrep -v IdentityFile $OBJ/ssh_proxy.orig > $OBJ/ssh_proxy
30bc5531deSDag-Erling Smørgrav
31bc5531deSDag-Erling Smørgravopts="-oProtocol=2 -F $OBJ/ssh_proxy -oIdentitiesOnly=yes"
32bc5531deSDag-Erling Smørgravopts="$opts -i $OBJ/cert_user_key1 -i $OBJ/user_key1 -i $OBJ/user_key2"
33bc5531deSDag-Erling Smørgrav
34*19261079SEd Mastefor match in no yes ; do
35bc5531deSDag-Erling Smørgrav	(
36*19261079SEd Maste		cat  $OBJ/sshd_proxy.orig
37bc5531deSDag-Erling Smørgrav		echo "Protocol 2"
38bc5531deSDag-Erling Smørgrav		echo "TrustedUserCAKeys $OBJ/user_ca_key.pub"
39bc5531deSDag-Erling Smørgrav		echo "AuthorizedPrincipalsFile $OBJ/authorized_principals_%u"
40bc5531deSDag-Erling Smørgrav 	) > $OBJ/sshd_proxy
41*19261079SEd Maste	if test "$match" = "yes" ; then
42*19261079SEd Maste		echo "AuthenticationMethods none" >> $OBJ/sshd_proxy
43*19261079SEd Maste		echo "PubkeyAuthentication no" >> $OBJ/sshd_proxy
44*19261079SEd Maste		echo "Match all" >> $OBJ/sshd_proxy
45*19261079SEd Maste		echo "PubkeyAuthentication yes" >> $OBJ/sshd_proxy
46*19261079SEd Maste	fi
47*19261079SEd Maste	echo "AuthenticationMethods publickey,publickey" >> $OBJ/sshd_proxy
48bc5531deSDag-Erling Smørgrav
49bc5531deSDag-Erling Smørgrav	# Single key should fail.
50*19261079SEd Maste	trace "match $match single key"
51bc5531deSDag-Erling Smørgrav	rm -f $OBJ/authorized_principals_$USER
52bc5531deSDag-Erling Smørgrav	cat $OBJ/user_key1.pub > $OBJ/authorized_keys_$USER
53bc5531deSDag-Erling Smørgrav	${SSH} $opts proxy true && fail "ssh succeeded with key"
54bc5531deSDag-Erling Smørgrav
55bc5531deSDag-Erling Smørgrav	# Single key with same-public cert should fail.
56*19261079SEd Maste	trace "match $match pubkey + identical cert"
57bc5531deSDag-Erling Smørgrav	echo mekmitasdigoat > $OBJ/authorized_principals_$USER
58bc5531deSDag-Erling Smørgrav	cat $OBJ/user_key1.pub > $OBJ/authorized_keys_$USER
59bc5531deSDag-Erling Smørgrav	${SSH} $opts proxy true && fail "ssh succeeded with key+cert"
60bc5531deSDag-Erling Smørgrav
61bc5531deSDag-Erling Smørgrav	# Multiple plain keys should succeed.
62*19261079SEd Maste	trace "match $match multiple public"
63bc5531deSDag-Erling Smørgrav	rm -f $OBJ/authorized_principals_$USER
64bc5531deSDag-Erling Smørgrav	cat $OBJ/user_key1.pub $OBJ/user_key2.pub > \
65bc5531deSDag-Erling Smørgrav	    $OBJ/authorized_keys_$USER
66bc5531deSDag-Erling Smørgrav	${SSH} $opts proxy true || fail "ssh failed with multiple keys"
67bc5531deSDag-Erling Smørgrav	# Cert and different key should succeed
68bc5531deSDag-Erling Smørgrav
69bc5531deSDag-Erling Smørgrav	# Key and different-public cert should succeed.
70*19261079SEd Maste	trace "match $match pubkey + different cert"
71bc5531deSDag-Erling Smørgrav	echo mekmitasdigoat > $OBJ/authorized_principals_$USER
72bc5531deSDag-Erling Smørgrav	cat $OBJ/user_key2.pub > $OBJ/authorized_keys_$USER
73bc5531deSDag-Erling Smørgrav	${SSH} $opts proxy true || fail "ssh failed with key/cert"
74bc5531deSDag-Erling Smørgravdone
75bc5531deSDag-Erling Smørgrav
76