Tests for parsing and printing certificates containing SCTs
[openssl.git] / tools / c_rehash.in
1 #!{- $config{perl} -}
2
3 # {- join("\n# ", @autowarntext) -}
4
5 # Perl c_rehash script, scan all files in a directory
6 # and add symbolic links to their hash values.
7
8 my $dir = {- quotify1($config{openssldir}) -};
9 my $prefix = {- quotify1($config{prefix}) -};
10
11 my $errorcount = 0;
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.
21 while ( $ARGV[0] =~ /^-/ ) {
22     my $flag = shift @ARGV;
23     last if ( $flag eq '--');
24     if ( $flag eq '-old') {
25             $x509hash = "-subject_hash_old";
26             $crlhash = "-hash_old";
27     } elsif ( $flag eq '-h') {
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     }
38 }
39
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
48 eval "require Cwd";
49 if (defined(&Cwd::getcwd)) {
50         $pwd=Cwd::getcwd();
51 } else {
52         $pwd=`pwd`;
53         chomp($pwd);
54 }
55
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} : "");
59
60 if (! -x $openssl) {
61         my $found = 0;
62         foreach (split /$path_delim/, $ENV{PATH}) {
63                 if (-x "$_/$openssl") {
64                         $found = 1;
65                         $openssl = "$_/$openssl";
66                         last;
67                 }       
68         }
69         if ($found == 0) {
70                 print STDERR "c_rehash: rehashing skipped ('openssl' program not available)\n";
71                 exit 0;
72         }
73 }
74
75 if (@ARGV) {
76         @dirlist = @ARGV;
77 } elsif ($ENV{SSL_CERT_DIR}) {
78         @dirlist = split /$path_delim/, $ENV{SSL_CERT_DIR};
79 } else {
80         $dirlist[0] = "$dir/certs";
81 }
82
83 if (-d $dirlist[0]) {
84         chdir $dirlist[0];
85         $openssl="$pwd/$openssl" if (!-x $openssl);
86         chdir $pwd;
87 }
88
89 foreach (@dirlist) {
90         if (-d $_ ) {
91             if ( -w $_) {
92                 hash_dir($_);
93             } else {
94                 print "Skipping $_, can't write\n";
95                 $errorcount++;
96             }
97         }
98 }
99 exit($errorcount);
100
101 sub hash_dir {
102         my %hashlist;
103         print "Doing $_[0]\n";
104         chdir $_[0];
105         opendir(DIR, ".");
106         my @flist = readdir(DIR);
107         closedir DIR;
108         if ( $removelinks ) {
109                 # Delete any existing symbolic links
110                 foreach (grep {/^[\da-f]+\.r{0,1}\d+$/} @flist) {
111                         if (-l $_) {
112                                 print "unlink $_" if $verbose;
113                                 unlink $_ || warn "Can't unlink $_, $!\n";
114                         }
115                 }
116         }
117         FILE: foreach $fname (grep {/\.(pem)|(crt)|(cer)|(crl)$/} @flist) {
118                 # Check to see if certificates and/or CRLs present.
119                 my ($cert, $crl) = check_file($fname);
120                 if (!$cert && !$crl) {
121                         print STDERR "WARNING: $fname does not contain a certificate or CRL: skipping\n";
122                         next;
123                 }
124                 link_hash_cert($fname) if ($cert);
125                 link_hash_crl($fname) if ($crl);
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>) {
134                 if (/^-----BEGIN (.*)-----/) {
135                         my $hdr = $1;
136                         if ($hdr =~ /^(X509 |TRUSTED |)CERTIFICATE$/) {
137                                 $is_cert = 1;
138                                 last if ($is_crl);
139                         } elsif ($hdr eq "X509 CRL") {
140                                 $is_crl = 1;
141                                 last if ($is_cert);
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];
158                 $fname =~ s/'/'\\''/g;
159                 my ($hash, $fprint) = `"$openssl" x509 $x509hash -fingerprint -noout -in "$fname"`;
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
168                         if ($hashlist{"$hash.$suffix"} eq $fprint) {
169                                 print STDERR "WARNING: Skipping duplicate certificate $fname\n";
170                                 return;
171                         }
172                         $suffix++;
173                 }
174                 $hash .= ".$suffix";
175                 if ($symlink_exists) {
176                         print "link $fname -> $hash\n" if $verbose;
177                         symlink $fname, $hash || warn "Can't symlink, $!";
178                 } else {
179                         print "copy $fname -> $hash\n" if $verbose;
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                         }
191                 }
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];
199                 $fname =~ s/'/'\\''/g;
200                 my ($hash, $fprint) = `"$openssl" crl $crlhash -fingerprint -noout -in '$fname'`;
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
209                         if ($hashlist{"$hash.r$suffix"} eq $fprint) {
210                                 print STDERR "WARNING: Skipping duplicate CRL $fname\n";
211                                 return;
212                         }
213                         $suffix++;
214                 }
215                 $hash .= ".r$suffix";
216                 if ($symlink_exists) {
217                         print "link $fname -> $hash\n" if $verbose;
218                         symlink $fname, $hash || warn "Can't symlink, $!";
219                 } else {
220                         print "cp $fname -> $hash\n" if $verbose;
221                         system ("cp", $fname, $hash);
222                         warn "Can't copy, $!" if ($? >> 8) != 0;
223                 }
224                 $hashlist{$hash} = $fprint;
225 }