1#	$OpenBSD: multipubkey.sh,v 1.4 2021/06/07 01:16:34 djm Exp $
2#	Placed in the Public Domain.
3
4tid="multiple pubkey"
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 ed25519 -f $OBJ/user_key2 || \
20	fatal "ssh-keygen failed"
21${SSHKEYGEN} -q -s $OBJ/user_ca_key -I "regress user key for $USER" \
22	-z $$ -n ${USER},mekmitasdigoat $OBJ/user_key1 ||
23		fail "couldn't sign user_key1"
24# Copy the private key alongside the cert to allow better control of when
25# it is offered.
26mv $OBJ/user_key1-cert.pub $OBJ/cert_user_key1.pub
27cp -p $OBJ/user_key1 $OBJ/cert_user_key1
28
29grep -v IdentityFile $OBJ/ssh_proxy.orig > $OBJ/ssh_proxy
30
31opts="-oProtocol=2 -F $OBJ/ssh_proxy -oIdentitiesOnly=yes"
32opts="$opts -i $OBJ/cert_user_key1 -i $OBJ/user_key1 -i $OBJ/user_key2"
33
34for match in no yes ; do
35	(
36		cat  $OBJ/sshd_proxy.orig
37		echo "Protocol 2"
38		echo "TrustedUserCAKeys $OBJ/user_ca_key.pub"
39		echo "AuthorizedPrincipalsFile $OBJ/authorized_principals_%u"
40 	) > $OBJ/sshd_proxy
41	if test "$match" = "yes" ; then
42		echo "AuthenticationMethods none" >> $OBJ/sshd_proxy
43		echo "PubkeyAuthentication no" >> $OBJ/sshd_proxy
44		echo "Match all" >> $OBJ/sshd_proxy
45		echo "PubkeyAuthentication yes" >> $OBJ/sshd_proxy
46	fi
47	echo "AuthenticationMethods publickey,publickey" >> $OBJ/sshd_proxy
48
49	# Single key should fail.
50	trace "match $match single key"
51	rm -f $OBJ/authorized_principals_$USER
52	cat $OBJ/user_key1.pub > $OBJ/authorized_keys_$USER
53	${SSH} $opts proxy true && fail "ssh succeeded with key"
54
55	# Single key with same-public cert should fail.
56	trace "match $match pubkey + identical cert"
57	echo mekmitasdigoat > $OBJ/authorized_principals_$USER
58	cat $OBJ/user_key1.pub > $OBJ/authorized_keys_$USER
59	${SSH} $opts proxy true && fail "ssh succeeded with key+cert"
60
61	# Multiple plain keys should succeed.
62	trace "match $match multiple public"
63	rm -f $OBJ/authorized_principals_$USER
64	cat $OBJ/user_key1.pub $OBJ/user_key2.pub > \
65	    $OBJ/authorized_keys_$USER
66	${SSH} $opts proxy true || fail "ssh failed with multiple keys"
67	# Cert and different key should succeed
68
69	# Key and different-public cert should succeed.
70	trace "match $match pubkey + different cert"
71	echo mekmitasdigoat > $OBJ/authorized_principals_$USER
72	cat $OBJ/user_key2.pub > $OBJ/authorized_keys_$USER
73	${SSH} $opts proxy true || fail "ssh failed with key/cert"
74done
75
76