blib/lib/Net/SSH/Perl/Auth/PublicKey.pm untested

filter: Statementstmt 0.0% 0 / 130 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 12 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 199 SCAR 56.1
CC
16
Cov
0%
_test_pubkey
30.0
authenticate
20.0
_auth_identity
20.0
CRAP 272.0
1 package Net::SSH::Perl::Auth::PublicKey;
2  
3 use strict;
4 use warnings;
5  
6 use Net::SSH::Perl::Constants qw(
7 SSH2_MSG_USERAUTH_REQUEST
8 SSH2_MSG_USERAUTH_PK_OK
9 SSH_COMPAT_OLD_SESSIONID
10 SSH_COMPAT_BUG_PKAUTH );
11  
12 use Net::SSH::Perl::Util qw( _read_passphrase );
13 use Net::SSH::Perl::Buffer;
14 use Net::SSH::Perl::Key;
15  
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->enabled( $ssh->config->get('auth_dsa') );
27 $auth;
28 }
29  
30 sub enabled {
31 my $auth = shift;
32 $auth->{enabled} = shift if @_;
33 $auth->{enabled};
34 }
35  
36 sub authenticate {
37 my $auth = shift;
38 my $ssh = $auth->{ssh};
39  
40 my $sent = 0;
41 if (my $agent = $auth->mgr->agent) {
42 do {
43 $sent = $auth->_auth_agent;
44 } until $sent || $agent->num_left <= 0;
45 }
46 return $sent if $sent;
47  
48 my $if = $ssh->config->get('identity_files') || [];
49 my $idx = $auth->{_identity_idx} || 0;
50 for my $f (@$if[$idx..$#$if]) {
51 $auth->{_identity_idx}++;
52 return 1 if $auth->_auth_identity($f);
53 }
54 }
55  
56 sub _auth_agent {
57 my $auth = shift;
58 my $agent = $auth->mgr->agent;
59  
60 my($iter);
61 $iter = $auth->{_identity_iter} = $agent->identity_iterator
62 unless $iter = $auth->{_identity_iter};
63 my($key, $comment) = $iter->();
64 return unless $key;
65 $auth->{ssh}->debug("Publickey: testing agent key '$comment'");
66 $auth->_test_pubkey($key, \&agent_sign);
67 }
68  
69 sub _auth_identity {
70 my $auth = shift;
71 my($auth_file) = @_;
72 my $ssh = $auth->{ssh};
73 my($packet);
74  
75 -s $auth_file or return 0;
76  
77 my($key);
78 $ssh->debug("Trying pubkey authentication with key file '$auth_file'");
79  
80 $key = Net::SSH::Perl::Key->read_private_pem($auth_file, '',
81 \$ssh->{datafellows}, $ssh->{'server-sig-algs'});
82 if (!$key) {
83 my $passphrase = "";
84 if ($ssh->config->get('interactive')) {
85 $passphrase = _read_passphrase("Enter passphrase for keyfile '$auth_file': ");
86 }
87 else {
88 $ssh->debug("Will not query passphrase for '$auth_file' in batch mode.");
89 }
90  
91 $key = Net::SSH::Perl::Key->read_private_pem($auth_file,
92 $passphrase, \$ssh->{datafellows});
93 if (!$key) {
94 $ssh->debug("Loading private key failed.");
95 return 0;
96 }
97 }
98  
99 $auth->_sign_send_pubkey($key, \&key_sign);
100 }
101  
102 sub agent_sign { $_[0]->mgr->agent->sign($_[1], $_[2]) }
103 sub key_sign { $_[1]->sign($_[2]) }
104  
105 sub _sign_send_pubkey {
106 my $auth = shift;
107 my($key, $cb) = @_;
108 my $ssh = $auth->{ssh};
109 my($packet);
110  
111 my $b = Net::SSH::Perl::Buffer->new( MP => 'SSH2' );
112 if ($ssh->{datafellows} & SSH_COMPAT_OLD_SESSIONID) {
113 $b->append($ssh->session_id);
114 }
115 else {
116 $b->put_str($ssh->session_id);
117 }
118 $b->put_int8(SSH2_MSG_USERAUTH_REQUEST);
119 my $skip = $b->length;
120  
121 $b->put_str($ssh->config->get('user'));
122 $b->put_str("ssh-connection");
123 $b->put_str("publickey");
124 $b->put_int8(1);
125 $b->put_str( $key->ssh_name );
126 $b->put_str( $key->as_blob );
127  
128 my $sigblob = $cb->($auth, $key, $b->bytes);
129 $ssh->debug("Signature generation failed for public key."), return
130 unless $sigblob;
131 $b->put_str($sigblob);
132  
133 $b->bytes(0, $skip, ''); ## Get rid of session ID and packet type.
134  
135 $packet = $ssh->packet_start(SSH2_MSG_USERAUTH_REQUEST);
136 $packet->append($b->bytes);
137 $packet->send;
138  
139 return 1;
140 }
141  
142 sub _test_pubkey {
143 my $auth = shift;
144 my($key, $cb) = @_;
145 my $ssh = $auth->{ssh};
146  
147 my $blob = $key->as_blob;
148  
149 ## Set up PK_OK callback; closure on $auth, $key, and $cb.
150 $auth->mgr->register_handler(SSH2_MSG_USERAUTH_PK_OK, sub {
151 my $amgr = shift;
152 my($packet) = @_;
153 my $ssh = $amgr->{ssh};
154 my $alg = $packet->get_str;
155 my $blob = $packet->get_str;
156  
157 $ssh->debug("PK_OK received without existing key state."), return
158 unless $key && $cb;
159  
160 my $s_key = Net::SSH::Perl::Key->new_from_blob($blob, undef,
161 $ssh->{'server-sig-algs'});
162 $ssh->debug("Failed extracting key from blob, pkalgorithm is '$alg'"),
163 return unless $s_key;
164 $ssh->debug("PK_OK key != saved state key"), return
165 unless $s_key->equal($key);
166  
167 $ssh->debug("Public key is accepted, signing data.");
168 $ssh->debug("Key fingerprint: " .
169 $key->fingerprint($ssh->config->get('fingerprint_hash')));
170 my $sent = $auth->_sign_send_pubkey($s_key, $cb);
171 $amgr->remove_handler(SSH2_MSG_USERAUTH_PK_OK);
172  
173 $sent;
174 });
175  
176 my $packet = $ssh->packet_start(SSH2_MSG_USERAUTH_REQUEST);
177 $packet->put_str($ssh->config->get('user'));
178 $packet->put_str("ssh-connection");
179 $packet->put_str("publickey");
180 $packet->put_int8(0); ## No signature, just public key blob.
181 $packet->put_str($key->ssh_name)
182 unless $ssh->{datafellows} & SSH_COMPAT_BUG_PKAUTH;
183 $packet->put_str($blob);
184 $packet->send;
185  
186 return 1;
187 }
188  
189 1;
190 __END__