blib/lib/Net/DNS/SEC/Keyset.pm

filter: Statementstmt 20.3% 21 / 103 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 31.5% 6 / 19 Podpod 100% 9 / 9 totaltotal 21.1% 36 / 170 CC 22 SCAR 37.8
CC
22
Cov
15%
verify
132.0
writekeyset
30.0
new
20.0
CRAP 43.7
↑ Summary blib/lib/Net/DNS/SEC/RSA.pm »
1 package Net::DNS::SEC::Keyset;
2  
3 #
4 # $Id: Keyset.pm 1613 2018-01-15 13:47:13Z willem $
5 #
6 2 our $VERSION = (qw$LastChangedRevision: 1613 $)[1];
7  
8  
9 =head1 NAME
10  
11 Net::DNS::SEC::Keyset - DNSSEC Keyset object class
12  
13  
14 =head1 SYNOPSIS
15  
16 use Net::DNS::SEC::Keyset;
17  
18  
19 =head1 DESCRIPTION
20  
21 A keyset is an "administrative" unit used for DNSSEC maintenance.
22  
23 This class provides interfaces for creating, reading and writing keysets.
24  
25 Object methods are provided to extract DNSKEY, RRSIG and DS records.
26  
27 Note that this class is still being developed.
28 Attributes and methods are subject to change.
29  
30 =cut
31  
32  
33 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 2 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 2 ▶use File::Spec::Functions;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 2 ▶use Net::DNS::ZoneFile;
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 2 our $keyset_err;
42  
43  
44 sub new {
45 0 ▶ my ( $class, $arg1, $arg2 ) = @_;
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
46  
47 0 my $ref1 = ref($arg1);
48 0 ▶ return &_new_from_file unless $ref1;
Branch: 1 branch
BranchTrueFalseTotal
unless $ref1 0 0 0
49  
50 0 ▶ return &_new_from_packet if $ref1 eq 'Net::DNS::Packet';
Branch: 1 branch
BranchTrueFalseTotal
if $ref1 eq "Net::DNS::Packet" 0 0 0
51  
52 0 ▶ return &_new_from_keys unless ref($arg2);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $arg2 0 0 0
53  
54 0 return &_new_from_keys_sigs;
55 }
56  
57  
58 =head2 new (from file)
59  
60 $keyset = Net::DNS::SEC::Keyset->new( $filename );
61 $keyset = Net::DNS::SEC::Keyset->new( $filename, $directory );
62 die Net::DNS::SEC::Keyset->keyset_err unless $keyset;
63  
64 Constructor method which reads the specified keyset file and returns a
65 keyset object.
66  
67 The optional second argument specifies the filename base directory.
68  
69 Sets keyset_err and returns undef on failure.
70  
71 =cut
72  
73 sub _new_from_file {
74 0 ▶ my ( $class, $name, @path ) = @_;
Subroutine _new_from_file: not called CC=1 SCAR=6.9
75  
76 0 my $file = catfile( @path, $name );
77  
78 0 my @rr = new Net::DNS::ZoneFile($file)->read;
79  
80 0 return $class->_new_from_keys_sigs( \@rr, \@rr );
81 }
82  
83  
84 =head2 new (by signing keys)
85  
86 $keyset = Net::DNS::SEC::Keyset->new( \@keyrr, $privatekeypath );
87  
88 Creates a keyset object from the keys provided through the reference to an
89 array of Net::DNS::RR::Key objects.
90  
91 The method will create and self-sign the whole keyset. The private keys as
92 generated by the BIND dnssec-keygen tool are assumed to be in the current
93 directory or, if specified, the directory indicated by $privatekeypath.
94  
95 Sets keyset_err and returns undef on failure.
96  
97 =cut
98  
99 sub _new_from_keys {
100 0 ▶ my ( $class, $keylist, @keypath ) = @_;
Subroutine _new_from_keys: not called CC=2 SCAR=17.9
101  
102 0 my @sigrr;
103 0 foreach my $key ( grep $_->type eq 'DNSKEY', @$keylist ) {
104 0 my $keyname = $key->privatekeyname;
105 0 my $keyfile = catfile( @keypath, $keyname );
106 0 my @rrsig = Net::DNS::RR::RRSIG->create( $keylist, $keyfile );
107 0 push @sigrr, grep defined, @rrsig;
108 }
109  
110 0 return $class->_new_from_keys_sigs( $keylist, \@sigrr );
111 }
112  
113  
114 =head2 new (from key and sig RRsets)
115  
116 $keyset = Net::DNS::Keyset->new( \@keyrr, \@sigrr );
117  
118 Creates a keyset object from the keys provided through the references
119 to arrays of Net::DNS::RR::DNSKEY and Net::DNS::RR::RRSIG objects.
120  
121 Sets keyset_err and returns undef on failure.
122  
123 =cut
124  
125 sub _new_from_keys_sigs {
126 0 ▶ my ( $class, $key_ref, $sig_ref ) = @_;
Subroutine _new_from_keys_sigs: not called CC=2 SCAR=17.9
127  
128 0 my @keyrr = grep $_->type eq 'DNSKEY', @$key_ref;
129 0 my @sigrr = grep $_->type eq 'RRSIG', @$sig_ref;
130  
131 0 my $keyset = bless {keys => \@keyrr, sigs => \@sigrr}, $class;
132  
133 0 ▶ return scalar( $keyset->verify ) ? $keyset : undef;
Branch: 1 branch
BranchTrueFalseTotal
scalar $keyset->verify ? : 0 0 0
134 }
135  
136  
137 =head2 new (from Packet)
138  
139 $res = Net::DNS::Resolver->new;
140 $res->dnssec(1);
141
142 $packet = $res->query ( "example.com", "DNSKEY", "IN" );
143  
144 $keyset = Net::DNS::SEC::Keyset->new( $packet )
145  
146 Creates a keyset object from a Net::DNS::Packet that contains the answer
147 to a query for the apex key records.
148  
149 This is the method you should use for automatically fetching keys.
150  
151 Sets keyset_err and returns undef on failure.
152  
153 =cut
154  
155 sub _new_from_packet {
156 0 ▶ my ( $class, $packet ) = @_;
Subroutine _new_from_packet: not called CC=1 SCAR=6.9
157 0 my @rrset = $packet->answer;
158 0 return $class->_new_from_keys_sigs( \@rrset, \@rrset );
159 }
160  
161  
162 =head2 keys
163  
164 @keyrr = $keyset->keys;
165  
166 Returns an array of Net::DNS::RR::DNSKEY objects.
167  
168 =cut
169  
170 sub keys {
171 0 ▶ my $self = shift;
Subroutine keys: not called CC=1 SCAR=6.9
Pod documented
172 0 my @keys = @{$self->{keys}};
173 }
174  
175  
176 =head2 sigs
177  
178 @sigrr = $keyset->sigs;
179  
180 Returns an array of Net::DNS::RR::RRSIG objects.
181  
182 =cut
183  
184 sub sigs {
185 0 ▶ my $self = shift;
Subroutine sigs: not called CC=1 SCAR=6.9
Pod documented
186 0 my @sigs = @{$self->{sigs}};
187 }
188  
189  
190 =head2 extract_ds
191  
192 @ds = $keyset->extract_ds;
193 die $keyset->keyset_err unless @ds;
194  
195 Extracts DS records from the keyset. Note that the keyset will be verified
196 during extraction. All keys will need to have a valid self-signature.
197  
198 The method sets keyset_err if verification fails.
199  
200 =cut
201  
202 sub extract_ds {
203 0 ▶ my $self = shift;
Subroutine extract_ds: not called CC=2 SCAR=17.9
Pod documented
204 0 my @ds;
205 0 ▶ @ds = map Net::DNS::RR::DS->create($_), $self->keys if $self->verify;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verify 0 0 0
206 0 return @ds;
207 }
208  
209  
210 =head2 verify
211  
212 @keytags = $keyset->verify();
213 die $keyset->keyset_err unless @keytags;
214  
215 $keyset->verify( $keytag ) || die $keyset->keyset_err;
216  
217 If no arguments are given:
218  
219 =over 2
220  
221 =item
222  
223 Verifies if all signatures present verify the keyset.
224  
225 =item
226  
227 Verifies if there are DNSKEYs with the SEP flag set, there is at
228 least one RRSIG made using that key.
229  
230 =item
231  
232 Verifies that if there are no DNSKEYs with the SEP flag set there
233 is at least one RRSIG made with one of the keys from the keyset.
234  
235 =back
236  
237 If an argument is given, it is should be the numeric keytag of the key
238 in the keyset which will be verified using the corresponding RRSIG.
239  
240 The method returns a list of keytags of verified keys in the keyset.
241  
242 The method sets keyset_err and returns empty list if verification fails.
243  
244 =cut
245  
246 sub verify {
247 0 ▶ my ( $self, $keyid ) = @_;
Subroutine verify: not called CC=11 SCAR=48.8
Pod documented
248  
249 0 my @keys = $self->keys;
250  
251 0 my %keysbytag;
252 0 push( @{$keysbytag{$_->keytag}}, $_ ) foreach @keys;
253  
254 0 my @sigs = $self->sigs;
255  
256 0 my @keyset_err;
257 0 my %names = map { ( $_->name => $_ ) } @keys, @sigs;
258 0 my @names = CORE::keys %names;
259 0 ▶ push @keyset_err, "Multiple names in keyset: @names" if scalar(@names) > 1;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @names > 1 0 0 0
260  
261 0 ▶ if ($keyid) {
Branch: 2 branches
BranchTrueFalseTotal
if ($keyid) { } 0 0 0
elsif (my(@sepkeys) = grep($_->sep, @keys)) { } 0 0 0
262 0 @sigs = grep $_->keytag == $keyid, @sigs;
263 0 ▶ push @keyset_err, "No signature made with $keyid found" unless @sigs;
Branch: 1 branch
BranchTrueFalseTotal
unless @sigs 0 0 0
264 } elsif ( my @sepkeys = grep $_->sep, @keys ) {
265 0 my %sepkey = map { ( $_->keytag => $_ ) } @sepkeys;
266 push @keyset_err, 'No signature found for key with SEP flag'
267 0 ▶ unless grep $sepkey{$_->keytag}, @sigs;
Branch: 1 branch
BranchTrueFalseTotal
unless grep $sepkey{$_->keytag}, @sigs 0 0 0
268 }
269  
270 0 foreach my $sig (@sigs) {
271 0 my $keytag = $sig->keytag;
272 0 ▶ next if $sig->verify( \@keys, $keysbytag{$keytag} || [] );
Branch: 1 branch
BranchTrueFalseTotal
if $sig->verify(\@keys, $keysbytag{$keytag} || []) 0 0 0
Condition0%0/2
$keysbytag{$keytag} || []
l!l
00
MC/DC0%0/1
$keysbytag{$keytag} || []
$keysbytag{$keytag}
Truth table0%0/2
A
A= $keysbytag{$keytag}
Aresult
11
00
273 0 my $vrfyerr = $sig->vrfyerrstr;
274 0 my $signame = $sig->signame;
275 0 push @keyset_err, "$vrfyerr on key $signame $keytag ";
276 }
277  
278 0 $keyset_err = join "\n", @keyset_err;
279  
280 0 my @tags_verified;
281 0 ▶ @tags_verified = map $_->keytag, @sigs unless $keyset_err;
Branch: 1 branch
BranchTrueFalseTotal
unless $Net::DNS::SEC::Keyset::keyset_err 0 0 0
282 0 return @tags_verified;
283 }
284  
285  
286 =head2 keyset_err
287
288 $keyset_err = Net::DNS::SEC::Keyset->keyset_err;
289  
290 $keyset_err = $keyset->keyset_err;
291  
292 Returns the keyset error string.
293  
294 =cut
295  
296 sub keyset_err {
297 0 ▶ return $keyset_err;
Subroutine keyset_err: not called CC=1 SCAR=6.9
Pod documented
298 }
299  
300  
301 =head2 string
302
303 $string = $keyset->string;
304  
305 Returns a string representation of the keyset.
306  
307 =cut
308  
309 sub string {
310 0 ▶ my $self = shift;
Subroutine string: not called CC=1 SCAR=6.9
Pod documented
311 0 return join "\n", map $_->string, ( $self->keys, $self->sigs );
312 }
313  
314  
315 =head2 print
316  
317 $keyset->print; # similar to print( $keyset->string )
318  
319 Prints the keyset.
320  
321 =cut
322  
323 sub print {
324 0 ▶ my $self = shift;
Subroutine print: not called CC=2 SCAR=17.9
Pod documented
325 0 foreach ( $self->keys, $self->sigs ) { $_->print }
326 }
327  
328  
329 =head2 writekeyset
330  
331 $keyset->writekeyset;
332 $keyset->writekeyset( $path );
333 $keyset->writekeyset( $prefix );
334 $keyset->writekeyset( $prefix, $path );
335  
336 Writes the keyset to a file named "keyset-<domain>." in the current
337 working directory or directory defined by the optional $path argument.
338  
339 The optional $prefix argument specifies the prefix that will be
340 prepended to the domain name to form the keyset filename.
341  
342 =cut
343  
344 sub writekeyset {
345 0 ▶ my $self = shift;
Subroutine writekeyset: not called CC=5 SCAR=34.0
Pod documented
346 0 my ( $arg1, @path ) = @_;
347 0 ▶ @path = shift() if $arg1 && file_name_is_absolute($arg1);
Branch: 1 branch
BranchTrueFalseTotal
if $arg1 and file_name_is_absolute($arg1) 0 0 0
Condition0%0/3
$arg1 and file_name_is_absolute($arg1)
!ll&&!rl&&r
000
MC/DC0%0/2
$arg1 and file_name_is_absolute($arg1)
$arg1 file_name_is_absolute($arg1)
Truth table0%0/3
A and B
A= $arg1B= file_name_is_absolute($arg1)
ABresult
0X0
100
111
348 0 ▶ my $prefix = shift || 'keyset-';
Condition0%0/2
shift() || 'keyset-'
l!l
00
MC/DC0%0/1
shift() || 'keyset-'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
349  
350 0 my @keysetrr = ( $self->keys, $self->sigs );
351 0 my $domainname = $keysetrr[0]->name;
352 0 my $keysetname = "$prefix$domainname.";
353 0 my $filename = catfile( @path, $keysetname );
354 0 $filename =~ s/[.]+/\./; ## avoid antisocial consequences of $path with ..
355 0 ▶ open( KEYSET, ">$filename" ) or croak qq(open: "$filename" $!);
Branch: 1 branch
BranchTrueFalseTotal
open KEYSET, ">$filename" or croak(qq[open: "$filename" $!]) 0 0 0
356 0 select( ( select(KEYSET), $self->print )[0] );
357 0 close(KEYSET);
358 0 return $filename;
359 }
360  
361  
362 2 1;
363  
364 __END__
↑ Summary blib/lib/Net/DNS/SEC/RSA.pm »