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