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