blob: 421fd892086f9344822e74e1ac8b5bd836364548 [file] [log] [blame]
Richard Levitteabe256e2018-03-06 20:35:30 +01001#!{- $config{HASHBANGPERL} -}
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002
Richard Levitte9ab6fc52016-01-25 21:19:59 +01003# {- join("\n# ", @autowarntext) -}
Matt Caswellb0edda12018-03-20 13:00:17 +00004# Copyright 1999-2018 The OpenSSL Project Authors. All Rights Reserved.
Rich Salz44c8a5e2016-06-01 11:26:40 -04005#
6# Licensed under the OpenSSL license (the "License"). You may not use
7# this file except in compliance with the License. You can obtain a copy
8# in the file LICENSE in the source distribution or at
9# https://www.openssl.org/source/license.html
Richard Levitte9ab6fc52016-01-25 21:19:59 +010010
Dr. Stephen Henson439df502000-05-18 00:33:00 +000011# Perl c_rehash script, scan all files in a directory
12# and add symbolic links to their hash values.
Ralf S. Engelschall13e91dd1998-12-22 15:59:57 +000013
Richard Levitte291e94d2015-05-18 22:35:23 +020014my $dir = {- quotify1($config{openssldir}) -};
15my $prefix = {- quotify1($config{prefix}) -};
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +000016
Rich Salz4c7103a2015-09-10 11:46:13 -040017my $errorcount = 0;
Matthias Andreea787c252014-09-07 18:45:02 -040018my $openssl = $ENV{OPENSSL} || "openssl";
19my $pwd;
20my $x509hash = "-subject_hash";
21my $crlhash = "-hash";
22my $verbose = 0;
23my $symlink_exists=eval {symlink("",""); 1};
24my $removelinks = 1;
25
26## Parse flags.
Olaf Johansson8846adb2015-06-02 07:41:35 -040027while ( $ARGV[0] =~ /^-/ ) {
Matthias Andreea787c252014-09-07 18:45:02 -040028 my $flag = shift @ARGV;
29 last if ( $flag eq '--');
Olaf Johansson8846adb2015-06-02 07:41:35 -040030 if ( $flag eq '-old') {
Matthias Andreea787c252014-09-07 18:45:02 -040031 $x509hash = "-subject_hash_old";
32 $crlhash = "-hash_old";
Rich Salz7d959c32016-09-12 11:29:22 -040033 } elsif ( $flag eq '-h' || $flag eq '-help' ) {
Matthias Andreea787c252014-09-07 18:45:02 -040034 help();
35 } elsif ( $flag eq '-n' ) {
36 $removelinks = 0;
37 } elsif ( $flag eq '-v' ) {
38 $verbose++;
39 }
40 else {
Rich Salz7d959c32016-09-12 11:29:22 -040041 print STDERR "Usage error; try -h.\n";
Matthias Andreea787c252014-09-07 18:45:02 -040042 exit 1;
43 }
Dr. Stephen Henson439df502000-05-18 00:33:00 +000044}
45
Matthias Andreea787c252014-09-07 18:45:02 -040046sub help {
Rich Salz7d959c32016-09-12 11:29:22 -040047 print "Usage: c_rehash [-old] [-h] [-help] [-v] [dirs...]\n";
Matthias Andreea787c252014-09-07 18:45:02 -040048 print " -old use old-style digest\n";
Rich Salz7d959c32016-09-12 11:29:22 -040049 print " -h or -help print this help text\n";
Matthias Andreea787c252014-09-07 18:45:02 -040050 print " -v print files removed and linked\n";
51 exit 0;
52}
53
Andy Polyakova2688c82006-10-26 10:52:12 +000054eval "require Cwd";
55if (defined(&Cwd::getcwd)) {
56 $pwd=Cwd::getcwd();
57} else {
Matthias Andreea787c252014-09-07 18:45:02 -040058 $pwd=`pwd`;
59 chomp($pwd);
Andy Polyakova2688c82006-10-26 10:52:12 +000060}
Andy Polyakovd8cdd152006-10-21 16:28:03 +000061
Matthias Andreea787c252014-09-07 18:45:02 -040062# DOS/Win32 or Unix delimiter? Prefix our installdir, then search.
63my $path_delim = ($pwd =~ /^[a-z]\:/i) ? ';' : ':';
64$ENV{PATH} = "$prefix/bin" . ($ENV{PATH} ? $path_delim . $ENV{PATH} : "");
Dr. Stephen Henson439df502000-05-18 00:33:00 +000065
Rich Salzff2f6bb2015-09-07 22:21:38 -040066if (! -x $openssl) {
Dr. Stephen Henson439df502000-05-18 00:33:00 +000067 my $found = 0;
Andy Polyakovd8cdd152006-10-21 16:28:03 +000068 foreach (split /$path_delim/, $ENV{PATH}) {
Rich Salzff2f6bb2015-09-07 22:21:38 -040069 if (-x "$_/$openssl") {
Dr. Stephen Henson439df502000-05-18 00:33:00 +000070 $found = 1;
Dr. Stephen Hensonef236ec2009-04-23 16:32:42 +000071 $openssl = "$_/$openssl";
Dr. Stephen Henson439df502000-05-18 00:33:00 +000072 last;
73 }
74 }
Rich Salzff2f6bb2015-09-07 22:21:38 -040075 if ($found == 0) {
Dr. Stephen Henson439df502000-05-18 00:33:00 +000076 print STDERR "c_rehash: rehashing skipped ('openssl' program not available)\n";
77 exit 0;
78 }
79}
80
Rich Salzff2f6bb2015-09-07 22:21:38 -040081if (@ARGV) {
Dr. Stephen Henson439df502000-05-18 00:33:00 +000082 @dirlist = @ARGV;
Rich Salzff2f6bb2015-09-07 22:21:38 -040083} elsif ($ENV{SSL_CERT_DIR}) {
Andy Polyakovd8cdd152006-10-21 16:28:03 +000084 @dirlist = split /$path_delim/, $ENV{SSL_CERT_DIR};
Dr. Stephen Henson439df502000-05-18 00:33:00 +000085} else {
86 $dirlist[0] = "$dir/certs";
87}
88
Andy Polyakovd8cdd152006-10-21 16:28:03 +000089if (-d $dirlist[0]) {
90 chdir $dirlist[0];
91 $openssl="$pwd/$openssl" if (!-x $openssl);
92 chdir $pwd;
93}
Dr. Stephen Henson439df502000-05-18 00:33:00 +000094
95foreach (@dirlist) {
Rich Salzff2f6bb2015-09-07 22:21:38 -040096 if (-d $_ ) {
97 if ( -w $_) {
Dr. Stephen Henson439df502000-05-18 00:33:00 +000098 hash_dir($_);
Rich Salzff2f6bb2015-09-07 22:21:38 -040099 } else {
100 print "Skipping $_, can't write\n";
Rich Salz4c7103a2015-09-10 11:46:13 -0400101 $errorcount++;
Rich Salzff2f6bb2015-09-07 22:21:38 -0400102 }
Dr. Stephen Henson439df502000-05-18 00:33:00 +0000103 }
104}
Rich Salz4c7103a2015-09-10 11:46:13 -0400105exit($errorcount);
Dr. Stephen Henson439df502000-05-18 00:33:00 +0000106
107sub hash_dir {
108 my %hashlist;
109 print "Doing $_[0]\n";
110 chdir $_[0];
Rich Salz6f46c3c2014-09-11 13:08:30 -0400111 opendir(DIR, ".");
Rich Salzb1ffe8d2016-05-25 08:59:10 -0400112 my @flist = sort readdir(DIR);
Rich Salz6f46c3c2014-09-11 13:08:30 -0400113 closedir DIR;
Matthias Andreea787c252014-09-07 18:45:02 -0400114 if ( $removelinks ) {
Matthias Andreea787c252014-09-07 18:45:02 -0400115 # Delete any existing symbolic links
116 foreach (grep {/^[\da-f]+\.r{0,1}\d+$/} @flist) {
Rich Salzff2f6bb2015-09-07 22:21:38 -0400117 if (-l $_) {
Matthias Andreea787c252014-09-07 18:45:02 -0400118 print "unlink $_" if $verbose;
Rich Salzff2f6bb2015-09-07 22:21:38 -0400119 unlink $_ || warn "Can't unlink $_, $!\n";
Matthias Andreea787c252014-09-07 18:45:02 -0400120 }
Dr. Stephen Henson439df502000-05-18 00:33:00 +0000121 }
122 }
Matthias Andreea787c252014-09-07 18:45:02 -0400123 FILE: foreach $fname (grep {/\.(pem)|(crt)|(cer)|(crl)$/} @flist) {
Dr. Stephen Henson439df502000-05-18 00:33:00 +0000124 # Check to see if certificates and/or CRLs present.
125 my ($cert, $crl) = check_file($fname);
Rich Salzff2f6bb2015-09-07 22:21:38 -0400126 if (!$cert && !$crl) {
Dr. Stephen Henson439df502000-05-18 00:33:00 +0000127 print STDERR "WARNING: $fname does not contain a certificate or CRL: skipping\n";
128 next;
129 }
Rich Salzff2f6bb2015-09-07 22:21:38 -0400130 link_hash_cert($fname) if ($cert);
131 link_hash_crl($fname) if ($crl);
Dr. Stephen Henson439df502000-05-18 00:33:00 +0000132 }
133}
134
135sub check_file {
136 my ($is_cert, $is_crl) = (0,0);
137 my $fname = $_[0];
138 open IN, $fname;
139 while(<IN>) {
Rich Salzff2f6bb2015-09-07 22:21:38 -0400140 if (/^-----BEGIN (.*)-----/) {
Dr. Stephen Henson439df502000-05-18 00:33:00 +0000141 my $hdr = $1;
Rich Salzff2f6bb2015-09-07 22:21:38 -0400142 if ($hdr =~ /^(X509 |TRUSTED |)CERTIFICATE$/) {
Dr. Stephen Henson439df502000-05-18 00:33:00 +0000143 $is_cert = 1;
Rich Salzff2f6bb2015-09-07 22:21:38 -0400144 last if ($is_crl);
145 } elsif ($hdr eq "X509 CRL") {
Dr. Stephen Henson439df502000-05-18 00:33:00 +0000146 $is_crl = 1;
Rich Salzff2f6bb2015-09-07 22:21:38 -0400147 last if ($is_cert);
Dr. Stephen Henson439df502000-05-18 00:33:00 +0000148 }
149 }
150 }
151 close IN;
152 return ($is_cert, $is_crl);
153}
154
155
156# Link a certificate to its subject name hash value, each hash is of
157# the form <hash>.<n> where n is an integer. If the hash value already exists
158# then we need to up the value of n, unless its a duplicate in which
159# case we skip the link. We check for duplicates by comparing the
160# certificate fingerprints
161
162sub link_hash_cert {
163 my $fname = $_[0];
Richard Levitteb7910992002-10-11 11:34:20 +0000164 $fname =~ s/'/'\\''/g;
Matthias Andreea787c252014-09-07 18:45:02 -0400165 my ($hash, $fprint) = `"$openssl" x509 $x509hash -fingerprint -noout -in "$fname"`;
Dr. Stephen Henson439df502000-05-18 00:33:00 +0000166 chomp $hash;
167 chomp $fprint;
168 $fprint =~ s/^.*=//;
169 $fprint =~ tr/://d;
170 my $suffix = 0;
171 # Search for an unused hash filename
172 while(exists $hashlist{"$hash.$suffix"}) {
173 # Hash matches: if fingerprint matches its a duplicate cert
Rich Salzff2f6bb2015-09-07 22:21:38 -0400174 if ($hashlist{"$hash.$suffix"} eq $fprint) {
Dr. Stephen Henson439df502000-05-18 00:33:00 +0000175 print STDERR "WARNING: Skipping duplicate certificate $fname\n";
176 return;
177 }
178 $suffix++;
179 }
180 $hash .= ".$suffix";
Richard Levitte967d95f2001-04-04 15:50:30 +0000181 if ($symlink_exists) {
Matthias Andreea787c252014-09-07 18:45:02 -0400182 print "link $fname -> $hash\n" if $verbose;
Rich Salzff2f6bb2015-09-07 22:21:38 -0400183 symlink $fname, $hash || warn "Can't symlink, $!";
Richard Levitte967d95f2001-04-04 15:50:30 +0000184 } else {
Matthias Andreea787c252014-09-07 18:45:02 -0400185 print "copy $fname -> $hash\n" if $verbose;
Rich Salzff2f6bb2015-09-07 22:21:38 -0400186 if (open($in, "<", $fname)) {
187 if (open($out,">", $hash)) {
188 print $out $_ while (<$in>);
189 close $out;
190 } else {
191 warn "can't open $hash for write, $!";
192 }
193 close $in;
194 } else {
195 warn "can't open $fname for read, $!";
196 }
Richard Levitte967d95f2001-04-04 15:50:30 +0000197 }
Dr. Stephen Henson439df502000-05-18 00:33:00 +0000198 $hashlist{$hash} = $fprint;
199}
200
201# Same as above except for a CRL. CRL links are of the form <hash>.r<n>
202
203sub link_hash_crl {
204 my $fname = $_[0];
Richard Levittecaa4f472002-10-11 20:28:23 +0000205 $fname =~ s/'/'\\''/g;
Matthias Andreea787c252014-09-07 18:45:02 -0400206 my ($hash, $fprint) = `"$openssl" crl $crlhash -fingerprint -noout -in '$fname'`;
Dr. Stephen Henson439df502000-05-18 00:33:00 +0000207 chomp $hash;
208 chomp $fprint;
209 $fprint =~ s/^.*=//;
210 $fprint =~ tr/://d;
211 my $suffix = 0;
212 # Search for an unused hash filename
213 while(exists $hashlist{"$hash.r$suffix"}) {
214 # Hash matches: if fingerprint matches its a duplicate cert
Rich Salzff2f6bb2015-09-07 22:21:38 -0400215 if ($hashlist{"$hash.r$suffix"} eq $fprint) {
Dr. Stephen Henson439df502000-05-18 00:33:00 +0000216 print STDERR "WARNING: Skipping duplicate CRL $fname\n";
217 return;
218 }
219 $suffix++;
220 }
221 $hash .= ".r$suffix";
Richard Levitte967d95f2001-04-04 15:50:30 +0000222 if ($symlink_exists) {
Matthias Andreea787c252014-09-07 18:45:02 -0400223 print "link $fname -> $hash\n" if $verbose;
Rich Salzff2f6bb2015-09-07 22:21:38 -0400224 symlink $fname, $hash || warn "Can't symlink, $!";
Richard Levitte967d95f2001-04-04 15:50:30 +0000225 } else {
Matthias Andreea787c252014-09-07 18:45:02 -0400226 print "cp $fname -> $hash\n" if $verbose;
Rich Salzff2f6bb2015-09-07 22:21:38 -0400227 system ("cp", $fname, $hash);
228 warn "Can't copy, $!" if ($? >> 8) != 0;
Richard Levitte967d95f2001-04-04 15:50:30 +0000229 }
Dr. Stephen Henson439df502000-05-18 00:33:00 +0000230 $hashlist{$hash} = $fprint;
231}