Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Module to control SSL handshake in python?

Is there a module to control the SSL handshake in python, Both client side and server? The python default SSL module is great but does the handshake automatic. I was wondering if there is a module that will allow me to do it manual similar to this:

import SSLManuel
import socket

s = socket.socket()
s.connect(("server.com",9999))

ClientHello = SSLManuel.generateClientHelloMessage(ssl=TLSv1_2, cipher="ECDHE-RSA-AES128-GCM-SHA256", server="www.server.com")
s.send(ClientHello)
ServerHello = s.recv()#this would receive the server hello
#This would verify the certificate of the server
if SSLManuel.check_cert(ServerHello) == true:
    Pre-Master-Key = SSLManuel.generatePreMasterKey()
    ClientKeyExchange = SSLManuel.generateClientKeyExchange(Pre-Master-Key)
    ChangeCiherSpec = SSLManuel.generateChangeCipherSpec()
    ClientFinished = SSLManuel.generateClientFinished()
    Sessionkey = SSLManuel.generateMasterKey(Pre-Master-Key)
    s.send(ClientKeyExchange)
    s.send(ChangeCiherSpec)
    s.send(ClientFinished)
    ServerFinished = s.recv()
    #This will check if the server is ready to communicate securely. 
    if SSLManuel.checkServerFinshed(ServerFinished) == true:
        #I can now use the SessionKey to encrypt data to and from the server
        s.send(SSLManuel.encrypt(SessionKey, "GET / HTTP/1.0\n\n"))
        response = s.recv()
        print(SSLManuel.decrypt(SessionKey, response))

I hope the naming conventions used in this example can help you understand what I'm trying to accomplish. Most of my knowledge of SSL comes from This Article. I have tried to write my own but have failed and I can't seem to find any module that will allow me to do this.

like image 502
raymon Luster Avatar asked Oct 30 '22 07:10

raymon Luster


1 Answers

There are several pure-python implementations of SSL/TLS. Any of them will allow you to do this:

  • https://github.com/pyca/tls
  • https://github.com/DinoTools/python-flextls
  • https://github.com/tomato42/tlslite-ng (maintained fork of https://github.com/trevp/tlslite)

As far as I understand your question, your aim is to improve your understanding of the protocol. I would personally use the latter for this purpose, because it has an extensive inline documentation. tlslite.tlsconnection.handshakeClientAnonymous is a good starting point for your investigation, the function eventually calls _handshakeClientAsyncHelper to perform the actual handshake.

like image 92
Phillip Avatar answered Nov 15 '22 03:11

Phillip