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