| 1 | package Net::SSH::Perl::Kex; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | use Net::SSH::Perl::Cipher; | ||
| 6 | use Net::SSH::Perl::Mac; | ||
| 7 | use Net::SSH::Perl::Comp; | ||
| 8 | use Net::SSH::Perl::Packet; | ||
| 9 | use Net::SSH::Perl::Buffer; | ||
| 10 | use Net::SSH::Perl::Constants qw( | ||
| 11 | :msg2 | ||
| 12 | :kex | ||
| 13 | :proposal | ||
| 14 | :protocol | ||
| 15 | SSH_COMPAT_BUG_HMAC ); | ||
| 16 | |||
| 17 | use Carp qw( croak ); | ||
| 18 | use Scalar::Util qw(weaken); | ||
| 19 | |||
| 20 | use vars qw( @PROPOSAL ); | ||
| 21 | @PROPOSAL = ( | ||
| 22 | KEX_DEFAULT_KEX, | ||
| 23 | KEX_DEFAULT_PK_ALG, | ||
| 24 | KEX_DEFAULT_ENCRYPT, | ||
| 25 | KEX_DEFAULT_ENCRYPT, | ||
| 26 | KEX_DEFAULT_MAC, | ||
| 27 | KEX_DEFAULT_MAC, | ||
| 28 | KEX_DEFAULT_COMP, | ||
| 29 | KEX_DEFAULT_COMP, | ||
| 30 | KEX_DEFAULT_LANG, | ||
| 31 | KEX_DEFAULT_LANG, | ||
| 32 | ); | ||
| 33 | |||
| 34 | sub new { | ||
| 35 | my $class = shift; | ||
| 36 | my $ssh = shift; | ||
| 37 | my $kex = bless { ssh => $ssh }, $class; | ||
| 38 | weaken $kex->{ssh}; | ||
| 39 | $kex; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub client_kexinit { $_[0]->{client_kexinit} } | ||
| 43 | sub server_kexinit { $_[0]->{server_kexinit} } | ||
| 44 | |||
| 45 | sub send_cipher { $_[0]->{ciph}[1] } | ||
| 46 | sub send_mac { $_[0]->{mac} [1] } | ||
| 47 | sub send_comp { $_[0]->{comp}[1] } | ||
| 48 | |||
| 49 | sub receive_cipher { $_[0]->{ciph}[0] } | ||
| 50 | sub receive_mac { $_[0]->{mac} [0] } | ||
| 51 | sub receive_comp { $_[0]->{comp}[0] } | ||
| 52 | |||
| 53 | sub exchange { | ||
| 54 | my $kex = shift; | ||
| 55 | my $ssh = $kex->{ssh}; | ||
| 56 | my $packet = shift; | ||
| 57 | |||
| 58 | my @proposal = @PROPOSAL; | ||
| 59 | if (!$ssh->config->get('ciphers')) { | ||
| 60 | if (my $c = $ssh->config->get('cipher')) { | ||
| 61 | $ssh->config->set('ciphers', $c); | ||
| 62 | } | ||
| 63 | } | ||
| 64 | if (my $cs = $ssh->config->get('ciphers')) { | ||
| 65 | # SSH2 cipher names are different; for compatibility, we'll map | ||
| 66 | # valid SSH1 ciphers to the SSH2 equivalent names | ||
| 67 | if($ssh->protocol eq PROTOCOL_SSH2) { | ||
| 68 | my %ssh2_cipher = reverse %Net::SSH::Perl::Cipher::CIPHERS_SSH2; | ||
| 69 | $cs = join ',', map $ssh2_cipher{$_} || $_, split(/,/, $cs); | ||
| 70 | } | ||
| 71 | $proposal[ PROPOSAL_CIPH_ALGS_CTOS ] = | ||
| 72 | $proposal[ PROPOSAL_CIPH_ALGS_STOC ] = $cs; | ||
| 73 | } | ||
| 74 | if ($ssh->config->get('compression')) { | ||
| 75 | $proposal[ PROPOSAL_COMP_ALGS_CTOS ] = | ||
| 76 | $proposal[ PROPOSAL_COMP_ALGS_STOC ] = "zlib,none"; | ||
| 77 | } | ||
| 78 | else { | ||
| 79 | $proposal[ PROPOSAL_COMP_ALGS_CTOS ] = | ||
| 80 | $proposal[ PROPOSAL_COMP_ALGS_STOC ] = "none"; | ||
| 81 | } | ||
| 82 | if (my $algos = $ssh->config->get('host_key_algorithms')) { | ||
| 83 | $proposal[ PROPOSAL_SERVER_HOST_KEY_ALGS ] = $algos; | ||
| 84 | } | ||
| 85 | if (my $algos = $ssh->config->get('kex_algorithms')) { | ||
| 86 | $proposal[ PROPOSAL_KEX_ALGS ] = $algos; | ||
| 87 | } | ||
| 88 | $proposal[ PROPOSAL_KEX_ALGS ] .= ',ext-info-c' | ||
| 89 | if $proposal[ PROPOSAL_KEX_ALGS ] !~ /ext-info-c/; | ||
| 90 | if (my $macs = $ssh->config->get('macs')) { | ||
| 91 | $proposal[ PROPOSAL_MAC_ALGS_CTOS ] = | ||
| 92 | $proposal[ PROPOSAL_MAC_ALGS_STOC ] = $macs; | ||
| 93 | } | ||
| 94 | |||
| 95 | $kex->{client_kexinit} = $kex->kexinit(\@proposal); | ||
| 96 | my($sprop) = $kex->exchange_kexinit($packet); | ||
| 97 | |||
| 98 | $kex->choose_conf(\@proposal, $sprop); | ||
| 99 | $ssh->debug("Algorithms, c->s: " . | ||
| 100 | "$kex->{ciph_name}[0] $kex->{mac_name}[0] $kex->{comp_name}[0]"); | ||
| 101 | $ssh->debug("Algorithms, s->c: " . | ||
| 102 | "$kex->{ciph_name}[1] $kex->{mac_name}[1] $kex->{comp_name}[1]"); | ||
| 103 | |||
| 104 | bless $kex, $kex->{class_name}; | ||
| 105 | $kex->exchange; | ||
| 106 | |||
| 107 | $ssh->debug("Send NEWKEYS."); | ||
| 108 | $packet = $ssh->packet_start(SSH2_MSG_NEWKEYS); | ||
| 109 | $packet->send; | ||
| 110 | |||
| 111 | $ssh->debug("Waiting for NEWKEYS message."); | ||
| 112 | $packet = Net::SSH::Perl::Packet->read_expect($ssh, SSH2_MSG_NEWKEYS); | ||
| 113 | |||
| 114 | $ssh->debug("Enabling encryption/MAC/compression."); | ||
| 115 | $ssh->{kex} = $kex; | ||
| 116 | for my $att (qw( mac ciph comp )) { | ||
| 117 | $kex->{$att}[0]->enable if $kex->{$att}[0]; | ||
| 118 | $kex->{$att}[1]->enable if $kex->{$att}[1]; | ||
| 119 | } | ||
| 120 | } | ||
| 121 | |||
| 122 | sub kexinit { | ||
| 123 | my $kex = shift; | ||
| 124 | my($proposal) = @_; | ||
| 125 | |||
| 126 | my $b = Net::SSH::Perl::Buffer->new( MP => 'SSH2' ); | ||
| 127 | my $cookie = join '', map chr rand 255, 1..16; | ||
| 128 | $b->put_chars($cookie); | ||
| 129 | $b->put_str($_) for @$proposal; | ||
| 130 | $b->put_int8(0); | ||
| 131 | $b->put_int32(0); | ||
| 132 | $b; | ||
| 133 | } | ||
| 134 | |||
| 135 | sub exchange_kexinit { | ||
| 136 | my $kex = shift; | ||
| 137 | my $ssh = $kex->{ssh}; | ||
| 138 | my $received_packet = shift; | ||
| 139 | my $packet; | ||
| 140 | |||
| 141 | $packet = $ssh->packet_start(SSH2_MSG_KEXINIT); | ||
| 142 | $packet->put_chars($kex->client_kexinit->bytes); | ||
| 143 | $packet->send; | ||
| 144 | |||
| 145 | if ( defined $received_packet ) { | ||
| 146 | $ssh->debug("Received key-exchange init (KEXINIT), sent response."); | ||
| 147 | $packet = $received_packet; | ||
| 148 | } | ||
| 149 | else { | ||
| 150 | $ssh->debug("Sent key-exchange init (KEXINIT), waiting for response."); | ||
| 151 | $packet = Net::SSH::Perl::Packet->read_expect($ssh, SSH2_MSG_KEXINIT); | ||
| 152 | } | ||
| 153 | $kex->{server_kexinit} = $packet->data; | ||
| 154 | |||
| 155 | $packet->get_char for 1..16; | ||
| 156 | my @s_props = map $packet->get_str, 1..10; | ||
| 157 | $packet->get_int8; | ||
| 158 | $packet->get_int32; | ||
| 159 | |||
| 160 | \@s_props; | ||
| 161 | } | ||
| 162 | |||
| 163 | sub derive_keys { | ||
| 164 | my $kex = shift; | ||
| 165 | my($hash, $shared_secret, $session_id) = @_; | ||
| 166 | my @keys; | ||
| 167 | for my $i (0..5) { | ||
| 168 | push @keys, $kex->derive_key(ord('A')+$i, $kex->{we_need}, $hash, | ||
| 169 | $shared_secret, $session_id); | ||
| 170 | } | ||
| 171 | my $is_ssh2 = $kex->{ssh}->protocol == PROTOCOL_SSH2; | ||
| 172 | for my $mode (0, 1) { | ||
| 173 | my $ctos = $mode == 1; | ||
| 174 | $kex->{ciph}[$mode]->init($keys[$ctos ? 2 : 3], $keys[$ctos ? 0 : 1], | ||
| 175 | $is_ssh2); | ||
| 176 | $kex->{mac}[$mode]->init($keys[$ctos ? 4 : 5]) if $kex->{mac}[$mode]; | ||
| 177 | $kex->{comp}[$mode]->init(6) if $kex->{comp}[$mode]; | ||
| 178 | } | ||
| 179 | } | ||
| 180 | |||
| 181 | sub choose_conf { | ||
| 182 | my $kex = shift; | ||
| 183 | my($cprop, $sprop) = @_; | ||
| 184 | for my $mode (0, 1) { | ||
| 185 | my $ctos = $mode == 1; | ||
| 186 | my $nciph = $ctos ? PROPOSAL_CIPH_ALGS_CTOS : PROPOSAL_CIPH_ALGS_STOC; | ||
| 187 | my $nmac = $ctos ? PROPOSAL_MAC_ALGS_CTOS : PROPOSAL_MAC_ALGS_STOC; | ||
| 188 | my $ncomp = $ctos ? PROPOSAL_COMP_ALGS_CTOS : PROPOSAL_COMP_ALGS_STOC; | ||
| 189 | $kex->choose_ciph($mode, $cprop->[$nciph], $sprop->[$nciph]); | ||
| 190 | if ($kex->{ciph}[$mode]->authlen) { | ||
| 191 | $kex->{mac_name}[$mode] = '<implicit>'; | ||
| 192 | } else { | ||
| 193 | $kex->choose_mac ($mode, $cprop->[$nmac], $sprop->[$nmac]) | ||
| 194 | } | ||
| 195 | $kex->choose_comp($mode, $cprop->[$ncomp], $sprop->[$ncomp]); | ||
| 196 | } | ||
| 197 | $kex->choose_kex($cprop->[PROPOSAL_KEX_ALGS], $sprop->[PROPOSAL_KEX_ALGS]); | ||
| 198 | $kex->choose_hostkeyalg($cprop->[PROPOSAL_SERVER_HOST_KEY_ALGS], | ||
| 199 | $sprop->[PROPOSAL_SERVER_HOST_KEY_ALGS]); | ||
| 200 | |||
| 201 | my $need = 0; | ||
| 202 | for my $mode (0, 1) { | ||
| 203 | $need = $kex->{ciph}[$mode]->keysize | ||
| 204 | if $need < $kex->{ciph}[$mode]->keysize; | ||
| 205 | $need = $kex->{ciph}[$mode]->blocksize | ||
| 206 | if $need < $kex->{ciph}[$mode]->blocksize; | ||
| 207 | if ($kex->{mac}[$mode]) { | ||
| 208 | $need = $kex->{mac}[$mode]->len | ||
| 209 | if $need < $kex->{mac}[$mode]->len; | ||
| 210 | } | ||
| 211 | } | ||
| 212 | $kex->{we_need} = $need; | ||
| 213 | } | ||
| 214 | |||
| 215 | sub choose_kex { | ||
| 216 | my $kex = shift; | ||
| 217 | my $name = _get_match(@_); | ||
| 218 | croak "No kex algorithm" unless $name; | ||
| 219 | $kex->{algorithm} = $name; | ||
| 220 | my %kexmap = ( | ||
| 221 | &KEX_CURVE25519_SHA256 => 'C25519', | ||
| 222 | &KEX_CURVE25519_SHA256_ALIAS | ||
| 223 | => 'C25519', | ||
| 224 | &KEX_DH_GEX_SHA256 => 'DHGEXSHA256', | ||
| 225 | &KEX_DH_GEX_SHA1 => 'DHGEXSHA1', | ||
| 226 | &KEX_DH14_SHA1 => 'DH14SHA1', | ||
| 227 | &KEX_DH14_SHA256 => 'DH14SHA256', | ||
| 228 | &KEX_DH16_SHA512 => 'DH16SHA512', | ||
| 229 | &KEX_DH18_SHA512 => 'DH18SHA512', | ||
| 230 | &KEX_DH1 => 'DH1' | ||
| 231 | ); | ||
| 232 | if (my $pkg = $kexmap{$name}) { | ||
| 233 | $kex->{ssh}->debug("Using $name for key exchange"); | ||
| 234 | eval "use Net::SSH::Perl::Kex::$pkg"; | ||
| 235 | $kex->{class_name} = __PACKAGE__ . '::' . $pkg; | ||
| 236 | } else { | ||
| 237 | croak "Bad kex algorithm $name"; | ||
| 238 | } | ||
| 239 | } | ||
| 240 | |||
| 241 | sub choose_hostkeyalg { | ||
| 242 | my $kex = shift; | ||
| 243 | my $name = _get_match(@_); | ||
| 244 | croak "No hostkey algorithm! CLIENT: $_[0] SERVER $_[1]" unless $name; | ||
| 245 | $kex->{ssh}->debug("Host key algorithm: $name"); | ||
| 246 | $kex->{hostkeyalg} = $name; | ||
| 247 | } | ||
| 248 | |||
| 249 | sub choose_ciph { | ||
| 250 | my $kex = shift; | ||
| 251 | my $mode = shift; | ||
| 252 | my $name = _get_match(@_); | ||
| 253 | croak "No matching cipher found: client ", $_[0], " server ", $_[1] | ||
| 254 | unless $name; | ||
| 255 | $kex->{ciph_name}[$mode] = $name; | ||
| 256 | $kex->{ciph}[$mode] = Net::SSH::Perl::Cipher->new($name); | ||
| 257 | } | ||
| 258 | |||
| 259 | sub choose_mac { | ||
| 260 | my $kex = shift; | ||
| 261 | my $mode = shift; | ||
| 262 | my $name = _get_match(@_); | ||
| 263 | croak "No matching mac found: client ", $_[0], " server ", $_[1] | ||
| 264 | unless $name; | ||
| 265 | $kex->{mac_name}[$mode] = $name; | ||
| 266 | my $mac = $kex->{mac}[$mode] = Net::SSH::Perl::Mac->new($name); | ||
| 267 | $mac->key_len( | ||
| 268 | $kex->{ssh}->{datafellows} & SSH_COMPAT_BUG_HMAC ? 16 : $mac->len); | ||
| 269 | } | ||
| 270 | |||
| 271 | sub choose_comp { | ||
| 272 | my $kex = shift; | ||
| 273 | my $mode = shift; | ||
| 274 | my $name = _get_match(@_); | ||
| 275 | croak "No matching comp found: client ", $_[0], " server ", $_[1] | ||
| 276 | unless $name; | ||
| 277 | $kex->{comp_name}[$mode] = $name; | ||
| 278 | $kex->{comp}[$mode] = Net::SSH::Perl::Comp->new($name); | ||
| 279 | } | ||
| 280 | |||
| 281 | sub _get_match { | ||
| 282 | my($c, $s) = @_; | ||
| 283 | my %sprop = map { $_ => 1 } split /,/, $s; | ||
| 284 | for my $cp (split /,/, $c) { | ||
| 285 | return $cp if $sprop{$cp}; | ||
| 286 | } | ||
| 287 | } | ||
| 288 | |||
| 289 | 1; | ||
| 290 | __END__ |