Richard Levitte | 291e94d | 2015-05-18 22:35:23 +0200 | [diff] [blame] | 1 | #!{- $config{perl} -} |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 2 | |
Richard Levitte | 9ab6fc5 | 2016-01-25 21:19:59 +0100 | [diff] [blame] | 3 | # {- join("\n# ", @autowarntext) -} |
| 4 | |
Dr. Stephen Henson | 439df50 | 2000-05-18 00:33:00 +0000 | [diff] [blame] | 5 | # Perl c_rehash script, scan all files in a directory |
| 6 | # and add symbolic links to their hash values. |
Ralf S. Engelschall | 13e91dd | 1998-12-22 15:59:57 +0000 | [diff] [blame] | 7 | |
Richard Levitte | 291e94d | 2015-05-18 22:35:23 +0200 | [diff] [blame] | 8 | my $dir = {- quotify1($config{openssldir}) -}; |
| 9 | my $prefix = {- quotify1($config{prefix}) -}; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 10 | |
Rich Salz | 4c7103a | 2015-09-10 11:46:13 -0400 | [diff] [blame] | 11 | my $errorcount = 0; |
Matthias Andree | a787c25 | 2014-09-07 18:45:02 -0400 | [diff] [blame] | 12 | my $openssl = $ENV{OPENSSL} || "openssl"; |
| 13 | my $pwd; |
| 14 | my $x509hash = "-subject_hash"; |
| 15 | my $crlhash = "-hash"; |
| 16 | my $verbose = 0; |
| 17 | my $symlink_exists=eval {symlink("",""); 1}; |
| 18 | my $removelinks = 1; |
| 19 | |
| 20 | ## Parse flags. |
Olaf Johansson | 8846adb | 2015-06-02 07:41:35 -0400 | [diff] [blame] | 21 | while ( $ARGV[0] =~ /^-/ ) { |
Matthias Andree | a787c25 | 2014-09-07 18:45:02 -0400 | [diff] [blame] | 22 | my $flag = shift @ARGV; |
| 23 | last if ( $flag eq '--'); |
Olaf Johansson | 8846adb | 2015-06-02 07:41:35 -0400 | [diff] [blame] | 24 | if ( $flag eq '-old') { |
Matthias Andree | a787c25 | 2014-09-07 18:45:02 -0400 | [diff] [blame] | 25 | $x509hash = "-subject_hash_old"; |
| 26 | $crlhash = "-hash_old"; |
Olaf Johansson | 8846adb | 2015-06-02 07:41:35 -0400 | [diff] [blame] | 27 | } elsif ( $flag eq '-h') { |
Matthias Andree | a787c25 | 2014-09-07 18:45:02 -0400 | [diff] [blame] | 28 | help(); |
| 29 | } elsif ( $flag eq '-n' ) { |
| 30 | $removelinks = 0; |
| 31 | } elsif ( $flag eq '-v' ) { |
| 32 | $verbose++; |
| 33 | } |
| 34 | else { |
| 35 | print STDERR "Usage error; try -help.\n"; |
| 36 | exit 1; |
| 37 | } |
Dr. Stephen Henson | 439df50 | 2000-05-18 00:33:00 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Matthias Andree | a787c25 | 2014-09-07 18:45:02 -0400 | [diff] [blame] | 40 | sub help { |
| 41 | print "Usage: c_rehash [-old] [-h] [-v] [dirs...]\n"; |
| 42 | print " -old use old-style digest\n"; |
| 43 | print " -h print this help text\n"; |
| 44 | print " -v print files removed and linked\n"; |
| 45 | exit 0; |
| 46 | } |
| 47 | |
Andy Polyakov | a2688c8 | 2006-10-26 10:52:12 +0000 | [diff] [blame] | 48 | eval "require Cwd"; |
| 49 | if (defined(&Cwd::getcwd)) { |
| 50 | $pwd=Cwd::getcwd(); |
| 51 | } else { |
Matthias Andree | a787c25 | 2014-09-07 18:45:02 -0400 | [diff] [blame] | 52 | $pwd=`pwd`; |
| 53 | chomp($pwd); |
Andy Polyakov | a2688c8 | 2006-10-26 10:52:12 +0000 | [diff] [blame] | 54 | } |
Andy Polyakov | d8cdd15 | 2006-10-21 16:28:03 +0000 | [diff] [blame] | 55 | |
Matthias Andree | a787c25 | 2014-09-07 18:45:02 -0400 | [diff] [blame] | 56 | # DOS/Win32 or Unix delimiter? Prefix our installdir, then search. |
| 57 | my $path_delim = ($pwd =~ /^[a-z]\:/i) ? ';' : ':'; |
| 58 | $ENV{PATH} = "$prefix/bin" . ($ENV{PATH} ? $path_delim . $ENV{PATH} : ""); |
Dr. Stephen Henson | 439df50 | 2000-05-18 00:33:00 +0000 | [diff] [blame] | 59 | |
Rich Salz | ff2f6bb | 2015-09-07 22:21:38 -0400 | [diff] [blame] | 60 | if (! -x $openssl) { |
Dr. Stephen Henson | 439df50 | 2000-05-18 00:33:00 +0000 | [diff] [blame] | 61 | my $found = 0; |
Andy Polyakov | d8cdd15 | 2006-10-21 16:28:03 +0000 | [diff] [blame] | 62 | foreach (split /$path_delim/, $ENV{PATH}) { |
Rich Salz | ff2f6bb | 2015-09-07 22:21:38 -0400 | [diff] [blame] | 63 | if (-x "$_/$openssl") { |
Dr. Stephen Henson | 439df50 | 2000-05-18 00:33:00 +0000 | [diff] [blame] | 64 | $found = 1; |
Dr. Stephen Henson | ef236ec | 2009-04-23 16:32:42 +0000 | [diff] [blame] | 65 | $openssl = "$_/$openssl"; |
Dr. Stephen Henson | 439df50 | 2000-05-18 00:33:00 +0000 | [diff] [blame] | 66 | last; |
| 67 | } |
| 68 | } |
Rich Salz | ff2f6bb | 2015-09-07 22:21:38 -0400 | [diff] [blame] | 69 | if ($found == 0) { |
Dr. Stephen Henson | 439df50 | 2000-05-18 00:33:00 +0000 | [diff] [blame] | 70 | print STDERR "c_rehash: rehashing skipped ('openssl' program not available)\n"; |
| 71 | exit 0; |
| 72 | } |
| 73 | } |
| 74 | |
Rich Salz | ff2f6bb | 2015-09-07 22:21:38 -0400 | [diff] [blame] | 75 | if (@ARGV) { |
Dr. Stephen Henson | 439df50 | 2000-05-18 00:33:00 +0000 | [diff] [blame] | 76 | @dirlist = @ARGV; |
Rich Salz | ff2f6bb | 2015-09-07 22:21:38 -0400 | [diff] [blame] | 77 | } elsif ($ENV{SSL_CERT_DIR}) { |
Andy Polyakov | d8cdd15 | 2006-10-21 16:28:03 +0000 | [diff] [blame] | 78 | @dirlist = split /$path_delim/, $ENV{SSL_CERT_DIR}; |
Dr. Stephen Henson | 439df50 | 2000-05-18 00:33:00 +0000 | [diff] [blame] | 79 | } else { |
| 80 | $dirlist[0] = "$dir/certs"; |
| 81 | } |
| 82 | |
Andy Polyakov | d8cdd15 | 2006-10-21 16:28:03 +0000 | [diff] [blame] | 83 | if (-d $dirlist[0]) { |
| 84 | chdir $dirlist[0]; |
| 85 | $openssl="$pwd/$openssl" if (!-x $openssl); |
| 86 | chdir $pwd; |
| 87 | } |
Dr. Stephen Henson | 439df50 | 2000-05-18 00:33:00 +0000 | [diff] [blame] | 88 | |
| 89 | foreach (@dirlist) { |
Rich Salz | ff2f6bb | 2015-09-07 22:21:38 -0400 | [diff] [blame] | 90 | if (-d $_ ) { |
| 91 | if ( -w $_) { |
Dr. Stephen Henson | 439df50 | 2000-05-18 00:33:00 +0000 | [diff] [blame] | 92 | hash_dir($_); |
Rich Salz | ff2f6bb | 2015-09-07 22:21:38 -0400 | [diff] [blame] | 93 | } else { |
| 94 | print "Skipping $_, can't write\n"; |
Rich Salz | 4c7103a | 2015-09-10 11:46:13 -0400 | [diff] [blame] | 95 | $errorcount++; |
Rich Salz | ff2f6bb | 2015-09-07 22:21:38 -0400 | [diff] [blame] | 96 | } |
Dr. Stephen Henson | 439df50 | 2000-05-18 00:33:00 +0000 | [diff] [blame] | 97 | } |
| 98 | } |
Rich Salz | 4c7103a | 2015-09-10 11:46:13 -0400 | [diff] [blame] | 99 | exit($errorcount); |
Dr. Stephen Henson | 439df50 | 2000-05-18 00:33:00 +0000 | [diff] [blame] | 100 | |
| 101 | sub hash_dir { |
| 102 | my %hashlist; |
| 103 | print "Doing $_[0]\n"; |
| 104 | chdir $_[0]; |
Rich Salz | 6f46c3c | 2014-09-11 13:08:30 -0400 | [diff] [blame] | 105 | opendir(DIR, "."); |
| 106 | my @flist = readdir(DIR); |
| 107 | closedir DIR; |
Matthias Andree | a787c25 | 2014-09-07 18:45:02 -0400 | [diff] [blame] | 108 | if ( $removelinks ) { |
Matthias Andree | a787c25 | 2014-09-07 18:45:02 -0400 | [diff] [blame] | 109 | # Delete any existing symbolic links |
| 110 | foreach (grep {/^[\da-f]+\.r{0,1}\d+$/} @flist) { |
Rich Salz | ff2f6bb | 2015-09-07 22:21:38 -0400 | [diff] [blame] | 111 | if (-l $_) { |
Matthias Andree | a787c25 | 2014-09-07 18:45:02 -0400 | [diff] [blame] | 112 | print "unlink $_" if $verbose; |
Rich Salz | ff2f6bb | 2015-09-07 22:21:38 -0400 | [diff] [blame] | 113 | unlink $_ || warn "Can't unlink $_, $!\n"; |
Matthias Andree | a787c25 | 2014-09-07 18:45:02 -0400 | [diff] [blame] | 114 | } |
Dr. Stephen Henson | 439df50 | 2000-05-18 00:33:00 +0000 | [diff] [blame] | 115 | } |
| 116 | } |
Matthias Andree | a787c25 | 2014-09-07 18:45:02 -0400 | [diff] [blame] | 117 | FILE: foreach $fname (grep {/\.(pem)|(crt)|(cer)|(crl)$/} @flist) { |
Dr. Stephen Henson | 439df50 | 2000-05-18 00:33:00 +0000 | [diff] [blame] | 118 | # Check to see if certificates and/or CRLs present. |
| 119 | my ($cert, $crl) = check_file($fname); |
Rich Salz | ff2f6bb | 2015-09-07 22:21:38 -0400 | [diff] [blame] | 120 | if (!$cert && !$crl) { |
Dr. Stephen Henson | 439df50 | 2000-05-18 00:33:00 +0000 | [diff] [blame] | 121 | print STDERR "WARNING: $fname does not contain a certificate or CRL: skipping\n"; |
| 122 | next; |
| 123 | } |
Rich Salz | ff2f6bb | 2015-09-07 22:21:38 -0400 | [diff] [blame] | 124 | link_hash_cert($fname) if ($cert); |
| 125 | link_hash_crl($fname) if ($crl); |
Dr. Stephen Henson | 439df50 | 2000-05-18 00:33:00 +0000 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
| 129 | sub check_file { |
| 130 | my ($is_cert, $is_crl) = (0,0); |
| 131 | my $fname = $_[0]; |
| 132 | open IN, $fname; |
| 133 | while(<IN>) { |
Rich Salz | ff2f6bb | 2015-09-07 22:21:38 -0400 | [diff] [blame] | 134 | if (/^-----BEGIN (.*)-----/) { |
Dr. Stephen Henson | 439df50 | 2000-05-18 00:33:00 +0000 | [diff] [blame] | 135 | my $hdr = $1; |
Rich Salz | ff2f6bb | 2015-09-07 22:21:38 -0400 | [diff] [blame] | 136 | if ($hdr =~ /^(X509 |TRUSTED |)CERTIFICATE$/) { |
Dr. Stephen Henson | 439df50 | 2000-05-18 00:33:00 +0000 | [diff] [blame] | 137 | $is_cert = 1; |
Rich Salz | ff2f6bb | 2015-09-07 22:21:38 -0400 | [diff] [blame] | 138 | last if ($is_crl); |
| 139 | } elsif ($hdr eq "X509 CRL") { |
Dr. Stephen Henson | 439df50 | 2000-05-18 00:33:00 +0000 | [diff] [blame] | 140 | $is_crl = 1; |
Rich Salz | ff2f6bb | 2015-09-07 22:21:38 -0400 | [diff] [blame] | 141 | last if ($is_cert); |
Dr. Stephen Henson | 439df50 | 2000-05-18 00:33:00 +0000 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | } |
| 145 | close IN; |
| 146 | return ($is_cert, $is_crl); |
| 147 | } |
| 148 | |
| 149 | |
| 150 | # Link a certificate to its subject name hash value, each hash is of |
| 151 | # the form <hash>.<n> where n is an integer. If the hash value already exists |
| 152 | # then we need to up the value of n, unless its a duplicate in which |
| 153 | # case we skip the link. We check for duplicates by comparing the |
| 154 | # certificate fingerprints |
| 155 | |
| 156 | sub link_hash_cert { |
| 157 | my $fname = $_[0]; |
Richard Levitte | b791099 | 2002-10-11 11:34:20 +0000 | [diff] [blame] | 158 | $fname =~ s/'/'\\''/g; |
Matthias Andree | a787c25 | 2014-09-07 18:45:02 -0400 | [diff] [blame] | 159 | my ($hash, $fprint) = `"$openssl" x509 $x509hash -fingerprint -noout -in "$fname"`; |
Dr. Stephen Henson | 439df50 | 2000-05-18 00:33:00 +0000 | [diff] [blame] | 160 | chomp $hash; |
| 161 | chomp $fprint; |
| 162 | $fprint =~ s/^.*=//; |
| 163 | $fprint =~ tr/://d; |
| 164 | my $suffix = 0; |
| 165 | # Search for an unused hash filename |
| 166 | while(exists $hashlist{"$hash.$suffix"}) { |
| 167 | # Hash matches: if fingerprint matches its a duplicate cert |
Rich Salz | ff2f6bb | 2015-09-07 22:21:38 -0400 | [diff] [blame] | 168 | if ($hashlist{"$hash.$suffix"} eq $fprint) { |
Dr. Stephen Henson | 439df50 | 2000-05-18 00:33:00 +0000 | [diff] [blame] | 169 | print STDERR "WARNING: Skipping duplicate certificate $fname\n"; |
| 170 | return; |
| 171 | } |
| 172 | $suffix++; |
| 173 | } |
| 174 | $hash .= ".$suffix"; |
Richard Levitte | 967d95f | 2001-04-04 15:50:30 +0000 | [diff] [blame] | 175 | if ($symlink_exists) { |
Matthias Andree | a787c25 | 2014-09-07 18:45:02 -0400 | [diff] [blame] | 176 | print "link $fname -> $hash\n" if $verbose; |
Rich Salz | ff2f6bb | 2015-09-07 22:21:38 -0400 | [diff] [blame] | 177 | symlink $fname, $hash || warn "Can't symlink, $!"; |
Richard Levitte | 967d95f | 2001-04-04 15:50:30 +0000 | [diff] [blame] | 178 | } else { |
Matthias Andree | a787c25 | 2014-09-07 18:45:02 -0400 | [diff] [blame] | 179 | print "copy $fname -> $hash\n" if $verbose; |
Rich Salz | ff2f6bb | 2015-09-07 22:21:38 -0400 | [diff] [blame] | 180 | if (open($in, "<", $fname)) { |
| 181 | if (open($out,">", $hash)) { |
| 182 | print $out $_ while (<$in>); |
| 183 | close $out; |
| 184 | } else { |
| 185 | warn "can't open $hash for write, $!"; |
| 186 | } |
| 187 | close $in; |
| 188 | } else { |
| 189 | warn "can't open $fname for read, $!"; |
| 190 | } |
Richard Levitte | 967d95f | 2001-04-04 15:50:30 +0000 | [diff] [blame] | 191 | } |
Dr. Stephen Henson | 439df50 | 2000-05-18 00:33:00 +0000 | [diff] [blame] | 192 | $hashlist{$hash} = $fprint; |
| 193 | } |
| 194 | |
| 195 | # Same as above except for a CRL. CRL links are of the form <hash>.r<n> |
| 196 | |
| 197 | sub link_hash_crl { |
| 198 | my $fname = $_[0]; |
Richard Levitte | caa4f47 | 2002-10-11 20:28:23 +0000 | [diff] [blame] | 199 | $fname =~ s/'/'\\''/g; |
Matthias Andree | a787c25 | 2014-09-07 18:45:02 -0400 | [diff] [blame] | 200 | my ($hash, $fprint) = `"$openssl" crl $crlhash -fingerprint -noout -in '$fname'`; |
Dr. Stephen Henson | 439df50 | 2000-05-18 00:33:00 +0000 | [diff] [blame] | 201 | chomp $hash; |
| 202 | chomp $fprint; |
| 203 | $fprint =~ s/^.*=//; |
| 204 | $fprint =~ tr/://d; |
| 205 | my $suffix = 0; |
| 206 | # Search for an unused hash filename |
| 207 | while(exists $hashlist{"$hash.r$suffix"}) { |
| 208 | # Hash matches: if fingerprint matches its a duplicate cert |
Rich Salz | ff2f6bb | 2015-09-07 22:21:38 -0400 | [diff] [blame] | 209 | if ($hashlist{"$hash.r$suffix"} eq $fprint) { |
Dr. Stephen Henson | 439df50 | 2000-05-18 00:33:00 +0000 | [diff] [blame] | 210 | print STDERR "WARNING: Skipping duplicate CRL $fname\n"; |
| 211 | return; |
| 212 | } |
| 213 | $suffix++; |
| 214 | } |
| 215 | $hash .= ".r$suffix"; |
Richard Levitte | 967d95f | 2001-04-04 15:50:30 +0000 | [diff] [blame] | 216 | if ($symlink_exists) { |
Matthias Andree | a787c25 | 2014-09-07 18:45:02 -0400 | [diff] [blame] | 217 | print "link $fname -> $hash\n" if $verbose; |
Rich Salz | ff2f6bb | 2015-09-07 22:21:38 -0400 | [diff] [blame] | 218 | symlink $fname, $hash || warn "Can't symlink, $!"; |
Richard Levitte | 967d95f | 2001-04-04 15:50:30 +0000 | [diff] [blame] | 219 | } else { |
Matthias Andree | a787c25 | 2014-09-07 18:45:02 -0400 | [diff] [blame] | 220 | print "cp $fname -> $hash\n" if $verbose; |
Rich Salz | ff2f6bb | 2015-09-07 22:21:38 -0400 | [diff] [blame] | 221 | system ("cp", $fname, $hash); |
| 222 | warn "Can't copy, $!" if ($? >> 8) != 0; |
Richard Levitte | 967d95f | 2001-04-04 15:50:30 +0000 | [diff] [blame] | 223 | } |
Dr. Stephen Henson | 439df50 | 2000-05-18 00:33:00 +0000 | [diff] [blame] | 224 | $hashlist{$hash} = $fprint; |
| 225 | } |