util/pl/VC-32.pl: bufferoverflowu.lib only when actually needed and
[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 "../apps/openssl.exe" ) {
62     $ossl_path = "../util/shlib_wrap.sh ../apps/openssl.exe";
63 }
64 elsif ( -f "..\\out32dll\\openssl.exe" ) {
65     $ossl_path = "..\\out32dll\\openssl.exe";
66 }
67 elsif ( -f "..\\out32\\openssl.exe" ) {
68     $ossl_path = "..\\out32\\openssl.exe";
69 }
70 else {
71     die "Can't find OpenSSL executable";
72 }
73
74 my $pk7cmd   = "$ossl_path smime ";
75 my $cmscmd   = "$ossl_path cms ";
76 my $smdir    = "smime-certs";
77 my $halt_err = 1;
78
79 my $badcmd = 0;
80 my $ossl8 = `$ossl_path version -v` =~ /0\.9\.8/;
81
82 my @smime_pkcs7_tests = (
83
84     [
85         "signed content DER format, RSA key",
86         "-sign -in smcont.txt -outform DER -nodetach"
87           . " -certfile $smdir/smroot.pem"
88           . " -signer $smdir/smrsa1.pem -out test.cms",
89         "-verify -in test.cms -inform DER "
90           . " -CAfile $smdir/smroot.pem -out smtst.txt"
91     ],
92
93     [
94         "signed detached content DER format, RSA key",
95         "-sign -in smcont.txt -outform DER"
96           . " -signer $smdir/smrsa1.pem -out test.cms",
97         "-verify -in test.cms -inform DER "
98           . " -CAfile $smdir/smroot.pem -out smtst.txt -content smcont.txt"
99     ],
100
101     [
102         "signed content test streaming BER format, RSA",
103         "-sign -in smcont.txt -outform DER -nodetach"
104           . " -stream -signer $smdir/smrsa1.pem -out test.cms",
105         "-verify -in test.cms -inform DER "
106           . " -CAfile $smdir/smroot.pem -out smtst.txt"
107     ],
108
109     [
110         "signed content DER format, DSA key",
111         "-sign -in smcont.txt -outform DER -nodetach"
112           . " -signer $smdir/smdsa1.pem -out test.cms",
113         "-verify -in test.cms -inform DER "
114           . " -CAfile $smdir/smroot.pem -out smtst.txt"
115     ],
116
117     [
118         "signed detached content DER format, DSA key",
119         "-sign -in smcont.txt -outform DER"
120           . " -signer $smdir/smdsa1.pem -out test.cms",
121         "-verify -in test.cms -inform DER "
122           . " -CAfile $smdir/smroot.pem -out smtst.txt -content smcont.txt"
123     ],
124
125     [
126         "signed detached content DER format, add RSA signer",
127         "-resign -inform DER -in test.cms -outform DER"
128           . " -signer $smdir/smrsa1.pem -out test2.cms",
129         "-verify -in test2.cms -inform DER "
130           . " -CAfile $smdir/smroot.pem -out smtst.txt -content smcont.txt"
131     ],
132
133     [
134         "signed content test streaming BER format, DSA key",
135         "-sign -in smcont.txt -outform DER -nodetach"
136           . " -stream -signer $smdir/smdsa1.pem -out test.cms",
137         "-verify -in test.cms -inform DER "
138           . " -CAfile $smdir/smroot.pem -out smtst.txt"
139     ],
140
141     [
142         "signed content test streaming BER format, 2 DSA and 2 RSA keys",
143         "-sign -in smcont.txt -outform DER -nodetach"
144           . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
145           . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
146           . " -stream -out test.cms",
147         "-verify -in test.cms -inform DER "
148           . " -CAfile $smdir/smroot.pem -out smtst.txt"
149     ],
150
151     [
152 "signed content test streaming BER format, 2 DSA and 2 RSA keys, no attributes",
153         "-sign -in smcont.txt -outform DER -noattr -nodetach"
154           . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
155           . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
156           . " -stream -out test.cms",
157         "-verify -in test.cms -inform DER "
158           . " -CAfile $smdir/smroot.pem -out smtst.txt"
159     ],
160
161     [
162         "signed content test streaming S/MIME format, 2 DSA and 2 RSA keys",
163         "-sign -in smcont.txt -nodetach"
164           . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
165           . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
166           . " -stream -out test.cms",
167         "-verify -in test.cms " . " -CAfile $smdir/smroot.pem -out smtst.txt"
168     ],
169
170     [
171 "signed content test streaming multipart S/MIME format, 2 DSA and 2 RSA keys",
172         "-sign -in smcont.txt"
173           . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
174           . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
175           . " -stream -out test.cms",
176         "-verify -in test.cms " . " -CAfile $smdir/smroot.pem -out smtst.txt"
177     ],
178
179     [
180         "enveloped content test streaming S/MIME format, 3 recipients",
181         "-encrypt -in smcont.txt"
182           . " -stream -out test.cms"
183           . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
184         "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
185     ],
186
187     [
188 "enveloped content test streaming S/MIME format, 3 recipients, 3rd used",
189         "-encrypt -in smcont.txt"
190           . " -stream -out test.cms"
191           . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
192         "-decrypt -recip $smdir/smrsa3.pem -in test.cms -out smtst.txt"
193     ],
194
195     [
196 "enveloped content test streaming S/MIME format, 3 recipients, key only used",
197         "-encrypt -in smcont.txt"
198           . " -stream -out test.cms"
199           . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
200         "-decrypt -inkey $smdir/smrsa3.pem -in test.cms -out smtst.txt"
201     ],
202
203     [
204 "enveloped content test streaming S/MIME format, AES-256 cipher, 3 recipients",
205         "-encrypt -in smcont.txt"
206           . " -aes256 -stream -out test.cms"
207           . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
208         "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
209     ],
210
211 );
212
213 my @smime_cms_tests = (
214
215     [
216         "signed content test streaming BER format, 2 DSA and 2 RSA keys, keyid",
217         "-sign -in smcont.txt -outform DER -nodetach -keyid"
218           . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
219           . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
220           . " -stream -out test.cms",
221         "-verify -in test.cms -inform DER "
222           . " -CAfile $smdir/smroot.pem -out smtst.txt"
223     ],
224
225     [
226         "signed content test streaming PEM format, 2 DSA and 2 RSA keys",
227         "-sign -in smcont.txt -outform PEM -nodetach"
228           . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
229           . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
230           . " -stream -out test.cms",
231         "-verify -in test.cms -inform PEM "
232           . " -CAfile $smdir/smroot.pem -out smtst.txt"
233     ],
234
235     [
236         "signed content MIME format, RSA key, signed receipt request",
237         "-sign -in smcont.txt -signer $smdir/smrsa1.pem -nodetach"
238           . " -receipt_request_to test\@openssl.org -receipt_request_all"
239           . " -out test.cms",
240         "-verify -in test.cms "
241           . " -CAfile $smdir/smroot.pem -out smtst.txt"
242     ],
243
244     [
245         "signed receipt MIME format, RSA key",
246         "-sign_receipt -in test.cms"
247           . " -signer $smdir/smrsa2.pem"
248           . " -out test2.cms",
249         "-verify_receipt test2.cms -in test.cms"
250           . " -CAfile $smdir/smroot.pem"
251     ],
252
253     [
254         "enveloped content test streaming S/MIME format, 3 recipients, keyid",
255         "-encrypt -in smcont.txt"
256           . " -stream -out test.cms -keyid"
257           . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
258         "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
259     ],
260
261     [
262         "enveloped content test streaming PEM format, KEK",
263         "-encrypt -in smcont.txt -outform PEM -aes128"
264           . " -stream -out test.cms "
265           . " -secretkey 000102030405060708090A0B0C0D0E0F "
266           . " -secretkeyid C0FEE0",
267         "-decrypt -in test.cms -out smtst.txt -inform PEM"
268           . " -secretkey 000102030405060708090A0B0C0D0E0F "
269           . " -secretkeyid C0FEE0"
270     ],
271
272     [
273         "enveloped content test streaming PEM format, KEK, key only",
274         "-encrypt -in smcont.txt -outform PEM -aes128"
275           . " -stream -out test.cms "
276           . " -secretkey 000102030405060708090A0B0C0D0E0F "
277           . " -secretkeyid C0FEE0",
278         "-decrypt -in test.cms -out smtst.txt -inform PEM"
279           . " -secretkey 000102030405060708090A0B0C0D0E0F "
280     ],
281
282     [
283         "data content test streaming PEM format",
284         "-data_create -in smcont.txt -outform PEM -nodetach"
285           . " -stream -out test.cms",
286         "-data_out -in test.cms -inform PEM -out smtst.txt"
287     ],
288
289     [
290         "encrypted content test streaming PEM format, 128 bit RC2 key",
291         "-EncryptedData_encrypt -in smcont.txt -outform PEM"
292           . " -rc2 -secretkey 000102030405060708090A0B0C0D0E0F"
293           . " -stream -out test.cms",
294         "-EncryptedData_decrypt -in test.cms -inform PEM "
295           . " -secretkey 000102030405060708090A0B0C0D0E0F -out smtst.txt"
296     ],
297
298     [
299         "encrypted content test streaming PEM format, 40 bit RC2 key",
300         "-EncryptedData_encrypt -in smcont.txt -outform PEM"
301           . " -rc2 -secretkey 0001020304"
302           . " -stream -out test.cms",
303         "-EncryptedData_decrypt -in test.cms -inform PEM "
304           . " -secretkey 0001020304 -out smtst.txt"
305     ],
306
307     [
308         "encrypted content test streaming PEM format, triple DES key",
309         "-EncryptedData_encrypt -in smcont.txt -outform PEM"
310           . " -des3 -secretkey 000102030405060708090A0B0C0D0E0F1011121314151617"
311           . " -stream -out test.cms",
312         "-EncryptedData_decrypt -in test.cms -inform PEM "
313           . " -secretkey 000102030405060708090A0B0C0D0E0F1011121314151617"
314           . " -out smtst.txt"
315     ],
316
317     [
318         "encrypted content test streaming PEM format, 128 bit AES key",
319         "-EncryptedData_encrypt -in smcont.txt -outform PEM"
320           . " -aes128 -secretkey 000102030405060708090A0B0C0D0E0F"
321           . " -stream -out test.cms",
322         "-EncryptedData_decrypt -in test.cms -inform PEM "
323           . " -secretkey 000102030405060708090A0B0C0D0E0F -out smtst.txt"
324     ],
325
326 );
327
328 my @smime_cms_comp_tests = (
329
330     [
331         "compressed content test streaming PEM format",
332         "-compress -in smcont.txt -outform PEM -nodetach"
333           . " -stream -out test.cms",
334         "-uncompress -in test.cms -inform PEM -out smtst.txt"
335     ]
336
337 );
338
339 print "CMS => PKCS#7 compatibility tests\n";
340
341 run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $cmscmd, $pk7cmd );
342
343 print "CMS <= PKCS#7 compatibility tests\n";
344
345 run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $pk7cmd, $cmscmd );
346
347 print "CMS <=> CMS consistency tests\n";
348
349 run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $cmscmd, $cmscmd );
350 run_smime_tests( \$badcmd, \@smime_cms_tests,   $cmscmd, $cmscmd );
351
352 if ( `$ossl_path version -f` =~ /ZLIB/ ) {
353     run_smime_tests( \$badcmd, \@smime_cms_comp_tests, $cmscmd, $cmscmd );
354 }
355 else {
356     print "Zlib not supported: compression tests skipped\n";
357 }
358
359 print "Running modified tests for OpenSSL 0.9.8 cms backport\n" if($ossl8);
360
361 if ($badcmd) {
362     print "$badcmd TESTS FAILED!!\n";
363 }
364 else {
365     print "ALL TESTS SUCCESSFUL.\n";
366 }
367
368 unlink "test.cms";
369 unlink "test2.cms";
370 unlink "smtst.txt";
371 unlink "cms.out";
372 unlink "cms.err";
373
374 sub run_smime_tests {
375     my ( $rv, $aref, $scmd, $vcmd ) = @_;
376
377     foreach $smtst (@$aref) {
378         my ( $tnam, $rscmd, $rvcmd ) = @$smtst;
379         if ($ossl8)
380                 {
381                 # Skip smime resign: 0.9.8 smime doesn't support -resign        
382                 next if ($scmd =~ /smime/ && $rscmd =~ /-resign/);
383                 # Disable streaming: option not supported in 0.9.8
384                 $tnam =~ s/streaming//; 
385                 $rscmd =~ s/-stream//;  
386                 $rvcmd =~ s/-stream//;
387                 }
388         system("$scmd$rscmd 2>cms.err 1>cms.out");
389         if ($?) {
390             print "$tnam: generation error\n";
391             $$rv++;
392             exit 1 if $halt_err;
393             next;
394         }
395         system("$vcmd$rvcmd 2>cms.err 1>cms.out");
396         if ($?) {
397             print "$tnam: verify error\n";
398             $$rv++;
399             exit 1 if $halt_err;
400             next;
401         }
402         if (!cmp_files("smtst.txt", "smcont.txt")) {
403             print "$tnam: content verify error\n";
404             $$rv++;
405             exit 1 if $halt_err;
406             next;
407         }
408         print "$tnam: OK\n";
409     }
410 }
411
412 sub cmp_files {
413     my ( $f1, $f2 ) = @_;
414     my ( $fp1, $fp2 );
415
416     my ( $rd1, $rd2 );
417
418     if ( !open( $fp1, "<$f1" ) ) {
419         print STDERR "Can't Open file $f1\n";
420         return 0;
421     }
422
423     if ( !open( $fp2, "<$f2" ) ) {
424         print STDERR "Can't Open file $f2\n";
425         return 0;
426     }
427
428     binmode $fp1;
429     binmode $fp2;
430
431     my $ret = 0;
432
433     for ( ; ; ) {
434         $n1 = sysread $fp1, $rd1, 4096;
435         $n2 = sysread $fp2, $rd2, 4096;
436         last if ( $n1 != $n2 );
437         last if ( $rd1 ne $rd2 );
438
439         if ( $n1 == 0 ) {
440             $ret = 1;
441             last;
442         }
443
444     }
445
446     close $fp1;
447     close $fp2;
448
449     return $ret;
450
451 }
452