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