DNS/RR/DNSKEY.pm

filter: Statementstmt 100% 137 / 137 Branchbran 100% 44 / 44 Conditioncond 100% 14 / 14 MC/DCmcdc 100% 7 / 7 Subroutinesub 100% 27 / 27 Podpod 100% 14 / 14 totaltotal 100% 243 / 243 CC 37 SCAR 11.8
CC
37
Cov
100%
algorithm
6.0
keylength
5.0
zone
4.0
CRAP 3.3
« DNS/RR/DS.pm ↑ Summary DNS/Parameters.pm »
1 package Net::DNS::RR::DNSKEY;
2  
3 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 our $VERSION = (qw$Id: DNSKEY.pm 2059 2026-08-28 10:04:18Z willem $)[2];
6  
7 13 ▶use base qw(Net::DNS::RR);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10 =head1 NAME
11  
12 Net::DNS::RR::DNSKEY - DNS DNSKEY resource record
13  
14 =cut
15  
16 13 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 13 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 13 ▶use constant BASE64 => defined eval { require MIME::Base64 };
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22  
23 13 ▶sub _decode_rdata { ## decode rdata from wire-format octet string
Subroutine _decode_rdata: called CC=1 SCAR=0.0
24 13 my ( $self, $data, $offset ) = @_;
25  
26 13 my $rdata = substr $$data, $offset, $self->{rdlength};
27 13 @{$self}{qw(flags protocol algorithm keybin)} = unpack 'n C2 a*', $rdata;
28 13 return;
29 }
30  
31  
32 25 ▶sub _encode_rdata { ## encode rdata as wire-format octet string
Subroutine _encode_rdata: called CC=1 SCAR=0.0
33 25 my $self = shift;
34  
35 25 return pack 'n C2 a*', @{$self}{qw(flags protocol algorithm keybin)};
36 }
37  
38  
39 10 ▶sub _format_rdata { ## format rdata portion of RR string.
Subroutine _format_rdata: called CC=2 SCAR=6.9
40 10 my $self = shift;
41  
42 10 my @rdata = @{$self}{qw(flags protocol algorithm)};
43 10 ▶ if ( my $keybin = $self->keybin ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $keybin = $self->keybin) { } 9 1 10
44 9 $self->_annotation( 'keytag', $self->keytag );
45 9 return $self->SUPER::_format_rdata() unless BASE64;
46 9 push @rdata, split /\s+/, MIME::Base64::encode($keybin);
47 } else {
48 1 push @rdata, '""';
49 }
50 10 return @rdata;
51 }
52  
53  
54 21 ▶sub _parse_rdata { ## populate RR from rdata in argument list
Subroutine _parse_rdata: called CC=2 SCAR=6.9
55 21 my ( $self, @argument ) = @_;
56  
57 21 $self->flags( shift @argument );
58 21 $self->protocol( shift @argument );
59 21 my $algorithm = shift @argument;
60 21 ▶ $self->key(@argument) if $algorithm;
Branch: 1 branch
BranchTrueFalseTotal
if $algorithm 19 2 21
61 21 $self->algorithm($algorithm);
62 21 return;
63 }
64  
65  
66 10 ▶sub _defaults { ## specify RR attribute default values
Subroutine _defaults: called CC=1 SCAR=0.0
67 10 my $self = shift;
68  
69 10 $self->flags(256);
70 10 $self->protocol(3);
71 10 $self->algorithm(1);
72 10 $self->keybin('');
73 10 return;
74 }
75  
76  
77 47 ▶sub flags {
Subroutine flags: called CC=3 SCAR=11.0
Pod documented
78 47 my ( $self, @value ) = @_;
79 47 for (@value) { $self->{flags} = 0 + $_ }
80 47 ▶ return $self->{flags} || 0;
Condition100%2/2
$self->{'flags'} || 0
l!l
3611
MC/DC100%1/1
$self->{'flags'} || 0
$self->{'flags'}
Truth table100%2/2
A
A= $self->{'flags'}
Aresult
11
00
81 }
82  
83  
84 30 ▶sub zone {
Subroutine zone: called CC=4 SCAR=13.9
Pod documented
85 30 my ( $self, @value ) = @_;
86 30 ▶ if ( scalar @value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @value) 7 23 30
87 7 for ( $self->{flags} |= 0x0100 ) {
88 7 ▶ $_ ^= 0x0100 unless shift @value;
Branch: 1 branch
BranchTrueFalseTotal
unless shift @value 4 3 7
89 }
90 }
91 30 return $self->{flags} & 0x0100;
92 }
93  
94  
95 31 ▶sub revoke {
Subroutine revoke: called CC=4 SCAR=13.9
Pod documented
96 31 my ( $self, @value ) = @_;
97 31 ▶ if ( scalar @value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @value) 7 24 31
98 7 for ( $self->{flags} |= 0x0080 ) {
99 7 ▶ $_ ^= 0x0080 unless shift @value;
Branch: 1 branch
BranchTrueFalseTotal
unless shift @value 4 3 7
100 }
101 }
102 31 return $self->{flags} & 0x0080;
103 }
104  
105  
106 8 ▶sub adt {
Subroutine adt: called CC=4 SCAR=13.9
Pod documented
107 8 my ( $self, @value ) = @_;
108 8 ▶ if ( scalar @value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @value) 4 4 8
109 4 for ( $self->{flags} |= 0x0002 ) {
110 4 ▶ $_ ^= 0x0002 unless shift @value;
Branch: 1 branch
BranchTrueFalseTotal
unless shift @value 2 2 4
111 }
112 }
113 8 return $self->{flags} & 0x0002;
114 }
115  
116  
117 19 ▶sub sep {
Subroutine sep: called CC=4 SCAR=13.9
Pod documented
118 19 my ( $self, @value ) = @_;
119 19 ▶ if ( scalar @value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @value) 7 12 19
120 7 for ( $self->{flags} |= 0x0001 ) {
121 7 ▶ $_ ^= 0x0001 unless shift @value;
Branch: 1 branch
BranchTrueFalseTotal
unless shift @value 4 3 7
122 }
123 }
124 19 return $self->{flags} & 0x0001;
125 }
126  
127  
128 58 ▶sub protocol {
Subroutine protocol: called CC=3 SCAR=11.0
Pod documented
129 58 my ( $self, @value ) = @_;
130 58 for (@value) { $self->{protocol} = 0 + $_ }
131 58 ▶ return $self->{protocol} || 0;
Condition100%2/2
$self->{'protocol'} || 0
l!l
544
MC/DC100%1/1
$self->{'protocol'} || 0
$self->{'protocol'}
Truth table100%2/2
A
A= $self->{'protocol'}
Aresult
11
00
132 }
133  
134  
135 97 ▶sub algorithm {
Subroutine algorithm: called CC=6 SCAR=17.9
Pod documented
136 97 my ( $self, $arg ) = @_;
137  
138 97 ▶ unless ( ref($self) ) { ## class method or simple function
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self) 3 94 97
139 3 my $argn = pop;
140 3 ▶ return $argn =~ /\D/ ? _algbyname($argn) : _algbyval($argn);
Branch: 1 branch
BranchTrueFalseTotal
$argn =~ /\D/ ? : 1 2 3
141 }
142  
143 94 ▶ return $self->{algorithm} unless defined $arg;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg 43 51 94
144 51 ▶ return _algbyval( $self->{algorithm} ) if uc($arg) eq 'MNEMONIC';
Branch: 1 branch
BranchTrueFalseTotal
if uc $arg eq "MNEMONIC" 5 46 51
145 46 ▶ return $self->{algorithm} = _algbyname($arg) || die _algbyname('') # disallow algorithm(0)
Condition100%2/2
_algbyname($arg) || die(_algbyname(''))
l!l
441
MC/DC100%1/1
_algbyname($arg) || die(_algbyname(''))
_algbyname($arg)
Truth table100%2/2
A
A= _algbyname($arg)
Aresult
11
00
146 }
147  
148  
149 39 ▶sub key {
Subroutine key: called CC=2 SCAR=6.9
Pod documented
150 39 my ( $self, @value ) = @_;
151 39 ▶ return MIME::Base64::encode( $self->keybin(), "" ) unless scalar @value;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @value 16 23 39
152 23 return $self->keybin( MIME::Base64::decode( join "", @value ) );
153 }
154  
155  
156 87 ▶sub keybin {
Subroutine keybin: called CC=3 SCAR=11.0
Pod documented
157 87 my ( $self, @value ) = @_;
158 87 for (@value) { $self->{keybin} = $_ }
159 87 ▶ return $self->{keybin} || "";
Condition100%2/2
$self->{'keybin'} || ''
l!l
7017
MC/DC100%1/1
$self->{'keybin'} || ''
$self->{'keybin'}
Truth table100%2/2
A
A= $self->{'keybin'}
Aresult
11
00
160 }
161  
162  
163 8 ▶sub publickey {
Subroutine publickey: called CC=1 SCAR=0.0
Pod documented
164 8 my ( $self, @value ) = @_;
165 8 return $self->key(@value);
166 }
167  
168  
169 6 ▶sub privatekeyname {
Subroutine privatekeyname: called CC=1 SCAR=0.0
Pod documented
170 6 my $self = shift;
171 6 my $name = $self->signame;
172 6 return sprintf 'K%s+%03d+%05d.private', $name, $self->algorithm, $self->keytag;
173 }
174  
175  
176 6 ▶sub signame {
Subroutine signame: called CC=1 SCAR=0.0
Pod documented
177 6 my $self = shift;
178 6 return lc $self->{owner}->fqdn;
179 }
180  
181  
182 15 ▶sub keylength {
Subroutine keylength: called CC=5 SCAR=16.1
Pod documented
183 15 my $self = shift;
184  
185 15 ▶ my $keybin = $self->keybin || return;
Condition100%2/2
$self->keybin || (return)
l!l
132
MC/DC100%1/1
$self->keybin || (return)
$self->keybin
Truth table100%2/2
A
A= $self->keybin
Aresult
11
00
186  
187 13 local $_ = _algbyval( $self->{algorithm} );
188  
189 13 ▶ if (/^RSA/) {
Branch: 2 branches
BranchTrueFalseTotal
if (/^RSA/) { } 9 4 13
elsif (/^DSA/) { } 2 2 4
190  
191 # Modulus length, see RFC 3110
192 9 ▶ if ( my $exp_length = unpack 'C', $keybin ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $exp_length = unpack('C', $keybin)) { } 7 2 9
193  
194 7 return ( length($keybin) - $exp_length - 1 ) << 3;
195  
196 } else {
197 2 $exp_length = unpack 'x n', $keybin;
198 2 return ( length($keybin) - $exp_length - 3 ) << 3;
199 }
200  
201 } elsif (/^DSA/) {
202  
203 # Modulus length, see RFC 2536
204 2 my $T = unpack 'C', $keybin;
205 2 return ( $T << 6 ) + 512;
206 }
207  
208 2 return length($keybin) << 2; ## ECDSA / EdDSA
209 }
210  
211  
212 49 ▶sub keytag {
Subroutine keytag: called CC=4 SCAR=13.9
Pod documented
213 49 my $self = shift;
214  
215 49 ▶ my $keybin = $self->{keybin} || return;
Condition100%2/2
$self->{'keybin'} || (return)
l!l
463
MC/DC100%1/1
$self->{'keybin'} || (return)
$self->{'keybin'}
Truth table100%2/2
A
A= $self->{'keybin'}
Aresult
11
00
216  
217 # RFC4034 Appendix B.1: most significant 16 bits of least significant 24 bits
218 46 ▶ return unpack 'n', substr $keybin, -3 if $self->{algorithm} == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"algorithm"} == 1 1 45 46
219  
220 # RFC4034 Appendix B
221 45 my $od = length($keybin) & 1;
222 45 my $rd = pack "n C2 a* x$od", @{$self}{qw(flags protocol algorithm)}, $keybin;
223 45 my $ac = 0;
224 45 $ac += $_ for unpack 'n*', $rd;
225 45 $ac += ( $ac >> 16 );
226 45 return $ac & 0xFFFF;
227 }
228  
229  
230 ########################################
231  
232 {
233 13 my @algbyname = (
234 'DELETE' => 0, # [RFC4034][RFC4398][RFC8078]
235 'RSAMD5' => 1, # [RFC3110][RFC4034]
236 'DH' => 2, # [RFC2539]
237 'DSA' => 3, # [RFC3755][RFC2536]
238 ## Reserved => 4, # [RFC6725]
239 'RSASHA1' => 5, # [RFC3110][RFC4034]
240 'DSA-NSEC3-SHA1' => 6, # [RFC5155]
241 'RSASHA1-NSEC3-SHA1' => 7, # [RFC5155]
242 'RSASHA256' => 8, # [RFC5702]
243 ## Reserved => 9, # [RFC6725]
244 'RSASHA512' => 10, # [RFC5702]
245 ## Reserved => 11, # [RFC6725]
246 'ECC-GOST' => 12, # [RFC5933]
247 'ECDSAP256SHA256' => 13, # [RFC6605]
248 'ECDSAP384SHA384' => 14, # [RFC6605]
249 'ED25519' => 15, # [RFC8080]
250 'ED448' => 16, # [RFC8080]
251 'SM2SM3' => 17, # [RFC9563]
252 'MLDSA44' => 18, # [draft-westerbaan-dnssec-mldsa]
253 'ECC-GOST12' => 23, # [RFC9558]
254  
255 'INDIRECT' => 252, # [RFC4034]
256 'PRIVATEDNS' => 253, # [RFC4034]
257 'PRIVATEOID' => 254, # [RFC4034]
258 ## Reserved => 255, # [RFC4034]
259 );
260  
261 13 my %algbyval = reverse @algbyname;
262  
263 13 foreach (@algbyname) { s/[\W_]//g; } # strip non-alphanumerics
264 13 ▶ my @algrehash = map { /^\d/ ? ($_) x 3 : uc($_) } @algbyname;
Branch: 1 branch
BranchTrueFalseTotal
/^\d/ ? : 260 260 520
265 13 my %algbyname = @algrehash; # work around broken cperl
266  
267 48 ▶ sub _algbyname {
Subroutine _algbyname: called CC=3 SCAR=11.0
268 48 my $arg = shift;
269 48 my $key = uc $arg; # synthetic key
270 48 $key =~ s/[\W_]//g; # strip non-alphanumerics
271 48 my $val = $algbyname{$key};
272 48 ▶ return $val if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 38 10 48
273 10 ▶ return $key =~ /^\d/ ? $arg : croak qq[unknown algorithm $arg];
Branch: 1 branch
BranchTrueFalseTotal
$key =~ /^\d/ ? : 8 2 10
274 }
275  
276 20 ▶ sub _algbyval {
Subroutine _algbyval: called CC=2 SCAR=6.9
277 20 my $value = shift;
278 20 ▶ return $algbyval{$value} || return $value;
Condition100%2/2
$algbyval{$value} || (return $value)
l!l
191
MC/DC100%1/1
$algbyval{$value} || (return $value)
$algbyval{$value}
Truth table100%2/2
A
A= $algbyval{$value}
Aresult
11
00
279 }
280 }
281  
282 ########################################
283  
284  
285 13 1;
286 __END__
« DNS/RR/DS.pm ↑ Summary DNS/Parameters.pm »