exit lingering gpg agents at the end of relevant tests
[notmuch] / test / T350-crypto.sh
1 #!/usr/bin/env bash
2
3 # TODO:
4 # - decryption/verification with signer key not available
5 # - verification of signatures from expired/revoked keys
6
7 test_description='PGP/MIME signature verification and decryption'
8 . ./test-lib.sh || exit 1
9
10 add_gnupg_home ()
11 {
12     local output
13     [ -d ${GNUPGHOME} ] && return
14     _gnupg_exit () { gpgconf --kill all 2>/dev/null || true; }
15     at_exit_function _gnupg_exit
16     mkdir -m 0700 "$GNUPGHOME"
17     gpg --no-tty --import <$TEST_DIRECTORY/gnupg-secret-key.asc >"$GNUPGHOME"/import.log 2>&1
18     test_debug "cat $GNUPGHOME/import.log"
19     if (gpg --quick-random --version >/dev/null 2>&1) ; then
20         echo quick-random >> "$GNUPGHOME"/gpg.conf
21     elif (gpg --debug-quick-random --version >/dev/null 2>&1) ; then
22         echo debug-quick-random >> "$GNUPGHOME"/gpg.conf
23     fi
24     echo no-emit-version >> "$GNUPGHOME"/gpg.conf
25 }
26
27 ##################################################
28
29 add_gnupg_home
30 # Change this if we ship a new test key
31 FINGERPRINT="5AEAB11F5E33DCE875DDB75B6D92612D94E46381"
32
33 test_begin_subtest "emacs delivery of signed message"
34 test_expect_success \
35 'emacs_fcc_message \
36     "test signed message 001" \
37     "This is a test signed message." \
38     "(mml-secure-message-sign)"'
39
40 test_begin_subtest "signature verification"
41 output=$(notmuch show --format=json --verify subject:"test signed message 001" \
42     | notmuch_json_show_sanitize \
43     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
44 expected='[[[{"id": "XXXXX",
45  "match": true,
46  "excluded": false,
47  "filename": ["YYYYY"],
48  "timestamp": 946728000,
49  "date_relative": "2000-01-01",
50  "tags": ["inbox","signed"],
51  "headers": {"Subject": "test signed message 001",
52  "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
53  "To": "test_suite@notmuchmail.org",
54  "Date": "Sat, 01 Jan 2000 12:00:00 +0000"},
55  "body": [{"id": 1,
56  "sigstatus": [{"status": "good",
57  "fingerprint": "'$FINGERPRINT'",
58  "created": 946728000}],
59  "content-type": "multipart/signed",
60  "content": [{"id": 2,
61  "content-type": "text/plain",
62  "content": "This is a test signed message.\n"},
63  {"id": 3,
64  "content-type": "application/pgp-signature",
65  "content-length": "NONZERO"}]}]},
66  []]]]'
67 test_expect_equal_json \
68     "$output" \
69     "$expected"
70
71 test_begin_subtest "signature verification with full owner trust"
72 # give the key full owner trust
73 echo "${FINGERPRINT}:6:" | gpg --no-tty --import-ownertrust >>"$GNUPGHOME"/trust.log 2>&1
74 gpg --no-tty --check-trustdb >>"$GNUPGHOME"/trust.log 2>&1
75 output=$(notmuch show --format=json --verify subject:"test signed message 001" \
76     | notmuch_json_show_sanitize \
77     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
78 expected='[[[{"id": "XXXXX",
79  "match": true,
80  "excluded": false,
81  "filename": ["YYYYY"],
82  "timestamp": 946728000,
83  "date_relative": "2000-01-01",
84  "tags": ["inbox","signed"],
85  "headers": {"Subject": "test signed message 001",
86  "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
87  "To": "test_suite@notmuchmail.org",
88  "Date": "Sat, 01 Jan 2000 12:00:00 +0000"},
89  "body": [{"id": 1,
90  "sigstatus": [{"status": "good",
91  "fingerprint": "'$FINGERPRINT'",
92  "created": 946728000,
93  "userid": " Notmuch Test Suite <test_suite@notmuchmail.org> (INSECURE!)"}],
94  "content-type": "multipart/signed",
95  "content": [{"id": 2,
96  "content-type": "text/plain",
97  "content": "This is a test signed message.\n"},
98  {"id": 3,
99  "content-type": "application/pgp-signature",
100  "content-length": "NONZERO"}]}]},
101  []]]]'
102 test_expect_equal_json \
103     "$output" \
104     "$expected"
105
106 test_begin_subtest "signature verification with signer key unavailable"
107 # move the gnupghome temporarily out of the way
108 mv "${GNUPGHOME}"{,.bak}
109 output=$(notmuch show --format=json --verify subject:"test signed message 001" \
110     | notmuch_json_show_sanitize \
111     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
112 expected='[[[{"id": "XXXXX",
113  "match": true,
114  "excluded": false,
115  "filename": ["YYYYY"],
116  "timestamp": 946728000,
117  "date_relative": "2000-01-01",
118  "tags": ["inbox","signed"],
119  "headers": {"Subject": "test signed message 001",
120  "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
121  "To": "test_suite@notmuchmail.org",
122  "Date": "Sat, 01 Jan 2000 12:00:00 +0000"},
123  "body": [{"id": 1,
124  "sigstatus": [{"status": "error",
125  "keyid": "'$(echo $FINGERPRINT | cut -c 25-)'",
126  "errors": 2}],
127  "content-type": "multipart/signed",
128  "content": [{"id": 2,
129  "content-type": "text/plain",
130  "content": "This is a test signed message.\n"},
131  {"id": 3,
132  "content-type": "application/pgp-signature",
133  "content-length": "NONZERO"}]}]},
134  []]]]'
135 test_expect_equal_json \
136     "$output" \
137     "$expected"
138 mv "${GNUPGHOME}"{.bak,}
139
140 test_begin_subtest "emacs delivery of encrypted message with attachment"
141 # create a test encrypted message with attachment
142 cat <<EOF >TESTATTACHMENT
143 This is a test file.
144 EOF
145 test_expect_success \
146 'emacs_fcc_message \
147     "test encrypted message 001" \
148     "This is a test encrypted message.\n" \
149     "(mml-attach-file \"TESTATTACHMENT\") (mml-secure-message-encrypt)"'
150
151 test_begin_subtest "decryption, --format=text"
152 output=$(notmuch show --format=text --decrypt subject:"test encrypted message 001" \
153     | notmuch_show_sanitize_all \
154     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
155 expected='\fmessage{ id:XXXXX depth:0 match:1 excluded:0 filename:XXXXX
156 \fheader{
157 Notmuch Test Suite <test_suite@notmuchmail.org> (2000-01-01) (encrypted inbox)
158 Subject: test encrypted message 001
159 From: Notmuch Test Suite <test_suite@notmuchmail.org>
160 To: test_suite@notmuchmail.org
161 Date: Sat, 01 Jan 2000 12:00:00 +0000
162 \fheader}
163 \fbody{
164 \fpart{ ID: 1, Content-type: multipart/encrypted
165 \fpart{ ID: 2, Content-type: application/pgp-encrypted
166 Non-text part: application/pgp-encrypted
167 \fpart}
168 \fpart{ ID: 3, Content-type: multipart/mixed
169 \fpart{ ID: 4, Content-type: text/plain
170 This is a test encrypted message.
171 \fpart}
172 \fattachment{ ID: 5, Filename: TESTATTACHMENT, Content-type: application/octet-stream
173 Non-text part: application/octet-stream
174 \fattachment}
175 \fpart}
176 \fpart}
177 \fbody}
178 \fmessage}'
179 test_expect_equal \
180     "$output" \
181     "$expected"
182
183 test_begin_subtest "decryption, --format=json"
184 output=$(notmuch show --format=json --decrypt subject:"test encrypted message 001" \
185     | notmuch_json_show_sanitize \
186     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
187 expected='[[[{"id": "XXXXX",
188  "match": true,
189  "excluded": false,
190  "filename": ["YYYYY"],
191  "timestamp": 946728000,
192  "date_relative": "2000-01-01",
193  "tags": ["encrypted","inbox"],
194  "headers": {"Subject": "test encrypted message 001",
195  "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
196  "To": "test_suite@notmuchmail.org",
197  "Date": "Sat, 01 Jan 2000 12:00:00 +0000"},
198  "body": [{"id": 1,
199  "encstatus": [{"status": "good"}],
200  "sigstatus": [],
201  "content-type": "multipart/encrypted",
202  "content": [{"id": 2,
203  "content-type": "application/pgp-encrypted",
204  "content-length": "NONZERO"},
205  {"id": 3,
206  "content-type": "multipart/mixed",
207  "content": [{"id": 4,
208  "content-type": "text/plain",
209  "content": "This is a test encrypted message.\n"},
210  {"id": 5,
211  "content-type": "application/octet-stream",
212  "content-disposition": "attachment",
213  "content-length": "NONZERO",
214  "content-transfer-encoding": "base64",
215  "filename": "TESTATTACHMENT"}]}]}]},
216  []]]]'
217 test_expect_equal_json \
218     "$output" \
219     "$expected"
220
221 test_begin_subtest "decryption, --format=json, --part=4"
222 output=$(notmuch show --format=json --part=4 --decrypt subject:"test encrypted message 001" \
223     | notmuch_json_show_sanitize \
224     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
225 expected='{"id": 4,
226  "content-type": "text/plain",
227  "content": "This is a test encrypted message.\n"}'
228 test_expect_equal_json \
229     "$output" \
230     "$expected"
231
232 test_begin_subtest "decrypt attachment (--part=5 --format=raw)"
233 notmuch show \
234     --format=raw \
235     --part=5 \
236     --decrypt \
237     subject:"test encrypted message 001" >OUTPUT
238 test_expect_equal_file TESTATTACHMENT OUTPUT
239
240 test_begin_subtest "decryption failure with missing key"
241 mv "${GNUPGHOME}"{,.bak}
242 output=$(notmuch show --format=json --decrypt subject:"test encrypted message 001" \
243     | notmuch_json_show_sanitize \
244     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
245 expected='[[[{"id": "XXXXX",
246  "match": true,
247  "excluded": false,
248  "filename": ["YYYYY"],
249  "timestamp": 946728000,
250  "date_relative": "2000-01-01",
251  "tags": ["encrypted","inbox"],
252  "headers": {"Subject": "test encrypted message 001",
253  "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
254  "To": "test_suite@notmuchmail.org",
255  "Date": "Sat, 01 Jan 2000 12:00:00 +0000"},
256  "body": [{"id": 1,
257  "encstatus": [{"status": "bad"}],
258  "content-type": "multipart/encrypted",
259  "content": [{"id": 2,
260  "content-type": "application/pgp-encrypted",
261  "content-length": "NONZERO"},
262  {"id": 3,
263  "content-type": "application/octet-stream",
264  "content-length": "NONZERO"}]}]},
265  []]]]'
266 test_expect_equal_json \
267     "$output" \
268     "$expected"
269 mv "${GNUPGHOME}"{.bak,}
270
271 test_begin_subtest "emacs delivery of encrypted + signed message"
272 test_expect_success \
273 'emacs_fcc_message \
274     "test encrypted message 002" \
275     "This is another test encrypted message.\n" \
276     "(mml-secure-message-sign-encrypt)"'
277
278 test_begin_subtest "decryption + signature verification"
279 output=$(notmuch show --format=json --decrypt subject:"test encrypted message 002" \
280     | notmuch_json_show_sanitize \
281     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
282 expected='[[[{"id": "XXXXX",
283  "match": true,
284  "excluded": false,
285  "filename": ["YYYYY"],
286  "timestamp": 946728000,
287  "date_relative": "2000-01-01",
288  "tags": ["encrypted","inbox"],
289  "headers": {"Subject": "test encrypted message 002",
290  "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
291  "To": "test_suite@notmuchmail.org",
292  "Date": "Sat, 01 Jan 2000 12:00:00 +0000"},
293  "body": [{"id": 1,
294  "encstatus": [{"status": "good"}],
295  "sigstatus": [{"status": "good",
296  "fingerprint": "'$FINGERPRINT'",
297  "created": 946728000,
298  "userid": " Notmuch Test Suite <test_suite@notmuchmail.org> (INSECURE!)"}],
299  "content-type": "multipart/encrypted",
300  "content": [{"id": 2,
301  "content-type": "application/pgp-encrypted",
302  "content-length": "NONZERO"},
303  {"id": 3,
304  "content-type": "text/plain",
305  "content": "This is another test encrypted message.\n"}]}]},
306  []]]]'
307 test_expect_equal_json \
308     "$output" \
309     "$expected"
310
311 test_begin_subtest "reply to encrypted message"
312 output=$(notmuch reply --decrypt subject:"test encrypted message 002" \
313     | grep -v -e '^In-Reply-To:' -e '^References:')
314 expected='From: Notmuch Test Suite <test_suite@notmuchmail.org>
315 Subject: Re: test encrypted message 002
316
317 On 01 Jan 2000 12:00:00 -0000, Notmuch Test Suite <test_suite@notmuchmail.org> wrote:
318 > This is another test encrypted message.'
319 test_expect_equal \
320     "$output" \
321     "$expected"
322
323 test_begin_subtest "Reply within emacs to an encrypted message"
324 test_emacs "(let ((message-hidden-headers '())
325       (notmuch-crypto-process-mime 't))
326   (notmuch-show \"subject:test.encrypted.message.002\")
327   (notmuch-show-reply)
328   (test-output))"
329 # the empty To: is probably a bug, but it's not to do with encryption
330 grep -v -e '^In-Reply-To:' -e '^References:' -e '^Fcc:' -e 'To:' < OUTPUT > OUTPUT.clean
331 cat <<EOF >EXPECTED
332 From: Notmuch Test Suite <test_suite@notmuchmail.org>
333 Subject: Re: test encrypted message 002
334 --text follows this line--
335 <#secure method=pgpmime mode=signencrypt>
336 Notmuch Test Suite <test_suite@notmuchmail.org> writes:
337
338 > This is another test encrypted message.
339 EOF
340 test_expect_equal_file EXPECTED OUTPUT.clean
341
342 test_begin_subtest "signature verification with revoked key"
343 # generate revocation certificate and load it to revoke key
344 echo "y
345 1
346 Notmuch Test Suite key revocation (automated) $(date '+%F_%T%z')
347
348 y
349
350 " \
351     | gpg --no-tty --quiet --command-fd 0 --armor --gen-revoke "0x${FINGERPRINT}!" 2>/dev/null \
352     | gpg --no-tty --quiet --import
353 output=$(notmuch show --format=json --verify subject:"test signed message 001" \
354     | notmuch_json_show_sanitize \
355     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
356 expected='[[[{"id": "XXXXX",
357  "match": true,
358  "excluded": false,
359  "filename": ["YYYYY"],
360  "timestamp": 946728000,
361  "date_relative": "2000-01-01",
362  "tags": ["inbox","signed"],
363  "headers": {"Subject": "test signed message 001",
364  "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
365  "To": "test_suite@notmuchmail.org",
366  "Date": "Sat, 01 Jan 2000 12:00:00 +0000"},
367  "body": [{"id": 1,
368  "sigstatus": [{"status": "error",
369  "keyid": "6D92612D94E46381",
370  "errors": 8}],
371  "content-type": "multipart/signed",
372  "content": [{"id": 2,
373  "content-type": "text/plain",
374  "content": "This is a test signed message.\n"},
375  {"id": 3,
376  "content-type": "application/pgp-signature",
377  "content-length": "NONZERO"}]}]},
378  []]]]'
379 test_expect_equal_json \
380     "$output" \
381     "$expected"
382
383 test_done