blib/lib/Data/SSHPubkey.pm

filter: Statementstmt 78.2% 72 / 92 Branchbran 67.2% 39 / 58 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 87.5% 7 / 8 Podpod 100% 2 / 2 totaltotal 75.0% 120 / 160 CC 31 SCAR 28.3
CC
31
Cov
74%
convert_pubkeys
42.0
_until_end
14.4
pubkeys
14.1
CRAP 16.9
↑ Summary
1 # -*- Perl -*-
2 #
3 # utility function to parse SSH public keys with
4 #
5 # run perldoc(1) on this file for documentation
6  
7 package Data::SSHPubkey;
8  
9 3 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use File::Temp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 3 our ($max_keys, $max_lines, %ssh_pubkey_types);
17  
18 3 require Exporter;
19 3 our @ISA = qw(Exporter);
20 3 our @EXPORT_OK = qw(&convert_pubkeys &pubkeys %ssh_pubkey_types);
21  
22 3 our $VERSION = '1.00';
23  
24 # rsa or ecdsa or ed25519 with the upper case forms presumably some
25 # other encoding of one of these, so set very low by default
26 3 $max_keys = 3;
27  
28 # a 4096-bit RSA key is 16 lines in RFC4716, though this may need to be
29 # set higher if you allow long comments, or
30 3 $max_lines = 100;
31  
32 3 @ssh_pubkey_types{qw(ecdsa ed25519 rsa PEM PKCS8 RFC4716)} = ();
33 # NOTE these are taken from the ssh-keygen(1) -t or -m options which
34 # differ from the strings present in the SSH key data
35 #
36 # type public key prefix
37 # -----------------------------------
38 # ecdsa ecdsa-sha2-nistp256 ...
39 # ed25519 ssh-ed25519 ...
40 # rsa ssh-rsa ...
41 #
42 # those responsible for the confusion between these two different bits
43 # of data in versions of this module prior to 0.05 have been sacked
44  
45 0 ▶sub convert_pubkeys {
Subroutine convert_pubkeys: not called CC=6 SCAR=37.4
Pod documented
46 0 my ($list) = @_;
47 0 my @pubkeys;
48 0 for my $ref (@$list) {
49 0 ▶ if ($ref->[0] =~ m/^(?:PEM|PKCS8|RFC4716)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref->[0] =~ /^(?:PEM|PKCS8|RFC4716)$/) { } 0 0 0
elsif ($ref->[0] =~ /^(?:ecdsa|ed25519|rsa)$/) { } 0 0 0
50 # TODO perl (or CPAN module) conversion of these so don't
51 # need to call out to this ssh-keygen which is not portable
52 # to olden versions of ssh-keygen
53 0 my $tmp = File::Temp->new;
54 0 print $tmp $ref->[1];
55 0 my $tfile = $tmp->filename;
56 0 ▶ open my $fh, '-|', qw(ssh-keygen -i -m), $ref->[0], '-f', $tfile
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "-|", ("ssh-keygen", "-i", "-m"), $ref->[0], "-f", $tfile or die "could not exec ssh-keygen: $!" 0 0 0
57 or die "could not exec ssh-keygen: $!";
58 0 binmode $fh;
59 0 push @pubkeys, do { local $/; readline $fh };
60 0 ▶ close $fh or die "ssh-keygen failed with exit status $?";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "ssh-keygen failed with exit status $?" 0 0 0
61 } elsif ($ref->[0] =~ m/^(?:ecdsa|ed25519|rsa)$/) {
62 0 push @pubkeys, $ref->[1];
63 } else {
64 0 croak 'unknown public key type ' . $ref->[0];
65 }
66 }
67 0 chomp @pubkeys;
68 0 return @pubkeys;
69 }
70  
71 15 ▶sub pubkeys {
Subroutine pubkeys: called CC=14 SCAR=26.5
Pod documented
72 15 my ($input) = @_;
73 15 ▶ croak "input must be string, GLOB, or scalar ref" if !defined $input;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input 1 14 15
74 14 my $fh;
75 14 ▶ if (ref $input eq 'GLOB') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $input eq 'GLOB') { } 1 13 14
76 1 $fh = $input;
77 } else {
78 13 ▶ open $fh, '<', $input or croak "could not open $input: $!";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $input or croak("could not open ${input}: $!") 13 0 13
79 13 binmode $fh;
80 }
81 14 my @keys;
82 14 ▶ while (my $line = readline $fh) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 24 10 34
83 24 ▶ croak "too many input lines" if $. > $max_lines;
Branch: 1 branch
BranchTrueFalseTotal
if $. > $Data::SSHPubkey::max_lines 0 24 24
84 24 ▶ if ($line =~ m{^(-----BEGIN RSA PUBLIC KEY-----)}) {
Branch: 4 branches
BranchTrueFalseTotal
if ($line =~ /^(-----BEGIN RSA PUBLIC KEY-----)/) { } 3 21 24
elsif ($line =~ /^(-----BEGIN PUBLIC KEY-----)/) { } 2 19 21
elsif ($line =~ /^(---- BEGIN SSH2 PUBLIC KEY ----)/) { } 8 11 19
elsif ($line =~ m[ (?<prefix>(?<type>ecdsa)-sha2-nistp256|ssh-(?<type>ed25519|rsa)) [\t ]+? (?<key>[A-Za-z0-9+/=]{64,717}) (?:[\t ]|$) ]x) { } 10 1 11
85 3 my $key = $1;
86 3 my ($ok, $data) = _until_end($fh, '-----END RSA PUBLIC KEY-----');
87 3 ▶ croak "could not parse PEM pubkey: $data" unless defined $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ok 0 3 3
88 3 push @keys, [ 'PEM', $key . $/ . $data ];
89 } elsif ($line =~ m{^(-----BEGIN PUBLIC KEY-----)}) {
90 2 my $key = $1;
91 2 my ($ok, $data) = _until_end($fh, '-----END PUBLIC KEY-----');
92 2 ▶ croak "could not parse PKCS8 pubkey: $data" unless defined $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ok 0 2 2
93 2 push @keys, [ 'PKCS8', $key . $/ . $data ];
94  
95 } elsif ($line =~ m{^(---- BEGIN SSH2 PUBLIC KEY ----)}) {
96 8 my $key = $1;
97 8 my ($ok, $data) = _until_end($fh, '---- END SSH2 PUBLIC KEY ----');
98 7 ▶ croak "could not parse RFC4716 pubkey: $data" unless defined $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ok 2 5 7
99 5 push @keys, [ 'RFC4716', $key . $/ . $data ];
100 } elsif (
101 # long enough for a RSA 4096-bit key, a bit too genereous
102 # for ed25519 so probably should instead be done for each
103 # key type
104 $line =~ m{
105 (?<prefix>(?<type>ecdsa)-sha2-nistp256|ssh-(?<type>ed25519|rsa)) [\t ]+?
106 (?<key>[A-Za-z0-9+/=]{64,717}) (?:[\t ]|$) }x
107 ) {
108 10 push @keys, [ $+{type}, $+{prefix} . ' ' . $+{key} ];
109 }
110 21 ▶ croak "too many keys" if @keys > $max_keys;
Branch: 1 branch
BranchTrueFalseTotal
if @keys > $Data::SSHPubkey::max_keys 1 20 21
111 }
112 10 return @keys;
113 }
114  
115 # this (probably incorrectly) enforces RFC 4716 parsing on all of the
116 # multiline formats so may not be correct for the other two formats,
117 # though attempts are made at supporting them
118 13 ▶sub _until_end {
Subroutine _until_end: called CC=13 SCAR=26.6
119 13 my ($fh, $fin) = @_;
120 13 my $ok;
121 13 my $ret = '';
122 13 ▶ while (my $line = readline $fh) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 86 1 87
123 86 ▶ die "too many input lines" if $. > $max_lines;
Branch: 1 branch
BranchTrueFalseTotal
if $. > $Data::SSHPubkey::max_lines 1 85 86
124 85 ▶ if ($line =~ m/^($fin)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^($fin)/) 10 75 85
125 10 $ret .= $1;
126 10 $ok = 1;
127 10 last;
128 }
129  
130 # RFC 4716 "implementations SHOULD be prepared to read files
131 # using any of the common line termination sequence[s]"
132 75 $line =~ s/(\012|\015|\015\012)$//;
133  
134 # RFC 4716 "line[s] ... MUST NOT be longer than 72 8-bit bytes
135 # excluding line termination characters" (TODO bytes vs. characters)
136 75 ▶ return undef, "line $. too long" if length $line > 72;
Branch: 1 branch
BranchTrueFalseTotal
if length $line > 72 0 75 75
137  
138 # RFC 4716 ignore "key file header" fields as this code pretends
139 # that it cannot recognize any
140 75 ▶ if ($line =~ m/:/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /:/) 7 68 75
141 7 ▶ if ($line =~ m/\\$/) { # backslash continues a line
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\\$/) 4 3 7
142 4 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
not $line =~ /\\$/ or do { do { $line = readline $fh; return undef, "continued to EOF" if eof $fh; $line =~ s/(\012|\015|\015\012)$//; return undef, "line $. too long" if length $line > 72 } } 3 182 185
143 186 $line = readline $fh;
144 186 ▶ return undef, "continued to EOF" if eof $fh;
Branch: 1 branch
BranchTrueFalseTotal
if eof $fh 1 185 186
145 185 $line =~ s/(\012|\015|\015\012)$//;
146 185 ▶ return undef, "line $. too long" if length $line > 72;
Branch: 1 branch
BranchTrueFalseTotal
if length $line > 72 0 185 185
147 } until $line !~ m/\\$/;
148 }
149 6 next;
150 }
151  
152 # RFC 4253 section 6.6 indicates there can be a "signature
153 # format identifier"; those are KLUGE not supported by this
154 # module as I don't know what that specific encoding looks like.
155 # go with a sloppy Base64ish match, meanwhile, as that is what
156 # OpenSSH generates as output
157 68 ▶ if ($line =~ m{^([A-Za-z0-9+/=]{1,72})$}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ m[^([A-Za-z0-9+/=]{1,72})$]) 68 0 68
158 68 $ret .= $1 . $/;
159 68 next;
160 }
161  
162 # support RFC 822 by way of RFC 1421 PEM header extensions that
163 # begin with leading whitespace (sloppy, should only happen for
164 # header lines)
165 0 ▶ next if $line =~ m{^[ \t]};
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^[ \t]/ 0 0 0
166  
167 # support RFC 1421 PEM blank line (poorly, as all blank lines
168 # are ignored)
169 0 ▶ next if $line =~ m{^$};
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^$/ 0 0 0
170  
171 0 return undef, "fell off end of parser at line $.";
172 }
173 11 return $ok, $ret;
174 }
175  
176 3 1;
177 __END__
↑ Summary