Пример #1
0
    def test_server_side_invalid_2(self):
        t = StringTransport()

        p = TestHandshakeProtocol()

        now = time.time()
        hs = CryptoHandshaker(p, now)

        p.init_handler(hs.gen_handler())

        p.makeConnection(t)
        self.assertEquals(t.value(), '')

        ver_pkt = struct.pack('B', self.protocol_version)
        invalid_hs_data = (('abcdefg' * int(self.handshake_length * 2 / 7)) +
                           'abcdefg'[:int(self.handshake_length * 2 % 7)])

        # version is ok
        err = p.dataReceived(ver_pkt)
        self.assertIdentical(err, None)
        # ... but the rest is just (the right amount of) rubbish
        err = p.dataReceived(invalid_hs_data)

        self.assertIsInstance(err, failure.Failure)
        self.assertIsInstance(err.value, HandshakeFailedError)
        self.assertFalse(t.disconnecting, 'should not be disconnecting')
        self.assertEquals(p.handshake_status, 'fail')
Пример #2
0
    def test_server_side_invalid_2(self):
        t = StringTransport()

        p = TestHandshakeProtocol()

        now = time.time()
        hs = CryptoHandshaker(p, now)

        p.init_handler(hs.gen_handler())

        p.makeConnection(t)
        self.assertEquals(t.value(), '')

        ver_pkt = struct.pack('B', self.protocol_version)
        invalid_hs_data = (('abcdefg' * int(self.handshake_length * 2 / 7)) +
                           'abcdefg'[:int(self.handshake_length * 2 % 7)])

        # version is ok
        err = p.dataReceived(ver_pkt)
        self.assertIdentical(err, None)
        # ... but the rest is just (the right amount of) rubbish
        err = p.dataReceived(invalid_hs_data)

        self.assertIsInstance(err, failure.Failure)
        self.assertIsInstance(err.value, HandshakeFailedError)
        self.assertFalse(t.disconnecting, 'should not be disconnecting')
        self.assertEquals(p.handshake_status, 'fail')
Пример #3
0
    def _test_complete_exchange(self, client_handshaker_class,
                                client_failure=False, server_failure=False):
        tcli = StringTransport()
        tsrv = StringTransport()

        p_cli = TestHandshakeProtocol()
        p_srv = TestHandshakeProtocol()

        now = time.time()
        hs_cli = client_handshaker_class(p_cli, now - 0.001, is_client=True)
        hs_srv = CryptoHandshaker(p_srv, now - 0.042)


        p_srv.init_handler(hs_srv.gen_handler())
        p_cli.init_handler(hs_cli.gen_handler(), do_init=False)

        p_srv.makeConnection(tsrv)
        self.assertEquals(tsrv.value(), '')

        p_cli.makeConnection(tcli)
        self.assertEquals(tcli.value(), '')

        p_cli.init_handler()

        # self.assertEquals(len(tcli.value()), 1 + 1536)
        err = p_srv.dataReceived(tcli.value())
        tcli.clear()

        # self.assertEquals(len(tsrv.value()), 1 + 1536 + 1536)
        self.assertIdentical(err, None)
        self.assertEquals(p_cli.handshake_status, None)

        err = p_cli.dataReceived(tsrv.value() + '\x00')
        tsrv.clear()

        if client_failure:
            self.assertIsInstance(err, failure.Failure)
            self.assertIsInstance(err.value, HandshakeFailedError)
            self.assertFalse(tcli.disconnecting, 'should not be disconnecting')
            self.assertEquals(p_cli.handshake_status, 'fail')
            self.assertEquals(p_srv.handshake_status, None)
        else:
            self.assertIdentical(err, None)
            self.assertFalse(tcli.disconnecting, 'disconnecting')
            self.assertEquals(p_cli.handshake_status, 'ok')
            self.assertEquals(p_srv.handshake_status, None)


        err = p_srv.dataReceived(tcli.value() + '\x00')
        tcli.clear()

        if server_failure:
            self.assertIsInstance(err, failure.Failure)
            self.assertIsInstance(err.value, HandshakeFailedError)
            self.assertFalse(tsrv.disconnecting, 'should not be disconnecting')
            self.assertEquals(p_srv.handshake_status, 'fail')
        else:
            self.assertIdentical(err, None)
            self.assertFalse(tsrv.disconnecting, 'disconnecting')
            self.assertEquals(p_srv.handshake_status, 'ok')
