test_key_expiration.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #!/bin/sh
  2. # Note: some of this code is lifted from zero_length_keys.sh and
  3. # test_keygen.sh, and could be unified.
  4. umask 077
  5. set -e
  6. if [ $# -eq 0 ] || [ ! -f "${1}" ] || [ ! -x "${1}" ]; then
  7. if [ "$TESTING_TOR_BINARY" = "" ] ; then
  8. echo "Usage: ${0} PATH_TO_TOR [case-number]"
  9. exit 1
  10. fi
  11. fi
  12. UNAME_OS=$(uname -s | cut -d_ -f1)
  13. if test "$UNAME_OS" = 'CYGWIN' || \
  14. test "$UNAME_OS" = 'MSYS' || \
  15. test "$UNAME_OS" = 'MINGW'; then
  16. echo "This test is unreliable on Windows. See trac #26076. Skipping." >&2
  17. exit 77
  18. fi
  19. if [ $# -ge 1 ]; then
  20. TOR_BINARY="${1}"
  21. shift
  22. else
  23. TOR_BINARY="${TESTING_TOR_BINARY}"
  24. fi
  25. if [ $# -ge 1 ]; then
  26. dflt=0
  27. else
  28. dflt=1
  29. fi
  30. CASE1=$dflt
  31. CASE2=$dflt
  32. CASE3=$dflt
  33. if [ $# -ge 1 ]; then
  34. eval "CASE${1}"=1
  35. fi
  36. dump() { xxd -p "$1" | tr -d '\n '; }
  37. die() { echo "$1" >&2 ; exit 5; }
  38. check_dir() { [ -d "$1" ] || die "$1 did not exist"; }
  39. check_file() { [ -e "$1" ] || die "$1 did not exist"; }
  40. check_no_file() { if [ -e "$1" ]; then die "$1 was not supposed to exist"; fi }
  41. check_files_eq() { cmp "$1" "$2" || die "$1 and $2 did not match: $(dump "$1") vs $(dump "$2")"; }
  42. check_keys_eq() { check_files_eq "${SRC}/keys/${1}" "${ME}/keys/${1}"; }
  43. DATA_DIR=$(mktemp -d -t tor_key_expiration_tests.XXXXXX)
  44. if [ -z "$DATA_DIR" ]; then
  45. echo "Failure: mktemp invocation returned empty string" >&2
  46. exit 3
  47. fi
  48. if [ ! -d "$DATA_DIR" ]; then
  49. echo "Failure: mktemp invocation result doesn't point to directory" >&2
  50. exit 3
  51. fi
  52. trap 'rm -rf "$DATA_DIR"' 0
  53. # Use an absolute path for this or Tor will complain
  54. DATA_DIR=$(cd "${DATA_DIR}" && pwd)
  55. touch "${DATA_DIR}/empty_torrc"
  56. touch "${DATA_DIR}/empty_defaults_torrc"
  57. QUIETLY="--hush"
  58. SILENTLY="--quiet"
  59. TOR="${TOR_BINARY} --DisableNetwork 1 --ShutdownWaitLength 0 --ORPort 12345 --ExitRelay 0 --DataDirectory ${DATA_DIR} -f ${DATA_DIR}/empty_torrc --defaults-torrc ${DATA_DIR}/empty_defaults_torrc"
  60. ##### SETUP
  61. #
  62. # Here we create a set of keys.
  63. # Step 1: Start Tor with --list-fingerprint --quiet. Make sure everything is there.
  64. echo "Setup step #1"
  65. ${TOR} --list-fingerprint ${SILENTLY} > /dev/null
  66. check_dir "${DATA_DIR}/keys"
  67. check_file "${DATA_DIR}/keys/ed25519_master_id_public_key"
  68. check_file "${DATA_DIR}/keys/ed25519_master_id_secret_key"
  69. check_file "${DATA_DIR}/keys/ed25519_signing_cert"
  70. check_file "${DATA_DIR}/keys/ed25519_signing_secret_key"
  71. check_file "${DATA_DIR}/keys/secret_id_key"
  72. check_file "${DATA_DIR}/keys/secret_onion_key"
  73. check_file "${DATA_DIR}/keys/secret_onion_key_ntor"
  74. ##### TEST CASES
  75. echo "=== Starting key expiration tests."
  76. FN="${DATA_DIR}/stderr"
  77. if [ "$CASE1" = 1 ]; then
  78. echo "==== Case 1: Test --key-expiration without argument and ensure usage"
  79. echo " instructions are printed."
  80. ${TOR} ${QUIETLY} --key-expiration 2>"$FN" || true
  81. grep "No valid argument to --key-expiration found!" "$FN" >/dev/null || \
  82. die "Tor didn't mention supported --key-expiration argmuents"
  83. echo "==== Case 1: ok"
  84. fi
  85. if [ "$CASE2" = 1 ]; then
  86. echo "==== Case 2: Start Tor with --key-expiration 'sign' and make sure it prints an expiration."
  87. ${TOR} ${QUIETLY} --key-expiration sign 2>"$FN"
  88. grep "signing-cert-expiry:" "$FN" >/dev/null || \
  89. die "Tor didn't print an expiration"
  90. echo "==== Case 2: ok"
  91. fi
  92. if [ "$CASE3" = 1 ]; then
  93. echo "==== Case 3: Start Tor with --key-expiration 'sign', when there is no"
  94. echo " signing key, and make sure that Tor generates a new key"
  95. echo " and prints its certificate's expiration."
  96. mv "${DATA_DIR}/keys/ed25519_signing_cert" \
  97. "${DATA_DIR}/keys/ed25519_signing_cert.bak"
  98. ${TOR} --key-expiration sign > "$FN" 2>&1
  99. grep "It looks like I need to generate and sign a new medium-term signing key" "$FN" >/dev/null || \
  100. die "Tor didn't create a new signing key"
  101. check_file "${DATA_DIR}/keys/ed25519_signing_cert"
  102. grep "signing-cert-expiry:" "$FN" >/dev/null || \
  103. die "Tor didn't print an expiration"
  104. mv "${DATA_DIR}/keys/ed25519_signing_cert.bak" \
  105. "${DATA_DIR}/keys/ed25519_signing_cert"
  106. echo "==== Case 3: ok"
  107. fi