The cipher() method of SSLSocket class

The method cipher() of SSLSocket class in Python returns a tuple consisting of the name of the cipher, the version of the TLS protocol and the number of secret bits used.

Sslsocket Updated 4 Apr 2020 1 min read

Overview

  • Between the two ends points of a communication which is connection oriented the TLS protocol provides privacy, message integrity and authentication services.
  • The instance method cipher() of the SSLSocket class returns the following information as a tuple
    • The cipher (i.e, The cryptographic methodology) used for the communication
    • The version of the TLS (also still called as SSL) protocol
    • Number of secret bits used

Example

python
# Example Python program that gets the cipher information

            # used in a secure communication through SSLScoket

            import socket

            import ssl

            import platform

# SSL context related

            contextObject               = ssl.create_default_context()

            contextObject.verify_mode   = ssl.CERT_REQUIRED

# Check for OS X platform

            if platform.system().lower() == 'darwin':

                import certifi

                import os

    # Load the CA certificates used for validating the peer's certificate

                contextObject.load_verify_locations(

                    cafile = os.path.relpath(certifi.where()),

                    capath = None,

                    cadata = None)

# Make a secure socket to connect to the server

            socketObject  = socket.socket()

            secureSocket  = contextObject.wrap_socket(socketObject,

                                                      server_hostname="example.org")

# Connect to the server

            secureSocket.connect(("example.com", 443))

            cipher = secureSocket.cipher()

print("Name of the cipher:%s"%cipher[0])

            print("TLS/SSL version:%s"%cipher[1])

print("Number of secret bits:%s"%cipher[2])

Output

Output
Name of the cipher:TLS_AES_256_GCM_SHA384

            TLS/SSL version:TLSv1.3

            Number of secret bits:256