How to use TLS with asyncore?

The asyncore-based XMPP client opens a regular TCP connection to the XMPP server. The server indicates that this requires an encrypted connection. The client is expected to initiate TLS handshaking so that subsequent requests can be encrypted.

tlslite integrates with asyncore, but the sample code for the server (?), and I don’t understand what it does,

I'm on Python 2.5. How can I make TLS magic work?


Here is what ultimately works for me:

from tlslite.api import * def handshakeTls(self): """ Encrypt the socket using the tlslite module """ self.logger.info("activating TLS encrpytion") self.socket = TLSConnection(self.socket) self.socket.handshakeClientCert() 
+4
source share
2 answers

I made sure that, in my opinion, all the steps of tlslite-documents for working asynchronous client - I can not get it to work, because the only asynchronous client that I have at hand for configuration is an example in Python docs, which is an HTTP 1.0 client, and I believe that because of this I am trying to configure an HTTPS connection very carefully. And I don't have an asyncore XMPP client, nor any XMPP server requesting TLS to get anywhere close to your situation. Nevertheless, I decided to sacrifice the fruits of my work, because (although there may be some step), it seems that this is slightly better than yours before - I think that I am showing all the necessary steps in __init__ . BTW, I copied the pem files from the tlslite / test directory.

 import asyncore, socket from tlslite.api import * s = open("./clientX509Cert.pem").read() x509 = X509() x509.parse(s) certChain = X509CertChain([x509]) s = open("./clientX509Key.pem").read() privateKey = parsePEMKey(s, private=True) class http_client(TLSAsyncDispatcherMixIn, asyncore.dispatcher): ac_in_buffer_size = 16384 def __init__(self, host, path): asyncore.dispatcher.__init__(self) self.create_socket(socket.AF_INET, socket.SOCK_STREAM) self.connect( (host, 80) ) TLSAsyncDispatcherMixIn.__init__(self, self.socket) self.tlsConnection.ignoreAbruptClose = True handshaker = self.tlsConnection.handshakeClientCert( certChain=certChain, privateKey=privateKey, async=True) self.setHandshakeOp(handshaker) self.buffer = 'GET %s HTTP/1.0\r\n\r\n' % path def handle_connect(self): pass def handle_close(self): self.close() def handle_read(self): print self.recv(8192) def writable(self): return (len(self.buffer) > 0) def handle_write(self): sent = self.send(self.buffer) self.buffer = self.buffer[sent:] c = http_client('www.readyhosting.com', '/') asyncore.loop() 

This is a combination of the asyncore http client example in Python docs, as well as what I got from tlslite docs and was able to reverse engineer from my sources. Hope this (although incomplete / does not work) can at least advance you in your quest ...

Personally, in your place, I would rather switch from asyncore to twisted - asyncore is old and rusty, Twisted already combines a lot of juicy, useful bits (the URL that I gave is a little in the documents that already integrate TLS and XMPP for you ... )

+2
source

Definitely check out twisted and wokkel. I created tons of bots and xmpp components with it, and it was a dream.

+4
source

All Articles