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"
61 my $cmscmd = "../util/shlib_wrap.sh ../apps/openssl cms";
63 my $exfile = "./rfc4134.txt";
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" ]
87 # Extract examples from RFC4134 text.
88 # Base64 decode all examples, certificates and
89 # private keys are converted to PEM format.
91 my ( $filename, $data );
93 my @cleanup = ( "cms.out", "cms.err", "tmp.der", "tmp.txt" );
97 open( IN, $exfile ) || die "Can't Open RFC examples file $exfile";
108 $filename = "$exdir/$filename";
109 if ( $filename =~ /\.bin$/ || $filename =~ /\.eml$/ ) {
110 $data = decode_base64($data);
111 open OUT, ">$filename";
115 push @cleanup, $filename;
117 elsif ( $filename =~ /\.cer$/ ) {
118 write_pem( $filename, "CERTIFICATE", $data );
120 elsif ( $filename =~ /\.pri$/ ) {
121 write_pem( $filename, "PRIVATE KEY", $data );
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";
136 print STDERR "FATAL ERROR: examples directory missing!!\n";
140 foreach (@test_list) {
141 my ( $file, $tlist ) = @$_;
142 print "Example file $file:\n";
143 if ( $tlist =~ /encode/ ) {
144 run_reencode_test( $exdir, $file );
146 if ( $tlist =~ /dataout/ ) {
147 run_dataout_test( $exdir, $file );
149 if ( $tlist =~ /verify/ ) {
150 run_verify_test( $exdir, $tlist, $file );
152 if ( $tlist =~ /digest/ ) {
153 run_digest_test( $exdir, $tlist, $file );
155 if ( $tlist =~ /encrypted/ ) {
156 run_encrypted_test( $exdir, $tlist, $file, $secretkey );
158 if ( $tlist =~ /envelope/ ) {
159 run_envelope_test( $exdir, $tlist, $file );
169 print "\n$badtest TESTS FAILED!!\n";
172 print "\n***All tests successful***\n";
176 my ( $filename, $str, $data ) = @_;
178 $filename =~ s/\.[^.]*$/.pem/;
180 push @cleanup, $filename;
182 open OUT, ">$filename";
184 print OUT "-----BEGIN $str-----\n";
186 print OUT "-----END $str-----\n";
191 sub run_reencode_test {
192 my ( $cmsdir, $tfile ) = @_;
195 system( "$cmscmd -cmsout -inform DER -outform DER"
196 . " -in $cmsdir/$tfile -out tmp.der" );
199 print "\tReencode command FAILED!!\n";
202 elsif ( !cmp_files( "$cmsdir/$tfile", "tmp.der" ) ) {
203 print "\tReencode FAILED!!\n";
207 print "\tReencode passed\n" if $verbose;
211 sub run_dataout_test {
212 my ( $cmsdir, $tfile ) = @_;
216 "$cmscmd -data_out -inform DER" . " -in $cmsdir/$tfile -out tmp.txt" );
219 print "\tDataout command FAILED!!\n";
222 elsif ( !cmp_files( "$cmsdir/ExContent.bin", "tmp.txt" ) ) {
223 print "\tDataout compare FAILED!!\n";
227 print "\tDataout passed\n" if $verbose;
231 sub run_verify_test {
232 my ( $cmsdir, $tlist, $tfile ) = @_;
235 $form = "DER" if $tlist =~ /verifyder/;
236 $form = "SMIME" if $tlist =~ /verifymime/;
237 $cafile = "$cmsdir/CarlDSSSelf.pem" if $tlist =~ /dss/;
238 $cafile = "$cmsdir/CarlRSASelf.pem" if $tlist =~ /rsa/;
241 "$cmscmd -verify -inform $form"
243 . " -in $cmsdir/$tfile -out tmp.txt";
245 $cmd .= " -content $cmsdir/ExContent.bin" if $tlist =~ /cont_extern/;
247 system("$cmd 2>cms.err 1>cms.out");
250 print "\tVerify command FAILED!!\n";
253 elsif ( $tlist =~ /cont/
254 && !cmp_files( "$cmsdir/ExContent.bin", "tmp.txt" ) )
256 print "\tVerify content compare FAILED!!\n";
260 print "\tVerify passed\n" if $verbose;
264 sub run_envelope_test {
265 my ( $cmsdir, $tlist, $tfile ) = @_;
268 $form = "DER" if $tlist =~ /envelopeder/;
269 $form = "SMIME" if $tlist =~ /envelopemime/;
272 "$cmscmd -decrypt -inform $form"
273 . " -recip $cmsdir/BobRSASignByCarl.pem"
274 . " -inkey $cmsdir/BobPrivRSAEncrypt.pem"
275 . " -in $cmsdir/$tfile -out tmp.txt";
277 system("$cmd 2>cms.err 1>cms.out");
280 print "\tDecrypt command FAILED!!\n";
283 elsif ( $tlist =~ /cont/
284 && !cmp_files( "$cmsdir/ExContent.bin", "tmp.txt" ) )
286 print "\tDecrypt content compare FAILED!!\n";
290 print "\tDecrypt passed\n" if $verbose;
294 sub run_digest_test {
295 my ( $cmsdir, $tlist, $tfile ) = @_;
299 "$cmscmd -digest_verify -inform DER" . " -in $cmsdir/$tfile -out tmp.txt";
301 system("$cmd 2>cms.err 1>cms.out");
304 print "\tDigest verify command FAILED!!\n";
307 elsif ( $tlist =~ /cont/
308 && !cmp_files( "$cmsdir/ExContent.bin", "tmp.txt" ) )
310 print "\tDigest verify content compare FAILED!!\n";
314 print "\tDigest verify passed\n" if $verbose;
318 sub run_encrypted_test {
319 my ( $cmsdir, $tlist, $tfile, $key ) = @_;
322 system( "$cmscmd -EncryptedData_decrypt -inform DER"
324 . " -in $cmsdir/$tfile -out tmp.txt" );
327 print "\tEncrypted Data command FAILED!!\n";
330 elsif ( $tlist =~ /cont/
331 && !cmp_files( "$cmsdir/ExContent.bin", "tmp.txt" ) )
333 print "\tEncrypted Data content compare FAILED!!\n";
337 print "\tEncryptedData verify passed\n" if $verbose;
342 my ( $f1, $f2 ) = @_;
347 if ( !open( $fp1, "<$f1" ) ) {
348 print STDERR "Can't Open file $f1\n";
352 if ( !open( $fp2, "<$f2" ) ) {
353 print STDERR "Can't Open file $f2\n";
363 $n1 = sysread $fp1, $rd1, 4096;
364 $n2 = sysread $fp2, $rd2, 4096;
365 last if ( $n1 != $n2 );
366 last if ( $rd1 ne $rd2 );