Example #1
0
    def connect(self):
        error = None
        # We ignore the returned sockaddr because SSL.Connection.connect needs
        # a host name.
        for (family, _, _, _, _) in \
                socket.getaddrinfo(self.host, self.port, 0, socket.SOCK_STREAM):
            sock = None
            try:
                try:
                    sock = SSL.Connection(self.ssl_ctx, family=family)
                    if self.session is not None:
                        sock.set_session(self.session)
                    sock.connect((self.host, self.port))

                    self.sock = sock
                    sock = None
                    return
                except socket.error, e:
                    # Other exception are probably SSL-related, in that case we
                    # abort and the exception is forwarded to the caller.
                    error = e
            finally:
                if sock is not None:
                    sock.close()

        if error is None:
            raise AssertionError("Empty list returned by getaddrinfo")
        raise error
Example #2
0
def test():
    context = SSL.Context(SSL.TLSv1_METHOD)
    sock = socket.create_connection(('www.google.com', 443), timeout=2)
    connection = SSL.Connection(context, sock)
    connection.set_connect_state()
    connection.do_handshake()
    connection.send('GET / HTTP/1.1\r\n\r\n')
    print(connection.recv(1024))
Example #3
0
 def auth_tls(self):
     """Secure the control connection per AUTH TLS, aka AUTH TLS-C."""
     self.voidcmd('AUTH TLS')
     s = SSL.Connection(self.ssl_ctx, self.sock)
     s.setup_ssl()
     s.set_connect_state()
     s.connect_ssl()
     self.sock = s
     self.file = self.sock.makefile()
Example #4
0
 def ntransfercmd(self, cmd, rest=None):
     """Initiate a data transfer."""
     conn, size = FTP.ntransfercmd(self, cmd, rest)
     if self.prot:
         conn = SSL.Connection(self.ssl_ctx, conn)
         conn.setup_ssl()
         conn.set_connect_state()
         conn.set_session(self.sock.get_session())
         conn.connect_ssl()
     return conn, size
Example #5
0
 def connect(self, host, port=None):
     # Cribbed from httplib.HTTP.
     if not port:
         i = string.find(host, ':')
         if i >= 0:
             host, port = host[:i], host[i+1:]
             try: port = string.atoi(port)
             except string.atoi_error:
                 raise socket.error, "nonnumeric port"
     if not port: port = HTTPS_PORT
     self.sock = SSL.Connection(self.ssl_ctx)
     if self.debuglevel > 0: print 'connect:', (host, port)
     self.sock.connect((host, port))
Example #6
0
    def connect(self):
        """Connect (using SSL) to the host and port specified in __init__
        (through a proxy)."""
        import socket
        # Set the connection with the proxy
        HTTPProxyConnection.connect(self)
        # Use the stock HTTPConnection putrequest
        host = "%s:%s" % (self._host, self._port)
        HTTPConnection.putrequest(self, "CONNECT", host)
        # Add proxy-specific stuff
        self._add_auth_proxy_header()
        # And send the request
        HTTPConnection.endheaders(self)
        # Save the response class
        response_class = self.response_class
        # And replace the response class with our own one, which does not
        # close the connection
        self.response_class = HTTPSProxyResponse
        response = HTTPConnection.getresponse(self)
        # Restore the response class
        self.response_class = response_class
        # Close the response object manually
        response.close()
        if response.status != 200:
            # Close the connection manually
            self.close()
            # XXX Find the appropriate error code
            raise socket.error(1001, response.status, response.value)

        # NgPS: I haven't read the code recently, but I think it is
        # reasonable to assume that self.sock is a connected TCP socket at
        # this point.

        # Use the real stuff. ;-)
        if self.ssl_ctx and isinstance(self.ssl_ctx, SSL.Context):
           self.sock =  SSL.Connection(self.ssl_ctx)
           self.sock.connect((self.host, self.port))
        else:
           # Fake the socket
           ssl = socket.ssl(self.sock, self.key_file, self.cert_file)
           self.sock = FakeSocket(self.sock, ssl)
        if self.debuglevel > 0: print('socket type:', self.sock)
Example #7
0
 def connect(self):
     self.sock = SSL.Connection(self.ssl_ctx)
     self.sock.connect((self.host, self.port))
Example #8
0
 def connect(self):
     self.sock = SSL.Connection(self.ssl_ctx)
     if self.session:
         self.sock.set_session(self.session)
     self.sock.connect((self.host, self.port))
Example #9
0
 def _start_ssl(self):
     """ Make this connection's socket SSL-aware. """
     self.sock = SSL.Connection(self.ssl_ctx, self.sock)
     self.sock.setup_ssl()
     self.sock.set_connect_state()
     self.sock.connect_ssl()
Example #10
0
 def __init__(self, *args):
     self._ssl_conn = _ssl.Connection(*args)
     self._lock = _RLock()