test_keygen.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. #!/bin/sh
  2. # Note: some of this code is lifted from zero_length_keys.sh, and could be
  3. # 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. CASE2A=$dflt
  31. CASE2B=$dflt
  32. CASE3A=$dflt
  33. CASE3B=$dflt
  34. CASE3C=$dflt
  35. CASE4=$dflt
  36. CASE5=$dflt
  37. CASE6=$dflt
  38. CASE7=$dflt
  39. CASE8=$dflt
  40. CASE9=$dflt
  41. CASE10=$dflt
  42. if [ $# -ge 1 ]; then
  43. eval "CASE${1}"=1
  44. fi
  45. dump() { xxd -p "$1" | tr -d '\n '; }
  46. die() { echo "$1" >&2 ; exit 5; }
  47. check_dir() { [ -d "$1" ] || die "$1 did not exist"; }
  48. check_file() { [ -e "$1" ] || die "$1 did not exist"; }
  49. check_no_file() { [ -e "$1" ] && die "$1 was not supposed to exist" || true; }
  50. check_files_eq() { cmp "$1" "$2" || die "$1 and $2 did not match: `dump $1` vs `dump $2`"; }
  51. check_keys_eq() { check_files_eq "${SRC}/keys/${1}" "${ME}/keys/${1}"; }
  52. DATA_DIR=`mktemp -d -t tor_keygen_tests.XXXXXX`
  53. if [ -z "$DATA_DIR" ]; then
  54. echo "Failure: mktemp invocation returned empty string" >&2
  55. exit 3
  56. fi
  57. if [ ! -d "$DATA_DIR" ]; then
  58. echo "Failure: mktemp invocation result doesn't point to directory" >&2
  59. exit 3
  60. fi
  61. trap "rm -rf '$DATA_DIR'" 0
  62. # Use an absolute path for this or Tor will complain
  63. DATA_DIR=`cd "${DATA_DIR}" && pwd`
  64. touch "${DATA_DIR}/empty_torrc"
  65. QUIETLY="--hush"
  66. SILENTLY="--quiet"
  67. TOR="${TOR_BINARY} ${QUIETLY} --DisableNetwork 1 --ShutdownWaitLength 0 --ORPort 12345 --ExitRelay 0 -f ${DATA_DIR}/empty_torrc"
  68. ##### SETUP
  69. #
  70. # Here we create three sets of keys: one using "tor", one using "tor
  71. # --keygen", and one using "tor --keygen" and encryption. We'll be
  72. # copying them into different keys directories in order to simulate
  73. # different kinds of configuration problems/issues.
  74. # Step 1: Start Tor with --list-fingerprint --quiet. Make sure everything is there.
  75. mkdir "${DATA_DIR}/orig"
  76. ${TOR} --DataDirectory "${DATA_DIR}/orig" --list-fingerprint ${SILENTLY} > /dev/null
  77. check_dir "${DATA_DIR}/orig/keys"
  78. check_file "${DATA_DIR}/orig/keys/ed25519_master_id_public_key"
  79. check_file "${DATA_DIR}/orig/keys/ed25519_master_id_secret_key"
  80. check_file "${DATA_DIR}/orig/keys/ed25519_signing_cert"
  81. check_file "${DATA_DIR}/orig/keys/ed25519_signing_secret_key"
  82. # Step 2: Start Tor with --keygen. Make sure everything is there.
  83. mkdir "${DATA_DIR}/keygen"
  84. ${TOR} --DataDirectory "${DATA_DIR}/keygen" --keygen --no-passphrase 2>"${DATA_DIR}/keygen/stderr"
  85. grep "Not encrypting the secret key" "${DATA_DIR}/keygen/stderr" >/dev/null || die "Tor didn't declare that there would be no encryption"
  86. check_dir "${DATA_DIR}/keygen/keys"
  87. check_file "${DATA_DIR}/keygen/keys/ed25519_master_id_public_key"
  88. check_file "${DATA_DIR}/keygen/keys/ed25519_master_id_secret_key"
  89. check_file "${DATA_DIR}/keygen/keys/ed25519_signing_cert"
  90. check_file "${DATA_DIR}/keygen/keys/ed25519_signing_secret_key"
  91. # Step 3: Start Tor with --keygen and a passphrase.
  92. # Make sure everything is there.
  93. mkdir "${DATA_DIR}/encrypted"
  94. echo "passphrase" | ${TOR} --DataDirectory "${DATA_DIR}/encrypted" --keygen --passphrase-fd 0
  95. check_dir "${DATA_DIR}/encrypted/keys"
  96. check_file "${DATA_DIR}/encrypted/keys/ed25519_master_id_public_key"
  97. check_file "${DATA_DIR}/encrypted/keys/ed25519_master_id_secret_key_encrypted"
  98. check_file "${DATA_DIR}/encrypted/keys/ed25519_signing_cert"
  99. check_file "${DATA_DIR}/encrypted/keys/ed25519_signing_secret_key"
  100. echo "=== Starting keygen tests."
  101. #
  102. # The "case X" numbers below come from s7r's email on
  103. # https://lists.torproject.org/pipermail/tor-dev/2015-August/009204.html
  104. # Case 2a: Missing secret key, public key exists, start tor.
  105. if [ "$CASE2A" = 1 ]; then
  106. ME="${DATA_DIR}/case2a"
  107. SRC="${DATA_DIR}/orig"
  108. mkdir -p "${ME}/keys"
  109. cp "${SRC}/keys/ed25519_master_id_public_key" "${ME}/keys/"
  110. ${TOR} --DataDirectory "${ME}" --list-fingerprint > "${ME}/stdout" && die "Somehow succeeded when missing secret key, certs: `cat ${ME}/stdout`" || true
  111. check_files_eq "${SRC}/keys/ed25519_master_id_public_key" "${ME}/keys/ed25519_master_id_public_key"
  112. grep "We needed to load a secret key.*but couldn't find it" "${ME}/stdout" >/dev/null || die "Tor didn't declare that it was missing a secret key"
  113. echo "==== Case 2A ok"
  114. fi
  115. # Case 2b: Encrypted secret key, public key exists, start tor.
  116. if [ "$CASE2B" = 1 ]; then
  117. ME="${DATA_DIR}/case2b"
  118. SRC="${DATA_DIR}/encrypted"
  119. mkdir -p "${ME}/keys"
  120. cp "${SRC}/keys/ed25519_master_id_public_key" "${ME}/keys/"
  121. cp "${SRC}/keys/ed25519_master_id_secret_key_encrypted" "${ME}/keys/"
  122. ${TOR} --DataDirectory "${ME}" --list-fingerprint > "${ME}/stdout" && dir "Somehow succeeded with encrypted secret key, missing certs"
  123. check_files_eq "${SRC}/keys/ed25519_master_id_public_key" "${ME}/keys/ed25519_master_id_public_key"
  124. check_files_eq "${SRC}/keys/ed25519_master_id_secret_key_encrypted" "${ME}/keys/ed25519_master_id_secret_key_encrypted"
  125. grep "We needed to load a secret key.*but it was encrypted.*--keygen" "${ME}/stdout" >/dev/null || die "Tor didn't declare that it was missing a secret key and suggest --keygen."
  126. echo "==== Case 2B ok"
  127. fi
  128. # Case 3a: Start Tor with only master key.
  129. if [ "$CASE3A" = 1 ]; then
  130. ME="${DATA_DIR}/case3a"
  131. SRC="${DATA_DIR}/orig"
  132. mkdir -p "${ME}/keys"
  133. cp "${SRC}/keys/ed25519_master_id_"* "${ME}/keys/"
  134. ${TOR} --DataDirectory "${ME}" --list-fingerprint ${SILENTLY} >/dev/null || die "Tor failed when starting with only master key"
  135. check_files_eq "${SRC}/keys/ed25519_master_id_public_key" "${ME}/keys/ed25519_master_id_public_key"
  136. check_files_eq "${SRC}/keys/ed25519_master_id_secret_key" "${ME}/keys/ed25519_master_id_secret_key"
  137. check_file "${ME}/keys/ed25519_signing_cert"
  138. check_file "${ME}/keys/ed25519_signing_secret_key"
  139. echo "==== Case 3A ok"
  140. fi
  141. # Case 3b: Call keygen with only unencrypted master key.
  142. if [ "$CASE3B" = 1 ]; then
  143. ME="${DATA_DIR}/case3b"
  144. SRC="${DATA_DIR}/orig"
  145. mkdir -p "${ME}/keys"
  146. cp "${SRC}/keys/ed25519_master_id_"* "${ME}/keys/"
  147. ${TOR} --DataDirectory "${ME}" --keygen || die "Keygen failed with only master key"
  148. check_files_eq "${SRC}/keys/ed25519_master_id_public_key" "${ME}/keys/ed25519_master_id_public_key"
  149. check_files_eq "${SRC}/keys/ed25519_master_id_secret_key" "${ME}/keys/ed25519_master_id_secret_key"
  150. check_file "${ME}/keys/ed25519_signing_cert"
  151. check_file "${ME}/keys/ed25519_signing_secret_key"
  152. echo "==== Case 3B ok"
  153. fi
  154. # Case 3c: Call keygen with only encrypted master key.
  155. if [ "$CASE3C" = 1 ]; then
  156. ME="${DATA_DIR}/case3c"
  157. SRC="${DATA_DIR}/encrypted"
  158. mkdir -p "${ME}/keys"
  159. cp "${SRC}/keys/ed25519_master_id_"* "${ME}/keys/"
  160. echo "passphrase" | ${TOR} --DataDirectory "${ME}" --keygen --passphrase-fd 0 || die "Keygen failed with only encrypted master key"
  161. check_files_eq "${SRC}/keys/ed25519_master_id_public_key" "${ME}/keys/ed25519_master_id_public_key"
  162. check_files_eq "${SRC}/keys/ed25519_master_id_secret_key_encrypted" "${ME}/keys/ed25519_master_id_secret_key_encrypted"
  163. check_file "${ME}/keys/ed25519_signing_cert"
  164. check_file "${ME}/keys/ed25519_signing_secret_key"
  165. echo "==== Case 3C ok"
  166. fi
  167. # Case 4: Make a new data directory with only an unencrypted secret key.
  168. # Then start tor. The rest should become correct.
  169. if [ "$CASE4" = 1 ]; then
  170. ME="${DATA_DIR}/case4"
  171. SRC="${DATA_DIR}/orig"
  172. mkdir -p "${ME}/keys"
  173. cp "${SRC}/keys/ed25519_master_id_secret_key" "${ME}/keys/"
  174. ${TOR} --DataDirectory "${ME}" --list-fingerprint ${SILENTLY} > "${ME}/fp1" || die "Tor wouldn't start with only unencrypted secret key"
  175. check_file "${ME}/keys/ed25519_master_id_public_key"
  176. check_file "${ME}/keys/ed25519_signing_cert"
  177. check_file "${ME}/keys/ed25519_signing_secret_key"
  178. ${TOR} --DataDirectory "${ME}" --list-fingerprint ${SILENTLY} > "${ME}/fp2" || die "Tor wouldn't start again after starting once with only unencrypted secret key."
  179. check_files_eq "${ME}/fp1" "${ME}/fp2"
  180. echo "==== Case 4 ok"
  181. fi
  182. # Case 5: Make a new data directory with only an encrypted secret key.
  183. if [ "$CASE5" = 1 ]; then
  184. ME="${DATA_DIR}/case5"
  185. SRC="${DATA_DIR}/encrypted"
  186. mkdir -p "${ME}/keys"
  187. cp "${SRC}/keys/ed25519_master_id_secret_key_encrypted" "${ME}/keys/"
  188. ${TOR} --DataDirectory "${ME}" --list-fingerprint >"${ME}/stdout" && die "Tor started with only encrypted secret key!"
  189. check_no_file "${ME}/keys/ed25519_master_id_public_key"
  190. check_no_file "${ME}/keys/ed25519_master_id_public_key"
  191. grep "but not public key file" "${ME}/stdout" >/dev/null || die "Tor didn't declare it couldn't find a public key."
  192. echo "==== Case 5 ok"
  193. fi
  194. # Case 6: Make a new data directory with encrypted secret key and public key
  195. if [ "$CASE6" = 1 ]; then
  196. ME="${DATA_DIR}/case6"
  197. SRC="${DATA_DIR}/encrypted"
  198. mkdir -p "${ME}/keys"
  199. cp "${SRC}/keys/ed25519_master_id_secret_key_encrypted" "${ME}/keys/"
  200. cp "${SRC}/keys/ed25519_master_id_public_key" "${ME}/keys/"
  201. ${TOR} --DataDirectory "${ME}" --list-fingerprint > "${ME}/stdout" && die "Tor started with encrypted secret key and no certs" || true
  202. check_no_file "${ME}/keys/ed25519_signing_cert"
  203. check_no_file "${ME}/keys/ed25519_signing_secret_key"
  204. grep "but it was encrypted" "${ME}/stdout" >/dev/null || die "Tor didn't declare that the secret key was encrypted."
  205. echo "==== Case 6 ok"
  206. fi
  207. # Case 7: Make a new data directory with unencrypted secret key and
  208. # certificates; missing master public.
  209. if [ "$CASE7" = 1 ]; then
  210. ME="${DATA_DIR}/case7"
  211. SRC="${DATA_DIR}/keygen"
  212. mkdir -p "${ME}/keys"
  213. cp "${SRC}/keys/ed25519_master_id_secret_key" "${ME}/keys/"
  214. cp "${SRC}/keys/ed25519_signing_cert" "${ME}/keys/"
  215. cp "${SRC}/keys/ed25519_signing_secret_key" "${ME}/keys/"
  216. ${TOR} --DataDirectory "${ME}" --list-fingerprint ${SILENTLY} >/dev/null || die "Failed when starting with missing public key"
  217. check_keys_eq ed25519_master_id_secret_key
  218. check_keys_eq ed25519_master_id_public_key
  219. check_keys_eq ed25519_signing_secret_key
  220. check_keys_eq ed25519_signing_cert
  221. echo "==== Case 7 ok"
  222. fi
  223. # Case 8: offline master secret key.
  224. if [ "$CASE8" = 1 ]; then
  225. ME="${DATA_DIR}/case8"
  226. SRC="${DATA_DIR}/keygen"
  227. mkdir -p "${ME}/keys"
  228. cp "${SRC}/keys/ed25519_master_id_public_key" "${ME}/keys/"
  229. cp "${SRC}/keys/ed25519_signing_cert" "${ME}/keys/"
  230. cp "${SRC}/keys/ed25519_signing_secret_key" "${ME}/keys/"
  231. ${TOR} --DataDirectory "${ME}" --list-fingerprint ${SILENTLY} >/dev/null || die "Failed when starting with offline secret key"
  232. check_no_file "${ME}/keys/ed25519_master_id_secret_key"
  233. check_keys_eq ed25519_master_id_public_key
  234. check_keys_eq ed25519_signing_secret_key
  235. check_keys_eq ed25519_signing_cert
  236. echo "==== Case 8 ok"
  237. fi
  238. # Case 9: signing cert and secret key provided; could infer master key.
  239. if [ "$CASE9" = 1 ]; then
  240. ME="${DATA_DIR}/case9"
  241. SRC="${DATA_DIR}/keygen"
  242. mkdir -p "${ME}/keys"
  243. cp "${SRC}/keys/ed25519_signing_cert" "${ME}/keys/"
  244. cp "${SRC}/keys/ed25519_signing_secret_key" "${ME}/keys/"
  245. ${TOR} --DataDirectory "${ME}" --list-fingerprint ${SILENTLY} >/dev/null || die "Failed when starting with only signing material"
  246. check_no_file "${ME}/keys/ed25519_master_id_secret_key"
  247. check_file "${ME}/keys/ed25519_master_id_public_key"
  248. check_keys_eq ed25519_signing_secret_key
  249. check_keys_eq ed25519_signing_cert
  250. echo "==== Case 9 ok"
  251. fi
  252. # Case 10: master key mismatch.
  253. if [ "$CASE10" = 1 ]; then
  254. ME="${DATA_DIR}/case10"
  255. SRC="${DATA_DIR}/keygen"
  256. OTHER="${DATA_DIR}/orig"
  257. mkdir -p "${ME}/keys"
  258. cp "${SRC}/keys/ed25519_master_id_public_key" "${ME}/keys/"
  259. cp "${OTHER}/keys/ed25519_master_id_secret_key" "${ME}/keys/"
  260. ${TOR} --DataDirectory "${ME}" --list-fingerprint >"${ME}/stdout" && die "Successfully started with mismatched keys!?" || true
  261. grep "public_key does not match.*secret_key" "${ME}/stdout" >/dev/null || die "Tor didn't declare that there was a key mismatch"
  262. echo "==== Case 10 ok"
  263. fi
  264. # Check cert-only.