]> git.notmuchmail.org Git - notmuch/blob - test/crypto
Do not replace multipart/signed part with content part when doing verification.
[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 <../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/mixed
164 \fpart{ ID: 2, Content-type: text/plain
165 This is a test encrypted message.
166 \fpart}
167 \fattachment{ ID: 3, Content-type: application/octet-stream
168 Attachment: TESTATTACHMENT (application/octet-stream)
169 Non-text part: application/octet-stream
170 \fattachment}
171 \fpart}
172 \fbody}
173 \fmessage}'
174 test_expect_equal \
175     "$output" \
176     "$expected"
177
178 test_begin_subtest "decryption, --format=json"
179 output=$(notmuch show --format=json --decrypt subject:"test encrypted message 001" \
180     | notmuch_json_show_sanitize \
181     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
182 expected='[[[{"id": "XXXXX",
183  "match": true,
184  "filename": "YYYYY",
185  "timestamp": 946728000,
186  "date_relative": "2000-01-01",
187  "tags": ["encrypted","inbox"],
188  "headers": {"Subject": "test encrypted message 001",
189  "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
190  "To": "test_suite@notmuchmail.org",
191  "Cc": "",
192  "Bcc": "",
193  "Date": "01 Jan 2000 12:00:00 -0000"},
194  "body": [{"id": 1,
195  "encstatus": [{"status": "good"}],
196  "sigstatus": [],
197  "content-type": "multipart/mixed",
198  "content": [{"id": 2,
199  "content-type": "text/plain",
200  "content": "This is a test encrypted message.\n"},
201  {"id": 3,
202  "content-type": "application/octet-stream",
203  "filename": "TESTATTACHMENT"}]}]},
204  []]]]'
205 test_expect_equal \
206     "$output" \
207     "$expected"
208
209 test_begin_subtest "decryption, --format=json, --part=2"
210 output=$(notmuch show --format=json --part=2 --decrypt subject:"test encrypted message 001" \
211     | notmuch_json_show_sanitize \
212     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
213 expected='{"id": 2,
214  "content-type": "text/plain",
215  "content": "This is a test encrypted message.\n"}'
216 test_expect_equal \
217     "$output" \
218     "$expected"
219
220 test_begin_subtest "decrypt attachment (--part=3 --format=raw)"
221 notmuch show \
222     --format=raw \
223     --part=3 \
224     --decrypt \
225     subject:"test encrypted message 001" >OUTPUT
226 test_expect_equal_file OUTPUT TESTATTACHMENT
227
228 test_begin_subtest "decryption failure with missing key"
229 mv "${GNUPGHOME}"{,.bak}
230 output=$(notmuch show --format=json --decrypt subject:"test encrypted message 001" \
231     | notmuch_json_show_sanitize \
232     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
233 expected='[[[{"id": "XXXXX",
234  "match": true,
235  "filename": "YYYYY",
236  "timestamp": 946728000,
237  "date_relative": "2000-01-01",
238  "tags": ["encrypted","inbox"],
239  "headers": {"Subject": "test encrypted message 001",
240  "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
241  "To": "test_suite@notmuchmail.org",
242  "Cc": "",
243  "Bcc": "",
244  "Date": "01 Jan 2000 12:00:00 -0000"},
245  "body": [{"id": 1,
246  "encstatus": [{"status": "bad"}],
247  "content-type": "multipart/encrypted",
248  "content": [{"id": 2,
249  "content-type": "application/pgp-encrypted"},
250  {"id": 3,
251  "content-type": "application/octet-stream"}]}]},
252  []]]]'
253 test_expect_equal \
254     "$output" \
255     "$expected"
256 mv "${GNUPGHOME}"{.bak,}
257
258 test_expect_success 'emacs delivery of encrypted + signed message' \
259 'emacs_deliver_message \
260     "test encrypted message 002" \
261     "This is another test encrypted message.\n" \
262     "(mml-secure-message-sign-encrypt)"'
263
264 test_begin_subtest "decryption + signature verification"
265 output=$(notmuch show --format=json --decrypt subject:"test encrypted message 002" \
266     | notmuch_json_show_sanitize \
267     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
268 expected='[[[{"id": "XXXXX",
269  "match": true,
270  "filename": "YYYYY",
271  "timestamp": 946728000,
272  "date_relative": "2000-01-01",
273  "tags": ["encrypted","inbox"],
274  "headers": {"Subject": "test encrypted message 002",
275  "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
276  "To": "test_suite@notmuchmail.org",
277  "Cc": "",
278  "Bcc": "",
279  "Date": "01 Jan 2000 12:00:00 -0000"},
280  "body": [{"id": 1,
281  "encstatus": [{"status": "good"}],
282  "sigstatus": [{"status": "good",
283  "fingerprint": "'$FINGERPRINT'",
284  "created": 946728000,
285  "userid": " Notmuch Test Suite <test_suite@notmuchmail.org> (INSECURE!)"}],
286  "content-type": "text/plain",
287  "content": "This is another test encrypted message.\n"}]},
288  []]]]'
289 test_expect_equal \
290     "$output" \
291     "$expected"
292
293 test_begin_subtest "reply to encrypted message"
294 output=$(notmuch reply --decrypt subject:"test encrypted message 002" \
295     | grep -v -e '^In-Reply-To:' -e '^References:')
296 expected='From: Notmuch Test Suite <test_suite@notmuchmail.org>
297 Subject: Re: test encrypted message 002
298
299 On 01 Jan 2000 12:00:00 -0000, Notmuch Test Suite <test_suite@notmuchmail.org> wrote:
300 > This is another test encrypted message.'
301 test_expect_equal \
302     "$output" \
303     "$expected"
304
305 test_begin_subtest "signature verification with revoked key"
306 # generate revokation certificate and load it to revoke key
307 echo "y
308 1
309 Notmuch Test Suite key revocation (automated) $(date '+%F_%T%z')
310
311 y
312
313 " \
314     | gpg --no-tty --quiet --command-fd 0 --armor --gen-revoke "0x${FINGERPRINT}!" 2>/dev/null \
315     | gpg --no-tty --quiet --import
316 output=$(notmuch show --format=json --verify subject:"test signed message 001" \
317     | notmuch_json_show_sanitize \
318     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
319 expected='[[[{"id": "XXXXX",
320  "match": true,
321  "filename": "YYYYY",
322  "timestamp": 946728000,
323  "date_relative": "2000-01-01",
324  "tags": ["inbox","signed"],
325  "headers": {"Subject": "test signed message 001",
326  "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
327  "To": "test_suite@notmuchmail.org",
328  "Cc": "",
329  "Bcc": "",
330  "Date": "01 Jan 2000 12:00:00 -0000"},
331  "body": [{"id": 1,
332  "sigstatus": [{"status": "error",
333  "keyid": "6D92612D94E46381",
334  "errors": 8}],
335  "content-type": "multipart/signed",
336  "content": [{"id": 2,
337  "content-type": "text/plain",
338  "content": "This is a test signed message.\n"},
339  {"id": 3,
340  "content-type": "application/pgp-signature"}]}]},
341  []]]]'
342 test_expect_equal \
343     "$output" \
344     "$expected"
345
346 test_done