From 317a4fd895128ec8d3371eb3416110e7f17d8b5c Mon Sep 17 00:00:00 2001 From: Gh0stReaper123 Date: Sat, 27 Jul 2019 14:18:30 +0100 Subject: [PATCH] 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. --- SecureSocketService/__init__.py | 8 +++++++- setup.py | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/SecureSocketService/__init__.py b/SecureSocketService/__init__.py index a04b0eb..a7db399 100644 --- a/SecureSocketService/__init__.py +++ b/SecureSocketService/__init__.py @@ -5,7 +5,7 @@ from cryptography.hazmat.primitives import serialization from cryptography.hazmat.primitives import hashes from cryptography.hazmat.primitives.asymmetric import padding from cryptography.fernet import Fernet - +import ssl class Socket: """Self @@ -126,6 +126,9 @@ class Socket: def connect_server(self, host: str, port: int, encryption=True): """self, sock (socket), host (str), port (int), service_id (int), encryption (bool) 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 self.socket.connect((host, port)) except socket.error: @@ -142,6 +145,9 @@ class Socket: def connect_client(self, encryption=True): """self, sock (socket), service_id (int), encryption (bool) 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 if encryption: self.set_secure_connexion(connexion) diff --git a/setup.py b/setup.py index e6b0e20..771b24b 100644 --- a/setup.py +++ b/setup.py @@ -22,6 +22,6 @@ setup(name="SecureSocketService", license="MIT", packages=find_packages(), install_requires=[ - 'cryptography', + 'cryptography','ssl' ], zip_safe=False)