def startTLS(self, ctx):
        """
        Start SSL/TLS. If this is not called, this instance just passes data
        through untouched.
        """
        # NOTE: This method signature must match the startTLS() method Twisted
        #       expects transports to have. This will be called automatically
        #       by Twisted in STARTTLS situations, for example with SMTP.
        if self.tlsStarted:
            raise Exception, 'TLS already started'

        if debug:
            print 'TwistedProtocolWrapper.startTLS'

        self.ctx = ctx

        self.internalBio = m2.bio_new(m2.bio_s_bio())
        m2.bio_set_write_buf_size(self.internalBio, 0)
        self.networkBio = _BioProxy(m2.bio_new(m2.bio_s_bio()))
        m2.bio_set_write_buf_size(self.networkBio._ptr(), 0)
        m2.bio_make_bio_pair(self.internalBio, self.networkBio._ptr())

        self.sslBio = _BioProxy(m2.bio_new(m2.bio_f_ssl()))

        self.ssl = _SSLProxy(m2.ssl_new(self.ctx.ctx))

        if self.isClient:
            m2.ssl_set_connect_state(self.ssl._ptr())
        else:
            m2.ssl_set_accept_state(self.ssl._ptr())
            
        m2.ssl_set_bio(self.ssl._ptr(), self.internalBio, self.internalBio)
        m2.bio_set_ssl(self.sslBio._ptr(), self.ssl._ptr(), m2.bio_noclose)

        # Need this for writes that are larger than BIO pair buffers
        mode = m2.ssl_get_mode(self.ssl._ptr())
        m2.ssl_set_mode(self.ssl._ptr(),
                        mode |
                        m2.SSL_MODE_ENABLE_PARTIAL_WRITE |
                        m2.SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER)

        self.tlsStarted = 1
    def startTLS(self, ctx):
        """
        Start SSL/TLS. If this is not called, this instance just passes data
        through untouched.
        """
        # NOTE: This method signature must match the startTLS() method Twisted
        #       expects transports to have. This will be called automatically
        #       by Twisted in STARTTLS situations, for example with SMTP.
        if self.tlsStarted:
            raise Exception, 'TLS already started'

        if debug:
            print 'TwistedProtocolWrapper.startTLS'

        self.ctx = ctx

        self.internalBio = m2.bio_new(m2.bio_s_bio())
        m2.bio_set_write_buf_size(self.internalBio, 0)
        self.networkBio = m2.bio_new(m2.bio_s_bio())
        m2.bio_set_write_buf_size(self.networkBio, 0)
        m2.bio_make_bio_pair(self.internalBio, self.networkBio)

        self.sslBio = _SSLBioProxy(m2.bio_new(m2.bio_f_ssl()))

        self.ssl = _SSLProxy(m2.ssl_new(self.ctx.ctx))

        if self.isClient:
            m2.ssl_set_connect_state(self.ssl._ptr())
        else:
            m2.ssl_set_accept_state(self.ssl._ptr())

        m2.ssl_set_bio(self.ssl._ptr(), self.internalBio, self.internalBio)
        m2.bio_set_ssl(self.sslBio._ptr(), self.ssl._ptr(), m2.bio_noclose)

        # Need this for writes that are larger than BIO pair buffers
        mode = m2.ssl_get_mode(self.ssl._ptr())
        m2.ssl_set_mode(
            self.ssl._ptr(), mode | m2.SSL_MODE_ENABLE_PARTIAL_WRITE
            | m2.SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER)

        self.tlsStarted = 1
Пример #3
0
Файл: m2.py Проект: clones/kaa
        #     ||    \/            |
        #    Application buffer <===> TLS read/write/etc
        #                         |     /\    ||
        #                         |     ||    \/
        #                         |   BIO pair (internal_bio)
        #                         |   BIO pair (network_bio)
        #                         |     /\    ||
        #                         |     ||    \/
        #    socket read/write  <===> BIO read/write
        #     /\    ||            |
        #     ||    \/            |
        #     network             |
        #
        # [From http://www.mail-archive.com/[email protected]/msg57297.html]

        bio_internal = m2.bio_new(m2.bio_s_bio())
        bio_network = m2.bio_new(m2.bio_s_bio())
        self._m2_check_err(m2.bio_make_bio_pair(bio_internal, bio_network))
        self._bio_network = _BIOWrapper(bio_network, self._ssl)

        self._bio_ssl = _BIOWrapper(m2.bio_new(m2.bio_f_ssl()), self._ssl)
        self._m2_check_err(m2.ssl_set_bio(self._ssl.obj, bio_internal, bio_internal))
        self._m2_check_err(m2.bio_set_ssl(self._bio_ssl.obj, self._ssl.obj, m2.bio_noclose))

        # Need this for writes that are larger than BIO pair buffers
        mode = m2.ssl_get_mode(self._ssl.obj)
        mode |= m2.SSL_MODE_ENABLE_PARTIAL_WRITE | m2.SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER
        self._m2_check_err(m2.ssl_set_mode(self._ssl.obj, mode))

        self._tls_started = True
        self._starttls_kwargs = kwargs
Пример #4
0
        #     ||    \/            |
        #    Application buffer <===> TLS read/write/etc
        #                         |     /\    ||
        #                         |     ||    \/
        #                         |   BIO pair (internal_bio)
        #                         |   BIO pair (network_bio)
        #                         |     /\    ||
        #                         |     ||    \/
        #    socket read/write  <===> BIO read/write
        #     /\    ||            |
        #     ||    \/            |
        #     network             |
        #
        # [From http://www.mail-archive.com/[email protected]/msg57297.html]

        bio_internal = m2.bio_new(m2.bio_s_bio())
        bio_network = m2.bio_new(m2.bio_s_bio())
        self._m2_check_err(m2.bio_make_bio_pair(bio_internal, bio_network))
        self._bio_network = _BIOWrapper(bio_network, self._ssl)

        self._bio_ssl = _BIOWrapper(m2.bio_new(m2.bio_f_ssl()), self._ssl)
        self._m2_check_err(
            m2.ssl_set_bio(self._ssl.obj, bio_internal, bio_internal))
        self._m2_check_err(
            m2.bio_set_ssl(self._bio_ssl.obj, self._ssl.obj, m2.bio_noclose))

        # Need this for writes that are larger than BIO pair buffers
        mode = m2.ssl_get_mode(self._ssl.obj)
        mode |= m2.SSL_MODE_ENABLE_PARTIAL_WRITE | m2.SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER
        self._m2_check_err(m2.ssl_set_mode(self._ssl.obj, mode))