Radius/Packet.pm

filter: Statementstmt 88.0% 206 / 234 Branchbran 65.0% 78 / 120 Conditioncond 59.0% 36 / 61 MC/DCmcdc 24.3% 9 / 37 Subroutinesub 95.2% 20 / 21 Podpod 57.1% 4 / 7 totaltotal 73.5% 353 / 480 CC 84 SCAR 37.0
CC
84
Cov
77%
build
71.3
parse
40.5
pack_attribute
21.8
CRAP 40.6
↑ Summary Radius/DictionaryParser.pm »
1 package Data::Radius::Packet;
2 # encode/decode RADIUS protocol messages
3  
4 4 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Digest::MD5 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Digest::HMAC_MD5 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 4 ▶use base qw(Class::Accessor::Fast);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 __PACKAGE__->mk_accessors(qw(secret dict));
14  
15 4 ▶use Data::Radius::Constants qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use Data::Radius::Encode qw(encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶use Data::Radius::Decode qw(decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use Data::Radius::Util qw(encrypt_pwd decrypt_pwd is_enum_type);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 4 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
21 # common attributes
22 4 ATTR_PASSWORD => 2,
23 ATTR_VENDOR => 26,
24 # Message-Authenticator
25 ATTR_MSG_AUTH_NAME => 'Message-Authenticator',
26 ATTR_MSG_AUTH => 80,
27 ATTR_MSG_AUTH_LEN => 18,
28  
29 # has extra byte in VSA header
30 WIMAX_VENDOR => 24757,
31 4 };
32 4 ▶use constant ATTR_MSG_AUTH_ZERO => pack('C C', ATTR_MSG_AUTH, ATTR_MSG_AUTH_LEN) . ("\x0" x (ATTR_MSG_AUTH_LEN - 2));
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 4 my %IS_REPLY = map { $_ => 1 } (
35 ACCESS_ACCEPT,
36 ACCESS_CHALLENGE,
37 ACCESS_REJECT,
38 DISCONNECT_ACCEPT,
39 DISCONNECT_REJECT,
40 COA_ACCEPT,
41 COA_REJECT
42 );
43 4 my %IS_REQUEST = map { $_ => 1 } (ACCESS_REQUEST, ACCOUNTING_REQUEST, DISCONNECT_REQUEST, COA_REQUEST);
44  
45 4 my %IS_ACCOUNTING = map { $_ => 1 } (ACCOUNTING_REQUEST, ACCOUNTING_RESPONSE);
46  
47 4 my $request_id = int( rand(255) );
48  
49 # Digest::MD5 object
50 4 my $md5;
51  
52 5 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
53 5 my ($class, %h) = @_;
54 my $obj = {
55 secret => $h{secret},
56 dict => $h{dict},
57 5 };
58  
59 5 bless $obj, $class;
60 }
61  
62 # build new request
63 # input:
64 # type - radius code
65 # authenticator - for access request allow to override random one,
66 # for replies - value from request must be used
67 # av_list - array-ref of AV in {Name, Value} or {Id,Type,VendorId,Value} form
68 # dict - allow to override default dictionary object from constructor
69 # secret - allow to override default secret from constructor
70 # with_msg_auth - boolean, to add Message-Authenticator.
71 # This can be archieved by adding Message-Authenticator to av_list with undefined value
72 # Is enabled by default now.
73 # request_id - allow to specify custom value (0..255), otherwise internal counter is used
74 # RaiseError - raise error from AV encoding/decoding - default is to print and forgive errors
75 # PrintError - print error from AV encoding/decoding - default on
76 7 ▶sub build {
Subroutine build: called CC=38 SCAR=42.7
Pod documented
77 7 my ($self, %h) = @_;
78  
79 7 ▶ $h{RaiseError} //= $Data::Radius::Encode::RaiseError;
Condition33%1/3
$h{"RaiseError"} //= $Data::Radius::Encode::RaiseError
l!l&&r!l&&!r
070
MC/DC0%0/2
$h{"RaiseError"} //= $Data::Radius::Encode::RaiseError
$h{"RaiseError"} $Data::Radius::Encode::RaiseError
Truth table33%1/3
A //= B
A= $h{"RaiseError"}B= $Data::Radius::Encode::RaiseError
ABresult
1X1
011
000
80 7 ▶ $h{PrintError} //= $Data::Radius::Encode::PrintError;
Condition33%1/3
$h{"PrintError"} //= $Data::Radius::Encode::PrintError
l!l&&r!l&&!r
070
MC/DC0%0/2
$h{"PrintError"} //= $Data::Radius::Encode::PrintError
$h{"PrintError"} $Data::Radius::Encode::PrintError
Truth table33%1/3
A //= B
A= $h{"PrintError"}B= $Data::Radius::Encode::PrintError
ABresult
1X1
011
000
81  
82 # RADIUS code
83 7 my $type = $h{type};
84 # list in form of { Name => ... Value => ... [Vendor => ...]}
85 7 my $av_list = $h{av_list};
86 # object of Data::Radius::Dictionary or compatible
87 7 ▶ my $dict = $h{dict} // $self->dict();
Condition33%1/3
$h{"dict"} // $self->dict
l!l&&r!l&&!r
070
MC/DC0%0/2
$h{"dict"} // $self->dict
$h{"dict"} $self->dict
Truth table33%1/3
A // B
A= $h{"dict"}B= $self->dict
ABresult
1X1
011
000
88 # RADIUS secret
89 7 ▶ if($h{secret}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h{"secret"}) 0 7 7
90 0 $self->secret($h{secret});
91 }
92 7 ▶ Carp::croak('No secret value') if ! defined $self->secret;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->secret 0 7 7
93  
94 7 ▶ if ($self->is_reply($type) && ! $h{authenticator}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_reply($type) and not $h{"authenticator"}) 0 7 7
Condition33%1/3
$self->is_reply($type) and not $h{"authenticator"}
!ll&&!rl&&r
700
MC/DC0%0/2
$self->is_reply($type) and not $h{"authenticator"}
$self->is_reply($type) not $h{"authenticator"}
Truth table33%1/3
A and not(B)
A= $self->is_reply($type)B= not $h{"authenticator"}
ABresult
0X0
100
111
95 0 Carp::croak("No authenticator value from request");
96 }
97  
98 # enable adding Message-Authenticator attribute (RFC3579)
99 7 my $with_msg_auth;
100 7 ▶ if ($IS_ACCOUNTING{ $type }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($IS_ACCOUNTING{$type}) { } 2 5 7
101 2 ▶ if ($h{with_msg_auth}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h{"with_msg_auth"}) 1 1 2
102 1 my $msg = 'Message-Authenticator is not used for accounting';
103 1 ▶ Carp::croak($msg) if ($h{RaiseError});
Branch: 1 branch
BranchTrueFalseTotal
if $h{"RaiseError"} 0 1 1
104 1 ▶ Carp::carp($msg) if ($h{PrintError});
Branch: 1 branch
BranchTrueFalseTotal
if $h{"PrintError"} 1 0 1
105 }
106 2 $with_msg_auth = 0;
107 }
108 else {
109 # enable it by default as protection against blast-RADIUS https://www.blastradius.fail/
110 5 ▶ $with_msg_auth = $h{with_msg_auth} // 1;
Condition100%2/2
$h{"with_msg_auth"} // 1
l!l
41
MC/DC100%1/1
$h{"with_msg_auth"} // 1
$h{"with_msg_auth"}
Truth table100%2/2
A
A= $h{"with_msg_auth"}
Aresult
11
00
111 }
112  
113 # Authenticator required now to encode password field (if present)
114 7 my $authenticator;
115 7 ▶ if ($type == ACCESS_REQUEST) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 1) 3 4 7
116 # random, but allow to override for testing
117 0 ▶ $authenticator = $h{authenticator} // pack 'L4', map { int(rand(2 ** 32 - 1)) } (0..3);
Condition50%1/2
$h{"authenticator"} // pack("L4", map({int rand 4294967295;} 0..3))
l!l
30
MC/DC0%0/1
$h{"authenticator"} // pack("L4", map({int rand 4294967295;} 0..3))
$h{"authenticator"}
Truth table50%1/2
A
A= $h{"authenticator"}
Aresult
11
00
118 }
119  
120 # pack attributes
121 7 my @bin_av = ();
122  
123 7 ▶ if ($with_msg_auth) {
Branch: 1 branch
BranchTrueFalseTotal
if ($with_msg_auth) 2 5 7
124 # now Message-Authenticator has to be the first attribute, add zero for now
125 2 push @bin_av, ATTR_MSG_AUTH_ZERO;
126 }
127  
128 7 my $n;
129 7 foreach my $av (@{$av_list}) {
130 15 $n++;
131 # Message-Authenticator
132 # now it has to be the first attribute
133 15 ▶ if (($av->{Name} eq ATTR_MSG_AUTH_NAME) && !$av->{Value}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($av->{"Name"} eq "Message-Authenticator" and not $av->{"Value"}) 2 13 15
Condition66%2/3
$av->{"Name"} eq "Message-Authenticator" and not $av->{"Value"}
!ll&&!rl&&r
1302
MC/DC50%1/2
$av->{"Name"} eq "Message-Authenticator" and not $av->{"Value"}
$av->{"Name"} eq "Message-Authenticator" not $av->{"Value"}
Truth table66%2/3
A and not(B)
A= $av->{"Name"} eq "Message-Authenticator"B= not $av->{"Value"}
ABresult
0X0
100
111
134 2 ▶ if ($IS_ACCOUNTING{$type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($IS_ACCOUNTING{$type}) 1 1 2
135 1 my $msg = 'Message-Authenticator attribute is ignored';
136 1 ▶ Carp::croak($msg) if ($h{RaiseError});
Branch: 1 branch
BranchTrueFalseTotal
if $h{"RaiseError"} 0 1 1
137 1 ▶ Carp::carp($msg) if ($h{PrintError});
Branch: 1 branch
BranchTrueFalseTotal
if $h{"PrintError"} 1 0 1
138 1 next;
139 }
140  
141 1 ▶ if ($n > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($n > 1) { } 0 1 1
elsif (not $with_msg_auth) { } 1 0 1
142 0 my $msg = 'Message-Authenticator must be the first attribute in the list';
143 0 ▶ Carp::croak($msg) if ($h{RaiseError});
Branch: 1 branch
BranchTrueFalseTotal
if $h{"RaiseError"} 0 0 0
144 0 ▶ Carp::carp($msg) if ($h{PrintError});
Branch: 1 branch
BranchTrueFalseTotal
if $h{"PrintError"} 0 0 0
145 }
146 elsif (! $with_msg_auth ) {
147 # not added yet
148 1 push @bin_av, ATTR_MSG_AUTH_ZERO;
149 1 $with_msg_auth = 1;
150 }
151  
152 # already added
153 1 next;
154 }
155  
156 13 my $bin = eval { $self->pack_attribute($av, $authenticator) };
157 13 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 13 13
158 0 my $msg = $@;
159 0 ▶ Carp::croak($msg) if ($h{RaiseError});
Branch: 1 branch
BranchTrueFalseTotal
if $h{"RaiseError"} 0 0 0
160 0 ▶ Carp::carp ($msg) if ($h{PrintError});
Branch: 1 branch
BranchTrueFalseTotal
if $h{"PrintError"} 0 0 0
161 }
162 13 ▶ push (@bin_av, $bin) if $bin;
Branch: 1 branch
BranchTrueFalseTotal
if $bin 13 0 13
163 }
164  
165 7 my $attributes = join('', @bin_av);
166  
167 # build packet header
168  
169 7 my $length = 20 + length($attributes);
170  
171 # generate new sequential id if not given (one byte size)
172 7 ▶ my $req_id = $h{request_id} // ($request_id++) & 0xff;
Condition100%2/2
$h{"request_id"} // $request_id++ & 255
l!l
52
MC/DC100%1/1
$h{"request_id"} // $request_id++ & 255
$h{"request_id"}
Truth table100%2/2
A
A= $h{"request_id"}
Aresult
11
00
173  
174 # RFC3579 Message-Authenticator (EAP)
175 7 ▶ if($with_msg_auth) {
Branch: 1 branch
BranchTrueFalseTotal
if ($with_msg_auth) 3 4 7
176 # calculate and update Message-Authenticator attribute
177  
178 3 my $used_auth;
179 3 ▶ if ($type == ACCESS_REQUEST) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type == 1) { } 2 1 3
elsif ($self->is_request($type)) { } 1 0 1
180 # random-generated
181 2 $used_auth = $authenticator;
182 }
183 elsif ($self->is_request($type)) {
184 1 $used_auth = "\x00" x 16;
185 }
186 else {
187 # must be passed when composing replies
188 0 $used_auth = $h{authenticator};
189 }
190  
191 3 my $data = join('',
192 pack('C C n', $type, $req_id, $length),
193 $used_auth,
194 $attributes,
195 );
196  
197 3 my $hmac = Digest::HMAC_MD5->new($self->secret);
198 3 $hmac->add( $data );
199 3 my $msg_auth = $hmac->digest;
200  
201 # replace zeroes with the actual value
202 3 substr($attributes, 2, ATTR_MSG_AUTH_LEN - 2, $msg_auth );
203 }
204  
205 # calculate authentificator value for non-authentication request
206 7 ▶ if (! $authenticator) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($authenticator) 4 3 7
207 # calculated from content
208 4 ▶ my $used_auth = $self->is_request($type) ? "\x0" x 16 : $h{authenticator};
Branch: 1 branch
BranchTrueFalseTotal
$self->is_request($type) ? : 4 0 4
209  
210 4 my $hdr = pack('C C n', $type, $req_id, $length);
211 4 ▶ $md5 //= Digest::MD5->new;
Condition66%2/3
$md5 //= "Digest::MD5"->new
l!l&&r!l&&!r
220
MC/DC0%0/2
$md5 //= "Digest::MD5"->new
$md5 "Digest::MD5"->new
Truth table66%2/3
A //= B
A= $md5B= "Digest::MD5"->new
ABresult
1X1
011
000
212 4 $md5->add($hdr, $used_auth, $attributes, $self->secret);
213 4 $authenticator = $md5->digest();
214 }
215  
216 # wtf?
217 7 ▶ Carp::croak("No authenticator") if ! $authenticator;
Branch: 1 branch
BranchTrueFalseTotal
unless $authenticator 0 7 7
218  
219 7 my $packet = join('',
220 pack('C C n', $type, $req_id, $length),
221 $authenticator,
222 $attributes,
223 );
224  
225 7 return ($packet, $req_id, $authenticator);
226 }
227  
228 # authenticator required only for password attribute
229 # av: {Name,Value,[Tag]} or {Id,Type,Value,[VendorId],[Tag]}
230 16 ▶sub pack_attribute {
Subroutine pack_attribute: called CC=20 SCAR=30.8
Pod undocumented
231 16 my ($self, $av, $authenticator) = @_;
232  
233 # optional
234 16 my $dict = $self->dict;
235  
236 16 my $attr;
237 my $vendor_id;
238  
239 # attribute not present in dictionary must be passed as {Id, Type, Value, VendorId, Tag },
240 # where VendorId and Tag are optional
241 16 ▶ if ($av->{Id}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($av->{'Id'}) { } 2 14 16
elsif (defined $av->{'Name'}) { } 14 0 14
242 2 ▶ die "No attribute type for $av->{Id}\n" if ! $av->{Type};
Branch: 1 branch
BranchTrueFalseTotal
unless $av->{"Type"} 0 2 2
243 $attr = {
244 id => $av->{Id},
245 name => $av->{Id},
246 type => $av->{Type},
247 vendor => $av->{VendorId},
248 has_tag => defined $av->{Tag},
249 2 };
250 2 $vendor_id = $av->{VendorId};
251 }
252 elsif (defined $av->{Name}) {
253 # av: {Name, Value}
254 14 ▶ die "No dictionary to encode attribute '$av->{Name}'\n" if ! $dict;
Branch: 1 branch
BranchTrueFalseTotal
unless $dict 0 14 14
255  
256 # tagged attribute
257 14 ▶ if ($av->{Name} =~ /^([\w-]+):(\d+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($av->{"Name"} =~ /^([\w-]+):(\d+)$/) 2 12 14
258 2 ($av->{Name}, $av->{Tag}) = ($1, $2);
259 }
260  
261 $attr = $dict->attribute($av->{Name})
262 14 ▶ or die "Unknown attribute '$av->{Name}'\n";
Branch: 1 branch
BranchTrueFalseTotal
$attr = $dict->attribute($av->{"Name"}) or die qq[Unknown attribute '$av->{"Name"}'\n] 14 0 14
263  
264 # TODO store vendor_id in dictionary parser
265 14 $vendor_id = $dict->vendor_id($attr->{vendor});
266 }
267  
268 16 ▶ if (defined $av->{Tag}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $av->{"Tag"}) 3 13 16
269 die "Tag value $av->{Tag} is out of range [1..31] for attribute '$attr->{name}'\n"
270 3 ▶ if $av->{Tag} < 1 || $av->{Tag} > 31;
Branch: 1 branch
BranchTrueFalseTotal
if $av->{"Tag"} < 1 or $av->{"Tag"} > 31 0 3 3
Condition33%1/3
$av->{"Tag"} < 1 or $av->{"Tag"} > 31
l!l&&r!l&&!r
003
MC/DC0%0/2
$av->{"Tag"} < 1 or $av->{"Tag"} > 31
$av->{"Tag"} < 1 $av->{"Tag"} > 31
Truth table33%1/3
A or B
A= $av->{"Tag"} < 1B= $av->{"Tag"} > 31
ABresult
1X1
011
000
271 }
272  
273 16 my $value = $av->{Value};
274 16 ▶ die "Undefined value for attribute '$attr->{name}'\n" unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 16 16
275  
276 16 ▶ if ($attr->{id} == ATTR_PASSWORD && ! $vendor_id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr->{"id"} == 2 and not $vendor_id) 3 13 16
Condition66%2/3
$attr->{"id"} == 2 and not $vendor_id
!ll&&!rl&&r
1303
MC/DC50%1/2
$attr->{"id"} == 2 and not $vendor_id
$attr->{"id"} == 2 not $vendor_id
Truth table66%2/3
A and not(B)
A= $attr->{"id"} == 2B= not $vendor_id
ABresult
0X0
100
111
277 # need an authenticator - this attribute must be present only in ACCESS REQUEST
278 3 $value = encrypt_pwd($value, $self->secret, $authenticator);
279 }
280  
281 16 ▶ if ($attr->{type} ne 'tlv' && is_enum_type($attr->{type}) && $dict) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr->{"type"} ne "tlv" and is_enum_type($attr->{"type"}) and $dict) 4 12 16
Condition83%5/6
$attr->{"type"} ne "tlv" and is_enum_type($attr->{"type"}) and $dict
!ll&&!rl&&r
1114
$attr->{"type"} ne "tlv" and is_enum_type($attr->{"type"})
!ll&&!rl&&r
0115
MC/DC66%2/3
$attr->{"type"} ne "tlv" and is_enum_type($attr->{"type"}) and $dict
$attr->{"type"} ne "tlv" is_enum_type($attr->{"type"}) $dict
Truth table75%3/4
A and B and C
A= $attr->{"type"} ne "tlv"B= is_enum_type($attr->{"type"})C= $dict
ABCresult
0XX0
10X0
1100
1111
282 # convert constant-like values to real value
283 4 ▶ $value = $dict->value($attr->{name}, $value) // $value;
Condition66%2/3
$dict->value($attr->{"name"}, $value) // $value
l!l&&r!l&&!r
310
MC/DC0%0/2
$dict->value($attr->{"name"}, $value) // $value
$dict->value($attr->{"name"}, $value) $value
Truth table66%2/3
A // B
A= $dict->value($attr->{"name"}, $value)B= $value
ABresult
1X1
011
000
284 } # else - for TVL type value is ARRAY-ref
285  
286 16 local ($Data::Radius::Encode::PrintError, $Data::Radius::Encode::RaiseError) = (0,1);
287 16 my $encoded = encode($attr, $value, $self->dict, $av->{Tag} );
288 16 ▶ my $len_encoded = length($encoded)
Branch: 1 branch
BranchTrueFalseTotal
my $len_encoded = length $encoded or die qq[Unable to encode value for attribute '$attr->{"name"}'\n] 16 0 16
289 or die "Unable to encode value for attribute '$attr->{name}'\n";
290  
291 16 ▶ if (! $vendor_id) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($vendor_id) 13 3 16
292 # tag already included into value, if any
293 13 return pack('C C', $attr->{id}, $len_encoded + 2) . $encoded;
294 }
295  
296 # VSA
297  
298 3 my $vsa_header;
299 3 ▶ if ($vendor_id == WIMAX_VENDOR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vendor_id == 24757) { } 1 2 3
300 1 $vsa_header = pack('N C C C', $vendor_id, $attr->{id}, $len_encoded + 3, 0);
301 }
302 else {
303 # tag already included into value, if any
304 2 $vsa_header = pack('N C C', $vendor_id, $attr->{id}, $len_encoded + 2);
305 }
306  
307 3 return pack('C C', ATTR_VENDOR, length($vsa_header) + $len_encoded + 2) . $vsa_header . $encoded;
308 }
309  
310 # parse binary-encoded radius packet
311 # returns list: type, request-id, authenticator, \@AV_list
312 6 ▶sub parse {
Subroutine parse: called CC=26 SCAR=37.0
Pod documented
313 6 my ($self, $packet, $orig_auth) = @_;
314  
315 6 my $dict = $self->dict;
316  
317 6 my($type, $req_id, $length, $auth, $attributes) = unpack('C C n a16 a*', $packet);
318  
319 # Validate authenticator field
320 6 my $expected_auth;
321 6 ▶ if ($type == ACCESS_REQUEST) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 1) { } 2 4 6
322 # authenticator is random value - no validation
323 }
324 else {
325 4 my $used_auth;
326 4 ▶ if ($self->is_request($type)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_request($type)) { } 4 0 4
327 4 $used_auth = "\x00" x 16;
328 }
329 else {
330 # fo replied we have to use authenticator from request:
331 0 ▶ if (! $orig_auth) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($orig_auth) 0 0 0
332 0 warn "No original authenticator - unable to verify reply";
333 0 return undef;
334 }
335 0 $used_auth = $orig_auth;
336 }
337  
338 4 ▶ $md5 //= Digest::MD5->new;
Condition33%1/3
$md5 //= "Digest::MD5"->new
l!l&&r!l&&!r
400
MC/DC0%0/2
$md5 //= "Digest::MD5"->new
$md5 "Digest::MD5"->new
Truth table33%1/3
A //= B
A= $md5B= "Digest::MD5"->new
ABresult
1X1
011
000
339  
340 4 my $hdr = pack('C C n', $type, $req_id, $length);
341 4 $md5->add($hdr, $used_auth, $attributes, $self->secret);
342 4 $expected_auth = $md5->digest();
343  
344 4 ▶ if($auth ne $expected_auth) {
Branch: 1 branch
BranchTrueFalseTotal
if ($auth ne $expected_auth) 0 4 4
345 0 warn "Bad authenticator value";
346 0 return undef;
347 }
348 }
349  
350 # decode attributes
351 6 my @attr;
352 my $msg_auth;
353 6 my $pos = 0;
354 6 my $len = length($attributes);
355  
356 6 ▶ while ($pos < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < $len) 13 6 19
357 13 my ($attr_val, $vendor_id, $vendor, $vsa_len, $attr, $tag) = ();
358 # FIXME not supported
359 13 my $wimax_cont;
360  
361 13 my ($attr_id, $attr_len) = unpack('C C', substr($attributes, $pos, 2));
362  
363 13 ▶ if ($attr_id == ATTR_VENDOR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr_id == 26) { } 1 12 13
364 1 my $vsa_header_len = 6;
365  
366 1 ($vendor_id, $attr_id, $vsa_len) = unpack('N C C', substr($attributes, $pos + 2, $vsa_header_len) );
367 1 ▶ if ($vendor_id == WIMAX_VENDOR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vendor_id == 24757) 0 1 1
368 # +1 continuation byte
369 0 $vsa_header_len = 7;
370 0 $wimax_cont = unpack('C', substr($attributes, $pos + 8, 1));
371 0 ▶ warn 'continuation field is not supported' if ($wimax_cont);
Branch: 1 branch
BranchTrueFalseTotal
if $wimax_cont 0 0 0
372 0 printf "WIMAX cont: %d\n", $wimax_cont;
373 }
374  
375 1 ▶ if ($dict) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dict) 1 0 1
376 1 ▶ $vendor = $dict->vendor_name($vendor_id) // $vendor_id;
Condition33%1/3
$dict->vendor_name($vendor_id) // $vendor_id
l!l&&r!l&&!r
100
MC/DC0%0/2
$dict->vendor_name($vendor_id) // $vendor_id
$dict->vendor_name($vendor_id) $vendor_id
Truth table33%1/3
A // B
A= $dict->vendor_name($vendor_id)B= $vendor_id
ABresult
1X1
011
000
377 1 $attr = $dict->attribute_name($vendor, $attr_id);
378 }
379  
380 1 $attr_val = substr($attributes, $pos + 2 + $vsa_header_len, $attr_len - 2 - $vsa_header_len);
381 }
382 else {
383 12 ▶ if ($dict) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dict) 12 0 12
384 12 $attr = $dict->attribute_name(undef, $attr_id);
385 }
386  
387 12 $attr_val = substr($attributes, $pos + 2, $attr_len - 2);
388 }
389  
390 13 ▶ if ($attr_id == ATTR_MSG_AUTH && ! $vendor) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr_id == 80 and not $vendor) 2 11 13
Condition66%2/3
$attr_id == 80 and not $vendor
!ll&&!rl&&r
1102
MC/DC50%1/2
$attr_id == 80 and not $vendor
$attr_id == 80 not $vendor
Truth table66%2/3
A and not(B)
A= $attr_id == 80B= not $vendor
ABresult
0X0
100
111
391 2 ▶ die "Invalid Message-Authenticator len" if ($attr_len != ATTR_MSG_AUTH_LEN);
Branch: 1 branch
BranchTrueFalseTotal
if $attr_len != 18 0 2 2
392 2 $msg_auth = $attr_val;
393 # zero it to verify later
394 2 $attr_val = "\x0" x (ATTR_MSG_AUTH_LEN - 2);
395 2 substr($attributes, $pos + 2, $attr_len - 2, $attr_val);
396 }
397  
398 13 $pos += $attr_len;
399  
400 13 ▶ if (! $attr) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($attr) 0 13 13
401 # raw data for unknown attribute
402 0 push @attr, {
403 Name => $attr_id,
404 Value => $attr_val,
405 Type => undef,
406 Vendor => $vendor,
407 Tag => undef,
408 };
409 0 next;
410 }
411  
412 13 (my $decoded, $tag) = decode($attr, $attr_val, $self->dict);
413 13 ▶ if (is_enum_type($attr->{type})) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_enum_type($attr->{"type"})) 4 9 13
414 # try to convert value to constants
415 4 ▶ $decoded = $dict->constant($attr->{name}, $decoded) // $decoded;
Condition66%2/3
$dict->constant($attr->{"name"}, $decoded) // $decoded
l!l&&r!l&&!r
310
MC/DC0%0/2
$dict->constant($attr->{"name"}, $decoded) // $decoded
$dict->constant($attr->{"name"}, $decoded) $decoded
Truth table66%2/3
A // B
A= $dict->constant($attr->{"name"}, $decoded)B= $decoded
ABresult
1X1
011
000
416 }
417  
418 # password is expected only in auth request
419 13 ▶ if ($type == ACCESS_REQUEST && $attr->{id} == ATTR_PASSWORD && ! $attr->{vendor}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 1 and $attr->{"id"} == 2 and not $attr->{"vendor"}) 2 11 13
Condition83%5/6
$type == 1 and $attr->{"id"} == 2 and not $attr->{"vendor"}
!ll&&!rl&&r
1102
$type == 1 and $attr->{"id"} == 2
!ll&&!rl&&r
562
MC/DC66%2/3
$type == 1 and $attr->{"id"} == 2 and not $attr->{"vendor"}
$type == 1 $attr->{"id"} == 2 not $attr->{"vendor"}
Truth table75%3/4
A and B and not(C)
A= $type == 1B= $attr->{"id"} == 2C= not $attr->{"vendor"}
ABCresult
0XX0
10X0
1100
1111
420 2 $decoded = decrypt_pwd($decoded, $self->secret, $auth);
421 }
422  
423 push @attr, {
424 Name => $attr->{name},
425 Value => $decoded,
426 Type => $attr->{type},
427 13 Vendor => $vendor,
428 Tag => $tag,
429 };
430 }
431  
432 6 ▶ if($msg_auth) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg_auth) 2 4 6
433 # we already replaced msg auth value to \x0...
434 2 my $auth_used;
435 2 ▶ if ($self->is_reply($type)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->is_reply($type)) { } 0 2 2
elsif ($type == 1) { } 1 1 2
436 0 $auth_used = $orig_auth;
437 }
438 elsif ($type == ACCESS_REQUEST) {
439 1 $auth_used = $auth;
440 }
441 else {
442 # other type of request should use 00x16
443 # Message-Authenticator should not be present in ACCOUNTING_REQUEST
444 1 $auth_used = "\x00" x 16;
445 }
446  
447 2 my $data = join('',
448 pack('C C n', $type, $req_id, $length),
449 $auth_used,
450 $attributes,
451 );
452 2 my $hmac = Digest::HMAC_MD5->new($self->secret);
453 2 $hmac->add( $data );
454 2 my $exp_msg_auth = $hmac->digest;
455  
456 2 ▶ if ($msg_auth ne $exp_msg_auth) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg_auth ne $exp_msg_auth) 0 2 2
457 0 warn "Message-Authenticator not verified";
458 0 return undef;
459 }
460 }
461  
462 6 return ($type, $req_id, $auth, \@attr);
463 }
464  
465 # extract request id from packet header without parsing attributes
466 # should be used to find original authenticator value for received reply packet to pass it to decode_request()
467 0 ▶sub request_id {
Subroutine request_id: not called CC=1 SCAR=6.9
Pod documented
468 0 my ($self, $packet) = @_;
469 0 my $req_id = unpack('C', substr($packet, 1, 1));
470 0 return $req_id;
471 }
472  
473 9 ▶sub is_reply {
Subroutine is_reply: called CC=2 SCAR=7.2
Pod undocumented
474 9 my ($class, $type) = @_;
475 9 ▶ return $IS_REPLY{ $type } // 0;
Condition50%1/2
$IS_REPLY{$type} // 0
l!l
09
MC/DC0%0/1
$IS_REPLY{$type} // 0
$IS_REPLY{$type}
Truth table50%1/2
A
A= $IS_REPLY{$type}
Aresult
11
00
476 }
477  
478 9 ▶sub is_request {
Subroutine is_request: called CC=2 SCAR=7.2
Pod undocumented
479 9 my ($class, $type) = @_;
480 9 ▶ return $IS_REQUEST{ $type } // 0;
Condition50%1/2
$IS_REQUEST{$type} // 0
l!l
90
MC/DC0%0/1
$IS_REQUEST{$type} // 0
$IS_REQUEST{$type}
Truth table50%1/2
A
A= $IS_REQUEST{$type}
Aresult
11
00
481 }
482  
483 4 1;
484  
485 __END__
↑ Summary Radius/DictionaryParser.pm »