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