Cipher method of SSLSocket class

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:

# 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.SSLContext();

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);

 

# 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:

Name of the cipher:TLS_AES_256_GCM_SHA384

TLS/SSL version:TLSv1.3

Number of secret bits:256

 


Copyright 2024 © pythontic.com