Don't need to check for examples directory any more.
[openssl.git] / test / runex.pl
1 # test/runex.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 # Perl script to run tests against S/MIME examples in RFC4134
54 # Assumes RFC is in current directory and called "rfc4134.txt"
55
56 use MIME::Base64;
57
58 my $badttest = 0;
59 my $verbose  = 1;
60
61 my $cmscmd = "../util/shlib_wrap.sh ../apps/openssl cms";
62 my $exdir  = "./";
63 my $exfile = "./rfc4134.txt";
64
65 my @test_list = (
66     [ "3.1.bin"  => "dataout" ],
67     [ "3.2.bin"  => "encode, dataout" ],
68     [ "4.1.bin"  => "encode, verifyder, content, dss" ],
69     [ "4.2.bin"  => "encode, verifyder, cont, rsa" ],
70     [ "4.3.bin"  => "encode, verifyder, cont_extern, dss" ],
71     [ "4.4.bin"  => "encode, verifyder, cont, dss" ],
72     [ "4.5.bin"  => "verifyder, content, rsa" ],
73     [ "4.6.bin"  => "encode, verifyder, cont, dss" ],
74     [ "4.7.bin"  => "encode, verifyder, cont, dss" ],
75     [ "4.8.eml"  => "verifymime, dss" ],
76     [ "4.9.eml"  => "verifymime, dss" ],
77     [ "4.10.bin" => "encode, verifyder, cont, dss" ],
78     [ "4.11.bin" => "encode" ],
79     [ "5.1.bin"  => "encode, envelopeder, cont" ],
80     [ "5.2.bin"  => "encode, envelopeder, cont" ],
81     [ "5.3.eml"  => "envelopemime, cont" ],
82     [ "6.0.bin"  => "encode, digest, cont" ],
83     [ "7.1.bin"  => "encode, encrypted, cont" ],
84     [ "7.2.bin"  => "encode, encrypted, cont" ]
85 );
86
87 # Extract examples from RFC4134 text.
88 # Base64 decode all examples, certificates and
89 # private keys are converted to PEM format.
90
91 my ( $filename, $data );
92
93 my @cleanup = ( "cms.out", "cms.err", "tmp.der", "tmp.txt" );
94
95 $data = "";
96
97 open( IN, $exfile ) || die "Can't Open RFC examples file $exfile";
98
99 while (<IN>) {
100     next unless (/^\|/);
101     s/^\|//;
102     next if (/^\*/);
103     if (/^>(.*)$/) {
104         $filename = $1;
105         next;
106     }
107     if (/^</) {
108         $filename = "$exdir/$filename";
109         if ( $filename =~ /\.bin$/ || $filename =~ /\.eml$/ ) {
110             $data = decode_base64($data);
111             open OUT, ">$filename";
112             binmode OUT;
113             print OUT $data;
114             close OUT;
115             push @cleanup, $filename;
116         }
117         elsif ( $filename =~ /\.cer$/ ) {
118             write_pem( $filename, "CERTIFICATE", $data );
119         }
120         elsif ( $filename =~ /\.pri$/ ) {
121             write_pem( $filename, "PRIVATE KEY", $data );
122         }
123         $data     = "";
124         $filename = "";
125     }
126     else {
127         $data .= $_;
128     }
129
130 }
131
132 my $secretkey =
133   "73:7c:79:1f:25:ea:d0:e0:46:29:25:43:52:f7:dc:62:91:e5:cb:26:91:7a:da:32";
134
135 foreach (@test_list) {
136     my ( $file, $tlist ) = @$_;
137     print "Example file $file:\n";
138     if ( $tlist =~ /encode/ ) {
139         run_reencode_test( $exdir, $file );
140     }
141     if ( $tlist =~ /dataout/ ) {
142         run_dataout_test( $exdir, $file );
143     }
144     if ( $tlist =~ /verify/ ) {
145         run_verify_test( $exdir, $tlist, $file );
146     }
147     if ( $tlist =~ /digest/ ) {
148         run_digest_test( $exdir, $tlist, $file );
149     }
150     if ( $tlist =~ /encrypted/ ) {
151         run_encrypted_test( $exdir, $tlist, $file, $secretkey );
152     }
153     if ( $tlist =~ /envelope/ ) {
154         run_envelope_test( $exdir, $tlist, $file );
155     }
156
157 }
158
159 foreach (@cleanup) {
160     unlink $_;
161 }
162
163 if ($badtest) {
164     print "\n$badtest TESTS FAILED!!\n";
165 }
166 else {
167     print "\n***All tests successful***\n";
168 }
169
170 sub write_pem {
171     my ( $filename, $str, $data ) = @_;
172
173     $filename =~ s/\.[^.]*$/.pem/;
174
175     push @cleanup, $filename;
176
177     open OUT, ">$filename";
178
179     print OUT "-----BEGIN $str-----\n";
180     print OUT $data;
181     print OUT "-----END $str-----\n";
182
183     close OUT;
184 }
185
186 sub run_reencode_test {
187     my ( $cmsdir, $tfile ) = @_;
188     unlink "tmp.der";
189
190     system( "$cmscmd -cmsout -inform DER -outform DER"
191           . " -in $cmsdir/$tfile -out tmp.der" );
192
193     if ($?) {
194         print "\tReencode command FAILED!!\n";
195         $badtest++;
196     }
197     elsif ( !cmp_files( "$cmsdir/$tfile", "tmp.der" ) ) {
198         print "\tReencode FAILED!!\n";
199         $badtest++;
200     }
201     else {
202         print "\tReencode passed\n" if $verbose;
203     }
204 }
205
206 sub run_dataout_test {
207     my ( $cmsdir, $tfile ) = @_;
208     unlink "tmp.txt";
209
210     system(
211         "$cmscmd -data_out -inform DER" . " -in $cmsdir/$tfile -out tmp.txt" );
212
213     if ($?) {
214         print "\tDataout command FAILED!!\n";
215         $badtest++;
216     }
217     elsif ( !cmp_files( "$cmsdir/ExContent.bin", "tmp.txt" ) ) {
218         print "\tDataout compare FAILED!!\n";
219         $badtest++;
220     }
221     else {
222         print "\tDataout passed\n" if $verbose;
223     }
224 }
225
226 sub run_verify_test {
227     my ( $cmsdir, $tlist, $tfile ) = @_;
228     unlink "tmp.txt";
229
230     $form   = "DER"                     if $tlist =~ /verifyder/;
231     $form   = "SMIME"                   if $tlist =~ /verifymime/;
232     $cafile = "$cmsdir/CarlDSSSelf.pem" if $tlist =~ /dss/;
233     $cafile = "$cmsdir/CarlRSASelf.pem" if $tlist =~ /rsa/;
234
235     $cmd =
236         "$cmscmd -verify -inform $form"
237       . " -CAfile $cafile"
238       . " -in $cmsdir/$tfile -out tmp.txt";
239
240     $cmd .= " -content $cmsdir/ExContent.bin" if $tlist =~ /cont_extern/;
241
242     system("$cmd 2>cms.err 1>cms.out");
243
244     if ($?) {
245         print "\tVerify command FAILED!!\n";
246         $badtest++;
247     }
248     elsif ( $tlist =~ /cont/
249         && !cmp_files( "$cmsdir/ExContent.bin", "tmp.txt" ) )
250     {
251         print "\tVerify content compare FAILED!!\n";
252         $badtest++;
253     }
254     else {
255         print "\tVerify passed\n" if $verbose;
256     }
257 }
258
259 sub run_envelope_test {
260     my ( $cmsdir, $tlist, $tfile ) = @_;
261     unlink "tmp.txt";
262
263     $form = "DER"   if $tlist =~ /envelopeder/;
264     $form = "SMIME" if $tlist =~ /envelopemime/;
265
266     $cmd =
267         "$cmscmd -decrypt -inform $form"
268       . " -recip $cmsdir/BobRSASignByCarl.pem"
269       . " -inkey $cmsdir/BobPrivRSAEncrypt.pem"
270       . " -in $cmsdir/$tfile -out tmp.txt";
271
272     system("$cmd 2>cms.err 1>cms.out");
273
274     if ($?) {
275         print "\tDecrypt command FAILED!!\n";
276         $badtest++;
277     }
278     elsif ( $tlist =~ /cont/
279         && !cmp_files( "$cmsdir/ExContent.bin", "tmp.txt" ) )
280     {
281         print "\tDecrypt content compare FAILED!!\n";
282         $badtest++;
283     }
284     else {
285         print "\tDecrypt passed\n" if $verbose;
286     }
287 }
288
289 sub run_digest_test {
290     my ( $cmsdir, $tlist, $tfile ) = @_;
291     unlink "tmp.txt";
292
293     my $cmd =
294       "$cmscmd -digest_verify -inform DER" . " -in $cmsdir/$tfile -out tmp.txt";
295
296     system("$cmd 2>cms.err 1>cms.out");
297
298     if ($?) {
299         print "\tDigest verify command FAILED!!\n";
300         $badtest++;
301     }
302     elsif ( $tlist =~ /cont/
303         && !cmp_files( "$cmsdir/ExContent.bin", "tmp.txt" ) )
304     {
305         print "\tDigest verify content compare FAILED!!\n";
306         $badtest++;
307     }
308     else {
309         print "\tDigest verify passed\n" if $verbose;
310     }
311 }
312
313 sub run_encrypted_test {
314     my ( $cmsdir, $tlist, $tfile, $key ) = @_;
315     unlink "tmp.txt";
316
317     system( "$cmscmd -EncryptedData_decrypt -inform DER"
318           . " -secretkey $key"
319           . " -in $cmsdir/$tfile -out tmp.txt" );
320
321     if ($?) {
322         print "\tEncrypted Data command FAILED!!\n";
323         $badtest++;
324     }
325     elsif ( $tlist =~ /cont/
326         && !cmp_files( "$cmsdir/ExContent.bin", "tmp.txt" ) )
327     {
328         print "\tEncrypted Data content compare FAILED!!\n";
329         $badtest++;
330     }
331     else {
332         print "\tEncryptedData verify passed\n" if $verbose;
333     }
334 }
335
336 sub cmp_files {
337     my ( $f1, $f2 ) = @_;
338     my ( $fp1, $fp2 );
339
340     my ( $rd1, $rd2 );
341
342     if ( !open( $fp1, "<$f1" ) ) {
343         print STDERR "Can't Open file $f1\n";
344         return 0;
345     }
346
347     if ( !open( $fp2, "<$f2" ) ) {
348         print STDERR "Can't Open file $f2\n";
349         return 0;
350     }
351
352     binmode $fp1;
353     binmode $fp2;
354
355     my $ret = 0;
356
357     for ( ; ; ) {
358         $n1 = sysread $fp1, $rd1, 4096;
359         $n2 = sysread $fp2, $rd2, 4096;
360         last if ( $n1 != $n2 );
361         last if ( $rd1 ne $rd2 );
362
363         if ( $n1 == 0 ) {
364             $ret = 1;
365             last;
366         }
367
368     }
369
370     close $fp1;
371     close $fp2;
372
373     return $ret;
374
375 }
376