zero_length_keys.sh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #!/bin/sh
  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
  4. #
  5. # Usage:
  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)
  14. #
  15. # Exit Statuses:
  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
  20. #
  21. if [ $# -eq 0 ] || [ ! -f ${1} ] || [ ! -x ${1} ]; then
  22. echo "Usage: ${0} PATH_TO_TOR [-z|-d|-e]"
  23. exit 1
  24. elif [ $# -eq 1 ]; then
  25. echo "Testing that tor correctly handles zero-length keys"
  26. "$0" "${1}" -z && "$0" "${1}" -d && "$0" "${1}" -e
  27. exit $?
  28. else #[$# -gt 1 ]; then
  29. TOR_BINARY="${1}"
  30. shift
  31. fi
  32. DATA_DIR=`mktemp -d -t tor_zero_length_keys.XXXXXX`
  33. if [ -z "$DATA_DIR" ]; then
  34. echo "Failure: mktemp invocation returned empty string" >&2
  35. exit 3
  36. fi
  37. if [ ! -d "$DATA_DIR" ]; then
  38. echo "Failure: mktemp invocation result doesn't point to directory" >&2
  39. exit 3
  40. fi
  41. trap "rm -rf '$DATA_DIR'" 0
  42. touch "$DATA_DIR"/empty_torrc
  43. # DisableNetwork means that the ORPort won't actually be opened.
  44. # 'ExitRelay 0' suppresses a warning.
  45. TOR="${TOR_BINARY} --hush --DisableNetwork 1 --ShutdownWaitLength 0 --ORPort 12345 --ExitRelay 0 -f $DATA_DIR/empty_torrc"
  46. if [ -s "$DATA_DIR"/keys/secret_id_key ] && [ -s "$DATA_DIR"/keys/secret_onion_key ] &&
  47. [ -s "$DATA_DIR"/keys/secret_onion_key_ntor ]; then
  48. echo "Failure: Previous tor keys present in tor data directory" >&2
  49. exit 3
  50. else
  51. echo "Generating initial tor keys"
  52. $TOR --DataDirectory "$DATA_DIR" --list-fingerprint
  53. # tor must successfully generate non-zero-length key files
  54. if [ -s "$DATA_DIR"/keys/secret_id_key ] && [ -s "$DATA_DIR"/keys/secret_onion_key ] &&
  55. [ -s "$DATA_DIR"/keys/secret_onion_key_ntor ]; then
  56. true #echo "tor generated the initial key files"
  57. else
  58. echo "Failure: tor failed to generate the initial key files"
  59. exit 2
  60. fi
  61. fi
  62. #ls -lh "$DATA_DIR"/keys/ || exit 3
  63. # backup and keep/delete/create zero-length files for the keys
  64. FILE_DESC="keeps existing"
  65. # make a backup
  66. cp -r "$DATA_DIR"/keys "$DATA_DIR"/keys.old
  67. # delete keys for -d or -z
  68. if [ "$1" != "-e" ]; then
  69. FILE_DESC="regenerates deleted"
  70. rm "$DATA_DIR"/keys/secret_id_key || exit 3
  71. rm "$DATA_DIR"/keys/secret_onion_key || exit 3
  72. rm "$DATA_DIR"/keys/secret_onion_key_ntor || exit 3
  73. fi
  74. # create empty files for -z
  75. if [ "$1" = "-z" ]; then
  76. FILE_DESC="regenerates zero-length"
  77. touch "$DATA_DIR"/keys/secret_id_key || exit 3
  78. touch "$DATA_DIR"/keys/secret_onion_key || exit 3
  79. touch "$DATA_DIR"/keys/secret_onion_key_ntor || exit 3
  80. fi
  81. echo "Running tor again to check if it $FILE_DESC keys"
  82. $TOR --DataDirectory "$DATA_DIR" --list-fingerprint
  83. #ls -lh "$DATA_DIR"/keys/ || exit 3
  84. # tor must always have non-zero-length key files
  85. if [ -s "$DATA_DIR"/keys/secret_id_key ] && [ -s "$DATA_DIR"/keys/secret_onion_key ] &&
  86. [ -s "$DATA_DIR"/keys/secret_onion_key_ntor ]; then
  87. # check if the keys are different to the old ones
  88. diff -q -r "$DATA_DIR"/keys "$DATA_DIR"/keys.old > /dev/null
  89. SAME_KEYS=$?
  90. # if we're not testing existing keys,
  91. # the current keys should be different to the old ones
  92. if [ "$1" != "-e" ]; then
  93. if [ $SAME_KEYS -ne 0 ]; then
  94. echo "Success: test that tor $FILE_DESC key files: different keys"
  95. exit 0
  96. else
  97. echo "Failure: test that tor $FILE_DESC key files: same keys"
  98. exit 1
  99. fi
  100. else #[ "$1" == "-e" ]; then
  101. if [ $SAME_KEYS -eq 0 ]; then
  102. echo "Success: test that tor $FILE_DESC key files: same keys"
  103. exit 0
  104. else
  105. echo "Failure: test that tor $FILE_DESC key files: different keys"
  106. exit 1
  107. fi
  108. fi
  109. else
  110. echo "Failure: test that tor $FILE_DESC key files: no key files"
  111. exit 1
  112. fi