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