Пример #4
0
    def _test_complete_exchange(self,
                                client_handshaker_class,
                                client_failure=False,
                                server_failure=False):
        tcli = StringTransport()
        tsrv = StringTransport()

        p_cli = TestHandshakeProtocol()
        p_srv = TestHandshakeProtocol()

        now = time.time()
        hs_cli = client_handshaker_class(p_cli, now - 0.001, is_client=True)
        hs_srv = CryptoHandshaker(p_srv, now - 0.042)

        p_srv.init_handler(hs_srv.gen_handler())
        p_cli.init_handler(hs_cli.gen_handler(), do_init=False)

        p_srv.makeConnection(tsrv)
        self.assertEquals(tsrv.value(), '')

        p_cli.makeConnection(tcli)
        self.assertEquals(tcli.value(), '')

        p_cli.init_handler()

        # self.assertEquals(len(tcli.value()), 1 + 1536)
        err = p_srv.dataReceived(tcli.value())
        tcli.clear()

        # self.assertEquals(len(tsrv.value()), 1 + 1536 + 1536)
        self.assertIdentical(err, None)
        self.assertEquals(p_cli.handshake_status, None)

        err = p_cli.dataReceived(tsrv.value() + '\x00')
        tsrv.clear()

        if client_failure:
            self.assertIsInstance(err, failure.Failure)
            self.assertIsInstance(err.value, HandshakeFailedError)
            self.assertFalse(tcli.disconnecting, 'should not be disconnecting')
            self.assertEquals(p_cli.handshake_status, 'fail')
            self.assertEquals(p_srv.handshake_status, None)
        else:
            self.assertIdentical(err, None)
            self.assertFalse(tcli.disconnecting, 'disconnecting')
            self.assertEquals(p_cli.handshake_status, 'ok')
            self.assertEquals(p_srv.handshake_status, None)

        err = p_srv.dataReceived(tcli.value() + '\x00')
        tcli.clear()

        if server_failure:
            self.assertIsInstance(err, failure.Failure)
            self.assertIsInstance(err.value, HandshakeFailedError)
            self.assertFalse(tsrv.disconnecting, 'should not be disconnecting')
            self.assertEquals(p_srv.handshake_status, 'fail')
        else:
            self.assertIdentical(err, None)
            self.assertFalse(tsrv.disconnecting, 'disconnecting')
            self.assertEquals(p_srv.handshake_status, 'ok')
Пример #5
0
    def test_server_side_ok(self):
        t = StringTransport()

        p = TestHandshakeProtocol()

        now = time.time()
        hs = CryptoHandshaker(p, now)

        p.init_handler(hs.gen_handler())

        p.makeConnection(t)
        # server shouldn't respond before spoken to
        self.assertEquals(t.value(), '')

        ver_pkt = struct.pack('B', self.protocol_version)

        err = p.dataReceived(ver_pkt)

        self.assertIdentical(err, None)

        # server may wait until it receives handshake packet before
        # responding, so not testing here

        hs_rand_data = '.' * (self.handshake_length - 8)
        timestamp = 42
        hs_pkt = struct.pack('>LL', timestamp, 0) + hs_rand_data

        err = p.dataReceived(hs_pkt)

        self.assertIdentical(err, None)
        # ... and here the server might send all the packets already,
        # and our implementation should do so, so that's what we test
        self.assert_(
            len(t.value()) == 1 + self.handshake_length * 2,
            'Response too short (%d)' % len(t.value()))

        self.assertEquals(
            struct.unpack_from('B', t.value(), 0)[0], self.protocol_version)

        srv_hs_pkt = t.value()[1:1 + self.handshake_length]
        hs_pkt_echo = t.value()[1 + self.handshake_length:1 +
                                self.handshake_length * 2]

        self.assertEquals(
            struct.unpack_from('>L', hs_pkt_echo, 0)[0], timestamp)
        self.assertEquals(hs_pkt_echo[8:], hs_rand_data)

        # server response ok, let's finish the server side, and
        # pretend client starts sending more data

        err = p.dataReceived(srv_hs_pkt + '\x00')

        self.assertIdentical(err, None)
        self.assertFalse(t.disconnecting, 'disconnecting')
        self.assertEquals(p.handshake_status, 'ok')
