Radius/Packet.pm

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