title: Python openSLL Server Crash on connection - odd error on SSL
body: <p>I am trying to get a SSL server running, so it can accept JSON object and reply with other JSON objects. However, before doing JSON objects, I decided to do a simplified version and I came across this weird error with SSL that I cannot find anything about. Solutions on the generation of the certificate did not seem to work, unless I am doing something wrong. So here is everything I did: </p>
  
  <p>Python Server</p>
  
  <pre><code>import socket
  import re
  import ssl
  
  # Standard socket stuff:
  host = ''  # do we need socket.gethostname() ?
  port = 8080
  sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  sock.bind((host, port))
  sock.listen(1)  # don't queue up any requests
  
  # Loop forever, listening for requests:
  while True:
      csock, caddr = sock.accept()
      connstream = ssl.wrap_socket(csock,
                                   server_side=True,
                                   certfile="cert.pem",
                                   keyfile="cert.pem",
                                   ssl_version=ssl.PROTOCOL_SSLv23)
      print "Connection from: " + `caddr`
  
      dataBuf = connstream.recv(4096) # read what is there to read
      extraData = connstream.recv(4096) #see if there is more
      while len(extraData) != 0: #if something extra was read
          dataBuf += extraData
          extraData = connstream.recv(4096) #check again
  
  
  
      print '--------'
      print len(dataBuf), dataBuf
      # Look in the first line of the request for a move command
      # A move command should be e.g. 'http://server/move?a=90'
      match = re.match('GET /move\?a=(\d+)\sHTTP/1', dataBuf)
      if match:
          angle = match.group(1)
          print "ANGLE: " + angle + "\n"
          connstream.sendall("HTTP/1.0 200 OK\r\n"+
                               "Content-Type: text/html\r\n"+
                               "Connection: close\r\n"+
                               "\r\n"+
          """&lt;!DOCTYPE html&gt;
          &lt;head&gt;
          &lt;title&gt;Success&lt;/title&gt;
          &lt;/head&gt;
          &lt;body&gt;
          Boo!
          &lt;/body&gt;
          &lt;/html&gt;\r\n
          """)
      else:
          # If there was no recognised command then return a 404 (page not found)
          print "Returning 404"
          connstream.sendall("HTTP/1.0 404 Not Found\r\n")
      #connstream.shutdown(socket.SHUT_RDWR)
      connstream.close()
  </code></pre>
  
  <p>Generation of key and certificate : I am not sure if I am doing something wrong in the "Common Name" part, as I am giving localhost.localdomain following the post <a href="https://stackoverflow.com/questions/15954467/python-ssl-example-from-docs-gives-connection-reset-by-peer-error">Python SSL example from docs gives &quot;Connection reset by peer&quot; error</a></p>
  
  <pre><code>openssl req -new -x509 -days 365 -nodes -out cert.pem -keyout cert.pem
  </code></pre>
  
  <p>How I am completing the fields: </p>
  
  <pre><code>Country Name (2 letter code) [AU]:UK
  State or Province Name (full name) [Some-State]:C
  Locality Name (eg, city) []:C
  Organization Name (eg, company) [Internet Widgits Pty Ltd]:U of C
  Organizational Unit Name (eg, section) []:CL
  Common Name (e.g. server FQDN or YOUR name) []:localhost.localdomain
  Email Address []:unis
  </code></pre>
  
  <p><code>Errors: after going to https://localhost:8080/move?a=77 on a browser (Firefox and Chrome)</code></p>
  
  <pre><code>Connection from: ('127.0.0.1', 39107)
  Traceback (most recent call last):
    File "ser.py", line 29, in &lt;module&gt;
      dataBuf = connstream.recv(44096) # read what is there to read
    File "/usr/lib/python2.7/ssl.py", line 241, in recv
      return self.read(buflen)
    File "/usr/lib/python2.7/ssl.py", line 160, in read
      return self._sslobj.read(len)
  ssl.SSLError: [Errno 1] _ssl.c:1359: error:14094418:SSL routines:SSL3_READ_BYTES:tlsv1 alert unknown ca
  </code></pre>
  
  <p>Help appreciated! Thanks! </p>
  
  <p>ps: I tried TLSv1 and PROTOCOL_SSLv23, same error...</p>
  
  <p>///FOLLOW UP:</p>
  
  <p>I went onto Firefox and manually added the certificate. </p>
  
  <p>Now I get: </p>
  
  <pre><code>Connection from: ('127.0.0.1', 39220)
  Traceback (most recent call last):
    File "ser.py", line 29, in &lt;module&gt;
      dataBuf = connstream.recv(44096) # read what is there to read
    File "/usr/lib/python2.7/ssl.py", line 241, in recv
      return self.read(buflen)
    File "/usr/lib/python2.7/ssl.py", line 160, in read
      return self._sslobj.read(len)
  ssl.SSLError: [Errno 1] _ssl.c:1359: error:14094419:SSL routines:SSL3_READ_BYTES:tlsv1 alert access denied
  </code></pre>
  
  <p>Any Ideas?</p>
  
  <p>//Another try:</p>
  
  <p>I told Firefox to trust the certificate and now I get </p>
  
  <pre><code>Connection from: ('127.0.0.1', 39248)
  Traceback (most recent call last):
    File "ser.py", line 29, in &lt;module&gt;
      dataBuf = connstream.recv(44096) # read what is there to read
    File "/usr/lib/python2.7/ssl.py", line 241, in recv
      return self.read(buflen)
    File "/usr/lib/python2.7/ssl.py", line 160, in read
      return self._sslobj.read(len)
  ssl.SSLError: [Errno 1] _ssl.c:1359: error:14094412:SSL routines:SSL3_READ_BYTES:sslv3 alert bad certificate
  </code></pre>
  
  <p>Searches for this give nothing!    </p>
  
  <p>//Follow up:</p>
  
  <p>Regenerated keys and cert using <a href="http://devsec.org/info/ssl-cert.html" rel="nofollow noreferrer">http://devsec.org/info/ssl-cert.html</a>, also put as the Common Name <code>localhost</code>. I get connection, but it server gets stuck and never replies... Could there be some problem in the read part? </p>