TLS protocol and certificate validation settings

Review TLS protocol selection and certificate validation.

Description

SSL and TLS 1.0 and 1.1 are obsolete. Set the lowest TLS version your service accepts and verify the server’s identity.

Potential impact

Obsolete protocols or a connection to an unverified server can expose sensitive data or allow it to be altered in transit.

Remediation

  • Require at least TLS 1.2 and prefer TLS 1.3 when both peers support it.
  • Clients must verify the server’s certificate chain and hostname. Servers must load their own certificate and private key into a server context.

Examples

Before

python
# Generic context without peer verification
import ssl
import socket

context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
with socket.create_connection(('example.com', 443)) as sock:
    with context.wrap_socket(sock, server_hostname='example.com') as ssock:
        print(ssock.version())

After

python
# Client TLS with certificate and hostname validation
import ssl
import socket

context = ssl.create_default_context()
context.minimum_version = ssl.TLSVersion.TLSv1_2
with socket.create_connection(('example.com', 443)) as sock:
    with context.wrap_socket(sock, server_hostname='example.com') as ssock:
        print(ssock.version())

Explanation

PROTOCOL_SSLv23 is an old name for a generic TLS context. The negotiated version depends on Python, OpenSSL and their settings. The first example skips certificate and hostname checks. The revised client uses create_default_context() for both checks and sets TLS 1.2 as the minimum.

  • The following context allows only TLS 1.3. Before connecting, confirm that both peers support it and load the trusted CAs into the context.

    python
    client_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
    client_context.minimum_version = ssl.TLSVersion.TLSv1_3
    client_context.maximum_version = ssl.TLSVersion.TLSv1_3
    

References