]> git.notmuchmail.org Git - notmuch/blob - test/T350-crypto.sh
lib: index the content type of signature parts
[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 test_subtest_known_broken
240 output=$(notmuch search mimetype:multipart/encrypted and mimetype:application/pgp-encrypted and mimetype:application/octet-stream | notmuch_search_sanitize)
241 test_expect_equal "$output" "thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; test encrypted message 001 (encrypted inbox)"
242
243 test_begin_subtest "decryption, --format=text"
244 output=$(notmuch show --format=text --decrypt subject:"test encrypted message 001" \
245     | notmuch_show_sanitize_all \
246     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
247 expected='\fmessage{ id:XXXXX depth:0 match:1 excluded:0 filename:XXXXX
248 \fheader{
249 Notmuch Test Suite <test_suite@notmuchmail.org> (2000-01-01) (encrypted inbox)
250 Subject: test encrypted message 001
251 From: Notmuch Test Suite <test_suite@notmuchmail.org>
252 To: test_suite@notmuchmail.org
253 Date: Sat, 01 Jan 2000 12:00:00 +0000
254 \fheader}
255 \fbody{
256 \fpart{ ID: 1, Content-type: multipart/encrypted
257 \fpart{ ID: 2, Content-type: application/pgp-encrypted
258 Non-text part: application/pgp-encrypted
259 \fpart}
260 \fpart{ ID: 3, Content-type: multipart/mixed
261 \fpart{ ID: 4, Content-type: text/plain
262 This is a test encrypted message.
263 \fpart}
264 \fattachment{ ID: 5, Filename: TESTATTACHMENT, Content-type: application/octet-stream
265 Non-text part: application/octet-stream
266 \fattachment}
267 \fpart}
268 \fpart}
269 \fbody}
270 \fmessage}'
271 test_expect_equal \
272     "$output" \
273     "$expected"
274
275 test_begin_subtest "decryption, --format=json"
276 output=$(notmuch show --format=json --decrypt subject:"test encrypted message 001" \
277     | notmuch_json_show_sanitize \
278     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
279 expected='[[[{"id": "XXXXX",
280  "match": true,
281  "excluded": false,
282  "filename": ["YYYYY"],
283  "timestamp": 946728000,
284  "date_relative": "2000-01-01",
285  "tags": ["encrypted","inbox"],
286  "headers": {"Subject": "test encrypted message 001",
287  "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
288  "To": "test_suite@notmuchmail.org",
289  "Date": "Sat, 01 Jan 2000 12:00:00 +0000"},
290  "body": [{"id": 1,
291  "encstatus": [{"status": "good"}],
292  "sigstatus": [],
293  "content-type": "multipart/encrypted",
294  "content": [{"id": 2,
295  "content-type": "application/pgp-encrypted",
296  "content-length": "NONZERO"},
297  {"id": 3,
298  "content-type": "multipart/mixed",
299  "content": [{"id": 4,
300  "content-type": "text/plain",
301  "content": "This is a test encrypted message.\n"},
302  {"id": 5,
303  "content-type": "application/octet-stream",
304  "content-disposition": "attachment",
305  "content-length": "NONZERO",
306  "content-transfer-encoding": "base64",
307  "filename": "TESTATTACHMENT"}]}]}]},
308  []]]]'
309 test_expect_equal_json \
310     "$output" \
311     "$expected"
312
313 test_begin_subtest "decryption, --format=json, --part=4"
314 output=$(notmuch show --format=json --part=4 --decrypt subject:"test encrypted message 001" \
315     | notmuch_json_show_sanitize \
316     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
317 expected='{"id": 4,
318  "content-type": "text/plain",
319  "content": "This is a test encrypted message.\n"}'
320 test_expect_equal_json \
321     "$output" \
322     "$expected"
323
324 test_begin_subtest "decrypt attachment (--part=5 --format=raw)"
325 notmuch show \
326     --format=raw \
327     --part=5 \
328     --decrypt \
329     subject:"test encrypted message 001" >OUTPUT
330 test_expect_equal_file TESTATTACHMENT OUTPUT
331
332 test_begin_subtest "decryption failure with missing key"
333 mv "${GNUPGHOME}"{,.bak}
334 output=$(notmuch show --format=json --decrypt subject:"test encrypted message 001" \
335     | notmuch_json_show_sanitize \
336     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
337 expected='[[[{"id": "XXXXX",
338  "match": true,
339  "excluded": false,
340  "filename": ["YYYYY"],
341  "timestamp": 946728000,
342  "date_relative": "2000-01-01",
343  "tags": ["encrypted","inbox"],
344  "headers": {"Subject": "test encrypted message 001",
345  "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
346  "To": "test_suite@notmuchmail.org",
347  "Date": "Sat, 01 Jan 2000 12:00:00 +0000"},
348  "body": [{"id": 1,
349  "encstatus": [{"status": "bad"}],
350  "content-type": "multipart/encrypted",
351  "content": [{"id": 2,
352  "content-type": "application/pgp-encrypted",
353  "content-length": "NONZERO"},
354  {"id": 3,
355  "content-type": "application/octet-stream",
356  "content-length": "NONZERO"}]}]},
357  []]]]'
358 test_expect_equal_json \
359     "$output" \
360     "$expected"
361 mv "${GNUPGHOME}"{.bak,}
362
363 test_begin_subtest "emacs delivery of encrypted + signed message"
364 test_expect_success \
365 'emacs_fcc_message \
366     "test encrypted message 002" \
367     "This is another test encrypted message.\n" \
368     "(mml-secure-message-sign-encrypt)"'
369
370 test_begin_subtest "decryption + signature verification"
371 test_subtest_broken_gmime_2
372 output=$(notmuch show --format=json --decrypt subject:"test encrypted message 002" \
373     | notmuch_json_show_sanitize \
374     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
375 expected='[[[{"id": "XXXXX",
376  "match": true,
377  "excluded": false,
378  "filename": ["YYYYY"],
379  "timestamp": 946728000,
380  "date_relative": "2000-01-01",
381  "tags": ["encrypted","inbox"],
382  "headers": {"Subject": "test encrypted message 002",
383  "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
384  "To": "test_suite@notmuchmail.org",
385  "Date": "Sat, 01 Jan 2000 12:00:00 +0000"},
386  "body": [{"id": 1,
387  "encstatus": [{"status": "good"}],
388  "sigstatus": [{"status": "good",
389  "fingerprint": "'$FINGERPRINT'",
390  "created": 946728000,
391  "userid": "Notmuch Test Suite <test_suite@notmuchmail.org> (INSECURE!)"}],
392  "content-type": "multipart/encrypted",
393  "content": [{"id": 2,
394  "content-type": "application/pgp-encrypted",
395  "content-length": "NONZERO"},
396  {"id": 3,
397  "content-type": "text/plain",
398  "content": "This is another test encrypted message.\n"}]}]},
399  []]]]'
400 test_expect_equal_json \
401     "$output" \
402     "$expected"
403
404 test_begin_subtest "reply to encrypted message"
405 output=$(notmuch reply --decrypt subject:"test encrypted message 002" \
406     | notmuch_drop_mail_headers In-Reply-To References)
407 expected='From: Notmuch Test Suite <test_suite@notmuchmail.org>
408 Subject: Re: test encrypted message 002
409
410 On 01 Jan 2000 12:00:00 -0000, Notmuch Test Suite <test_suite@notmuchmail.org> wrote:
411 > This is another test encrypted message.'
412 test_expect_equal \
413     "$output" \
414     "$expected"
415
416 test_begin_subtest "Reply within emacs to an encrypted message"
417 test_emacs "(let ((message-hidden-headers '())
418       (notmuch-crypto-process-mime 't))
419   (notmuch-show \"subject:test.encrypted.message.002\")
420   (notmuch-show-reply)
421   (test-output))"
422 # the empty To: is probably a bug, but it's not to do with encryption
423 grep -v -e '^In-Reply-To:' -e '^References:' -e '^Fcc:' -e 'To:' < OUTPUT > OUTPUT.clean
424 cat <<EOF >EXPECTED
425 From: Notmuch Test Suite <test_suite@notmuchmail.org>
426 Subject: Re: test encrypted message 002
427 --text follows this line--
428 <#secure method=pgpmime mode=signencrypt>
429 Notmuch Test Suite <test_suite@notmuchmail.org> writes:
430
431 > This is another test encrypted message.
432 EOF
433 test_expect_equal_file EXPECTED OUTPUT.clean
434
435 test_begin_subtest "signature verification with revoked key"
436 # generate revocation certificate and load it to revoke key
437 echo "y
438 1
439 Notmuch Test Suite key revocation (automated) $(date '+%F_%T%z')
440
441 y
442
443 " \
444     | gpg --no-tty --quiet --command-fd 0 --armor --gen-revoke "0x${FINGERPRINT}!" 2>/dev/null \
445     | gpg --no-tty --quiet --import
446 output=$(notmuch show --format=json --verify subject:"test signed message 001" \
447     | notmuch_json_show_sanitize \
448     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
449 expected='[[[{"id": "XXXXX",
450  "match": true,
451  "excluded": false,
452  "filename": ["YYYYY"],
453  "timestamp": 946728000,
454  "date_relative": "2000-01-01",
455  "tags": ["inbox","signed"],
456  "headers": {"Subject": "test signed message 001",
457  "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
458  "To": "test_suite@notmuchmail.org",
459  "Date": "Sat, 01 Jan 2000 12:00:00 +0000"},
460  "body": [{"id": 1,
461  "sigstatus": [{"status": "error",
462  "keyid": "6D92612D94E46381",
463  "errors": {"key-revoked": true}}],
464  "content-type": "multipart/signed",
465  "content": [{"id": 2,
466  "content-type": "text/plain",
467  "content": "This is a test signed message.\n"},
468  {"id": 3,
469  "content-type": "application/pgp-signature",
470  "content-length": "NONZERO"}]}]},
471  []]]]'
472 test_expect_equal_json \
473     "$output" \
474     "$expected"
475
476 test_done