test_keygen.sh 12 KB

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