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