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