How to Create SSL Socket Server With Python

Viewed 558

How i Can To Create A SSL Socket Server With Using Python? I Took Ssl Files From One Site And I Want To Use Them On Another Server.Can I do This?

My SSL Files:

ca.pem: Certificate Authority Bundle

cert.crt: Certificate

key.key: Private Key

My Server Python Code:

from socket import *
import hashlib,base64,ssl,datetime

Socket = socket(2,1)
Socket.bind((IP,Port))
Socket.listen()

print("Service Started on "+IP+":"+str(Port))

while True:
    Connection,Address = Socket.accept()
    Secure_Connection = ssl.wrap_socket(Connection, server_side=True, ca_certs="ca.pem", certfile="cert.crt", keyfile="key.key", cert_reqs=ssl.CERT_REQUIRED, ssl_version=ssl.PROTOCOL_TLSv1_2);
    
    # Get certificate from the client
    Client_Cert = Secure_Connection.getpeercert();
    
    CLT_Subject    = dict(item[0] for item in Client_Cert['subject']);
    CLT_Common_Name = CLT_Subject['commonName'];

    # Check the client certificate bears the expected name as per server's policy
    if not Client_Cert:
        raise Exception("Unable to get the certificate from the client");
        
    if CLT_Common_Name != 'DemoClt':
        raise Exception("Incorrect common name in client certificate");

    # Check time validity of the client certificate
    T1  = ssl.cert_time_to_seconds(Client_Cert['notBefore']);
    T2  = ssl.cert_time_to_seconds(Client_Cert['notAfter']);
    TS  = time.time();

    if TS < T1:
        raise Exception("Client certificate not yet active");
        
    if TS > T2:
        raise Exception("Expired client certificate");

    # Send current server time to the client
    Time = "%s"%datetime.datetime.now();
    Secure_Connection.send(Time.encode());
    print("Securely sent %s to %s"%(Time,Address[0]));

    # Close the connection to the client
    Secure_Connection.close();

But i Get This SSL Error:

Secure_Connection = ssl.wrap_socket(Connection, server_side=True, ca_certs="ca.pem", certfile="cert.crt", keyfile="key.key", cert_reqs=ssl.CERT_REQUIRED, ssl_version=ssl.PROTOCOL_TLSv1_2);
File "C:\Program Files\Python39\lib\ssl.py", line 1405, in wrap_socket
return context.wrap_socket(
File "C:\Program Files\Python39\lib\ssl.py", line 500, in wrap_socket
return self.sslsocket_class._create(
File "C:\Program Files\Python39\lib\ssl.py", line 1040, in _create
self.do_handshake()
File "C:\Program Files\Python39\lib\ssl.py", line 1309, in do_handshake
self._sslobj.do_handshake()
ssl.SSLError: [SSL: SSLV3_ALERT_CERTIFICATE_UNKNOWN] sslv3 alert certificate unknown (_ssl.c:1129)

What is The Reason For This Error?

1 Answers

This module provides encryption and peer authentication functionality for network sockets based on the Transport Layer Security (often called Secure Sockets Layer). OpenSSL is used to implement the module. Almost all modern Unix systems, Windows, macOS, and probably other platforms, assuming that OpenSSL is installed on the platform.

Please be aware: Due to the use of the operating system socket APIs, some behavior might be platform-dependent. Installation of different versions of OpenSSL may also alter behavior. A good example would be TLSv1.3 with OpenSSL version 1.1.1.

Note: This module needs to be read carefully before being used. Default SSL module settings might give the wrong impression of security, as your application might not be suitable for those settings. It explains in this section the objects and functions contained within the SSL module. SSLSocket is derived from socket.socket and provides a socket-like wrapper that offers SSL encryption and decryption of data inside the socket. Get peer certificates from the other side of the connection with getpeercert() or obtain the cipher being used for a secure connection with cipher() by utilizing the cipher() method. Through the wrap_socket() method of SSLContext, sophisticated applications can inherit SSL settings and certificates. SSL.SSLContext is one of the classes that are employed to manage SSL settings and certificates. Linking with OpenSSL 1.1.0 has been improved in version 3.5.3 OpenSSL versions 0.9.8, 1.0.0 and 1.0.1 have been deprecated. It will be required that at least OpenSSL 1.0.2 or 1.1.0 is installed in the future for SSL to work. Update for version 3.10: PEP 644 has been implemented. An OpenSSL version of 1.1.1 is required for the SSL module. There are deprecation warnings for constants and functions that are deprecated.

Related