c_rehash 5.0 KB

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