1
0
Fork 0

Initial SSL Implementation (needs testing)

Added ssl wrapper to self.socket with parameters dependant on if the Socket class is waiting for connections in connect_client or if it connects as a client in connect_server.
This commit is contained in:
Gh0stReaper123 2019-07-27 14:18:30 +01:00
parent 4a94cc3220
commit 317a4fd895
2 changed files with 8 additions and 2 deletions

View file

@ -5,7 +5,7 @@ from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives import hashes from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import padding from cryptography.hazmat.primitives.asymmetric import padding
from cryptography.fernet import Fernet from cryptography.fernet import Fernet
import ssl
class Socket: class Socket:
"""Self """Self
@ -126,6 +126,9 @@ class Socket:
def connect_server(self, host: str, port: int, encryption=True): def connect_server(self, host: str, port: int, encryption=True):
"""self, sock (socket), host (str), port (int), service_id (int), encryption (bool) """self, sock (socket), host (str), port (int), service_id (int), encryption (bool)
Connect to a socket server""" Connect to a socket server"""
self.context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
self.context.load_default_certs(ssl.Purpose.SERVER_AUTH)
self.socket = self.context.wrap_socket(self.socket, server_side=False)
try: # Try to connect, else raise a custom error try: # Try to connect, else raise a custom error
self.socket.connect((host, port)) self.socket.connect((host, port))
except socket.error: except socket.error:
@ -142,6 +145,9 @@ class Socket:
def connect_client(self, encryption=True): def connect_client(self, encryption=True):
"""self, sock (socket), service_id (int), encryption (bool) """self, sock (socket), service_id (int), encryption (bool)
Connect a socket client to the server""" Connect a socket client to the server"""
self.context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
self.context.load_default_certs(ssl.Purpose.CLIENT_AUTH)
self.socket = self.context.wrap_socket(self.socket, server_side=True)
connexion, address = self.socket.accept() # Await for connexion connexion, address = self.socket.accept() # Await for connexion
if encryption: if encryption:
self.set_secure_connexion(connexion) self.set_secure_connexion(connexion)

View file

@ -22,6 +22,6 @@ setup(name="SecureSocketService",
license="MIT", license="MIT",
packages=find_packages(), packages=find_packages(),
install_requires=[ install_requires=[
'cryptography', 'cryptography','ssl'
], ],
zip_safe=False) zip_safe=False)