SSL-PSK for PyMobileDevice3

1.0.3 · active · verified Thu Apr 16

sslpsk-pmd3 is a fork of the `sslpsk` library, specifically tailored to add TLS-PSK (Pre-Shared Key) support to the Python `ssl` package for `pymobiledevice3` usage. It enables secure socket communication using pre-shared keys, an alternative to traditional certificate-based TLS. The current version is 1.0.3, released on February 9, 2024, with irregular but active maintenance.

Common errors

Warnings

Install

Imports

Quickstart

This client-side example demonstrates how to establish a TLS-PSK connection using `sslpsk_pmd3.wrap_socket`. It connects to a specified host and port, wraps the socket with a pre-shared key and client identity, sends a message, and receives a response. Ensure a compatible TLS-PSK server is running for this example to fully succeed. Environment variables are used for sensitive information like PSK and connection details.

import socket
import sslpsk_pmd3
import os

# Configuration from environment variables for security and flexibility
HOST = os.environ.get('PSK_HOST', '127.0.0.1')
PORT = int(os.environ.get('PSK_PORT', 6000))
PSK_KEY = os.environ.get('PSK_KEY', 'abcdef').encode('utf-8')
CLIENT_IDENTITY = os.environ.get('PSK_IDENTITY', 'client1').encode('utf-8')

def client_example(host, port, psk_key, client_identity):
    try:
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        print(f"Attempting to connect to {host}:{port}...")
        sock.connect((host, port))
        print("Socket connected. Wrapping with TLS-PSK...")
        
        # Wrap the socket with TLS-PSK support
        # PROTOCOL_TLSv1_2 is a common, widely supported version.
        ssl_sock = sslpsk_pmd3.wrap_socket(
            sock,
            psk=psk_key,
            psk_identity=client_identity,
            ssl_version=sslpsk_pmd3.PROTOCOL_TLSv1_2
        )
        print("SSL socket wrapped. Sending data...")
        
        message = "Hello, TLS-PSK Server!\n"
        ssl_sock.sendall(message.encode())
        print(f"Client sent: {message.strip()}")
        
        received_data = ssl_sock.recv(1024).decode().strip()
        print(f"Client received: {received_data}")
        
    except ConnectionRefusedError:
        print(f"Error: Connection refused. Ensure a TLS-PSK server is running on {host}:{port}.")
    except Exception as e:
        print(f"An error occurred: {e}")
    finally:
        if 'ssl_sock' in locals() and ssl_sock:
            ssl_sock.shutdown(socket.SHUT_RDWR)
            ssl_sock.close()
        elif 'sock' in locals() and sock:
            sock.close()
        print("Connection closed.")

if __name__ == '__main__':
    print("This quickstart demonstrates client-side usage of sslpsk-pmd3.")
    print("A corresponding TLS-PSK server is required to fully execute this example.")
    print("You can configure host, port, key, and identity via PSK_HOST, PSK_PORT, PSK_KEY, PSK_IDENTITY environment variables.")
    print(f"Using defaults: Host={HOST}, Port={PORT}, PSK_KEY={'*' * len(PSK_KEY)}, PSK_IDENTITY={CLIENT_IDENTITY.decode()}")
    client_example(HOST, PORT, PSK_KEY, CLIENT_IDENTITY)

view raw JSON →