zero_length_keys.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. DATA_DIR=`mktemp -d -t tor_zero_length_keys.XXXXXX`
  27. if [ -z "$DATA_DIR" ]; then
  28. echo "Failure: mktemp invocation returned empty string"
  29. exit 255
  30. fi
  31. if [ -d "$DATA_DIR" ]; then
  32. echo "Failure: mktemp invocation result doesn't point to directory"
  33. exit 255
  34. fi
  35. trap "rm -rf '$DATA_DIR'" 0
  36. # DisableNetwork means that the ORPort won't actually be opened.
  37. # 'ExitRelay 0' suppresses a warning.
  38. TOR="./src/or/tor --hush --DisableNetwork 1 --ShutdownWaitLength 0 --ORPort 12345 --ExitRelay 0"
  39. 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
  40. echo "Failure: Previous tor keys present in tor data directory"
  41. exit 255
  42. else
  43. echo "Generating initial tor keys"
  44. $TOR --DataDirectory "$DATA_DIR" --PidFile "$DATA_DIR"/pid &
  45. TOR_PID=$!
  46. # generate SIGTERM, hopefully after the keys have been regenerated
  47. sleep 5
  48. kill $TOR_PID
  49. wait $TOR_PID
  50. # tor must successfully generate non-zero-length key files
  51. 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
  52. true #echo "tor generated the initial key files"
  53. else
  54. echo "Failure: tor failed to generate the initial key files"
  55. exit 254
  56. fi
  57. fi
  58. #ls -lh "$DATA_DIR"/keys/ || exit 255
  59. # backup and keep/delete/create zero-length files for the keys
  60. FILE_DESC="keeps existing"
  61. # make a backup
  62. cp -r "$DATA_DIR"/keys "$DATA_DIR"/keys.old
  63. # delete keys for -d or -z
  64. if [ "$1" != "-e" ]; then
  65. FILE_DESC="regenerates deleted"
  66. rm "$DATA_DIR"/keys/secret_id_key || exit 255
  67. rm "$DATA_DIR"/keys/secret_onion_key || exit 255
  68. rm "$DATA_DIR"/keys/secret_onion_key_ntor || exit 255
  69. fi
  70. # create empty files for -z
  71. if [ "$1" = "-z" ]; then
  72. FILE_DESC="regenerates zero-length"
  73. touch "$DATA_DIR"/keys/secret_id_key || exit 255
  74. touch "$DATA_DIR"/keys/secret_onion_key || exit 255
  75. touch "$DATA_DIR"/keys/secret_onion_key_ntor || exit 255
  76. fi
  77. echo "Running tor again to check if it $FILE_DESC keys"
  78. $TOR --DataDirectory "$DATA_DIR" --PidFile "$DATA_DIR"/pid &
  79. TOR_PID=$!
  80. # generate SIGTERM, hopefully after the keys have been regenerated
  81. sleep 5
  82. kill $TOR_PID
  83. wait $TOR_PID
  84. #ls -lh "$DATA_DIR"/keys/ || exit 255
  85. # tor must always have non-zero-length key files
  86. 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
  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