xref: /openbsd/regress/usr.bin/ssh/integrity.sh (revision 3cab2bb3)
1#	$OpenBSD: integrity.sh,v 1.24 2020/01/21 08:06:27 djm 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# sshd-command for proxy (see test-exec.sh)
16cmd="$SUDO env SSH_SK_HELPER="$SSH_SK_HELPER" sh ${SRC}/sshd-log-wrapper.sh ${TEST_SSHD_LOGFILE} ${SSHD} -i -f $OBJ/sshd_proxy"
17
18for m in $macs; do
19	trace "test $tid: mac $m"
20	elen=0
21	epad=0
22	emac=0
23	etmo=0
24	ecnt=0
25	skip=0
26	for off in `jot $tries $startoffset`; do
27		skip=`expr $skip - 1`
28		if [ $skip -gt 0 ]; then
29			# avoid modifying the high bytes of the length
30			continue
31		fi
32		cp $OBJ/sshd_proxy_bak $OBJ/sshd_proxy
33		# modify output from sshd at offset $off
34		pxy="proxycommand=$cmd | $OBJ/modpipe -wm xor:$off:1"
35		if ${SSH} -Q cipher-auth | grep "^${m}\$" >/dev/null 2>&1 ; then
36			echo "Ciphers=$m" >> $OBJ/sshd_proxy
37			macopt="-c $m"
38		else
39			echo "Ciphers=aes128-ctr" >> $OBJ/sshd_proxy
40			echo "MACs=$m" >> $OBJ/sshd_proxy
41			macopt="-m $m -c aes128-ctr"
42		fi
43		verbose "test $tid: $m @$off"
44		${SSH} $macopt -F $OBJ/ssh_proxy -o "$pxy" \
45		    -oServerAliveInterval=1 -oServerAliveCountMax=30 \
46		    999.999.999.999 'printf "%4096s" " "' >/dev/null
47		if [ $? -eq 0 ]; then
48			fail "ssh -m $m succeeds with bit-flip at $off"
49		fi
50		ecnt=`expr $ecnt + 1`
51		out=$(egrep -v "^debug" $TEST_SSH_LOGFILE | tail -2 | \
52		     tr -s '\r\n' '.')
53		case "$out" in
54		Bad?packet*)	elen=`expr $elen + 1`; skip=3;;
55		Corrupted?MAC* | *message?authentication?code?incorrect*)
56				emac=`expr $emac + 1`; skip=0;;
57		padding*)	epad=`expr $epad + 1`; skip=0;;
58		*Timeout,?server*)
59				etmo=`expr $etmo + 1`; skip=0;;
60		*)		fail "unexpected error mac $m at $off: $out";;
61		esac
62	done
63	verbose "test $tid: $ecnt errors: mac $emac padding $epad length $elen timeout $etmo"
64	if [ $emac -eq 0 ]; then
65		fail "$m: no mac errors"
66	fi
67	expect=`expr $ecnt - $epad - $elen - $etmo`
68	if [ $emac -ne $expect ]; then
69		fail "$m: expected $expect mac errors, got $emac"
70	fi
71done
72