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:
parent
4a94cc3220
commit
317a4fd895
2 changed files with 8 additions and 2 deletions
|
@ -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)
|
||||
|
|
2
setup.py
2
setup.py
|
@ -22,6 +22,6 @@ setup(name="SecureSocketService",
|
|||
license="MIT",
|
||||
packages=find_packages(),
|
||||
install_requires=[
|
||||
'cryptography',
|
||||
'cryptography','ssl'
|
||||
],
|
||||
zip_safe=False)
|
||||
|
|
Reference in a new issue