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