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";
135 foreach (@test_list) {
136 my ( $file, $tlist ) = @$_;
137 print "Example file $file:\n";
138 if ( $tlist =~ /encode/ ) {
139 run_reencode_test( $exdir, $file );
141 if ( $tlist =~ /dataout/ ) {
142 run_dataout_test( $exdir, $file );
144 if ( $tlist =~ /verify/ ) {
145 run_verify_test( $exdir, $tlist, $file );
147 if ( $tlist =~ /digest/ ) {
148 run_digest_test( $exdir, $tlist, $file );
150 if ( $tlist =~ /encrypted/ ) {
151 run_encrypted_test( $exdir, $tlist, $file, $secretkey );
153 if ( $tlist =~ /envelope/ ) {
154 run_envelope_test( $exdir, $tlist, $file );
164 print "\n$badtest TESTS FAILED!!\n";
167 print "\n***All tests successful***\n";
171 my ( $filename, $str, $data ) = @_;
173 $filename =~ s/\.[^.]*$/.pem/;
175 push @cleanup, $filename;
177 open OUT, ">$filename";
179 print OUT "-----BEGIN $str-----\n";
181 print OUT "-----END $str-----\n";
186 sub run_reencode_test {
187 my ( $cmsdir, $tfile ) = @_;
190 system( "$cmscmd -cmsout -inform DER -outform DER"
191 . " -in $cmsdir/$tfile -out tmp.der" );
194 print "\tReencode command FAILED!!\n";
197 elsif ( !cmp_files( "$cmsdir/$tfile", "tmp.der" ) ) {
198 print "\tReencode FAILED!!\n";
202 print "\tReencode passed\n" if $verbose;
206 sub run_dataout_test {
207 my ( $cmsdir, $tfile ) = @_;
211 "$cmscmd -data_out -inform DER" . " -in $cmsdir/$tfile -out tmp.txt" );
214 print "\tDataout command FAILED!!\n";
217 elsif ( !cmp_files( "$cmsdir/ExContent.bin", "tmp.txt" ) ) {
218 print "\tDataout compare FAILED!!\n";
222 print "\tDataout passed\n" if $verbose;
226 sub run_verify_test {
227 my ( $cmsdir, $tlist, $tfile ) = @_;
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/;
236 "$cmscmd -verify -inform $form"
238 . " -in $cmsdir/$tfile -out tmp.txt";
240 $cmd .= " -content $cmsdir/ExContent.bin" if $tlist =~ /cont_extern/;
242 system("$cmd 2>cms.err 1>cms.out");
245 print "\tVerify command FAILED!!\n";
248 elsif ( $tlist =~ /cont/
249 && !cmp_files( "$cmsdir/ExContent.bin", "tmp.txt" ) )
251 print "\tVerify content compare FAILED!!\n";
255 print "\tVerify passed\n" if $verbose;
259 sub run_envelope_test {
260 my ( $cmsdir, $tlist, $tfile ) = @_;
263 $form = "DER" if $tlist =~ /envelopeder/;
264 $form = "SMIME" if $tlist =~ /envelopemime/;
267 "$cmscmd -decrypt -inform $form"
268 . " -recip $cmsdir/BobRSASignByCarl.pem"
269 . " -inkey $cmsdir/BobPrivRSAEncrypt.pem"
270 . " -in $cmsdir/$tfile -out tmp.txt";
272 system("$cmd 2>cms.err 1>cms.out");
275 print "\tDecrypt command FAILED!!\n";
278 elsif ( $tlist =~ /cont/
279 && !cmp_files( "$cmsdir/ExContent.bin", "tmp.txt" ) )
281 print "\tDecrypt content compare FAILED!!\n";
285 print "\tDecrypt passed\n" if $verbose;
289 sub run_digest_test {
290 my ( $cmsdir, $tlist, $tfile ) = @_;
294 "$cmscmd -digest_verify -inform DER" . " -in $cmsdir/$tfile -out tmp.txt";
296 system("$cmd 2>cms.err 1>cms.out");
299 print "\tDigest verify command FAILED!!\n";
302 elsif ( $tlist =~ /cont/
303 && !cmp_files( "$cmsdir/ExContent.bin", "tmp.txt" ) )
305 print "\tDigest verify content compare FAILED!!\n";
309 print "\tDigest verify passed\n" if $verbose;
313 sub run_encrypted_test {
314 my ( $cmsdir, $tlist, $tfile, $key ) = @_;
317 system( "$cmscmd -EncryptedData_decrypt -inform DER"
319 . " -in $cmsdir/$tfile -out tmp.txt" );
322 print "\tEncrypted Data command FAILED!!\n";
325 elsif ( $tlist =~ /cont/
326 && !cmp_files( "$cmsdir/ExContent.bin", "tmp.txt" ) )
328 print "\tEncrypted Data content compare FAILED!!\n";
332 print "\tEncryptedData verify passed\n" if $verbose;
337 my ( $f1, $f2 ) = @_;
342 if ( !open( $fp1, "<$f1" ) ) {
343 print STDERR "Can't Open file $f1\n";
347 if ( !open( $fp2, "<$f2" ) ) {
348 print STDERR "Can't Open file $f2\n";
358 $n1 = sysread $fp1, $rd1, 4096;
359 $n2 = sysread $fp2, $rd2, 4096;
360 last if ( $n1 != $n2 );
361 last if ( $rd1 ne $rd2 );