| 1 | ################################################################ | ||
| 2 | # | ||
| 3 | # Copyright (c) 2022 SUSE LLC | ||
| 4 | # | ||
| 5 | # This program is free software; you can redistribute it and/or modify | ||
| 6 | # it under the terms of the GNU General Public License version 2 or 3 as | ||
| 7 | # published by the Free Software Foundation. | ||
| 8 | # | ||
| 9 | # This program is distributed in the hope that it will be useful, | ||
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 12 | # GNU General Public License for more details. | ||
| 13 | # | ||
| 14 | # You should have received a copy of the GNU General Public License | ||
| 15 | # along with this program (see the file COPYING); if not, write to the | ||
| 16 | # Free Software Foundation, Inc., | ||
| 17 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA | ||
| 18 | # | ||
| 19 | ################################################################ | ||
| 20 | |||
| 21 | package Net::OBS::SigAuth; | ||
| 22 | |||
| 23 | use MIME::Base64 (); | ||
| 24 | use File::Temp qw/tempfile/; | ||
| 25 | use Data::Dumper; | ||
| 26 | |||
| 27 | use strict; | ||
| 28 | |||
| 29 | sub dosshsign { | ||
| 30 | my ($signdata, $keyfile, $namespace) = @_; | ||
| 31 | die("no key file specified\n") unless $keyfile; | ||
| 32 | my $out = _execute_cmd(['ssh-keygen', '-Y', 'sign', '-n', $namespace, '-f', $keyfile], $signdata); | ||
| 33 | die("Signature authentification: bad ssh signature format\n") unless $out =~ s/.*-----BEGIN SSH SIGNATURE-----\n//s; | ||
| 34 | die("Signature authentification: bad ssh signature format\n") unless $out =~ s/-----END SSH SIGNATURE-----.*//s; | ||
| 35 | my $sig = MIME::Base64::decode_base64($out); | ||
| 36 | die("Signature authentification: bad ssh signature\n") unless substr($sig, 0, 6) eq 'SSHSIG'; | ||
| 37 | return $sig; | ||
| 38 | } | ||
| 39 | |||
| 40 | sub _execute_cmd { | ||
| 41 | my ($cmd, $data) = @_; | ||
| 42 | my $fh; | ||
| 43 | my $pid = open($fh, '-|'); | ||
| 44 | die("pipe open: $!\n") unless defined $pid; | ||
| 45 | if (!$pid) { | ||
| 46 | if ($data) { | ||
| 47 | my $pid2 = open(STDIN, '-|'); | ||
| 48 | die("pipe open: $!\n") unless defined $pid2; | ||
| 49 | if (!$pid2) { | ||
| 50 | print STDOUT $data or die("write data: $!\n"); | ||
| 51 | exit 0; | ||
| 52 | } | ||
| 53 | } | ||
| 54 | exec(@$cmd); | ||
| 55 | die("@$cmd: $!\n"); | ||
| 56 | } | ||
| 57 | my $out = ''; | ||
| 58 | 1 while sysread($fh, $out, 8192, length($out)); | ||
| 59 | return $out; | ||
| 60 | } | ||
| 61 | |||
| 62 | sub _get_tmp_keyfile_from_agent { | ||
| 63 | my ($keyid) = @_; | ||
| 64 | my ($fh, $filename) = tempfile(); | ||
| 65 | my @keys = split("\n", _execute_cmd(['ssh-add','-L'])); | ||
| 66 | my $agent_key; | ||
| 67 | my $kl; | ||
| 68 | if ($keyid) { | ||
| 69 | for my $key_line (@keys) { | ||
| 70 | my ($type, $key, $id) = split(" ", $key_line); | ||
| 71 | $kl = $key_line if $id eq $keyid; | ||
| 72 | } | ||
| 73 | } else { | ||
| 74 | $kl = $keys[0]; | ||
| 75 | } | ||
| 76 | return if ! $kl; | ||
| 77 | print $fh $kl || die "Could not write to $filename: $!\n"; | ||
| 78 | close $fh || die "Could not close $filename: $!\n"; | ||
| 79 | return $filename | ||
| 80 | } | ||
| 81 | |||
| 82 | |||
| 83 | sub generate_authorization { | ||
| 84 | my ($auth_param, $keyid, $keyfile) = @_; | ||
| 85 | my $realm = $auth_param->{'realm'} || ''; | ||
| 86 | my $headers = $auth_param->{'headers'} || '(created)'; | ||
| 87 | my $created = time(); | ||
| 88 | my $tosign = ''; | ||
| 89 | for my $h (split(/ /, $headers)) { | ||
| 90 | if ($h eq '(created)') { | ||
| 91 | $tosign .= "(created): $created\n"; | ||
| 92 | } else { | ||
| 93 | die("Signature authentication: unsupported header element: $h\n"); | ||
| 94 | } | ||
| 95 | } | ||
| 96 | die("Signature authentication: no keyid specified\n") unless defined($keyid); | ||
| 97 | die("Signature authentication: nothing to sign?\n") unless $tosign; | ||
| 98 | chop $tosign; | ||
| 99 | my $algorithm = $auth_param->{'algorithm'} || 'ssh'; | ||
| 100 | die("Signature authentication: unsupported algorithm '$algorithm'\n") unless $algorithm eq 'ssh'; | ||
| 101 | my $sig = dosshsign($tosign, $keyfile, $realm); | ||
| 102 | $sig = MIME::Base64::encode_base64($sig, ''); | ||
| 103 | die("bad keyid '$keyid'\n") if $keyid =~ /\"/; | ||
| 104 | return "Signature keyId=\"$keyid\",algorithm=\"$algorithm\",headers=\"$headers\",created=$created,signature=\"$sig\""; | ||
| 105 | } | ||
| 106 | |||
| 107 | sub get_key_data { | ||
| 108 | my ($uri, $creds) = @_; | ||
| 109 | my $keyid = $::ENV{SSH_PUB_KEY_ID} || $creds->{keyid}; | ||
| 110 | my $auth_type = $creds->{auth_type} || 'agent'; | ||
| 111 | my $keyfile; | ||
| 112 | if ($auth_type eq 'agent' ) { | ||
| 113 | $keyfile = _get_tmp_keyfile_from_agent($keyid); | ||
| 114 | die 'No key (keyid: '.($keyid||'NONE').') found in ssh-agent and nofallback expicitly configured!' if !$keyfile and $creds->{nofallback}; | ||
| 115 | } | ||
| 116 | my $username = $creds->{user} || $keyid; | ||
| 117 | my $authority = $uri->authority; | ||
| 118 | if ($authority =~ s/^([^\@]*)\@//) { | ||
| 119 | $username = $1; | ||
| 120 | $username =~ s/:.*//; # ignore password | ||
| 121 | } | ||
| 122 | if (!defined($keyfile) && $creds->{keyfile}) { | ||
| 123 | $keyfile = $creds->{keyfile}; | ||
| 124 | die "Key file '$keyfile' doesn't exit and nofallback expicitly configured!" if !(-e $keyfile) && $creds->{nofallback}; | ||
| 125 | }; | ||
| 126 | if (!defined($keyfile)) { | ||
| 127 | my @_dirs = ("$ENV{'HOME'}/.ssh"); | ||
| 128 | unshift @_dirs, $creds->{keydir} if ($creds->{keydir}); | ||
| 129 | for my $_dir (@_dirs) { | ||
| 130 | $_dir =~ s#/$##; | ||
| 131 | if (-d "$_dir") { | ||
| 132 | for my $idfile (qw{id_ed25519 id_rsa}) { | ||
| 133 | next unless -s "$_dir/$idfile"; | ||
| 134 | $keyfile = "$_dir/$idfile"; | ||
| 135 | last; | ||
| 136 | } | ||
| 137 | } else { | ||
| 138 | die "Key dir '$_dir' does not exists and nofallback expicitly configured!" if $creds->{nofallback}; | ||
| 139 | } | ||
| 140 | } | ||
| 141 | } | ||
| 142 | die "No keyfile found!" unless $keyfile; | ||
| 143 | return ($username, $keyid, $keyfile); | ||
| 144 | } | ||
| 145 | |||
| 146 | sub authenticate { | ||
| 147 | my ($class, $ua, $proxy, $auth_param, $response, $request, $arg, $size) = @_; | ||
| 148 | my $uri = $request->uri->canonical; | ||
| 149 | return $response unless $uri && !$proxy; | ||
| 150 | my ($username, $keyid, $keyfile) = get_key_data($uri, $ua->sigauth_credentials); | ||
| 151 | my $host_port = $uri->host_port; | ||
| 152 | my $auth = generate_authorization($auth_param, $username, $keyfile); | ||
| 153 | my $h = $ua->get_my_handler('request_prepare', 'm_host_port' => $host_port, sub { | ||
| 154 | $_[0]{callback} = sub { $_[0]->header('Authorization' => $auth) }; | ||
| 155 | }); | ||
| 156 | my $fin = $ua->request($request->clone, $arg, $size, $response); | ||
| 157 | return $fin; | ||
| 158 | } | ||
| 159 | |||
| 160 | # install handler | ||
| 161 | no warnings; | ||
| 162 | *LWP::Authen::Signature::authenticate = \&authenticate; | ||
| 163 | |||
| 164 | 1; |