backports-ssl-match-hostname

3.7.0.1 · deprecated · verified Tue Apr 14

This library provides a backport of the `ssl.match_hostname()` function from Python 3.5 to earlier Python versions. It ensures proper hostname verification against SSL/TLS certificates, a critical security measure. The current version is 3.7.0.1, released in 2019. The project is considered unmaintained, as its functionality is now present in standard Python versions.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the core usage of `match_hostname()` within a simulated SSL context. In a real-world scenario, you would obtain the peer certificate from an active `sslsock` object after establishing an SSL/TLS connection. It's crucial to handle `CertificateError` to manage hostname mismatches securely.

import socket
import ssl
from backports.ssl_match_hostname import match_hostname, CertificateError

def verify_ssl_hostname(hostname: str, port: int):
    try:
        # Simulate a socket connection (replace with actual connection in real use)
        # For demonstration, we'll create a dummy context and peer certificate
        context = ssl.create_default_context()
        with socket.create_connection((hostname, port)) as sock:
            with context.wrap_socket(sock, server_hostname=hostname) as sslsock:
                cert = sslsock.getpeercert()
                match_hostname(cert, hostname)
                print(f"Hostname '{hostname}' successfully matched certificate.")
    except CertificateError as e:
        print(f"Certificate hostname mismatch for '{hostname}': {e}")
    except Exception as e:
        print(f"An error occurred: {e}")

# Example Usage (replace with a real hostname and port for actual testing)
# For a live example, you'd connect to a server with SSL.
# Using a dummy here for runnable example without actual network call or certs.
# To make it runnable for demonstration, let's just show the logic structure.

# This part is illustrative, assumes you have a 'cert' object from a real ssl connection
# For an actual runnable quickstart with mock, it's complex. 
# The essence is `match_hostname(sslsock.getpeercert(), hostname)`

# For a truly runnable example (requires a running SSL server at specified host/port)
# try:
#     hostname = "www.google.com"
#     port = 443
#     context = ssl.create_default_context()
#     with socket.create_connection((hostname, port)) as sock:
#         with context.wrap_socket(sock, server_hostname=hostname) as sslsock:
#             cert = sslsock.getpeercert()
#             match_hostname(cert, hostname)
#             print(f"Hostname '{hostname}' successfully matched certificate.")
# except CertificateError as e:
#     print(f"Certificate hostname mismatch for '{hostname}': {e}")
# except Exception as e:
#     print(f"An error occurred: {e}")

print("Consult the documentation for actual socket setup as this is a backport.")

view raw JSON →