You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
4.6 KiB

  1. Author: Daniel F. Dickinson <cshored@thecshore.com>
  2. Date: Sun Jan 27 01:04:25 2019 -0500
  3. gitolite: Eliminate the need for ssh-keygen dependency
  4. Previously gitolite used ssh-keygen to generate fingerprints
  5. from OpenSSH keys to ensure non-duplication of keys when
  6. processing them to create / manage user ssh access to the
  7. git repositories. This ends up depending on openssl,
  8. which is large and unnecessary when we are running on an
  9. embedded distro such as OpenWrt.
  10. Signed-off-by: Daniel F. Dickinson <cshored@thecshore.com>
  11. --- a/src/lib/Gitolite/Common.pm
  12. +++ b/src/lib/Gitolite/Common.pm
  13. @@ -26,6 +26,8 @@ package Gitolite::Common;
  14. use Exporter 'import';
  15. use File::Path qw(mkpath);
  16. use File::Temp qw(tempfile);
  17. +use MIME::Base64 qw(decode_base64);
  18. +use Digest::SHA qw(sha256_base64);
  19. use Carp qw(carp cluck croak confess);
  20. use strict;
  21. @@ -352,43 +354,82 @@ sub logger_plus_stderr {
  22. }
  23. # ----------------------------------------------------------------------
  24. +# Decode OpenSSH key
  25. +# If the key cannot be parsed it will be undef
  26. +# Returns (algorithm_name, algo_data1, algo_data2, ...)
  27. +sub ssh_decode_key($) {
  28. + my $key = shift;
  29. + my $keydata = decode_base64($key);
  30. + my @keyparts = ();
  31. + my $partlen;
  32. + my $algorithm;
  33. + my $data;
  34. + my $pos = 0;
  35. + $partlen = unpack('N', substr $keydata, $pos, 4) or return undef;
  36. + $algorithm = substr $keydata, $pos + 4, $partlen or return undef;
  37. + $pos = $pos + 4 + $partlen;
  38. + while ( $pos <= length($keydata) ) {
  39. + $partlen = unpack('N', substr $keydata, $pos, 4) or last;
  40. + $data = unpack('s>*', substr $keydata, $pos + 4, 4) or last;
  41. + $pos = $pos + 4 + $partlen;
  42. + push @keyparts, $data;
  43. + }
  44. + return ( $algorithm, @keyparts );
  45. +}
  46. +
  47. +# ----------------------------------------------------------------------
  48. +# Parse OpenSSH line
  49. +# If the file cannot be parsed it will be undef
  50. +# Returns (restrictions, algorithm, PEMkey, comment)
  51. +sub ssh_parse_line($) {
  52. + my $ssh_line = shift;
  53. + my @ssh_parts = split / /, $ssh_line, 5;
  54. + if (scalar @ssh_parts < 4) {
  55. + @ssh_parts = ('', @ssh_parts);
  56. + }
  57. + if (scalar @ssh_parts > 4) {
  58. + @ssh_parts = @ssh_parts[0,3]
  59. + }
  60. + if (scalar @ssh_parts < 4) {
  61. + @ssh_parts = undef;
  62. + }
  63. + return ( @ssh_parts );
  64. +}
  65. +
  66. +# ----------------------------------------------------------------------
  67. +# Get the SSH fingerprint of a line of text
  68. +# If the fingerprint cannot be parsed, it will be undef
  69. +# In a scalar context, returns the fingerprint
  70. +# In a list context, returns (fingerprint, output) where output
  71. +# is the parsed input line (less algorithm)
  72. +sub ssh_fingerprint_line($) {
  73. + my $ssh_line = shift;
  74. + my @parsed_line = ssh_parse_line($ssh_line) or return undef;
  75. + my @ssh_parts = ssh_decode_key($parsed_line[2]) or return undef;
  76. + ( $parsed_line[1] eq $ssh_parts[0] ) or die "algorithm mismatch: $parsed_line[1] vs. $ssh_parts[0]";
  77. + my $fp = sha256_base64(join(' ', @ssh_parts[1,-1]));
  78. + return wantarray ? ($fp, join(' ', @ssh_parts[1,-1])) : $fp;
  79. +}
  80. +
  81. +# ----------------------------------------------------------------------
  82. # Get the SSH fingerprint of a file
  83. # If the fingerprint cannot be parsed, it will be undef
  84. # In a scalar context, returns the fingerprint
  85. # In a list context, returns (fingerprint, output) where output
  86. -# is the raw output of the ssh-keygen command
  87. -sub ssh_fingerprint_file {
  88. +# is the raw input line
  89. +sub ssh_fingerprint_file($) {
  90. my $in = shift;
  91. -f $in or die "file not found: $in\n";
  92. my $fh;
  93. - open( $fh, "ssh-keygen -l -f $in |" ) or die "could not fork: $!\n";
  94. + open( $fh, $in ) or die "could not open $in: $!\n";
  95. my $output = <$fh>;
  96. chomp $output;
  97. - # dbg("fp = $fp");
  98. close $fh;
  99. # Return a valid fingerprint or undef
  100. - my $fp = undef;
  101. - if($output =~ /((?:MD5:)?(?:[0-9a-f]{2}:){15}[0-9a-f]{2})/i or
  102. - $output =~ m{((?:RIPEMD|SHA)\d+:[A-Za-z0-9+/=]+)}i) {
  103. - $fp = $1;
  104. - }
  105. + my $fp = ssh_fingerprint_line($output);
  106. return wantarray ? ($fp, $output) : $fp;
  107. }
  108. -# Get the SSH fingerprint of a line of text
  109. -# If the fingerprint cannot be parsed, it will be undef
  110. -# In a scalar context, returns the fingerprint
  111. -# In a list context, returns (fingerprint, output) where output
  112. -# is the raw output of the ssh-keygen command
  113. -sub ssh_fingerprint_line {
  114. - my ( $fh, $fn ) = tempfile();
  115. - print $fh shift() . "\n";
  116. - close $fh;
  117. - my ($fp,$output) = ssh_fingerprint_file($fn);
  118. - unlink $fn;
  119. - return wantarray ? ($fp,$output) : $fp;
  120. -}
  121. -
  122. # ----------------------------------------------------------------------
  123. # bare-minimum subset of 'Tsh' (see github.com/sitaramc/tsh)