]> git.cworth.org Git - notmuch-old/blob - test/crypto
notmuch-show.el: import calendar data with public function after CR removal
[notmuch-old] / 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  "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  []]]]'
66 test_expect_equal_json \
67     "$output" \
68     "$expected"
69
70 test_begin_subtest "signature verification with full owner trust"
71 # give the key full owner trust
72 echo "${FINGERPRINT}:6:" | gpg --no-tty --import-ownertrust >>"$GNUPGHOME"/trust.log 2>&1
73 gpg --no-tty --check-trustdb >>"$GNUPGHOME"/trust.log 2>&1
74 output=$(notmuch show --format=json --verify subject:"test signed message 001" \
75     | notmuch_json_show_sanitize \
76     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
77 expected='[[[{"id": "XXXXX",
78  "match": true,
79  "excluded": false,
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  "Date": "Sat, 01 Jan 2000 12:00:00 +0000"},
88  "body": [{"id": 1,
89  "sigstatus": [{"status": "good",
90  "fingerprint": "'$FINGERPRINT'",
91  "created": 946728000,
92  "userid": " Notmuch Test Suite <test_suite@notmuchmail.org> (INSECURE!)"}],
93  "content-type": "multipart/signed",
94  "content": [{"id": 2,
95  "content-type": "text/plain",
96  "content": "This is a test signed message.\n"},
97  {"id": 3,
98  "content-type": "application/pgp-signature"}]}]},
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  []]]]'
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_deliver_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  {"id": 3,
201  "content-type": "multipart/mixed",
202  "content": [{"id": 4,
203  "content-type": "text/plain",
204  "content": "This is a test encrypted message.\n"},
205  {"id": 5,
206  "content-type": "application/octet-stream",
207  "filename": "TESTATTACHMENT"}]}]}]},
208  []]]]'
209 test_expect_equal_json \
210     "$output" \
211     "$expected"
212
213 test_begin_subtest "decryption, --format=json, --part=4"
214 output=$(notmuch show --format=json --part=4 --decrypt subject:"test encrypted message 001" \
215     | notmuch_json_show_sanitize \
216     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
217 expected='{"id": 4,
218  "content-type": "text/plain",
219  "content": "This is a test encrypted message.\n"}'
220 test_expect_equal_json \
221     "$output" \
222     "$expected"
223
224 test_begin_subtest "decrypt attachment (--part=5 --format=raw)"
225 notmuch show \
226     --format=raw \
227     --part=5 \
228     --decrypt \
229     subject:"test encrypted message 001" >OUTPUT
230 test_expect_equal_file OUTPUT TESTATTACHMENT
231
232 test_begin_subtest "decryption failure with missing key"
233 mv "${GNUPGHOME}"{,.bak}
234 output=$(notmuch show --format=json --decrypt subject:"test encrypted message 001" \
235     | notmuch_json_show_sanitize \
236     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
237 expected='[[[{"id": "XXXXX",
238  "match": true,
239  "excluded": false,
240  "filename": "YYYYY",
241  "timestamp": 946728000,
242  "date_relative": "2000-01-01",
243  "tags": ["encrypted","inbox"],
244  "headers": {"Subject": "test encrypted message 001",
245  "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
246  "To": "test_suite@notmuchmail.org",
247  "Date": "Sat, 01 Jan 2000 12:00:00 +0000"},
248  "body": [{"id": 1,
249  "encstatus": [{"status": "bad"}],
250  "content-type": "multipart/encrypted",
251  "content": [{"id": 2,
252  "content-type": "application/pgp-encrypted"},
253  {"id": 3,
254  "content-type": "application/octet-stream"}]}]},
255  []]]]'
256 test_expect_equal_json \
257     "$output" \
258     "$expected"
259 mv "${GNUPGHOME}"{.bak,}
260
261 test_expect_success 'emacs delivery of encrypted + signed message' \
262 'emacs_deliver_message \
263     "test encrypted message 002" \
264     "This is another test encrypted message.\n" \
265     "(mml-secure-message-sign-encrypt)"'
266
267 test_begin_subtest "decryption + signature verification"
268 output=$(notmuch show --format=json --decrypt subject:"test encrypted message 002" \
269     | notmuch_json_show_sanitize \
270     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
271 expected='[[[{"id": "XXXXX",
272  "match": true,
273  "excluded": false,
274  "filename": "YYYYY",
275  "timestamp": 946728000,
276  "date_relative": "2000-01-01",
277  "tags": ["encrypted","inbox"],
278  "headers": {"Subject": "test encrypted message 002",
279  "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
280  "To": "test_suite@notmuchmail.org",
281  "Date": "Sat, 01 Jan 2000 12:00:00 +0000"},
282  "body": [{"id": 1,
283  "encstatus": [{"status": "good"}],
284  "sigstatus": [{"status": "good",
285  "fingerprint": "'$FINGERPRINT'",
286  "created": 946728000,
287  "userid": " Notmuch Test Suite <test_suite@notmuchmail.org> (INSECURE!)"}],
288  "content-type": "multipart/encrypted",
289  "content": [{"id": 2,
290  "content-type": "application/pgp-encrypted"},
291  {"id": 3,
292  "content-type": "text/plain",
293  "content": "This is another test encrypted message.\n"}]}]},
294  []]]]'
295 test_expect_equal_json \
296     "$output" \
297     "$expected"
298
299 test_begin_subtest "reply to encrypted message"
300 output=$(notmuch reply --decrypt subject:"test encrypted message 002" \
301     | grep -v -e '^In-Reply-To:' -e '^References:')
302 expected='From: Notmuch Test Suite <test_suite@notmuchmail.org>
303 Subject: Re: test encrypted message 002
304
305 On 01 Jan 2000 12:00:00 -0000, Notmuch Test Suite <test_suite@notmuchmail.org> wrote:
306 > This is another test encrypted message.'
307 test_expect_equal \
308     "$output" \
309     "$expected"
310
311 test_begin_subtest "signature verification with revoked key"
312 # generate revocation certificate and load it to revoke key
313 echo "y
314 1
315 Notmuch Test Suite key revocation (automated) $(date '+%F_%T%z')
316
317 y
318
319 " \
320     | gpg --no-tty --quiet --command-fd 0 --armor --gen-revoke "0x${FINGERPRINT}!" 2>/dev/null \
321     | gpg --no-tty --quiet --import
322 output=$(notmuch show --format=json --verify subject:"test signed message 001" \
323     | notmuch_json_show_sanitize \
324     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
325 expected='[[[{"id": "XXXXX",
326  "match": true,
327  "excluded": false,
328  "filename": "YYYYY",
329  "timestamp": 946728000,
330  "date_relative": "2000-01-01",
331  "tags": ["inbox","signed"],
332  "headers": {"Subject": "test signed message 001",
333  "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
334  "To": "test_suite@notmuchmail.org",
335  "Date": "Sat, 01 Jan 2000 12:00:00 +0000"},
336  "body": [{"id": 1,
337  "sigstatus": [{"status": "error",
338  "keyid": "6D92612D94E46381",
339  "errors": 8}],
340  "content-type": "multipart/signed",
341  "content": [{"id": 2,
342  "content-type": "text/plain",
343  "content": "This is a test signed message.\n"},
344  {"id": 3,
345  "content-type": "application/pgp-signature"}]}]},
346  []]]]'
347 test_expect_equal_json \
348     "$output" \
349     "$expected"
350
351 test_done