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