isaacpittman-hitachi / rpms / openssl

Forked from rpms/openssl 2 years ago
Clone

Blame SOURCES/0068-CVE-2022-2068.patch

22d461
diff -up openssl-3.0.1/tools/c_rehash.in.cve20222068 openssl-3.0.1/tools/c_rehash.in
22d461
--- openssl-3.0.1/tools/c_rehash.in.cve20222068	2022-06-22 13:15:57.347421765 +0200
22d461
+++ openssl-3.0.1/tools/c_rehash.in	2022-06-22 13:16:14.797576250 +0200
22d461
@@ -104,18 +104,41 @@ foreach (@dirlist) {
22d461
 }
22d461
 exit($errorcount);
22d461
 
22d461
+sub copy_file {
22d461
+    my ($src_fname, $dst_fname) = @_;
22d461
+
22d461
+    if (open(my $in, "<", $src_fname)) {
22d461
+        if (open(my $out, ">", $dst_fname)) {
22d461
+            print $out $_ while (<$in>);
22d461
+            close $out;
22d461
+        } else {
22d461
+            warn "Cannot open $dst_fname for write, $!";
22d461
+        }
22d461
+        close $in;
22d461
+    } else {
22d461
+        warn "Cannot open $src_fname for read, $!";
22d461
+    }
22d461
+}
22d461
+
22d461
 sub hash_dir {
22d461
+    my $dir = shift;
22d461
     my %hashlist;
22d461
-    print "Doing $_[0]\n";
22d461
-    chdir $_[0];
22d461
-    opendir(DIR, ".");
22d461
+
22d461
+    print "Doing $dir\n";
22d461
+
22d461
+    if (!chdir $dir) {
22d461
+        print STDERR "WARNING: Cannot chdir to '$dir', $!\n";
22d461
+        return;
22d461
+    }
22d461
+
22d461
+    opendir(DIR, ".") || print STDERR "WARNING: Cannot opendir '.', $!\n";
22d461
     my @flist = sort readdir(DIR);
22d461
     closedir DIR;
22d461
     if ( $removelinks ) {
22d461
         # Delete any existing symbolic links
22d461
         foreach (grep {/^[\da-f]+\.r{0,1}\d+$/} @flist) {
22d461
             if (-l $_) {
22d461
-                print "unlink $_" if $verbose;
22d461
+                print "unlink $_\n" if $verbose;
22d461
                 unlink $_ || warn "Can't unlink $_, $!\n";
22d461
             }
22d461
         }
22d461
@@ -130,13 +153,16 @@ sub hash_dir {
22d461
         link_hash_cert($fname) if ($cert);
22d461
         link_hash_crl($fname) if ($crl);
22d461
     }
22d461
+
22d461
+    chdir $pwd;
22d461
 }
22d461
 
22d461
 sub check_file {
22d461
     my ($is_cert, $is_crl) = (0,0);
22d461
     my $fname = $_[0];
22d461
-    open IN, $fname;
22d461
-    while(<IN>) {
22d461
+
22d461
+    open(my $in, "<", $fname);
22d461
+    while(<$in>) {
22d461
         if (/^-----BEGIN (.*)-----/) {
22d461
             my $hdr = $1;
22d461
             if ($hdr =~ /^(X509 |TRUSTED |)CERTIFICATE$/) {
22d461
@@ -148,7 +174,7 @@ sub check_file {
22d461
             }
22d461
         }
22d461
     }
22d461
-    close IN;
22d461
+    close $in;
22d461
     return ($is_cert, $is_crl);
22d461
 }
22d461
 
22d461
@@ -177,76 +203,49 @@ sub compute_hash {
22d461
 # certificate fingerprints
22d461
 
22d461
 sub link_hash_cert {
22d461
-    my $fname = $_[0];
22d461
-    my ($hash, $fprint) = compute_hash($openssl, "x509", $x509hash,
22d461
-                                       "-fingerprint", "-noout",
22d461
-                                       "-in", $fname);
22d461
-    chomp $hash;
22d461
-    chomp $fprint;
22d461
-    return if !$hash;
22d461
-    $fprint =~ s/^.*=//;
22d461
-    $fprint =~ tr/://d;
22d461
-    my $suffix = 0;
22d461
-    # Search for an unused hash filename
22d461
-    while(exists $hashlist{"$hash.$suffix"}) {
22d461
-        # Hash matches: if fingerprint matches its a duplicate cert
22d461
-        if ($hashlist{"$hash.$suffix"} eq $fprint) {
22d461
-            print STDERR "WARNING: Skipping duplicate certificate $fname\n";
22d461
-            return;
22d461
-        }
22d461
-        $suffix++;
22d461
-    }
22d461
-    $hash .= ".$suffix";
22d461
-    if ($symlink_exists) {
22d461
-        print "link $fname -> $hash\n" if $verbose;
22d461
-        symlink $fname, $hash || warn "Can't symlink, $!";
22d461
-    } else {
22d461
-        print "copy $fname -> $hash\n" if $verbose;
22d461
-        if (open($in, "<", $fname)) {
22d461
-            if (open($out,">", $hash)) {
22d461
-                print $out $_ while (<$in>);
22d461
-                close $out;
22d461
-            } else {
22d461
-                warn "can't open $hash for write, $!";
22d461
-            }
22d461
-            close $in;
22d461
-        } else {
22d461
-            warn "can't open $fname for read, $!";
22d461
-        }
22d461
-    }
22d461
-    $hashlist{$hash} = $fprint;
22d461
+    link_hash($_[0], 'cert');
22d461
 }
22d461
 
22d461
 # Same as above except for a CRL. CRL links are of the form <hash>.r<n>
22d461
 
22d461
 sub link_hash_crl {
22d461
-    my $fname = $_[0];
22d461
-    my ($hash, $fprint) = compute_hash($openssl, "crl", $crlhash,
22d461
+    link_hash($_[0], 'crl');
22d461
+}
22d461
+
22d461
+sub link_hash {
22d461
+    my ($fname, $type) = @_;
22d461
+    my $is_cert = $type eq 'cert';
22d461
+
22d461
+    my ($hash, $fprint) = compute_hash($openssl,
22d461
+                                       $is_cert ? "x509" : "crl",
22d461
+                                       $is_cert ? $x509hash : $crlhash,
22d461
                                        "-fingerprint", "-noout",
22d461
                                        "-in", $fname);
22d461
     chomp $hash;
22d461
+    $hash =~ s/^.*=// if !$is_cert;
22d461
     chomp $fprint;
22d461
     return if !$hash;
22d461
     $fprint =~ s/^.*=//;
22d461
     $fprint =~ tr/://d;
22d461
     my $suffix = 0;
22d461
     # Search for an unused hash filename
22d461
-    while(exists $hashlist{"$hash.r$suffix"}) {
22d461
+    my $crlmark = $is_cert ? "" : "r";
22d461
+    while(exists $hashlist{"$hash.$crlmark$suffix"}) {
22d461
         # Hash matches: if fingerprint matches its a duplicate cert
22d461
-        if ($hashlist{"$hash.r$suffix"} eq $fprint) {
22d461
-            print STDERR "WARNING: Skipping duplicate CRL $fname\n";
22d461
+        if ($hashlist{"$hash.$crlmark$suffix"} eq $fprint) {
22d461
+            my $what = $is_cert ? 'certificate' : 'CRL';
22d461
+            print STDERR "WARNING: Skipping duplicate $what $fname\n";
22d461
             return;
22d461
         }
22d461
         $suffix++;
22d461
     }
22d461
-    $hash .= ".r$suffix";
22d461
+    $hash .= ".$crlmark$suffix";
22d461
     if ($symlink_exists) {
22d461
         print "link $fname -> $hash\n" if $verbose;
22d461
         symlink $fname, $hash || warn "Can't symlink, $!";
22d461
     } else {
22d461
-        print "cp $fname -> $hash\n" if $verbose;
22d461
-        system ("cp", $fname, $hash);
22d461
-        warn "Can't copy, $!" if ($? >> 8) != 0;
22d461
+        print "copy $fname -> $hash\n" if $verbose;
22d461
+        copy_file($fname, $hash);
22d461
     }
22d461
     $hashlist{$hash} = $fprint;
22d461
 }