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