1#	$OpenBSD: integrity.sh,v 1.25 2023/03/01 09:29:32 dtucker Exp $
2#	Placed in the Public Domain.
3
4tid="integrity"
5cp $OBJ/sshd_proxy $OBJ/sshd_proxy_bak
6
7# start at byte 2900 (i.e. after kex) and corrupt at different offsets
8tries=10
9startoffset=2900
10macs=`${SSH} -Q mac`
11# The following are not MACs, but ciphers with integrated integrity. They are
12# handled specially below.
13macs="$macs `${SSH} -Q cipher-auth`"
14
15# avoid DH group exchange as the extra traffic makes it harder to get the
16# offset into the stream right.
17#echo "KexAlgorithms -diffie-hellman-group*" \
18#	>> $OBJ/ssh_proxy
19
20# sshd-command for proxy (see test-exec.sh)
21cmd="$SUDO env SSH_SK_HELPER="$SSH_SK_HELPER" sh ${OBJ}/sshd-log-wrapper.sh -i -f $OBJ/sshd_proxy"
22
23for m in $macs; do
24	trace "test $tid: mac $m"
25	elen=0
26	epad=0
27	emac=0
28	etmo=0
29	ecnt=0
30	skip=0
31	for off in `jot $tries $startoffset`; do
32		skip=`expr $skip - 1`
33		if [ $skip -gt 0 ]; then
34			# avoid modifying the high bytes of the length
35			continue
36		fi
37		cp $OBJ/sshd_proxy_bak $OBJ/sshd_proxy
38		# modify output from sshd at offset $off
39		pxy="proxycommand=$cmd | $OBJ/modpipe -wm xor:$off:1"
40		if ${SSH} -Q cipher-auth | grep "^${m}\$" >/dev/null 2>&1 ; then
41			echo "Ciphers=$m" >> $OBJ/sshd_proxy
42			macopt="-c $m"
43		else
44			echo "Ciphers=aes128-ctr" >> $OBJ/sshd_proxy
45			echo "MACs=$m" >> $OBJ/sshd_proxy
46			macopt="-m $m -c aes128-ctr"
47		fi
48		verbose "test $tid: $m @$off"
49		${SSH} $macopt -F $OBJ/ssh_proxy -o "$pxy" \
50		    -oServerAliveInterval=1 -oServerAliveCountMax=30 \
51		    999.999.999.999 'printf "%4096s" " "' >/dev/null
52		if [ $? -eq 0 ]; then
53			fail "ssh -m $m succeeds with bit-flip at $off"
54		fi
55		ecnt=`expr $ecnt + 1`
56		out=$(egrep -v "^debug" $TEST_SSH_LOGFILE | tail -2 | \
57		     tr -s '\r\n' '.')
58		case "$out" in
59		Bad?packet*)	elen=`expr $elen + 1`; skip=3;;
60		Corrupted?MAC* | *message?authentication?code?incorrect*)
61				emac=`expr $emac + 1`; skip=0;;
62		padding*)	epad=`expr $epad + 1`; skip=0;;
63		*Timeout,?server*)
64				etmo=`expr $etmo + 1`; skip=0;;
65		*)		fail "unexpected error mac $m at $off: $out";;
66		esac
67	done
68	verbose "test $tid: $ecnt errors: mac $emac padding $epad length $elen timeout $etmo"
69	if [ $emac -eq 0 ]; then
70		fail "$m: no mac errors"
71	fi
72	expect=`expr $ecnt - $epad - $elen - $etmo`
73	if [ $emac -ne $expect ]; then
74		fail "$m: expected $expect mac errors, got $emac"
75	fi
76done
77