zero_length_keys.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. touch "$DATA_DIR"/empty_defaults_torrc
  44. # DisableNetwork means that the ORPort won't actually be opened.
  45. # 'ExitRelay 0' suppresses a warning.
  46. TOR="${TOR_BINARY} --hush --DisableNetwork 1 --ShutdownWaitLength 0 --ORPort 12345 --ExitRelay 0 -f $DATA_DIR/empty_torrc --defaults-torrc $DATA_DIR/empty_defaults_torrc"
  47. if [ -s "$DATA_DIR"/keys/secret_id_key ] && [ -s "$DATA_DIR"/keys/secret_onion_key ] &&
  48. [ -s "$DATA_DIR"/keys/secret_onion_key_ntor ]; then
  49. echo "Failure: Previous tor keys present in tor data directory" >&2
  50. exit 3
  51. else
  52. echo "Generating initial tor keys"
  53. $TOR --DataDirectory "$DATA_DIR" --list-fingerprint
  54. # tor must successfully generate non-zero-length key files
  55. if [ -s "$DATA_DIR"/keys/secret_id_key ] && [ -s "$DATA_DIR"/keys/secret_onion_key ] &&
  56. [ -s "$DATA_DIR"/keys/secret_onion_key_ntor ]; then
  57. true #echo "tor generated the initial key files"
  58. else
  59. echo "Failure: tor failed to generate the initial key files"
  60. exit 2
  61. fi
  62. fi
  63. #ls -lh "$DATA_DIR"/keys/ || exit 3
  64. # backup and keep/delete/create zero-length files for the keys
  65. FILE_DESC="keeps existing"
  66. # make a backup
  67. cp -r "$DATA_DIR"/keys "$DATA_DIR"/keys.old
  68. # delete keys for -d or -z
  69. if [ "$1" != "-e" ]; then
  70. FILE_DESC="regenerates deleted"
  71. rm "$DATA_DIR"/keys/secret_id_key || exit 3
  72. rm "$DATA_DIR"/keys/secret_onion_key || exit 3
  73. rm "$DATA_DIR"/keys/secret_onion_key_ntor || exit 3
  74. fi
  75. # create empty files for -z
  76. if [ "$1" = "-z" ]; then
  77. FILE_DESC="regenerates zero-length"
  78. touch "$DATA_DIR"/keys/secret_id_key || exit 3
  79. touch "$DATA_DIR"/keys/secret_onion_key || exit 3
  80. touch "$DATA_DIR"/keys/secret_onion_key_ntor || exit 3
  81. fi
  82. echo "Running tor again to check if it $FILE_DESC keys"
  83. $TOR --DataDirectory "$DATA_DIR" --list-fingerprint
  84. #ls -lh "$DATA_DIR"/keys/ || exit 3
  85. # tor must always have non-zero-length key files
  86. if [ -s "$DATA_DIR"/keys/secret_id_key ] && [ -s "$DATA_DIR"/keys/secret_onion_key ] &&
  87. [ -s "$DATA_DIR"/keys/secret_onion_key_ntor ]; then
  88. # check if the keys are different to the old ones
  89. diff -q -r "$DATA_DIR"/keys "$DATA_DIR"/keys.old > /dev/null
  90. SAME_KEYS=$?
  91. # if we're not testing existing keys,
  92. # the current keys should be different to the old ones
  93. if [ "$1" != "-e" ]; then
  94. if [ $SAME_KEYS -ne 0 ]; then
  95. echo "Success: test that tor $FILE_DESC key files: different keys"
  96. exit 0
  97. else
  98. echo "Failure: test that tor $FILE_DESC key files: same keys"
  99. exit 1
  100. fi
  101. else #[ "$1" == "-e" ]; then
  102. if [ $SAME_KEYS -eq 0 ]; then
  103. echo "Success: test that tor $FILE_DESC key files: same keys"
  104. exit 0
  105. else
  106. echo "Failure: test that tor $FILE_DESC key files: different keys"
  107. exit 1
  108. fi
  109. fi
  110. else
  111. echo "Failure: test that tor $FILE_DESC key files: no key files"
  112. exit 1
  113. fi