xref: /minix3/crypto/external/bsd/openssl/dist/test/cms-examples.pl (revision ebfedea0ce5bbe81e252ddf32d732e40fb633fae)
1*ebfedea0SLionel Sambuc# test/cms-examples.pl
2*ebfedea0SLionel Sambuc# Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3*ebfedea0SLionel Sambuc# project.
4*ebfedea0SLionel Sambuc#
5*ebfedea0SLionel Sambuc# ====================================================================
6*ebfedea0SLionel Sambuc# Copyright (c) 2008 The OpenSSL Project.  All rights reserved.
7*ebfedea0SLionel Sambuc#
8*ebfedea0SLionel Sambuc# Redistribution and use in source and binary forms, with or without
9*ebfedea0SLionel Sambuc# modification, are permitted provided that the following conditions
10*ebfedea0SLionel Sambuc# are met:
11*ebfedea0SLionel Sambuc#
12*ebfedea0SLionel Sambuc# 1. Redistributions of source code must retain the above copyright
13*ebfedea0SLionel Sambuc#    notice, this list of conditions and the following disclaimer.
14*ebfedea0SLionel Sambuc#
15*ebfedea0SLionel Sambuc# 2. Redistributions in binary form must reproduce the above copyright
16*ebfedea0SLionel Sambuc#    notice, this list of conditions and the following disclaimer in
17*ebfedea0SLionel Sambuc#    the documentation and/or other materials provided with the
18*ebfedea0SLionel Sambuc#    distribution.
19*ebfedea0SLionel Sambuc#
20*ebfedea0SLionel Sambuc# 3. All advertising materials mentioning features or use of this
21*ebfedea0SLionel Sambuc#    software must display the following acknowledgment:
22*ebfedea0SLionel Sambuc#    "This product includes software developed by the OpenSSL Project
23*ebfedea0SLionel Sambuc#    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24*ebfedea0SLionel Sambuc#
25*ebfedea0SLionel Sambuc# 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26*ebfedea0SLionel Sambuc#    endorse or promote products derived from this software without
27*ebfedea0SLionel Sambuc#    prior written permission. For written permission, please contact
28*ebfedea0SLionel Sambuc#    licensing@OpenSSL.org.
29*ebfedea0SLionel Sambuc#
30*ebfedea0SLionel Sambuc# 5. Products derived from this software may not be called "OpenSSL"
31*ebfedea0SLionel Sambuc#    nor may "OpenSSL" appear in their names without prior written
32*ebfedea0SLionel Sambuc#    permission of the OpenSSL Project.
33*ebfedea0SLionel Sambuc#
34*ebfedea0SLionel Sambuc# 6. Redistributions of any form whatsoever must retain the following
35*ebfedea0SLionel Sambuc#    acknowledgment:
36*ebfedea0SLionel Sambuc#    "This product includes software developed by the OpenSSL Project
37*ebfedea0SLionel Sambuc#    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38*ebfedea0SLionel Sambuc#
39*ebfedea0SLionel Sambuc# THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40*ebfedea0SLionel Sambuc# EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41*ebfedea0SLionel Sambuc# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42*ebfedea0SLionel Sambuc# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43*ebfedea0SLionel Sambuc# ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44*ebfedea0SLionel Sambuc# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45*ebfedea0SLionel Sambuc# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46*ebfedea0SLionel Sambuc# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47*ebfedea0SLionel Sambuc# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48*ebfedea0SLionel Sambuc# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49*ebfedea0SLionel Sambuc# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50*ebfedea0SLionel Sambuc# OF THE POSSIBILITY OF SUCH DAMAGE.
51*ebfedea0SLionel Sambuc# ====================================================================
52*ebfedea0SLionel Sambuc
53*ebfedea0SLionel Sambuc# Perl script to run tests against S/MIME examples in RFC4134
54*ebfedea0SLionel Sambuc# Assumes RFC is in current directory and called "rfc4134.txt"
55*ebfedea0SLionel Sambuc
56*ebfedea0SLionel Sambucuse MIME::Base64;
57*ebfedea0SLionel Sambuc
58*ebfedea0SLionel Sambucmy $badttest = 0;
59*ebfedea0SLionel Sambucmy $verbose  = 1;
60*ebfedea0SLionel Sambuc
61*ebfedea0SLionel Sambucmy $cmscmd;
62*ebfedea0SLionel Sambucmy $exdir  = "./";
63*ebfedea0SLionel Sambucmy $exfile = "./rfc4134.txt";
64*ebfedea0SLionel Sambuc
65*ebfedea0SLionel Sambucif (-f "../apps/openssl")
66*ebfedea0SLionel Sambuc	{
67*ebfedea0SLionel Sambuc	$cmscmd = "../util/shlib_wrap.sh ../apps/openssl cms";
68*ebfedea0SLionel Sambuc	}
69*ebfedea0SLionel Sambucelsif (-f "..\\out32dll\\openssl.exe")
70*ebfedea0SLionel Sambuc	{
71*ebfedea0SLionel Sambuc	$cmscmd = "..\\out32dll\\openssl.exe cms";
72*ebfedea0SLionel Sambuc	}
73*ebfedea0SLionel Sambucelsif (-f "..\\out32\\openssl.exe")
74*ebfedea0SLionel Sambuc	{
75*ebfedea0SLionel Sambuc	$cmscmd = "..\\out32\\openssl.exe cms";
76*ebfedea0SLionel Sambuc	}
77*ebfedea0SLionel Sambuc
78*ebfedea0SLionel Sambucmy @test_list = (
79*ebfedea0SLionel Sambuc    [ "3.1.bin"  => "dataout" ],
80*ebfedea0SLionel Sambuc    [ "3.2.bin"  => "encode, dataout" ],
81*ebfedea0SLionel Sambuc    [ "4.1.bin"  => "encode, verifyder, cont, dss" ],
82*ebfedea0SLionel Sambuc    [ "4.2.bin"  => "encode, verifyder, cont, rsa" ],
83*ebfedea0SLionel Sambuc    [ "4.3.bin"  => "encode, verifyder, cont_extern, dss" ],
84*ebfedea0SLionel Sambuc    [ "4.4.bin"  => "encode, verifyder, cont, dss" ],
85*ebfedea0SLionel Sambuc    [ "4.5.bin"  => "verifyder, cont, rsa" ],
86*ebfedea0SLionel Sambuc    [ "4.6.bin"  => "encode, verifyder, cont, dss" ],
87*ebfedea0SLionel Sambuc    [ "4.7.bin"  => "encode, verifyder, cont, dss" ],
88*ebfedea0SLionel Sambuc    [ "4.8.eml"  => "verifymime, dss" ],
89*ebfedea0SLionel Sambuc    [ "4.9.eml"  => "verifymime, dss" ],
90*ebfedea0SLionel Sambuc    [ "4.10.bin" => "encode, verifyder, cont, dss" ],
91*ebfedea0SLionel Sambuc    [ "4.11.bin" => "encode, certsout" ],
92*ebfedea0SLionel Sambuc    [ "5.1.bin"  => "encode, envelopeder, cont" ],
93*ebfedea0SLionel Sambuc    [ "5.2.bin"  => "encode, envelopeder, cont" ],
94*ebfedea0SLionel Sambuc    [ "5.3.eml"  => "envelopemime, cont" ],
95*ebfedea0SLionel Sambuc    [ "6.0.bin"  => "encode, digest, cont" ],
96*ebfedea0SLionel Sambuc    [ "7.1.bin"  => "encode, encrypted, cont" ],
97*ebfedea0SLionel Sambuc    [ "7.2.bin"  => "encode, encrypted, cont" ]
98*ebfedea0SLionel Sambuc);
99*ebfedea0SLionel Sambuc
100*ebfedea0SLionel Sambuc# Extract examples from RFC4134 text.
101*ebfedea0SLionel Sambuc# Base64 decode all examples, certificates and
102*ebfedea0SLionel Sambuc# private keys are converted to PEM format.
103*ebfedea0SLionel Sambuc
104*ebfedea0SLionel Sambucmy ( $filename, $data );
105*ebfedea0SLionel Sambuc
106*ebfedea0SLionel Sambucmy @cleanup = ( "cms.out", "cms.err", "tmp.der", "tmp.txt" );
107*ebfedea0SLionel Sambuc
108*ebfedea0SLionel Sambuc$data = "";
109*ebfedea0SLionel Sambuc
110*ebfedea0SLionel Sambucopen( IN, $exfile ) || die "Can't Open RFC examples file $exfile";
111*ebfedea0SLionel Sambuc
112*ebfedea0SLionel Sambucwhile (<IN>) {
113*ebfedea0SLionel Sambuc    next unless (/^\|/);
114*ebfedea0SLionel Sambuc    s/^\|//;
115*ebfedea0SLionel Sambuc    next if (/^\*/);
116*ebfedea0SLionel Sambuc    if (/^>(.*)$/) {
117*ebfedea0SLionel Sambuc        $filename = $1;
118*ebfedea0SLionel Sambuc        next;
119*ebfedea0SLionel Sambuc    }
120*ebfedea0SLionel Sambuc    if (/^</) {
121*ebfedea0SLionel Sambuc        $filename = "$exdir/$filename";
122*ebfedea0SLionel Sambuc        if ( $filename =~ /\.bin$/ || $filename =~ /\.eml$/ ) {
123*ebfedea0SLionel Sambuc            $data = decode_base64($data);
124*ebfedea0SLionel Sambuc            open OUT, ">$filename";
125*ebfedea0SLionel Sambuc            binmode OUT;
126*ebfedea0SLionel Sambuc            print OUT $data;
127*ebfedea0SLionel Sambuc            close OUT;
128*ebfedea0SLionel Sambuc            push @cleanup, $filename;
129*ebfedea0SLionel Sambuc        }
130*ebfedea0SLionel Sambuc        elsif ( $filename =~ /\.cer$/ ) {
131*ebfedea0SLionel Sambuc            write_pem( $filename, "CERTIFICATE", $data );
132*ebfedea0SLionel Sambuc        }
133*ebfedea0SLionel Sambuc        elsif ( $filename =~ /\.pri$/ ) {
134*ebfedea0SLionel Sambuc            write_pem( $filename, "PRIVATE KEY", $data );
135*ebfedea0SLionel Sambuc        }
136*ebfedea0SLionel Sambuc        $data     = "";
137*ebfedea0SLionel Sambuc        $filename = "";
138*ebfedea0SLionel Sambuc    }
139*ebfedea0SLionel Sambuc    else {
140*ebfedea0SLionel Sambuc        $data .= $_;
141*ebfedea0SLionel Sambuc    }
142*ebfedea0SLionel Sambuc
143*ebfedea0SLionel Sambuc}
144*ebfedea0SLionel Sambuc
145*ebfedea0SLionel Sambucmy $secretkey =
146*ebfedea0SLionel Sambuc  "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*ebfedea0SLionel Sambuc
148*ebfedea0SLionel Sambucforeach (@test_list) {
149*ebfedea0SLionel Sambuc    my ( $file, $tlist ) = @$_;
150*ebfedea0SLionel Sambuc    print "Example file $file:\n";
151*ebfedea0SLionel Sambuc    if ( $tlist =~ /encode/ ) {
152*ebfedea0SLionel Sambuc        run_reencode_test( $exdir, $file );
153*ebfedea0SLionel Sambuc    }
154*ebfedea0SLionel Sambuc    if ( $tlist =~ /certsout/ ) {
155*ebfedea0SLionel Sambuc        run_certsout_test( $exdir, $file );
156*ebfedea0SLionel Sambuc    }
157*ebfedea0SLionel Sambuc    if ( $tlist =~ /dataout/ ) {
158*ebfedea0SLionel Sambuc        run_dataout_test( $exdir, $file );
159*ebfedea0SLionel Sambuc    }
160*ebfedea0SLionel Sambuc    if ( $tlist =~ /verify/ ) {
161*ebfedea0SLionel Sambuc        run_verify_test( $exdir, $tlist, $file );
162*ebfedea0SLionel Sambuc    }
163*ebfedea0SLionel Sambuc    if ( $tlist =~ /digest/ ) {
164*ebfedea0SLionel Sambuc        run_digest_test( $exdir, $tlist, $file );
165*ebfedea0SLionel Sambuc    }
166*ebfedea0SLionel Sambuc    if ( $tlist =~ /encrypted/ ) {
167*ebfedea0SLionel Sambuc        run_encrypted_test( $exdir, $tlist, $file, $secretkey );
168*ebfedea0SLionel Sambuc    }
169*ebfedea0SLionel Sambuc    if ( $tlist =~ /envelope/ ) {
170*ebfedea0SLionel Sambuc        run_envelope_test( $exdir, $tlist, $file );
171*ebfedea0SLionel Sambuc    }
172*ebfedea0SLionel Sambuc
173*ebfedea0SLionel Sambuc}
174*ebfedea0SLionel Sambuc
175*ebfedea0SLionel Sambucforeach (@cleanup) {
176*ebfedea0SLionel Sambuc    unlink $_;
177*ebfedea0SLionel Sambuc}
178*ebfedea0SLionel Sambuc
179*ebfedea0SLionel Sambucif ($badtest) {
180*ebfedea0SLionel Sambuc    print "\n$badtest TESTS FAILED!!\n";
181*ebfedea0SLionel Sambuc}
182*ebfedea0SLionel Sambucelse {
183*ebfedea0SLionel Sambuc    print "\n***All tests successful***\n";
184*ebfedea0SLionel Sambuc}
185*ebfedea0SLionel Sambuc
186*ebfedea0SLionel Sambucsub write_pem {
187*ebfedea0SLionel Sambuc    my ( $filename, $str, $data ) = @_;
188*ebfedea0SLionel Sambuc
189*ebfedea0SLionel Sambuc    $filename =~ s/\.[^.]*$/.pem/;
190*ebfedea0SLionel Sambuc
191*ebfedea0SLionel Sambuc    push @cleanup, $filename;
192*ebfedea0SLionel Sambuc
193*ebfedea0SLionel Sambuc    open OUT, ">$filename";
194*ebfedea0SLionel Sambuc
195*ebfedea0SLionel Sambuc    print OUT "-----BEGIN $str-----\n";
196*ebfedea0SLionel Sambuc    print OUT $data;
197*ebfedea0SLionel Sambuc    print OUT "-----END $str-----\n";
198*ebfedea0SLionel Sambuc
199*ebfedea0SLionel Sambuc    close OUT;
200*ebfedea0SLionel Sambuc}
201*ebfedea0SLionel Sambuc
202*ebfedea0SLionel Sambucsub run_reencode_test {
203*ebfedea0SLionel Sambuc    my ( $cmsdir, $tfile ) = @_;
204*ebfedea0SLionel Sambuc    unlink "tmp.der";
205*ebfedea0SLionel Sambuc
206*ebfedea0SLionel Sambuc    system( "$cmscmd -cmsout -inform DER -outform DER"
207*ebfedea0SLionel Sambuc          . " -in $cmsdir/$tfile -out tmp.der" );
208*ebfedea0SLionel Sambuc
209*ebfedea0SLionel Sambuc    if ($?) {
210*ebfedea0SLionel Sambuc        print "\tReencode command FAILED!!\n";
211*ebfedea0SLionel Sambuc        $badtest++;
212*ebfedea0SLionel Sambuc    }
213*ebfedea0SLionel Sambuc    elsif ( !cmp_files( "$cmsdir/$tfile", "tmp.der" ) ) {
214*ebfedea0SLionel Sambuc        print "\tReencode FAILED!!\n";
215*ebfedea0SLionel Sambuc        $badtest++;
216*ebfedea0SLionel Sambuc    }
217*ebfedea0SLionel Sambuc    else {
218*ebfedea0SLionel Sambuc        print "\tReencode passed\n" if $verbose;
219*ebfedea0SLionel Sambuc    }
220*ebfedea0SLionel Sambuc}
221*ebfedea0SLionel Sambuc
222*ebfedea0SLionel Sambucsub run_certsout_test {
223*ebfedea0SLionel Sambuc    my ( $cmsdir, $tfile ) = @_;
224*ebfedea0SLionel Sambuc    unlink "tmp.der";
225*ebfedea0SLionel Sambuc    unlink "tmp.pem";
226*ebfedea0SLionel Sambuc
227*ebfedea0SLionel Sambuc    system( "$cmscmd -cmsout -inform DER -certsout tmp.pem"
228*ebfedea0SLionel Sambuc          . " -in $cmsdir/$tfile -out tmp.der" );
229*ebfedea0SLionel Sambuc
230*ebfedea0SLionel Sambuc    if ($?) {
231*ebfedea0SLionel Sambuc        print "\tCertificate output command FAILED!!\n";
232*ebfedea0SLionel Sambuc        $badtest++;
233*ebfedea0SLionel Sambuc    }
234*ebfedea0SLionel Sambuc    else {
235*ebfedea0SLionel Sambuc        print "\tCertificate output passed\n" if $verbose;
236*ebfedea0SLionel Sambuc    }
237*ebfedea0SLionel Sambuc}
238*ebfedea0SLionel Sambuc
239*ebfedea0SLionel Sambucsub run_dataout_test {
240*ebfedea0SLionel Sambuc    my ( $cmsdir, $tfile ) = @_;
241*ebfedea0SLionel Sambuc    unlink "tmp.txt";
242*ebfedea0SLionel Sambuc
243*ebfedea0SLionel Sambuc    system(
244*ebfedea0SLionel Sambuc        "$cmscmd -data_out -inform DER" . " -in $cmsdir/$tfile -out tmp.txt" );
245*ebfedea0SLionel Sambuc
246*ebfedea0SLionel Sambuc    if ($?) {
247*ebfedea0SLionel Sambuc        print "\tDataout command FAILED!!\n";
248*ebfedea0SLionel Sambuc        $badtest++;
249*ebfedea0SLionel Sambuc    }
250*ebfedea0SLionel Sambuc    elsif ( !cmp_files( "$cmsdir/ExContent.bin", "tmp.txt" ) ) {
251*ebfedea0SLionel Sambuc        print "\tDataout compare FAILED!!\n";
252*ebfedea0SLionel Sambuc        $badtest++;
253*ebfedea0SLionel Sambuc    }
254*ebfedea0SLionel Sambuc    else {
255*ebfedea0SLionel Sambuc        print "\tDataout passed\n" if $verbose;
256*ebfedea0SLionel Sambuc    }
257*ebfedea0SLionel Sambuc}
258*ebfedea0SLionel Sambuc
259*ebfedea0SLionel Sambucsub run_verify_test {
260*ebfedea0SLionel Sambuc    my ( $cmsdir, $tlist, $tfile ) = @_;
261*ebfedea0SLionel Sambuc    unlink "tmp.txt";
262*ebfedea0SLionel Sambuc
263*ebfedea0SLionel Sambuc    $form   = "DER"                     if $tlist =~ /verifyder/;
264*ebfedea0SLionel Sambuc    $form   = "SMIME"                   if $tlist =~ /verifymime/;
265*ebfedea0SLionel Sambuc    $cafile = "$cmsdir/CarlDSSSelf.pem" if $tlist =~ /dss/;
266*ebfedea0SLionel Sambuc    $cafile = "$cmsdir/CarlRSASelf.pem" if $tlist =~ /rsa/;
267*ebfedea0SLionel Sambuc
268*ebfedea0SLionel Sambuc    $cmd =
269*ebfedea0SLionel Sambuc        "$cmscmd -verify -inform $form"
270*ebfedea0SLionel Sambuc      . " -CAfile $cafile"
271*ebfedea0SLionel Sambuc      . " -in $cmsdir/$tfile -out tmp.txt";
272*ebfedea0SLionel Sambuc
273*ebfedea0SLionel Sambuc    $cmd .= " -content $cmsdir/ExContent.bin" if $tlist =~ /cont_extern/;
274*ebfedea0SLionel Sambuc
275*ebfedea0SLionel Sambuc    system("$cmd 2>cms.err 1>cms.out");
276*ebfedea0SLionel Sambuc
277*ebfedea0SLionel Sambuc    if ($?) {
278*ebfedea0SLionel Sambuc        print "\tVerify command FAILED!!\n";
279*ebfedea0SLionel Sambuc        $badtest++;
280*ebfedea0SLionel Sambuc    }
281*ebfedea0SLionel Sambuc    elsif ( $tlist =~ /cont/
282*ebfedea0SLionel Sambuc        && !cmp_files( "$cmsdir/ExContent.bin", "tmp.txt" ) )
283*ebfedea0SLionel Sambuc    {
284*ebfedea0SLionel Sambuc        print "\tVerify content compare FAILED!!\n";
285*ebfedea0SLionel Sambuc        $badtest++;
286*ebfedea0SLionel Sambuc    }
287*ebfedea0SLionel Sambuc    else {
288*ebfedea0SLionel Sambuc        print "\tVerify passed\n" if $verbose;
289*ebfedea0SLionel Sambuc    }
290*ebfedea0SLionel Sambuc}
291*ebfedea0SLionel Sambuc
292*ebfedea0SLionel Sambucsub run_envelope_test {
293*ebfedea0SLionel Sambuc    my ( $cmsdir, $tlist, $tfile ) = @_;
294*ebfedea0SLionel Sambuc    unlink "tmp.txt";
295*ebfedea0SLionel Sambuc
296*ebfedea0SLionel Sambuc    $form = "DER"   if $tlist =~ /envelopeder/;
297*ebfedea0SLionel Sambuc    $form = "SMIME" if $tlist =~ /envelopemime/;
298*ebfedea0SLionel Sambuc
299*ebfedea0SLionel Sambuc    $cmd =
300*ebfedea0SLionel Sambuc        "$cmscmd -decrypt -inform $form"
301*ebfedea0SLionel Sambuc      . " -recip $cmsdir/BobRSASignByCarl.pem"
302*ebfedea0SLionel Sambuc      . " -inkey $cmsdir/BobPrivRSAEncrypt.pem"
303*ebfedea0SLionel Sambuc      . " -in $cmsdir/$tfile -out tmp.txt";
304*ebfedea0SLionel Sambuc
305*ebfedea0SLionel Sambuc    system("$cmd 2>cms.err 1>cms.out");
306*ebfedea0SLionel Sambuc
307*ebfedea0SLionel Sambuc    if ($?) {
308*ebfedea0SLionel Sambuc        print "\tDecrypt command FAILED!!\n";
309*ebfedea0SLionel Sambuc        $badtest++;
310*ebfedea0SLionel Sambuc    }
311*ebfedea0SLionel Sambuc    elsif ( $tlist =~ /cont/
312*ebfedea0SLionel Sambuc        && !cmp_files( "$cmsdir/ExContent.bin", "tmp.txt" ) )
313*ebfedea0SLionel Sambuc    {
314*ebfedea0SLionel Sambuc        print "\tDecrypt content compare FAILED!!\n";
315*ebfedea0SLionel Sambuc        $badtest++;
316*ebfedea0SLionel Sambuc    }
317*ebfedea0SLionel Sambuc    else {
318*ebfedea0SLionel Sambuc        print "\tDecrypt passed\n" if $verbose;
319*ebfedea0SLionel Sambuc    }
320*ebfedea0SLionel Sambuc}
321*ebfedea0SLionel Sambuc
322*ebfedea0SLionel Sambucsub run_digest_test {
323*ebfedea0SLionel Sambuc    my ( $cmsdir, $tlist, $tfile ) = @_;
324*ebfedea0SLionel Sambuc    unlink "tmp.txt";
325*ebfedea0SLionel Sambuc
326*ebfedea0SLionel Sambuc    my $cmd =
327*ebfedea0SLionel Sambuc      "$cmscmd -digest_verify -inform DER" . " -in $cmsdir/$tfile -out tmp.txt";
328*ebfedea0SLionel Sambuc
329*ebfedea0SLionel Sambuc    system("$cmd 2>cms.err 1>cms.out");
330*ebfedea0SLionel Sambuc
331*ebfedea0SLionel Sambuc    if ($?) {
332*ebfedea0SLionel Sambuc        print "\tDigest verify command FAILED!!\n";
333*ebfedea0SLionel Sambuc        $badtest++;
334*ebfedea0SLionel Sambuc    }
335*ebfedea0SLionel Sambuc    elsif ( $tlist =~ /cont/
336*ebfedea0SLionel Sambuc        && !cmp_files( "$cmsdir/ExContent.bin", "tmp.txt" ) )
337*ebfedea0SLionel Sambuc    {
338*ebfedea0SLionel Sambuc        print "\tDigest verify content compare FAILED!!\n";
339*ebfedea0SLionel Sambuc        $badtest++;
340*ebfedea0SLionel Sambuc    }
341*ebfedea0SLionel Sambuc    else {
342*ebfedea0SLionel Sambuc        print "\tDigest verify passed\n" if $verbose;
343*ebfedea0SLionel Sambuc    }
344*ebfedea0SLionel Sambuc}
345*ebfedea0SLionel Sambuc
346*ebfedea0SLionel Sambucsub run_encrypted_test {
347*ebfedea0SLionel Sambuc    my ( $cmsdir, $tlist, $tfile, $key ) = @_;
348*ebfedea0SLionel Sambuc    unlink "tmp.txt";
349*ebfedea0SLionel Sambuc
350*ebfedea0SLionel Sambuc    system( "$cmscmd -EncryptedData_decrypt -inform DER"
351*ebfedea0SLionel Sambuc          . " -secretkey $key"
352*ebfedea0SLionel Sambuc          . " -in $cmsdir/$tfile -out tmp.txt" );
353*ebfedea0SLionel Sambuc
354*ebfedea0SLionel Sambuc    if ($?) {
355*ebfedea0SLionel Sambuc        print "\tEncrypted Data command FAILED!!\n";
356*ebfedea0SLionel Sambuc        $badtest++;
357*ebfedea0SLionel Sambuc    }
358*ebfedea0SLionel Sambuc    elsif ( $tlist =~ /cont/
359*ebfedea0SLionel Sambuc        && !cmp_files( "$cmsdir/ExContent.bin", "tmp.txt" ) )
360*ebfedea0SLionel Sambuc    {
361*ebfedea0SLionel Sambuc        print "\tEncrypted Data content compare FAILED!!\n";
362*ebfedea0SLionel Sambuc        $badtest++;
363*ebfedea0SLionel Sambuc    }
364*ebfedea0SLionel Sambuc    else {
365*ebfedea0SLionel Sambuc        print "\tEncryptedData verify passed\n" if $verbose;
366*ebfedea0SLionel Sambuc    }
367*ebfedea0SLionel Sambuc}
368*ebfedea0SLionel Sambuc
369*ebfedea0SLionel Sambucsub cmp_files {
370*ebfedea0SLionel Sambuc    my ( $f1, $f2 ) = @_;
371*ebfedea0SLionel Sambuc    my ( $fp1, $fp2 );
372*ebfedea0SLionel Sambuc
373*ebfedea0SLionel Sambuc    my ( $rd1, $rd2 );
374*ebfedea0SLionel Sambuc
375*ebfedea0SLionel Sambuc    if ( !open( $fp1, "<$f1" ) ) {
376*ebfedea0SLionel Sambuc        print STDERR "Can't Open file $f1\n";
377*ebfedea0SLionel Sambuc        return 0;
378*ebfedea0SLionel Sambuc    }
379*ebfedea0SLionel Sambuc
380*ebfedea0SLionel Sambuc    if ( !open( $fp2, "<$f2" ) ) {
381*ebfedea0SLionel Sambuc        print STDERR "Can't Open file $f2\n";
382*ebfedea0SLionel Sambuc        return 0;
383*ebfedea0SLionel Sambuc    }
384*ebfedea0SLionel Sambuc
385*ebfedea0SLionel Sambuc    binmode $fp1;
386*ebfedea0SLionel Sambuc    binmode $fp2;
387*ebfedea0SLionel Sambuc
388*ebfedea0SLionel Sambuc    my $ret = 0;
389*ebfedea0SLionel Sambuc
390*ebfedea0SLionel Sambuc    for ( ; ; ) {
391*ebfedea0SLionel Sambuc        $n1 = sysread $fp1, $rd1, 4096;
392*ebfedea0SLionel Sambuc        $n2 = sysread $fp2, $rd2, 4096;
393*ebfedea0SLionel Sambuc        last if ( $n1 != $n2 );
394*ebfedea0SLionel Sambuc        last if ( $rd1 ne $rd2 );
395*ebfedea0SLionel Sambuc
396*ebfedea0SLionel Sambuc        if ( $n1 == 0 ) {
397*ebfedea0SLionel Sambuc            $ret = 1;
398*ebfedea0SLionel Sambuc            last;
399*ebfedea0SLionel Sambuc        }
400*ebfedea0SLionel Sambuc
401*ebfedea0SLionel Sambuc    }
402*ebfedea0SLionel Sambuc
403*ebfedea0SLionel Sambuc    close $fp1;
404*ebfedea0SLionel Sambuc    close $fp2;
405*ebfedea0SLionel Sambuc
406*ebfedea0SLionel Sambuc    return $ret;
407*ebfedea0SLionel Sambuc
408*ebfedea0SLionel Sambuc}
409*ebfedea0SLionel Sambuc
410