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

filter: Statementstmt 0.0% 0 / 86 Branchbran 0.0% 0 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 105 SCAR 17.9
CC
2
Cov
0%
exchange
6.0
new
2.0
kex_hash
2.0
CRAP 6.0
1 package Net::SSH::Perl::Kex::DH;
2 use strict;
3 use warnings;
4  
5 use Net::SSH::Perl::Buffer;
6 use Net::SSH::Perl::Packet;
7 use Net::SSH::Perl::Constants qw( :msg2 :kex );
8 use Net::SSH::Perl::Key;
9  
10 use Carp qw( croak );
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 exchange {
24 my $kex = shift;
25 my $ssh = $kex->{ssh};
26 my $packet;
27  
28 $ssh->debug('Generating new Diffie-Hellman Group ' . $kex->group . ' keys');
29 my $dh = $kex->_dh_new_group();
30  
31 $ssh->debug('Entering Diffie-Hellman Group ' . $kex->group . ' key exchange.');
32 $packet = $ssh->packet_start(SSH2_MSG_KEXDH_INIT);
33 my $pub_key = $dh->export_key_raw('public');
34 $packet->put_mp_int($pub_key);
35 $packet->send;
36  
37 $ssh->debug("Sent DH public key, waiting for reply.");
38 $packet = Net::SSH::Perl::Packet->read_expect($ssh,
39 SSH2_MSG_KEXDH_REPLY);
40  
41 my $host_key_blob = $packet->get_str;
42 my $s_host_key = Net::SSH::Perl::Key->new_from_blob($host_key_blob,
43 \$ssh->{datafellows});
44 $ssh->debug("Received host key, type '" . $s_host_key->ssh_name . "'.");
45  
46 $ssh->check_host_key($s_host_key);
47  
48 my $dh_server_pub = $packet->get_mp_int;
49 my $signature = $packet->get_str;
50  
51 my $dh_server_pub_key = Crypt::PK::DH->new;
52 # create public key object (which will also check the public key for validity)
53 $dh_server_pub_key->import_key_raw($dh_server_pub, 'public', $dh->params2hash);
54  
55 $ssh->debug("Computing shared secret key.");
56 my $shared_secret = $dh->shared_secret($dh_server_pub_key);
57  
58 my $hash = $kex->kex_hash(
59 $ssh->client_version_string,
60 $ssh->server_version_string,
61 $kex->client_kexinit,
62 $kex->server_kexinit,
63 $host_key_blob,
64 $pub_key,
65 $dh_server_pub,
66 $shared_secret);
67  
68 $ssh->debug("Verifying server signature.");
69 croak "Key verification failed for server host key"
70 unless $s_host_key->verify($signature, $hash);
71  
72 $ssh->session_id($hash);
73  
74 $kex->derive_keys($hash, $shared_secret, $ssh->session_id);
75 }
76  
77 sub kex_hash {
78 my $kex = shift;
79 my($c_vs, $s_vs, $c_kexinit, $s_kexinit, $s_host_key_blob,
80 $c_dh_pub, $s_dh_pub, $shared_secret) = @_;
81 my $b = Net::SSH::Perl::Buffer->new( MP => 'SSH2' );
82 $b->put_str($c_vs);
83 $b->put_str($s_vs);
84  
85 $b->put_int32($c_kexinit->length + 1);
86 $b->put_int8(SSH2_MSG_KEXINIT);
87 $b->put_chars($c_kexinit->bytes);
88 $b->put_int32($s_kexinit->length + 1);
89 $b->put_int8(SSH2_MSG_KEXINIT);
90 $b->put_chars($s_kexinit->bytes);
91  
92 $b->put_str($s_host_key_blob);
93 $b->put_mp_int($c_dh_pub);
94 $b->put_mp_int($s_dh_pub);
95 $b->put_mp_int($shared_secret);
96  
97 $kex->hash($b->bytes);
98 }
99  
100 sub derive_key {
101 my($kex, $id, $need, $hash, $shared_secret, $session_id) = @_;
102 my $b = Net::SSH::Perl::Buffer->new( MP => 'SSH2' );
103 $b->put_mp_int($shared_secret);
104 my $digest = $kex->hash($b->bytes, $hash, chr($id), $session_id);
105 for (my $have = $kex->hash_len; $need > $have; $have += $kex->hash_len) {
106 $digest .= $kex->hash($b->bytes, $hash, $digest);
107 }
108 $digest;
109 }
110  
111 1;
112 __END__