zero_length_keys.sh 4.0 KB

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