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