Example #1
0
 def set_ssl(self, conn, close_flag=m2.bio_noclose):
     """
     Sets the bio to the SSL pointer which is
     contained in the connection object.
     """
     self._pyfree = 0
     m2.bio_set_ssl(self.bio, conn.ssl, close_flag)
     if close_flag == m2.bio_noclose:
         conn.set_ssl_close_flag(m2.bio_close)
Example #2
0
 def setup_ssl(self):
     # Make a BIO_s_socket.
     self.sockbio = m2.bio_new_socket(self.socket.fileno(), 0)
     # Link SSL struct with the BIO_socket.
     m2.ssl_set_bio(self.ssl, self.sockbio, self.sockbio)
     # Make a BIO_f_ssl.
     self.sslbio = m2.bio_new(m2.bio_f_ssl())
     # Link BIO_f_ssl with the SSL struct.
     m2.bio_set_ssl(self.sslbio, self.ssl, m2.bio_noclose)
    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
Example #4
0
File: c.py Project: 0xkag/M2Crypto
def c_style(HOST, PORT, req):

    # Set up SSL context.
    ctx = m2.ssl_ctx_new(m2.sslv3_method())
    m2.ssl_ctx_use_cert(ctx, 'client.pem')
    m2.ssl_ctx_use_privkey(ctx, 'client.pem')

    # Make the socket connection.
    s = socket(AF_INET, SOCK_STREAM)
    s.connect((HOST, PORT))

    # Set up the SSL connection.
    sbio = m2.bio_new_socket(s.fileno(), 0)
    ssl = m2.ssl_new(ctx)
    m2.ssl_set_bio(ssl, sbio, sbio)
    m2.ssl_connect(ssl)
    sslbio = m2.bio_new(m2.bio_f_ssl())
    m2.bio_set_ssl(sslbio, ssl, 0)

    # Push a buffering BIO over the SSL BIO.
    iobuf = m2.bio_new(m2.bio_f_buffer())
    topbio = m2.bio_push(iobuf, sslbio)

    # Send the request.
    m2.bio_write(sslbio, req)

    # Receive the response.
    while 1:
        data = m2.bio_gets(topbio, 4096)
        if not data: break
        sys.stdout.write(data)

    # Cleanup. May be missing some necessary steps. ;-|
    m2.bio_pop(topbio)
    m2.bio_free(iobuf)
    m2.ssl_shutdown(ssl)
    m2.ssl_free(ssl)
    m2.ssl_ctx_free(ctx)
    s.close()
Example #5
0
    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')

        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
Example #6
0
File: m2.py Project: clones/kaa
        #                         |     ||    \/
        #    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
        if kwargs['client']:
            self._hello()
        return self._tls_ip


    def starttls_client(self, **kwargs):
        """
Example #7
0
        #     /\    ||            |
        #     ||    \/            |
        #     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
        if kwargs['client']:
            self._hello()
        return self._tls_ip

    def starttls_client(self, **kwargs):
        """
        TODO: document me.