def should_read(self): # type: () -> int """ Returns whether the cause of the condition is the bio should read more data """ return m2.bio_should_read(self.bio)
def _is_read_connected(self): """ Returns True if we're interested in read events. """ # While doing initial handshake from ClientHello, we are interested # in read events internally, even if we have no listeners. should_read = m2.bio_should_read(self._bio_ssl.obj) if self._bio_ssl else False return should_read or self._handshake or super(M2TLSSocket, self)._is_read_connected()
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 _is_read_connected(self): """ Returns True if we're interested in read events. """ # While doing initial handshake from ClientHello, we are interested # in read events internally, even if we have no listeners. should_read = m2.bio_should_read( self._bio_ssl.obj) if self._bio_ssl else False return should_read or self._handshake or super( M2TLSSocket, self)._is_read_connected()
def should_read(self): # type: () -> int """Should we read more data?""" return m2.bio_should_read(self.bio)
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)