Пример #6
0
    def test_server_side_ok(self):
        t = StringTransport()

        p = TestHandshakeProtocol()

        now = time.time()
        hs = CryptoHandshaker(p, now)

        p.init_handler(hs.gen_handler())

        p.makeConnection(t)
        # server shouldn't respond before spoken to
        self.assertEquals(t.value(), '')

        ver_pkt = struct.pack('B', self.protocol_version)

        err = p.dataReceived(ver_pkt)

        self.assertIdentical(err, None)

        # server may wait until it receives handshake packet before
        # responding, so not testing here

        hs_rand_data = '.' * (self.handshake_length - 8)
        timestamp = 42
        hs_pkt = struct.pack('>LL', timestamp, 0) + hs_rand_data

        err = p.dataReceived(hs_pkt)

        self.assertIdentical(err, None)
        # ... and here the server might send all the packets already,
        # and our implementation should do so, so that's what we test
        self.assert_(len(t.value()) == 1 + self.handshake_length * 2,
                     'Response too short (%d)' % len(t.value()))

        self.assertEquals(struct.unpack_from('B', t.value(), 0)[0],
                          self.protocol_version)

        srv_hs_pkt = t.value()[1:1+self.handshake_length]
        hs_pkt_echo = t.value()[1+self.handshake_length:
                                    1+self.handshake_length*2]

        self.assertEquals(struct.unpack_from('>L', hs_pkt_echo, 0)[0],
                          timestamp)
        self.assertEquals(hs_pkt_echo[8:], hs_rand_data)

        # server response ok, let's finish the server side, and
        # pretend client starts sending more data

        err = p.dataReceived(srv_hs_pkt + '\x00')

        self.assertIdentical(err, None)
        self.assertFalse(t.disconnecting, 'disconnecting')
        self.assertEquals(p.handshake_status, 'ok')
Пример #7
0
    def test_server_side_invalid_1(self):
        t = StringTransport()

        p = TestHandshakeProtocol()

        now = time.time()
        hs = CryptoHandshaker(p, now)

        p.init_handler(hs.gen_handler())

        p.makeConnection(t)
        self.assertEquals(t.value(), '')

        # sending version different from the server's version
        ver_pkt = struct.pack('B', self.protocol_version + 1)

        err = p.dataReceived(ver_pkt)

        self.assertIsInstance(err, failure.Failure)
        self.assertIsInstance(err.value, HandshakeFailedError)
        self.assertFalse(t.disconnecting, 'should not be disconnecting')
        self.assertEquals(p.handshake_status, 'fail')
Пример #8
0
    def test_server_side_invalid_1(self):
        t = StringTransport()

        p = TestHandshakeProtocol()

        now = time.time()
        hs = CryptoHandshaker(p, now)

        p.init_handler(hs.gen_handler())

        p.makeConnection(t)
        self.assertEquals(t.value(), '')

        # sending version different from the server's version
        ver_pkt = struct.pack('B', self.protocol_version + 1)

        err = p.dataReceived(ver_pkt)

        self.assertIsInstance(err, failure.Failure)
        self.assertIsInstance(err.value, HandshakeFailedError)
        self.assertFalse(t.disconnecting, 'should not be disconnecting')
        self.assertEquals(p.handshake_status, 'fail')
Пример #9
0
 def __init__(self, *a, **kw):
     CryptoHandshaker.__init__(self, *a, **kw)
     self.compat_version = (255, 254, 253, 252)
     x = chandshake._make_offset_extractor(22, 6, 728, 20)
     self._digest_offset_extractor = x
Пример #10
0
 def __init__(self, *a, **kw):
     CryptoHandshaker.__init__(self, *a, **kw)
     self.compat_version = (255, 0, 3, 2)
     self._digest_offset_extractor = chandshake.schemes[0][0]
Пример #11
0
 def select_own_key(self):
     if self.is_client:
         return _full_fp_key
     return CryptoHandshaker.select_own_key(self)
Пример #12
0
 def __init__(self, *a, **kw):
     CryptoHandshaker.__init__(self, *a, **kw)
     self.compat_version = (255, 254, 253, 252)
     x = chandshake._make_offset_extractor(22, 6, 728, 20)
     self._digest_offset_extractor = x
Пример #13
0
 def __init__(self, *a, **kw):
     CryptoHandshaker.__init__(self, *a, **kw)
     self.compat_version = (255, 0, 3, 2)
     self._digest_offset_extractor = chandshake.schemes[0][0]
Пример #14
0
 def select_own_key(self):
     if self.is_client:
         return _full_fp_key
     return CryptoHandshaker.select_own_key(self)