Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement STARTTLS event; Fix #113 #214

Merged
merged 1 commit into from Jan 30, 2017
Merged

Implement STARTTLS event; Fix #113 #214

merged 1 commit into from Jan 30, 2017

Conversation

spaceone
Copy link
Contributor

from circuits import Component, Debugger
from circuits.net.sockets import TCPServer, write, starttls

class TLSEchoServer(Component):

    def init(self, bind):
        self.transport = TCPServer(bind,
            certfile="/circuits/tests/net/cert.pem",
        ).register(self)

    def connect(self, sock, foo, bar):
        self.fire(write(sock, b'* OK [CAPABILITY STARTTLS] Dovecot ready.\r\n'))

    def read(self, sock, data):
        if data.strip().upper() == b". STARTTLS":
            yield self.fire(write(sock, b'. OK Begin TLS negotiation now.'))
            self.fire(starttls(sock))
            return
        if data.strip() == b'. CAPABILITY':
            self.fire(write(sock, b'* CAPABILITY STARTTLS \r\n. OK Pre-login capabilities listed, post-login capabilities have more.\r\n'))
        else:
            self.fire(write(sock, b'Thanks for TLS encrypting! You wrote: %r' % (data,)))

(TLSEchoServer(("0.0.0.0", 8000)) + Debugger()).run()

$ openssl s_client -connect localhost:8000 -starttls imap

sock.setblocking(False)
self._poller.addReader(self, sock)
self._clients.append(sock)
self.fire(connect(sock, *sock.getpeername()))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add check for already started starttls and don't fire connect again.

def starttls(self, sock):
if not HAS_SSL:
return
if self.__starttls:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, this seems wrong. Should be a dict of sock -> bool.

@spaceone spaceone force-pushed the starttls branch 2 times, most recently from 7853628 to f8edea5 Compare January 24, 2017 09:38
@prologic
Copy link
Member

Nice!

@handler('starttls')
def starttls(self, sock):
if not HAS_SSL:
return # TODO: emit a warning?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just raise an appropriate exception maybe?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@prologic Done that.

@prologic
Copy link
Member

Rebase?

@codecov-io
Copy link

codecov-io commented Jan 29, 2017

Current coverage is 77.10% (diff: 60.97%)

No coverage report found for master at be3405a.

Powered by Codecov. Last update be3405a...0c8e9dd

@spaceone spaceone force-pushed the starttls branch 2 times, most recently from 0c8e9dd to 27c689e Compare January 29, 2017 12:50
from circuits import Component, Debugger
from circuits.net.sockets import TCPServer, write
from circuits.net.events import starttls

class TLSEchoServer(Component):

    def init(self, bind):
        self.transport = TCPServer(bind,
            certfile="/circuits/tests/net/cert.pem",
        ).register(self)

    def connect(self, sock, foo, bar):
        self.fire(write(sock, b'* OK [CAPABILITY STARTTLS] Dovecot ready.\r\n'))

    def read(self, sock, data):
        if data.strip().upper() == b". STARTTLS":
            yield self.fire(write(sock, b'. OK Begin TLS negotiation now.'))
            self.fire(starttls(sock))
            return
        if data.strip() == b'. CAPABILITY':
            self.fire(write(sock, b'* CAPABILITY STARTTLS \r\n. OK Pre-login capabilities listed, post-login capabilities have more.\r\n'))
        else:
            self.fire(write(sock, b'Thanks for TLS encrypting! You wrote: %r' % (data,)))

(TLSEchoServer(("0.0.0.0", 8000)) + Debugger()).run()

$ openssl s_client -connect localhost:8000 -starttls imap
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants