]> git.notmuchmail.org Git - notmuch/blob - test/T350-crypto.sh
tests: move FINGERPRINT definition to add_gnupg_home
[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 . $(dirname "$0")/test-lib.sh || exit 1
9
10 ##################################################
11
12 add_gnupg_home
13
14 test_begin_subtest "emacs delivery of signed message"
15 test_expect_success \
16 'emacs_fcc_message \
17     "test signed message 001" \
18     "This is a test signed message." \
19     "(mml-secure-message-sign)"'
20
21 test_begin_subtest "signed part content-type indexing"
22 output=$(notmuch search mimetype:multipart/signed and mimetype:application/pgp-signature | notmuch_search_sanitize)
23 test_expect_equal "$output" "thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; test signed message 001 (inbox signed)"
24
25 test_begin_subtest "signature verification"
26 output=$(notmuch show --format=json --verify subject:"test signed message 001" \
27     | notmuch_json_show_sanitize \
28     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
29 expected='[[[{"id": "XXXXX",
30  "match": true,
31  "excluded": false,
32  "filename": ["YYYYY"],
33  "timestamp": 946728000,
34  "date_relative": "2000-01-01",
35  "tags": ["inbox","signed"],
36  "headers": {"Subject": "test signed message 001",
37  "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
38  "To": "test_suite@notmuchmail.org",
39  "Date": "Sat, 01 Jan 2000 12:00:00 +0000"},
40  "body": [{"id": 1,
41  "sigstatus": [{"status": "good",
42  "fingerprint": "'$FINGERPRINT'",
43  "created": 946728000}],
44  "content-type": "multipart/signed",
45  "content": [{"id": 2,
46  "content-type": "text/plain",
47  "content": "This is a test signed message.\n"},
48  {"id": 3,
49  "content-type": "application/pgp-signature",
50  "content-length": "NONZERO"}]}]},
51  []]]]'
52 test_expect_equal_json \
53     "$output" \
54     "$expected"
55
56 test_begin_subtest "detection of modified signed contents"
57 emacs_fcc_message \
58     "bad signed message 001" \
59     "Incriminating stuff. This is a test signed message." \
60     "(mml-secure-message-sign)"
61
62 file=$(notmuch search --output=files subject:"bad signed message 001")
63
64 sed -i 's/Incriminating stuff. //' ${file}
65
66 output=$(notmuch show --format=json --verify subject:"bad signed message 001" \
67     | notmuch_json_show_sanitize \
68     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
69 expected='[[[{"id": "XXXXX",
70  "match": true,
71  "excluded": false,
72  "filename": ["YYYYY"],
73  "timestamp": 946728000,
74  "date_relative": "2000-01-01",
75  "tags": ["inbox","signed"],
76  "headers": {"Subject": "bad signed message 001",
77  "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
78  "To": "test_suite@notmuchmail.org",
79  "Date": "Sat, 01 Jan 2000 12:00:00 +0000"},
80  "body": [{"id": 1,
81  "sigstatus": [{"status": "bad",
82  "keyid": "'$(echo $FINGERPRINT | cut -c 25-)'"}],
83  "content-type": "multipart/signed",
84  "content": [{"id": 2,
85  "content-type": "text/plain",
86  "content": "This is a test signed message.\n"},
87  {"id": 3,
88  "content-type": "application/pgp-signature",
89  "content-length": "NONZERO"}]}]},
90  []]]]'
91 test_expect_equal_json \
92     "$output" \
93     "$expected"
94
95 test_begin_subtest "corrupted pgp/mime signature"
96 emacs_fcc_message \
97     "bad signed message 002" \
98     "Incriminating stuff. This is a test signed message." \
99     "(mml-secure-message-sign)"
100
101 file=$(notmuch search --output=files subject:"bad signed message 002")
102
103 awk '/-----BEGIN PGP SIGNATURE-----/{flag=1;print;next} \
104      /-----END PGP SIGNATURE-----/{flag=0;print;next} \
105      flag{gsub(/[A-Za-z]/,"0");print}!flag{print}' $file > $file.new
106
107 rm $file
108 mv $file.new $file
109
110 output=$(notmuch show --format=json --verify subject:"bad signed message 002" \
111     | notmuch_json_show_sanitize \
112     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
113 expected='[[[{"id": "XXXXX",
114  "match": true,
115  "excluded": false,
116  "filename": ["YYYYY"],
117  "timestamp": 946728000,
118  "date_relative": "2000-01-01",
119  "tags": ["inbox","signed"],
120  "headers": {"Subject": "bad signed message 002",
121  "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
122  "To": "test_suite@notmuchmail.org",
123  "Date": "Sat, 01 Jan 2000 12:00:00 +0000"},
124  "body": [{"id": 1,
125  "sigstatus": [],
126  "content-type": "multipart/signed",
127  "content": [{"id": 2,
128  "content-type": "text/plain",
129  "content": "Incriminating stuff. This is a test signed message.\n"},
130  {"id": 3,
131  "content-type": "application/pgp-signature",
132  "content-length": "NONZERO"}]}]},
133  []]]]'
134 test_expect_equal_json \
135     "$output" \
136     "$expected"
137
138 test_begin_subtest "signature verification with full owner trust"
139 test_subtest_broken_gmime_2
140 # give the key full owner trust
141 echo "${FINGERPRINT}:6:" | gpg --no-tty --import-ownertrust >>"$GNUPGHOME"/trust.log 2>&1
142 gpg --no-tty --check-trustdb >>"$GNUPGHOME"/trust.log 2>&1
143 output=$(notmuch show --format=json --verify subject:"test signed message 001" \
144     | notmuch_json_show_sanitize \
145     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
146 expected='[[[{"id": "XXXXX",
147  "match": true,
148  "excluded": false,
149  "filename": ["YYYYY"],
150  "timestamp": 946728000,
151  "date_relative": "2000-01-01",
152  "tags": ["inbox","signed"],
153  "headers": {"Subject": "test signed message 001",
154  "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
155  "To": "test_suite@notmuchmail.org",
156  "Date": "Sat, 01 Jan 2000 12:00:00 +0000"},
157  "body": [{"id": 1,
158  "sigstatus": [{"status": "good",
159  "fingerprint": "'$FINGERPRINT'",
160  "created": 946728000,
161  "userid": "Notmuch Test Suite <test_suite@notmuchmail.org> (INSECURE!)"}],
162  "content-type": "multipart/signed",
163  "content": [{"id": 2,
164  "content-type": "text/plain",
165  "content": "This is a test signed message.\n"},
166  {"id": 3,
167  "content-type": "application/pgp-signature",
168  "content-length": "NONZERO"}]}]},
169  []]]]'
170 test_expect_equal_json \
171     "$output" \
172     "$expected"
173
174 test_begin_subtest "signature verification with signer key unavailable"
175 # move the gnupghome temporarily out of the way
176 mv "${GNUPGHOME}"{,.bak}
177 output=$(notmuch show --format=json --verify subject:"test signed message 001" \
178     | notmuch_json_show_sanitize \
179     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
180 expected='[[[{"id": "XXXXX",
181  "match": true,
182  "excluded": false,
183  "filename": ["YYYYY"],
184  "timestamp": 946728000,
185  "date_relative": "2000-01-01",
186  "tags": ["inbox","signed"],
187  "headers": {"Subject": "test signed message 001",
188  "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
189  "To": "test_suite@notmuchmail.org",
190  "Date": "Sat, 01 Jan 2000 12:00:00 +0000"},
191  "body": [{"id": 1,
192  "sigstatus": [{"status": "error",
193  "keyid": "'$(echo $FINGERPRINT | cut -c 25-)'",
194  "errors": {"key-missing": true}}],
195  "content-type": "multipart/signed",
196  "content": [{"id": 2,
197  "content-type": "text/plain",
198  "content": "This is a test signed message.\n"},
199  {"id": 3,
200  "content-type": "application/pgp-signature",
201  "content-length": "NONZERO"}]}]},
202  []]]]'
203 test_expect_equal_json \
204     "$output" \
205     "$expected"
206 mv "${GNUPGHOME}"{.bak,}
207
208 test_begin_subtest "emacs delivery of encrypted message with attachment"
209 # create a test encrypted message with attachment
210 cat <<EOF >TESTATTACHMENT
211 This is a test file.
212 EOF
213 test_expect_success \
214 'emacs_fcc_message \
215     "test encrypted message 001" \
216     "This is a test encrypted message.\n" \
217     "(mml-attach-file \"TESTATTACHMENT\") (mml-secure-message-encrypt)"'
218
219 test_begin_subtest "encrypted part content-type indexing"
220 output=$(notmuch search mimetype:multipart/encrypted and mimetype:application/pgp-encrypted and mimetype:application/octet-stream | notmuch_search_sanitize)
221 test_expect_equal "$output" "thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; test encrypted message 001 (encrypted inbox)"
222
223 test_begin_subtest "decryption, --format=text"
224 output=$(notmuch show --format=text --decrypt=true subject:"test encrypted message 001" \
225     | notmuch_show_sanitize_all \
226     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
227 expected='\fmessage{ id:XXXXX depth:0 match:1 excluded:0 filename:XXXXX
228 \fheader{
229 Notmuch Test Suite <test_suite@notmuchmail.org> (2000-01-01) (encrypted inbox)
230 Subject: test encrypted message 001
231 From: Notmuch Test Suite <test_suite@notmuchmail.org>
232 To: test_suite@notmuchmail.org
233 Date: Sat, 01 Jan 2000 12:00:00 +0000
234 \fheader}
235 \fbody{
236 \fpart{ ID: 1, Content-type: multipart/encrypted
237 \fpart{ ID: 2, Content-type: application/pgp-encrypted
238 Non-text part: application/pgp-encrypted
239 \fpart}
240 \fpart{ ID: 3, Content-type: multipart/mixed
241 \fpart{ ID: 4, Content-type: text/plain
242 This is a test encrypted message.
243 \fpart}
244 \fattachment{ ID: 5, Filename: TESTATTACHMENT, Content-type: application/octet-stream
245 Non-text part: application/octet-stream
246 \fattachment}
247 \fpart}
248 \fpart}
249 \fbody}
250 \fmessage}'
251 test_expect_equal \
252     "$output" \
253     "$expected"
254
255 test_begin_subtest "decryption, --format=json"
256 output=$(notmuch show --format=json --decrypt=true subject:"test encrypted message 001" \
257     | notmuch_json_show_sanitize \
258     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
259 expected='[[[{"id": "XXXXX",
260  "match": true,
261  "excluded": false,
262  "filename": ["YYYYY"],
263  "timestamp": 946728000,
264  "date_relative": "2000-01-01",
265  "tags": ["encrypted","inbox"],
266  "headers": {"Subject": "test encrypted message 001",
267  "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
268  "To": "test_suite@notmuchmail.org",
269  "Date": "Sat, 01 Jan 2000 12:00:00 +0000"},
270  "body": [{"id": 1,
271  "encstatus": [{"status": "good"}],
272  "sigstatus": [],
273  "content-type": "multipart/encrypted",
274  "content": [{"id": 2,
275  "content-type": "application/pgp-encrypted",
276  "content-length": "NONZERO"},
277  {"id": 3,
278  "content-type": "multipart/mixed",
279  "content": [{"id": 4,
280  "content-type": "text/plain",
281  "content": "This is a test encrypted message.\n"},
282  {"id": 5,
283  "content-type": "application/octet-stream",
284  "content-disposition": "attachment",
285  "content-length": "NONZERO",
286  "content-transfer-encoding": "base64",
287  "filename": "TESTATTACHMENT"}]}]}]},
288  []]]]'
289 test_expect_equal_json \
290     "$output" \
291     "$expected"
292
293 test_begin_subtest "decryption, --format=json, --part=4"
294 output=$(notmuch show --format=json --part=4 --decrypt=true subject:"test encrypted message 001" \
295     | notmuch_json_show_sanitize \
296     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
297 expected='{"id": 4,
298  "content-type": "text/plain",
299  "content": "This is a test encrypted message.\n"}'
300 test_expect_equal_json \
301     "$output" \
302     "$expected"
303
304 test_begin_subtest "decrypt attachment (--part=5 --format=raw)"
305 notmuch show \
306     --format=raw \
307     --part=5 \
308     --decrypt=true \
309     subject:"test encrypted message 001" >OUTPUT
310 test_expect_equal_file TESTATTACHMENT OUTPUT
311
312 test_begin_subtest "decryption failure with missing key"
313 mv "${GNUPGHOME}"{,.bak}
314 output=$(notmuch show --format=json --decrypt=true subject:"test encrypted message 001" \
315     | notmuch_json_show_sanitize \
316     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
317 expected='[[[{"id": "XXXXX",
318  "match": true,
319  "excluded": false,
320  "filename": ["YYYYY"],
321  "timestamp": 946728000,
322  "date_relative": "2000-01-01",
323  "tags": ["encrypted","inbox"],
324  "headers": {"Subject": "test encrypted message 001",
325  "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
326  "To": "test_suite@notmuchmail.org",
327  "Date": "Sat, 01 Jan 2000 12:00:00 +0000"},
328  "body": [{"id": 1,
329  "encstatus": [{"status": "bad"}],
330  "content-type": "multipart/encrypted",
331  "content": [{"id": 2,
332  "content-type": "application/pgp-encrypted",
333  "content-length": "NONZERO"},
334  {"id": 3,
335  "content-type": "application/octet-stream",
336  "content-length": "NONZERO"}]}]},
337  []]]]'
338 test_expect_equal_json \
339     "$output" \
340     "$expected"
341 mv "${GNUPGHOME}"{.bak,}
342
343 test_begin_subtest "emacs delivery of encrypted + signed message"
344 test_expect_success \
345 'emacs_fcc_message \
346     "test encrypted message 002" \
347     "This is another test encrypted message.\n" \
348     "(mml-secure-message-sign-encrypt)"'
349
350 test_begin_subtest "decryption + signature verification"
351 test_subtest_broken_gmime_2
352 output=$(notmuch show --format=json --decrypt=true subject:"test encrypted message 002" \
353     | notmuch_json_show_sanitize \
354     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
355 expected='[[[{"id": "XXXXX",
356  "match": true,
357  "excluded": false,
358  "filename": ["YYYYY"],
359  "timestamp": 946728000,
360  "date_relative": "2000-01-01",
361  "tags": ["encrypted","inbox"],
362  "headers": {"Subject": "test encrypted message 002",
363  "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
364  "To": "test_suite@notmuchmail.org",
365  "Date": "Sat, 01 Jan 2000 12:00:00 +0000"},
366  "body": [{"id": 1,
367  "encstatus": [{"status": "good"}],
368  "sigstatus": [{"status": "good",
369  "fingerprint": "'$FINGERPRINT'",
370  "created": 946728000,
371  "userid": "Notmuch Test Suite <test_suite@notmuchmail.org> (INSECURE!)"}],
372  "content-type": "multipart/encrypted",
373  "content": [{"id": 2,
374  "content-type": "application/pgp-encrypted",
375  "content-length": "NONZERO"},
376  {"id": 3,
377  "content-type": "text/plain",
378  "content": "This is another test encrypted message.\n"}]}]},
379  []]]]'
380 test_expect_equal_json \
381     "$output" \
382     "$expected"
383
384 test_begin_subtest "reply to encrypted message"
385 output=$(notmuch reply --decrypt=true subject:"test encrypted message 002" \
386     | notmuch_drop_mail_headers In-Reply-To References)
387 expected='From: Notmuch Test Suite <test_suite@notmuchmail.org>
388 Subject: Re: test encrypted message 002
389 To: test_suite@notmuchmail.org
390
391 On 01 Jan 2000 12:00:00 -0000, Notmuch Test Suite <test_suite@notmuchmail.org> wrote:
392 > This is another test encrypted message.'
393 test_expect_equal \
394     "$output" \
395     "$expected"
396
397 test_begin_subtest "Reply within emacs to an encrypted message"
398 test_emacs "(let ((message-hidden-headers '())
399       (notmuch-crypto-process-mime 't))
400   (notmuch-show \"subject:test.encrypted.message.002\")
401   (notmuch-show-reply)
402   (test-output))"
403 grep -v -e '^In-Reply-To:' -e '^References:' -e '^Fcc:' < OUTPUT > OUTPUT.clean
404 cat <<EOF >EXPECTED
405 From: Notmuch Test Suite <test_suite@notmuchmail.org>
406 To: test_suite@notmuchmail.org
407 Subject: Re: test encrypted message 002
408 --text follows this line--
409 <#secure method=pgpmime mode=signencrypt>
410 Notmuch Test Suite <test_suite@notmuchmail.org> writes:
411
412 > This is another test encrypted message.
413 EOF
414 test_expect_equal_file EXPECTED OUTPUT.clean
415
416 test_begin_subtest "signature verification with revoked key"
417 # generate revocation certificate and load it to revoke key
418 echo "y
419 1
420 Notmuch Test Suite key revocation (automated) $(date '+%F_%T%z')
421
422 y
423
424 " \
425     | gpg --no-tty --quiet --command-fd 0 --armor --gen-revoke "0x${FINGERPRINT}!" 2>/dev/null \
426     | gpg --no-tty --quiet --import
427 output=$(notmuch show --format=json --verify subject:"test signed message 001" \
428     | notmuch_json_show_sanitize \
429     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
430 expected='[[[{"id": "XXXXX",
431  "match": true,
432  "excluded": false,
433  "filename": ["YYYYY"],
434  "timestamp": 946728000,
435  "date_relative": "2000-01-01",
436  "tags": ["inbox","signed"],
437  "headers": {"Subject": "test signed message 001",
438  "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
439  "To": "test_suite@notmuchmail.org",
440  "Date": "Sat, 01 Jan 2000 12:00:00 +0000"},
441  "body": [{"id": 1,
442  "sigstatus": [{"status": "error",
443  "keyid": "6D92612D94E46381",
444  "errors": {"key-revoked": true}}],
445  "content-type": "multipart/signed",
446  "content": [{"id": 2,
447  "content-type": "text/plain",
448  "content": "This is a test signed message.\n"},
449  {"id": 3,
450  "content-type": "application/pgp-signature",
451  "content-length": "NONZERO"}]}]},
452  []]]]'
453 test_expect_equal_json \
454     "$output" \
455     "$expected"
456
457 test_done