Example #1
0
 def read(self, size=0):
     if not self.readable():
         raise IOError('cannot read')
     if size:
         return m2.bio_read(self.bio, size)
     else:
         return m2.bio_read(self.bio, m2.bio_ctrl_pending(self.bio))
Example #2
0
 def read(self, size=0):
     if not self.readable():
         raise IOError('cannot read')
     if size:
         return m2.bio_read(self.bio, size)
     else:
         return m2.bio_read(self.bio, m2.bio_ctrl_pending(self.bio))
    def _encrypt(self, data='', clientHello=0):
        # XXX near mirror image of _decrypt - refactor
        self.data += data
        g = m2.bio_ctrl_get_write_guarantee(self.sslBio._ptr())
        if g > 0 and self.data != '' or clientHello:
            r = m2.bio_write(self.sslBio._ptr(), self.data)
            if r <= 0:
                assert (m2.bio_should_retry(self.sslBio._ptr()))
            else:
                assert (self.checked)
                self.data = self.data[r:]

        encryptedData = ''
        while 1:
            pending = m2.bio_ctrl_pending(self.networkBio)
            if pending:
                d = m2.bio_read(self.networkBio, pending)
                if d is not None:  # This is strange, but d can be None
                    encryptedData += d
                else:
                    assert (m2.bio_should_retry(self.networkBio))
            else:
                break

        return encryptedData
Example #4
0
 def read(self, size=None):
     if not self.readable():
         raise IOError('cannot read')
     if size is None:
         buf = io.BytesIO()
         while 1:
             data = m2.bio_read(self.bio, 4096)
             if not data:
                 break
             buf.write(data)
         return buf.getvalue()
     elif size == 0:
         return ''
     elif size < 0:
         raise ValueError('read count is negative')
     else:
         return m2.bio_read(self.bio, size)
Example #5
0
 def read(self, size=None):
     if not self.readable():
         raise IOError('cannot read')
     if size is None:
         buf = StringIO()
         while 1:
             data = m2.bio_read(self.bio, 4096)
             if not data:
                 break
             buf.write(data)
         return buf.getvalue()
     elif size == 0:
         return ''
     elif size < 0:
         raise ValueError('read count is negative')
     else:
         return m2.bio_read(self.bio, size)
Example #6
0
 def read(self, size=None):
     # type: (int) -> Union[bytes, bytearray]
     if not self.readable():
         raise IOError('cannot read')
     if size is None:
         buf = bytearray()
         while 1:
             data = m2.bio_read(self.bio, 4096)
             if not data:
                 break
             buf += data
         return buf
     elif size == 0:
         return b''
     elif size < 0:
         raise ValueError('read count is negative')
     else:
         return bytes(m2.bio_read(self.bio, size))
Example #7
0
 def read(self, size=None):
     # type: (int) -> Union[bytes, bytearray]
     if not self.readable():
         raise IOError('cannot read')
     if size is None:
         buf = bytearray()
         while 1:
             data = m2.bio_read(self.bio, 4096)
             if not data:
                 break
             buf += data
         return buf
     elif size == 0:
         return b''
     elif size < 0:
         raise ValueError('read count is negative')
     else:
         return bytes(m2.bio_read(self.bio, size))
Example #8
0
File: m2.py Project: clones/kaa
    def _translate(self, write_bio, write_bio_buf, read_bio, force_write=False):
        data = []
        encrypting = write_bio is self._bio_ssl
        write_bio = write_bio.obj
        read_bio = read_bio.obj

        while True:
            writable = m2.bio_ctrl_get_write_guarantee(write_bio) > 0
            if (writable and write_bio_buf) or force_write:
                # If force_write is True, we want to start the handshake.  We call
                # bio_write() even if there's nothing in the buffer, to cause OpenSSL to
                # implicitly send the client hello.
                chunk = write_bio_buf.pop(0) if write_bio_buf else ''
                r = m2.bio_write(write_bio, chunk)
                if r <= 0:
                    # If BIO_write returns <= 0 due to an error condition, it should
                    # raise.  Otherwise we expect bio_should_retry() to return True.  Do a
                    # quick sanity check.
                    if not m2.bio_should_retry(write_bio):
                        raise TLSProtocolError('Unexpected internal state: should_retry()'
                                               'is False without error')

                    if not self._rmon.active and m2.bio_should_read(self._bio_ssl.obj):
                        # The BIO write failed, the SSL BIO is now telling us we should
                        # read, and the read monitor is not active.  Update the read
                        # monitor now, which will register with the notifier because the
                        # SSL BIO should read, allowing us to read from the socket to
                        # satisfy whatever the underlying SSL protocol is doing.
                        self._update_read_monitor()
                else:
                    if encrypting:
                        # We are encrypting user data to send to peer.  Require the
                        # remote end be validated first.  We should not normally
                        # get here until ClientHello is completed successfully.
                        assert(self._validated)
                    chunk = chunk[r:]

                if chunk:
                    # Insert remainder of chunk back into the buffer.
                    write_bio_buf.insert(0, chunk)

            pending = m2.bio_ctrl_pending(read_bio)
            if not pending:
                break

            chunk  = m2.bio_read(read_bio, pending)
            if chunk is not None:
                data.append(chunk)
            else:
                # It's possible for chunk to be None, even though bio_ctrl_pending()
                # told us there was data waiting in the BIO.  I suspect this happens
                # when all the bytes in the BIO are used for the SSL protocol and
                # none are user data.
                assert(m2.bio_should_retry(read_bio))

        return ''.join(data)
    def _decrypt(self, data=''):
        # XXX near mirror image of _encrypt - refactor
        self.encrypted += data
        g = m2.bio_ctrl_get_write_guarantee(self.networkBio)
        if g > 0 and self.encrypted != '':
            r = m2.bio_write(self.networkBio, self.encrypted)
            if r <= 0:
                assert (m2.bio_should_retry(self.networkBio))
            else:
                self.encrypted = self.encrypted[r:]

        decryptedData = ''
        while 1:
            pending = m2.bio_ctrl_pending(self.sslBio._ptr())
            if pending:
                d = m2.bio_read(self.sslBio._ptr(), pending)
                if d is not None:  # This is strange, but d can be None
                    decryptedData += d
                else:
                    assert (m2.bio_should_retry(self.sslBio._ptr()))
            else:
                break

        return decryptedData
