integrity.sh revision 262566
1#	$OpenBSD: integrity.sh,v 1.12 2013/11/21 03:18:51 djm Exp $
2#	Placed in the Public Domain.
3
4tid="integrity"
5
6# start at byte 2900 (i.e. after kex) and corrupt at different offsets
7# XXX the test hangs if we modify the low bytes of the packet length
8# XXX and ssh tries to read...
9tries=10
10startoffset=2900
11macs=`${SSH} -Q mac`
12# The following are not MACs, but ciphers with integrated integrity. They are
13# handled specially below.
14macs="$macs `${SSH} -Q cipher-auth`"
15
16# avoid DH group exchange as the extra traffic makes it harder to get the
17# offset into the stream right.
18echo "KexAlgorithms diffie-hellman-group14-sha1,diffie-hellman-group1-sha1" \
19	>> $OBJ/ssh_proxy
20
21# sshd-command for proxy (see test-exec.sh)
22cmd="$SUDO sh ${SRC}/sshd-log-wrapper.sh ${SSHD} ${TEST_SSHD_LOGFILE} -i -f $OBJ/sshd_proxy"
23
24for m in $macs; do
25	trace "test $tid: mac $m"
26	elen=0
27	epad=0
28	emac=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		# modify output from sshd at offset $off
38		pxy="proxycommand=$cmd | $OBJ/modpipe -wm xor:$off:1"
39		if ssh -Q cipher-auth | grep "^${m}\$" >/dev/null 2>&1 ; then
40			macopt="-c $m"
41		else
42			macopt="-m $m -c aes128-ctr"
43		fi
44		verbose "test $tid: $m @$off"
45		${SSH} $macopt -2F $OBJ/ssh_proxy -o "$pxy" \
46		    -oServerAliveInterval=1 -oServerAliveCountMax=30 \
47		    999.999.999.999 'printf "%4096s" " "' >/dev/null
48		if [ $? -eq 0 ]; then
49			fail "ssh -m $m succeeds with bit-flip at $off"
50		fi
51		ecnt=`expr $ecnt + 1`
52		output=$(tail -2 $TEST_SSH_LOGFILE | egrep -v "^debug" | \
53		     tr -s '\r\n' '.')
54		case "$output" in
55		Bad?packet*)	elen=`expr $elen + 1`; skip=3;;
56		Corrupted?MAC* | Decryption?integrity?check?failed*)
57				emac=`expr $emac + 1`; skip=0;;
58		padding*)	epad=`expr $epad + 1`; skip=0;;
59		*)		fail "unexpected error mac $m at $off";;
60		esac
61	done
62	verbose "test $tid: $ecnt errors: mac $emac padding $epad length $elen"
63	if [ $emac -eq 0 ]; then
64		fail "$m: no mac errors"
65	fi
66	expect=`expr $ecnt - $epad - $elen`
67	if [ $emac -ne $expect ]; then
68		fail "$m: expected $expect mac errors, got $emac"
69	fi
70done
71