]> git.notmuchmail.org Git - notmuch/blob - test/crypto
test: test for absence of "Non-text part: application/pgp-*" lines in reply
[notmuch] / test / crypto
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
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 }
23
24 ##################################################
25
26 add_gnupg_home
27 # get key fingerprint
28 FINGERPRINT=$(gpg --no-tty --list-secret-keys --with-colons --fingerprint | grep '^fpr:' | cut -d: -f10)
29
30 # for some reason this is needed for emacs_deliver_message to work,
31 # although I can't figure out why
32 add_email_corpus
33
34 test_expect_success 'emacs delivery of signed message' \
35 'emacs_deliver_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  "filename": "YYYYY",
47  "timestamp": 946728000,
48  "date_relative": "2000-01-01",
49  "tags": ["inbox","signed"],
50  "headers": {"Subject": "test signed message 001",
51  "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
52  "To": "test_suite@notmuchmail.org",
53  "Cc": "",
54  "Bcc": "",
55  "Date": "01 Jan 2000 12:00:00 -0000"},
56  "body": [{"id": 1,
57  "sigstatus": [{"status": "good",
58  "fingerprint": "'$FINGERPRINT'",
59  "created": 946728000}],
60  "content-type": "multipart/signed",
61  "content": [{"id": 2,
62  "content-type": "text/plain",
63  "content": "This is a test signed message.\n"},
64  {"id": 3,
65  "content-type": "application/pgp-signature"}]}]},
66  []]]]'
67 test_expect_equal \
68     "$output" \
69     "$expected"
70
71 test_begin_subtest "signature verification with full owner trust"
72 # give the key full owner trust
73 echo "${FINGERPRINT}:6:" | gpg --no-tty --import-ownertrust >>"$GNUPGHOME"/trust.log 2>&1
74 gpg --no-tty --check-trustdb >>"$GNUPGHOME"/trust.log 2>&1
75 output=$(notmuch show --format=json --verify subject:"test signed message 001" \
76     | notmuch_json_show_sanitize \
77     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
78 expected='[[[{"id": "XXXXX",
79  "match": true,
80  "filename": "YYYYY",
81  "timestamp": 946728000,
82  "date_relative": "2000-01-01",
83  "tags": ["inbox","signed"],
84  "headers": {"Subject": "test signed message 001",
85  "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
86  "To": "test_suite@notmuchmail.org",
87  "Cc": "",
88  "Bcc": "",
89  "Date": "01 Jan 2000 12:00:00 -0000"},
90  "body": [{"id": 1,
91  "sigstatus": [{"status": "good",
92  "fingerprint": "'$FINGERPRINT'",
93  "created": 946728000,
94  "userid": " Notmuch Test Suite <test_suite@notmuchmail.org> (INSECURE!)"}],
95  "content-type": "multipart/signed",
96  "content": [{"id": 2,
97  "content-type": "text/plain",
98  "content": "This is a test signed message.\n"},
99  {"id": 3,
100  "content-type": "application/pgp-signature"}]}]},
101  []]]]'
102 test_expect_equal \
103     "$output" \
104     "$expected"
105
106 test_begin_subtest "signature verification with signer key unavailable"
107 # move the gnupghome temporarily out of the way
108 mv "${GNUPGHOME}"{,.bak}
109 output=$(notmuch show --format=json --verify subject:"test signed message 001" \
110     | notmuch_json_show_sanitize \
111     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
112 expected='[[[{"id": "XXXXX",
113  "match": true,
114  "filename": "YYYYY",
115  "timestamp": 946728000,
116  "date_relative": "2000-01-01",
117  "tags": ["inbox","signed"],
118  "headers": {"Subject": "test signed message 001",
119  "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
120  "To": "test_suite@notmuchmail.org",
121  "Cc": "",
122  "Bcc": "",
123  "Date": "01 Jan 2000 12:00:00 -0000"},
124  "body": [{"id": 1,
125  "sigstatus": [{"status": "error",
126  "keyid": "'$(echo $FINGERPRINT | cut -c 25-)'",
127  "errors": 2}],
128  "content-type": "multipart/signed",
129  "content": [{"id": 2,
130  "content-type": "text/plain",
131  "content": "This is a test signed message.\n"},
132  {"id": 3,
133  "content-type": "application/pgp-signature"}]}]},
134  []]]]'
135 test_expect_equal \
136     "$output" \
137     "$expected"
138 mv "${GNUPGHOME}"{.bak,}
139
140 # create a test encrypted message with attachment
141 cat <<EOF >TESTATTACHMENT
142 This is a test file.
143 EOF
144 test_expect_success 'emacs delivery of encrypted message with attachment' \
145 'emacs_deliver_message \
146     "test encrypted message 001" \
147     "This is a test encrypted message.\n" \
148     "(mml-attach-file \"TESTATTACHMENT\") (mml-secure-message-encrypt)"'
149
150 test_begin_subtest "decryption, --format=text"
151 output=$(notmuch show --format=text --decrypt subject:"test encrypted message 001" \
152     | notmuch_show_sanitize_all \
153     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
154 expected='\fmessage{ id:XXXXX depth:0 match:1 filename:XXXXX
155 \fheader{
156 Notmuch Test Suite <test_suite@notmuchmail.org> (2000-01-01) (encrypted inbox)
157 Subject: test encrypted message 001
158 From: Notmuch Test Suite <test_suite@notmuchmail.org>
159 To: test_suite@notmuchmail.org
160 Date: 01 Jan 2000 12:00:00 -0000
161 \fheader}
162 \fbody{
163 \fpart{ ID: 1, Content-type: multipart/encrypted
164 \fpart{ ID: 2, Content-type: application/pgp-encrypted
165 Non-text part: application/pgp-encrypted
166 \fpart}
167 \fpart{ ID: 3, Content-type: multipart/mixed
168 \fpart{ ID: 4, Content-type: text/plain
169 This is a test encrypted message.
170 \fpart}
171 \fattachment{ ID: 5, Filename: TESTATTACHMENT, Content-type: application/octet-stream
172 Non-text part: application/octet-stream
173 \fattachment}
174 \fpart}
175 \fpart}
176 \fbody}
177 \fmessage}'
178 test_expect_equal \
179     "$output" \
180     "$expected"
181
182 test_begin_subtest "decryption, --format=json"
183 output=$(notmuch show --format=json --decrypt subject:"test encrypted message 001" \
184     | notmuch_json_show_sanitize \
185     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
186 expected='[[[{"id": "XXXXX",
187  "match": true,
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  "Cc": "",
196  "Bcc": "",
197  "Date": "01 Jan 2000 12:00:00 -0000"},
198  "body": [{"id": 1,
199  "encstatus": [{"status": "good"}],
200  "sigstatus": [],
201  "content-type": "multipart/encrypted",
202  "content": [{"id": 2,
203  "content-type": "application/pgp-encrypted"},
204  {"id": 3,
205  "content-type": "multipart/mixed",
206  "content": [{"id": 4,
207  "content-type": "text/plain",
208  "content": "This is a test encrypted message.\n"},
209  {"id": 5,
210  "content-type": "application/octet-stream",
211  "filename": "TESTATTACHMENT"}]}]}]},
212  []]]]'
213 test_expect_equal \
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 \
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  "filename": "YYYYY",
244  "timestamp": 946728000,
245  "date_relative": "2000-01-01",
246  "tags": ["encrypted","inbox"],
247  "headers": {"Subject": "test encrypted message 001",
248  "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
249  "To": "test_suite@notmuchmail.org",
250  "Cc": "",
251  "Bcc": "",
252  "Date": "01 Jan 2000 12:00:00 -0000"},
253  "body": [{"id": 1,
254  "encstatus": [{"status": "bad"}],
255  "content-type": "multipart/encrypted",
256  "content": [{"id": 2,
257  "content-type": "application/pgp-encrypted"},
258  {"id": 3,
259  "content-type": "application/octet-stream"}]}]},
260  []]]]'
261 test_expect_equal \
262     "$output" \
263     "$expected"
264 mv "${GNUPGHOME}"{.bak,}
265
266 test_expect_success 'emacs delivery of encrypted + signed message' \
267 'emacs_deliver_message \
268     "test encrypted message 002" \
269     "This is another test encrypted message.\n" \
270     "(mml-secure-message-sign-encrypt)"'
271
272 test_begin_subtest "decryption + signature verification"
273 output=$(notmuch show --format=json --decrypt subject:"test encrypted message 002" \
274     | notmuch_json_show_sanitize \
275     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
276 expected='[[[{"id": "XXXXX",
277  "match": true,
278  "filename": "YYYYY",
279  "timestamp": 946728000,
280  "date_relative": "2000-01-01",
281  "tags": ["encrypted","inbox"],
282  "headers": {"Subject": "test encrypted message 002",
283  "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
284  "To": "test_suite@notmuchmail.org",
285  "Cc": "",
286  "Bcc": "",
287  "Date": "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  {"id": 3,
298  "content-type": "text/plain",
299  "content": "This is another test encrypted message.\n"}]}]},
300  []]]]'
301 test_expect_equal \
302     "$output" \
303     "$expected"
304
305 test_begin_subtest "reply to encrypted message"
306 output=$(notmuch reply --decrypt subject:"test encrypted message 002" \
307     | grep -v -e '^In-Reply-To:' -e '^References:')
308 expected='From: Notmuch Test Suite <test_suite@notmuchmail.org>
309 Subject: Re: test encrypted message 002
310
311 On 01 Jan 2000 12:00:00 -0000, Notmuch Test Suite <test_suite@notmuchmail.org> wrote:
312 > This is another test encrypted message.'
313 test_expect_equal \
314     "$output" \
315     "$expected"
316
317 test_begin_subtest "signature verification with revoked key"
318 # generate revocation certificate and load it to revoke key
319 echo "y
320 1
321 Notmuch Test Suite key revocation (automated) $(date '+%F_%T%z')
322
323 y
324
325 " \
326     | gpg --no-tty --quiet --command-fd 0 --armor --gen-revoke "0x${FINGERPRINT}!" 2>/dev/null \
327     | gpg --no-tty --quiet --import
328 output=$(notmuch show --format=json --verify subject:"test signed message 001" \
329     | notmuch_json_show_sanitize \
330     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
331 expected='[[[{"id": "XXXXX",
332  "match": true,
333  "filename": "YYYYY",
334  "timestamp": 946728000,
335  "date_relative": "2000-01-01",
336  "tags": ["inbox","signed"],
337  "headers": {"Subject": "test signed message 001",
338  "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
339  "To": "test_suite@notmuchmail.org",
340  "Cc": "",
341  "Bcc": "",
342  "Date": "01 Jan 2000 12:00:00 -0000"},
343  "body": [{"id": 1,
344  "sigstatus": [{"status": "error",
345  "keyid": "6D92612D94E46381",
346  "errors": 8}],
347  "content-type": "multipart/signed",
348  "content": [{"id": 2,
349  "content-type": "text/plain",
350  "content": "This is a test signed message.\n"},
351  {"id": 3,
352  "content-type": "application/pgp-signature"}]}]},
353  []]]]'
354 test_expect_equal \
355     "$output" \
356     "$expected"
357
358 test_done