Example #10
0
    def _translate(self,
                   write_bio,
                   write_bio_buf,
                   read_bio,
                   force_write=False):
        data = []
        encrypting = write_bio is self._bio_ssl
        write_bio = write_bio.obj
        read_bio = read_bio.obj

        while True:
            writable = m2.bio_ctrl_get_write_guarantee(write_bio) > 0
            if (writable and write_bio_buf) or force_write:
                # If force_write is True, we want to start the handshake.  We call
                # bio_write() even if there's nothing in the buffer, to cause OpenSSL to
                # implicitly send the client hello.
                chunk = write_bio_buf.pop(0) if write_bio_buf else ''
                r = m2.bio_write(write_bio, chunk)
                if r <= 0:
                    # If BIO_write returns <= 0 due to an error condition, it should
                    # raise.  Otherwise we expect bio_should_retry() to return True.  Do a
                    # quick sanity check.
                    if not m2.bio_should_retry(write_bio):
                        raise TLSProtocolError(
                            'Unexpected internal state: should_retry()'
                            'is False without error')

                    if not self._rmon.active and m2.bio_should_read(
                            self._bio_ssl.obj):
                        # The BIO write failed, the SSL BIO is now telling us we should
                        # read, and the read monitor is not active.  Update the read
                        # monitor now, which will register with the notifier because the
                        # SSL BIO should read, allowing us to read from the socket to
                        # satisfy whatever the underlying SSL protocol is doing.
                        self._update_read_monitor()
                else:
                    if encrypting:
                        # We are encrypting user data to send to peer.  Require the
                        # remote end be validated first.  We should not normally
                        # get here until ClientHello is completed successfully.
                        assert (self._validated)
                    chunk = chunk[r:]

                if chunk:
                    # Insert remainder of chunk back into the buffer.
                    write_bio_buf.insert(0, chunk)

            pending = m2.bio_ctrl_pending(read_bio)
            if not pending:
                break

            chunk = m2.bio_read(read_bio, pending)
            if chunk is not None:
                data.append(chunk)
            else:
                # It's possible for chunk to be None, even though bio_ctrl_pending()
                # told us there was data waiting in the BIO.  I suspect this happens
                # when all the bytes in the BIO are used for the SSL protocol and
                # none are user data.
                assert (m2.bio_should_retry(read_bio))

        return ''.join(data)
Example #11
0
def cmembufi(iter, txt=txt):
    buf = m2.bio_new(m2.bio_s_mem())
    for i in range(iter):
        m2.bio_write(buf, txt)
    m2.bio_set_mem_eof_return(buf, 0)
    out = m2.bio_read(buf, m2.bio_ctrl_pending(buf))
Example #12
0
if use_mem:
    bio = m2.bio_new(m2.bio_s_mem())
else:
    bio = m2.bio_new_file('XXX', 'wb')
ciph = m2.bf_cbc()
filt = m2.bio_new(m2.bio_f_cipher())
m2.bio_set_cipher(filt, ciph, 'key', 'iv', 1)
m2.bio_push(filt, bio)
m2.bio_write(filt, '12345678901234567890')
m2.bio_flush(filt)
m2.bio_pop(filt)
m2.bio_free(filt)
if use_mem:
    m2.bio_set_mem_eof_return(bio, 0)
    xxx = m2.bio_read(bio, 100)
    print `xxx`, len(xxx)
m2.bio_free(bio)

if use_mem:
    bio = m2.bio_new(m2.bio_s_mem())
    m2.bio_write(bio, xxx)
    m2.bio_set_mem_eof_return(bio, 0)
else:
    bio = m2.bio_new_file('XXX', 'rb')
ciph = m2.bf_cbc()
filt = m2.bio_new(m2.bio_f_cipher())
m2.bio_set_cipher(filt, ciph, 'key', 'iv', 0)
m2.bio_push(filt, bio)
yyy = m2.bio_read(filt, 100)
print `yyy`
Example #13
0
def cmembufi(iter, txt=txt):
    buf = m2.bio_new(m2.bio_s_mem())
    for i in range(iter):
        m2.bio_write(buf, txt)
    m2.bio_set_mem_eof_return(buf, 0)
    out = m2.bio_read(buf, m2.bio_ctrl_pending(buf))