python ssl模块用法详解_用于在python中控制SSL握手的模块?

  • Post author:
  • Post category:python


是否有一个模块来控制

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实现.其中任何一个都允许你这样做:

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



版权声明:本文为weixin_33654339原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。