| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | $type = $t; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | ▶ | $options = substr($blob,0,$type_offset,'') if $type_offset > 0; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | last; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | # TODO do something with ssh options $options | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | ▶ | if (!defined $type) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
defined $expected_type and $expected_type ne $type
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | $class .= "::" . shift(); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | eval "use $class;"; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | die "Key class '$class' is unsupported: $@" if $@; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | chomp(my $desc = <$fh>); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | ▶ | close $fh or warn qq{Could not close "$keyfile": $!\n}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | ▶ | return unless $desc; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | $class = __PACKAGE__ . "::" . $rec->[0]; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | eval "use $class;"; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | ▶ | die "Key class '$class' is unsupported: $@" if $@; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | my @args = $rec->[1] ? @{ $rec->[1] } : (); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/6
$type and $type eq "bubblebabble"
$type and $type eq "md5"
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"
Truth table0%0/3
A and B
A= $typeB= $type eq "md5"
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 0 | eval "use Crypt::Digest::SHA1 qw( sha1 )"; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 0 | ▶ | die "Can't load SHA1: $@" if $@; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||||||||||