Last active
August 14, 2021 14:57
-
-
Save Kaezon/b2523e111929b9c0f6fd8dd6b21404dc to your computer and use it in GitHub Desktop.
A framework for testing SSL in a threaded environment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""A framework for testing SSL in a threaded environment. | |
This script is built with a testing certificate authority in mind. | |
For this reason, I reccomend building this script into a derrivative | |
of my docker-python-ssl image for testing: | |
https://github.com/Kaezon/docker-python-ssl | |
The MIT License (MIT) | |
Copyright © 2021 Brett Costabile | |
Permission is hereby granted, free of charge, to any person obtaining a | |
copy of this software and associated documentation files | |
(the “Software”), to deal in the Software without restriction, | |
including without limitation the rights to use, copy, modify, merge, | |
publish, distribute, sublicense, and/or sell copies of the Software, | |
and to permit persons to whom the Software is furnished to do so, | |
subject to the following conditions: | |
The above copyright notice and this permission notice shall be included | |
in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS | |
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | |
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | |
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | |
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
""" | |
import socket | |
import ssl | |
from threading import Thread | |
# Set values for sockets and test timeout | |
thread_timeout = 60 | |
hostname = 'test-server' | |
port = 8443 | |
# Certificate paths | |
path_root_cert = '/var/certs/testCA.pem' | |
path_server_cert = '/var/certs/server.crt' | |
path_server_key = '/var/certs/server.key' | |
# Define client and server functions | |
def run_client(server_hostname: str, | |
server_port: int, | |
path_root_cert: str) -> None: | |
"""Connect to a server using SSL. | |
This function will attempt to connect to an SSL socket and then | |
immediately return. | |
Args: | |
server_hostname (str): The hostname to connect to | |
server_port (int): The port to connect to | |
path_root_cert (str): Path to the root certificate | |
""" | |
print(f'Attempting to connect to {server_hostname} on port {server_port}') | |
context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) | |
context.load_verify_locations('/var/certs/testCA.pem') | |
with socket.create_connection((server_hostname, server_port)) as sock: | |
with context.wrap_socket( | |
sock, server_hostname=server_hostname) as ssock: | |
print('Client connected') | |
def run_server(hostname: str, | |
port: int, | |
path_server_cert: str, | |
path_server_key: str) -> None: | |
"""Listen for SSL connections. | |
This function will wait indenfinately for a connection. Once a | |
client connects, the connection's origin address will be printed, | |
then the function will return. | |
Args: | |
hostname (str): The hostname to bind to | |
port (int): The port to bind to | |
path_server_cert (str): Path to the server's certificate | |
path_server_key (str): Path to the server's private key | |
""" | |
print(f'Starting server on port {port}, ' | |
f'listening for connections to {hostname}') | |
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) | |
context.load_cert_chain('/var/certs/server.crt', | |
'/var/certs/server.key') | |
with socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) as sock: | |
sock.bind((hostname, port)) | |
sock.listen(5) | |
with context.wrap_socket(sock, server_side=True) as ssock: | |
conn, addr = ssock.accept() | |
print(f'Server got connection from {addr}') | |
# Instantiate Thread objects | |
server_thread = Thread( | |
target=run_server, | |
args=(hostname, port, path_server_cert, path_server_key), | |
daemon=False) | |
client_thread = Thread( | |
target=run_client, | |
args=(hostname, port, path_root_cert), | |
daemon=False) | |
# Start threads | |
server_thread.start() | |
client_thread.start() | |
# Wait for server thread to stop or time out | |
server_thread.join(timeout=thread_timeout) | |
if server_thread.is_alive(): | |
print(f'Server thread timed out after {thread_timeout}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment