2015-01-10 07:14:29 +01:00
|
|
|
#!/bin/sh
|
|
|
|
# Check that tor regenerates keys when key files are zero-length
|
|
|
|
# Test for bug #13111 - Tor fails to start if onion keys are zero length
|
|
|
|
#
|
|
|
|
# Usage:
|
2015-03-05 09:03:06 +01:00
|
|
|
# ./zero_length_keys.sh PATH_TO_TOR
|
2015-01-10 07:14:29 +01:00
|
|
|
# Run all the tests below
|
2015-03-05 09:03:06 +01:00
|
|
|
# ./zero_length_keys.sh PATH_TO_TOR -z
|
2015-01-10 07:14:29 +01:00
|
|
|
# Check tor will launch and regenerate zero-length keys
|
2015-03-05 09:03:06 +01:00
|
|
|
# ./zero_length_keys.sh PATH_TO_TOR -d
|
2015-01-10 07:14:29 +01:00
|
|
|
# Check tor regenerates deleted keys (existing behaviour)
|
2015-03-05 09:03:06 +01:00
|
|
|
# ./zero_length_keys.sh PATH_TO_TOR -e
|
2015-01-10 07:14:29 +01:00
|
|
|
# Check tor does not overwrite existing keys (existing behaviour)
|
|
|
|
#
|
|
|
|
# Exit Statuses:
|
|
|
|
# 0: test succeeded - tor regenerated/kept the files
|
|
|
|
# 1: test failed - tor did not regenerate/keep the files
|
2015-02-11 21:40:49 +01:00
|
|
|
# 2: test failed - tor did not generate the key files on first run
|
|
|
|
# 3: a command failed - the test could not be completed
|
2015-01-10 07:14:29 +01:00
|
|
|
#
|
|
|
|
|
2019-02-07 16:22:06 +01:00
|
|
|
if [ $# -eq 0 ] || [ ! -f "${1}" ] || [ ! -x "${1}" ]; then
|
2015-03-05 09:03:06 +01:00
|
|
|
echo "Usage: ${0} PATH_TO_TOR [-z|-d|-e]"
|
|
|
|
exit 1
|
|
|
|
elif [ $# -eq 1 ]; then
|
2015-01-10 07:14:29 +01:00
|
|
|
echo "Testing that tor correctly handles zero-length keys"
|
2015-03-05 09:03:06 +01:00
|
|
|
"$0" "${1}" -z && "$0" "${1}" -d && "$0" "${1}" -e
|
2015-01-10 07:14:29 +01:00
|
|
|
exit $?
|
2015-03-05 09:03:06 +01:00
|
|
|
else #[$# -gt 1 ]; then
|
|
|
|
TOR_BINARY="${1}"
|
|
|
|
shift
|
2015-01-10 07:14:29 +01:00
|
|
|
fi
|
|
|
|
|
2019-02-07 16:22:06 +01:00
|
|
|
DATA_DIR=$(mktemp -d -t tor_zero_length_keys.XXXXXX)
|
2015-02-06 23:21:20 +01:00
|
|
|
if [ -z "$DATA_DIR" ]; then
|
2015-02-11 21:43:41 +01:00
|
|
|
echo "Failure: mktemp invocation returned empty string" >&2
|
2015-02-11 21:40:49 +01:00
|
|
|
exit 3
|
2015-02-06 23:21:20 +01:00
|
|
|
fi
|
2015-02-07 14:54:21 +01:00
|
|
|
if [ ! -d "$DATA_DIR" ]; then
|
2015-02-11 21:43:41 +01:00
|
|
|
echo "Failure: mktemp invocation result doesn't point to directory" >&2
|
2015-02-11 21:40:49 +01:00
|
|
|
exit 3
|
2015-02-07 14:48:06 +01:00
|
|
|
fi
|
2019-02-07 16:22:06 +01:00
|
|
|
trap 'rm -rf "$DATA_DIR"' 0
|
2015-02-06 23:21:20 +01:00
|
|
|
|
2015-02-26 21:21:31 +01:00
|
|
|
touch "$DATA_DIR"/empty_torrc
|
2019-04-10 11:03:43 +02:00
|
|
|
touch "$DATA_DIR"/empty_defaults_torrc
|
2015-02-26 21:21:31 +01:00
|
|
|
|
2015-01-13 03:28:01 +01:00
|
|
|
# DisableNetwork means that the ORPort won't actually be opened.
|
|
|
|
# 'ExitRelay 0' suppresses a warning.
|
2019-04-10 11:03:43 +02:00
|
|
|
TOR="${TOR_BINARY} --hush --DisableNetwork 1 --ShutdownWaitLength 0 --ORPort 12345 --ExitRelay 0 -f $DATA_DIR/empty_torrc --defaults-torrc $DATA_DIR/empty_defaults_torrc"
|
2015-01-10 07:14:29 +01:00
|
|
|
|
2015-02-07 14:54:21 +01:00
|
|
|
if [ -s "$DATA_DIR"/keys/secret_id_key ] && [ -s "$DATA_DIR"/keys/secret_onion_key ] &&
|
|
|
|
[ -s "$DATA_DIR"/keys/secret_onion_key_ntor ]; then
|
2015-02-11 21:43:41 +01:00
|
|
|
echo "Failure: Previous tor keys present in tor data directory" >&2
|
2015-02-11 21:40:49 +01:00
|
|
|
exit 3
|
2015-01-10 07:14:29 +01:00
|
|
|
else
|
|
|
|
echo "Generating initial tor keys"
|
2015-02-17 14:30:32 +01:00
|
|
|
$TOR --DataDirectory "$DATA_DIR" --list-fingerprint
|
2015-01-10 07:14:29 +01:00
|
|
|
|
|
|
|
# tor must successfully generate non-zero-length key files
|
2015-02-07 14:54:21 +01:00
|
|
|
if [ -s "$DATA_DIR"/keys/secret_id_key ] && [ -s "$DATA_DIR"/keys/secret_onion_key ] &&
|
|
|
|
[ -s "$DATA_DIR"/keys/secret_onion_key_ntor ]; then
|
2015-01-10 07:14:29 +01:00
|
|
|
true #echo "tor generated the initial key files"
|
|
|
|
else
|
|
|
|
echo "Failure: tor failed to generate the initial key files"
|
2015-02-11 21:40:49 +01:00
|
|
|
exit 2
|
2015-01-10 07:14:29 +01:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2015-02-11 21:40:49 +01:00
|
|
|
#ls -lh "$DATA_DIR"/keys/ || exit 3
|
2015-01-10 07:14:29 +01:00
|
|
|
|
|
|
|
# backup and keep/delete/create zero-length files for the keys
|
|
|
|
|
|
|
|
FILE_DESC="keeps existing"
|
|
|
|
# make a backup
|
|
|
|
cp -r "$DATA_DIR"/keys "$DATA_DIR"/keys.old
|
|
|
|
|
|
|
|
# delete keys for -d or -z
|
|
|
|
if [ "$1" != "-e" ]; then
|
|
|
|
FILE_DESC="regenerates deleted"
|
2015-02-11 21:40:49 +01:00
|
|
|
rm "$DATA_DIR"/keys/secret_id_key || exit 3
|
|
|
|
rm "$DATA_DIR"/keys/secret_onion_key || exit 3
|
|
|
|
rm "$DATA_DIR"/keys/secret_onion_key_ntor || exit 3
|
2015-01-10 07:14:29 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
# create empty files for -z
|
2015-01-13 04:30:40 +01:00
|
|
|
if [ "$1" = "-z" ]; then
|
2015-01-10 07:14:29 +01:00
|
|
|
FILE_DESC="regenerates zero-length"
|
2015-02-11 21:40:49 +01:00
|
|
|
touch "$DATA_DIR"/keys/secret_id_key || exit 3
|
|
|
|
touch "$DATA_DIR"/keys/secret_onion_key || exit 3
|
|
|
|
touch "$DATA_DIR"/keys/secret_onion_key_ntor || exit 3
|
2015-01-10 07:14:29 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
echo "Running tor again to check if it $FILE_DESC keys"
|
2015-02-17 14:30:32 +01:00
|
|
|
$TOR --DataDirectory "$DATA_DIR" --list-fingerprint
|
2015-01-10 07:14:29 +01:00
|
|
|
|
2015-02-11 21:40:49 +01:00
|
|
|
#ls -lh "$DATA_DIR"/keys/ || exit 3
|
2015-01-10 07:14:29 +01:00
|
|
|
|
|
|
|
# tor must always have non-zero-length key files
|
2015-02-07 14:54:21 +01:00
|
|
|
if [ -s "$DATA_DIR"/keys/secret_id_key ] && [ -s "$DATA_DIR"/keys/secret_onion_key ] &&
|
|
|
|
[ -s "$DATA_DIR"/keys/secret_onion_key_ntor ]; then
|
2015-01-10 07:14:29 +01:00
|
|
|
# check if the keys are different to the old ones
|
|
|
|
diff -q -r "$DATA_DIR"/keys "$DATA_DIR"/keys.old > /dev/null
|
|
|
|
SAME_KEYS=$?
|
|
|
|
# if we're not testing existing keys,
|
|
|
|
# the current keys should be different to the old ones
|
|
|
|
if [ "$1" != "-e" ]; then
|
|
|
|
if [ $SAME_KEYS -ne 0 ]; then
|
|
|
|
echo "Success: test that tor $FILE_DESC key files: different keys"
|
|
|
|
exit 0
|
|
|
|
else
|
|
|
|
echo "Failure: test that tor $FILE_DESC key files: same keys"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
else #[ "$1" == "-e" ]; then
|
|
|
|
if [ $SAME_KEYS -eq 0 ]; then
|
|
|
|
echo "Success: test that tor $FILE_DESC key files: same keys"
|
|
|
|
exit 0
|
|
|
|
else
|
|
|
|
echo "Failure: test that tor $FILE_DESC key files: different keys"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "Failure: test that tor $FILE_DESC key files: no key files"
|
|
|
|
exit 1
|
|
|
|
fi
|