TLS echo server and echoclient in python crashes

client-server, python, sockets, ssl

Solution

openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout my.key -out my.crt

Problem

I wrote a simple echo server client code in Python. I also generated keyfile.pem and certfile.pem, with commands: `openssl genrsa -des3 -out keyfile.pem 2048` and `openssl req -new -key keyfile.pem -out certfile.pem` when I ran client server, it asked me about passphase: `Enter PEM pass phrase:` and I enterec correct text and got errors (dont really know why): ``` Traceback (most recent call last): File "echo_server.py", line 19, in <module> connection, client_address= tls_server.accept() File "/usr/lib/python2.7/ssl.py", line 354, in accept suppress_ragged_eofs=self.suppress_ragged_eofs), File "/usr/lib/python2.7/ssl.py", line 141, in __init__ ciphers) ssl.SSLError: [Errno 336445449] _ssl.c:365: error:140DC009:SSL routines:SSL_CTX_use_certificate_chain_file:PEM lib ``` Heres my server.py: ``` #server side # echo client from socket import * from ssl import * #create socket server_socket=socket(AF_INET, SOCK_STREAM) #Bind to an unused port on the local machine server_socket.bind(('localhost',6668)) #listen for connection server_socket.listen (1) tls_server = wrap_socket(server_socket, ssl_version=PROTOCOL_TLSv1, cert_reqs=CERT_NONE, server_side=True, keyfile='./keyfile.pem', certfile='./certfile.pem') print('server started') #accept connection connection, client_address= tls_server.accept() print ('connection from', client_address) #server is not finnished finnished =False #while not finnished while not finnished: #send and receive data from the client socket data_in=connection.recv(1024) message=data_in.decode() print('client send',message) if message=='quit': finnished= True else: data_out=message.encode() connection.send(data_out) #close the connection connection.shutdown(SHUT_RDWR) connection.close() #close the server socket server_socket.shutdown(SHUT_RDWR) server_socket.close() ``` and client.py: ``` #client side # echo client from socket import * from ssl import * #user is not finnished finnished =False #create socket client_socket=socket(AF_INET, SOCK_STREAM) tls_client = wrap_socket(client_socket, ssl_version=PROTOCOL_TLSv1, cert_reqs=CERT_NONE) #connect to the echo server tls_client.connect(('localhost',6668)) #while not finnished while not finnished: #message message=input ('enter message: ') data_out= message.encode () #send data out tls_client.send(data_out) #receive data data_in=tls_client.recv(1024) #decode message response= data_in.decode() print('Received from client:', response) reapet=input('yes or no? ') if reapet == 'n': finnished= True client_socket.send(b'quit') #close the socket client_socket.shutdown(SHUT_RDWR) client_socket.close() ``` What might be wrong? I use Kubuntu 12.04 LTS and Python 2.7.

Original source