Exemple #1
0
 def tls_connect(cls, host, port, username, password, **kwargs):
     from tlslite.api import TLSConnection
     s = cls._connect(host, port, **kwargs)
     s2 = TLSConnection(s)
     s2.fileno = lambda fd=s.fileno(): fd
     s2.handshakeClientSRP(username, password)
     return cls(s2)
def run_handshake(client_cb, server_cb):
    """Run the given client/server callbacks in parallel, returning sent data.
    
    This function spawns two background threads on which to run the given
    callbacks in parallel. Each will be passed an instance of `TLSConnection`
    through which it can communicate with the other.

    Once both callbacks complete, this function will return a tuple (c, s) of
    the data sent by the client, and by the server. If either callback throws
    an error it will be propagated into the main thread.
    """
    c, s = mocketpair()
    client = TLSConnection(c)
    server = TLSConnection(s)
    errors = []

    def run_client():
        try:
            client_cb(client)
        except Exception as err:
            errors.append(err)

    def run_server():
        try:
            server_cb(server)
        except Exception as err:
            errors.append(err)

    tc = threading.Thread(target=run_client)
    ts = threading.Thread(target=run_server)
    tc.start()
    ts.start()
    tc.join()
    ts.join()
    if errors:
        raise errors[0]
    assert len(MOCKED_URANDOM_CALLS) == 0, 'unused mock urandom call'
    return (c.sent_data, s.sent_data)
Exemple #3
0
                def __init__(self, s):
                    self.s = TLSConnection(s)
                    self.s.handshakeClientCert()

                    self.writing = None
                    self.reading = None
    return h_dict


if __name__ == '__main__':
    host, port = parse_args()
    out_dir = os.path.join(os.getcwd(), host)
    if not os.path.exists(out_dir):
        try:
            os.mkdir(out_dir)
        except OSError:
            out_dir = mkdtemp()
    print('Using %s as output directory' % (out_dir))
    print('Trying to connect to %s:%i' % (host, port))
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((host, port))
    connection = TLSConnection(sock)
    connection.handshakeClientCert(nextProtos=["spdy/%i" % SPDY_VERSION])
    print('TLS NPN Selected: %s' % connection.next_proto)
    spdy_ctx = Context(CLIENT, version=SPDY_VERSION)
    connection.write(get_site_resource(spdy_ctx, host))
    headers = {spdy_ctx._last_stream_id: {'path': 'index'}}
    res_files = {
        spdy_ctx._last_stream_id: (os.path.join(out_dir, 'index'), None)
    }
    resources = None
    goaway = False
    while not goaway:
        answer = connection.read()  # Blocking
        spdy_ctx.incoming(answer)
        frame = get_frame(spdy_ctx)
        while frame:
Exemple #5
0
 def from_secure_server_socket(cls, sock, vdb):
     from tlslite.api import TLSConnection
     sock = TLSConnection(sock)
     sock.handshakeServer(verifierDB=vdb)
     return cls(sock)
Exemple #6
0
 def from_new_secure_socket(cls, host, port, username, password):
     from tlslite.api import TLSConnection
     stream = cls.from_new_socket(host, port)
     stream.sock = TLSConnection(stream.sock)
     stream.sock.handshakeClientSRP(username, password)
     return stream