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