zero_length_keys.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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
  7. # Run all the tests below
  8. # ./zero_length_keys.sh -z
  9. # Check tor will launch and regenerate zero-length keys
  10. # ./zero_length_keys.sh -d
  11. # Check tor regenerates deleted keys (existing behaviour)
  12. # ./zero_length_keys.sh -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. # 254: test failed - tor did not generate the key files on first run
  19. # 255: a command failed - the test could not be completed
  20. #
  21. if [ $# -lt 1 ]; then
  22. echo "Testing that tor correctly handles zero-length keys"
  23. "$0" -z && "$0" -d && "$0" -e
  24. exit $?
  25. fi
  26. export DATA_DIR=`mktemp -d -t tor_zero_length_keys.XXXXXX`
  27. # DisableNetwork means that the ORPort won't actually be opened.
  28. # 'ExitRelay 0' suppresses a warning.
  29. TOR="./src/or/tor --hush --DisableNetwork 1 --ShutdownWaitLength 0 --ORPort 12345 --ExitRelay 0"
  30. if [ -s "$DATA_DIR"/keys/secret_id_key -a -s "$DATA_DIR"/keys/secret_onion_key -a -s "$DATA_DIR"/keys/secret_onion_key_ntor ]; then
  31. echo "Failure: Previous tor keys present in tor data directory"
  32. exit 255
  33. else
  34. echo "Generating initial tor keys"
  35. $TOR --DataDirectory "$DATA_DIR" --PidFile "$DATA_DIR"/pid &
  36. TOR_PID=$!
  37. # generate SIGTERM, hopefully after the keys have been regenerated
  38. sleep 5
  39. kill $TOR_PID
  40. wait $TOR_PID
  41. # tor must successfully generate non-zero-length key files
  42. if [ -s "$DATA_DIR"/keys/secret_id_key -a -s "$DATA_DIR"/keys/secret_onion_key -a -s "$DATA_DIR"/keys/secret_onion_key_ntor ]; then
  43. true #echo "tor generated the initial key files"
  44. else
  45. echo "Failure: tor failed to generate the initial key files"
  46. exit 254
  47. fi
  48. fi
  49. #ls -lh "$DATA_DIR"/keys/ || exit 255
  50. # backup and keep/delete/create zero-length files for the keys
  51. FILE_DESC="keeps existing"
  52. # make a backup
  53. cp -r "$DATA_DIR"/keys "$DATA_DIR"/keys.old
  54. # delete keys for -d or -z
  55. if [ "$1" != "-e" ]; then
  56. FILE_DESC="regenerates deleted"
  57. rm "$DATA_DIR"/keys/secret_id_key || exit 255
  58. rm "$DATA_DIR"/keys/secret_onion_key || exit 255
  59. rm "$DATA_DIR"/keys/secret_onion_key_ntor || exit 255
  60. fi
  61. # create empty files for -z
  62. if [ "$1" = "-z" ]; then
  63. FILE_DESC="regenerates zero-length"
  64. touch "$DATA_DIR"/keys/secret_id_key || exit 255
  65. touch "$DATA_DIR"/keys/secret_onion_key || exit 255
  66. touch "$DATA_DIR"/keys/secret_onion_key_ntor || exit 255
  67. fi
  68. echo "Running tor again to check if it $FILE_DESC keys"
  69. $TOR --DataDirectory "$DATA_DIR" --PidFile "$DATA_DIR"/pid &
  70. TOR_PID=$!
  71. # generate SIGTERM, hopefully after the keys have been regenerated
  72. sleep 5
  73. kill $TOR_PID
  74. wait $TOR_PID
  75. #ls -lh "$DATA_DIR"/keys/ || exit 255
  76. # tor must always have non-zero-length key files
  77. if [ -s "$DATA_DIR"/keys/secret_id_key -a -s "$DATA_DIR"/keys/secret_onion_key -a -s "$DATA_DIR"/keys/secret_onion_key_ntor ]; then
  78. # check if the keys are different to the old ones
  79. diff -q -r "$DATA_DIR"/keys "$DATA_DIR"/keys.old > /dev/null
  80. SAME_KEYS=$?
  81. # if we're not testing existing keys,
  82. # the current keys should be different to the old ones
  83. if [ "$1" != "-e" ]; then
  84. if [ $SAME_KEYS -ne 0 ]; then
  85. echo "Success: test that tor $FILE_DESC key files: different keys"
  86. exit 0
  87. else
  88. echo "Failure: test that tor $FILE_DESC key files: same keys"
  89. exit 1
  90. fi
  91. else #[ "$1" == "-e" ]; then
  92. if [ $SAME_KEYS -eq 0 ]; then
  93. echo "Success: test that tor $FILE_DESC key files: same keys"
  94. exit 0
  95. else
  96. echo "Failure: test that tor $FILE_DESC key files: different keys"
  97. exit 1
  98. fi
  99. fi
  100. else
  101. echo "Failure: test that tor $FILE_DESC key files: no key files"
  102. exit 1
  103. fi