title: Python - SSL - wrong version number
body: <p>This will probably be just another unsolved thread but i'll fill in some info anyway.</p>
  <p>I can't get my SSL wrapping together not even for a second.
  Any ideas to what i'm doing wrong with my wrap_socket() and do_handshake()?</p>
  <p>The key files appear to be 100% perfect, and i've tried with AND without the .recv() before the handshake.
  That just generates these depending on where i put the recv():</p>
  <blockquote>
  <p>SSL3_GET_CLIENT_HELLO:wrong version number</p>
  <p>SSL3_GET_RECORD:wrong version number</p>
  </blockquote>
  <pre><code>class Server():
      def __init__(self, listen = '', port = 8080, ssl = False):
          self.sock = socket.socket()
          self.sock.bind((listen, port))
          self.sock.listen(5)
  
      def accept(self):
          newsocket, fromaddr = self.sock.accept()
          newsocket.recv(32)
          newsocket.setblocking(0)
          sslsock = ssl.wrap_socket(newsocket,
                                      server_side=True,
                                      certfile=&quot;./kernel/sock/server.crt&quot;,
                                      keyfile=&quot;./kernel/sock/server.key&quot;,
                                      cert_reqs=ssl.CERT_NONE,
                                      ssl_version=ssl.PROTOCOL_TLSv1,
                                      do_handshake_on_connect=False,
                                      suppress_ragged_eofs=True)
          sslsock.do_handshake()
          return sslsock, fromaddr
  </code></pre>
  <p>For the record, if it's not obvious or i'm wrong, it's the handshake that fails :)</p>
  <p>I modified the code a bit, trying SSLv3 and also change the position of the wrapping a bit:</p>
  <pre><code>import socket, ssl, time, select
  
  class Server():
      def __init__(self, listen = '', port = 443, ssl = False):
          self.sock = socket.socket()
          self.sock.bind((listen, port))
          self.sock.listen(5)
  
      def accept(self):
          self.sock = ssl.wrap_socket(self.sock,
                                      server_side=True,
                                      certfile=&quot;./kernel/sock/server.crt&quot;,
                                      keyfile=&quot;./kernel/sock/server.key&quot;,
                                      cert_reqs=ssl.CERT_NONE,
                                      ssl_version=ssl.PROTOCOL_SSLv3,
                                      do_handshake_on_connect=False,
                                      suppress_ragged_eofs=True)
  
          newsocket, fromaddr = self.sock.accept()
  
          print [newsocket.recv(32)]
          newsocket.setblocking(False)
          newsocket.do_handshake()
  
          return newsocket, fromaddr
  
  s = Server()
  ns, na = s.accept()
  print ns.recv(1024)
  </code></pre>
  <p>Now i get with the newsocket.recv(32):</p>
  <blockquote>
  <p>ssl.SSLError: [Errno 1] _ssl.c:1331: error:140940E5:SSL routines:SSL3_READ_BYTES:ssl handshake failure</p>
  </blockquote>
  <p>and without:</p>
  <blockquote>
  <p>ssl.SSLError: [Errno 2] _ssl.c:490: The operation did not complete (read)</p>
  </blockquote>
  <p>Also: <strong>i refuse to use Twisted</strong></p>
  <p>Scaling things down:</p>
  <pre><code>import socket, ssl, time, select
  from OpenSSL import SSL
  
  class Server():
      def __init__(self, listen = '', port = 443, ssl = False):
          ctx = SSL.Context(SSL.SSLv23_METHOD)
          ctx.use_privatekey_file(&quot;server.pem&quot;)
          ctx.use_certificate_file(&quot;server.pem&quot;)
          self.sock = SSL.Connection(ctx, socket.socket())
  
          self.sock.bind((listen, port))
          self.sock.listen(5)
  
      def accept(self):
          newsocket, fromaddr = self.sock.accept()
          return newsocket, fromaddr
  
  s = Server()
  ns, na = s.accept()
  print ns.recv(1024)
  </code></pre>
  <p>This works just as good as the &quot;native&quot; ssl library.
  However now i get this error:</p>
  <blockquote>
  <p>OpenSSL.SSL.Error: [('SSL routines', 'SSL23_READ', 'ssl handshake failure')]</p>
  </blockquote>
  <hr />
  <p>This is where i'm at now:</p>
  <pre><code>import socket, ssl, time #, select
  
  class Server():
      def __init__(self, listen = '', port = 443, ssl = False):
          self.sock = socket.socket()
          self.sock.bind((listen, port))
          self.sock.listen(5)
  
      def accept(self):
          self.ssl_sock = None
          while not self.ssl_sock:
              self.ssl_sock = ssl.wrap_socket(self.sock,
                  server_side=True,
                  certfile=r&quot;C:\moo.pem&quot;,
                  keyfile=r&quot;C:\moo.key&quot;,
                  cert_reqs=ssl.CERT_NONE,
                  ssl_version=ssl.PROTOCOL_TLSv1)
  
          newsocket, fromaddr = self.ssl_sock.accept()
  
          print([newsocket.recv()])
  
          return newsocket, fromaddr
  
  s = Server()
  ns, na = s.accept()
  print(ns.recv(1024))
  </code></pre>
  <p>This works &quot;prefectly&quot; in Firefox, but <strong>NOT</strong> in Google Chrome.
  Why? what's the difference? -.-</p>