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