summaryrefslogtreecommitdiff
path: root/tests/keytrans
blob: 0c465c3679690d989101107f069ca42407bab4b6 (plain)
  1. #!/usr/bin/env bash
  2. # Tests to ensure that the monkeysphere is working
  3. # Authors:
  4. # Daniel Kahn Gillmor <dkg@fifthhorseman.net>
  5. # Jameson Rollins <jrollins@fifthhorseman.net>
  6. # Micah Anderson <micah@riseup.net>
  7. #
  8. # Copyright: 2008-2009
  9. # License: GPL v3 or later
  10. # these tests should all be able to run as a non-privileged user.
  11. # all subcommands in this script should complete without failure:
  12. set -e
  13. # piped commands should return the code of the first non-zero return
  14. set -o pipefail
  15. # make sure the TESTDIR is an absolute path, not a relative one.
  16. export TESTDIR=$(cd $(dirname "$0") && pwd)
  17. source "$TESTDIR"/common
  18. perl -MCrypt::OpenSSL::RSA -e 1 2>/dev/null || { echo "You must have the perl module Crypt::OpenSSL::RSA installed to run this test.
  19. On debian-derived systems, you can set this up with:
  20. apt-get install libcrypt-openssl-rsa-perl" ; exit 1; }
  21. perl -MDigest::SHA -e 1 2>/dev/null || { echo "You must have the perl module Digest::SHA installed to run this test.
  22. On debian-derived systems, you can set this up with:
  23. apt-get install libdigest-sha1-perl" ; exit 1; }
  24. ######################################################################
  25. ### SETUP VARIABLES
  26. ## set up some variables to ensure that we're operating strictly in
  27. ## the tests, not system-wide:
  28. mkdir -p "$TESTDIR"/tmp
  29. TEMPDIR=$(mktemp -d "${TMPDIR:-$TESTDIR/tmp}/monkeyspheretest.XXXXXXX")
  30. mkdir "$TEMPDIR"/bin
  31. ln -s "$TESTDIR"/../src/share/keytrans "$TEMPDIR"/bin/openpgp2ssh
  32. ln -s "$TESTDIR"/../src/share/keytrans "$TEMPDIR"/bin/pem2openpgp
  33. ln -s "$TESTDIR"/../src/share/keytrans "$TEMPDIR"/bin/keytrans
  34. # Use the local copy of executables first, instead of system ones.
  35. # This should help us test without installing.
  36. export PATH="$TEMPDIR"/bin:"$PATH"
  37. ## setup trap
  38. trap failed_cleanup EXIT
  39. ######################################################################
  40. ### TEST KEYTRANS
  41. echo "##################################################"
  42. echo "### generating openpgp key..."
  43. export GNUPGHOME="$TEMPDIR"
  44. chmod 700 "$TEMPDIR"
  45. # create the key with the same preferences that monkeysphere uses.
  46. cat > "$TEMPDIR"/gpg.conf <<EOF
  47. default-preference-list SHA512 SHA384 SHA256 SHA224 RIPEMD160 SHA1 ZLIB BZIP2 ZIP AES256 AES192 AES CAST5 3DES
  48. cert-digest-algo SHA256
  49. list-options show-uid-validity,show-unusable-uids
  50. fixed-list-mode
  51. EOF
  52. # generate a key
  53. gpg --batch --$(get_gpg_prng_arg) --gen-key <<EOF
  54. Key-Type: RSA
  55. Key-Length: 1024
  56. Key-Usage: sign
  57. Name-Real: testtest
  58. Expire-Date: 0
  59. %commit
  60. %echo done
  61. EOF
  62. echo "##################################################"
  63. echo "### retrieving key timestamp..."
  64. timestamp=$(gpg --list-key --with-colons | \
  65. grep ^pub: | cut -d: -f6)
  66. echo "##################################################"
  67. echo "### exporting key to ssh file..."
  68. gpg --export-secret-key | openpgp2ssh > \
  69. "$TEMPDIR"/test.pem
  70. gpg --export-secret-key > "$TEMPDIR"/secret.key
  71. PEM2OPENPGP_USAGE_FLAGS=sign,certify \
  72. PEM2OPENPGP_TIMESTAMP="$timestamp" pem2openpgp testtest \
  73. < "$TEMPDIR"/test.pem > "$TEMPDIR"/converted.secret.key
  74. echo "##################################################"
  75. echo "### reconvert key, and compare to key in gpg keyring..."
  76. diff -u \
  77. <(gpg --list-packets < "$TEMPDIR"/secret.key) \
  78. <(gpg --list-packets < "$TEMPDIR"/converted.secret.key)
  79. diff -u \
  80. <(hd "$TEMPDIR"/secret.key) \
  81. <(hd "$TEMPDIR"/converted.secret.key)
  82. KEYFPR=$(gpg --fingerprint --with-colons --list-keys | grep ^fpr | cut -f10 -d:)
  83. KEYID=$(printf "%s" "$KEYFPR" | cut -b25-40)
  84. echo "conversions look good!"
  85. echo "Now working with key $KEYID at time $timestamp"
  86. gpg --check-trustdb
  87. gpg --list-keys
  88. echo "##################################################"
  89. echo "### test User ID addition..."
  90. < "$TEMPDIR"/secring.gpg \
  91. PEM2OPENPGP_TIMESTAMP="$timestamp" \
  92. PEM2OPENPGP_USAGE_FLAGS=sign,certify \
  93. keytrans adduserid "$KEYID" "monkeymonkey" | gpg --import
  94. gpg --check-trustdb
  95. gpg --list-keys
  96. cat >"$TEMPDIR"/expectedout <<EOF
  97. pub:u:1024:1:$KEYID:$timestamp:::u:::scSC:
  98. uid:u::::$timestamp::E90EC72E68C6C2A0751DADC70F54F60D27B88C3D::monkeymonkey:
  99. sig:!::1:$KEYID:$timestamp::::monkeymonkey:13x:
  100. uid:u::::$timestamp::8200BD0425CC70C7D698DF3FE412044EAAB83F94::testtest:
  101. sig:!::1:$KEYID:$timestamp::::monkeymonkey:13x:
  102. EOF
  103. diff -u "$TEMPDIR"/expectedout <(gpg --check-sigs --with-colons | grep -v ^tru)
  104. echo "##################################################"
  105. echo "### sleeping to avoid test suite breakage on fast"
  106. echo "### processors (see http://bugs.debian.org/591118)"
  107. sleep 2
  108. echo "##################################################"
  109. echo "### test User ID revocation ... "
  110. revtime=$(($timestamp + 1))
  111. < "$TEMPDIR"/secring.gpg \
  112. PEM2OPENPGP_TIMESTAMP="$revtime" \
  113. keytrans revokeuserid "$KEYID" "testtest" | gpg --import
  114. gpg --check-trustdb
  115. gpg --list-keys
  116. cat >"$TEMPDIR"/expectedout <<EOF
  117. pub:u:1024:1:$KEYID:$timestamp:::u:::scSC:
  118. uid:u::::$timestamp::E90EC72E68C6C2A0751DADC70F54F60D27B88C3D::monkeymonkey:
  119. sig:!::1:$KEYID:$timestamp::::monkeymonkey:13x:
  120. uid:r::::::8200BD0425CC70C7D698DF3FE412044EAAB83F94::testtest:
  121. sig:!::1:$KEYID:$timestamp::::monkeymonkey:13x:
  122. rev:!::1:$KEYID:$revtime::::monkeymonkey:30x:
  123. EOF
  124. diff -u "$TEMPDIR"/expectedout <(gpg --check-sigs --with-colons | grep -v ^tru)
  125. echo "##################################################"
  126. echo "### test working with two primary keys ... "
  127. ssh-keygen -t rsa -b 1024 -N '' -f "$TEMPDIR"/newkey
  128. PEM2OPENPGP_USAGE_FLAGS=authenticate,certify \
  129. PEM2OPENPGP_TIMESTAMP="$(( $timestamp + 1 ))" pem2openpgp fubar \
  130. < "$TEMPDIR"/newkey > "$TEMPDIR"/newkey.gpg
  131. NEWKEYFPR=$(< "$TEMPDIR"/newkey.gpg keytrans listfprs)
  132. NEWKEYID=$( printf "%s" "$NEWKEYFPR" | cut -b25-40)
  133. < "$TEMPDIR"/newkey.gpg gpg --import
  134. < "$TEMPDIR"/secring.gpg \
  135. PEM2OPENPGP_TIMESTAMP="$timestamp" \
  136. keytrans adduserid "$KEYID" "baz" | gpg --import
  137. cat >"$TEMPDIR"/expectedout <<EOF
  138. pub:u:1024:1:$KEYID:$timestamp:::u:::scSC:
  139. uid:u::::$timestamp::E90EC72E68C6C2A0751DADC70F54F60D27B88C3D::monkeymonkey:
  140. sig:!::1:$KEYID:$timestamp::::monkeymonkey:13x:
  141. uid:r::::::8200BD0425CC70C7D698DF3FE412044EAAB83F94::testtest:
  142. sig:!::1:$KEYID:$timestamp::::monkeymonkey:13x:
  143. rev:!::1:$KEYID:$revtime::::monkeymonkey:30x:
  144. uid:u::::$timestamp::EDDC32D783E7F4C7B6982D9AE5DC4A61000648BA::baz:
  145. sig:!::1:$KEYID:$timestamp::::monkeymonkey:13x:
  146. pub:-:1024:1:$NEWKEYID:$(($timestamp + 1)):::-:::caCA:
  147. uid:-::::$(($timestamp + 1))::A0D708F51CC257DEFC01AEDE1E0A5F329DFD8F16::fubar:
  148. sig:!::1:$NEWKEYID:$(($timestamp + 1))::::fubar:13x:
  149. EOF
  150. echo "test: diff expected gpg list output"
  151. diff -u "$TEMPDIR"/expectedout <(gpg --check-sigs --with-colons | grep -v ^tru)
  152. sort >"$TEMPDIR"/expectedout <<EOF
  153. $KEYFPR
  154. $NEWKEYFPR
  155. EOF
  156. echo "test: diff expected keytrans listfpr output"
  157. diff -u "$TEMPDIR"/expectedout <( < "$TEMPDIR"/secring.gpg keytrans listfprs | sort )
  158. ## FIXME: addtest: not testing subkeys at the moment.
  159. trap - EXIT
  160. echo "##################################################"
  161. echo " Monkeysphere keytrans test completed successfully!"
  162. echo "##################################################"
  163. cleanup