예제 #1
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)
예제 #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)
예제 #3
0
    def set_bio(self, readbio, writebio):
        # type: (BIO.BIO, BIO.BIO) -> None
        """Explicitly set read and write bios

        Connects the BIOs for the read and write operations of the
        TLS/SSL (encrypted) side of ssl.

        The SSL engine inherits the behaviour of both BIO objects,
        respectively. If a BIO is non-blocking, the Connection will also
        have non-blocking behaviour.

        If there was already a BIO connected to Connection, BIO_free()
        will be called (for both the reading and writing side, if
        different).

        :param readbio: BIO for reading
        :param writebio: BIO for writing.
        """
        m2.ssl_set_bio(self.ssl, readbio._ptr(), writebio._ptr())
예제 #4
0
    def set_bio(self, readbio, writebio):
        # type: (BIO.BIO, BIO.BIO) -> None
        """Explicitly set read and write bios

        Connects the BIOs for the read and write operations of the
        TLS/SSL (encrypted) side of ssl.

        The SSL engine inherits the behaviour of both BIO objects,
        respectively. If a BIO is non-blocking, the Connection will also
        have non-blocking behaviour.

        If there was already a BIO connected to Connection, BIO_free()
        will be called (for both the reading and writing side, if
        different).

        :param readbio: BIO for reading
        :param writebio: BIO for writing.
        """
        m2.ssl_set_bio(self.ssl, readbio._ptr(), writebio._ptr())
    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
예제 #7
0
파일: c.py 프로젝트: xampserver1/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()
예제 #8
0
파일: c.py 프로젝트: 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()
예제 #9
0
 def set_bio(self, readbio, writebio):
     """
     Explicitly set read and write bios
     """
     m2.ssl_set_bio(self.ssl, readbio._ptr(), writebio._ptr())
예제 #10
0
 def set_bio(self, readbio, writebio):
     """
     Explicitly set read and write bios
     """
     m2.ssl_set_bio(self.ssl, readbio._ptr(), writebio._ptr())
예제 #11
0
파일: m2.py 프로젝트: 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):
예제 #12
0
        #                         |     ||    \/
        #    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):