| 1 | package Net::SSH::Perl::Agent; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | use IO::Socket; | ||
| 6 | use Carp qw( croak ); | ||
| 7 | use Net::SSH::Perl::Constants qw( :agent SSH_COM_AGENT2_FAILURE ); | ||
| 8 | use Net::SSH::Perl::Buffer; | ||
| 9 | use Net::SSH::Perl::Key; | ||
| 10 | |||
| 11 | sub new { | ||
| 12 | my $class = shift; | ||
| 13 | my $agent = bless {}, $class; | ||
| 14 | $agent->init(@_); | ||
| 15 | } | ||
| 16 | |||
| 17 | sub init { | ||
| 18 | my $agent = shift; | ||
| 19 | my($version) = @_; | ||
| 20 | $agent->{sock} = $agent->create_socket or return; | ||
| 21 | $agent->{version} = $version; | ||
| 22 | $agent; | ||
| 23 | } | ||
| 24 | |||
| 25 | sub create_socket { | ||
| 26 | my $agent = shift; | ||
| 27 | my $authsock = $ENV{"SSH_AUTH_SOCK"} or return; | ||
| 28 | |||
| 29 | $agent->{sock} = IO::Socket::UNIX->new( | ||
| 30 | Type => SOCK_STREAM, | ||
| 31 | Peer => $authsock | ||
| 32 | ) or return; | ||
| 33 | } | ||
| 34 | |||
| 35 | sub request { | ||
| 36 | my $agent = shift; | ||
| 37 | my($req) = @_; | ||
| 38 | my $len = pack "N", $req->length; | ||
| 39 | my $sock = $agent->{sock}; | ||
| 40 | (syswrite($sock, $len, 4) == 4 and | ||
| 41 | syswrite($sock, $req->bytes, $req->length) == $req->length) or | ||
| 42 | croak "Error writing to auth socket."; | ||
| 43 | $len = 4; | ||
| 44 | my $buf; | ||
| 45 | while ($len > 0) { | ||
| 46 | my $l = sysread $sock, $buf, $len; | ||
| 47 | croak "Error reading response length from auth socket." unless $l > 0; | ||
| 48 | $len -= $l; | ||
| 49 | } | ||
| 50 | $len = unpack "N", $buf; | ||
| 51 | croak "Auth response too long: $len" if $len > 256 * 1024; | ||
| 52 | |||
| 53 | $buf = Net::SSH::Perl::Buffer->new( MP => "SSH$agent->{version}" ); | ||
| 54 | while ($buf->length < $len) { | ||
| 55 | my $b; | ||
| 56 | my $l = sysread $sock, $b, $len; | ||
| 57 | croak "Error reading response from auth socket." unless $l > 0; | ||
| 58 | $buf->append($b); | ||
| 59 | } | ||
| 60 | $buf; | ||
| 61 | } | ||
| 62 | |||
| 63 | sub num_left { $_[0]->{num} } | ||
| 64 | |||
| 65 | sub num_identities { | ||
| 66 | my $agent = shift; | ||
| 67 | my($type1, $type2) = $agent->{version} == 2 ? | ||
| 68 | (SSH2_AGENTC_REQUEST_IDENTITIES, SSH2_AGENT_IDENTITIES_ANSWER) : | ||
| 69 | (SSH_AGENTC_REQUEST_RSA_IDENTITIES, SSH_AGENT_RSA_IDENTITIES_ANSWER); | ||
| 70 | |||
| 71 | my $r = Net::SSH::Perl::Buffer->new( MP => "SSH$agent->{version}" ); | ||
| 72 | $r->put_int8($type1); | ||
| 73 | my $reply = $agent->{identities} = $agent->request($r); | ||
| 74 | |||
| 75 | my $type = $reply->get_int8; | ||
| 76 | if ($type == SSH_AGENT_FAILURE || $type == SSH_COM_AGENT2_FAILURE) { | ||
| 77 | return; | ||
| 78 | } | ||
| 79 | elsif ($type != $type2) { | ||
| 80 | croak "Bad auth reply message type: $type != $type2"; | ||
| 81 | } | ||
| 82 | |||
| 83 | $agent->{num} = $reply->get_int32; | ||
| 84 | croak "Too many identities in agent reply: $agent->{num}" | ||
| 85 | if $agent->{num} > 1024; | ||
| 86 | |||
| 87 | $agent->{num}; | ||
| 88 | } | ||
| 89 | |||
| 90 | sub identity_iterator { | ||
| 91 | my $agent = shift; | ||
| 92 | return sub { } unless $agent->num_identities; | ||
| 93 | sub { $agent->next_identity }; | ||
| 94 | } | ||
| 95 | |||
| 96 | sub first_identity { | ||
| 97 | my $agent = shift; | ||
| 98 | $agent->next_identity if $agent->num_identities; | ||
| 99 | } | ||
| 100 | |||
| 101 | sub next_identity { | ||
| 102 | my $agent = shift; | ||
| 103 | return unless $agent->{num} > 0; | ||
| 104 | |||
| 105 | my($ident, $key, $comment) = ($agent->{identities}); | ||
| 106 | if ($agent->{version} == 1) { | ||
| 107 | $key = Net::SSH::Perl::Key->new('RSA1'); | ||
| 108 | $key->{rsa}{bits} = $ident->get_int32; | ||
| 109 | $key->{rsa}{e} = $ident->get_mp_int; | ||
| 110 | $key->{rsa}{n} = $ident->get_mp_int; | ||
| 111 | $comment = $ident->get_str; | ||
| 112 | } | ||
| 113 | else { | ||
| 114 | my $blob = $ident->get_str; | ||
| 115 | $comment = $ident->get_str; | ||
| 116 | $key = Net::SSH::Perl::Key->new_from_blob($blob); | ||
| 117 | } | ||
| 118 | $agent->{num}--; | ||
| 119 | wantarray ? ($key, $comment) : $key; | ||
| 120 | } | ||
| 121 | |||
| 122 | sub sign { | ||
| 123 | my $agent = shift; | ||
| 124 | my($key, $data) = @_; | ||
| 125 | my $blob = $key->as_blob; | ||
| 126 | my $r = Net::SSH::Perl::Buffer->new( MP => "SSH$agent->{version}" ); | ||
| 127 | $r->put_int8(SSH2_AGENTC_SIGN_REQUEST); | ||
| 128 | $r->put_str($blob); | ||
| 129 | $r->put_str($data); | ||
| 130 | $r->put_int32(0); | ||
| 131 | |||
| 132 | my $reply = $agent->request($r); | ||
| 133 | my $type = $reply->get_int8; | ||
| 134 | if ($type == SSH_AGENT_FAILURE || $type == SSH_COM_AGENT2_FAILURE) { | ||
| 135 | return; | ||
| 136 | } | ||
| 137 | elsif ($type != SSH2_AGENT_SIGN_RESPONSE) { | ||
| 138 | croak "Bad auth response: $type != ", SSH2_AGENT_SIGN_RESPONSE; | ||
| 139 | } | ||
| 140 | else { | ||
| 141 | return $reply->get_str; | ||
| 142 | } | ||
| 143 | } | ||
| 144 | |||
| 145 | sub decrypt { | ||
| 146 | my $agent = shift; | ||
| 147 | my($key, $data, $session_id) = @_; | ||
| 148 | my $r = Net::SSH::Perl::Buffer->new( MP => "SSH$agent->{version}" ); | ||
| 149 | $r->put_int8(SSH_AGENTC_RSA_CHALLENGE); | ||
| 150 | $r->put_int32($key->{rsa}{bits}); | ||
| 151 | $r->put_mp_int($key->{rsa}{e}); | ||
| 152 | $r->put_mp_int($key->{rsa}{n}); | ||
| 153 | $r->put_mp_int($data); | ||
| 154 | $r->put_chars($session_id); | ||
| 155 | $r->put_int32(1); | ||
| 156 | |||
| 157 | my $reply = $agent->request($r); | ||
| 158 | my $type = $reply->get_int8; | ||
| 159 | my $response = ''; | ||
| 160 | if ($type == SSH_AGENT_FAILURE || $type == SSH_COM_AGENT2_FAILURE) { | ||
| 161 | return; | ||
| 162 | } | ||
| 163 | elsif ($type != SSH_AGENT_RSA_RESPONSE) { | ||
| 164 | croak "Bad auth response: $type"; | ||
| 165 | } | ||
| 166 | else { | ||
| 167 | $response .= $reply->get_char for 1..16; | ||
| 168 | } | ||
| 169 | $response; | ||
| 170 | } | ||
| 171 | |||
| 172 | sub close_socket { | ||
| 173 | my $agent = shift; | ||
| 174 | close($agent->{sock}) or warn qq{Could not close socket: $!\n}; | ||
| 175 | } | ||
| 176 | |||
| 177 | 1; | ||
| 178 | __END__ |