c_rehash.pl 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #!/usr/bin/env perl
  2. # WARNING: do not edit!
  3. # Generated by makefile from tools\c_rehash.in
  4. # Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
  5. #
  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
  10. # Perl c_rehash script, scan all files in a directory
  11. # and add symbolic links to their hash values.
  12. my $dir = "";
  13. my $prefix = "D:\\Tests\\KoljaStrohm-Games\\OpenSSL\\x64\\Debug";
  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 hash_dir {
  96. my %hashlist;
  97. print "Doing $_[0]\n";
  98. chdir $_[0];
  99. opendir(DIR, ".");
  100. my @flist = sort readdir(DIR);
  101. closedir DIR;
  102. if ( $removelinks ) {
  103. # Delete any existing symbolic links
  104. foreach (grep {/^[\da-f]+\.r{0,1}\d+$/} @flist) {
  105. if (-l $_) {
  106. print "unlink $_" if $verbose;
  107. unlink $_ || warn "Can't unlink $_, $!\n";
  108. }
  109. }
  110. }
  111. FILE: foreach $fname (grep {/\.(pem)|(crt)|(cer)|(crl)$/} @flist) {
  112. # Check to see if certificates and/or CRLs present.
  113. my ($cert, $crl) = check_file($fname);
  114. if (!$cert && !$crl) {
  115. print STDERR "WARNING: $fname does not contain a certificate or CRL: skipping\n";
  116. next;
  117. }
  118. link_hash_cert($fname) if ($cert);
  119. link_hash_crl($fname) if ($crl);
  120. }
  121. }
  122. sub check_file {
  123. my ($is_cert, $is_crl) = (0,0);
  124. my $fname = $_[0];
  125. open IN, $fname;
  126. while(<IN>) {
  127. if (/^-----BEGIN (.*)-----/) {
  128. my $hdr = $1;
  129. if ($hdr =~ /^(X509 |TRUSTED |)CERTIFICATE$/) {
  130. $is_cert = 1;
  131. last if ($is_crl);
  132. } elsif ($hdr eq "X509 CRL") {
  133. $is_crl = 1;
  134. last if ($is_cert);
  135. }
  136. }
  137. }
  138. close IN;
  139. return ($is_cert, $is_crl);
  140. }
  141. # Link a certificate to its subject name hash value, each hash is of
  142. # the form <hash>.<n> where n is an integer. If the hash value already exists
  143. # then we need to up the value of n, unless its a duplicate in which
  144. # case we skip the link. We check for duplicates by comparing the
  145. # certificate fingerprints
  146. sub link_hash_cert {
  147. my $fname = $_[0];
  148. $fname =~ s/'/'\\''/g;
  149. my ($hash, $fprint) = `"$openssl" x509 $x509hash -fingerprint -noout -in "$fname"`;
  150. chomp $hash;
  151. chomp $fprint;
  152. $fprint =~ s/^.*=//;
  153. $fprint =~ tr/://d;
  154. my $suffix = 0;
  155. # Search for an unused hash filename
  156. while(exists $hashlist{"$hash.$suffix"}) {
  157. # Hash matches: if fingerprint matches its a duplicate cert
  158. if ($hashlist{"$hash.$suffix"} eq $fprint) {
  159. print STDERR "WARNING: Skipping duplicate certificate $fname\n";
  160. return;
  161. }
  162. $suffix++;
  163. }
  164. $hash .= ".$suffix";
  165. if ($symlink_exists) {
  166. print "link $fname -> $hash\n" if $verbose;
  167. symlink $fname, $hash || warn "Can't symlink, $!";
  168. } else {
  169. print "copy $fname -> $hash\n" if $verbose;
  170. if (open($in, "<", $fname)) {
  171. if (open($out,">", $hash)) {
  172. print $out $_ while (<$in>);
  173. close $out;
  174. } else {
  175. warn "can't open $hash for write, $!";
  176. }
  177. close $in;
  178. } else {
  179. warn "can't open $fname for read, $!";
  180. }
  181. }
  182. $hashlist{$hash} = $fprint;
  183. }
  184. # Same as above except for a CRL. CRL links are of the form <hash>.r<n>
  185. sub link_hash_crl {
  186. my $fname = $_[0];
  187. $fname =~ s/'/'\\''/g;
  188. my ($hash, $fprint) = `"$openssl" crl $crlhash -fingerprint -noout -in '$fname'`;
  189. chomp $hash;
  190. chomp $fprint;
  191. $fprint =~ s/^.*=//;
  192. $fprint =~ tr/://d;
  193. my $suffix = 0;
  194. # Search for an unused hash filename
  195. while(exists $hashlist{"$hash.r$suffix"}) {
  196. # Hash matches: if fingerprint matches its a duplicate cert
  197. if ($hashlist{"$hash.r$suffix"} eq $fprint) {
  198. print STDERR "WARNING: Skipping duplicate CRL $fname\n";
  199. return;
  200. }
  201. $suffix++;
  202. }
  203. $hash .= ".r$suffix";
  204. if ($symlink_exists) {
  205. print "link $fname -> $hash\n" if $verbose;
  206. symlink $fname, $hash || warn "Can't symlink, $!";
  207. } else {
  208. print "cp $fname -> $hash\n" if $verbose;
  209. system ("cp", $fname, $hash);
  210. warn "Can't copy, $!" if ($? >> 8) != 0;
  211. }
  212. $hashlist{$hash} = $fprint;
  213. }