2 # Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
5 # ====================================================================
6 # Copyright (c) 2008 The OpenSSL Project. All rights reserved.
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
12 # 1. Redistributions of source code must retain the above copyright
13 # notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
34 # 6. Redistributions of any form whatsoever must retain the following
36 # "This product includes software developed by the OpenSSL Project
37 # for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
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 # ====================================================================
53 # Perl script to run tests against S/MIME examples in RFC4134
54 # Assumes RFC is in current directory and called "rfc4134.txt"
63 my $exfile = "./rfc4134.txt";
65 if (-f "../apps/openssl")
67 $cmscmd = "../util/shlib_wrap.sh ../apps/openssl cms";
69 elsif (-f "..\\out32dll\\openssl.exe")
71 $cmscmd = "..\\out32dll\\openssl.exe cms";
73 elsif (-f "..\\out32\\openssl.exe")
75 $cmscmd = "..\\out32\\openssl.exe cms";
79 [ "3.1.bin" => "dataout" ],
80 [ "3.2.bin" => "encode, dataout" ],
81 [ "4.1.bin" => "encode, verifyder, cont, 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, cont, 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, certsout" ],
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" ]
100 # Extract examples from RFC4134 text.
101 # Base64 decode all examples, certificates and
102 # private keys are converted to PEM format.
104 my ( $filename, $data );
106 my @cleanup = ( "cms.out", "cms.err", "tmp.der", "tmp.txt" );
110 open( IN, $exfile ) || die "Can't Open RFC examples file $exfile";
121 $filename = "$exdir/$filename";
122 if ( $filename =~ /\.bin$/ || $filename =~ /\.eml$/ ) {
123 $data = decode_base64($data);
124 open OUT, ">$filename";
128 push @cleanup, $filename;
130 elsif ( $filename =~ /\.cer$/ ) {
131 write_pem( $filename, "CERTIFICATE", $data );
133 elsif ( $filename =~ /\.pri$/ ) {
134 write_pem( $filename, "PRIVATE KEY", $data );
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";
148 foreach (@test_list) {
149 my ( $file, $tlist ) = @$_;
150 print "Example file $file:\n";
151 if ( $tlist =~ /encode/ ) {
152 run_reencode_test( $exdir, $file );
154 if ( $tlist =~ /certsout/ ) {
155 run_certsout_test( $exdir, $file );
157 if ( $tlist =~ /dataout/ ) {
158 run_dataout_test( $exdir, $file );
160 if ( $tlist =~ /verify/ ) {
161 run_verify_test( $exdir, $tlist, $file );
163 if ( $tlist =~ /digest/ ) {
164 run_digest_test( $exdir, $tlist, $file );
166 if ( $tlist =~ /encrypted/ ) {
167 run_encrypted_test( $exdir, $tlist, $file, $secretkey );
169 if ( $tlist =~ /envelope/ ) {
170 run_envelope_test( $exdir, $tlist, $file );
180 print "\n$badtest TESTS FAILED!!\n";
183 print "\n***All tests successful***\n";
187 my ( $filename, $str, $data ) = @_;
189 $filename =~ s/\.[^.]*$/.pem/;
191 push @cleanup, $filename;
193 open OUT, ">$filename";
195 print OUT "-----BEGIN $str-----\n";
197 print OUT "-----END $str-----\n";
202 sub run_reencode_test {
203 my ( $cmsdir, $tfile ) = @_;
206 system( "$cmscmd -cmsout -inform DER -outform DER"
207 . " -in $cmsdir/$tfile -out tmp.der" );
210 print "\tReencode command FAILED!!\n";
213 elsif ( !cmp_files( "$cmsdir/$tfile", "tmp.der" ) ) {
214 print "\tReencode FAILED!!\n";
218 print "\tReencode passed\n" if $verbose;
222 sub run_certsout_test {
223 my ( $cmsdir, $tfile ) = @_;
227 system( "$cmscmd -cmsout -inform DER -certsout tmp.pem"
228 . " -in $cmsdir/$tfile -out tmp.der" );
231 print "\tCertificate output command FAILED!!\n";
235 print "\tCertificate output passed\n" if $verbose;
239 sub run_dataout_test {
240 my ( $cmsdir, $tfile ) = @_;
244 "$cmscmd -data_out -inform DER" . " -in $cmsdir/$tfile -out tmp.txt" );
247 print "\tDataout command FAILED!!\n";
250 elsif ( !cmp_files( "$cmsdir/ExContent.bin", "tmp.txt" ) ) {
251 print "\tDataout compare FAILED!!\n";
255 print "\tDataout passed\n" if $verbose;
259 sub run_verify_test {
260 my ( $cmsdir, $tlist, $tfile ) = @_;
263 $form = "DER" if $tlist =~ /verifyder/;
264 $form = "SMIME" if $tlist =~ /verifymime/;
265 $cafile = "$cmsdir/CarlDSSSelf.pem" if $tlist =~ /dss/;
266 $cafile = "$cmsdir/CarlRSASelf.pem" if $tlist =~ /rsa/;
269 "$cmscmd -verify -inform $form"
271 . " -in $cmsdir/$tfile -out tmp.txt";
273 $cmd .= " -content $cmsdir/ExContent.bin" if $tlist =~ /cont_extern/;
275 system("$cmd 2>cms.err 1>cms.out");
278 print "\tVerify command FAILED!!\n";
281 elsif ( $tlist =~ /cont/
282 && !cmp_files( "$cmsdir/ExContent.bin", "tmp.txt" ) )
284 print "\tVerify content compare FAILED!!\n";
288 print "\tVerify passed\n" if $verbose;
292 sub run_envelope_test {
293 my ( $cmsdir, $tlist, $tfile ) = @_;
296 $form = "DER" if $tlist =~ /envelopeder/;
297 $form = "SMIME" if $tlist =~ /envelopemime/;
300 "$cmscmd -decrypt -inform $form"
301 . " -recip $cmsdir/BobRSASignByCarl.pem"
302 . " -inkey $cmsdir/BobPrivRSAEncrypt.pem"
303 . " -in $cmsdir/$tfile -out tmp.txt";
305 system("$cmd 2>cms.err 1>cms.out");
308 print "\tDecrypt command FAILED!!\n";
311 elsif ( $tlist =~ /cont/
312 && !cmp_files( "$cmsdir/ExContent.bin", "tmp.txt" ) )
314 print "\tDecrypt content compare FAILED!!\n";
318 print "\tDecrypt passed\n" if $verbose;
322 sub run_digest_test {
323 my ( $cmsdir, $tlist, $tfile ) = @_;
327 "$cmscmd -digest_verify -inform DER" . " -in $cmsdir/$tfile -out tmp.txt";
329 system("$cmd 2>cms.err 1>cms.out");
332 print "\tDigest verify command FAILED!!\n";
335 elsif ( $tlist =~ /cont/
336 && !cmp_files( "$cmsdir/ExContent.bin", "tmp.txt" ) )
338 print "\tDigest verify content compare FAILED!!\n";
342 print "\tDigest verify passed\n" if $verbose;
346 sub run_encrypted_test {
347 my ( $cmsdir, $tlist, $tfile, $key ) = @_;
350 system( "$cmscmd -EncryptedData_decrypt -inform DER"
352 . " -in $cmsdir/$tfile -out tmp.txt" );
355 print "\tEncrypted Data command FAILED!!\n";
358 elsif ( $tlist =~ /cont/
359 && !cmp_files( "$cmsdir/ExContent.bin", "tmp.txt" ) )
361 print "\tEncrypted Data content compare FAILED!!\n";
365 print "\tEncryptedData verify passed\n" if $verbose;
370 my ( $f1, $f2 ) = @_;
375 if ( !open( $fp1, "<$f1" ) ) {
376 print STDERR "Can't Open file $f1\n";
380 if ( !open( $fp2, "<$f2" ) ) {
381 print STDERR "Can't Open file $f2\n";
391 $n1 = sysread $fp1, $rd1, 4096;
392 $n2 = sysread $fp2, $rd2, 4096;
393 last if ( $n1 != $n2 );
394 last if ( $rd1 ne $rd2 );