]> git.notmuchmail.org Git - notmuch/blob - test/T355-smime.sh
smime: Identify encrypted S/MIME parts during indexing
[notmuch] / test / T355-smime.sh
1 #!/usr/bin/env bash
2
3 test_description='S/MIME signature verification and decryption'
4 . $(dirname "$0")/test-lib.sh || exit 1
5
6 test_require_external_prereq openssl
7 test_require_external_prereq gpgsm
8
9 FINGERPRINT=$(openssl x509 -fingerprint -in "$NOTMUCH_SRCDIR/test/smime/key+cert.pem" -noout | sed -e 's/^.*=//' -e s/://g)
10
11 add_gpgsm_home
12
13 test_begin_subtest "emacs delivery of S/MIME signed message"
14 test_expect_success \
15      'emacs_fcc_message \
16      "test signed message 001" \
17      "This is a test signed message." \
18      "(mml-secure-message-sign \"smime\")"'
19
20 test_begin_subtest "emacs delivery of S/MIME encrypted + signed message"
21 # Hard code the MML to avoid several interactive questions
22 test_expect_success \
23 'emacs_fcc_message \
24     "test encrypted message 001" \
25     "<#secure method=smime mode=signencrypt>\nThis is a test encrypted message.\n"'
26
27 test_begin_subtest "Signature verification (openssl)"
28 notmuch show --format=raw subject:"test signed message 001" |\
29     openssl smime -verify -CAfile $NOTMUCH_SRCDIR/test/smime/test.crt 2>OUTPUT
30 cat <<EOF > EXPECTED
31 Verification successful
32 EOF
33 test_expect_equal_file EXPECTED OUTPUT
34
35 test_begin_subtest "signature verification (notmuch CLI)"
36 output=$(notmuch show --format=json --verify subject:"test signed message 001" \
37     | notmuch_json_show_sanitize \
38     | sed -e 's|"created": [-1234567890]*|"created": 946728000|g' \
39           -e 's|"expires": [-1234567890]*|"expires": 424242424|g' )
40 expected='[[[{"id": "XXXXX",
41  "match": true,
42  "excluded": false,
43  "filename": ["YYYYY"],
44  "timestamp": 946728000,
45  "date_relative": "2000-01-01",
46  "tags": ["inbox","signed"],
47  "crypto": {"signed": {"status": [{"fingerprint": "'$FINGERPRINT'", "status": "good","userid": "CN=Notmuch Test Suite","expires": 424242424, "created": 946728000}]}},
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": [{"fingerprint": "'$FINGERPRINT'",
54  "status": "good",
55  "userid": "CN=Notmuch Test Suite",
56  "expires": 424242424,
57  "created": 946728000}],
58  "content-type": "multipart/signed",
59  "content": [{"id": 2,
60  "content-type": "text/plain",
61  "content": "This is a test signed message.\n"},
62  {"id": 3,
63   "content-disposition": "attachment",
64   "content-length": "NONZERO",
65   "content-transfer-encoding": "base64",
66   "content-type": "application/pkcs7-signature",
67   "filename": "smime.p7s"}]}]},
68  []]]]'
69 test_expect_equal_json \
70     "$output" \
71     "$expected"
72
73 test_begin_subtest "Decryption and signature verification (openssl)"
74 notmuch show --format=raw subject:"test encrypted message 001" |\
75     openssl smime -decrypt -recip $NOTMUCH_SRCDIR/test/smime/key+cert.pem |\
76     openssl smime -verify -CAfile $NOTMUCH_SRCDIR/test/smime/test.crt 2>OUTPUT
77 cat <<EOF > EXPECTED
78 Verification successful
79 EOF
80 test_expect_equal_file EXPECTED OUTPUT
81
82 test_begin_subtest "Decryption (notmuch CLI)"
83 test_subtest_known_broken
84 notmuch show --decrypt=true subject:"test encrypted message 001" |\
85     grep "^This is a" > OUTPUT
86 cat <<EOF > EXPECTED
87 This is a test encrypted message.
88 EOF
89 test_expect_equal_file EXPECTED OUTPUT
90
91 test_begin_subtest "Cryptographic message status (encrypted+signed)"
92 test_subtest_known_broken
93 output=$(notmuch show --format=json --decrypt=true subject:"test encrypted message 001")
94 test_json_nodes <<<"$output" \
95                 'crypto_encrypted:[0][0][0]["crypto"]["decrypted"]["status"]="full"' \
96                 'crypto_sigok:[0][0][0]["crypto"]["signed"]["status"][0]["status"]="good"' \
97                 'crypto_fpr:[0][0][0]["crypto"]["signed"]["status"][0]["fingerprint"]="616F46CD73834C63847756AF0DFB64A6E0972A47"' \
98                 'crypto_uid:[0][0][0]["crypto"]["signed"]["status"][0]["userid"]="CN=Notmuch Test Suite"'
99
100 test_begin_subtest "encrypted+signed message is known to be encrypted, but signature is unknown"
101 output=$(notmuch search subject:"test encrypted message 001")
102 test_expect_equal "$output" "thread:0000000000000002   2000-01-01 [1/1] Notmuch Test Suite; test encrypted message 001 (encrypted inbox)"
103
104 test_begin_subtest "Encrypted body is not indexed"
105 output=$(notmuch search 'this is a test encrypted message')
106 test_expect_equal "$output" ""
107
108 test_begin_subtest "Reindex cleartext"
109 test_expect_success "notmuch reindex --decrypt=true subject:'test encrypted message 001'"
110
111 test_begin_subtest "signature is now known"
112 test_subtest_known_broken
113 output=$(notmuch search subject:"test encrypted message 001")
114 test_expect_equal "$output" "thread:0000000000000002   2000-01-01 [1/1] Notmuch Test Suite; test encrypted message 001 (encrypted inbox signed)"
115
116 test_begin_subtest "Encrypted body is indexed"
117 test_subtest_known_broken
118 output=$(notmuch search 'this is a test encrypted message')
119 test_expect_equal "$output" "thread:0000000000000002   2000-01-01 [1/1] Notmuch Test Suite; test encrypted message 001 (encrypted inbox signed)"
120
121 add_email_corpus pkcs7
122
123 test_begin_subtest "index PKCS#7 SignedData message"
124 output=$(notmuch search --output=messages Thanks)
125 expected=id:smime-onepart-signed@protected-headers.example
126 test_expect_equal "$expected" "$output"
127
128 test_begin_subtest "do not index embedded certificates from PKCS#7 SignedData"
129 output=$(notmuch search --output=messages 'LAMPS Certificate')
130 expected=''
131 test_expect_equal "$expected" "$output"
132
133 test_begin_subtest "know the MIME type of the embedded part in PKCS#7 SignedData"
134 output=$(notmuch search --output=messages 'mimetype:text/plain')
135 expected=id:smime-onepart-signed@protected-headers.example
136 test_expect_equal "$expected" "$output"
137
138 test_begin_subtest "PKCS#7 SignedData message is tagged 'signed'"
139 output=$(notmuch dump id:smime-onepart-signed@protected-headers.example)
140 expected='#notmuch-dump batch-tag:3 config,properties,tags
141 +inbox +signed +unread -- id:smime-onepart-signed@protected-headers.example'
142 test_expect_equal "$expected" "$output"
143
144 test_begin_subtest "show contents of PKCS#7 SignedData message"
145 test_subtest_known_broken
146 output=$(notmuch show --format=raw --part=2 id:smime-onepart-signed@protected-headers.example)
147 whitespace=' '
148 expected="Bob, we need to cancel this contract.
149
150 Please start the necessary processes to make that happen today.
151
152 Thanks, Alice
153 --${whitespace}
154 Alice Lovelace
155 President
156 OpenPGP Example Corp"
157 test_expect_equal "$expected" "$output"
158
159 test_begin_subtest "reply to PKCS#7 SignedData message with proper quoting and attribution"
160 test_subtest_known_broken
161 output=$(notmuch reply id:smime-onepart-signed@protected-headers.example)
162 expected="From: Notmuch Test Suite <test_suite@notmuchmail.org>
163 Subject: Re: The FooCorp contract
164 To: Alice Lovelace <alice@smime.example>, Bob Babbage <bob@smime.example>
165 In-Reply-To: <smime-onepart-signed@protected-headers.example>
166 References: <smime-onepart-signed@protected-headers.example>
167
168 On Tue, 26 Nov 2019 20:11:29 -0400, Alice Lovelace <alice@smime.example> wrote:
169 > Bob, we need to cancel this contract.
170 >${whitespace}
171 > Please start the necessary processes to make that happen today.
172 >${whitespace}
173 > Thanks, Alice
174 > --${whitespace}
175 > Alice Lovelace
176 > President
177 > OpenPGP Example Corp"
178 test_expect_equal "$expected" "$output"
179
180 test_begin_subtest "show PKCS#7 SignedData outputs valid JSON"
181 output=$(notmuch show --format=json id:smime-onepart-signed@protected-headers.example)
182 test_valid_json "$output"
183
184 test_begin_subtest "Verify signature on PKCS#7 SignedData message"
185 test_subtest_known_broken
186 output=$(notmuch show --format=json id:smime-onepart-signed@protected-headers.example)
187
188 test_json_nodes <<<"$output" \
189                 'created:[0][0][0]["crypto"]["signed"]["status"][0]["created"]=1574813489' \
190                 'expires:[0][0][0]["crypto"]["signed"]["status"][0]["expires"]=2611032858' \
191                 'fingerprint:[0][0][0]["crypto"]["signed"]["status"][0]["fingerprint"]="702BA4B157F1E2B7D16B0C6A5FFC8A7DE2057DEB"' \
192                 'status:[0][0][0]["crypto"]["signed"]["status"][0]["status"]="good"'
193
194 test_begin_subtest "Verify signature on PKCS#7 SignedData message signer User ID"
195 test_subtest_known_broken
196 test_json_nodes <<<"$output" \
197                 'userid:[0][0][0]["crypto"]["signed"]["status"][0]["userid"]="CN=Alice Lovelace"'
198
199 test_done