SNMP/Security/USM.pm

filter: Statementstmt 39.4% 241 / 611 Branchbran 26.3% 109 / 414 Conditioncond 26.0% 19 / 73 MC/DCmcdc 8.8% 4 / 45 Subroutinesub 58.3% 42 / 72 Podpod 0.0% 0 / 26 totaltotal 33.4% 415 / 1241 CC 237 SCAR 50.7
CC
237
Cov
34%
process_incoming_msg
930.0
generate_request_msg
600.0
_timeliness
110.0
CRAP 159.3
« SNMP.pm ↑ Summary SNMP/PDU.pm »
1 # -*- mode: perl -*-
2 # ============================================================================
3  
4 package Net::SNMP::Security::USM;
5  
6 # $Id: USM.pm,v 4.1 2010/09/10 00:01:22 dtown Rel $
7  
8 # Object that implements the SNMPv3 User-based Security Model.
9  
10 # Copyright (c) 2001-2010 David M. Town <dtown@cpan.org>
11 # All rights reserved.
12  
13 # This program is free software; you may redistribute it and/or modify it
14 # under the same terms as the Perl 5 programming language system itself.
15  
16 # ============================================================================
17  
18 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 ▶use Net::SNMP::Security qw( :ALL );
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 ▶use Net::SNMP::Message qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
23 :msgFlags asn1_itoa OCTET_STRING SEQUENCE INTEGER SNMP_VERSION_3 TRUE FALSE
24 1 );
25  
26 1 ▶use Crypt::DES();
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use Digest::MD5();
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use Digest::SHA1();
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use Digest::HMAC();
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 ## Version of the Net::SNMP::Security::USM module
32  
33 1 our $VERSION = v4.0.1;
34  
35 ## Handle importing/exporting of symbols
36  
37 1 ▶use base qw( Net::SNMP::Security );
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 1 our @EXPORT_OK;
40  
41 1 our %EXPORT_TAGS = (
42 authprotos => [
43 qw( AUTH_PROTOCOL_NONE AUTH_PROTOCOL_HMACMD5 AUTH_PROTOCOL_HMACSHA )
44 ],
45 levels => [
46 qw( SECURITY_LEVEL_NOAUTHNOPRIV SECURITY_LEVEL_AUTHNOPRIV
47 SECURITY_LEVEL_AUTHPRIV )
48 ],
49 models => [
50 qw( SECURITY_MODEL_ANY SECURITY_MODEL_SNMPV1 SECURITY_MODEL_SNMPV2C
51 SECURITY_MODEL_USM )
52 ],
53 privprotos => [
54 qw( PRIV_PROTOCOL_NONE PRIV_PROTOCOL_DES PRIV_PROTOCOL_AESCFB128
55 PRIV_PROTOCOL_DRAFT_3DESEDE PRIV_PROTOCOL_DRAFT_AESCFB128
56 PRIV_PROTOCOL_DRAFT_AESCFB192 PRIV_PROTOCOL_DRAFT_AESCFB256 )
57 ],
58 );
59  
60 1 Exporter::export_ok_tags( qw( authprotos levels models privprotos ) );
61  
62 1 $EXPORT_TAGS{ALL} = [ @EXPORT_OK ];
63  
64 ## RCC 3414 - Authentication protocols
65  
66 0 ▶sub AUTH_PROTOCOL_NONE { '1.3.6.1.6.3.10.1.1.1' } # usmNoAuthProtocol
Subroutine AUTH_PROTOCOL_NONE: not called CC=1 SCAR=6.9
Pod undocumented
67 15 ▶sub AUTH_PROTOCOL_HMACMD5 { '1.3.6.1.6.3.10.1.1.2' } # usmHMACMD5AuthProtocol
Subroutine AUTH_PROTOCOL_HMACMD5: called CC=1 SCAR=0.0
Pod undocumented
68 12 ▶sub AUTH_PROTOCOL_HMACSHA { '1.3.6.1.6.3.10.1.1.3' } # usmHMACSHAAuthProtocol
Subroutine AUTH_PROTOCOL_HMACSHA: called CC=1 SCAR=0.0
Pod undocumented
69  
70 ## RFC 3414 - Privacy protocols
71  
72 0 ▶sub PRIV_PROTOCOL_NONE { '1.3.6.1.6.3.10.1.2.1' } # usmNoPrivProtocol
Subroutine PRIV_PROTOCOL_NONE: not called CC=1 SCAR=6.9
Pod undocumented
73 15 ▶sub PRIV_PROTOCOL_DES { '1.3.6.1.6.3.10.1.2.2' } # usmDESPrivProtocol
Subroutine PRIV_PROTOCOL_DES: called CC=1 SCAR=0.0
Pod undocumented
74  
75 ## RFC 3826 - The AES Cipher Algorithm in the SNMP USM
76  
77 # usmAesCfb128Protocol
78 16 ▶sub PRIV_PROTOCOL_AESCFB128 { '1.3.6.1.6.3.10.1.2.4' }
Subroutine PRIV_PROTOCOL_AESCFB128: called CC=1 SCAR=0.0
Pod undocumented
79  
80 # The privacy protocols below have been implemented using the draft
81 # specifications intended to extend the User-based Security Model
82 # defined in RFC 3414. Since the object definitions have not been
83 # standardized, they have been based on the Extended Security Options
84 # Consortium MIB found at http://www.snmp.com/eso/esoConsortiumMIB.txt.
85  
86 # Extension to Support Triple-DES EDE <draft-reeder-snmpv3-usm-3desede-00.txt>
87 # Reeder and Gudmunsson; October 1999, expired April 2000
88  
89 # usm3DESPrivProtocol
90 19 ▶sub PRIV_PROTOCOL_DRAFT_3DESEDE { '1.3.6.1.4.1.14832.1.1' }
Subroutine PRIV_PROTOCOL_DRAFT_3DESEDE: called CC=1 SCAR=0.0
Pod undocumented
91  
92 # AES Cipher Algorithm in the USM <draft-blumenthal-aes-usm-04.txt>
93 # Blumenthal, Maino, and McCloghrie; October 2002, expired April 2003
94  
95 # usmAESCfb128PrivProtocol
96 1 ▶sub PRIV_PROTOCOL_DRAFT_AESCFB128 { '1.3.6.1.4.1.14832.1.2' }
Subroutine PRIV_PROTOCOL_DRAFT_AESCFB128: called CC=1 SCAR=0.0
Pod undocumented
97  
98 # usmAESCfb192PrivProtocol
99 17 ▶sub PRIV_PROTOCOL_DRAFT_AESCFB192 { '1.3.6.1.4.1.14832.1.3' }
Subroutine PRIV_PROTOCOL_DRAFT_AESCFB192: called CC=1 SCAR=0.0
Pod undocumented
100  
101 # usmAESCfb256PrivProtocol
102 17 ▶sub PRIV_PROTOCOL_DRAFT_AESCFB256 { '1.3.6.1.4.1.14832.1.4' }
Subroutine PRIV_PROTOCOL_DRAFT_AESCFB256: called CC=1 SCAR=0.0
Pod undocumented
103  
104 ## Package variables
105  
106 1 our $ENGINE_ID; # Our authoritative snmpEngineID
107 # [public methods] -----------------------------------------------------------
108  
109 sub new
110 2 ▶{
Subroutine new: called CC=24 SCAR=44.3
Pod undocumented
111 2 my ($class, %argv) = @_;
112  
113 # Create a new data structure for the object
114 2 my $this = bless {
115 '_error' => undef, # Error message
116 '_version' => SNMP_VERSION_3, # version
117 '_authoritative' => FALSE, # Authoritative flag
118 '_discovered' => FALSE, # Engine discovery flag
119 '_synchronized' => FALSE, # Synchronization flag
120 '_engine_id' => q{}, # snmpEngineID
121 '_engine_boots' => 0, # snmpEngineBoots
122 '_engine_time' => 0, # snmpEngineTime
123 '_latest_engine_time' => 0, # latestReceivedEngineTime
124 '_time_epoc' => time(), # snmpEngineBoots epoc
125 '_user_name' => q{}, # securityName
126 '_auth_data' => undef, # Authentication data
127 '_auth_key' => undef, # authKey
128 '_auth_password' => undef, # Authentication password
129 '_auth_protocol' => AUTH_PROTOCOL_HMACMD5, # authProtocol
130 '_priv_data' => undef, # Privacy data
131 '_priv_key' => undef, # privKey
132 '_priv_password' => undef, # Privacy password
133 '_priv_protocol' => PRIV_PROTOCOL_DES, # privProtocol
134 '_security_level' => SECURITY_LEVEL_NOAUTHNOPRIV
135 }, $class;
136  
137 # We first need to find out if we are an authoritative SNMP
138 # engine and set the authProtocol and privProtocol if they
139 # have been provided.
140  
141 2 foreach (keys %argv) {
142  
143 9 ▶ if (/^-?authoritative$/i) {
Branch: 3 branches
BranchTrueFalseTotal
if (/^-?authoritative$/i) { } 0 9 9
elsif (/^-?authprotocol$/i) { } 1 8 9
elsif (/^-?privprotocol$/i) { } 2 6 8
144 0 ▶ $this->{_authoritative} = (delete $argv{$_}) ? TRUE : FALSE;
Branch: 1 branch
BranchTrueFalseTotal
delete $argv{$_} ? : 0 0 0
145 } elsif (/^-?authprotocol$/i) {
146 1 $this->_auth_protocol(delete $argv{$_});
147 } elsif (/^-?privprotocol$/i) {
148 2 $this->_priv_protocol(delete $argv{$_});
149 }
150  
151 9 ▶ if (defined $this->{_error}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $this->{"_error"}) 0 9 9
152 0 ▶ return wantarray ? (undef, $this->{_error}) : undef;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
153 }
154 }
155  
156 # Now validate the rest of the passed arguments
157  
158 2 for (keys %argv) {
159  
160 6 ▶ if (/^-?version$/i) {
Branch: 8 branches
BranchTrueFalseTotal
if (/^-?version$/i) { } 0 6 6
elsif (/^-?debug$/i) { } 0 6 6
elsif (/^-?engineid$/i and $this->{'_authoritative'}) { } 0 6 6
elsif (/^-?username$/i) { } 2 4 6
elsif (/^-?authkey$/i) { } 0 4 4
elsif (/^-?authpassword$/i) { } 2 2 4
elsif (/^-?privkey$/i) { } 0 2 2
elsif (/^-?privpassword$/i) { } 2 0 2
161 0 $this->_version($argv{$_});
162 } elsif (/^-?debug$/i) {
163 0 $this->debug($argv{$_});
164 ▶ } elsif ((/^-?engineid$/i) && ($this->{_authoritative})) {
Condition33%1/3
/^-?engineid$/i and $this->{"_authoritative"}
!ll&&!rl&&r
600
MC/DC0%0/2
/^-?engineid$/i and $this->{"_authoritative"}
/^-?engineid$/i $this->{"_authoritative"}
Truth table33%1/3
A and B
A= /^-?engineid$/iB= $this->{"_authoritative"}
ABresult
0X0
100
111
165 0 $this->_engine_id($argv{$_});
166 } elsif (/^-?username$/i) {
167 2 $this->_user_name($argv{$_});
168 } elsif (/^-?authkey$/i) {
169 0 $this->_auth_key($argv{$_});
170 } elsif (/^-?authpassword$/i) {
171 2 $this->_auth_password($argv{$_});
172 } elsif (/^-?privkey$/i) {
173 0 $this->_priv_key($argv{$_});
174 } elsif (/^-?privpassword$/i) {
175 2 $this->_priv_password($argv{$_});
176 } else {
177 0 $this->_error('The argument "%s" is unknown', $_);
178 }
179  
180 6 ▶ if (defined $this->{_error}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $this->{"_error"}) 0 6 6
181 0 ▶ return wantarray ? (undef, $this->{_error}) : undef;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
182 }
183  
184 }
185  
186 # Generate a snmpEngineID and populate the object accordingly
187 # if we are an authoritative snmpEngine.
188  
189 2 ▶ if ($this->{_authoritative}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_authoritative"}) 0 2 2
190 0 $this->_snmp_engine_init();
191 }
192  
193 # Define the securityParameters
194 2 ▶ if (!defined $this->_security_params()) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $this->_security_params) 0 2 2
195 0 ▶ return wantarray ? (undef, $this->{_error}) : undef;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
196 }
197  
198 # Return the object and an empty error message (in list context)
199 2 ▶ return wantarray ? ($this, q{}) : $this;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 0 2
200 }
201  
202 sub generate_request_msg
203 0 ▶{
Subroutine generate_request_msg: not called CC=24 SCAR=64.0
Pod undocumented
204 0 my ($this, $pdu, $msg) = @_;
205  
206 # Clear any previous errors
207 0 $this->_error_clear();
208  
209 0 ▶ if (@_ < 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ < 3) 0 0 0
210 0 return $this->_error('The required PDU and/or Message object is missing');
211 }
212  
213 # Validate the SNMP version of the PDU
214 0 ▶ if ($pdu->version() != $this->{_version}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pdu->version != $this->{"_version"}) 0 0 0
215 return $this->_error(
216 'The SNMP version %d was expected, but %d was found',
217 0 $this->{_version}, $pdu->version()
218 );
219 }
220  
221 # Validate the securityLevel of the PDU
222 0 ▶ if ($pdu->security_level() > $this->{_security_level}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pdu->security_level > $this->{"_security_level"}) 0 0 0
223 return $this->_error(
224 'The PDU securityLevel %d is greater than the configured value %d',
225 $pdu->security_level(), $this->{_security_level}
226 0 );
227 }
228  
229 # Validate PDU type with snmpEngine type
230 0 ▶ if ($pdu->expect_response()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pdu->expect_response) { } 0 0 0
231 0 ▶ if ($this->{_authoritative}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_authoritative"}) 0 0 0
232 0 return $this->_error(
233 'Must be a non-authoritative SNMP engine to generate a %s',
234 asn1_itoa($pdu->pdu_type())
235 );
236 }
237 } else {
238 0 ▶ if (!$this->{_authoritative}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($this->{"_authoritative"}) 0 0 0
239 0 return $this->_error(
240 'Must be an authoritative SNMP engine to generate a %s',
241 asn1_itoa($pdu->pdu_type())
242 );
243 }
244 }
245  
246 # Extract the msgGlobalData out of the message
247 0 my $msg_global_data = $msg->clear();
248  
249 # AES in the USM Section 3.1.2.1 - "The 128-bit IV is obtained as
250 # the concatenation of the... ...snmpEngineBoots, ...snmpEngineTime,
251 # and a local 64-bit integer. We store the current snmpEngineBoots
252 # and snmpEngineTime before encrypting the PDU so that the computed
253 # IV matches the transmitted msgAuthoritativeEngineBoots and
254 # msgAuthoritativeEngineTime.
255  
256 0 my $msg_engine_time = $this->_engine_time();
257 0 my $msg_engine_boots = $this->_engine_boots();
258  
259 # Copy the PDU into a "plain text" buffer
260 0 my $pdu_buffer = $pdu->copy();
261 0 my $priv_params = q{};
262  
263 # encryptedPDU::=OCTET STRING
264 0 ▶ if ($pdu->security_level() > SECURITY_LEVEL_AUTHNOPRIV) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pdu->security_level > SECURITY_LEVEL_AUTHNOPRIV()) 0 0 0
265 0 ▶ if (!defined $this->_encrypt_data($msg, $priv_params, $pdu_buffer)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $this->_encrypt_data($msg, $priv_params, $pdu_buffer)) 0 0 0
266 0 return $this->_error();
267 }
268 }
269  
270 # msgPrivacyParameters::=OCTET STRING
271 0 ▶ if (!defined $msg->prepare(OCTET_STRING, $priv_params)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $msg->prepare(OCTET_STRING(), $priv_params)) 0 0 0
272 0 return $this->_error($msg->error());
273 }
274  
275 # msgAuthenticationParameters::=OCTET STRING
276  
277 0 my $auth_params = q{};
278 0 my $auth_location = 0;
279  
280 0 ▶ if ($pdu->security_level() > SECURITY_LEVEL_NOAUTHNOPRIV) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pdu->security_level > SECURITY_LEVEL_NOAUTHNOPRIV()) 0 0 0
281  
282 # Save the location to fill in msgAuthenticationParameters later
283 0 $auth_location = $msg->length() + 12 + length $pdu_buffer;
284  
285 # Set the msgAuthenticationParameters to all zeros
286 0 $auth_params = pack 'x12';
287 }
288  
289 0 ▶ if (!defined $msg->prepare(OCTET_STRING, $auth_params)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $msg->prepare(OCTET_STRING(), $auth_params)) 0 0 0
290 0 return $this->_error($msg->error());
291 }
292  
293 # msgUserName::=OCTET STRING
294 0 ▶ if (!defined $msg->prepare(OCTET_STRING, $pdu->security_name())) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $msg->prepare(OCTET_STRING(), $pdu->security_name)) 0 0 0
295 0 return $this->_error($msg->error());
296 }
297  
298 # msgAuthoritativeEngineTime::=INTEGER
299 0 ▶ if (!defined $msg->prepare(INTEGER, $msg_engine_time)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $msg->prepare(INTEGER(), $msg_engine_time)) 0 0 0
300 0 return $this->_error($msg->error());
301 }
302  
303 # msgAuthoritativeEngineBoots::=INTEGER
304 0 ▶ if (!defined $msg->prepare(INTEGER, $msg_engine_boots)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $msg->prepare(INTEGER(), $msg_engine_boots)) 0 0 0
305 0 return $this->_error($msg->error());
306 }
307  
308 # msgAuthoritativeEngineID
309 0 ▶ if (!defined $msg->prepare(OCTET_STRING, $this->_engine_id())) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $msg->prepare(OCTET_STRING(), $this->_engine_id)) 0 0 0
310 0 return $this->_error($msg->error());
311 }
312  
313 # UsmSecurityParameters::= SEQUENCE
314 0 ▶ if (!defined $msg->prepare(SEQUENCE)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $msg->prepare(SEQUENCE())) 0 0 0
315 0 return $this->_error($msg->error());
316 }
317  
318 # msgSecurityParameters::=OCTET STRING
319 0 ▶ if (!defined $msg->prepare(OCTET_STRING, $msg->clear())) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $msg->prepare(OCTET_STRING(), $msg->clear)) 0 0 0
320 0 return $this->_error($msg->error());
321 }
322  
323 # Append the PDU
324 0 ▶ if (!defined $msg->append($pdu_buffer)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $msg->append($pdu_buffer)) 0 0 0
325 0 return $this->_error($msg->error());
326 }
327  
328 # Prepend the msgGlobalData
329 0 ▶ if (!defined $msg->prepend($msg_global_data)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $msg->prepend($msg_global_data)) 0 0 0
330 0 return $this->_error($msg->error());
331 }
332  
333 # version::=INTEGER
334 0 ▶ if (!defined $msg->prepare(INTEGER, $this->{_version})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $msg->prepare(INTEGER(), $this->{"_version"})) 0 0 0
335 0 return $this->_error($msg->error());
336 }
337  
338 # message::=SEQUENCE
339 0 ▶ if (!defined $msg->prepare(SEQUENCE)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $msg->prepare(SEQUENCE())) 0 0 0
340 0 return $this->_error($msg->error());
341 }
342  
343 # Apply authentication
344 0 ▶ if ($pdu->security_level() > SECURITY_LEVEL_NOAUTHNOPRIV) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pdu->security_level > SECURITY_LEVEL_NOAUTHNOPRIV()) 0 0 0
345 0 ▶ if (!defined $this->_authenticate_outgoing_msg($msg, $auth_location)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $this->_authenticate_outgoing_msg($msg, $auth_location)) 0 0 0
346 0 return $this->_error($msg->error());
347 }
348 }
349  
350 # Return the Message
351 0 return $msg;
352 }
353  
354 sub process_incoming_msg
355 0 ▶{
Subroutine process_incoming_msg: not called CC=30 SCAR=68.4
Pod undocumented
356 0 my ($this, $msg) = @_;
357  
358 # Clear any previous errors
359 0 $this->_error_clear();
360  
361 0 ▶ return $this->_error('The required Message object is missing') if (@_ < 2);
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 2 0 0 0
362  
363 # msgSecurityParameters::=OCTET STRING
364  
365 0 my $msg_params = $msg->process(OCTET_STRING);
366 0 ▶ return $this->_error($msg->error()) if !defined $msg_params;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $msg_params 0 0 0
367  
368 # Need to move the buffer index back to the begining of the data
369 # portion of the OCTET STRING that contains the msgSecurityParameters.
370  
371 0 $msg->index($msg->index() - length $msg_params);
372  
373 # UsmSecurityParameters::=SEQUENCE
374 0 ▶ return $this->_error($msg->error()) if !defined $msg->process(SEQUENCE);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $msg->process(SEQUENCE()) 0 0 0
375  
376 # msgAuthoritativeEngineID::=OCTET STRING
377 0 my $msg_engine_id;
378 0 ▶ if (!defined($msg_engine_id = $msg->process(OCTET_STRING))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined($msg_engine_id = $msg->process(OCTET_STRING()))) 0 0 0
379 0 return $this->_error($msg->error());
380 }
381  
382 # msgAuthoritativeEngineBoots::=INTEGER (0..2147483647)
383 0 my $msg_engine_boots;
384 0 ▶ if (!defined ($msg_engine_boots = $msg->process(INTEGER))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined($msg_engine_boots = $msg->process(INTEGER()))) 0 0 0
385 0 return $this->_error($msg->error());
386 }
387 0 ▶ if (($msg_engine_boots < 0) || ($msg_engine_boots > 2147483647)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg_engine_boots < 0 or $msg_engine_boots > 2147483647) 0 0 0
Condition0%0/3
$msg_engine_boots < 0 or $msg_engine_boots > 2147483647
l!l&&r!l&&!r
000
MC/DC0%0/2
$msg_engine_boots < 0 or $msg_engine_boots > 2147483647
$msg_engine_boots < 0 $msg_engine_boots > 2147483647
Truth table0%0/3
A or B
A= $msg_engine_boots < 0B= $msg_engine_boots > 2147483647
ABresult
1X1
011
000
388 0 return $this->_error(
389 'The msgAuthoritativeEngineBoots value %d is out of range ' .
390 '(0..2147483647)', $msg_engine_boots
391 );
392 }
393  
394 # msgAuthoritativeEngineTime::=INTEGER (0..2147483647)
395 0 my $msg_engine_time;
396 0 ▶ if (!defined ($msg_engine_time = $msg->process(INTEGER))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined($msg_engine_time = $msg->process(INTEGER()))) 0 0 0
397 0 return $this->_error($msg->error());
398 }
399 0 ▶ if (($msg_engine_time < 0) || ($msg_engine_time > 2147483647)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg_engine_time < 0 or $msg_engine_time > 2147483647) 0 0 0
Condition0%0/3
$msg_engine_time < 0 or $msg_engine_time > 2147483647
l!l&&r!l&&!r
000
MC/DC0%0/2
$msg_engine_time < 0 or $msg_engine_time > 2147483647
$msg_engine_time < 0 $msg_engine_time > 2147483647
Truth table0%0/3
A or B
A= $msg_engine_time < 0B= $msg_engine_time > 2147483647
ABresult
1X1
011
000
400 0 return $this->_error(
401 'The msgAuthoritativeEngineTime value %d is out of range ' .
402 '(0..2147483647)', $msg_engine_time
403 );
404 }
405  
406 # msgUserName::=OCTET STRING (SIZE(0..32))
407 0 ▶ if (!defined $msg->security_name($msg->process(OCTET_STRING))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $msg->security_name($msg->process(OCTET_STRING()))) 0 0 0
408 0 return $this->_error($msg->error());
409 }
410  
411 # msgAuthenticationParameters::=OCTET STRING
412 0 my $auth_params;
413 0 ▶ if (!defined ($auth_params = $msg->process(OCTET_STRING))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined($auth_params = $msg->process(OCTET_STRING()))) 0 0 0
414 0 return $this->_error($msg->error());
415 }
416  
417 # We need to zero out the msgAuthenticationParameters in order
418 # to compute the HMAC properly.
419  
420 0 ▶ if (my $len = length $auth_params) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $len = length $auth_params) 0 0 0
421 0 ▶ if ($len != 12) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len != 12) 0 0 0
422 0 return $this->_error(
423 'The msgAuthenticationParameters length of %d is invalid', $len
424 );
425 }
426 0 substr ${$msg->reference}, ($msg->index() - 12), 12, pack 'x12';
427 }
428  
429 # msgPrivacyParameters::=OCTET STRING
430 0 my $priv_params;
431 0 ▶ if (!defined ($priv_params = $msg->process(OCTET_STRING))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined($priv_params = $msg->process(OCTET_STRING()))) 0 0 0
432 0 return $this->_error($msg->error());
433 }
434  
435 # Validate the msgAuthoritativeEngineID and msgUserName
436  
437 0 ▶ if ($this->{_discovered}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{'_discovered'}) { } 0 0 0
438  
439 0 ▶ if ($msg_engine_id ne $this->_engine_id()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg_engine_id ne $this->_engine_id) 0 0 0
440 0 return $this->_error(
441 'The msgAuthoritativeEngineID "%s" was expected, but "%s" was ' .
442 'found', unpack('H*', $this->_engine_id()),
443 unpack 'H*', $msg_engine_id
444 );
445 }
446  
447 0 ▶ if ($msg->security_name() ne $this->_user_name()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg->security_name ne $this->_user_name) 0 0 0
448 0 return $this->_error(
449 'The msgUserName "%s" was expected, but "%s" was found',
450 $this->_user_name(), $msg->security_name()
451 );
452 }
453  
454 } else {
455  
456 # Handle authoritativeEngineID discovery
457 0 ▶ if (!defined $this->_engine_id_discovery($msg_engine_id)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $this->_engine_id_discovery($msg_engine_id)) 0 0 0
458 0 return $this->_error();
459 }
460  
461 }
462  
463 # Validate the incoming securityLevel
464  
465 0 my $security_level = $msg->security_level();
466  
467 0 ▶ if ($security_level > $this->{_security_level}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($security_level > $this->{"_security_level"}) 0 0 0
468 return $this->_error(
469 'The message securityLevel %d is greater than the configured ' .
470 'value %d', $security_level, $this->{_security_level}
471 0 );
472 }
473  
474 0 ▶ if ($security_level > SECURITY_LEVEL_NOAUTHNOPRIV) {
Branch: 1 branch
BranchTrueFalseTotal
if ($security_level > SECURITY_LEVEL_NOAUTHNOPRIV()) 0 0 0
475  
476 # Authenticate the message
477 0 ▶ if (!defined $this->_authenticate_incoming_msg($msg, $auth_params)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $this->_authenticate_incoming_msg($msg, $auth_params)) 0 0 0
478 0 return $this->_error();
479 }
480  
481 # Synchronize the time
482 0 ▶ if (!$this->_synchronize($msg_engine_boots, $msg_engine_time)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($this->_synchronize($msg_engine_boots, $msg_engine_time)) 0 0 0
483 0 return $this->_error();
484 }
485  
486 # Check for timeliness
487 0 ▶ if (!defined $this->_timeliness($msg_engine_boots, $msg_engine_time)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $this->_timeliness($msg_engine_boots, $msg_engine_time)) 0 0 0
488 0 return $this->_error();
489 }
490  
491 0 ▶ if ($security_level > SECURITY_LEVEL_AUTHNOPRIV) {
Branch: 1 branch
BranchTrueFalseTotal
if ($security_level > SECURITY_LEVEL_AUTHNOPRIV()) 0 0 0
492  
493 # Validate the msgPrivacyParameters length.
494  
495 0 ▶ if (length($priv_params) != 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $priv_params != 8) 0 0 0
496 0 return $this->_error(
497 'The msgPrivacyParameters length of %d is invalid',
498 length $priv_params
499 );
500 }
501  
502 # AES in the USM Section 3.1.2.1 - "The 128-bit IV is
503 # obtained as the concatenation of the... ...snmpEngineBoots,
504 # ...snmpEngineTime, and a local 64-bit integer. ...The
505 # 64-bit integer must be placed in the msgPrivacyParameters
506 # field..." We must prepend the snmpEngineBoots and
507 # snmpEngineTime as received in order to compute the IV.
508  
509 0 ▶ if (($this->{_priv_protocol} eq PRIV_PROTOCOL_AESCFB128) ||
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_priv_protocol"} eq PRIV_PROTOCOL_AESCFB128() or $this->{"_priv_protocol"} eq PRIV_PROTOCOL_DRAFT_AESCFB192() or $this->{"_priv_protocol"} eq PRIV_PROTOCOL_DRAFT_AESCFB256()) 0 0 0
Condition0%0/6
$this->{"_priv_protocol"} eq PRIV_PROTOCOL_AESCFB128() or $this->{"_priv_protocol"} eq PRIV_PROTOCOL_DRAFT_AESCFB192() or $this->{"_priv_protocol"} eq PRIV_PROTOCOL_DRAFT_AESCFB256()
l!l&&r!l&&!r
000
$this->{"_priv_protocol"} eq PRIV_PROTOCOL_AESCFB128() or $this->{"_priv_protocol"} eq PRIV_PROTOCOL_DRAFT_AESCFB192()
l!l&&r!l&&!r
000
MC/DC0%0/3
$this->{"_priv_protocol"} eq PRIV_PROTOCOL_AESCFB128() or $this->{"_priv_protocol"} eq PRIV_PROTOCOL_DRAFT_AESCFB192() or $this->{"_priv_protocol"} eq PRIV_PROTOCOL_DRAFT_AESCFB256()
$this->{"_priv_protocol"} eq PRIV_PROTOCOL_AESCFB128() $this->{"_priv_protocol"} eq PRIV_PROTOCOL_DRAFT_AESCFB192() $this->{"_priv_protocol"} eq PRIV_PROTOCOL_DRAFT_AESCFB256()
Truth table0%0/4
A or B or C
A= $this->{"_priv_protocol"} eq PRIV_PROTOCOL_AESCFB128()B= $this->{"_priv_protocol"} eq PRIV_PROTOCOL_DRAFT_AESCFB192()C= $this->{"_priv_protocol"} eq PRIV_PROTOCOL_DRAFT_AESCFB256()
ABCresult
1XX1
01X1
0011
0000
510 ($this->{_priv_protocol} eq PRIV_PROTOCOL_DRAFT_AESCFB192) ||
511 ($this->{_priv_protocol} eq PRIV_PROTOCOL_DRAFT_AESCFB256))
512 {
513 0 substr $priv_params, 0, 0, pack 'NN', $msg_engine_boots,
514 $msg_engine_time;
515 }
516  
517 # encryptedPDU::=OCTET STRING
518  
519 0 return $this->_decrypt_data($msg,
520 $priv_params,
521 $msg->process(OCTET_STRING));
522  
523 }
524  
525 }
526  
527 0 return TRUE;
528 }
529  
530 sub user_name
531 0 ▶{
Subroutine user_name: not called CC=1 SCAR=6.9
Pod undocumented
532 0 return $_[0]->{_user_name};
533 }
534  
535 sub auth_protocol
536 0 ▶{
Subroutine auth_protocol: not called CC=2 SCAR=17.9
Pod undocumented
537 0 my ($this) = @_;
538  
539 0 ▶ if ($this->{_security_level} > SECURITY_LEVEL_NOAUTHNOPRIV) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_security_level"} > SECURITY_LEVEL_NOAUTHNOPRIV()) 0 0 0
540 0 return $this->{_auth_protocol};
541 }
542  
543 0 return AUTH_PROTOCOL_NONE;
544 }
545  
546 sub auth_key
547 2 ▶{
Subroutine auth_key: called CC=1 SCAR=0.0
Pod undocumented
548 2 return $_[0]->{_auth_key};
549 }
550  
551 sub priv_protocol
552 0 ▶{
Subroutine priv_protocol: not called CC=2 SCAR=17.9
Pod undocumented
553 0 my ($this) = @_;
554  
555 0 ▶ if ($this->{_security_level} > SECURITY_LEVEL_AUTHNOPRIV) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_security_level"} > SECURITY_LEVEL_AUTHNOPRIV()) 0 0 0
556 0 return $this->{_priv_protocol};
557 }
558  
559 0 return PRIV_PROTOCOL_NONE;
560 }
561  
562 sub priv_key
563 2 ▶{
Subroutine priv_key: called CC=1 SCAR=0.0
Pod undocumented
564 2 return $_[0]->{_priv_key};
565 }
566  
567 sub engine_id
568 0 ▶{
Subroutine engine_id: not called CC=1 SCAR=6.9
Pod undocumented
569 0 return $_[0]->{_engine_id};
570 }
571  
572 sub engine_boots
573 0 ▶{
Subroutine engine_boots: not called CC=1 SCAR=6.9
Pod undocumented
574 0 goto _engine_boots;
575 }
576  
577 sub engine_time
578 0 ▶{
Subroutine engine_time: not called CC=1 SCAR=6.9
Pod undocumented
579 0 goto &_engine_time;
580 }
581  
582 sub security_level
583 0 ▶{
Subroutine security_level: not called CC=1 SCAR=6.9
Pod undocumented
584 0 return $_[0]->{_security_level};
585 }
586  
587 sub security_model
588 0 ▶{
Subroutine security_model: not called CC=1 SCAR=6.9
Pod undocumented
589 # RFC 3411 - SnmpSecurityModel::=TEXTUAL-CONVENTION
590  
591 0 return SECURITY_MODEL_USM;
592 }
593  
594 sub security_name
595 0 ▶{
Subroutine security_name: not called CC=1 SCAR=6.9
Pod undocumented
596 0 goto &_user_name;
597 }
598  
599 sub discovered
600 0 ▶{
Subroutine discovered: not called CC=3 SCAR=24.8
Pod undocumented
601 0 my ($this) = @_;
602  
603 0 ▶ if ($this->{_security_level} > SECURITY_LEVEL_NOAUTHNOPRIV) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_security_level"} > SECURITY_LEVEL_NOAUTHNOPRIV()) 0 0 0
604 0 ▶ return ($this->{_discovered} && $this->{_synchronized});
Condition0%0/3
$this->{'_discovered'} && $this->{'_synchronized'}
!ll&&!rl&&r
000
MC/DC0%0/2
$this->{'_discovered'} && $this->{'_synchronized'}
$this->{'_discovered'} $this->{'_synchronized'}
Truth table0%0/3
A && B
A= $this->{'_discovered'}B= $this->{'_synchronized'}
ABresult
0X0
100
111
605 }
606  
607 0 return $this->{_discovered};
608 }
609  
610 # [private methods] ----------------------------------------------------------
611  
612 sub _version
613 0 ▶{
Subroutine _version: not called CC=2 SCAR=17.9
614 0 my ($this, $version) = @_;
615  
616 0 ▶ if ($version != SNMP_VERSION_3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version != SNMP_VERSION_3()) 0 0 0
617 0 return $this->_error('The SNMP version %s is not supported', $version);
618 }
619  
620 0 return $this->{_version} = $version;
621 }
622  
623 sub _engine_id
624 0 ▶{
Subroutine _engine_id: not called CC=6 SCAR=37.4
625 0 my ($this, $engine_id) = @_;
626  
627 0 ▶ if (@_ < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ < 2) 0 0 0
628 0 return $this->{_engine_id};
629 }
630  
631 0 ▶ if ($engine_id =~ m/^(?:0x)?([A-F0-9]+)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($engine_id =~ /^(?:0x)?([A-F0-9]+)$/i) { } 0 0 0
632 0 ▶ my $eid = pack 'H*', length($1) % 2 ? '0'.$1 : $1;
Branch: 1 branch
BranchTrueFalseTotal
length($1) % 2 ? : 0 0 0
633 0 my $len = length $eid;
634 0 ▶ if ($len < 5 || $len > 32) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len < 5 or $len > 32) 0 0 0
Condition0%0/3
$len < 5 or $len > 32
l!l&&r!l&&!r
000
MC/DC0%0/2
$len < 5 or $len > 32
$len < 5 $len > 32
Truth table0%0/3
A or B
A= $len < 5B= $len > 32
ABresult
1X1
011
000
635 0 return $this->_error(
636 'The authoritativeEngineID length of %d is out of range (5..32)',
637 $len
638 );
639 }
640 0 $this->{_engine_id} = $eid;
641 } else {
642 0 return $this->_error(
643 'The authoritativeEngineID "%s" is expected in hexadecimal format',
644 $engine_id
645 );
646 }
647  
648 0 return $this->{_engine_id};
649 }
650  
651 sub _user_name
652 2 ▶{
Subroutine _user_name: called CC=5 SCAR=18.9
653 2 my ($this, $user_name) = @_;
654  
655 2 ▶ if (@_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) 2 0 2
656 2 ▶ if ($user_name eq q{}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($user_name eq '') { } 0 2 2
elsif (length $user_name > 32) { } 0 2 2
657 0 return $this->_error('An empty userName was specified');
658 } elsif (length($user_name) > 32) {
659 0 return $this->_error(
660 'The userName length of %d is out of range (1..32)',
661 length $user_name
662 );
663 }
664 2 $this->{_user_name} = $user_name;
665 }
666  
667 # RFC 3414 Section 4 - "Discovery... ...msgUserName of zero-length..."
668  
669 2 ▶ return ($this->{_discovered}) ? $this->{_user_name} : q{};
Branch: 1 branch
BranchTrueFalseTotal
$this->{'_discovered'} ? : 0 2 2
670 }
671  
672 sub _snmp_engine_init
673 0 ▶{
Subroutine _snmp_engine_init: not called CC=4 SCAR=30.0
674 0 my ($this) = @_;
675  
676 0 ▶ if ($this->{_engine_id} eq q{}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_engine_id"} eq "") 0 0 0
677  
678 # Initialize our snmpEngineID using the algorithm described
679 # in RFC 3411 - SnmpEngineID::=TEXTUAL-CONVENTION.
680  
681 # The first bit is set to one to indicate that the RFC 3411
682 # algorithm is being used. The first fours bytes are to be
683 # the agent's SNMP management private enterprise number, but
684 # they are set to all zeros. The fifth byte is set to one to
685 # indicate that the final four bytes are an IPv4 address.
686  
687 0 ▶ if (!defined $ENGINE_ID) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $Net::SNMP::Security::USM::ENGINE_ID) 0 0 0
688 0 $ENGINE_ID = eval {
689 0 require Sys::Hostname;
690 0 pack('H10', '8000000001') . gethostbyname Sys::Hostname::hostname();
691 };
692  
693 # Fallback in case gethostbyname() or hostname() fail
694 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
695 0 $ENGINE_ID = pack 'x11H2', '01';
696 }
697 }
698  
699 0 $this->{_engine_id} = $ENGINE_ID;
700 }
701  
702 0 $this->{_engine_boots} = 1;
703 0 $this->{_time_epoc} = $^T;
704 0 $this->{_synchronized} = TRUE;
705 0 $this->{_discovered} = TRUE;
706  
707 0 return TRUE;
708 }
709  
710 sub _auth_key
711 0 ▶{
Subroutine _auth_key: not called CC=5 SCAR=34.0
712 0 my ($this, $auth_key) = @_;
713  
714 0 ▶ if (@_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) 0 0 0
715 0 ▶ if ($auth_key =~ m/^(?:0x)?([A-F0-9]+)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($auth_key =~ /^(?:0x)?([A-F0-9]+)$/i) { } 0 0 0
716 0 ▶ $this->{_auth_key} = pack 'H*', length($1) % 2 ? '0'.$1 : $1;
Branch: 1 branch
BranchTrueFalseTotal
length($1) % 2 ? : 0 0 0
717 0 ▶ if (!defined $this->_auth_key_validate()) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $this->_auth_key_validate) 0 0 0
718 0 return $this->_error();
719 }
720 } else {
721 0 return $this->_error(
722 'The authKey "%s" is expected in hexadecimal format', $auth_key
723 );
724 }
725 }
726  
727 0 return $this->{_auth_key};
728 }
729  
730 sub _auth_password
731 2 ▶{
Subroutine _auth_password: called CC=3 SCAR=11.8
732 2 my ($this, $auth_password) = @_;
733  
734 2 ▶ if (@_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) 2 0 2
735 2 ▶ if ($auth_password eq q{}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($auth_password eq "") 0 2 2
736 0 return $this->_error('An empty authentication password was specified');
737 }
738 2 $this->{_auth_password} = $auth_password;
739 }
740  
741 2 return $this->{_auth_password};
742 }
743  
744 {
745 1 my $protocols = {
746 '(?:hmac-)?md5(?:-96)?', AUTH_PROTOCOL_HMACMD5,
747 quotemeta AUTH_PROTOCOL_HMACMD5, AUTH_PROTOCOL_HMACMD5,
748 '(?:hmac-)?sha(?:-?1|-96)?', AUTH_PROTOCOL_HMACSHA,
749 quotemeta AUTH_PROTOCOL_HMACSHA, AUTH_PROTOCOL_HMACSHA,
750 };
751  
752 sub _auth_protocol
753 1 ▶ {
Subroutine _auth_protocol: called CC=5 SCAR=17.5
754 1 my ($this, $proto) = @_;
755  
756 1 ▶ if (@_ < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ < 2) 0 1 1
757 0 return $this->{_auth_protocol};
758 }
759  
760 1 ▶ if ($proto eq q{}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proto eq "") 0 1 1
761 0 return $this->_error('An empty authProtocol was specified');
762 }
763  
764 1 for (keys %{$protocols}) {
765 2 ▶ if ($proto =~ /^$_$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proto =~ /^$_$/i) 1 1 2
766 1 return $this->{_auth_protocol} = $protocols->{$_};
767 }
768 }
769  
770 0 return $this->_error('The authProtocol "%s" is unknown', $proto);
771 }
772  
773 }
774  
775 sub _priv_key
776 1 ▶{
Subroutine _priv_key: not called CC=5 SCAR=32.5
777 0 my ($this, $priv_key) = @_;
778  
779 0 ▶ if (@_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) 0 0 0
780 0 ▶ if ($priv_key =~ m/^(?:0x)?([A-F0-9]+)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($priv_key =~ /^(?:0x)?([A-F0-9]+)$/i) { } 0 0 0
781 0 ▶ $this->{_priv_key} = pack 'H*', length($1) % 2 ? '0'.$1 : $1;
Branch: 1 branch
BranchTrueFalseTotal
length($1) % 2 ? : 0 0 0
782 0 ▶ if (!defined $this->_priv_key_validate()) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $this->_priv_key_validate) 0 0 0
783 0 return $this->_error();
784 }
785 } else {
786 0 return $this->_error(
787 'The privKey "%s" is expected in hexadecimal format', $priv_key
788 );
789 }
790 }
791  
792 0 return $this->{_priv_key};
793 }
794  
795 sub _priv_password
796 2 ▶{
Subroutine _priv_password: called CC=3 SCAR=11.8
797 2 my ($this, $priv_password) = @_;
798  
799 2 ▶ if (@_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) 2 0 2
800 2 ▶ if ($priv_password eq q{}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($priv_password eq "") 0 2 2
801 0 return $this->_error('An empty privacy password was specified');
802 }
803 2 $this->{_priv_password} = $priv_password;
804 }
805  
806 2 return $this->{_priv_password};
807 }
808  
809 {
810 1 my $protocols = {
811 '(?:cbc-)?des', PRIV_PROTOCOL_DES,
812 quotemeta PRIV_PROTOCOL_DES, PRIV_PROTOCOL_DES,
813 '(?:cbc-)?(?:3|triple-)des(?:-?ede)?', PRIV_PROTOCOL_DRAFT_3DESEDE,
814 quotemeta PRIV_PROTOCOL_DRAFT_3DESEDE, PRIV_PROTOCOL_DRAFT_3DESEDE,
815 '(?:(?:cfb)?128-?)?aes(?:-?128)?', PRIV_PROTOCOL_AESCFB128,
816 quotemeta PRIV_PROTOCOL_AESCFB128, PRIV_PROTOCOL_AESCFB128,
817 quotemeta PRIV_PROTOCOL_DRAFT_AESCFB128, PRIV_PROTOCOL_AESCFB128,
818 '(?:(?:cfb)?192-?)aes(?:-?128)?', PRIV_PROTOCOL_DRAFT_AESCFB192,
819 quotemeta PRIV_PROTOCOL_DRAFT_AESCFB192, PRIV_PROTOCOL_DRAFT_AESCFB192,
820 '(?:(?:cfb)?256-?)aes(?:-?128)?', PRIV_PROTOCOL_DRAFT_AESCFB256,
821 quotemeta PRIV_PROTOCOL_DRAFT_AESCFB256, PRIV_PROTOCOL_DRAFT_AESCFB256,
822 };
823  
824 sub _priv_protocol
825 2 ▶ {
Subroutine _priv_protocol: called CC=10 SCAR=28.8
826 2 my ($this, $proto) = @_;
827  
828 2 ▶ if (@_ < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ < 2) 0 2 2
829 0 return $this->{_priv_protocol};
830 }
831  
832 2 ▶ if ($proto eq q{}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proto eq "") 0 2 2
833 0 return $this->_error('An empty privProtocol was specified');
834 }
835  
836 2 my $priv_proto;
837  
838 2 for (keys %{$protocols}) {
839 20 ▶ if ($proto =~ /^$_$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proto =~ /^$_$/i) 2 18 20
840 2 $priv_proto = $protocols->{$_};
841 2 last;
842 }
843 }
844  
845 2 ▶ if (!defined $priv_proto) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $priv_proto) 0 2 2
846 0 return $this->_error('The privProtocol "%s" is unknown', $proto);
847 }
848  
849 # Validate the support of the AES cipher algorithm. Attempt to
850 # load the Crypt::Rijndael module. If this module is not found,
851 # do not provide support for the AES Cipher Algorithm.
852  
853 2 ▶ if (($priv_proto eq PRIV_PROTOCOL_AESCFB128) ||
Branch: 1 branch
BranchTrueFalseTotal
if ($priv_proto eq PRIV_PROTOCOL_AESCFB128() or $priv_proto eq PRIV_PROTOCOL_DRAFT_AESCFB192() or $priv_proto eq PRIV_PROTOCOL_DRAFT_AESCFB256()) 0 2 2
Condition33%2/6
$priv_proto eq PRIV_PROTOCOL_AESCFB128() or $priv_proto eq PRIV_PROTOCOL_DRAFT_AESCFB192() or $priv_proto eq PRIV_PROTOCOL_DRAFT_AESCFB256()
l!l&&r!l&&!r
002
$priv_proto eq PRIV_PROTOCOL_AESCFB128() or $priv_proto eq PRIV_PROTOCOL_DRAFT_AESCFB192()
l!l&&r!l&&!r
002
MC/DC0%0/3
$priv_proto eq PRIV_PROTOCOL_AESCFB128() or $priv_proto eq PRIV_PROTOCOL_DRAFT_AESCFB192() or $priv_proto eq PRIV_PROTOCOL_DRAFT_AESCFB256()
$priv_proto eq PRIV_PROTOCOL_AESCFB128() $priv_proto eq PRIV_PROTOCOL_DRAFT_AESCFB192() $priv_proto eq PRIV_PROTOCOL_DRAFT_AESCFB256()
Truth table25%1/4
A or B or C
A= $priv_proto eq PRIV_PROTOCOL_AESCFB128()B= $priv_proto eq PRIV_PROTOCOL_DRAFT_AESCFB192()C= $priv_proto eq PRIV_PROTOCOL_DRAFT_AESCFB256()
ABCresult
1XX1
01X1
0011
0000
854 ($priv_proto eq PRIV_PROTOCOL_DRAFT_AESCFB192) ||
855 ($priv_proto eq PRIV_PROTOCOL_DRAFT_AESCFB256))
856 {
857 0 ▶ if (defined (my $error = load_module('Crypt::Rijndael'))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $error = load_module("Crypt::Rijndael"))) 0 0 0
858 0 return $this->_error(
859 'Support for privProtocol "%s" is unavailable %s', $proto, $error
860 );
861 }
862 }
863  
864 2 return $this->{_priv_protocol} = $priv_proto;
865 }
866  
867 }
868  
869 sub _engine_boots
870 1 ▶{
Subroutine _engine_boots: called CC=2 SCAR=7.2
871 2 ▶ return ($_[0]->{_synchronized}) ? $_[0]->{_engine_boots} : 0;
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'_synchronized'} ? : 0 2 2
872 }
873  
874 sub _engine_time
875 0 ▶{
Subroutine _engine_time: not called CC=6 SCAR=37.4
876 0 my ($this) = @_;
877  
878 0 ▶ return 0 if (!$this->{_synchronized});
Branch: 1 branch
BranchTrueFalseTotal
unless $this->{"_synchronized"} 0 0 0
879  
880 0 $this->{_engine_time} = time() - $this->{_time_epoc};
881  
882 0 ▶ if ($this->{_engine_time} > 2147483647) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_engine_time"} > 2147483647) 0 0 0
883 0 DEBUG_INFO('snmpEngineTime rollover');
884 0 ▶ if (++$this->{_engine_boots} == 2147483647) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$this->{"_engine_boots"} == 2147483647) 0 0 0
885 0 die 'FATAL: Unable to handle snmpEngineBoots value';
886 }
887 0 $this->{_engine_time} -= 2147483647;
888 0 $this->{_time_epoc} = time() - $this->{_engine_time};
889 0 ▶ if (!$this->{_authoritative}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($this->{"_authoritative"}) 0 0 0
890 0 $this->{_synchronized} = FALSE;
891 0 return $this->{_latest_engine_time} = 0;
892 }
893 }
894  
895 0 ▶ if ($this->{_engine_time} < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_engine_time"} < 0) 0 0 0
896 0 die 'FATAL: Unable to handle negative snmpEngineTime value';
897 }
898  
899 0 return $this->{_engine_time};
900 }
901  
902 sub _security_params
903 6 ▶{
Subroutine _security_params: called CC=22 SCAR=36.5
904 6 my ($this) = @_;
905  
906 # Clear any previous error messages
907 6 $this->_error_clear();
908  
909 # We must have an usmUserName
910 6 ▶ if ($this->{_user_name} eq q{}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_user_name"} eq "") 0 6 6
911 0 return $this->_error('The required userName was not specified');
912 }
913  
914 # Define the authentication parameters
915  
916 6 ▶ if ((defined $this->{_auth_password}) && ($this->{_discovered})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $this->{"_auth_password"} and $this->{"_discovered"}) 2 4 6
Condition100%3/3
defined $this->{"_auth_password"} and $this->{"_discovered"}
!ll&&!rl&&r
222
MC/DC100%2/2
defined $this->{"_auth_password"} and $this->{"_discovered"}
defined $this->{"_auth_password"} $this->{"_discovered"}
Truth table100%3/3
A and B
A= defined $this->{"_auth_password"}B= $this->{"_discovered"}
ABresult
0X0
100
111
917 2 ▶ if (!defined $this->{_auth_key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $this->{"_auth_key"}) 2 0 2
918 2 ▶ return $this->_error() if !defined $this->_auth_key_generate();
Branch: 1 branch
BranchTrueFalseTotal
unless defined $this->_auth_key_generate 0 2 2
919 }
920 2 $this->{_auth_password} = undef;
921 }
922  
923 6 ▶ if (defined $this->{_auth_key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $this->{"_auth_key"}) 4 2 6
924  
925 # Validate the key based on the protocol
926 4 ▶ if (!defined $this->_auth_key_validate()) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $this->_auth_key_validate) 0 4 4
927 0 return $this->_error('The authKey is invalid');
928 }
929  
930 # Initialize the authentication data
931 4 ▶ if (!defined $this->_auth_data_init()) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $this->_auth_data_init) 0 4 4
932 0 return $this->_error('Failed to initialize the authentication data');
933 }
934  
935 4 ▶ if ($this->{_discovered}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_discovered"}) 4 0 4
936 4 $this->{_security_level} = SECURITY_LEVEL_AUTHNOPRIV;
937 }
938  
939 }
940  
941 # You must have authentication to have privacy
942  
943 6 ▶ if (!defined ($this->{_auth_key}) && !defined $this->{_auth_password}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $this->{"_auth_key"} or defined $this->{"_auth_password"}) 0 6 6
Condition66%2/3
defined $this->{"_auth_key"} or defined $this->{"_auth_password"}
l!l&&r!l&&!r
420
MC/DC0%0/2
defined $this->{"_auth_key"} or defined $this->{"_auth_password"}
defined $this->{"_auth_key"} defined $this->{"_auth_password"}
Truth table66%2/3
A or B
A= defined $this->{"_auth_key"}B= defined $this->{"_auth_password"}
ABresult
1X1
011
000
944 0 ▶ if (defined ($this->{_priv_key}) || defined $this->{_priv_password}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $this->{"_priv_key"} or defined $this->{"_priv_password"}) 0 0 0
Condition0%0/3
defined $this->{"_priv_key"} or defined $this->{"_priv_password"}
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $this->{"_priv_key"} or defined $this->{"_priv_password"}
defined $this->{"_priv_key"} defined $this->{"_priv_password"}
Truth table0%0/3
A or B
A= defined $this->{"_priv_key"}B= defined $this->{"_priv_password"}
ABresult
1X1
011
000
945 0 return $this->_error(
946 'The securityLevel is unsupported (privacy requires authentication)'
947 );
948 }
949 }
950  
951 # Define the privacy parameters
952  
953 6 ▶ if ((defined $this->{_priv_password}) && ($this->{_discovered})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $this->{"_priv_password"} and $this->{"_discovered"}) 2 4 6
Condition100%3/3
defined $this->{"_priv_password"} and $this->{"_discovered"}
!ll&&!rl&&r
222
MC/DC100%2/2
defined $this->{"_priv_password"} and $this->{"_discovered"}
defined $this->{"_priv_password"} $this->{"_discovered"}
Truth table100%3/3
A and B
A= defined $this->{"_priv_password"}B= $this->{"_discovered"}
ABresult
0X0
100
111
954 2 ▶ if (!defined $this->{_priv_key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $this->{"_priv_key"}) 2 0 2
955 2 ▶ return $this->_error() if !defined $this->_priv_key_generate();
Branch: 1 branch
BranchTrueFalseTotal
unless defined $this->_priv_key_generate 0 2 2
956 }
957 2 $this->{_priv_password} = undef;
958 }
959  
960 6 ▶ if (defined $this->{_priv_key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $this->{"_priv_key"}) 4 2 6
961  
962 # Validate the key based on the protocol
963 4 ▶ if (!defined $this->_priv_key_validate()) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $this->_priv_key_validate) 0 4 4
964 0 return $this->_error('The privKey is invalid');
965 }
966  
967 # Initialize the privacy data
968 4 ▶ if (!defined $this->_priv_data_init()) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $this->_priv_data_init) 0 4 4
969 0 return $this->_error('Failed to initialize the privacy data');
970 }
971  
972 4 ▶ if ($this->{_discovered}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_discovered"}) 4 0 4
973 4 $this->{_security_level} = SECURITY_LEVEL_AUTHPRIV;
974 }
975  
976 }
977  
978 6 DEBUG_INFO('securityLevel = %d', $this->{_security_level});
979  
980 6 return $this->{_security_level};
981 }
982  
983 sub _engine_id_discovery
984 2 ▶{
Subroutine _engine_id_discovery: called CC=6 SCAR=21.4
985 2 my ($this, $engine_id) = @_;
986  
987 2 ▶ return TRUE if ($this->{_authoritative});
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"_authoritative"} 0 2 2
988  
989 2 ▶ DEBUG_INFO('engineID = 0x%s', unpack 'H*', $engine_id || q{});
Condition50%1/2
$engine_id || ''
l!l
20
MC/DC0%0/1
$engine_id || ''
$engine_id
Truth table50%1/2
A
A= $engine_id
Aresult
11
00
990  
991 2 ▶ if (length($engine_id) < 5 || length($engine_id) > 32) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $engine_id < 5 or length $engine_id > 32) 0 2 2
Condition33%1/3
length $engine_id < 5 or length $engine_id > 32
l!l&&r!l&&!r
002
MC/DC0%0/2
length $engine_id < 5 or length $engine_id > 32
length $engine_id < 5 length $engine_id > 32
Truth table33%1/3
A or B
A= length $engine_id < 5B= length $engine_id > 32
ABresult
1X1
011
000
992 0 return $this->_error(
993 'The msgAuthoritativeEngineID length of %d is out of range (5..32)',
994 length $engine_id
995 );
996 }
997  
998 2 $this->{_engine_id} = $engine_id;
999 2 $this->{_discovered} = TRUE;
1000  
1001 2 ▶ if (!defined $this->_security_params()) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $this->_security_params) 0 2 2
1002 0 $this->{_discovered} = FALSE;
1003 0 return $this->_error();
1004 }
1005  
1006 2 return TRUE;
1007 }
1008  
1009 sub _synchronize
1010 2 ▶{
Subroutine _synchronize: called CC=8 SCAR=25.4
1011 2 my ($this, $msg_boots, $msg_time) = @_;
1012  
1013 2 ▶ return TRUE if ($this->{_authoritative});
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"_authoritative"} 0 2 2
1014 2 ▶ return TRUE if ($this->{_security_level} < SECURITY_LEVEL_AUTHNOPRIV);
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"_security_level"} < SECURITY_LEVEL_AUTHNOPRIV() 0 2 2
1015  
1016 2 ▶ if (($msg_boots > $this->_engine_boots()) ||
Branch: 1 branch
BranchTrueFalseTotal
if ($msg_boots > $this->_engine_boots or $msg_boots == $this->_engine_boots and $msg_time > $this->{"_latest_engine_time"}) 2 0 2
Condition16%1/6
$msg_boots > $this->_engine_boots or $msg_boots == $this->_engine_boots and $msg_time > $this->{"_latest_engine_time"}
l!l&&r!l&&!r
200
$msg_boots == $this->_engine_boots and $msg_time > $this->{"_latest_engine_time"}
!ll&&!rl&&r
000
MC/DC0%0/3
$msg_boots > $this->_engine_boots or $msg_boots == $this->_engine_boots and $msg_time > $this->{"_latest_engine_time"}
$msg_boots > $this->_engine_boots $msg_boots == $this->_engine_boots $msg_time > $this->{"_latest_engine_time"}
Truth table25%1/4
A or B and C
A= $msg_boots > $this->_engine_bootsB= $msg_boots == $this->_engine_bootsC= $msg_time > $this->{"_latest_engine_time"}
ABCresult
1XX1
0111
00X0
0100
1017 (($msg_boots == $this->_engine_boots()) &&
1018 ($msg_time > $this->{_latest_engine_time})))
1019 {
1020 2 DEBUG_INFO(
1021 'update: engineBoots = %d, engineTime = %d', $msg_boots, $msg_time
1022 );
1023  
1024 2 $this->{_engine_boots} = $msg_boots;
1025 2 $this->{_latest_engine_time} = $this->{_engine_time} = $msg_time;
1026 2 $this->{_time_epoc} = time() - $this->{_engine_time};
1027  
1028 2 ▶ if (!$this->{_synchronized}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($this->{"_synchronized"}) 2 0 2
1029 2 $this->{_synchronized} = TRUE;
1030 2 ▶ if (!defined $this->_security_params()) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $this->_security_params) 0 2 2
1031 0 return ($this->{_synchronized} = FALSE);
1032 }
1033 }
1034  
1035 2 return TRUE;
1036 }
1037  
1038 DEBUG_INFO(
1039 'no update: engineBoots = %d, msgBoots = %d; ' .
1040 'latestTime = %d, msgTime = %d',
1041 $this->_engine_boots(), $msg_boots,
1042 0 $this->{_latest_engine_time}, $msg_time
1043 );
1044  
1045 0 return TRUE;
1046 }
1047  
1048 sub _timeliness
1049 0 ▶{
Subroutine _timeliness: not called CC=10 SCAR=47.0
1050 0 my ($this, $msg_boots, $msg_time) = @_;
1051  
1052 0 ▶ return TRUE if ($this->{_security_level} < SECURITY_LEVEL_AUTHNOPRIV);
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"_security_level"} < SECURITY_LEVEL_AUTHNOPRIV() 0 0 0
1053  
1054 # Retrieve a local copy of our snmpEngineBoots and snmpEngineTime
1055 # to avoid the possibilty of using different values in each of
1056 # the comparisons.
1057  
1058 0 my $engine_time = $this->_engine_time();
1059 0 my $engine_boots = $this->_engine_boots();
1060  
1061 0 ▶ if ($engine_boots == 2147483647) {
Branch: 1 branch
BranchTrueFalseTotal
if ($engine_boots == 2147483647) 0 0 0
1062 0 $this->{_synchronized} = FALSE;
1063 0 return $this->_error('The system is not in the time window');
1064 }
1065  
1066 0 ▶ if (!$this->{_authoritative}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $this->{'_authoritative'}) { } 0 0 0
1067  
1068 0 ▶ if ($msg_boots < $engine_boots) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg_boots < $engine_boots) 0 0 0
1069 0 return $this->_error('The message is not in the time window');
1070 }
1071 0 ▶ if (($msg_boots == $engine_boots) && ($msg_time < ($engine_time - 150))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg_boots == $engine_boots and $msg_time < $engine_time - 150) 0 0 0
Condition0%0/3
$msg_boots == $engine_boots and $msg_time < $engine_time - 150
!ll&&!rl&&r
000
MC/DC0%0/2
$msg_boots == $engine_boots and $msg_time < $engine_time - 150
$msg_boots == $engine_boots $msg_time < $engine_time - 150
Truth table0%0/3
A and B
A= $msg_boots == $engine_bootsB= $msg_time < $engine_time - 150
ABresult
0X0
100
111
1072 0 return $this->_error('The message is not in the time window');
1073 }
1074  
1075 } else {
1076  
1077 0 ▶ if ($msg_boots != $engine_boots) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg_boots != $engine_boots) 0 0 0
1078 0 return $this->_error('The message is not in the time window');
1079 }
1080 0 ▶ if (($msg_time < ($engine_time - 150)) ||
Branch: 1 branch
BranchTrueFalseTotal
if ($msg_time < $engine_time - 150 or $msg_time > $engine_time + 150) 0 0 0
Condition0%0/3
$msg_time < $engine_time - 150 or $msg_time > $engine_time + 150
l!l&&r!l&&!r
000
MC/DC0%0/2
$msg_time < $engine_time - 150 or $msg_time > $engine_time + 150
$msg_time < $engine_time - 150 $msg_time > $engine_time + 150
Truth table0%0/3
A or B
A= $msg_time < $engine_time - 150B= $msg_time > $engine_time + 150
ABresult
1X1
011
000
1081 ($msg_time > ($engine_time + 150)))
1082 {
1083 0 return $this->_error('The message is not in the time window');
1084 }
1085  
1086 }
1087  
1088 0 return TRUE;
1089 }
1090  
1091 sub _authenticate_outgoing_msg
1092 0 ▶{
Subroutine _authenticate_outgoing_msg: not called CC=2 SCAR=17.9
1093 0 my ($this, $msg, $auth_location) = @_;
1094  
1095 0 ▶ if (!$auth_location) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($auth_location) 0 0 0
1096 0 return $this->_error(
1097 'Authentication failure (Unable to set msgAuthenticationParameters)'
1098 );
1099 }
1100  
1101 # Set the msgAuthenticationParameters
1102 0 substr ${$msg->reference}, -$auth_location, 12, $this->_auth_hmac($msg);
1103  
1104 0 return TRUE;
1105 }
1106  
1107 sub _authenticate_incoming_msg
1108 0 ▶{
Subroutine _authenticate_incoming_msg: not called CC=2 SCAR=17.9
1109 0 my ($this, $msg, $auth_params) = @_;
1110  
1111 # Authenticate the message
1112 0 ▶ if ($auth_params ne $this->_auth_hmac($msg)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($auth_params ne $this->_auth_hmac($msg)) 0 0 0
1113 0 return $this->_error('Authentication failure');
1114 }
1115  
1116 0 DEBUG_INFO('authentication passed');
1117  
1118 0 return TRUE;
1119 }
1120  
1121 sub _auth_hmac
1122 4 ▶{
Subroutine _auth_hmac: called CC=3 SCAR=12.0
1123 4 my ($this, $msg) = @_;
1124  
1125 4 ▶ return q{} if (!defined($this->{_auth_data}) || !defined $msg);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $this->{"_auth_data"} and defined $msg 0 4 4
Condition33%1/3
defined $this->{"_auth_data"} and defined $msg
!ll&&!rl&&r
004
MC/DC0%0/2
defined $this->{"_auth_data"} and defined $msg
defined $this->{"_auth_data"} defined $msg
Truth table33%1/3
A and B
A= defined $this->{"_auth_data"}B= defined $msg
ABresult
0X0
100
111
1126  
1127 return substr
1128 4 $this->{_auth_data}->reset()->add(${$msg->reference()})->digest(), 0, 12;
1129 }
1130  
1131 sub _auth_data_init
1132 4 ▶{
Subroutine _auth_data_init: called CC=5 SCAR=16.7
1133 4 my ($this) = @_;
1134  
1135 4 ▶ if (!defined $this->{_auth_key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $this->{"_auth_key"}) 0 4 4
1136 0 return $this->_error('The required authKey is not defined');
1137 }
1138  
1139 4 ▶ return TRUE if defined $this->{_auth_data};
Branch: 1 branch
BranchTrueFalseTotal
if defined $this->{"_auth_data"} 2 2 4
1140  
1141 2 ▶ if ($this->{_auth_protocol} eq AUTH_PROTOCOL_HMACMD5) {
Branch: 2 branches
BranchTrueFalseTotal
if ($this->{'_auth_protocol'} eq AUTH_PROTOCOL_HMACMD5()) { } 1 1 2
elsif ($this->{'_auth_protocol'} eq AUTH_PROTOCOL_HMACSHA()) { } 1 0 1
1142  
1143 $this->{_auth_data} =
1144 1 Digest::HMAC->new($this->{_auth_key}, 'Digest::MD5');
1145  
1146 } elsif ($this->{_auth_protocol} eq AUTH_PROTOCOL_HMACSHA) {
1147  
1148 $this->{_auth_data} =
1149 1 Digest::HMAC->new($this->{_auth_key}, 'Digest::SHA1');
1150  
1151 } else {
1152  
1153 return $this->_error(
1154 'The authProtocol "%s" is unknown', $this->{_auth_protocol}
1155 0 );
1156  
1157 }
1158  
1159 2 return TRUE;
1160 }
1161  
1162 {
1163 1 my $encrypt =
1164 {
1165 PRIV_PROTOCOL_DES, \&_priv_encrypt_des,
1166 PRIV_PROTOCOL_DRAFT_3DESEDE, \&_priv_encrypt_3desede,
1167 PRIV_PROTOCOL_AESCFB128, \&_priv_encrypt_aescfbxxx,
1168 PRIV_PROTOCOL_DRAFT_AESCFB192, \&_priv_encrypt_aescfbxxx,
1169 PRIV_PROTOCOL_DRAFT_AESCFB256, \&_priv_encrypt_aescfbxxx
1170 };
1171  
1172 sub _encrypt_data
1173 2 ▶ {
Subroutine _encrypt_data: called CC=3 SCAR=12.7
1174 # my ($this, $msg, $priv_params, $plain) = @_;
1175  
1176 2 ▶ if (!exists $encrypt->{$_[0]->{_priv_protocol}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $$encrypt{$_[0]{"_priv_protocol"}}) 0 2 2
1177 0 return $_[0]->_error('Encryption error (Unknown protocol)');
1178 }
1179  
1180 2 ▶ if (!defined
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $_[1]->prepare(OCTET_STRING(), $_[0]->${\$$encrypt{$_[0]{"_priv_protocol"}};}($_[2], $_[3]))) 0 2 2
1181 $_[1]->prepare(
1182 OCTET_STRING,
1183 2 $_[0]->${\$encrypt->{$_[0]->{_priv_protocol}}}($_[2], $_[3])
1184 )
1185 )
1186 {
1187 0 return $_[0]->_error('Encryption error');
1188 }
1189  
1190 # Set the PDU buffer equal to the encryptedPDU
1191 2 return $_[3] = $_[1]->clear();
1192 }
1193 }
1194  
1195 {
1196 1 my $decrypt =
1197 {
1198 PRIV_PROTOCOL_DES, \&_priv_decrypt_des,
1199 PRIV_PROTOCOL_DRAFT_3DESEDE, \&_priv_decrypt_3desede,
1200 PRIV_PROTOCOL_AESCFB128, \&_priv_decrypt_aescfbxxx,
1201 PRIV_PROTOCOL_DRAFT_AESCFB192, \&_priv_decrypt_aescfbxxx,
1202 PRIV_PROTOCOL_DRAFT_AESCFB256, \&_priv_decrypt_aescfbxxx
1203 };
1204  
1205 sub _decrypt_data
1206 2 ▶ {
Subroutine _decrypt_data: called CC=8 SCAR=25.8
1207 # my ($this, $msg, $priv_params, $cipher) = @_;
1208  
1209 # Make sure there is data to decrypt.
1210 2 ▶ if (!defined $_[3]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $_[3]) 0 2 2
1211 0 ▶ return $_[0]->_error($_[1]->error() || 'Decryption error (No data)');
Condition0%0/2
$_[1]->error || 'Decryption error (No data)'
l!l
00
MC/DC0%0/1
$_[1]->error || 'Decryption error (No data)'
$_[1]->error
Truth table0%0/2
A
A= $_[1]->error
Aresult
11
00
1212 }
1213  
1214 2 ▶ if (!exists $decrypt->{$_[0]->{_priv_protocol}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $$decrypt{$_[0]{"_priv_protocol"}}) 0 2 2
1215 0 return $_[0]->_error('Decryption error (Unknown protocol)');
1216 }
1217  
1218 # Clear the Message buffer
1219 2 $_[1]->clear();
1220  
1221 # Put the decrypted data back into the Message buffer
1222 2 ▶ if (!defined
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $_[1]->prepend($_[0]->${\$$decrypt{$_[0]{"_priv_protocol"}};}($_[2], $_[3]))) 0 2 2
1223 $_[1]->prepend(
1224 2 $_[0]->${\$decrypt->{$_[0]->{_priv_protocol}}}($_[2], $_[3])
1225 )
1226 )
1227 {
1228 0 return $_[0]->_error($_[1]->error());
1229 }
1230 2 ▶ return $_[0]->_error($_[1]->error()) if (!$_[1]->length());
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1]->length 0 2 2
1231  
1232 # See if the decrypted data starts with a SEQUENCE
1233 # and has a reasonable length.
1234  
1235 2 my $msglen = $_[1]->process(SEQUENCE);
1236 2 ▶ if ((!defined $msglen) || ($msglen > $_[1]->length())) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $msglen or $msglen > $_[1]->length) 0 2 2
Condition33%1/3
not defined $msglen or $msglen > $_[1]->length
l!l&&r!l&&!r
002
MC/DC0%0/2
not defined $msglen or $msglen > $_[1]->length
not defined $msglen $msglen > $_[1]->length
Truth table33%1/3
not(A) or B
A= not defined $msglenB= $msglen > $_[1]->length
ABresult
1X1
011
000
1237 0 return $_[0]->_error('Decryption error');
1238 }
1239 2 $_[1]->index(0); # Reset the index
1240  
1241 2 DEBUG_INFO('privacy passed');
1242  
1243 2 return TRUE;
1244 }
1245 }
1246  
1247 sub _priv_data_init
1248 1 ▶{
Subroutine _priv_data_init: called CC=4 SCAR=14.5
1249 4 my ($this) = @_;
1250  
1251 4 ▶ if (!defined $this->{_priv_key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $this->{"_priv_key"}) 0 4 4
1252 0 return $this->_error('The required privKey is not defined');
1253 }
1254  
1255 4 ▶ return TRUE if defined $this->{_priv_data};
Branch: 1 branch
BranchTrueFalseTotal
if defined $this->{"_priv_data"} 2 2 4
1256  
1257 2 my $init =
1258 {
1259 PRIV_PROTOCOL_DES, \&_priv_data_init_des,
1260 PRIV_PROTOCOL_DRAFT_3DESEDE, \&_priv_data_init_3desede,
1261 PRIV_PROTOCOL_AESCFB128, \&_priv_data_init_aescfbxxx,
1262 PRIV_PROTOCOL_DRAFT_AESCFB192, \&_priv_data_init_aescfbxxx,
1263 PRIV_PROTOCOL_DRAFT_AESCFB256, \&_priv_data_init_aescfbxxx
1264 };
1265  
1266 2 ▶ if (!exists $init->{$this->{_priv_protocol}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $$init{$this->{"_priv_protocol"}}) 0 2 2
1267 return $this->_error(
1268 'The privProtocol "%s" is unknown', $this->{_priv_protocol}
1269 0 );
1270 }
1271  
1272 2 return $this->${\$init->{$this->{_priv_protocol}}}();
1273 }
1274  
1275 sub _priv_data_init_des
1276 2 ▶{
Subroutine _priv_data_init_des: called CC=2 SCAR=7.1
1277 2 my ($this) = @_;
1278  
1279 2 ▶ if (!defined $this->{_priv_key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $this->{"_priv_key"}) 0 2 2
1280 0 return $this->_error('The required privKey is not defined');
1281 }
1282  
1283 # Create the DES object
1284 $this->{_priv_data}->{des} =
1285 2 Crypt::DES->new(substr $this->{_priv_key}, 0, 8);
1286  
1287 # Extract the pre-IV
1288 2 $this->{_priv_data}->{pre_iv} = substr $this->{_priv_key}, 8, 8;
1289  
1290 # Initialize the salt
1291 2 $this->{_priv_data}->{salt} = int rand ~0;
1292  
1293 2 return TRUE;
1294 }
1295  
1296 sub _priv_encrypt_des
1297 2 ▶{
Subroutine _priv_encrypt_des: called CC=4 SCAR=14.3
1298 # my ($this, $priv_params, $plain) = @_;
1299  
1300 2 ▶ if (!defined $_[0]->{_priv_data}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $_[0]{"_priv_data"}) 0 2 2
1301 0 return $_[0]->_error('The required privacy data is not defined');
1302 }
1303  
1304 # Always pad the plain text data. "The actual pad value is
1305 # irrelevant..." according RFC 3414 Section 8.1.1.2. However,
1306 # there are some agents out there that expect "standard block
1307 # padding" where each of the padding byte(s) are set to the size
1308 # of the padding (even for data that is a multiple of block size).
1309  
1310 2 my $pad = 8 - (length($_[2]) % 8);
1311 2 $_[2] .= pack('C', $pad) x $pad;
1312  
1313 # Create and set the salt
1314 2 ▶ if ($_[0]->{_priv_data}->{salt}++ == ~0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]{"_priv_data"}{"salt"}++ == '18446744073709551615') 0 2 2
1315 0 $_[0]->{_priv_data}->{salt} = 0;
1316 }
1317 2 $_[1] = pack 'NN', $_[0]->{_engine_boots}, $_[0]->{_priv_data}->{salt};
1318  
1319 # Create the initial vector (IV)
1320 2 my $iv = $_[0]->{_priv_data}->{pre_iv} ^ $_[1];
1321  
1322 2 my $cipher = q{};
1323  
1324 # Perform Cipher Block Chaining (CBC)
1325 2 ▶ while ($_[2] =~ /(.{8})/gs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[2] =~ /(.{8})/gs) 10 2 12
1326 10 $cipher .= $iv = $_[0]->{_priv_data}->{des}->encrypt($1 ^ $iv);
1327 }
1328  
1329 2 return $cipher;
1330 }
1331  
1332 sub _priv_decrypt_des
1333 2 ▶{
Subroutine _priv_decrypt_des: called CC=5 SCAR=17.4
1334 # my ($this, $priv_params, $cipher) = @_;
1335  
1336 2 ▶ if (!defined $_[0]->{_priv_data}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $_[0]{"_priv_data"}) 0 2 2
1337 0 return $_[0]->_error('The required privacy data is not defined');
1338 }
1339  
1340 2 ▶ if (length($_[1]) != 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_[1] != 8) 0 2 2
1341 0 return $_[0]->_error(
1342 'The msgPrivParameters length of %d is invalid', length $_[1]
1343 );
1344 }
1345  
1346 2 ▶ if (length($_[2]) % 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (length($_[2]) % 8) 0 2 2
1347 0 return $_[0]->_error(
1348 'The DES cipher length is not a multiple of the block size'
1349 );
1350 }
1351  
1352 # Create the initial vector (IV)
1353 2 my $iv = $_[0]->{_priv_data}->{pre_iv} ^ $_[1];
1354  
1355 2 my $plain = q{};
1356  
1357 # Perform Cipher Block Chaining (CBC)
1358 2 ▶ while ($_[2] =~ /(.{8})/gs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[2] =~ /(.{8})/gs) 10 2 12
1359 10 $plain .= $iv ^ $_[0]->{_priv_data}->{des}->decrypt($1);
1360 10 $iv = $1;
1361 }
1362  
1363 2 return $plain;
1364 }
1365  
1366 sub _priv_data_init_3desede
1367 0 ▶{
Subroutine _priv_data_init_3desede: not called CC=4 SCAR=30.0
1368 0 my ($this) = @_;
1369  
1370 0 ▶ if (!defined $this->{_priv_key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $this->{"_priv_key"}) 0 0 0
1371 0 return $this->_error('The required privKey is not defined');
1372 }
1373  
1374 # Create the 3 DES objects
1375  
1376 $this->{_priv_data}->{des1} =
1377 0 Crypt::DES->new(substr $this->{_priv_key}, 0, 8);
1378 $this->{_priv_data}->{des2} =
1379 0 Crypt::DES->new(substr $this->{_priv_key}, 8, 8);
1380 $this->{_priv_data}->{des3} =
1381 0 Crypt::DES->new(substr $this->{_priv_key}, 16, 8);
1382  
1383 # Extract the pre-IV
1384 0 $this->{_priv_data}->{pre_iv} = substr $this->{_priv_key}, 24, 8;
1385  
1386 # Initialize the salt
1387 0 $this->{_priv_data}->{salt} = int rand ~0;
1388  
1389 # Assign a hash algorithm to "bit spread" the salt
1390  
1391 0 ▶ if ($this->{_auth_protocol} eq AUTH_PROTOCOL_HMACMD5) {
Branch: 2 branches
BranchTrueFalseTotal
if ($this->{'_auth_protocol'} eq AUTH_PROTOCOL_HMACMD5()) { } 0 0 0
elsif ($this->{'_auth_protocol'} eq AUTH_PROTOCOL_HMACSHA()) { } 0 0 0
1392 0 $this->{_priv_data}->{hash} = Digest::MD5->new();
1393 } elsif ($this->{_auth_protocol} eq AUTH_PROTOCOL_HMACSHA) {
1394 0 $this->{_priv_data}->{hash} = Digest::SHA1->new();
1395 }
1396  
1397 0 return TRUE;
1398 }
1399  
1400 sub _priv_encrypt_3desede
1401 0 ▶{
Subroutine _priv_encrypt_3desede: not called CC=5 SCAR=34.0
1402 # my ($this, $priv_params, $plain) = @_;
1403  
1404 0 ▶ if (!defined $_[0]->{_priv_data}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $_[0]{"_priv_data"}) 0 0 0
1405 0 return $_[0]->_error('The required privacy data is not defined');
1406 }
1407  
1408 # Pad the plain text data using "standard block padding".
1409 0 my $pad = 8 - (length($_[2]) % 8);
1410 0 $_[2] .= pack('C', $pad) x $pad;
1411  
1412 # Create and set the salt
1413 0 ▶ if ($_[0]->{_priv_data}->{salt}++ == ~0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]{"_priv_data"}{"salt"}++ == '18446744073709551615') 0 0 0
1414 0 $_[0]->{_priv_data}->{salt} = 0;
1415 }
1416 0 $_[1] = pack 'NN', $_[0]->{_engine_boots}, $_[0]->{_priv_data}->{salt};
1417  
1418 # Draft 3DES-EDE for USM Section 5.1.1.1.2 - "To achieve effective
1419 # bit spreading, the complete 8-octet 'salt' value SHOULD be
1420 # hashed using the usmUserAuthProtocol."
1421  
1422 0 ▶ if (exists $_[0]->{_priv_data}->{hash}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $_[0]{"_priv_data"}{"hash"}) 0 0 0
1423 0 $_[1] = substr $_[0]->{_priv_data}->{hash}->add($_[1])->digest(), 0, 8;
1424 }
1425  
1426 # Create the initial vector (IV)
1427 0 my $iv = $_[0]->{_priv_data}->{pre_iv} ^ $_[1];
1428  
1429 0 my $cipher = q{};
1430  
1431 # Perform Cipher Block Chaining (CBC)
1432 0 ▶ while ($_[2] =~ /(.{8})/gs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[2] =~ /(.{8})/gs) 0 0 0
1433 $cipher .= $iv =
1434 $_[0]->{_priv_data}->{des3}->encrypt(
1435 $_[0]->{_priv_data}->{des2}->decrypt(
1436 0 $_[0]->{_priv_data}->{des1}->encrypt($1 ^ $iv)
1437 )
1438 );
1439 }
1440  
1441 0 return $cipher;
1442 }
1443  
1444 sub _priv_decrypt_3desede
1445 0 ▶{
Subroutine _priv_decrypt_3desede: not called CC=5 SCAR=34.0
1446 # my ($this, $priv_params, $cipher) = @_;
1447  
1448 0 ▶ if (!defined $_[0]->{_priv_data}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $_[0]{"_priv_data"}) 0 0 0
1449 0 return $_[0]->_error('The required privacy data is not defined');
1450 }
1451  
1452 0 ▶ if (length($_[1]) != 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_[1] != 8) 0 0 0
1453 0 return $_[0]->_error(
1454 'The msgPrivParameters length of %d is invalid', length $_[1]
1455 );
1456 }
1457  
1458 0 ▶ if (length($_[2]) % 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (length($_[2]) % 8) 0 0 0
1459 0 return $_[0]->_error(
1460 'The CBC-3DES-EDE cipher length is not a multiple of the block size'
1461 );
1462 }
1463  
1464 # Create the initial vector (IV)
1465 0 my $iv = $_[0]->{_priv_data}->{pre_iv} ^ $_[1];
1466  
1467 0 my $plain = q{};
1468  
1469 # Perform Cipher Block Chaining (CBC)
1470 0 ▶ while ($_[2] =~ /(.{8})/gs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[2] =~ /(.{8})/gs) 0 0 0
1471 $plain .=
1472 $iv ^ $_[0]->{_priv_data}->{des1}->decrypt(
1473 $_[0]->{_priv_data}->{des2}->encrypt(
1474 0 $_[0]->{_priv_data}->{des3}->decrypt($1)
1475 )
1476 );
1477 0 $iv = $1;
1478 }
1479  
1480 0 return $plain;
1481 }
1482  
1483 sub _priv_data_init_aescfbxxx
1484 0 ▶{
Subroutine _priv_data_init_aescfbxxx: not called CC=2 SCAR=13.4
1485 0 my ($this) = @_;
1486  
1487 0 ▶ if (!defined $this->{_priv_key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $this->{"_priv_key"}) 0 0 0
1488 0 return $this->_error('The required privKey is not defined');
1489 }
1490  
1491 {
1492 # Avoid a "strict subs" error if Crypt::Rijndael is not loaded.
1493 0 ▶ no strict 'subs';
Subroutine BEGIN: called CC=1 SCAR=0.2
1494  
1495 # Create the AES (Rijndael) object with a 128, 192, or 256 bit key.
1496  
1497 $this->{_priv_data}->{aes} =
1498 0 Crypt::Rijndael->new($this->{_priv_key}, Crypt::Rijndael::MODE_CFB());
1499 }
1500  
1501 # Initialize the salt
1502 0 $this->{_priv_data}->{salt1} = int rand ~0;
1503 0 $this->{_priv_data}->{salt2} = int rand ~0;
1504  
1505 0 return TRUE;
1506 }
1507  
1508 sub _priv_encrypt_aescfbxxx
1509 0 ▶{
Subroutine _priv_encrypt_aescfbxxx: not called CC=5 SCAR=34.0
1510 # my ($this, $priv_params, $plain) = @_;
1511  
1512 0 ▶ if (!defined $_[0]->{_priv_data}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $_[0]{"_priv_data"}) 0 0 0
1513 0 return $_[0]->_error('The required privacy data is not defined');
1514 }
1515  
1516 # Validate the plain text length
1517 0 my $length = length $_[2];
1518 0 ▶ if ($length <= 16) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length <= 16) 0 0 0
1519 0 return $_[0]->_error(
1520 'The AES plain text length is not greater than the block size'
1521 );
1522 }
1523  
1524 # Create and set the salt
1525 0 ▶ if ($_[0]->{_priv_data}->{salt1}++ == ~0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]{"_priv_data"}{"salt1"}++ == '18446744073709551615') 0 0 0
1526 0 $_[0]->{_priv_data}->{salt1} = 0;
1527 0 ▶ if ($_[0]->{_priv_data}->{salt2}++ == ~0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]{"_priv_data"}{"salt2"}++ == '18446744073709551615') 0 0 0
1528 0 $_[0]->{_priv_data}->{salt2} = 0;
1529 }
1530 }
1531 $_[1] = pack 'NN', $_[0]->{_priv_data}->{salt2},
1532 0 $_[0]->{_priv_data}->{salt1};
1533  
1534 # AES in the USM Section - Section 3.1.3 "The last ciphertext
1535 # block is produced by exclusive-ORing the last plaintext segment
1536 # of r bits (r is less or equal to 128) with the segment of the r
1537 # most significant bits of the last output block."
1538  
1539 # This operation is identical to those performed on the previous
1540 # blocks except for the fact that the block can be less than the
1541 # block size. We can just pad the last block and operate on it as
1542 # usual and then ignore the padding after encrypting.
1543  
1544 0 $_[2] .= "\000" x (16 - ($length % 16));
1545  
1546 # Create the IV by concatenating "...the generating SNMP engine's
1547 # 32-bit snmpEngineBoots, the SNMP engine's 32-bit snmpEngineTime,
1548 # and a local 64-bit integer..."
1549  
1550 $_[0]->{_priv_data}->{aes}->set_iv(
1551 0 pack('NN', $_[0]->{_engine_boots}, $_[0]->{_engine_time}) . $_[1]
1552 );
1553  
1554 # Let the Crypt::Rijndael module perform 128 bit Cipher Feedback
1555 # (CFB) and return the result minus the "internal" padding.
1556  
1557 0 return substr $_[0]->{_priv_data}->{aes}->encrypt($_[2]), 0, $length;
1558 }
1559  
1560 sub _priv_decrypt_aescfbxxx
1561 0 ▶{
Subroutine _priv_decrypt_aescfbxxx: not called CC=4 SCAR=30.0
1562 # my ($this, $priv_params, $cipher) = @_;
1563  
1564 0 ▶ if (!defined $_[0]->{_priv_data}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $_[0]{"_priv_data"}) 0 0 0
1565 0 return $_[0]->_error('The required privacy data is not defined');
1566 }
1567  
1568 # Validate the msgPrivParameters length. We assume that the
1569 # msgAuthoritativeEngineBoots and msgAuthoritativeEngineTime
1570 # have been prepended to the msgPrivParameters to create the
1571 # required 128 bit IV.
1572  
1573 0 ▶ if (length($_[1]) != 16) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_[1] != 16) 0 0 0
1574 0 return $_[0]->_error(
1575 'The AES IV length of %d is invalid', length $_[1]
1576 );
1577 }
1578  
1579 # Validate the cipher length
1580 0 my $length = length $_[2];
1581 0 ▶ if ($length <= 16) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length <= 16) 0 0 0
1582 0 return $_[0]->_error(
1583 'The AES cipher length is not greater than the block size'
1584 );
1585 }
1586  
1587 # AES in the USM Section - Section 3.1.4 "The last ciphertext
1588 # block (whose size r is less or equal to 128) is less or equal
1589 # to 128) is exclusive-ORed with the segment of the r most
1590 # significant bits of the last output block to recover the last
1591 # plaintext block of r bits."
1592  
1593 # This operation is identical to those performed on the previous
1594 # blocks except for the fact that the block can be less than the
1595 # block size. We can just pad the last block and operate on it as
1596 # usual and then ignore the padding after decrypting.
1597  
1598 0 $_[2] .= "\000" x (16 - ($length % 16));
1599  
1600 # Use the msgPrivParameters as the IV.
1601 0 $_[0]->{_priv_data}->{aes}->set_iv($_[1]);
1602  
1603 # Let the Crypt::Rijndael module perform 128 bit Cipher Feedback
1604 # (CFB) and return the result minus the "internal" padding.
1605  
1606 0 return substr $_[0]->{_priv_data}->{aes}->decrypt($_[2]), 0, $length;
1607 }
1608  
1609 sub _auth_key_generate
1610 2 ▶{
Subroutine _auth_key_generate: called CC=3 SCAR=12.7
1611 2 my ($this) = @_;
1612  
1613 2 ▶ if (!defined($this->{_engine_id}) || !defined $this->{_auth_password}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $this->{"_engine_id"} and defined $this->{"_auth_password"}) 0 2 2
Condition33%1/3
defined $this->{"_engine_id"} and defined $this->{"_auth_password"}
!ll&&!rl&&r
002
MC/DC0%0/2
defined $this->{"_engine_id"} and defined $this->{"_auth_password"}
defined $this->{"_engine_id"} defined $this->{"_auth_password"}
Truth table33%1/3
A and B
A= defined $this->{"_engine_id"}B= defined $this->{"_auth_password"}
ABresult
0X0
100
111
1614 0 return $this->_error('Unable to generate the authKey');
1615 }
1616  
1617 2 $this->{_auth_key} = $this->_password_localize($this->{_auth_password});
1618  
1619 2 return $this->{_auth_key};
1620 }
1621  
1622 sub _auth_key_validate
1623 4 ▶{
Subroutine _auth_key_validate: called CC=3 SCAR=12.3
1624 4 my ($this) = @_;
1625  
1626 4 my $key_len =
1627 {
1628 AUTH_PROTOCOL_HMACMD5, [ 16, 'HMAC-MD5' ],
1629 AUTH_PROTOCOL_HMACSHA, [ 20, 'HMAC-SHA1' ],
1630 };
1631  
1632 4 ▶ if (!exists $key_len->{$this->{_auth_protocol}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $$key_len{$this->{"_auth_protocol"}}) 0 4 4
1633 return $this->_error(
1634 'The authProtocol "%s" is unknown', $this->{_auth_protocol}
1635 0 );
1636 }
1637  
1638 4 ▶ if (length($this->{_auth_key}) != $key_len->{$this->{_auth_protocol}}->[0])
Branch: 1 branch
BranchTrueFalseTotal
if (length $this->{"_auth_key"} != $$key_len{$this->{"_auth_protocol"}}[0]) 0 4 4
1639 {
1640 return $this->_error(
1641 'The %s authKey length of %d is invalid, expected %d',
1642 $key_len->{$this->{_auth_protocol}}->[1], length($this->{_auth_key}),
1643 0 $key_len->{$this->{_auth_protocol}}->[0]
1644 );
1645 }
1646  
1647 4 return TRUE;
1648 }
1649  
1650 sub _priv_key_generate
1651 2 ▶{
Subroutine _priv_key_generate: called CC=10 SCAR=33.8
1652 2 my ($this) = @_;
1653  
1654 2 ▶ if (!defined($this->{_engine_id}) || !defined $this->{_priv_password}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $this->{"_engine_id"} and defined $this->{"_priv_password"}) 0 2 2
Condition33%1/3
defined $this->{"_engine_id"} and defined $this->{"_priv_password"}
!ll&&!rl&&r
002
MC/DC0%0/2
defined $this->{"_engine_id"} and defined $this->{"_priv_password"}
defined $this->{"_engine_id"} defined $this->{"_priv_password"}
Truth table33%1/3
A and B
A= defined $this->{"_engine_id"}B= defined $this->{"_priv_password"}
ABresult
0X0
100
111
1655 0 return $this->_error('Unable to generate the privKey');
1656 }
1657  
1658 2 $this->{_priv_key} = $this->_password_localize($this->{_priv_password});
1659  
1660 2 ▶ return $this->_error() if !defined $this->{_priv_key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $this->{"_priv_key"} 0 2 2
1661  
1662 2 ▶ if ($this->{_priv_protocol} eq PRIV_PROTOCOL_DRAFT_3DESEDE) {
Branch: 2 branches
BranchTrueFalseTotal
if ($this->{'_priv_protocol'} eq PRIV_PROTOCOL_DRAFT_3DESEDE()) { } 0 2 2
elsif ($this->{'_priv_protocol'} eq PRIV_PROTOCOL_DRAFT_AESCFB192() or $this->{'_priv_protocol'} eq PRIV_PROTOCOL_DRAFT_AESCFB256()) { } 0 2 2
1663  
1664 # Draft 3DES-EDE for USM Section 2.1 - "To acquire the necessary
1665 # number of key bits, the password-to-key algorithm may be chained
1666 # using its output as further input in order to generate an
1667 # appropriate number of key bits."
1668  
1669 0 $this->{_priv_key} .= $this->_password_localize($this->{_priv_key});
1670  
1671 ▶ } elsif (($this->{_priv_protocol} eq PRIV_PROTOCOL_DRAFT_AESCFB192) ||
Condition33%1/3
$this->{"_priv_protocol"} eq PRIV_PROTOCOL_DRAFT_AESCFB192() or $this->{"_priv_protocol"} eq PRIV_PROTOCOL_DRAFT_AESCFB256()
l!l&&r!l&&!r
002
MC/DC0%0/2
$this->{"_priv_protocol"} eq PRIV_PROTOCOL_DRAFT_AESCFB192() or $this->{"_priv_protocol"} eq PRIV_PROTOCOL_DRAFT_AESCFB256()
$this->{"_priv_protocol"} eq PRIV_PROTOCOL_DRAFT_AESCFB192() $this->{"_priv_protocol"} eq PRIV_PROTOCOL_DRAFT_AESCFB256()
Truth table33%1/3
A or B
A= $this->{"_priv_protocol"} eq PRIV_PROTOCOL_DRAFT_AESCFB192()B= $this->{"_priv_protocol"} eq PRIV_PROTOCOL_DRAFT_AESCFB256()
ABresult
1X1
011
000
1672 ($this->{_priv_protocol} eq PRIV_PROTOCOL_DRAFT_AESCFB256))
1673 {
1674 # Draft AES in the USM Section 3.1.2.1 - "...if the size of the
1675 # localized key is not large enough to generate an encryption
1676 # key... ...set Kul = Kul || Hnnn(Kul) where Hnnn is the hash
1677 # function for the authentication protocol..."
1678  
1679 0 my $hnnn;
1680  
1681 0 ▶ if ($this->{_auth_protocol} eq AUTH_PROTOCOL_HMACMD5) {
Branch: 2 branches
BranchTrueFalseTotal
if ($this->{'_auth_protocol'} eq AUTH_PROTOCOL_HMACMD5()) { } 0 0 0
elsif ($this->{'_auth_protocol'} eq AUTH_PROTOCOL_HMACSHA()) { } 0 0 0
1682 0 $hnnn = Digest::MD5->new();
1683 } elsif ($this->{_auth_protocol} eq AUTH_PROTOCOL_HMACSHA) {
1684 0 $hnnn = Digest::SHA1->new();
1685 } else {
1686 return $this->_error(
1687 'The authProtocol "%s" is unknown', $this->{_auth_protocol}
1688 0 );
1689 }
1690  
1691 0 $this->{_priv_key} .= $hnnn->add($this->{_priv_key})->digest();
1692  
1693 }
1694  
1695 # Truncate the privKey to the appropriate length.
1696  
1697 2 my $key_len =
1698 {
1699 PRIV_PROTOCOL_DES, 16, # RFC 3414 Section 8.2.1
1700 PRIV_PROTOCOL_DRAFT_3DESEDE, 32, # Draft 3DES for USM Section 5.2.1
1701 PRIV_PROTOCOL_AESCFB128, 16, # AES in the USM Section 3.2.1
1702 PRIV_PROTOCOL_DRAFT_AESCFB192, 24, # Draft AES in the USM Section 3.2.1
1703 PRIV_PROTOCOL_DRAFT_AESCFB256, 32 # Draft AES in the USM Section 3.2.1
1704 };
1705  
1706 2 ▶ if (!exists $key_len->{$this->{_priv_protocol}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $$key_len{$this->{"_priv_protocol"}}) 0 2 2
1707 return $this->_error(
1708 'The privProtocol "%s" is unknown', $this->{_priv_protocol}
1709 0 );
1710 }
1711  
1712 $this->{_priv_key} =
1713 2 substr $this->{_priv_key}, 0, $key_len->{$this->{_priv_protocol}};
1714  
1715 2 return $this->{_priv_key};
1716 }
1717  
1718 sub _priv_key_validate
1719 4 ▶{
Subroutine _priv_key_validate: called CC=7 SCAR=30.3
1720 4 my ($this) = @_;
1721  
1722 4 my $key_len =
1723 {
1724 PRIV_PROTOCOL_DES, [ 16, 'CBC-DES' ],
1725 PRIV_PROTOCOL_DRAFT_3DESEDE, [ 32, 'CBC-3DES-EDE' ],
1726 PRIV_PROTOCOL_AESCFB128, [ 16, 'CFB128-AES-128' ],
1727 PRIV_PROTOCOL_DRAFT_AESCFB192, [ 24, 'CFB128-AES-192' ],
1728 PRIV_PROTOCOL_DRAFT_AESCFB256, [ 32, 'CFB128-AES-256' ]
1729 };
1730  
1731 4 ▶ if (!exists $key_len->{$this->{_priv_protocol}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $$key_len{$this->{"_priv_protocol"}}) 0 4 4
1732 return $this->_error(
1733 'The privProtocol "%s" is unknown', $this->{_priv_protocol}
1734 0 );
1735 }
1736  
1737 4 ▶ if (length($this->{_priv_key}) != $key_len->{$this->{_priv_protocol}}->[0])
Branch: 1 branch
BranchTrueFalseTotal
if (length $this->{"_priv_key"} != $$key_len{$this->{"_priv_protocol"}}[0]) 0 4 4
1738 {
1739 return $this->_error(
1740 'The %s privKey length of %d is invalid, expected %d',
1741 $key_len->{$this->{_priv_protocol}}->[1], length($this->{_priv_key}),
1742 0 $key_len->{$this->{_priv_protocol}}->[0]
1743 );
1744 }
1745  
1746 4 ▶ if ($this->{_priv_protocol} eq PRIV_PROTOCOL_DRAFT_3DESEDE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_priv_protocol"} eq PRIV_PROTOCOL_DRAFT_3DESEDE()) 0 4 4
1747  
1748 # Draft 3DES-EDE for USM Section 5.1.1.1.1 "The checks for difference
1749 # and weakness... ...should be performed when the key is assigned.
1750 # If any of the mandated tests fail, then the whole key MUST be
1751 # discarded and an appropriate exception noted."
1752  
1753 0 ▶ if (substr($this->{_priv_key}, 0, 8) eq substr $this->{_priv_key}, 8, 8)
Branch: 1 branch
BranchTrueFalseTotal
if (substr($this->{"_priv_key"}, 0, 8) eq substr($this->{"_priv_key"}, 8, 8)) 0 0 0
1754 {
1755 0 return $this->_error(
1756 'The CBC-3DES-EDE privKey is invalid (K1 equals K2)'
1757 );
1758 }
1759  
1760 0 ▶ if (substr($this->{_priv_key}, 8, 8) eq substr $this->{_priv_key}, 16, 8)
Branch: 1 branch
BranchTrueFalseTotal
if (substr($this->{"_priv_key"}, 8, 8) eq substr($this->{"_priv_key"}, 16, 8)) 0 0 0
1761 {
1762 0 return $this->_error(
1763 'The CBC-3DES-EDE privKey is invalid (K2 equals K3)'
1764 );
1765 }
1766  
1767 0 ▶ if (substr($this->{_priv_key}, 0, 8) eq substr $this->{_priv_key}, 16, 8)
Branch: 1 branch
BranchTrueFalseTotal
if (substr($this->{"_priv_key"}, 0, 8) eq substr($this->{"_priv_key"}, 16, 8)) 0 0 0
1768 {
1769 0 return $this->_error(
1770 'The CBC-3DES-EDE privKey is invalid (K1 equals K3)'
1771 );
1772 }
1773  
1774 }
1775  
1776 4 return TRUE;
1777 }
1778  
1779 sub _password_localize
1780 4 ▶{
Subroutine _password_localize: called CC=3 SCAR=11.1
1781 4 my ($this, $password) = @_;
1782  
1783 4 my $digests =
1784 {
1785 AUTH_PROTOCOL_HMACMD5, 'Digest::MD5',
1786 AUTH_PROTOCOL_HMACSHA, 'Digest::SHA1',
1787 };
1788  
1789 4 ▶ if (!exists $digests->{$this->{_auth_protocol}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $$digests{$this->{"_auth_protocol"}}) 0 4 4
1790 return $this->_error(
1791 'The authProtocol "%s" is unknown', $this->{_auth_protocol}
1792 0 );
1793 }
1794  
1795 4 my $digest = $digests->{$this->{_auth_protocol}}->new;
1796  
1797 # Create the initial digest using the password
1798  
1799 4 my $d = my $pad = $password x ((2048 / length $password) + 1);
1800  
1801 4 ▶ for (my $count = 0; $count < 2**20; $count += 2048) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count < 1048576) 2048 4 2052
1802 2048 $digest->add(substr $d, 0, 2048, q{});
1803 2048 $d .= $pad;
1804 }
1805 4 $d = $digest->digest;
1806  
1807 # Localize the key with the authoritativeEngineID
1808  
1809 4 return $digest->add($d . $this->{_engine_id} . $d)->digest();
1810 }
1811  
1812 {
1813 1 my %modules;
1814  
1815 sub load_module
1816 0 ▶ {
Subroutine load_module: not called CC=4 SCAR=30.0
Pod undocumented
1817 0 my ($module) = @_;
1818  
1819 # We attempt to load the required module under the protection of an
1820 # eval statement. If there is a failure, typically it is due to a
1821 # missing module required by the requested module and we attempt to
1822 # simplify the error message by just listing that module. We also
1823 # need to track failures since require() only produces an error on
1824 # the first attempt to load the module.
1825  
1826 # NOTE: Contrary to our typical convention, a return value of "undef"
1827 # actually means success and a defined value means error.
1828  
1829 0 ▶ return $modules{$module} if exists $modules{$module};
Branch: 1 branch
BranchTrueFalseTotal
if exists $modules{$module} 0 0 0
1830  
1831 0 ▶ if (!eval "require $module") {
Branch: 1 branch
BranchTrueFalseTotal
if (not eval "require $module") { } 0 0 0
1832 0 ▶ if ($@ =~ /locate (\S+\.pm)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ =~ /locate (\S+\.pm)/) { } 0 0 0
1833 0 $modules{$module} = sprintf '(Required module %s not found)', $1;
1834 } else {
1835 0 $modules{$module} = sprintf '(%s)', $@;
1836 }
1837 } else {
1838 0 $modules{$module} = undef;
1839 }
1840  
1841 0 return $modules{$module};
1842 }
1843 }
1844  
1845 # ============================================================================
1846 1 1; # [end Net::SNMP::Security::USM]
1847  
« SNMP.pm ↑ Summary SNMP/PDU.pm »