用于在python中控制SSL握手的模块?

是否有一个模块来控制
python中的SSL握手,客户端和服务器? python默认SSL模块很棒但是会自动进行握手.我想知道是否有一个模块允许我这样做手动:

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

我希望本例中使用的命名约定可以帮助您理解我正在尝试完成的任务.我对SSL的大部分知识来自This Article.我曾尝试自己编写但失败了,我似乎无法找到任何允许我这样做的模块.

最佳答案 SSL / TLS有几种纯python实现.其中任何一个都允许你这样做:

> https://github.com/pyca/tls
> https://github.com/DinoTools/python-flextls
> https://github.com/tomato42/tlslite-ng(保持https://github.com/trevp/tlslite的分店)

据我了解您的问题,您的目标是提高您对协议的理解.我个人会将后者用于此目的,因为它具有广泛的内联文档. tlslite.tlsconnection.handshakeClientAnonymous是您调查的良好起点,该函数最终调用_handshakeClientAsyncHelper来执行实际握手.

点赞