test_keygen.sh 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. CASE11A=$dflt
  43. CASE11B=$dflt
  44. CASE11C=$dflt
  45. CASE11D=$dflt
  46. CASE11E=$dflt
  47. CASE11F=$dflt
  48. if [ $# -ge 1 ]; then
  49. eval "CASE${1}"=1
  50. fi
  51. dump() { xxd -p "$1" | tr -d '\n '; }
  52. die() { echo "$1" >&2 ; exit 5; }
  53. check_dir() { [ -d "$1" ] || die "$1 did not exist"; }
  54. check_file() { [ -e "$1" ] || die "$1 did not exist"; }
  55. check_no_file() { [ -e "$1" ] && die "$1 was not supposed to exist" || true; }
  56. check_files_eq() { cmp "$1" "$2" || die "$1 and $2 did not match: `dump $1` vs `dump $2`"; }
  57. check_keys_eq() { check_files_eq "${SRC}/keys/${1}" "${ME}/keys/${1}"; }
  58. DATA_DIR=`mktemp -d -t tor_keygen_tests.XXXXXX`
  59. if [ -z "$DATA_DIR" ]; then
  60. echo "Failure: mktemp invocation returned empty string" >&2
  61. exit 3
  62. fi
  63. if [ ! -d "$DATA_DIR" ]; then
  64. echo "Failure: mktemp invocation result doesn't point to directory" >&2
  65. exit 3
  66. fi
  67. trap "rm -rf '$DATA_DIR'" 0
  68. # Use an absolute path for this or Tor will complain
  69. DATA_DIR=`cd "${DATA_DIR}" && pwd`
  70. touch "${DATA_DIR}/empty_torrc"
  71. QUIETLY="--hush"
  72. SILENTLY="--quiet"
  73. TOR="${TOR_BINARY} ${QUIETLY} --DisableNetwork 1 --ShutdownWaitLength 0 --ORPort 12345 --ExitRelay 0 -f ${DATA_DIR}/empty_torrc"
  74. ##### SETUP
  75. #
  76. # Here we create three sets of keys: one using "tor", one using "tor
  77. # --keygen", and one using "tor --keygen" and encryption. We'll be
  78. # copying them into different keys directories in order to simulate
  79. # different kinds of configuration problems/issues.
  80. # Step 1: Start Tor with --list-fingerprint --quiet. Make sure everything is there.
  81. mkdir "${DATA_DIR}/orig"
  82. ${TOR} --DataDirectory "${DATA_DIR}/orig" --list-fingerprint ${SILENTLY} > /dev/null
  83. check_dir "${DATA_DIR}/orig/keys"
  84. check_file "${DATA_DIR}/orig/keys/ed25519_master_id_public_key"
  85. check_file "${DATA_DIR}/orig/keys/ed25519_master_id_secret_key"
  86. check_file "${DATA_DIR}/orig/keys/ed25519_signing_cert"
  87. check_file "${DATA_DIR}/orig/keys/ed25519_signing_secret_key"
  88. # Step 2: Start Tor with --keygen. Make sure everything is there.
  89. mkdir "${DATA_DIR}/keygen"
  90. ${TOR} --DataDirectory "${DATA_DIR}/keygen" --keygen --no-passphrase 2>"${DATA_DIR}/keygen/stderr"
  91. grep "Not encrypting the secret key" "${DATA_DIR}/keygen/stderr" >/dev/null || die "Tor didn't declare that there would be no encryption"
  92. check_dir "${DATA_DIR}/keygen/keys"
  93. check_file "${DATA_DIR}/keygen/keys/ed25519_master_id_public_key"
  94. check_file "${DATA_DIR}/keygen/keys/ed25519_master_id_secret_key"
  95. check_file "${DATA_DIR}/keygen/keys/ed25519_signing_cert"
  96. check_file "${DATA_DIR}/keygen/keys/ed25519_signing_secret_key"
  97. # Step 3: Start Tor with --keygen and a passphrase.
  98. # Make sure everything is there.
  99. mkdir "${DATA_DIR}/encrypted"
  100. echo "passphrase" | ${TOR} --DataDirectory "${DATA_DIR}/encrypted" --keygen --passphrase-fd 0
  101. check_dir "${DATA_DIR}/encrypted/keys"
  102. check_file "${DATA_DIR}/encrypted/keys/ed25519_master_id_public_key"
  103. check_file "${DATA_DIR}/encrypted/keys/ed25519_master_id_secret_key_encrypted"
  104. check_file "${DATA_DIR}/encrypted/keys/ed25519_signing_cert"
  105. check_file "${DATA_DIR}/encrypted/keys/ed25519_signing_secret_key"
  106. echo "=== Starting keygen tests."
  107. #
  108. # The "case X" numbers below come from s7r's email on
  109. # https://lists.torproject.org/pipermail/tor-dev/2015-August/009204.html
  110. # Case 2a: Missing secret key, public key exists, start tor.
  111. if [ "$CASE2A" = 1 ]; then
  112. ME="${DATA_DIR}/case2a"
  113. SRC="${DATA_DIR}/orig"
  114. mkdir -p "${ME}/keys"
  115. cp "${SRC}/keys/ed25519_master_id_public_key" "${ME}/keys/"
  116. ${TOR} --DataDirectory "${ME}" --list-fingerprint > "${ME}/stdout" && die "Somehow succeeded when missing secret key, certs: `cat ${ME}/stdout`" || true
  117. check_files_eq "${SRC}/keys/ed25519_master_id_public_key" "${ME}/keys/ed25519_master_id_public_key"
  118. 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"
  119. echo "==== Case 2A ok"
  120. fi
  121. # Case 2b: Encrypted secret key, public key exists, start tor.
  122. if [ "$CASE2B" = 1 ]; then
  123. ME="${DATA_DIR}/case2b"
  124. SRC="${DATA_DIR}/encrypted"
  125. mkdir -p "${ME}/keys"
  126. cp "${SRC}/keys/ed25519_master_id_public_key" "${ME}/keys/"
  127. cp "${SRC}/keys/ed25519_master_id_secret_key_encrypted" "${ME}/keys/"
  128. ${TOR} --DataDirectory "${ME}" --list-fingerprint > "${ME}/stdout" && dir "Somehow succeeded with encrypted secret key, missing certs"
  129. check_files_eq "${SRC}/keys/ed25519_master_id_public_key" "${ME}/keys/ed25519_master_id_public_key"
  130. check_files_eq "${SRC}/keys/ed25519_master_id_secret_key_encrypted" "${ME}/keys/ed25519_master_id_secret_key_encrypted"
  131. 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."
  132. echo "==== Case 2B ok"
  133. fi
  134. # Case 3a: Start Tor with only master key.
  135. if [ "$CASE3A" = 1 ]; then
  136. ME="${DATA_DIR}/case3a"
  137. SRC="${DATA_DIR}/orig"
  138. mkdir -p "${ME}/keys"
  139. cp "${SRC}/keys/ed25519_master_id_"* "${ME}/keys/"
  140. ${TOR} --DataDirectory "${ME}" --list-fingerprint ${SILENTLY} >/dev/null || die "Tor failed when starting 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 3A ok"
  146. fi
  147. # Case 3b: Call keygen with only unencrypted master key.
  148. if [ "$CASE3B" = 1 ]; then
  149. ME="${DATA_DIR}/case3b"
  150. SRC="${DATA_DIR}/orig"
  151. mkdir -p "${ME}/keys"
  152. cp "${SRC}/keys/ed25519_master_id_"* "${ME}/keys/"
  153. ${TOR} --DataDirectory "${ME}" --keygen || die "Keygen failed with only 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" "${ME}/keys/ed25519_master_id_secret_key"
  156. check_file "${ME}/keys/ed25519_signing_cert"
  157. check_file "${ME}/keys/ed25519_signing_secret_key"
  158. echo "==== Case 3B ok"
  159. fi
  160. # Case 3c: Call keygen with only encrypted master key.
  161. if [ "$CASE3C" = 1 ]; then
  162. ME="${DATA_DIR}/case3c"
  163. SRC="${DATA_DIR}/encrypted"
  164. mkdir -p "${ME}/keys"
  165. cp "${SRC}/keys/ed25519_master_id_"* "${ME}/keys/"
  166. echo "passphrase" | ${TOR} --DataDirectory "${ME}" --keygen --passphrase-fd 0 || die "Keygen failed with only encrypted master key"
  167. check_files_eq "${SRC}/keys/ed25519_master_id_public_key" "${ME}/keys/ed25519_master_id_public_key"
  168. check_files_eq "${SRC}/keys/ed25519_master_id_secret_key_encrypted" "${ME}/keys/ed25519_master_id_secret_key_encrypted"
  169. check_file "${ME}/keys/ed25519_signing_cert"
  170. check_file "${ME}/keys/ed25519_signing_secret_key"
  171. echo "==== Case 3C ok"
  172. fi
  173. # Case 4: Make a new data directory with only an unencrypted secret key.
  174. # Then start tor. The rest should become correct.
  175. if [ "$CASE4" = 1 ]; then
  176. ME="${DATA_DIR}/case4"
  177. SRC="${DATA_DIR}/orig"
  178. mkdir -p "${ME}/keys"
  179. cp "${SRC}/keys/ed25519_master_id_secret_key" "${ME}/keys/"
  180. ${TOR} --DataDirectory "${ME}" --list-fingerprint ${SILENTLY} > "${ME}/fp1" || die "Tor wouldn't start with only unencrypted secret key"
  181. check_file "${ME}/keys/ed25519_master_id_public_key"
  182. check_file "${ME}/keys/ed25519_signing_cert"
  183. check_file "${ME}/keys/ed25519_signing_secret_key"
  184. ${TOR} --DataDirectory "${ME}" --list-fingerprint ${SILENTLY} > "${ME}/fp2" || die "Tor wouldn't start again after starting once with only unencrypted secret key."
  185. check_files_eq "${ME}/fp1" "${ME}/fp2"
  186. echo "==== Case 4 ok"
  187. fi
  188. # Case 5: Make a new data directory with only an encrypted secret key.
  189. if [ "$CASE5" = 1 ]; then
  190. ME="${DATA_DIR}/case5"
  191. SRC="${DATA_DIR}/encrypted"
  192. mkdir -p "${ME}/keys"
  193. cp "${SRC}/keys/ed25519_master_id_secret_key_encrypted" "${ME}/keys/"
  194. ${TOR} --DataDirectory "${ME}" --list-fingerprint >"${ME}/stdout" && die "Tor started with only encrypted secret key!"
  195. check_no_file "${ME}/keys/ed25519_master_id_public_key"
  196. check_no_file "${ME}/keys/ed25519_master_id_public_key"
  197. grep "but not public key file" "${ME}/stdout" >/dev/null || die "Tor didn't declare it couldn't find a public key."
  198. echo "==== Case 5 ok"
  199. fi
  200. # Case 6: Make a new data directory with encrypted secret key and public key
  201. if [ "$CASE6" = 1 ]; then
  202. ME="${DATA_DIR}/case6"
  203. SRC="${DATA_DIR}/encrypted"
  204. mkdir -p "${ME}/keys"
  205. cp "${SRC}/keys/ed25519_master_id_secret_key_encrypted" "${ME}/keys/"
  206. cp "${SRC}/keys/ed25519_master_id_public_key" "${ME}/keys/"
  207. ${TOR} --DataDirectory "${ME}" --list-fingerprint > "${ME}/stdout" && die "Tor started with encrypted secret key and no certs" || true
  208. check_no_file "${ME}/keys/ed25519_signing_cert"
  209. check_no_file "${ME}/keys/ed25519_signing_secret_key"
  210. grep "but it was encrypted" "${ME}/stdout" >/dev/null || die "Tor didn't declare that the secret key was encrypted."
  211. echo "==== Case 6 ok"
  212. fi
  213. # Case 7: Make a new data directory with unencrypted secret key and
  214. # certificates; missing master public.
  215. if [ "$CASE7" = 1 ]; then
  216. ME="${DATA_DIR}/case7"
  217. SRC="${DATA_DIR}/keygen"
  218. mkdir -p "${ME}/keys"
  219. cp "${SRC}/keys/ed25519_master_id_secret_key" "${ME}/keys/"
  220. cp "${SRC}/keys/ed25519_signing_cert" "${ME}/keys/"
  221. cp "${SRC}/keys/ed25519_signing_secret_key" "${ME}/keys/"
  222. ${TOR} --DataDirectory "${ME}" --list-fingerprint ${SILENTLY} >/dev/null || die "Failed when starting with missing public key"
  223. check_keys_eq ed25519_master_id_secret_key
  224. check_keys_eq ed25519_master_id_public_key
  225. check_keys_eq ed25519_signing_secret_key
  226. check_keys_eq ed25519_signing_cert
  227. echo "==== Case 7 ok"
  228. fi
  229. # Case 8: offline master secret key.
  230. if [ "$CASE8" = 1 ]; then
  231. ME="${DATA_DIR}/case8"
  232. SRC="${DATA_DIR}/keygen"
  233. mkdir -p "${ME}/keys"
  234. cp "${SRC}/keys/ed25519_master_id_public_key" "${ME}/keys/"
  235. cp "${SRC}/keys/ed25519_signing_cert" "${ME}/keys/"
  236. cp "${SRC}/keys/ed25519_signing_secret_key" "${ME}/keys/"
  237. ${TOR} --DataDirectory "${ME}" --list-fingerprint ${SILENTLY} >/dev/null || die "Failed when starting with offline secret key"
  238. check_no_file "${ME}/keys/ed25519_master_id_secret_key"
  239. check_keys_eq ed25519_master_id_public_key
  240. check_keys_eq ed25519_signing_secret_key
  241. check_keys_eq ed25519_signing_cert
  242. echo "==== Case 8 ok"
  243. fi
  244. # Case 9: signing cert and secret key provided; could infer master key.
  245. if [ "$CASE9" = 1 ]; then
  246. ME="${DATA_DIR}/case9"
  247. SRC="${DATA_DIR}/keygen"
  248. mkdir -p "${ME}/keys"
  249. cp "${SRC}/keys/ed25519_signing_cert" "${ME}/keys/"
  250. cp "${SRC}/keys/ed25519_signing_secret_key" "${ME}/keys/"
  251. ${TOR} --DataDirectory "${ME}" --list-fingerprint ${SILENTLY} >/dev/null || die "Failed when starting with only signing material"
  252. check_no_file "${ME}/keys/ed25519_master_id_secret_key"
  253. check_file "${ME}/keys/ed25519_master_id_public_key"
  254. check_keys_eq ed25519_signing_secret_key
  255. check_keys_eq ed25519_signing_cert
  256. echo "==== Case 9 ok"
  257. fi
  258. # Case 10: master key mismatch.
  259. if [ "$CASE10" = 1 ]; then
  260. ME="${DATA_DIR}/case10"
  261. SRC="${DATA_DIR}/keygen"
  262. OTHER="${DATA_DIR}/orig"
  263. mkdir -p "${ME}/keys"
  264. cp "${SRC}/keys/ed25519_master_id_public_key" "${ME}/keys/"
  265. cp "${OTHER}/keys/ed25519_master_id_secret_key" "${ME}/keys/"
  266. ${TOR} --DataDirectory "${ME}" --list-fingerprint >"${ME}/stdout" && die "Successfully started with mismatched keys!?" || true
  267. grep "public_key does not match.*secret_key" "${ME}/stdout" >/dev/null || die "Tor didn't declare that there was a key mismatch"
  268. echo "==== Case 10 ok"
  269. fi
  270. # Case 11a: -passphrase-fd without --keygen
  271. if [ "$CASE11A" = 1 ]; then
  272. ME="${DATA_DIR}/case11a"
  273. mkdir -p "${ME}/keys"
  274. ${TOR} --DataDirectory "${ME}" --passphrase-fd 1 > "${ME}/stdout" && die "Successfully started with passphrase-fd but no keygen?" || true
  275. grep "passphrase-fd specified without --keygen" "${ME}/stdout" >/dev/null || die "Tor didn't declare that there was a problem with the arguments."
  276. echo "==== Case 11A ok"
  277. fi
  278. # Case 11b: --no-passphrase without --keygen
  279. if [ "$CASE11B" = 1 ]; then
  280. ME="${DATA_DIR}/case11b"
  281. mkdir -p "${ME}/keys"
  282. ${TOR} --DataDirectory "${ME}" --no-passphrase > "${ME}/stdout" && die "Successfully started with no-passphrase but no keygen?" || true
  283. grep "no-passphrase specified without --keygen" "${ME}/stdout" >/dev/null || die "Tor didn't declare that there was a problem with the arguments."
  284. echo "==== Case 11B ok"
  285. fi
  286. # Case 11c: --newpass without --keygen
  287. if [ "$CASE11C" = 1 ]; then
  288. ME="${DATA_DIR}/case11C"
  289. mkdir -p "${ME}/keys"
  290. ${TOR} --DataDirectory "${ME}" --newpass > "${ME}/stdout" && die "Successfully started with newpass but no keygen?" || true
  291. grep "newpass specified without --keygen" "${ME}/stdout" >/dev/null || die "Tor didn't declare that there was a problem with the arguments."
  292. echo "==== Case 11C ok"
  293. fi
  294. ######## --master-key does not work yet, but this will test the error case
  295. ######## when it does.
  296. #
  297. # Case 11d: --master-key without --keygen
  298. #
  299. if [ "$CASE11D" = 1 ]; then
  300. #
  301. # ME="${DATA_DIR}/case11d"
  302. #
  303. # mkdir -p "${ME}/keys"
  304. #
  305. # ${TOR} --DataDirectory "${ME}" --master-key "${ME}/foobar" > "${ME}/stdout" && die "Successfully started with master-key but no keygen?" || true
  306. #
  307. # cat "${ME}/stdout"
  308. #
  309. # grep "master-key without --keygen" "${ME}/stdout" >/dev/null || die "Tor didn't declare that there was a problem with the arguments."
  310. echo "==== Case 11D skipped"
  311. fi
  312. # Case 11E: Silly passphrase-fd
  313. if [ "$CASE11E" = 1 ]; then
  314. ME="${DATA_DIR}/case11E"
  315. mkdir -p "${ME}/keys"
  316. ${TOR} --DataDirectory "${ME}" --keygen --passphrase-fd ewigeblumenkraft > "${ME}/stdout" && die "Successfully started with bogus passphrase-fd?" || true
  317. grep "Invalid --passphrase-fd value" "${ME}/stdout" >/dev/null || die "Tor didn't declare that there was a problem with the arguments."
  318. echo "==== Case 11E ok"
  319. fi
  320. # Case 11F: --no-passphrase with --passphrase-fd
  321. if [ "$CASE11F" = 1 ]; then
  322. ME="${DATA_DIR}/case11F"
  323. mkdir -p "${ME}/keys"
  324. ${TOR} --DataDirectory "${ME}" --keygen --passphrase-fd 1 --no-passphrase > "${ME}/stdout" && die "Successfully started with bogus passphrase-fd combination?" || true
  325. grep "no-passphrase specified with --passphrase-fd" "${ME}/stdout" >/dev/null || die "Tor didn't declare that there was a problem with the arguments."
  326. echo "==== Case 11F ok"
  327. fi
  328. # Check cert-only.