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