DNS/RR/NSEC3.pm

filter: Statementstmt 100% 195 / 195 Branchbran 100% 42 / 42 Conditioncond 100% 23 / 23 MC/DCmcdc 100% 12 / 12 Subroutinesub 100% 32 / 32 Podpod 92.8% 12 / 14 (1 uncoverable) totaltotal 99.6% 316 / 318 (1 uncoverable) CC 41 SCAR 11.7
CC
41
Cov
100%
covers
6.0
algorithm
5.0
encloser
5.0
CRAP 3.2
« DNS/Parameters.pm ↑ Summary DNS/RR/NSEC.pm »
1 package Net::DNS::RR::NSEC3;
2  
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 our $VERSION = (qw$Id: NSEC3.pm 2003 2025-01-21 12:06:06Z willem $)[2];
6  
7 8 ▶use base qw(Net::DNS::RR::NSEC);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10 =head1 NAME
11  
12 Net::DNS::RR::NSEC3 - DNS NSEC3 resource record
13  
14 =cut
15  
16 8 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 8 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 8 our @EXPORT_OK = qw(name2hash);
20  
21 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 8 require Net::DNS::DomainName;
24  
25 8 eval { require Digest::SHA }; ## optional for simple Net::DNS RR
26  
27  
28 160 ▶sub _decode_rdata { ## decode rdata from wire-format octet string
Subroutine _decode_rdata: called CC=1 SCAR=0.0
29 160 my ( $self, $data, $offset ) = @_;
30  
31 160 my $limit = $offset + $self->{rdlength};
32 160 my $ssize = unpack "\@$offset x4 C", $$data;
33 160 my ( $algorithm, $flags, $iterations, $saltbin ) = unpack "\@$offset CCnx a$ssize", $$data;
34 160 @{$self}{qw(algorithm flags iterations saltbin)} = ( $algorithm, $flags, $iterations, $saltbin );
35 160 $offset += 5 + $ssize;
36 160 my $hsize = unpack "\@$offset C", $$data;
37 160 $self->{hnxtname} = unpack "\@$offset x a$hsize", $$data;
38 160 $offset += 1 + $hsize;
39 160 $self->{typebm} = substr $$data, $offset, ( $limit - $offset );
40 160 $self->{hashfn} = _hashfn( $algorithm, $iterations, $saltbin );
41 160 return;
42 }
43  
44  
45 20 ▶sub _encode_rdata { ## encode rdata as wire-format octet string
Subroutine _encode_rdata: called CC=1 SCAR=0.0
46 20 my $self = shift;
47  
48 20 my $salt = $self->saltbin;
49 20 my $hash = $self->{hnxtname};
50 return pack 'CCn C a* C a* a*', $self->algorithm, $self->flags, $self->iterations,
51 length($salt), $salt,
52 length($hash), $hash,
53 20 $self->{typebm};
54 }
55  
56  
57 4 ▶sub _format_rdata { ## format rdata portion of RR string.
Subroutine _format_rdata: called CC=2 SCAR=6.9
58 4 my $self = shift;
59  
60 4 ▶ my @rdata = (
Condition100%2/2
$self->salt || '-'
l!l
31
MC/DC100%1/1
$self->salt || '-'
$self->salt
Truth table100%2/2
A
A= $self->salt
Aresult
11
00
61 $self->algorithm, $self->flags, $self->iterations,
62 $self->salt || '-', $self->hnxtname, $self->typelist
63 );
64 4 return @rdata;
65 }
66  
67  
68 29 ▶sub _parse_rdata { ## populate RR from rdata in argument list
Subroutine _parse_rdata: called CC=2 SCAR=6.9
69 29 my ( $self, @argument ) = @_;
70  
71 29 my $alg = $self->algorithm( shift @argument );
72 29 $self->flags( shift @argument );
73 29 my $iter = $self->iterations( shift @argument );
74 29 my $salt = shift @argument;
75 29 ▶ $self->salt($salt) unless $salt eq '-';
Branch: 1 branch
BranchTrueFalseTotal
unless $salt eq "-" 28 1 29
76 29 $self->hnxtname( shift @argument );
77 29 $self->typelist(@argument);
78 29 $self->{hashfn} = _hashfn( $alg, $iter, $self->{saltbin} );
79 29 return;
80 }
81  
82  
83 6 ▶sub _defaults { ## specify RR attribute default values
Subroutine _defaults: called CC=1 SCAR=0.0
84 6 my $self = shift;
85  
86 6 $self->_parse_rdata( 1, 0, 0, '' );
87 6 return;
88 }
89  
90  
91 64 ▶sub algorithm {
Subroutine algorithm: called CC=5 SCAR=16.1
Pod documented
92 64 my ( $self, $arg ) = @_;
93  
94 64 ▶ unless ( ref($self) ) { ## class method or simple function
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self) 3 61 64
95 3 my $argn = pop;
96 3 ▶ return $argn =~ /[^0-9]/ ? _digestbyname($argn) : _digestbyval($argn);
Branch: 1 branch
BranchTrueFalseTotal
$argn =~ /[^0-9]/ ? : 1 2 3
97 }
98  
99 61 ▶ return $self->{algorithm} unless defined $arg;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg 28 33 61
100 33 ▶ return _digestbyval( $self->{algorithm} ) if $arg =~ /MNEMONIC/i;
Branch: 1 branch
BranchTrueFalseTotal
if $arg =~ /MNEMONIC/i 1 32 33
101 32 return $self->{algorithm} = _digestbyname($arg);
102 }
103  
104  
105 55 ▶sub flags {
Subroutine flags: called CC=3 SCAR=11.0
Pod documented
106 55 my ( $self, @value ) = @_;
107 55 for (@value) { $self->{flags} = 0 + $_ }
108 55 ▶ return $self->{flags} || 0;
Condition100%2/2
$self->{'flags'} || 0
l!l
3421
MC/DC100%1/1
$self->{'flags'} || 0
$self->{'flags'}
Truth table100%2/2
A
A= $self->{'flags'}
Aresult
11
00
109 }
110  
111  
112 6 ▶sub optout {
Subroutine optout: called CC=4 SCAR=13.9
Pod documented
113 6 my ( $self, @value ) = @_;
114 6 ▶ if ( scalar @value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @value) 3 3 6
115 3 for ( $self->{flags} |= 0x01 ) {
116 3 ▶ $_ ^= 0x01 unless shift @value;
Branch: 1 branch
BranchTrueFalseTotal
unless shift @value 2 1 3
117 }
118 }
119 6 return $self->{flags} & 0x01;
120 }
121  
122  
123 55 ▶sub iterations {
Subroutine iterations: called CC=3 SCAR=11.0
Pod documented
124 55 my ( $self, @value ) = @_;
125 55 for (@value) { $self->{iterations} = 0 + $_ }
126 55 ▶ return $self->{iterations} || 0;
Condition100%2/2
$self->{'iterations'} || 0
l!l
3421
MC/DC100%1/1
$self->{'iterations'} || 0
$self->{'iterations'}
Truth table100%2/2
A
A= $self->{'iterations'}
Aresult
11
00
127 }
128  
129  
130 36 ▶sub salt {
Subroutine salt: called CC=3 SCAR=11.0
Pod documented
131 36 my ( $self, @value ) = @_;
132 36 ▶ return unpack "H*", $self->saltbin() unless scalar @value;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @value 6 30 36
133 30 ▶ my @hex = map { /^"*([\dA-Fa-f]*)"*$/ || croak("corrupt hex"); $1 } @value;
Branch: 1 branch
BranchTrueFalseTotal
/^"*([\dA-Fa-f]*)"*$/ or croak("corrupt hex") 29 1 30
134 29 return $self->saltbin( pack "H*", join "", @hex );
135 }
136  
137  
138 55 ▶sub saltbin {
Subroutine saltbin: called CC=3 SCAR=11.0
Pod documented
139 55 my ( $self, @value ) = @_;
140 55 for (@value) { $self->{saltbin} = $_ }
141 55 ▶ return $self->{saltbin} || "";
Condition100%2/2
$self->{'saltbin'} || ''
l!l
3223
MC/DC100%1/1
$self->{'saltbin'} || ''
$self->{'saltbin'}
Truth table100%2/2
A
A= $self->{'saltbin'}
Aresult
11
00
142 }
143  
144  
145 36 ▶sub hnxtname {
Subroutine hnxtname: called CC=3 SCAR=11.0
Pod documented
146 36 my ( $self, @name ) = @_;
147 36 for (@name) { $self->{hnxtname} = _decode_base32hex($_) }
148 36 ▶ return defined(wantarray) ? _encode_base32hex( $self->{hnxtname} ) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined wantarray ? : 5 31 36
149 }
150  
151  
152 2 ▶sub match {
Subroutine match: called CC=1 SCAR=0.0
Pod documented
153 2 my ( $self, $name ) = @_;
154  
155 2 my ($owner) = $self->{owner}->label;
156 2 my $ownerhash = _decode_base32hex($owner);
157  
158 2 my $hashfn = $self->{hashfn};
159 2 return $ownerhash eq &$hashfn($name);
160 }
161  
162 16 ▶sub covers {
Subroutine covers: called CC=6 SCAR=17.9
Pod documented
163 16 my ( $self, $name ) = @_;
164  
165 16 my ( $owner, @zone ) = $self->{owner}->label;
166 16 my $ownerhash = _decode_base32hex($owner);
167 16 my $nexthash = $self->{hnxtname};
168  
169 16 my @label = Net::DNS::DomainName->new($name)->label;
170 16 my @close = @label;
171 16 foreach (@zone) { pop(@close) } # strip zone labels
172 16 ▶ return if lc($name) ne lc( join '.', @close, @zone ); # out of zone
Branch: 1 branch
BranchTrueFalseTotal
if lc $name ne lc join(".", @close, @zone) 2 14 16
173  
174 14 my $hashfn = $self->{hashfn};
175  
176 14 foreach (@close) {
177 21 my $hash = &$hashfn( join '.', @label );
178 21 my $cmp1 = $hash cmp $ownerhash;
179 21 ▶ last unless $cmp1; # stop at provable encloser
Branch: 1 branch
BranchTrueFalseTotal
unless $cmp1 5 16 21
180 16 ▶ return 1 if ( $cmp1 + ( $nexthash cmp $hash ) ) == 2;
Branch: 1 branch
BranchTrueFalseTotal
if $cmp1 + ($nexthash cmp $hash) == 2 5 11 16
181 11 shift @label;
182 }
183 9 return;
184 }
185  
186  
187 4 ▶sub encloser {
Subroutine encloser: called CC=5 SCAR=16.1
Pod documented
188 4 my ( $self, $qname ) = @_;
189  
190 4 my ( $owner, @zone ) = $self->{owner}->label;
191 4 my $ownerhash = _decode_base32hex($owner);
192 4 my $nexthash = $self->{hnxtname};
193  
194 4 my @label = Net::DNS::DomainName->new($qname)->label;
195 4 my @close = @label;
196 4 foreach (@zone) { pop(@close) } # strip zone labels
197 4 ▶ return if lc($qname) ne lc( join '.', @close, @zone ); # out of zone
Branch: 1 branch
BranchTrueFalseTotal
if lc $qname ne lc join(".", @close, @zone) 1 3 4
198  
199 3 my $hashfn = $self->{hashfn};
200  
201 3 my $encloser = $qname;
202 3 foreach (@close) {
203 10 my $nextcloser = $encloser;
204 10 shift @label;
205 10 my $hash = &$hashfn( $encloser = join '.', @label );
206 10 ▶ next if $hash ne $ownerhash;
Branch: 1 branch
BranchTrueFalseTotal
if $hash ne $ownerhash 8 2 10
207 2 $self->{nextcloser} = $nextcloser; # next closer name
208 2 $self->{wildcard} = "*.$encloser"; # wildcard at provable encloser
209 2 return $encloser; # provable encloser
210 }
211 1 return;
212 }
213  
214  
215 2 ▶sub nextcloser { return shift->{nextcloser}; }
Subroutine nextcloser: called CC=1 SCAR=0.0
Pod documented
216  
217 2 ▶sub wildcard { return shift->{wildcard}; }
Subroutine wildcard: called CC=1 SCAR=0.0
Pod documented
218  
219  
220 ########################################
221  
222 8 my @digestbyname = (
223 'SHA-1' => 1, # [RFC3658]
224 );
225  
226 8 my @digestalias = ( 'SHA' => 1 );
227  
228 8 my %digestbyval = reverse @digestbyname;
229  
230 8 foreach (@digestbyname) { s/[\W_]//g; } # strip non-alphanumerics
231 8 ▶my @digestrehash = map { /^\d/ ? ($_) x 3 : uc($_) } @digestbyname;
Branch: 1 branch
BranchTrueFalseTotal
/^\d/ ? : 8 8 16
232 8 my %digestbyname = ( @digestalias, @digestrehash ); # work around broken cperl
233  
234 33 ▶sub _digestbyname {
Subroutine _digestbyname: called CC=2 SCAR=6.9
235 33 my $arg = shift;
236 33 my $key = uc $arg; # synthetic key
237 33 $key =~ s/[\W_]//g; # strip non-alphanumerics
238 33 my $val = $digestbyname{$key};
239 33 ▶ croak qq[unknown algorithm $arg] unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 1 32 33
240 32 return $val;
241 }
242  
243 3 ▶sub _digestbyval {
Subroutine _digestbyval: called CC=2 SCAR=6.9
244 3 my $value = shift;
245 3 ▶ return $digestbyval{$value} || return $value;
Condition100%2/2
$digestbyval{$value} || (return $value)
l!l
21
MC/DC100%1/1
$digestbyval{$value} || (return $value)
$digestbyval{$value}
Truth table100%2/2
A
A= $digestbyval{$value}
Aresult
11
00
246 }
247  
248  
249 my %digest = (
250 8 '1' => scalar( eval { Digest::SHA->new(1) } ), # RFC3658
251 );
252  
253  
254 63 ▶sub _decode_base32hex {
Subroutine _decode_base32hex: called CC=2 SCAR=6.9
255 63 ▶ local $_ = shift || '';
Condition100%2/2
shift() || ''
l!l
576
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
256 63 tr [0-9A-Va-v\060-\071\101-\126\141-\166] [\000-\037\012-\037\000-\037\012-\037];
257 63 my $l = ( 5 * length ) & ~7;
258 63 return pack "B$l", join '', map { unpack( 'x3a5', unpack 'B8', $_ ) } split //;
259 }
260  
261  
262 27 ▶sub _encode_base32hex {
Subroutine _encode_base32hex: called CC=1 SCAR=0.0
263 27 my @split = grep {length} split /(\S{5})/, unpack 'B*', shift;
264 27 local $_ = join '', map { pack( 'B*', "000$_" ) } @split;
265 27 tr [\000-\037] [0-9a-v];
266 27 return $_;
267 }
268  
269  
270 8 my ( $cache1, $cache2, $limit ) = ( {}, {}, 10 );
271  
272 202 ▶sub _hashfn {
Subroutine _hashfn: called CC=4 SCAR=13.9
273 202 my $hashalg = shift;
274 202 ▶ my $iterations = shift || 0;
Condition100%2/2
shift() || 0
l!l
35167
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
275 202 ▶ my $salt = shift || '';
Condition100%2/2
shift() || ''
l!l
34168
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
276  
277 202 my $hash = $digest{$hashalg};
278 1 ▶ return sub { croak "algorithm $hashalg not supported" }
Subroutine __ANON__: called CC=1 SCAR=0.0
279 202 ▶ unless $hash;
Branch: 1 branch
BranchTrueFalseTotal
unless $hash 1 201 202
280 201 my $clone = $hash->clone;
281  
282 201 my $key_adjunct = pack 'Cna*', $hashalg, $iterations, $salt;
283  
284 45 ▶ return sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
285 45 my $name = Net::DNS::DomainName->new(shift)->canonical;
286 45 my $key = join '', $name, $key_adjunct;
287 45 ▶ my $cache = $$cache1{$key} ||= $$cache2{$key}; # two layer cache
Condition100%3/3
$cache1->{$key} ||= $cache2->{$key}
l!l&&r!l&&!r
15327
MC/DC100%2/2
$cache1->{$key} ||= $cache2->{$key}
$cache1->{$key} $cache2->{$key}
Truth table100%3/3
A ||= B
A= $cache1->{$key}B= $cache2->{$key}
ABresult
1X1
011
000
288 45 ▶ return $cache if defined $cache;
Branch: 1 branch
BranchTrueFalseTotal
if defined $cache 18 27 45
289 27 ▶ ( $cache1, $cache2, $limit ) = ( {}, $cache1, 50 ) unless $limit--; # recycle cache
Branch: 1 branch
BranchTrueFalseTotal
unless $limit-- 2 25 27
290  
291 27 $clone->add($name);
292 27 $clone->add($salt);
293 27 my $digest = $clone->digest;
294 27 my $count = $iterations;
295 27 ▶ while ( $count-- ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count--) 312 27 339
296 312 $clone->add($digest);
297 312 $clone->add($salt);
298 312 $digest = $clone->digest;
299 }
300 27 return $$cache1{$key} = $digest;
301 201 };
302 }
303  
304  
305 2 ▶sub hashalgo { return &algorithm; } # uncoverable pod
Subroutine hashalgo: called CC=1 SCAR=0.0
Pod undocumented (uncoverable)
306  
307 13 ▶sub name2hash {
Subroutine name2hash: called CC=3 SCAR=11.0
Pod undocumented
308 13 my $hashalg = shift; # uncoverable pod
309 13 my $name = shift;
310 13 ▶ my $iterations = shift || 0;
Condition100%2/2
shift() || 0
l!l
112
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
311 13 ▶ my $salt = pack 'H*', shift || '';
Condition100%2/2
shift() || ''
l!l
112
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
312 13 my $hash = _hashfn( $hashalg, $iterations, $salt );
313 13 return _encode_base32hex( &$hash($name) );
314 }
315  
316 ########################################
317  
318  
319 8 1;
320 __END__
« DNS/Parameters.pm ↑ Summary DNS/RR/NSEC.pm »