blib/lib/Net/SSH/Perl/Key.pm

filter: Statementstmt 34.5% 38 / 110 Branchbran 4.7% 2 / 42 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 54.5% 12 / 22 Podpod 57.1% 4 / 7 totaltotal 28.5% 56 / 196 SCAR 58.2
CC
27
Cov
25%
extract_public
72.0
read_private_pem
56.0
fingerprint
30.0
CRAP 336.5
« blib/lib/Net/SSH/Perl/ChannelMgr.pm ↑ Summary blib/lib/Net/SSH/Perl/Agent.pm »
1 package Net::SSH::Perl::Key;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use Crypt::Digest::SHA256 qw( sha256 );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Crypt::Digest::MD5 qw( md5 );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Crypt::Misc qw( encode_b64 decode_b64 );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Net::SSH::Perl::Buffer;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 sub new {
11 1 ▶ my $class = shift;
Subroutine new: called CC=3 SCAR=13.6
Pod documented
12 1 ▶ if ($class eq __PACKAGE__) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class eq __PACKAGE__) 1 0 1
13 1 $class .= "::" . shift();
14 1 ▶ eval "use $class;";
Subroutine BEGIN: called CC=1 SCAR=1.2
15 1 ▶ die "Key class '$class' is unsupported: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 0 1
16 }
17 0 my $key = bless {}, $class;
18 0 $key->init(@_);
19 0 $key;
20 }
21  
22 2 ▶use vars qw( %KEY_TYPES );
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 %KEY_TYPES = (
24 'ssh-dss' => 'DSA',
25 'ssh-rsa' => 'RSA',
26 'ssh-ed25519' => 'Ed25519',
27 'ecdsa-sha2-nistp256' => 'ECDSA256',
28 'ecdsa-sha2-nistp384' => 'ECDSA384',
29 'ecdsa-sha2-nistp521' => 'ECDSA521',
30 );
31  
32 sub new_from_blob {
33 0 ▶ my $class = shift;
Subroutine new_from_blob: not called CC=2 SCAR=17.9
Pod undocumented
34 0 my($blob) = @_;
35 0 my $b = Net::SSH::Perl::Buffer->new( MP => 'SSH1' );
36 0 $b->append($blob);
37 0 my $ssh_name = $b->get_str;
38 0 ▶ my $type = $KEY_TYPES{$ssh_name} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $type = $Net::SSH::Perl::Key::KEY_TYPES{$ssh_name} or return 0 0 0
39 0 __PACKAGE__->new($type, @_);
40 }
41  
42 sub extract_public {
43 0 ▶ my $class = shift;
Subroutine extract_public: not called CC=8 SCAR=42.8
Pod documented
44 0 my($blob) = pop @_;
45 0 ▶ my $expected_type = @_ ? shift : undef;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
46  
47 0 my $type;
48 my $options;
49 0 foreach my $t (keys %KEY_TYPES) {
50 0 ▶ if ((my $type_offset = index($blob,$t)) >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $type_offset = index($blob, $t)) >= 0) 0 0 0
51 0 $type = $t;
52 0 ▶ $options = substr($blob,0,$type_offset,'') if $type_offset > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $type_offset > 0 0 0 0
53 0 last;
54 }
55 }
56  
57 # TODO do something with ssh options $options
58  
59 0 ▶ if (!defined $type) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $type) 0 0 0
60 0 warn "Invalid public key line";
61 0 return;
62 }
63 0 substr($blob,0,length($type)+1,'');
64 0 my($data, $comment) = split /\s+/, $blob, 2;
65 0 ▶ if (defined $expected_type && $expected_type ne $type) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $expected_type and $expected_type ne $type) 0 0 0
Condition0%0/3
defined $expected_type and $expected_type ne $type
!ll&&!rl&&r
000
MC/DC0%0/2
defined $expected_type and $expected_type ne $type
defined $expected_type $expected_type ne $type
Truth table0%0/3
A and B
A= defined $expected_typeB= $expected_type ne $type
ABresult
0X0
100
111
66 0 warn "Requested type '$expected_type' mismatches actual type '$type'";
67 0 return;
68 }
69 0 $type = $KEY_TYPES{$type};
70 0 my $key = __PACKAGE__->new($type, decode_b64($data));
71 0 $key->comment($comment);
72 0 $key;
73 }
74  
75 BEGIN {
76 2 ▶ no strict 'refs'; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
77 2 ▶ for my $meth (qw( read_private keygen )) {
Subroutine BEGIN: called CC=2 SCAR=14.6
78 *$meth = sub {
79 0 ▶ my $class = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
80 0 ▶ if ($class eq __PACKAGE__) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class eq __PACKAGE__) 0 0 0
81 0 $class .= "::" . shift();
82 0 eval "use $class;";
83 0 ▶ die "Key class '$class' is unsupported: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
84 }
85 0 $class->$meth(@_);
86 4 };
87 }
88 }
89  
90 2 ▶use vars qw( %OBJ_MAP );
Subroutine BEGIN: called CC=1 SCAR=0.0
91 2 %OBJ_MAP = (
92 'DSA PRIVATE KEY' => [ 'DSA' ],
93 'SSH2 ENCRYPTED PRIVATE KEY' => [ 'DSA', [ 'SSH2' ] ],
94 'RSA PRIVATE KEY' => [ 'RSA' ],
95 'OPENSSH PRIVATE KEY' => [ 'Ed25519' ],
96 'EC PRIVATE KEY' => [ 'ECDSA' ],
97 );
98  
99 sub read_private_pem {
100 0 ▶ my $class = shift;
Subroutine read_private_pem: not called CC=7 SCAR=40.3
Pod undocumented
101 0 my $keyfile = $_[0];
102 0 ▶ open my $fh, '<', $keyfile or return;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $keyfile or return 0 0 0
103 0 chomp(my $desc = <$fh>);
104 0 ▶ close $fh or warn qq{Could not close "$keyfile": $!\n};
Branch: 1 branch
BranchTrueFalseTotal
close $fh or warn qq[Could not close "$keyfile": $!\n] 0 0 0
105 0 ▶ return unless $desc;
Branch: 1 branch
BranchTrueFalseTotal
unless $desc 0 0 0
106 0 my($object) = $desc =~ /^-----?\s?BEGIN ([^\n\-]+)\s?-?----\s*$/;
107 0 $object =~ s/\s*$//;
108 0 ▶ my $rec = $OBJ_MAP{$object} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $rec = $Net::SSH::Perl::Key::OBJ_MAP{$object} or return 0 0 0
109 0 $class = __PACKAGE__ . "::" . $rec->[0];
110 0 eval "use $class;";
111 0 ▶ die "Key class '$class' is unsupported: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
112 0 ▶ my @args = $rec->[1] ? @{ $rec->[1] } : ();
Branch: 1 branch
BranchTrueFalseTotal
$rec->[1] ? : 0 0 0
113 0 $class->read_private(@_, @args);
114 }
115  
116 sub init;
117 sub extract_public;
118 sub as_blob;
119 sub equal;
120 sub size;
121  
122 sub fingerprint {
123 0 ▶ my $key = shift;
Subroutine fingerprint: not called CC=5 SCAR=34.0
Pod documented
124 0 my($type) = @_;
125 0 my $data = $key->fingerprint_raw;
126 0 ▶ $type && $type eq 'bubblebabble' ?
Branch: 2 branches
BranchTrueFalseTotal
$type && $type eq 'bubblebabble' ? : 0 0 0
$type && $type eq 'md5' ? : 0 0 0
Condition0%0/6
$type and $type eq "bubblebabble"
!ll&&!rl&&r
000
$type and $type eq "md5"
!ll&&!rl&&r
000
MC/DC0%0/2
$type and $type eq "bubblebabble"
$type $type eq "bubblebabble"
MC/DC0%0/2
$type and $type eq "md5"
$type $type eq "md5"
Truth table0%0/3
A and B
A= $typeB= $type eq "bubblebabble"
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $typeB= $type eq "md5"
ABresult
0X0
100
111
127 _fp_bubblebabble($data) :
128 $type && $type eq 'md5' ?
129 _fp_md5($data) :
130 _fp_sha256($data);
131 }
132  
133 sub _fp_bubblebabble {
134 0 ▶ eval "use Digest::BubbleBabble qw( bubblebabble )";
Subroutine _fp_bubblebabble: not called CC=3 SCAR=24.8
135 0 ▶ die "Can't load BubbleBabble implementation: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
136 0 eval "use Crypt::Digest::SHA1 qw( sha1 )";
137 0 ▶ die "Can't load SHA1: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
138 0 bubblebabble( Digest => sha1($_[0]) )
139 }
140  
141 0 ▶sub _fp_sha256 { "SHA256:" . encode_b64(sha256(shift)) }
Subroutine _fp_sha256: not called CC=1 SCAR=6.9
142 0 ▶sub _fp_md5 { join ':', map { sprintf "%02x", ord } split //, md5($_[0]) }
Subroutine _fp_md5: not called CC=1 SCAR=6.9
143  
144 sub comment {
145 0 ▶ my $key = shift;
Subroutine comment: not called CC=2 SCAR=17.9
Pod undocumented
146 0 my $comment = shift;
147 0 ▶ $key->{comment} = $comment if defined $comment;
Branch: 1 branch
BranchTrueFalseTotal
if defined $comment 0 0 0
148 0 $key->{comment};
149 }
150  
151 0 ▶sub dump_public { join ' ', grep { defined } $_[0]->ssh_name, encode_b64( $_[0]->as_blob ), $_[0]->comment }
Subroutine dump_public: not called CC=1 SCAR=6.9
Pod documented
152  
153 2 1;
154 __END__
« blib/lib/Net/SSH/Perl/ChannelMgr.pm ↑ Summary blib/lib/Net/SSH/Perl/Agent.pm »