| 1 | package Net::SSH::Perl::Auth::RSA; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Net::SSH::Perl::Constants qw( | ||
| 7 | SSH_SMSG_FAILURE | ||
| 8 | SSH_SMSG_SUCCESS | ||
| 9 | SSH_CMSG_AUTH_RSA | ||
| 10 | SSH_SMSG_AUTH_RSA_CHALLENGE | ||
| 11 | SSH_CMSG_AUTH_RSA_RESPONSE ); | ||
| 12 | |||
| 13 | use Net::SSH::Perl::Util qw( :rsa :authfile _read_passphrase ); | ||
| 14 | use Net::SSH::Perl::Packet; | ||
| 15 | use Net::SSH::Perl::Agent; | ||
| 16 | use Net::SSH::Perl::Auth; | ||
| 17 | use base qw( Net::SSH::Perl::Auth ); | ||
| 18 | |||
| 19 | use Scalar::Util qw(weaken); | ||
| 20 | |||
| 21 | sub new { | ||
| 22 | my $class = shift; | ||
| 23 | my $ssh = shift; | ||
| 24 | my $auth = bless { ssh => $ssh }, $class; | ||
| 25 | weaken $auth->{ssh}; | ||
| 26 | $auth; | ||
| 27 | } | ||
| 28 | |||
| 29 | sub authenticate { | ||
| 30 | my $auth = shift; | ||
| 31 | my $ssh = $auth->{ssh}; | ||
| 32 | |||
| 33 | $ssh->debug("RSA authentication is disabled by the client."), return | ||
| 34 | unless $ssh->config->get('auth_rsa'); | ||
| 35 | |||
| 36 | return 1 if $auth->_try_agent; | ||
| 37 | |||
| 38 | my $if = $ssh->config->get('identity_files') || []; | ||
| 39 | for my $f (@$if) { | ||
| 40 | return 1 if $auth->_authenticate($f); | ||
| 41 | } | ||
| 42 | } | ||
| 43 | |||
| 44 | sub _try_agent { | ||
| 45 | my $auth = shift; | ||
| 46 | my $ssh = $auth->{ssh}; | ||
| 47 | return unless my $agent = Net::SSH::Perl::Agent->new(1); | ||
| 48 | |||
| 49 | my $iter = $agent->identity_iterator; | ||
| 50 | while(my($key, $comment) = $iter->()) { | ||
| 51 | $ssh->debug("Trying RSA authentication via agent with '$comment'"); | ||
| 52 | $ssh->debug("Server refused our key."), next unless | ||
| 53 | my $packet = $auth->_server_likes_key($key); | ||
| 54 | |||
| 55 | my $challenge = $packet->get_mp_int; | ||
| 56 | $ssh->debug("Received RSA challenge from server."); | ||
| 57 | my $response; | ||
| 58 | unless ($response = $agent->decrypt($key, $challenge, $ssh->session_id)) { | ||
| 59 | $ssh->debug("Authentication agent failed to decrypt challenge."); | ||
| 60 | $response = "\0" x 16; | ||
| 61 | } | ||
| 62 | $ssh->debug("Sending response to RSA challenge."); | ||
| 63 | $packet = $ssh->packet_start(SSH_CMSG_AUTH_RSA_RESPONSE); | ||
| 64 | $packet->put_chars($response); | ||
| 65 | $packet->send; | ||
| 66 | |||
| 67 | $packet = Net::SSH::Perl::Packet->read($ssh); | ||
| 68 | if ($packet->type == SSH_SMSG_SUCCESS) { | ||
| 69 | $agent->close_socket; | ||
| 70 | $ssh->debug("RSA authentication accepted by server."); | ||
| 71 | return 1; | ||
| 72 | } | ||
| 73 | elsif ($packet->type != SSH_SMSG_FAILURE) { | ||
| 74 | $ssh->fatal_disconnect("Protocol error waiting RSA auth response"); | ||
| 75 | } | ||
| 76 | } | ||
| 77 | $agent->close_socket; | ||
| 78 | $ssh->debug("RSA authentication using agent refused."); | ||
| 79 | |||
| 80 | return 0; | ||
| 81 | } | ||
| 82 | |||
| 83 | sub _server_likes_key { | ||
| 84 | my $auth = shift; | ||
| 85 | my($key) = @_; | ||
| 86 | my $ssh = $auth->{ssh}; | ||
| 87 | |||
| 88 | my $packet = $ssh->packet_start(SSH_CMSG_AUTH_RSA); | ||
| 89 | $packet->put_mp_int($key->{rsa}{n}); | ||
| 90 | $packet->send; | ||
| 91 | |||
| 92 | $packet = Net::SSH::Perl::Packet->read($ssh); | ||
| 93 | my $type = $packet->type; | ||
| 94 | if ($type == SSH_SMSG_FAILURE) { | ||
| 95 | $ssh->debug("Server refused our key."); | ||
| 96 | return 0; | ||
| 97 | } | ||
| 98 | |||
| 99 | if ($type != SSH_SMSG_AUTH_RSA_CHALLENGE) { | ||
| 100 | $ssh->fatal_disconnect("Protocol error during RSA authentication: $type"); | ||
| 101 | } | ||
| 102 | |||
| 103 | $packet; | ||
| 104 | } | ||
| 105 | |||
| 106 | sub _authenticate { | ||
| 107 | my($auth, $auth_file) = @_; | ||
| 108 | my $ssh = $auth->{ssh}; | ||
| 109 | my($packet); | ||
| 110 | |||
| 111 | my($public_key, $comment, $private_key); | ||
| 112 | eval { | ||
| 113 | ($public_key, $comment) = _load_public_key($auth_file); | ||
| 114 | }; | ||
| 115 | $ssh->debug("RSA authentication failed: Can't load public key."), | ||
| 116 | return 0 if $@; | ||
| 117 | |||
| 118 | $ssh->debug("Trying RSA authentication with key '$comment'"); | ||
| 119 | |||
| 120 | $ssh->debug("Server refused our key."), return unless | ||
| 121 | $packet = $auth->_server_likes_key($public_key); | ||
| 122 | |||
| 123 | my $challenge = $packet->get_mp_int; | ||
| 124 | $ssh->debug("Received RSA challenge from server."); | ||
| 125 | |||
| 126 | eval { | ||
| 127 | $private_key = _load_private_key($auth_file, ""); | ||
| 128 | }; | ||
| 129 | if (!$private_key || $@) { | ||
| 130 | my $passphrase = ""; | ||
| 131 | if ($ssh->config->get('interactive')) { | ||
| 132 | $passphrase = _read_passphrase("Enter passphrase for RSA key '$comment': "); | ||
| 133 | } | ||
| 134 | else { | ||
| 135 | $ssh->debug("Will not query passphrase for '$comment' in batch mode."); | ||
| 136 | } | ||
| 137 | |||
| 138 | eval { | ||
| 139 | $private_key = _load_private_key($auth_file, $passphrase); | ||
| 140 | }; | ||
| 141 | if (!$private_key || $@) { | ||
| 142 | $ssh->debug("Loading private key failed: $@."); | ||
| 143 | $packet = $ssh->packet_start(SSH_CMSG_AUTH_RSA_RESPONSE); | ||
| 144 | $packet->put_char(0) for (1..16); | ||
| 145 | $packet->send; | ||
| 146 | |||
| 147 | Net::SSH::Perl::Packet->read_expect($ssh, SSH_SMSG_FAILURE); | ||
| 148 | return 0; | ||
| 149 | } | ||
| 150 | } | ||
| 151 | |||
| 152 | _respond_to_rsa_challenge($ssh, $challenge, $private_key); | ||
| 153 | |||
| 154 | $packet = Net::SSH::Perl::Packet->read($ssh); | ||
| 155 | my $type = $packet->type; | ||
| 156 | if ($type == SSH_SMSG_SUCCESS) { | ||
| 157 | $ssh->debug("RSA authentication accepted by server."); | ||
| 158 | return 1; | ||
| 159 | } | ||
| 160 | elsif ($type != SSH_SMSG_FAILURE) { | ||
| 161 | $ssh->fatal_disconnect("Protocol error waiting RSA auth response: $type"); | ||
| 162 | } | ||
| 163 | |||
| 164 | $ssh->debug("RSA authentication refused."); | ||
| 165 | return 0; | ||
| 166 | } | ||
| 167 | |||
| 168 | 1; | ||
| 169 | __END__ |