DNS/RR/TSIG.pm

filter: Statementstmt 100% 309 / 309 Branchbran 100% 104 / 104 Conditioncond 100% 27 / 27 MC/DCmcdc 100% 14 / 14 Subroutinesub 100% 45 / 45 Podpod 100% 21 / 23 (2 uncoverable) totaltotal 100% 520 / 522 (2 uncoverable) CC 75 SCAR 19.2
CC
75
Cov
100%
create
19.0
verify
14.0
sig_data
8.0
CRAP 6.8
« DNS/Packet.pm ↑ Summary DNS/Resolver/Base.pm »
1 package Net::DNS::RR::TSIG;
2  
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 our $VERSION = (qw$Id: TSIG.pm 2060 2026-08-31 11:29:36Z willem $)[2];
6  
7 7 ▶use base qw(Net::DNS::RR);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10 =head1 NAME
11  
12 Net::DNS::RR::TSIG - DNS TSIG resource record
13  
14 =cut
15  
16 7 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 7 ▶use Net::DNS::DomainName;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 7 ▶use Net::DNS::Parameters qw(:class :type :rcode);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 7 ▶use constant SYMLINK => defined(&CORE::readlink); # Except Win32, VMS, RISC OS
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 7 ▶use constant ANY => classbyname q(ANY);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 7 ▶use constant TSIG => typebyname q(TSIG);
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 7 eval { require Digest::HMAC };
29 7 eval { require Digest::MD5 };
30 7 eval { require Digest::SHA };
31 7 eval { require MIME::Base64 };
32  
33  
34 27 ▶sub _decode_rdata { ## decode rdata from wire-format octet string
Subroutine _decode_rdata: called CC=2 SCAR=6.9
35 27 my ( $self, $data, $offset ) = @_;
36  
37 27 my $limit = $offset + delete $self->{rdlength};
38 27 ▶ croak('misplaced or corrupt TSIG') unless $limit == length $$data;
Branch: 1 branch
BranchTrueFalseTotal
unless $limit == length $$data 1 26 27
39  
40 26 ( $self->{algorithm}, $offset ) = Net::DNS::DomainName->decode( $data, $offset );
41  
42 # Design decision: Use 32 bits, which will work until the end of time()!
43 26 @{$self}{qw(time_signed fudge)} = unpack "\@$offset xxN n", $$data;
44 26 $offset += 8;
45  
46 26 my $mac_size = unpack "\@$offset n", $$data;
47 26 $self->{macbin} = unpack "\@$offset xx a$mac_size", $$data;
48 26 $offset += $mac_size + 2;
49  
50 26 @{$self}{qw(original_id error)} = unpack "\@$offset nn", $$data;
51 26 $offset += 4;
52  
53 26 my $other_size = unpack "\@$offset n", $$data;
54 26 $self->{other} = unpack "\@$offset xx a$other_size", $$data;
55 26 $offset += $other_size + 2;
56  
57 26 my $raw = substr $$data, 0, $self->{offset}++;
58 26 $self->{rawref} = \$raw;
59 26 return;
60 }
61  
62  
63 36 ▶sub _encode_rdata { ## encode rdata as wire-format octet string
Subroutine _encode_rdata: called CC=3 SCAR=11.0
64 36 my $self = shift;
65  
66 36 my $offset = shift;
67 36 my $undef = shift;
68 36 my $message = shift;
69 36 ▶ my $algorithm = $self->{algorithm} || return '';
Condition100%2/2
$self->{'algorithm'} || (return '')
l!l
351
MC/DC100%1/1
$self->{'algorithm'} || (return '')
$self->{'algorithm'}
Truth table100%2/2
A
A= $self->{'algorithm'}
Aresult
11
00
70 35 my $macbin = $self->macbin;
71 35 ▶ unless ($macbin) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($macbin) 30 5 35
72 30 my $sigdata = $self->sig_data($message); # form data to be signed
73 30 $macbin = $self->macbin( $self->_mac_function($sigdata) );
74 }
75  
76 35 my $rdata = $algorithm->canonical;
77  
78 # Design decision: Use 32 bits, which will work until the end of time()!
79 35 $rdata .= pack 'xxN n', $self->time_signed, $self->fudge;
80  
81 35 $rdata .= pack 'na*', length($macbin), $macbin;
82  
83 35 $rdata .= pack 'nn', $self->original_id, $self->{error};
84  
85 35 my $other = $self->other;
86 35 $rdata .= pack 'na*', length($other), $other;
87  
88 35 return $rdata;
89 }
90  
91  
92 6 ▶sub _defaults { ## specify RR attribute default values
Subroutine _defaults: called CC=1 SCAR=0.0
93 6 my $self = shift;
94  
95 6 $self->algorithm(157);
96 6 $self->class('ANY');
97 6 $self->error(0);
98 6 $self->fudge(300);
99 6 $self->other('');
100 6 return;
101 }
102  
103  
104 2 ▶sub _size { ## estimate encoded size
Subroutine _size: called CC=1 SCAR=0.0
105 2 my $self = shift;
106 2 my $clone = bless {%$self}, ref($self); # shallow clone
107 2 return length $clone->encode( 0, undef, Net::DNS::Packet->new() );
108 }
109  
110  
111 35 ▶sub encode { ## override RR method
Subroutine encode: called CC=2 SCAR=6.9
Pod documented
112 35 my ( $self, @argument ) = @_;
113 35 my $kname = $self->{owner}->encode(); # uncompressed key name
114 35 ▶ my $rdata = eval { $self->_encode_rdata(@argument) } || '';
Condition100%2/2
eval { do { $self->_encode_rdata(@argument) } } || ''
l!l
341
MC/DC100%1/1
eval { do { $self->_encode_rdata(@argument) } } || ''
eval { do { $self->_encode_rdata(@argument) } }
Truth table100%2/2
A
A= eval { do { $self->_encode_rdata(@argument) } }
Aresult
11
00
115 35 return pack 'a* n2 N n a*', $kname, TSIG, ANY, 0, length $rdata, $rdata;
116 }
117  
118  
119 3 ▶sub string { ## override RR method
Subroutine string: called CC=2 SCAR=6.9
Pod documented
120 3 my $self = shift;
121 3 my $name = $self->{owner}->string;
122 3 my $type = $self->type;
123 3 ▶ return "; $name $type ; no data" unless $self->{algorithm};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"algorithm"} 1 2 3
124 2 my $algorithm = $self->algorithm;
125 2 my $time_signed = $self->time_signed;
126 2 my $fudge = $self->fudge;
127 2 my $signature = $self->mac;
128 2 my $original_id = $self->original_id;
129 2 my $error = $self->error;
130 2 my $other = $self->other;
131  
132 2 return <<"QQ";
133 ; $name $type
134 ; algorithm: $algorithm
135 ; time signed: $time_signed fudge: $fudge
136 ; signature: $signature
137 ; original id: $original_id
138 ; error: $error $other
139 QQ
140 }
141  
142  
143 120 ▶sub algorithm { return &_algorithm; }
Subroutine algorithm: called CC=1 SCAR=0.0
Pod documented
144  
145  
146 26 ▶sub key {
Subroutine key: called CC=2 SCAR=6.9
Pod documented
147 26 my ( $self, @argument ) = @_;
148 26 ▶ return MIME::Base64::encode( $self->keybin(), "" ) unless scalar @argument;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @argument 1 25 26
149 25 return $self->keybin( MIME::Base64::decode( join "", @argument ) );
150 }
151  
152  
153 27 ▶sub keybin { return &_keybin; }
Subroutine keybin: called CC=1 SCAR=0.0
Pod documented
154  
155  
156 128 ▶sub time_signed {
Subroutine time_signed: called CC=3 SCAR=11.0
Pod documented
157 128 my ( $self, @value ) = @_;
158 128 for (@value) { $self->{time_signed} = 0 + $_ }
159 128 ▶ return $self->{time_signed} ? $self->{time_signed} : ( $self->{time_signed} = time() );
Branch: 1 branch
BranchTrueFalseTotal
$self->{'time_signed'} ? : 102 26 128
160 }
161  
162  
163 136 ▶sub fudge {
Subroutine fudge: called CC=3 SCAR=11.0
Pod documented
164 136 my ( $self, @value ) = @_;
165 136 for (@value) { $self->{fudge} = 0 + $_ }
166 136 ▶ return $self->{fudge} || 0;
Condition100%2/2
$self->{'fudge'} || 0
l!l
1288
MC/DC100%1/1
$self->{'fudge'} || 0
$self->{'fudge'}
Truth table100%2/2
A
A= $self->{'fudge'}
Aresult
11
00
167 }
168  
169  
170 5 ▶sub mac {
Subroutine mac: called CC=2 SCAR=6.9
Pod documented
171 5 my ( $self, @value ) = @_;
172 5 ▶ return MIME::Base64::encode( $self->macbin(), "" ) unless scalar @value;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @value 4 1 5
173 1 return $self->macbin( MIME::Base64::decode( join "", @value ) );
174 }
175  
176  
177 151 ▶sub macbin {
Subroutine macbin: called CC=3 SCAR=11.0
Pod documented
178 151 my ( $self, @value ) = @_;
179 151 for (@value) { $self->{macbin} = $_ }
180 151 ▶ return $self->{macbin} || "";
Condition100%2/2
$self->{'macbin'} || ''
l!l
10546
MC/DC100%1/1
$self->{'macbin'} || ''
$self->{'macbin'}
Truth table100%2/2
A
A= $self->{'macbin'}
Aresult
11
00
181 }
182  
183  
184 3 ▶sub prior_mac {
Subroutine prior_mac: called CC=2 SCAR=6.9
Pod documented
185 3 my ( $self, @value ) = @_;
186 3 ▶ return MIME::Base64::encode( $self->prior_macbin(), "" ) unless scalar @value;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @value 2 1 3
187 1 return $self->prior_macbin( MIME::Base64::decode( join "", @value ) );
188 }
189  
190  
191 93 ▶sub prior_macbin {
Subroutine prior_macbin: called CC=3 SCAR=11.0
Pod documented
192 93 my ( $self, @value ) = @_;
193 93 for (@value) { $self->{prior_macbin} = $_ }
194 93 ▶ return $self->{prior_macbin} || "";
Condition100%2/2
$self->{'prior_macbin'} || ''
l!l
3954
MC/DC100%1/1
$self->{'prior_macbin'} || ''
$self->{'prior_macbin'}
Truth table100%2/2
A
A= $self->{'prior_macbin'}
Aresult
11
00
195 }
196  
197  
198 3 ▶sub request_mac {
Subroutine request_mac: called CC=2 SCAR=6.9
Pod documented
199 3 my ( $self, @value ) = @_;
200 3 ▶ return MIME::Base64::encode( $self->request_macbin(), "" ) unless scalar @value;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @value 2 1 3
201 1 return $self->request_macbin( MIME::Base64::decode( join "", @value ) );
202 }
203  
204  
205 60 ▶sub request_macbin {
Subroutine request_macbin: called CC=3 SCAR=11.0
Pod documented
206 60 my ( $self, @value ) = @_;
207 60 for (@value) { $self->{request_macbin} = $_ }
208 60 ▶ return $self->{request_macbin} || "";
Condition100%2/2
$self->{'request_macbin'} || ''
l!l
2634
MC/DC100%1/1
$self->{'request_macbin'} || ''
$self->{'request_macbin'}
Truth table100%2/2
A
A= $self->{'request_macbin'}
Aresult
11
00
209 }
210  
211  
212 95 ▶sub original_id {
Subroutine original_id: called CC=3 SCAR=11.0
Pod documented
213 95 my ( $self, @value ) = @_;
214 95 for (@value) { $self->{original_id} = 0 + $_ }
215 95 ▶ return $self->{original_id} || 0;
Condition100%2/2
$self->{'original_id'} || 0
l!l
932
MC/DC100%1/1
$self->{'original_id'} || 0
$self->{'original_id'}
Truth table100%2/2
A
A= $self->{'original_id'}
Aresult
11
00
216 }
217  
218  
219 48 ▶sub error {
Subroutine error: called CC=4 SCAR=13.9
Pod documented
220 48 my ( $self, @value ) = @_;
221 48 for (@value) {
222 20 my $error = $self->{error} = rcodebyname($_);
223 20 ▶ $self->other( time() ) if $error == 18;
Branch: 1 branch
BranchTrueFalseTotal
if $error == 18 2 18 20
224 }
225 48 ▶ return rcodebyval( $self->{error} || '' );
Condition100%2/2
$self->{'error'} || ''
l!l
3018
MC/DC100%1/1
$self->{'error'} || ''
$self->{'error'}
Truth table100%2/2
A
A= $self->{'error'}
Aresult
11
00
226 }
227  
228  
229 90 ▶sub other {
Subroutine other: called CC=4 SCAR=13.9
Pod documented
230 90 my ( $self, @value ) = @_;
231 90 ▶ for (@value) { $self->{other} = $_ ? pack( 'xxN', $_ ) : '' }
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 3 6 9
232 90 ▶ return $self->{other} ? unpack( 'N', $self->{other} ) : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'other'} ? : 6 84 90
233 }
234  
235  
236 1 ▶sub other_data { return &other; } # uncoverable pod
Subroutine other_data: called CC=1 SCAR=0.0
Pod undocumented (uncoverable)
237  
238  
239 51 ▶sub sig_function {
Subroutine sig_function: called CC=2 SCAR=6.9
Pod documented
240 51 my ( $self, @value ) = @_;
241 51 for (@value) { $self->{sig_function} = $_ }
242 51 return $self->{sig_function};
243 }
244  
245 1 ▶sub sign_func { return &sig_function; } # uncoverable pod
Subroutine sign_func: called CC=1 SCAR=0.0
Pod undocumented (uncoverable)
246  
247  
248 60 ▶sub sig_data {
Subroutine sig_data: called CC=8 SCAR=20.8
Pod documented
249 60 my ( $self, $message ) = @_;
250  
251 60 ▶ if ( ref($message) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $message) 59 1 60
252 59 ▶ die 'missing packet reference' unless $message->isa('Net::DNS::Packet');
Branch: 1 branch
BranchTrueFalseTotal
unless $message->isa("Net::DNS::Packet") 1 58 59
253 58 my @unsigned = grep { ref($_) ne ref($self) } @{$message->{additional}};
254 58 local $message->{additional} = \@unsigned; # remake header image
255 58 my @part = qw(question answer authority additional);
256 58 my @size = map { scalar @{$message->{$_}} } @part;
257 58 ▶ if ( my $rawref = delete $self->{rawref} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $rawref = delete $self->{'rawref'}) { } 16 42 58
258 16 my $hbin = pack 'n6', $self->original_id, $message->{status}, @size;
259 16 $message = join '', $hbin, substr $$rawref, length $hbin;
260 } else {
261 42 my $data = $message->encode;
262 42 my $id = $message->header->id;
263 42 my $hbin = pack 'n6', $id, $message->{status}, @size;
264 42 $message = join '', $hbin, substr $data, length $hbin;
265 42 $self->original_id($id);
266 }
267 }
268  
269 # Design decision: Use 32 bits, which will work until the end of time()!
270 59 my $time = pack 'xxN n', $self->time_signed, $self->fudge;
271  
272 # Insert the prior MAC if present (multi-packet message).
273 59 ▶ $self->prior_macbin( $self->{link}->macbin ) if $self->{link};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"link"} 15 44 59
274 59 my $prior_macbin = $self->prior_macbin;
275 59 ▶ return pack 'na* a* a*', length($prior_macbin), $prior_macbin, $message, $time if $prior_macbin;
Branch: 1 branch
BranchTrueFalseTotal
if $prior_macbin 18 41 59
276  
277 # Insert the request MAC if present (used to validate responses).
278 41 my $req_mac = $self->request_macbin;
279 41 ▶ my $sigdata = $req_mac ? pack( 'na*', length($req_mac), $req_mac ) : '';
Branch: 1 branch
BranchTrueFalseTotal
$req_mac ? : 8 33 41
280  
281 41 ▶ $sigdata .= $message || '';
Condition100%2/2
$message || ''
l!l
401
MC/DC100%1/1
$message || ''
$message
Truth table100%2/2
A
A= $message
Aresult
11
00
282  
283 41 my $kname = $self->{owner}->canonical; # canonical key name
284 41 $sigdata .= pack 'a* n N', $kname, ANY, 0;
285  
286 41 $sigdata .= $self->{algorithm}->canonical; # canonical algorithm name
287  
288 41 $sigdata .= $time;
289  
290 41 $sigdata .= pack 'n', $self->{error};
291  
292 41 my $other = $self->other;
293 41 $sigdata .= pack 'na*', length($other), $other;
294  
295 41 return $sigdata;
296 }
297  
298  
299 54 ▶sub create {
Subroutine create: called CC=19 SCAR=29.4
Pod documented
300 54 my ( $class, $karg, @argument ) = @_;
301 54 ▶ croak 'argument undefined' unless defined $karg;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $karg 2 52 54
302  
303 52 ▶ if ( ref($karg) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $karg) { } 32 20 52
elsif (scalar(@argument) % 2 == 0) { } 19 1 20
304 32 ▶ if ( $karg->isa('Net::DNS::Packet') ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($karg->isa('Net::DNS::Packet')) { } 5 27 32
elsif (ref $karg eq __PACKAGE__) { } 16 11 27
elsif (ref $karg eq 'Net::DNS::RR::KEY') { } 10 1 11
305 5 my $sigrr = $karg->sigrr;
306 5 ▶ croak 'no TSIG in request packet' unless defined $sigrr;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sigrr 2 3 5
307 3 return Net::DNS::RR->new( # ( request, options )
308 name => $sigrr->name,
309 type => 'TSIG',
310 algorithm => $sigrr->algorithm,
311 request_macbin => $sigrr->macbin,
312 @argument
313 );
314  
315 } elsif ( ref($karg) eq __PACKAGE__ ) {
316 16 my $tsig = $karg->_chain;
317 16 $tsig->{macbin} = undef;
318 16 return $tsig;
319  
320 } elsif ( ref($karg) eq 'Net::DNS::RR::KEY' ) {
321 10 return Net::DNS::RR->new(
322 name => $karg->name,
323 type => 'TSIG',
324 algorithm => $karg->algorithm,
325 key => $karg->key,
326 @argument
327 );
328 }
329  
330 } elsif ( ( scalar(@argument) % 2 ) == 0 ) {
331 19 require File::Spec; # ( keyfile, options )
332 19 require Net::DNS::ZoneFile;
333 19 my ($keypath) = SYMLINK ? grep( {$_} readlink($karg), $karg ) : $karg;
334 19 my ( $vol, $dir, $name ) = File::Spec->splitpath($keypath);
335 19 $name =~ m/^K([^+]+)\+\d+\+(\d+)\./; # BIND dnssec-keygen
336 19 my ( $keyname, $keytag ) = ( $1, $2 );
337  
338 19 my $keyfile = Net::DNS::ZoneFile->new($karg);
339 18 my ( $algorithm, $secret );
340 18 ▶ while ( $keyfile->_getline ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keyfile->_getline) 65 15 80
341 65 ▶ /^key "([^"]+)"/ and $keyname = $1; # BIND tsig key
Branch: 1 branch
BranchTrueFalseTotal
/^key "([^"]+)"/ and $keyname = $1 14 51 65
342 65 ▶ /algorithm ([^;]+);/ and $algorithm = $1;
Branch: 1 branch
BranchTrueFalseTotal
/algorithm ([^;]+);/ and $algorithm = $1 14 51 65
343 65 ▶ /secret "([^"]+)";/ and $secret = $1;
Branch: 1 branch
BranchTrueFalseTotal
/secret "([^"]+)";/ and $secret = $1 14 51 65
344  
345 65 ▶ /^Algorithm:/ and ( undef, $algorithm ) = split; # BIND dnssec private key
Branch: 1 branch
BranchTrueFalseTotal
/^Algorithm:/ and (undef, $algorithm) = split(' ', $_, 3) 2 63 65
346 65 ▶ /^Key:/ and ( undef, $secret ) = split;
Branch: 1 branch
BranchTrueFalseTotal
/^Key:/ and (undef, $secret) = split(' ', $_, 3) 2 63 65
347  
348 65 ▶ next unless /\bIN\s+KEY\b/; # BIND dnssec public key
Branch: 1 branch
BranchTrueFalseTotal
unless /\bIN\s+KEY\b/ 62 3 65
349 3 my $keyrr = Net::DNS::RR->new($_);
350 3 ▶ carp "$karg does not appear to be a BIND dnssec public key"
Branch: 1 branch
BranchTrueFalseTotal
unless $keyrr->keytag == ($keytag || 0) 2 1 3
Condition100%2/2
$keytag || 0
l!l
21
MC/DC100%1/1
$keytag || 0
$keytag
Truth table100%2/2
A
A= $keytag
Aresult
11
00
351 unless $keyrr->keytag == ( $keytag || 0 );
352 3 return $class->create( $keyrr, @argument );
353 }
354  
355 15 foreach ( $keyname, $algorithm, $secret ) {
356 44 ▶ croak 'key file incompatible with TSIG' unless $_;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ 1 43 44
357 }
358  
359 14 return Net::DNS::RR->new(
360 name => $keyname,
361 type => 'TSIG',
362 algorithm => $algorithm,
363 key => $secret,
364 @argument
365 );
366 }
367  
368 2 croak "Usage: $class->create( \$keyfile, \@options )";
369 }
370  
371  
372 40 ▶sub verify {
Subroutine verify: called CC=14 SCAR=26.4
Pod documented
373 40 my ( $self, $data, @link ) = @_;
374 40 my $fail = undef;
375  
376 40 ▶ if ( scalar @link ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @link) 31 9 40
377  
378 31 my $link = shift @link;
379 31 ▶ unless ( ref($link) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $link) 1 30 31
380 1 $self->error('BADSIG'); # (multi-packet)
381 1 return $fail;
382 }
383  
384 30 my $signerkey = lc( join '+', $self->name, $self->algorithm );
385 30 ▶ if ( $link->isa('Net::DNS::Packet') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($link->isa('Net::DNS::Packet')) { } 13 17 30
elsif ($link->isa(__PACKAGE__)) { } 16 1 17
386 13 my $request = $link->sigrr; # request TSIG
387 13 my $rqstkey = lc( join '+', $request->name, $request->algorithm );
388 13 ▶ $self->error('BADKEY') unless $signerkey eq $rqstkey;
Branch: 1 branch
BranchTrueFalseTotal
unless $signerkey eq $rqstkey 1 12 13
389 13 $self->request_macbin( $request->macbin );
390  
391 } elsif ( $link->isa(__PACKAGE__) ) {
392 16 my $priorkey = lc( join '+', $link->name, $link->algorithm );
393 16 ▶ $self->error('BADKEY') unless $signerkey eq $priorkey;
Branch: 1 branch
BranchTrueFalseTotal
unless $signerkey eq $priorkey 1 15 16
394 16 $self->prior_macbin( $link->macbin );
395  
396 } else {
397 1 croak 'Usage: $tsig->verify( $reply, $query )';
398 }
399 }
400  
401 38 ▶ return $fail if $self->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"error"} 8 30 38
402  
403 30 my $sigdata = $self->sig_data($data); # form data to be verified
404 29 my $tsigmac = $self->_mac_function($sigdata);
405 29 my $tsig = $self->_chain;
406  
407 29 my $macbin = $self->macbin;
408 29 my $maclen = length $macbin;
409 29 ▶ $self->error('BADSIG') if $macbin ne substr $tsigmac, 0, $maclen;
Branch: 1 branch
BranchTrueFalseTotal
if $macbin ne substr($tsigmac, 0, $maclen) 4 25 29
410  
411 29 my $minlen = length($tsigmac) >> 1; # per RFC4635, 3.1
412 29 ▶ $self->error('BADTRUNC') if $maclen < $minlen or $maclen > length $tsigmac;
Branch: 1 branch
BranchTrueFalseTotal
if $maclen < $minlen or $maclen > length $tsigmac 3 26 29
Condition100%3/3
$maclen < $minlen or $maclen > length $tsigmac
l!l&&r!l&&!r
2126
MC/DC100%2/2
$maclen < $minlen or $maclen > length $tsigmac
$maclen < $minlen $maclen > length $tsigmac
Truth table100%3/3
A or B
A= $maclen < $minlenB= $maclen > length $tsigmac
ABresult
1X1
011
000
413 29 ▶ $self->error('BADTRUNC') if $maclen < 10;
Branch: 1 branch
BranchTrueFalseTotal
if $maclen < 10 2 27 29
414  
415 29 my $time_signed = $self->time_signed;
416 29 ▶ if ( abs( time() - $time_signed ) > $self->fudge ) {
Branch: 1 branch
BranchTrueFalseTotal
if (abs time - $time_signed > $self->fudge) 1 28 29
417 1 $self->error('BADTIME');
418 1 $self->other($time_signed);
419 }
420  
421 29 ▶ return $self->{error} ? $fail : $tsig;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'error'} ? : 7 22 29
422 }
423  
424 25 ▶sub vrfyerrstr { return shift->error; }
Subroutine vrfyerrstr: called CC=1 SCAR=0.0
Pod documented
425  
426  
427 ########################################
428  
429 {
430 7 my @algbyname = (
431 'HMAC-MD5.SIG-ALG.REG.INT' => 157, # numbers are from ISC BIND keygen
432 'HMAC-SHA1' => 161, # and not blessed by IANA
433 'HMAC-SHA224' => 162,
434 'HMAC-SHA256' => 163,
435 'HMAC-SHA384' => 164,
436 'HMAC-SHA512' => 165,
437 );
438  
439 7 my @algalias = (
440 'HMAC-MD5' => 157,
441 'HMAC-SHA' => 161,
442 );
443  
444 7 my %algbyval = reverse @algbyname;
445  
446 7 ▶ my @algrehash = map { /^\d/ ? ($_) x 3 : uc($_) } @algbyname, @algalias;
Branch: 1 branch
BranchTrueFalseTotal
/^\d/ ? : 56 56 112
447 7 foreach (@algrehash) { s/[\W_]//g; } # strip non-alphanumerics
448 7 my %algbyname = @algrehash; # work around broken cperl
449  
450 43 ▶ sub _algbyname {
Subroutine _algbyname: called CC=1 SCAR=0.0
451 43 my $key = uc shift; # synthetic key
452 43 $key =~ s/[\W_]//g; # strip non-alphanumerics
453 43 return $algbyname{$key};
454 }
455  
456 41 ▶ sub _algbyval {
Subroutine _algbyval: called CC=1 SCAR=0.0
457 41 my $value = shift;
458 41 return $algbyval{$value};
459 }
460 }
461  
462  
463 {
464 7 my %digest = (
465 '157' => ['Digest::MD5'],
466 '161' => ['Digest::SHA'],
467 '162' => ['Digest::SHA', 224, 64],
468 '163' => ['Digest::SHA', 256, 64],
469 '164' => ['Digest::SHA', 384, 128],
470 '165' => ['Digest::SHA', 512, 128],
471 );
472  
473  
474 7 my %keytable;
475  
476 120 ▶ sub _algorithm { ## install sig function in key table
Subroutine _algorithm: called CC=5 SCAR=16.1
477 120 my $self = shift;
478  
479 120 ▶ if ( my $algname = shift ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $algname = shift()) 43 77 120
480  
481 43 ▶ unless ( my $digtype = _algbyname($algname) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $digtype = _algbyname($algname)) { } 41 2 43
482 2 $self->{algorithm} = Net::DNS::DomainName->new($algname);
483  
484 } else {
485 41 $algname = _algbyval($digtype);
486 41 $self->{algorithm} = Net::DNS::DomainName->new($algname);
487  
488 41 my ( $hash, @param ) = @{$digest{$digtype}};
489 41 my ( undef, @block ) = @param;
490 41 my $digest = $hash->new(@param);
491 93 ▶ my $function = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
492 93 my $hmac = Digest::HMAC->new( shift, $digest, @block );
493 93 $hmac->add(shift);
494 93 return $hmac->digest;
495 41 };
496  
497 41 $self->sig_function($function);
498  
499 41 ▶ my $keyname = ( $self->{owner} || return )->canonical;
Condition100%2/2
$self->{'owner'} || (return)
l!l
356
MC/DC100%1/1
$self->{'owner'} || (return)
$self->{'owner'}
Truth table100%2/2
A
A= $self->{'owner'}
Aresult
11
00
500 35 $keytable{$keyname}{digest} = $function;
501 }
502 }
503  
504 114 ▶ return defined wantarray ? $self->{algorithm}->name : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined wantarray ? : 77 37 114
505 }
506  
507  
508 27 ▶ sub _keybin { ## install key in key table
Subroutine _keybin: called CC=3 SCAR=11.0
509 27 my ( $self, @argument ) = @_;
510 27 ▶ croak 'access to TSIG key material denied' unless scalar @argument;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @argument 1 26 27
511 26 ▶ my $keyref = $keytable{$self->{owner}->canonical} ||= {};
Condition100%2/2
$keytable{$self->{"owner"}->canonical} ||= {}
l!l
242
MC/DC100%1/1
$keytable{$self->{"owner"}->canonical} ||= {}
$keytable{$self->{"owner"}->canonical}
Truth table100%2/2
A
A= $keytable{$self->{"owner"}->canonical}
Aresult
11
00
512 26 my $private = shift @argument; # closure keeps private key private
513 59 ▶ $keyref->{key} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
514 59 my $function = $keyref->{digest};
515 59 return &$function( $private, @_ );
516 26 };
517 26 return;
518 }
519  
520  
521 59 ▶ sub _mac_function { ## apply keyed hash function to argument
Subroutine _mac_function: called CC=3 SCAR=11.0
522 59 my ( $self, @argument ) = @_;
523 59 my $owner = $self->{owner}->canonical;
524 59 ▶ $self->algorithm( $self->algorithm ) unless $keytable{$owner}{digest};
Branch: 1 branch
BranchTrueFalseTotal
unless $keytable{$owner}{"digest"} 2 57 59
525 59 my $keyref = $keytable{$owner};
526 59 ▶ $keyref->{digest} = $self->sig_function unless $keyref->{digest};
Branch: 1 branch
BranchTrueFalseTotal
unless $keyref->{"digest"} 1 58 59
527 59 my $function = $keyref->{key};
528 59 return &$function(@argument);
529 }
530 }
531  
532  
533 # _chain() creates a new TSIG object linked to the original
534 # RR, for the purpose of signing multi-message transfers.
535  
536 7 ▶sub _chain {
Subroutine _chain: called CC=1 SCAR=0.0
537 45 my $self = shift;
538 45 $self->{link} = undef;
539 45 return bless {%$self, link => $self}, ref($self);
540 }
541  
542 ########################################
543  
544  
545 7 1;
546 __END__
« DNS/Packet.pm ↑ Summary DNS/Resolver/Base.pm »