DNS/RR/TSIG.pm

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