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