2 # Check that tor regenerates keys when key files are zero-length
3 # Test for bug #13111 - Tor fails to start if onion keys are zero length
6 # ./zero_length_keys.sh PATH_TO_TOR
7 # Run all the tests below
8 # ./zero_length_keys.sh PATH_TO_TOR -z
9 # Check tor will launch and regenerate zero-length keys
10 # ./zero_length_keys.sh PATH_TO_TOR -d
11 # Check tor regenerates deleted keys (existing behaviour)
12 # ./zero_length_keys.sh PATH_TO_TOR -e
13 # Check tor does not overwrite existing keys (existing behaviour)
16 # 0: test succeeded - tor regenerated/kept the files
17 # 1: test failed - tor did not regenerate/keep the files
18 # 2: test failed - tor did not generate the key files on first run
19 # 3: a command failed - the test could not be completed
22 if [ $# -eq 0 ] ||
[ ! -f ${1} ] ||
[ ! -x ${1} ]; then
23 echo "Usage: ${0} PATH_TO_TOR [-z|-d|-e]"
25 elif [ $# -eq 1 ]; then
26 echo "Testing that tor correctly handles zero-length keys"
27 "$0" "${1}" -z && "$0" "${1}" -d && "$0" "${1}" -e
29 else #[$# -gt 1 ]; then
34 DATA_DIR
=`mktemp -d -t tor_zero_length_keys.XXXXXX`
35 if [ -z "$DATA_DIR" ]; then
36 echo "Failure: mktemp invocation returned empty string" >&2
39 if [ ! -d "$DATA_DIR" ]; then
40 echo "Failure: mktemp invocation result doesn't point to directory" >&2
43 trap "rm -rf '$DATA_DIR'" 0
45 touch "$DATA_DIR"/empty_torrc
47 # DisableNetwork means that the ORPort won't actually be opened.
48 # 'ExitRelay 0' suppresses a warning.
49 TOR
="${TOR_BINARY} --hush --DisableNetwork 1 --ShutdownWaitLength 0 --ORPort 12345 --ExitRelay 0 -f $DATA_DIR/empty_torrc"
51 if [ -s "$DATA_DIR"/keys
/secret_id_key
] && [ -s "$DATA_DIR"/keys
/secret_onion_key
] &&
52 [ -s "$DATA_DIR"/keys
/secret_onion_key_ntor
]; then
53 echo "Failure: Previous tor keys present in tor data directory" >&2
56 echo "Generating initial tor keys"
57 $TOR --DataDirectory "$DATA_DIR" --list-fingerprint
59 # tor must successfully generate non-zero-length key files
60 if [ -s "$DATA_DIR"/keys
/secret_id_key
] && [ -s "$DATA_DIR"/keys
/secret_onion_key
] &&
61 [ -s "$DATA_DIR"/keys
/secret_onion_key_ntor
]; then
62 true
#echo "tor generated the initial key files"
64 echo "Failure: tor failed to generate the initial key files"
69 #ls -lh "$DATA_DIR"/keys/ || exit 3
71 # backup and keep/delete/create zero-length files for the keys
73 FILE_DESC
="keeps existing"
75 cp -r "$DATA_DIR"/keys
"$DATA_DIR"/keys.old
77 # delete keys for -d or -z
78 if [ "$1" != "-e" ]; then
79 FILE_DESC
="regenerates deleted"
80 rm "$DATA_DIR"/keys
/secret_id_key ||
exit 3
81 rm "$DATA_DIR"/keys
/secret_onion_key ||
exit 3
82 rm "$DATA_DIR"/keys
/secret_onion_key_ntor ||
exit 3
85 # create empty files for -z
86 if [ "$1" = "-z" ]; then
87 FILE_DESC
="regenerates zero-length"
88 touch "$DATA_DIR"/keys
/secret_id_key ||
exit 3
89 touch "$DATA_DIR"/keys
/secret_onion_key ||
exit 3
90 touch "$DATA_DIR"/keys
/secret_onion_key_ntor ||
exit 3
93 echo "Running tor again to check if it $FILE_DESC keys"
94 $TOR --DataDirectory "$DATA_DIR" --list-fingerprint
96 #ls -lh "$DATA_DIR"/keys/ || exit 3
98 # tor must always have non-zero-length key files
99 if [ -s "$DATA_DIR"/keys
/secret_id_key
] && [ -s "$DATA_DIR"/keys
/secret_onion_key
] &&
100 [ -s "$DATA_DIR"/keys
/secret_onion_key_ntor
]; then
101 # check if the keys are different to the old ones
102 diff -q -r "$DATA_DIR"/keys
"$DATA_DIR"/keys.old
> /dev
/null
104 # if we're not testing existing keys,
105 # the current keys should be different to the old ones
106 if [ "$1" != "-e" ]; then
107 if [ $SAME_KEYS -ne 0 ]; then
108 echo "Success: test that tor $FILE_DESC key files: different keys"
111 echo "Failure: test that tor $FILE_DESC key files: same keys"
114 else #[ "$1" == "-e" ]; then
115 if [ $SAME_KEYS -eq 0 ]; then
116 echo "Success: test that tor $FILE_DESC key files: same keys"
119 echo "Failure: test that tor $FILE_DESC key files: different keys"
124 echo "Failure: test that tor $FILE_DESC key files: no key files"