c_rehash.in 6.7 KB

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