Commits

Anonymous committed 57e345a

Fixed test_socket_ssl failures.

Comments (0)

Files changed (3)

eventlet/green/socket.py

 _fileobject = __socket._fileobject
 
 from eventlet.api import get_hub
+from eventlet.util import wrap_ssl_obj
 from eventlet.greenio import GreenSocket as socket
 from eventlet.greenio import GreenSSL as _GreenSSL
 from eventlet.greenio import GreenSSLObject as _GreenSSLObject
 
 
 def ssl(sock, certificate=None, private_key=None):
-    from OpenSSL import SSL
-    context = SSL.Context(SSL.SSLv23_METHOD)
-    if certificate is not None:
-        context.use_certificate_file(certificate)
-    if private_key is not None:
-        context.use_privatekey_file(private_key)
-    context.set_verify(SSL.VERIFY_NONE, lambda *x: True)
-
-    ## TODO only do this on client sockets? how?
-    connection = SSL.Connection(context, sock)
-    connection.set_connect_state()
-    return _GreenSSLObject(_GreenSSL(connection))
+    return wrap_ssl_obj(sock, certificate, private_key)

eventlet/greenio.py

         return fn(*args, **kw)
         
         
+def _convert_to_sslerror(ex):
+    """ Transliterates SSL.SysCallErrors to socket.sslerrors"""
+    return socket.sslerror((ex[0], ex[1]))
+        
 class GreenSSLObject(object):
     """ Wrapper object around the SSLObjects returned by socket.ssl, which have a 
     slightly different interface from SSL.Connection objects. """
         """ Should only be called by a 'green' socket.ssl """
         assert(isinstance(green_ssl_obj, GreenSSL))
         self.connection = green_ssl_obj
+        try:
+            self.connection.do_handshake()
+        except SSL.SysCallError, e:
+            raise _convert_to_sslerror(e)
         
     def read(self, n=None):
         """If n is provided, read n bytes from the SSL connection, otherwise read
             raise NotImplementedError("GreenSSLObject does not support "\
             " unlimited reads until we hear of someone needing to use them.")
         else:
-            return self.connection.read(n)
+            try:
+                return self.connection.read(n)
+            except SSL.SysCallError, e:
+                raise _convert_to_sslerror(e)
             
     def write(self, s):
         """Writes the string s to the on the object's SSL connection. 
         The return value is the number of bytes written. """
-        return self.connection.write(s)
+        try:
+            return self.connection.write(s)
+        except SSL.SysCallError, e:
+            raise _convert_to_sslerror(e)
 
     def server(self):
         """ Returns a string describing the server's certificate. Useful for debugging
         purposes; do not parse the content of this string because its format can't be
         parsed unambiguously. """
-        # NOTE: probably not the same as the real SSLObject, but, if someone actually
-        # uses this then we can fix it.
-        return str(self.connection.get_peer_certificate())
+        return str(self.connection.get_peer_certificate().get_subject())
         
     def issuer(self):
         """Returns a string describing the issuer of the server's certificate. Useful
         for debugging purposes; do not parse the content of this string because its 
         format can't be parsed unambiguously."""
-        # NOTE: probably not the same as the real SSLObject, but, if someone actually
-        # uses this then we can fix it.
         return str(self.connection.get_peer_certificate().get_issuer())
+        
                 
 
 def socketpair(*args):
     return greenio.GreenSSL(connection)
 
 
+def wrap_ssl_obj(sock, certificate=None, private_key=None):
+    """ For 100% compatibility with the socket module, this wraps and handshakes an 
+    open connection, returning a SSLObject."""
+    from eventlet import greenio
+    wrapped = wrap_ssl(sock, certificate, private_key)
+    return greenio.GreenSSLObject(wrapped)
+
 socket_already_wrapped = False
 def wrap_socket_with_coroutine_socket(use_thread_pool=True):
     global socket_already_wrapped
     socket.socket = new_socket
 
     # for 100% compatibility, return a GreenSSLObject
-    def new_ssl(sock, certificate=None, private_key=None):
-        wrapped = wrap_ssl(sock, certificate, private_key)
-        return greenio.GreenSSLObject(wrapped)
-    socket.ssl = new_ssl
+    socket.ssl = wrap_ssl_obj
 
     if use_thread_pool:
         from eventlet import tpool