zero_length_keys.sh 3.7 KB

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