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
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 should_retry(self): """ Can the call be attempted again, or was there an error ie do_handshake """ return m2.bio_should_retry(self.bio)
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
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)