lib/Net/BitTorrent/Protocol/MSE.pm

filter: Statementstmt 86.6% 168 / 194 Branchbran 68.6% 59 / 86 Conditioncond 61.1% 11 / 18 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 100% 25 / 25 Podpod 100% 5 / 5 totaltotal 80.1% 270 / 337 CC 51 SCAR 22.0
CC
51
Cov
80%
receive_data
20.7
_b_wait_reqs
15.4
_a_wait_pubkey
5.7
CRAP 9.0
« lib/Net/BitTorrent/Protocol/BEP53.pm ↑ Summary blib/lib/Net/BitTorrent/Emitter.pm »
1 26 ▶use v5.40;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 26 ▶use feature 'class';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 26 ▶no warnings 'experimental::class';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 26 ▶use Net::BitTorrent::Emitter;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 26 class Net::BitTorrent::Protocol::MSE v2.1.1 : isa(Net::BitTorrent::Emitter) {
6 26 ▶ use Net::BitTorrent::Protocol::MSE::KeyExchange;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 26 ▶ use Digest::SHA qw[sha1];
Subroutine BEGIN: called CC=1 SCAR=0.0
8 #
9 26 field $infohash : param = undef;
10 26 field $is_initiator : param = 0;
11 26 field $on_infohash_probe : param = undef;
12 26 field $allow_plaintext : param = 1;
13 26 field $kx;
14 26 field $state : reader;
15 26 field $buffer_in : reader = '';
16 26 field $buffer_out : reader = '';
17 26 field $wait_len = 0;
18 26 field $crypto_select = 0;
19 #
20 26 my $VC = "\0" x 8;
21 26 my $CRYPTO_PLAINTEXT = 0x01;
22 26 my $CRYPTO_RC4 = 0x02;
23 26 my $MAX_HANDSHAKE_BUFFER_SIZE = 32 * 1024; # 32KB
24  
25 #
26 13 ▶ method supported () {1}
Subroutine supported: called CC=1 SCAR=0.0
Pod documented
27 39 ▶ ADJUST {
Subroutine __ANON__: called CC=2 SCAR=6.9
28 39 $kx = Net::BitTorrent::Protocol::MSE::KeyExchange->new( infohash => $infohash, is_initiator => $is_initiator );
29 39 ▶ if ($is_initiator) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_initiator) { } 36 3 39
30 36 $buffer_out .= $kx->public_key;
31 36 $buffer_out .= $self->_random_pad();
32 36 $state = 'A_WAIT_PUBKEY';
33 }
34 else {
35 3 $state = 'B_WAIT_PUBKEY';
36 }
37 }
38  
39 58 ▶ method _random_pad () {
Subroutine _random_pad: called CC=1 SCAR=0.0
40 26 ▶ use Crypt::URandom qw[urandom];
Subroutine BEGIN: called CC=1 SCAR=0.0
41 58 my $len_bytes = urandom(2);
42 58 my $len = unpack( 'n', $len_bytes ) % 513;
43 58 return urandom($len);
44 }
45  
46 8 ▶ method write_buffer () {
Subroutine write_buffer: called CC=1 SCAR=0.0
Pod documented
47 8 my $tmp = $buffer_out;
48 8 $buffer_out = '';
49 8 return $tmp;
50 }
51  
52 1 ▶ method decrypt_data ($data) {
Subroutine decrypt_data: called CC=1 SCAR=0.0
Pod documented
53 1 return $self->receive_data($data);
54 }
55  
56 1 ▶ method encrypt_data ($data) {
Subroutine encrypt_data: called CC=2 SCAR=7.2
Pod documented
57 1 ▶ return $data unless $state eq 'PAYLOAD';
Branch: 1 branch
BranchTrueFalseTotal
unless $state eq "PAYLOAD" 0 1 1
58 1 return $kx->encrypt_rc4->crypt($data);
59 }
60  
61 106 ▶ method receive_data ($data) {
Subroutine receive_data: called CC=20 SCAR=30.3
Pod documented
62 106 ▶ if ( $state eq 'PAYLOAD' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state eq "PAYLOAD") 1 105 106
63 1 return $kx->decrypt_rc4->crypt($data);
64 }
65 105 $buffer_in .= $data;
66 105 ▶ if ( length($buffer_in) > $MAX_HANDSHAKE_BUFFER_SIZE ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $buffer_in > $MAX_HANDSHAKE_BUFFER_SIZE) 35 70 105
67 35 $state = 'FAILED';
68 35 return undef;
69 }
70 70 my $continue = 1;
71 70 ▶ while ( $continue && $state ne 'PAYLOAD' && $state ne 'FAILED' && $state ne 'PLAINTEXT_FALLBACK' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($continue and $state ne "PAYLOAD" and $state ne "FAILED" and $state ne "PLAINTEXT_FALLBACK") 17 70 87
Condition77%7/9
$continue and $state ne "PAYLOAD" and $state ne "FAILED" and $state ne "PLAINTEXT_FALLBACK"
!ll&&!rl&&r
70017
$continue and $state ne "PAYLOAD" and $state ne "FAILED"
!ll&&!rl&&r
76317
$continue and $state ne "PAYLOAD"
!ll&&!rl&&r
7080
MC/DC50%2/4
$continue and $state ne "PAYLOAD" and $state ne "FAILED" and $state ne "PLAINTEXT_FALLBACK"
$continue $state ne "PAYLOAD" $state ne "FAILED" $state ne "PLAINTEXT_FALLBACK"
Truth table60%3/5
A and B and C and D
A= $continueB= $state ne "PAYLOAD"C= $state ne "FAILED"D= $state ne "PLAINTEXT_FALLBACK"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
72 17 $continue = 0;
73 17 ▶ if ( $state eq 'A_WAIT_PUBKEY' ) { $continue = $self->_a_wait_pubkey() }
Branch: 11 branches
BranchTrueFalseTotal
if ($state eq 'A_WAIT_PUBKEY') { } 2 15 17
elsif ($state eq 'A_WAIT_VC') { } 4 11 15
elsif ($state eq 'A_WAIT_SELECT') { } 1 10 11
elsif ($state eq 'A_WAIT_PADD') { } 1 9 10
elsif ($state eq 'B_WAIT_PUBKEY') { } 2 7 9
elsif ($state eq 'B_WAIT_REQS') { } 2 5 7
elsif ($state eq 'B_WAIT_VC') { } 1 4 5
elsif ($state eq 'B_WAIT_PROVIDE') { } 1 3 4
elsif ($state eq 'B_WAIT_PADC') { } 1 2 3
elsif ($state eq 'B_WAIT_IA_LEN') { } 1 1 2
elsif ($state eq 'B_WAIT_IA') { } 1 0 1
74 4 elsif ( $state eq 'A_WAIT_VC' ) { $continue = $self->_a_wait_vc() }
75 1 elsif ( $state eq 'A_WAIT_SELECT' ) { $continue = $self->_a_wait_select() }
76 1 elsif ( $state eq 'A_WAIT_PADD' ) { $continue = $self->_a_wait_padd() }
77 2 elsif ( $state eq 'B_WAIT_PUBKEY' ) { $continue = $self->_b_wait_pubkey() }
78 2 elsif ( $state eq 'B_WAIT_REQS' ) { $continue = $self->_b_wait_reqs() }
79 1 elsif ( $state eq 'B_WAIT_VC' ) { $continue = $self->_b_wait_vc() }
80 1 elsif ( $state eq 'B_WAIT_PROVIDE' ) { $continue = $self->_b_wait_provide() }
81 1 elsif ( $state eq 'B_WAIT_PADC' ) { $continue = $self->_b_wait_padc() }
82 1 elsif ( $state eq 'B_WAIT_IA_LEN' ) { $continue = $self->_b_wait_ia_len() }
83 1 elsif ( $state eq 'B_WAIT_IA' ) { $continue = $self->_b_wait_ia() }
84 }
85 70 ▶ if ( $state eq 'PAYLOAD' && length($buffer_in) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state eq "PAYLOAD" and length $buffer_in > 0) 0 70 70
Condition66%2/3
$state eq "PAYLOAD" and length $buffer_in > 0
!ll&&!rl&&r
6820
MC/DC0%0/2
$state eq "PAYLOAD" and length $buffer_in > 0
$state eq "PAYLOAD" length $buffer_in > 0
Truth table66%2/3
A and B
A= $state eq "PAYLOAD"B= length $buffer_in > 0
ABresult
0X0
100
111
86 0 my $decrypted = $kx->decrypt_rc4->crypt($buffer_in);
87 0 $buffer_in = '';
88 0 return $decrypted;
89 }
90 70 return undef;
91 }
92  
93 2 ▶ method _a_wait_pubkey () {
Subroutine _a_wait_pubkey: called CC=5 SCAR=17.5
94 2 ▶ if ( $allow_plaintext && length($buffer_in) >= 1 && ord( substr( $buffer_in, 0, 1 ) ) == 19 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($allow_plaintext and length $buffer_in >= 1 and ord substr($buffer_in, 0, 1) == 19) 0 2 2
Condition33%2/6
$allow_plaintext and length $buffer_in >= 1 and ord substr($buffer_in, 0, 1) == 19
!ll&&!rl&&r
020
$allow_plaintext and length $buffer_in >= 1
!ll&&!rl&&r
002
MC/DC0%0/3
$allow_plaintext and length $buffer_in >= 1 and ord substr($buffer_in, 0, 1) == 19
$allow_plaintext length $buffer_in >= 1 ord substr($buffer_in, 0, 1) == 19
Truth table25%1/4
A and B and C
A= $allow_plaintextB= length $buffer_in >= 1C= ord substr($buffer_in, 0, 1) == 19
ABCresult
0XX0
10X0
1100
1111
95 0 $state = 'PLAINTEXT_FALLBACK';
96 0 return 0;
97 }
98 2 ▶ return 0 if length($buffer_in) < 96;
Branch: 1 branch
BranchTrueFalseTotal
if length $buffer_in < 96 0 2 2
99 2 my $remote_pub = substr( $buffer_in, 0, 96, '' );
100 2 $kx->compute_secret($remote_pub);
101 2 my ( $req1, $xor_part ) = $kx->get_sync_data();
102 2 $buffer_out .= $req1 . $xor_part;
103 2 $kx->init_rc4($infohash);
104 2 my $payload = $VC;
105 2 $payload .= pack( 'N', $CRYPTO_RC4 | $CRYPTO_PLAINTEXT );
106 2 $payload .= pack( 'n', 0 );
107 2 $payload .= pack( 'n', 0 );
108 2 $buffer_out .= $kx->encrypt_rc4->crypt($payload);
109 2 $state = 'A_WAIT_VC';
110 2 return 1;
111 }
112  
113 4 ▶ method _a_wait_vc () {
Subroutine _a_wait_vc: called CC=4 SCAR=13.9
114 4 ▶ return 0 if length($buffer_in) < 8;
Branch: 1 branch
BranchTrueFalseTotal
if length $buffer_in < 8 0 4 4
115 4 my $pad_len = $kx->scan_for_vc($buffer_in);
116 4 ▶ if ( $pad_len >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pad_len >= 0) 1 3 4
117 1 substr( $buffer_in, 0, $pad_len, '' );
118 1 my $vc_enc = substr( $buffer_in, 0, 8, '' );
119 1 $kx->decrypt_rc4->crypt($vc_enc);
120 1 $state = 'A_WAIT_SELECT';
121 1 return 1;
122 }
123 3 ▶ if ( length($buffer_in) > 600 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $buffer_in > 600) 1 2 3
124 1 $state = 'FAILED';
125 }
126 3 return 0;
127 }
128  
129 1 ▶ method _a_wait_select () {
Subroutine _a_wait_select: called CC=3 SCAR=11.7
130 1 ▶ return 0 if length($buffer_in) < 6;
Branch: 1 branch
BranchTrueFalseTotal
if length $buffer_in < 6 0 1 1
131 1 my $dec = $kx->decrypt_rc4->crypt( substr( $buffer_in, 0, 6, '' ) );
132 1 my $select = unpack( 'N', substr( $dec, 0, 4 ) );
133 1 my $pad_len = unpack( 'n', substr( $dec, 4, 2 ) );
134 1 ▶ if ( !( $select & $CRYPTO_RC4 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($select & $CRYPTO_RC4) 0 1 1
135 0 $state = 'FAILED';
136 0 return 0;
137 }
138 1 $wait_len = $pad_len;
139 1 $state = 'A_WAIT_PADD';
140 1 return 1;
141 }
142  
143 1 ▶ method _a_wait_padd () {
Subroutine _a_wait_padd: called CC=3 SCAR=11.8
144 1 ▶ return 0 if length($buffer_in) < $wait_len;
Branch: 1 branch
BranchTrueFalseTotal
if length $buffer_in < $wait_len 0 1 1
145 1 ▶ if ( $wait_len > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wait_len > 0) 0 1 1
146 0 $kx->decrypt_rc4->crypt( substr( $buffer_in, 0, $wait_len, '' ) );
147 }
148 1 $self->_emit( 'infohash_identified', $infohash );
149 1 $state = 'PAYLOAD';
150 1 return 0;
151 }
152  
153 2 ▶ method _b_wait_pubkey () {
Subroutine _b_wait_pubkey: called CC=2 SCAR=6.9
154 2 ▶ return 0 if length($buffer_in) < 96;
Branch: 1 branch
BranchTrueFalseTotal
if length $buffer_in < 96 1 1 2
155 1 my $pub_a = substr( $buffer_in, 0, 96, '' );
156 1 $kx->compute_secret($pub_a);
157 1 $buffer_out .= $kx->public_key;
158 1 $buffer_out .= $self->_random_pad();
159 1 $state = 'B_WAIT_REQS';
160 1 return 1;
161 }
162  
163 2 ▶ method _b_wait_reqs () {
Subroutine _b_wait_reqs: called CC=8 SCAR=27.4
164 2 my $s = $kx->get_secret;
165 2 my $req1_hash = sha1( 'req1' . $s );
166 2 my $idx = index( $buffer_in, $req1_hash );
167 2 ▶ if ( $idx == -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx == -1) 1 1 2
168 0 ▶ if ( length($buffer_in) > 600 ) { $state = 'FAILED'; }
Branch: 1 branch
BranchTrueFalseTotal
if (length $buffer_in > 600) 0 1 1
169 1 return 0;
170 }
171 1 substr( $buffer_in, 0, $idx + 20, '' );
172 0 ▶ if ( length($buffer_in) < 20 ) { return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (length $buffer_in < 20) 0 1 1
173 1 my $xor_block = substr( $buffer_in, 0, 20, '' );
174 1 ▶ if ( defined $infohash ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $infohash) { } 1 0 1
elsif ($on_infohash_probe) { } 0 0 0
175 1 ▶ if ( !$kx->verify_skey( $xor_block, $infohash ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($kx->verify_skey($xor_block, $infohash)) 0 1 1
176 0 $state = 'FAILED';
177 0 return 0;
178 }
179 }
180 elsif ($on_infohash_probe) {
181 0 my $req3_hash = sha1( 'req3' . $s );
182  
183 # FIX: Use ^. for string XOR
184 0 my $target = $xor_block^.$req3_hash;
185 0 $infohash = $on_infohash_probe->( $self, $target );
186 0 ▶ if ( !$infohash ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($infohash) 0 0 0
187 0 $state = 'FAILED';
188 0 return 0;
189 }
190 }
191 else {
192 0 $state = 'FAILED';
193 0 return 0;
194 }
195 1 $kx->init_rc4($infohash);
196 1 $self->_emit( 'infohash_identified', $infohash );
197 1 $state = 'B_WAIT_VC';
198 1 return 1;
199 }
200  
201 1 ▶ method _b_wait_vc () {
Subroutine _b_wait_vc: called CC=4 SCAR=14.8
202 1 ▶ return 0 if length($buffer_in) < 8;
Branch: 1 branch
BranchTrueFalseTotal
if length $buffer_in < 8 0 1 1
203 1 my $vc_check = $kx->decrypt_rc4->crypt( substr( $buffer_in, 0, 8, '' ) );
204 1 my $ok = 0;
205 1 for my $i ( 0 .. 7 ) {
206 8 $ok |= ord( substr( $vc_check, $i, 1 ) ) ^ ord( substr( $VC, $i, 1 ) );
207 }
208 1 ▶ if ( $ok != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok != 0) 0 1 1
209 0 $state = 'FAILED';
210 0 return 0;
211 }
212 1 $state = 'B_WAIT_PROVIDE';
213 1 return 1;
214 }
215  
216 1 ▶ method _b_wait_provide () {
Subroutine _b_wait_provide: called CC=3 SCAR=11.7
217 1 ▶ return 0 if length($buffer_in) < 6;
Branch: 1 branch
BranchTrueFalseTotal
if length $buffer_in < 6 0 1 1
218 1 my $dec = $kx->decrypt_rc4->crypt( substr( $buffer_in, 0, 6, '' ) );
219 1 my $provide = unpack( 'N', substr( $dec, 0, 4 ) );
220 1 my $len = unpack( 'n', substr( $dec, 4, 2 ) );
221 1 ▶ unless ( $provide & $CRYPTO_RC4 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($provide & $CRYPTO_RC4) 0 1 1
222 0 $state = 'FAILED';
223 0 return 0;
224 }
225 1 $wait_len = $len;
226 1 $state = 'B_WAIT_PADC';
227 1 return 1;
228 }
229  
230 1 ▶ method _b_wait_padc () {
Subroutine _b_wait_padc: called CC=3 SCAR=12.0
231 1 ▶ return 0 if length($buffer_in) < $wait_len;
Branch: 1 branch
BranchTrueFalseTotal
if length $buffer_in < $wait_len 0 1 1
232 1 ▶ if ( $wait_len > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wait_len > 0) 0 1 1
233 0 $kx->decrypt_rc4->crypt( substr( $buffer_in, 0, $wait_len, '' ) );
234 }
235 1 $state = 'B_WAIT_IA_LEN';
236 1 return 1;
237 }
238  
239 1 ▶ method _b_wait_ia_len () {
Subroutine _b_wait_ia_len: called CC=2 SCAR=7.0
240 1 ▶ return 0 if length($buffer_in) < 2;
Branch: 1 branch
BranchTrueFalseTotal
if length $buffer_in < 2 0 1 1
241 1 my $dec = $kx->decrypt_rc4->crypt( substr( $buffer_in, 0, 2, '' ) );
242 1 $wait_len = unpack( 'n', $dec );
243 1 $state = 'B_WAIT_IA';
244 1 return 1;
245 }
246  
247 1 ▶ method _b_wait_ia () {
Subroutine _b_wait_ia: called CC=3 SCAR=11.3
248 1 ▶ return 0 if length($buffer_in) < $wait_len;
Branch: 1 branch
BranchTrueFalseTotal
if length $buffer_in < $wait_len 0 1 1
249 1 ▶ if ( $wait_len > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wait_len > 0) 0 1 1
250 0 $kx->decrypt_rc4->crypt( substr( $buffer_in, 0, $wait_len, '' ) );
251 }
252 1 my $res = $VC;
253 1 $res .= pack( 'N', $CRYPTO_RC4 );
254 1 $res .= pack( 'n', 0 );
255 1 $buffer_out .= $kx->encrypt_rc4->crypt($res);
256 1 $state = 'PAYLOAD';
257 1 return 0;
258 }
259 };
260 26 1;
« lib/Net/BitTorrent/Protocol/BEP53.pm ↑ Summary blib/lib/Net/BitTorrent/Emitter.pm »