1# test/cms-test.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# CMS, PKCS7 consistency test script. Run extensive tests on
54# OpenSSL PKCS#7 and CMS implementations.
55
56my $ossl_path;
57my $redir = " 2> cms.err > cms.out";
58# Make VMS work
59if ( $^O eq "VMS" && -f "OSSLX:openssl.exe" ) {
60    $ossl_path = "pipe mcr OSSLX:openssl";
61    $null_path = "NL:";
62    # On VMS, the lowest 3 bits of the exit code indicates severity
63    # 1 is success (perl translates it to 0 for $?), 2 is error
64    # (perl doesn't translate it)
65    $failure_code = 512;	# 2 << 8 = 512
66}
67# Make MSYS work
68elsif ( $^O eq "MSWin32" && -f "../apps/openssl.exe" ) {
69    $ossl_path = "cmd /c ..\\apps\\openssl";
70    $null_path = "NUL";
71    $failure_code = 256;
72}
73elsif ( -f "../apps/openssl$ENV{EXE_EXT}" ) {
74    $ossl_path = "../util/shlib_wrap.sh ../apps/openssl";
75    $null_path = "/dev/null";
76    $failure_code = 256;
77}
78elsif ( -f "..\\out32dll\\openssl.exe" ) {
79    $ossl_path = "..\\out32dll\\openssl.exe";
80    $null_path = "NUL";
81    $failure_code = 256;
82}
83elsif ( -f "..\\out32\\openssl.exe" ) {
84    $ossl_path = "..\\out32\\openssl.exe";
85    $null_path = "NUL";
86    $failure_code = 256;
87}
88else {
89    die "Can't find OpenSSL executable";
90}
91
92my $pk7cmd   = "$ossl_path smime ";
93my $cmscmd   = "$ossl_path cms ";
94my $smdir    = "smime-certs";
95my $halt_err = 1;
96
97my $badcmd = 0;
98my $no_ec;
99my $no_ec2m;
100my $no_ecdh;
101my $ossl8 = `$ossl_path version -v` =~ /0\.9\.8/;
102
103system ("$ossl_path no-ec > $null_path");
104if ($? == 0)
105	{
106	$no_ec = 1;
107	}
108elsif ($? == $failure_code)
109	{
110	$no_ec = 0;
111	}
112else
113	{
114	die "Error checking for EC support\n";
115	}
116
117system ("$ossl_path no-ec2m > $null_path");
118if ($? == 0)
119	{
120	$no_ec2m = 1;
121	}
122elsif ($? == $failure_code)
123	{
124	$no_ec2m = 0;
125	}
126else
127	{
128	die "Error checking for EC2M support\n";
129	}
130
131system ("$ossl_path no-ec > $null_path");
132if ($? == 0)
133	{
134	$no_ecdh = 1;
135	}
136elsif ($? == $failure_code)
137	{
138	$no_ecdh = 0;
139	}
140else
141	{
142	die "Error checking for ECDH support\n";
143	}
144
145my @smime_pkcs7_tests = (
146
147    [
148        "signed content DER format, RSA key",
149        "-sign -in smcont.txt -outform \"DER\" -nodetach"
150          . " -certfile $smdir/smroot.pem"
151          . " -signer $smdir/smrsa1.pem -out test.cms",
152        "-verify -in test.cms -inform \"DER\" "
153          . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
154    ],
155
156    [
157        "signed detached content DER format, RSA key",
158        "-sign -in smcont.txt -outform \"DER\""
159          . " -signer $smdir/smrsa1.pem -out test.cms",
160        "-verify -in test.cms -inform \"DER\" "
161          . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt -content smcont.txt"
162    ],
163
164    [
165        "signed content test streaming BER format, RSA",
166        "-sign -in smcont.txt -outform \"DER\" -nodetach"
167          . " -stream -signer $smdir/smrsa1.pem -out test.cms",
168        "-verify -in test.cms -inform \"DER\" "
169          . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
170    ],
171
172    [
173        "signed content DER format, DSA key",
174        "-sign -in smcont.txt -outform \"DER\" -nodetach"
175          . " -signer $smdir/smdsa1.pem -out test.cms",
176        "-verify -in test.cms -inform \"DER\" "
177          . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
178    ],
179
180    [
181        "signed detached content DER format, DSA key",
182        "-sign -in smcont.txt -outform \"DER\""
183          . " -signer $smdir/smdsa1.pem -out test.cms",
184        "-verify -in test.cms -inform \"DER\" "
185          . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt -content smcont.txt"
186    ],
187
188    [
189        "signed detached content DER format, add RSA signer",
190        "-resign -inform \"DER\" -in test.cms -outform \"DER\""
191          . " -signer $smdir/smrsa1.pem -out test2.cms",
192        "-verify -in test2.cms -inform \"DER\" "
193          . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt -content smcont.txt"
194    ],
195
196    [
197        "signed content test streaming BER format, DSA key",
198        "-sign -in smcont.txt -outform \"DER\" -nodetach"
199          . " -stream -signer $smdir/smdsa1.pem -out test.cms",
200        "-verify -in test.cms -inform \"DER\" "
201          . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
202    ],
203
204    [
205        "signed content test streaming BER format, 2 DSA and 2 RSA keys",
206        "-sign -in smcont.txt -outform \"DER\" -nodetach"
207          . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
208          . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
209          . " -stream -out test.cms",
210        "-verify -in test.cms -inform \"DER\" "
211          . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
212    ],
213
214    [
215"signed content test streaming BER format, 2 DSA and 2 RSA keys, no attributes",
216        "-sign -in smcont.txt -outform \"DER\" -noattr -nodetach"
217          . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
218          . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
219          . " -stream -out test.cms",
220        "-verify -in test.cms -inform \"DER\" "
221          . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
222    ],
223
224    [
225        "signed content test streaming S/MIME format, 2 DSA and 2 RSA keys",
226        "-sign -in smcont.txt -nodetach"
227          . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
228          . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
229          . " -stream -out test.cms",
230        "-verify -in test.cms " . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
231    ],
232
233    [
234"signed content test streaming multipart S/MIME format, 2 DSA and 2 RSA keys",
235        "-sign -in smcont.txt"
236          . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
237          . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
238          . " -stream -out test.cms",
239        "-verify -in test.cms " . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
240    ],
241
242    [
243        "enveloped content test streaming S/MIME format, 3 recipients",
244        "-encrypt -in smcont.txt"
245          . " -stream -out test.cms"
246          . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
247        "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
248    ],
249
250    [
251"enveloped content test streaming S/MIME format, 3 recipients, 3rd used",
252        "-encrypt -in smcont.txt"
253          . " -stream -out test.cms"
254          . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
255        "-decrypt -recip $smdir/smrsa3.pem -in test.cms -out smtst.txt"
256    ],
257
258    [
259"enveloped content test streaming S/MIME format, 3 recipients, key only used",
260        "-encrypt -in smcont.txt"
261          . " -stream -out test.cms"
262          . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
263        "-decrypt -inkey $smdir/smrsa3.pem -in test.cms -out smtst.txt"
264    ],
265
266    [
267"enveloped content test streaming S/MIME format, AES-256 cipher, 3 recipients",
268        "-encrypt -in smcont.txt"
269          . " -aes256 -stream -out test.cms"
270          . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
271        "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
272    ],
273
274);
275
276my @smime_cms_tests = (
277
278    [
279        "signed content test streaming BER format, 2 DSA and 2 RSA keys, keyid",
280        "-sign -in smcont.txt -outform \"DER\" -nodetach -keyid"
281          . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
282          . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
283          . " -stream -out test.cms",
284        "-verify -in test.cms -inform \"DER\" "
285          . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
286    ],
287
288    [
289        "signed content test streaming PEM format, 2 DSA and 2 RSA keys",
290        "-sign -in smcont.txt -outform PEM -nodetach"
291          . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
292          . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
293          . " -stream -out test.cms",
294        "-verify -in test.cms -inform PEM "
295          . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
296    ],
297
298    [
299        "signed content MIME format, RSA key, signed receipt request",
300        "-sign -in smcont.txt -signer $smdir/smrsa1.pem -nodetach"
301          . " -receipt_request_to test\@openssl.org -receipt_request_all"
302          . " -out test.cms",
303        "-verify -in test.cms "
304          . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
305    ],
306
307    [
308        "signed receipt MIME format, RSA key",
309        "-sign_receipt -in test.cms"
310          . " -signer $smdir/smrsa2.pem"
311          . " -out test2.cms",
312        "-verify_receipt test2.cms -in test.cms"
313          . " \"-CAfile\" $smdir/smroot.pem"
314    ],
315
316    [
317        "enveloped content test streaming S/MIME format, 3 recipients, keyid",
318        "-encrypt -in smcont.txt"
319          . " -stream -out test.cms -keyid"
320          . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
321        "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
322    ],
323
324    [
325        "enveloped content test streaming PEM format, KEK",
326        "-encrypt -in smcont.txt -outform PEM -aes128"
327          . " -stream -out test.cms "
328          . " -secretkey 000102030405060708090A0B0C0D0E0F "
329          . " -secretkeyid C0FEE0",
330        "-decrypt -in test.cms -out smtst.txt -inform PEM"
331          . " -secretkey 000102030405060708090A0B0C0D0E0F "
332          . " -secretkeyid C0FEE0"
333    ],
334
335    [
336        "enveloped content test streaming PEM format, KEK, key only",
337        "-encrypt -in smcont.txt -outform PEM -aes128"
338          . " -stream -out test.cms "
339          . " -secretkey 000102030405060708090A0B0C0D0E0F "
340          . " -secretkeyid C0FEE0",
341        "-decrypt -in test.cms -out smtst.txt -inform PEM"
342          . " -secretkey 000102030405060708090A0B0C0D0E0F "
343    ],
344
345    [
346        "data content test streaming PEM format",
347        "-data_create -in smcont.txt -outform PEM -nodetach"
348          . " -stream -out test.cms",
349        "-data_out -in test.cms -inform PEM -out smtst.txt"
350    ],
351
352    [
353        "encrypted content test streaming PEM format, 128 bit RC2 key",
354        "\"-EncryptedData_encrypt\" -in smcont.txt -outform PEM"
355          . " -rc2 -secretkey 000102030405060708090A0B0C0D0E0F"
356          . " -stream -out test.cms",
357        "\"-EncryptedData_decrypt\" -in test.cms -inform PEM "
358          . " -secretkey 000102030405060708090A0B0C0D0E0F -out smtst.txt"
359    ],
360
361    [
362        "encrypted content test streaming PEM format, 40 bit RC2 key",
363        "\"-EncryptedData_encrypt\" -in smcont.txt -outform PEM"
364          . " -rc2 -secretkey 0001020304"
365          . " -stream -out test.cms",
366        "\"-EncryptedData_decrypt\" -in test.cms -inform PEM "
367          . " -secretkey 0001020304 -out smtst.txt"
368    ],
369
370    [
371        "encrypted content test streaming PEM format, triple DES key",
372        "\"-EncryptedData_encrypt\" -in smcont.txt -outform PEM"
373          . " -des3 -secretkey 000102030405060708090A0B0C0D0E0F1011121314151617"
374          . " -stream -out test.cms",
375        "\"-EncryptedData_decrypt\" -in test.cms -inform PEM "
376          . " -secretkey 000102030405060708090A0B0C0D0E0F1011121314151617"
377          . " -out smtst.txt"
378    ],
379
380    [
381        "encrypted content test streaming PEM format, 128 bit AES key",
382        "\"-EncryptedData_encrypt\" -in smcont.txt -outform PEM"
383          . " -aes128 -secretkey 000102030405060708090A0B0C0D0E0F"
384          . " -stream -out test.cms",
385        "\"-EncryptedData_decrypt\" -in test.cms -inform PEM "
386          . " -secretkey 000102030405060708090A0B0C0D0E0F -out smtst.txt"
387    ],
388
389);
390
391my @smime_cms_comp_tests = (
392
393    [
394        "compressed content test streaming PEM format",
395        "-compress -in smcont.txt -outform PEM -nodetach"
396          . " -stream -out test.cms",
397        "-uncompress -in test.cms -inform PEM -out smtst.txt"
398    ]
399
400);
401
402my @smime_cms_param_tests = (
403    [
404        "signed content test streaming PEM format, RSA keys, PSS signature",
405        "-sign -in smcont.txt -outform PEM -nodetach"
406          . " -signer $smdir/smrsa1.pem -keyopt rsa_padding_mode:pss"
407	  . " -out test.cms",
408        "-verify -in test.cms -inform PEM "
409          . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
410    ],
411
412    [
413        "signed content test streaming PEM format, RSA keys, PSS signature, no attributes",
414        "-sign -in smcont.txt -outform PEM -nodetach -noattr"
415          . " -signer $smdir/smrsa1.pem -keyopt rsa_padding_mode:pss"
416	  . " -out test.cms",
417        "-verify -in test.cms -inform PEM "
418          . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
419    ],
420
421    [
422        "signed content test streaming PEM format, RSA keys, PSS signature, SHA384 MGF1",
423        "-sign -in smcont.txt -outform PEM -nodetach"
424          . " -signer $smdir/smrsa1.pem -keyopt rsa_padding_mode:pss"
425	  . " -keyopt rsa_mgf1_md:sha384 -out test.cms",
426        "-verify -in test.cms -inform PEM "
427          . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
428    ],
429
430    [
431"enveloped content test streaming S/MIME format, OAEP default parameters",
432        "-encrypt -in smcont.txt"
433          . " -stream -out test.cms"
434          . " -recip $smdir/smrsa1.pem -keyopt rsa_padding_mode:oaep",
435        "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
436    ],
437
438    [
439"enveloped content test streaming S/MIME format, OAEP SHA256",
440        "-encrypt -in smcont.txt"
441          . " -stream -out test.cms"
442          . " -recip $smdir/smrsa1.pem -keyopt rsa_padding_mode:oaep"
443	  . " -keyopt rsa_oaep_md:sha256",
444        "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
445    ],
446
447    [
448"enveloped content test streaming S/MIME format, ECDH",
449        "-encrypt -in smcont.txt"
450          . " -stream -out test.cms"
451          . " -recip $smdir/smec1.pem",
452        "-decrypt -recip $smdir/smec1.pem -in test.cms -out smtst.txt"
453    ],
454
455    [
456"enveloped content test streaming S/MIME format, ECDH, key identifier",
457        "-encrypt -keyid -in smcont.txt"
458          . " -stream -out test.cms"
459          . " -recip $smdir/smec1.pem",
460        "-decrypt -recip $smdir/smec1.pem -in test.cms -out smtst.txt"
461    ],
462
463    [
464"enveloped content test streaming S/MIME format, ECDH, AES128, SHA256 KDF",
465        "-encrypt -in smcont.txt"
466          . " -stream -out test.cms"
467          . " -recip $smdir/smec1.pem -aes128 -keyopt ecdh_kdf_md:sha256",
468        "-decrypt -recip $smdir/smec1.pem -in test.cms -out smtst.txt"
469    ],
470
471    [
472"enveloped content test streaming S/MIME format, ECDH, K-283, cofactor DH",
473        "-encrypt -in smcont.txt"
474          . " -stream -out test.cms"
475          . " -recip $smdir/smec2.pem -aes128"
476	  . " -keyopt ecdh_kdf_md:sha256 -keyopt ecdh_cofactor_mode:1",
477        "-decrypt -recip $smdir/smec2.pem -in test.cms -out smtst.txt"
478    ],
479
480    [
481"enveloped content test streaming S/MIME format, X9.42 DH",
482        "-encrypt -in smcont.txt"
483          . " -stream -out test.cms"
484          . " -recip $smdir/smdh.pem -aes128",
485        "-decrypt -recip $smdir/smdh.pem -in test.cms -out smtst.txt"
486    ]
487);
488
489print "CMS => PKCS#7 compatibility tests\n";
490
491run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $cmscmd, $pk7cmd );
492
493print "CMS <= PKCS#7 compatibility tests\n";
494
495run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $pk7cmd, $cmscmd );
496
497print "CMS <=> CMS consistency tests\n";
498
499run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $cmscmd, $cmscmd );
500run_smime_tests( \$badcmd, \@smime_cms_tests,   $cmscmd, $cmscmd );
501
502print "CMS <=> CMS consistency tests, modified key parameters\n";
503run_smime_tests( \$badcmd, \@smime_cms_param_tests,   $cmscmd, $cmscmd );
504
505if ( `$ossl_path version -f` =~ /ZLIB/ ) {
506    run_smime_tests( \$badcmd, \@smime_cms_comp_tests, $cmscmd, $cmscmd );
507}
508else {
509    print "Zlib not supported: compression tests skipped\n";
510}
511
512print "Running modified tests for OpenSSL 0.9.8 cms backport\n" if($ossl8);
513
514if ($badcmd) {
515    print "$badcmd TESTS FAILED!!\n";
516}
517else {
518    print "ALL TESTS SUCCESSFUL.\n";
519}
520
521unlink "test.cms";
522unlink "test2.cms";
523unlink "smtst.txt";
524unlink "cms.out";
525unlink "cms.err";
526
527sub run_smime_tests {
528    my ( $rv, $aref, $scmd, $vcmd ) = @_;
529
530    foreach $smtst (@$aref) {
531        my ( $tnam, $rscmd, $rvcmd ) = @$smtst;
532	if ($ossl8)
533		{
534		# Skip smime resign: 0.9.8 smime doesn't support -resign
535		next if ($scmd =~ /smime/ && $rscmd =~ /-resign/);
536		# Disable streaming: option not supported in 0.9.8
537		$tnam =~ s/streaming//;
538		$rscmd =~ s/-stream//;
539		$rvcmd =~ s/-stream//;
540		}
541	if ($no_ec && $tnam =~ /ECDH/)
542		{
543		print "$tnam: skipped, EC disabled\n";
544		next;
545		}
546	if ($no_ecdh && $tnam =~ /ECDH/)
547		{
548		print "$tnam: skipped, ECDH disabled\n";
549		next;
550		}
551	if ($no_ec2m && $tnam =~ /K-283/)
552		{
553		print "$tnam: skipped, EC2M disabled\n";
554		next;
555		}
556        system("$scmd$rscmd$redir");
557        if ($?) {
558            print "$tnam: generation error\n";
559            $$rv++;
560            exit 1 if $halt_err;
561            next;
562        }
563        system("$vcmd$rvcmd$redir");
564        if ($?) {
565            print "$tnam: verify error\n";
566            $$rv++;
567            exit 1 if $halt_err;
568            next;
569        }
570	if (!cmp_files("smtst.txt", "smcont.txt")) {
571            print "$tnam: content verify error\n";
572            $$rv++;
573            exit 1 if $halt_err;
574            next;
575	}
576        print "$tnam: OK\n";
577    }
578}
579
580sub cmp_files {
581    use FileHandle;
582    my ( $f1, $f2 ) = @_;
583    my $fp1 = FileHandle->new();
584    my $fp2 = FileHandle->new();
585
586    my ( $rd1, $rd2 );
587
588    if ( !open( $fp1, "<$f1" ) ) {
589        print STDERR "Can't Open file $f1\n";
590        return 0;
591    }
592
593    if ( !open( $fp2, "<$f2" ) ) {
594        print STDERR "Can't Open file $f2\n";
595        return 0;
596    }
597
598    binmode $fp1;
599    binmode $fp2;
600
601    my $ret = 0;
602
603    for ( ; ; ) {
604        $n1 = sysread $fp1, $rd1, 4096;
605        $n2 = sysread $fp2, $rd2, 4096;
606        last if ( $n1 != $n2 );
607        last if ( $rd1 ne $rd2 );
608
609        if ( $n1 == 0 ) {
610            $ret = 1;
611            last;
612        }
613
614    }
615
616    close $fp1;
617    close $fp2;
618
619    return $ret;
620
621}
622
623