]> git.notmuchmail.org Git - notmuch/blob - test/crypto
test: add crypto tests for signature verification and decryption
[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"],
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": "text/plain",
61  "content": "This is a test signed message.\n"}]},
62  []]]]'
63 test_expect_equal \
64     "$output" \
65     "$expected"
66
67 test_begin_subtest "signature verification with full owner trust"
68 # give the key full owner trust
69 echo "${FINGERPRINT}:6:" | gpg --no-tty --import-ownertrust >>"$GNUPGHOME"/trust.log 2>&1
70 gpg --no-tty --check-trustdb >>"$GNUPGHOME"/trust.log 2>&1
71 output=$(notmuch show --format=json --verify subject:"test signed message 001" \
72     | notmuch_json_show_sanitize \
73     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
74 expected='[[[{"id": "XXXXX",
75  "match": true,
76  "filename": "YYYYY",
77  "timestamp": 946728000,
78  "date_relative": "2000-01-01",
79  "tags": ["inbox"],
80  "headers": {"Subject": "test signed message 001",
81  "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
82  "To": "test_suite@notmuchmail.org",
83  "Cc": "",
84  "Bcc": "",
85  "Date": "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": "text/plain",
92  "content": "This is a test signed message.\n"}]},
93  []]]]'
94 test_expect_equal \
95     "$output" \
96     "$expected"
97
98 test_begin_subtest "signature verification with signer key unavailable"
99 # move the gnupghome temporarily out of the way
100 mv "${GNUPGHOME}"{,.bak}
101 output=$(notmuch show --format=json --verify subject:"test signed message 001" \
102     | notmuch_json_show_sanitize \
103     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
104 expected='[[[{"id": "XXXXX",
105  "match": true,
106  "filename": "YYYYY",
107  "timestamp": 946728000,
108  "date_relative": "2000-01-01",
109  "tags": ["inbox"],
110  "headers": {"Subject": "test signed message 001",
111  "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
112  "To": "test_suite@notmuchmail.org",
113  "Cc": "",
114  "Bcc": "",
115  "Date": "01 Jan 2000 12:00:00 -0000"},
116  "body": [{"id": 1,
117  "sigstatus": [{"status": "error",
118  "keyid": "'$(echo $FINGERPRINT | cut -c 25-)'",
119  "errors": 2}],
120  "content-type": "text/plain",
121  "content": "This is a test signed message.\n"}]},
122  []]]]'
123 test_expect_equal \
124     "$output" \
125     "$expected"
126 mv "${GNUPGHOME}"{.bak,}
127
128 # create a test encrypted message with attachment
129 cat <<EOF >TESTATTACHMENT
130 This is a test file.
131 EOF
132 test_expect_success 'emacs delivery of encrypted message with attachment' \
133 'emacs_deliver_message \
134     "test encrypted message 001" \
135     "This is a test encrypted message.\n" \
136     "(mml-attach-file \"TESTATTACHMENT\") (mml-secure-message-encrypt)"'
137
138 test_begin_subtest "decryption, --format=text"
139 output=$(notmuch show --format=text --decrypt subject:"test encrypted message 001" \
140     | notmuch_show_sanitize_all \
141     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
142 expected='\fmessage{ id:XXXXX depth:0 match:1 filename:XXXXX
143 \fheader{
144 Notmuch Test Suite <test_suite@notmuchmail.org> (2000-01-01) (inbox)
145 Subject: test encrypted message 001
146 From: Notmuch Test Suite <test_suite@notmuchmail.org>
147 To: test_suite@notmuchmail.org
148 Date: 01 Jan 2000 12:00:00 -0000
149 \fheader}
150 \fbody{
151 \fpart{ ID: 1, Content-type: multipart/mixed
152 \fpart{ ID: 2, Content-type: text/plain
153 This is a test encrypted message.
154 \fpart}
155 \fattachment{ ID: 3, Content-type: application/octet-stream
156 Attachment: TESTATTACHMENT (application/octet-stream)
157 Non-text part: application/octet-stream
158 \fattachment}
159 \fpart}
160 \fbody}
161 \fmessage}'
162 test_expect_equal \
163     "$output" \
164     "$expected"
165
166 test_begin_subtest "decryption, --format=json"
167 output=$(notmuch show --format=json --decrypt subject:"test encrypted message 001" \
168     | notmuch_json_show_sanitize \
169     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
170 expected='[[[{"id": "XXXXX",
171  "match": true,
172  "filename": "YYYYY",
173  "timestamp": 946728000,
174  "date_relative": "2000-01-01",
175  "tags": ["inbox"],
176  "headers": {"Subject": "test encrypted message 001",
177  "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
178  "To": "test_suite@notmuchmail.org",
179  "Cc": "",
180  "Bcc": "",
181  "Date": "01 Jan 2000 12:00:00 -0000"},
182  "body": [{"id": 1,
183  "encstatus": [{"status": "good"}],
184  "sigstatus": [],
185  "content-type": "multipart/mixed",
186  "content": [{"id": 2,
187  "content-type": "text/plain",
188  "content": "This is a test encrypted message.\n"},
189  {"id": 3,
190  "content-type": "application/octet-stream",
191  "filename": "TESTATTACHMENT"}]}]},
192  []]]]'
193 test_expect_equal \
194     "$output" \
195     "$expected"
196
197 test_begin_subtest "decryption, --format=json, --part=2"
198 output=$(notmuch show --format=json --part=2 --decrypt subject:"test encrypted message 001" \
199     | notmuch_json_show_sanitize \
200     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
201 expected='{"id": 2,
202  "content-type": "text/plain",
203  "content": "This is a test encrypted message.\n"}'
204 test_expect_equal \
205     "$output" \
206     "$expected"
207
208 test_begin_subtest "decrypt attachment (--part=3 --format=raw)"
209 notmuch show \
210     --format=raw \
211     --part=3 \
212     --decrypt \
213     subject:"test encrypted message 001" >OUTPUT
214 test_expect_equal_file OUTPUT TESTATTACHMENT
215
216 test_begin_subtest "decryption failure with missing key"
217 mv "${GNUPGHOME}"{,.bak}
218 output=$(notmuch show --format=json --decrypt subject:"test encrypted message 001" \
219     | notmuch_json_show_sanitize \
220     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
221 expected='[[[{"id": "XXXXX",
222  "match": true,
223  "filename": "YYYYY",
224  "timestamp": 946728000,
225  "date_relative": "2000-01-01",
226  "tags": ["inbox"],
227  "headers": {"Subject": "test encrypted message 001",
228  "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
229  "To": "test_suite@notmuchmail.org",
230  "Cc": "",
231  "Bcc": "",
232  "Date": "01 Jan 2000 12:00:00 -0000"},
233  "body": [{"id": 1,
234  "encstatus": [{"status": "bad"}],
235  "content-type": "multipart/encrypted",
236  "content": [{"id": 2,
237  "content-type": "application/pgp-encrypted"},
238  {"id": 3,
239  "content-type": "application/octet-stream"}]}]},
240  []]]]'
241 test_expect_equal \
242     "$output" \
243     "$expected"
244 mv "${GNUPGHOME}"{.bak,}
245
246 test_expect_success 'emacs delivery of encrypted + signed message' \
247 'emacs_deliver_message \
248     "test encrypted message 002" \
249     "This is another test encrypted message.\n" \
250     "(mml-secure-message-sign-encrypt)"'
251
252 test_begin_subtest "decryption + signature verification"
253 output=$(notmuch show --format=json --decrypt subject:"test encrypted message 002" \
254     | notmuch_json_show_sanitize \
255     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
256 expected='[[[{"id": "XXXXX",
257  "match": true,
258  "filename": "YYYYY",
259  "timestamp": 946728000,
260  "date_relative": "2000-01-01",
261  "tags": ["inbox"],
262  "headers": {"Subject": "test encrypted message 002",
263  "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
264  "To": "test_suite@notmuchmail.org",
265  "Cc": "",
266  "Bcc": "",
267  "Date": "01 Jan 2000 12:00:00 -0000"},
268  "body": [{"id": 1,
269  "encstatus": [{"status": "good"}],
270  "sigstatus": [{"status": "good",
271  "fingerprint": "'$FINGERPRINT'",
272  "created": 946728000,
273  "userid": " Notmuch Test Suite <test_suite@notmuchmail.org> (INSECURE!)"}],
274  "content-type": "text/plain",
275  "content": "This is another test encrypted message.\n"}]},
276  []]]]'
277 test_expect_equal \
278     "$output" \
279     "$expected"
280
281 test_begin_subtest "reply to encrypted message"
282 output=$(notmuch reply --decrypt subject:"test encrypted message 002" \
283     | grep -v -e '^In-Reply-To:' -e '^References:')
284 expected='From: Notmuch Test Suite <test_suite@notmuchmail.org>
285 Subject: Re: test encrypted message 002
286
287 On 01 Jan 2000 12:00:00 -0000, Notmuch Test Suite <test_suite@notmuchmail.org> wrote:
288 > This is another test encrypted message.'
289 test_expect_equal \
290     "$output" \
291     "$expected"
292
293 test_begin_subtest "signature verification with revoked key"
294 # generate revokation certificate and load it to revoke key
295 echo "y
296 1
297 Notmuch Test Suite key revocation (automated) $(date '+%F_%T%z')
298
299 y
300
301 " \
302     | gpg --no-tty --quiet --command-fd 0 --armor --gen-revoke "0x${FINGERPRINT}!" 2>/dev/null \
303     | gpg --no-tty --quiet --import
304 output=$(notmuch show --format=json --verify subject:"test signed message 001" \
305     | notmuch_json_show_sanitize \
306     | sed -e 's|"created": [1234567890]*|"created": 946728000|')
307 expected='[[[{"id": "XXXXX",
308  "match": true,
309  "filename": "YYYYY",
310  "timestamp": 946728000,
311  "date_relative": "2000-01-01",
312  "tags": ["inbox"],
313  "headers": {"Subject": "test signed message 001",
314  "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
315  "To": "test_suite@notmuchmail.org",
316  "Cc": "",
317  "Bcc": "",
318  "Date": "01 Jan 2000 12:00:00 -0000"},
319  "body": [{"id": 1,
320  "sigstatus": [{"status": "error",
321  "keyid": "6D92612D94E46381",
322  "errors": 8}],
323  "content-type": "text/plain",
324  "content": "This is a test signed message.\n"}]},
325  []]]]'
326 test_expect_equal \
327     "$output" \
328     "$expected"
329
330 test_done