ssl server and client using python - client is closing the connection due to exception

不打扰是莪最后的温柔 提交于 2021-01-29 05:42:54

问题


I am newbie to socket and ssl programming in python. Current requirement is that, Client and server shares the pre-shared key so the certificate exchange,authentication and verifying is not needed in both ends. So,during handshake mechanism, there is no need to authenticate the server from client part but the minimal check is that Cipher suites needs to be send along with random number from client is > TLS_PSK_WITH_SHA256 and will be acknowledged by server. In this case, i have created a custom socket instead of Create_default_socket() and passed the parameters TLSv1.2,CERT_NONE and same in client side as well. But,when the client is executed, connection is refused due to exception. Can you point the error in below code , it would certainly help me a lot. (For testing, i am using HOST - localhost and port - 4443 )

Server code:

context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
import socket, ssl
HOST = '127.0.0.1'
PORT = 4443
def handle(conn):
  #print(conn.recv())
  print("entered to handle case")
def main():
  context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
  context.options |= ssl.OP_NO_TLSv1 | ssl.OP_NO_TLSv1_1  # optional
  context.check_hostname = False
  context.set_ciphers('TLS_PSK_WITH_NULL_SHA256')
  context.verify_mode=ssl.CERT_NONE
  sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
  sock.bind((HOST, PORT))
  sock.listen(5)
  while True:
    conn = None
    try:
        ssock = context.wrap_socket(sock,server_side=True,
                                   do_handshake_on_connect=False,
                                   suppress_ragged_eofs=False,
                                   session=None
                                   )
        print("created wrap socket")
        conn, addr = ssock.accept()
        handle(conn)
    except ssl.SSLError as e:
        print(e)
    finally:
        if conn:
            conn.close()

print("Executing server main fn call") main()

Client code:

context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
context.check_hostname=False
context.options |= ssl.OP_NO_TLSv1 | ssl.OP_NO_TLSv1_1  # optional
context.verify_mode =ssl.CERT_NONE
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
conn = context.wrap_socket(sock,
                           server_side=False,
                           do_handshake_on_connect=False,
                           suppress_ragged_eofs=False,
                           server_hostname=HOST)
try:
    conn.connect((HOST, PORT))
    #handle(conn)
except ssl.SSLError as e:
    print(e)
finally:
    if conn:
        conn.close()
        print("client is closed due to exception")

Getting Errors in server side : if sock.getsockopt(SOL_SOCKET, SO_TYPE) != SOCK_STREAM: OSError: [WinError 10038] An operation was attempted on something that is not a socket

Error in Client side - connection is closed due to exception.

来源:https://stackoverflow.com/questions/62147261/ssl-server-and-client-using-python-client-is-closing-the-connection-due-to-exc

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!