Fixes for S/MIME consistency checker and flexibility enhancements.
[openssl.git] / test / cms-test.pl
1 # test/cms-test.pl
2 # Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 # project.
4 #
5 # ====================================================================
6 # Copyright (c) 2008 The OpenSSL Project.  All rights reserved.
7 #
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
10 # are met:
11 #
12 # 1. Redistributions of source code must retain the above copyright
13 #    notice, this list of conditions and the following disclaimer.
14 #
15 # 2. Redistributions in binary form must reproduce the above copyright
16 #    notice, this list of conditions and the following disclaimer in
17 #    the documentation and/or other materials provided with the
18 #    distribution.
19 #
20 # 3. All advertising materials mentioning features or use of this
21 #    software must display the following acknowledgment:
22 #    "This product includes software developed by the OpenSSL Project
23 #    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 #
25 # 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 #    endorse or promote products derived from this software without
27 #    prior written permission. For written permission, please contact
28 #    licensing@OpenSSL.org.
29 #
30 # 5. Products derived from this software may not be called "OpenSSL"
31 #    nor may "OpenSSL" appear in their names without prior written
32 #    permission of the OpenSSL Project.
33 #
34 # 6. Redistributions of any form whatsoever must retain the following
35 #    acknowledgment:
36 #    "This product includes software developed by the OpenSSL Project
37 #    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 #
39 # THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 # EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 # ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 # OF THE POSSIBILITY OF SUCH DAMAGE.
51 # ====================================================================
52
53 # CMS, PKCS7 consistency test script. Run extensive tests on
54 # OpenSSL PKCS#7 and CMS implementations.
55
56 my $ossl_path;
57
58 if ( -f "../apps/openssl" ) {
59     $ossl_path = "../util/shlib_wrap.sh ../apps/openssl";
60 }
61 elsif ( -f "..\\out32dll\\openssl.exe" ) {
62     $ossl_path = "..\\out32dll\\openssl.exe";
63 }
64 elsif ( -f "..\\out32\\openssl.exe" ) {
65     $ossl_path = "..\\out32\\openssl.exe";
66 }
67 else {
68     die "Can't find OpenSSL executable";
69 }
70
71 my $pk7cmd   = "$ossl_path smime ";
72 my $cmscmd   = "$ossl_path cms ";
73 my $smdir    = "smime-certs";
74 my $halt_err = 1;
75
76 my $badcmd = 0;
77
78 my @smime_pkcs7_tests = (
79
80     [
81         "signed content DER format, RSA key",
82         "-sign -in smcont.txt -outform DER -nodetach"
83           . " -signer $smdir/smrsa1.pem -out test.cms",
84         "-verify -in test.cms -inform DER "
85           . " -CAfile $smdir/smroot.pem -out smtst.txt"
86     ],
87
88     [
89         "signed detached content DER format, RSA key",
90         "-sign -in smcont.txt -outform DER"
91           . " -signer $smdir/smrsa1.pem -out test.cms",
92         "-verify -in test.cms -inform DER "
93           . " -CAfile $smdir/smroot.pem -out smtst.txt -content smcont.txt"
94     ],
95
96     [
97         "signed content test streaming BER format, RSA",
98         "-sign -in smcont.txt -outform DER -nodetach"
99           . " -stream -signer $smdir/smrsa1.pem -out test.cms",
100         "-verify -in test.cms -inform DER "
101           . " -CAfile $smdir/smroot.pem -out smtst.txt"
102     ],
103
104     [
105         "signed content DER format, DSA key",
106         "-sign -in smcont.txt -outform DER -nodetach"
107           . " -signer $smdir/smdsa1.pem -out test.cms",
108         "-verify -in test.cms -inform DER "
109           . " -CAfile $smdir/smroot.pem -out smtst.txt"
110     ],
111
112     [
113         "signed detached content DER format, DSA key",
114         "-sign -in smcont.txt -outform DER"
115           . " -signer $smdir/smdsa1.pem -out test.cms",
116         "-verify -in test.cms -inform DER "
117           . " -CAfile $smdir/smroot.pem -out smtst.txt -content smcont.txt"
118     ],
119
120     [
121         "signed detached content DER format, add RSA signer",
122         "-resign -inform DER -in test.cms -outform DER"
123           . " -signer $smdir/smrsa1.pem -out test2.cms",
124         "-verify -in test2.cms -inform DER "
125           . " -CAfile $smdir/smroot.pem -out smtst.txt -content smcont.txt"
126     ],
127
128     [
129         "signed content test streaming BER format, DSA key",
130         "-sign -in smcont.txt -outform DER -nodetach"
131           . " -stream -signer $smdir/smdsa1.pem -out test.cms",
132         "-verify -in test.cms -inform DER "
133           . " -CAfile $smdir/smroot.pem -out smtst.txt"
134     ],
135
136     [
137         "signed content test streaming BER format, 2 DSA and 2 RSA keys",
138         "-sign -in smcont.txt -outform DER -nodetach"
139           . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
140           . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
141           . " -stream -out test.cms",
142         "-verify -in test.cms -inform DER "
143           . " -CAfile $smdir/smroot.pem -out smtst.txt"
144     ],
145
146     [
147 "signed content test streaming BER format, 2 DSA and 2 RSA keys, no attributes",
148         "-sign -in smcont.txt -outform DER -noattr -nodetach"
149           . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
150           . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
151           . " -stream -out test.cms",
152         "-verify -in test.cms -inform DER "
153           . " -CAfile $smdir/smroot.pem -out smtst.txt"
154     ],
155
156     [
157         "signed content test streaming S/MIME format, 2 DSA and 2 RSA keys",
158         "-sign -in smcont.txt -nodetach"
159           . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
160           . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
161           . " -stream -out test.cms",
162         "-verify -in test.cms " . " -CAfile $smdir/smroot.pem -out smtst.txt"
163     ],
164
165     [
166 "signed content test streaming multipart S/MIME format, 2 DSA and 2 RSA keys",
167         "-sign -in smcont.txt"
168           . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
169           . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
170           . " -stream -out test.cms",
171         "-verify -in test.cms " . " -CAfile $smdir/smroot.pem -out smtst.txt"
172     ],
173
174     [
175         "enveloped content test streaming S/MIME format, 3 recipients",
176         "-encrypt -in smcont.txt"
177           . " -stream -out test.cms"
178           . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
179         "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
180     ],
181
182     [
183 "enveloped content test streaming S/MIME format, 3 recipients, 3rd used",
184         "-encrypt -in smcont.txt"
185           . " -stream -out test.cms"
186           . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
187         "-decrypt -recip $smdir/smrsa3.pem -in test.cms -out smtst.txt"
188     ],
189
190     [
191 "enveloped content test streaming S/MIME format, 3 recipients, key only used",
192         "-encrypt -in smcont.txt"
193           . " -stream -out test.cms"
194           . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
195         "-decrypt -inkey $smdir/smrsa3.pem -in test.cms -out smtst.txt"
196     ],
197
198     [
199 "enveloped content test streaming S/MIME format, AES-256 cipher, 3 recipients",
200         "-encrypt -in smcont.txt"
201           . " -aes256 -stream -out test.cms"
202           . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
203         "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
204     ],
205
206 );
207
208 my @smime_cms_tests = (
209
210     [
211         "signed content test streaming BER format, 2 DSA and 2 RSA keys, keyid",
212         "-sign -in smcont.txt -outform DER -nodetach -keyid"
213           . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
214           . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
215           . " -stream -out test.cms",
216         "-verify -in test.cms -inform DER "
217           . " -CAfile $smdir/smroot.pem -out smtst.txt"
218     ],
219
220     [
221         "signed content test streaming PEM format, 2 DSA and 2 RSA keys",
222         "-sign -in smcont.txt -outform PEM -nodetach"
223           . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
224           . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
225           . " -stream -out test.cms",
226         "-verify -in test.cms -inform PEM "
227           . " -CAfile $smdir/smroot.pem -out smtst.txt"
228     ],
229
230     [
231         "data content test streaming PEM format",
232         "-data_create -in smcont.txt -outform PEM -nodetach"
233           . " -stream -out test.cms",
234         "-data_out -in test.cms -inform PEM -out smtst.txt"
235     ],
236
237     [
238         "encrypted content test streaming PEM format, 128 bit RC2 key",
239         "-EncryptedData_encrypt -in smcont.txt -outform PEM"
240           . " -rc2 -secretkey 000102030405060708090A0B0C0D0E0F"
241           . " -stream -out test.cms",
242         "-EncryptedData_decrypt -in test.cms -inform PEM "
243           . " -secretkey 000102030405060708090A0B0C0D0E0F -out smtst.txt"
244     ],
245
246     [
247         "encrypted content test streaming PEM format, 40 bit RC2 key",
248         "-EncryptedData_encrypt -in smcont.txt -outform PEM"
249           . " -rc2 -secretkey 0001020304"
250           . " -stream -out test.cms",
251         "-EncryptedData_decrypt -in test.cms -inform PEM "
252           . " -secretkey 0001020304 -out smtst.txt"
253     ],
254
255     [
256         "encrypted content test streaming PEM format, triple DES key",
257         "-EncryptedData_encrypt -in smcont.txt -outform PEM"
258           . " -des3 -secretkey 000102030405060708090A0B0C0D0E0F1011121314151617"
259           . " -stream -out test.cms",
260         "-EncryptedData_decrypt -in test.cms -inform PEM "
261           . " -secretkey 000102030405060708090A0B0C0D0E0F1011121314151617"
262           . " -out smtst.txt"
263     ],
264
265     [
266         "encrypted content test streaming PEM format, 128 bit AES key",
267         "-EncryptedData_encrypt -in smcont.txt -outform PEM"
268           . " -aes128 -secretkey 000102030405060708090A0B0C0D0E0F"
269           . " -stream -out test.cms",
270         "-EncryptedData_decrypt -in test.cms -inform PEM "
271           . " -secretkey 000102030405060708090A0B0C0D0E0F -out smtst.txt"
272     ],
273
274 );
275
276 my @smime_cms_comp_tests = (
277
278     [
279         "compressed content test streaming PEM format",
280         "-compress -in smcont.txt -outform PEM -nodetach"
281           . " -stream -out test.cms",
282         "-uncompress -in test.cms -inform PEM -out smtst.txt"
283     ]
284
285 );
286
287 print "CMS => PKCS#7 compatibility tests\n";
288
289 run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $cmscmd, $pk7cmd );
290
291 print "CMS <= PKCS#7 compatibility tests\n";
292
293 run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $pk7cmd, $cmscmd );
294
295 print "CMS <=> CMS consistency tests\n";
296
297 run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $cmscmd, $cmscmd );
298 run_smime_tests( \$badcmd, \@smime_cms_tests,   $cmscmd, $cmscmd );
299
300 if ( `$ossl_path version -f` =~ /ZLIB/ ) {
301     run_smime_tests( \$badcmd, \@smime_cms_comp_tests, $cmscmd, $cmscmd );
302 }
303 else {
304     print "Zlib not supported: compression tests skipped\n";
305 }
306
307 if ($badcmd) {
308     print "$badcmd TESTS FAILED!!\n";
309 }
310 else {
311     print "ALL TESTS SUCCESSFUL.\n";
312 }
313
314 sub run_smime_tests {
315     my ( $rv, $aref, $scmd, $vcmd ) = @_;
316
317     foreach $smtst (@$aref) {
318         my ( $tnam, $rscmd, $rvcmd ) = @$smtst;
319         system( $scmd . $rscmd );
320         if ($?) {
321             print "$tnam: generation error\n";
322             $$rv++;
323             exit 1 if $halt_err;
324             next;
325         }
326         system( $vcmd . $rvcmd );
327         if ($?) {
328             print "$tnam: verify error\n";
329             $$rv++;
330             exit 1 if $halt_err;
331             next;
332         }
333         print "$tnam: OK\n";
334     }
335 }
336