1
0
Fork 0
This repository has been archived on 2024-02-17. You can view files and clone it, but cannot push or open issues or pull requests.
SecureSocketService/Socket.py

144 lines
5.6 KiB
Python
Raw Normal View History

2019-07-13 13:29:31 +02:00
import socket
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import padding
from cryptography.fernet import Fernet
class Socket:
"""Self
Socket service with security system"""
2019-07-13 13:29:31 +02:00
def __init__(self):
"""Self
Socket service with security system"""
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Set the socket object
self.key = None # Set the key encryption
self.fernet = None # Set the encryption object
def send(self, sock: socket.socket, message: str, encryption=True):
"""self, sock (socket), message (str), encryption (bool)
Send a message with a specific socket, encrypted if available"""
if type(message) != bytes: # If the message are not a bytes type, encode in Utf8
2019-07-13 13:29:31 +02:00
message = message.encode("Utf8")
if self.fernet and encryption: # Encrypt if available
2019-07-13 13:29:31 +02:00
message = self.fernet.encrypt(message)
try: # Try to send, if fail raise a custom error
2019-07-13 13:29:31 +02:00
sock.send(message)
except socket.error:
raise ConnectionError("Fail to send")
else:
return True
def receive(self, sock: socket.socket, utf8=True, encryption=True):
"""self, sock (socket), utf8 (bool), encryption (bool)
Receive a message from a socket"""
try: # Try to receive, else raise a custom error
2019-07-13 13:29:31 +02:00
response = sock.recv(1028)
except socket.error:
raise ConnectionError("Fail to receive")
else:
if self.fernet and encryption: # Encrypt if available
2019-07-13 13:29:31 +02:00
response = self.fernet.decrypt(response)
if utf8: # decode the utf8 by default
2019-07-13 13:29:31 +02:00
response = response.decode("Utf8")
return response
def set_secure_connexion(self, sock: socket.socket):
"""self, sock (socket)
Set encryption for a connected socket"""
if not self.key: # If any key generated, generate one
2019-07-13 13:29:31 +02:00
self.key = Fernet.generate_key()
self.fernet = Fernet(self.key)
# Receive the public key of the targed socket
2019-07-13 13:29:31 +02:00
public_key = serialization.load_pem_public_key(
self.receive(sock, False, False),
backend=default_backend()
)
# Encrypt the key and send back with private key encryption to the target socket
2019-07-13 13:29:31 +02:00
sock.send(
public_key.encrypt(
self.key,
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None
)
)
)
if self.receive(sock) == "Key receive": # Check if the kay is valid
2019-07-13 13:29:31 +02:00
self.send(sock, "Key valid")
return True
else:
raise ConnectionError("Invalid key !")
def get_secure_connexion(self, sock: socket.socket):
"""self, sock (socket)
Get encryption for a connected socket"""
# Generate private key
2019-07-13 13:29:31 +02:00
private_key = rsa.generate_private_key(
public_exponent=65537,
key_size=2048,
backend=default_backend()
)
public_key = private_key.public_key() # Create pubic kay
2019-07-13 13:29:31 +02:00
# Make a sendable public key
2019-07-13 13:29:31 +02:00
pem = public_key.public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo
)
self.send(sock, pem) # Send the public key
2019-07-13 13:29:31 +02:00
# Decrypt the receive key with the private key and set it
2019-07-13 13:29:31 +02:00
self.key = private_key.decrypt(
self.receive(sock, False, False),
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None
)
)
self.fernet = Fernet(self.key) # Set the encryption object
2019-07-13 13:29:31 +02:00
self.send(sock, "Key receive") # Check if the key is valid
2019-07-13 13:29:31 +02:00
if self.receive(sock) != "Key valid":
raise ConnectionError("Invalid key !")
return True
def connect_server(self, sock: socket.socket, host: str, port: int, service_id: int, encryption=True):
"""self, sock (socket), host (str), port (int), service_id (int), encryption (bool)
Connect a socket to a socket server"""
try: # Try to connect, else raise a custom error
2019-07-13 13:29:31 +02:00
sock.connect((host, port))
except socket.error:
raise ConnectionError("Can't connect to server !")
else:
if encryption: # Set secure connexion is asked
self.get_secure_connexion(self.socket)
self.send(sock, f"Client service ID: {service_id}") # Check if service id match
2019-07-13 13:29:31 +02:00
if self.receive(sock) != f"Server service ID: {service_id}":
raise ConnectionError("Server service ID")
return True
def connect_client(self, sock: socket, service_id: int, encryption=True):
"""self, sock (socket), service_id (int), encryption (bool)
Connect a socket server to a socket"""
connexion, address = sock.accept() # Await for connexion
if encryption:
self.set_secure_connexion(connexion)
if self.receive(connexion) == f"Client service ID: {service_id}": # Check if service id match
2019-07-13 13:29:31 +02:00
self.send(connexion, f"Server service ID: {service_id}")
return connexion
else:
raise ConnectionError("Invalid client service ID !")