blib/lib/Net/SSH/Perl/Kex/DHGEX.pm untested

filter: Statementstmt 0.0% 0 / 111 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 5 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 145 SCAR 24.8
CC
3
Cov
0%
exchange
12.0
new
2.0
min_bits
2.0
CRAP 12.0
1 package Net::SSH::Perl::Kex::DHGEX;
2 use strict;
3  
4 use Net::SSH::Perl::Buffer;
5 use Net::SSH::Perl::Packet;
6 use Net::SSH::Perl::Constants qw( :msg2 :kex );
7 use Net::SSH::Perl::Key;
8  
9 use Carp qw( croak );
10 use Crypt::PK::DH;
11 use Scalar::Util qw(weaken);
12  
13 use base qw( Net::SSH::Perl::Kex );
14  
15 sub new {
16 my $class = shift;
17 my $ssh = shift;
18 my $kex = bless { ssh => $ssh }, $class;
19 weaken $kex->{ssh};
20 $kex;
21 }
22  
23 sub min_bits { 2048 }
24 sub want_bits { 4096 }
25 sub max_bits { 8192 }
26  
27 sub exchange {
28 my $kex = shift;
29 my $ssh = $kex->{ssh};
30 my $packet;
31  
32 # step 1 in rfc 4419
33 $ssh->debug('Entering Diffie-Hellman Group Exchange.');
34 $packet = $ssh->packet_start(SSH2_MSG_KEX_DH_GEX_REQUEST);
35 $packet->put_int32($kex->min_bits);
36 $packet->put_int32($kex->want_bits);
37 $packet->put_int32($kex->max_bits);
38 $packet->send;
39 $ssh->debug('SSH2_MSG_KEX_DH_GEX_REQUEST(' . $kex->min_bits . '<' .
40 $kex->want_bits . '<' . $kex->max_bits . ') sent');
41  
42 # step 2 in rfc 4419
43 $ssh->debug('Sent DH Group Exchange request, waiting for reply.');
44 $packet = Net::SSH::Perl::Packet->read_expect($ssh,
45 SSH2_MSG_KEX_DH_GEX_GROUP);
46 my $p = $packet->get_mp_int;
47 my $g = $packet->get_mp_int;
48 # range check on p
49 my $p_bits = $kex->bitsize($p);
50 if ($p_bits < $kex->min_bits || $p_bits > $kex->max_bits) {
51 $ssh->fatal_disconnect("DH Group Exchange reply out of range ($p_bits bits)");
52 }
53 $ssh->debug("Received $p_bits bit DH Group Exchange reply.");
54  
55 # step 3 in rfc 4419
56 $ssh->debug('Generating new Diffie-Hellman keys.');
57 my $dh = $kex->_dh_new_group($p, $g);
58  
59 $ssh->debug('Entering Diffie-Hellman key exchange.');
60 $packet = $ssh->packet_start(SSH2_MSG_KEX_DH_GEX_INIT);
61 my $pub_key = $dh->export_key_raw('public');
62 $packet->put_mp_int($pub_key);
63 $packet->send;
64 $ssh->debug('Sent DH public key, waiting for reply.');
65  
66 # step 4 in rfc 4419
67 $packet = Net::SSH::Perl::Packet->read_expect($ssh,
68 SSH2_MSG_KEX_DH_GEX_REPLY);
69  
70 my $host_key_blob = $packet->get_str;
71 my $s_host_key = Net::SSH::Perl::Key->new_from_blob($host_key_blob,
72 \$ssh->{datafellows});
73 $ssh->debug("Received host key, type '" . $s_host_key->ssh_name . "'.");
74  
75 # step 5 in rfc 4419
76 $ssh->check_host_key($s_host_key);
77  
78 my $dh_server_pub = $packet->get_mp_int;
79 my $dh_server_pub_key = Crypt::PK::DH->new;
80 # create public key object (which will also check the public key for validity)
81 $dh_server_pub_key->import_key_raw($dh_server_pub, 'public', $dh->params2hash);
82  
83 my $shared_secret = $dh->shared_secret($dh_server_pub_key);
84 my $signature = $packet->get_str;
85  
86 my $hash = $kex->kex_hash(
87 $ssh->client_version_string,
88 $ssh->server_version_string,
89 $kex->client_kexinit,
90 $kex->server_kexinit,
91 $host_key_blob,
92 $p, $g,
93 $pub_key,
94 $dh_server_pub,
95 $shared_secret);
96  
97 $ssh->debug("Verifying server signature.");
98 croak "Key verification failed for server host key"
99 unless $s_host_key->verify($signature, $hash);
100  
101 $ssh->session_id($hash);
102  
103 $kex->derive_keys($hash, $shared_secret, $ssh->session_id);
104 }
105  
106 sub kex_hash {
107 my $kex = shift;
108 my($c_vs, $s_vs, $c_kexinit, $s_kexinit, $s_host_key_blob,
109 $p, $g, $c_dh_pub, $s_dh_pub, $shared_secret) = @_;
110 my $b = Net::SSH::Perl::Buffer->new( MP => 'SSH2' );
111 # version strings (V_C, V_S)
112 $b->put_str($c_vs);
113 $b->put_str($s_vs);
114 # client,server payload of SSH_MSG_KEXINIT (I_C, I_S)
115 $b->put_int32($c_kexinit->length + 1);
116 $b->put_int8(SSH2_MSG_KEXINIT);
117 $b->put_chars($c_kexinit->bytes);
118 $b->put_int32($s_kexinit->length + 1);
119 $b->put_int8(SSH2_MSG_KEXINIT);
120 $b->put_chars($s_kexinit->bytes);
121 # host key (K_S)
122 $b->put_str($s_host_key_blob);
123 # min, n, max
124 $b->put_int32($kex->min_bits);
125 $b->put_int32($kex->want_bits);
126 $b->put_int32($kex->max_bits);
127 # p, g
128 $b->put_mp_int($p);
129 $b->put_mp_int($g);
130 # e, f, K
131 $b->put_mp_int($c_dh_pub);
132 $b->put_mp_int($s_dh_pub);
133 $b->put_mp_int($shared_secret);
134  
135 $kex->hash($b->bytes);
136 }
137  
138 sub bitsize {
139 my $kex = shift;
140 my $num = shift or return 0;
141 my $first = substr($num,0,1);
142 my $bitsize = 0;
143 while (ord($first)) {
144 $bitsize++;
145 $first = chr(ord($first) >> 1);
146 }
147 $bitsize += ((length($num)-1) * 8);
148 return $bitsize;
149 }
150  
151 sub _dh_new_group {
152 my $kex = shift;
153 my ($p,$g) = @_;
154 my $dh = Crypt::PK::DH->new;
155 $dh->generate_key({ g => unpack('H*',$g), p => unpack('H*',$p) });
156 $dh;
157 }
158  
159 1;
160 __END__