def _copy_server(self): source = self._sock self._udp_demux = source._udp_demux rsock = self._udp_demux.get_connection(source._pending_peer_address) self._ctx = source._ctx self._ssl = source._ssl new_source_wbio = _BIO( BIO_new_dgram(source._sock.fileno(), BIO_NOCLOSE)) if hasattr(source, "_rsock"): self._sock = source._sock self._rsock = rsock self._wbio = _BIO(BIO_new_dgram(self._sock.fileno(), BIO_NOCLOSE)) self._rbio = _BIO(BIO_new_dgram(rsock.fileno(), BIO_NOCLOSE)) new_source_rbio = _BIO( BIO_new_dgram(source._rsock.fileno(), BIO_NOCLOSE)) BIO_dgram_set_peer(self._wbio.value, source._pending_peer_address) else: self._sock = rsock self._wbio = _BIO(BIO_new_dgram(self._sock.fileno(), BIO_NOCLOSE)) self._rbio = self._wbio new_source_rbio = new_source_wbio BIO_dgram_set_connected(self._wbio.value, source._pending_peer_address) source._ssl = _SSL(SSL_new(self._ctx.value)) self._intf_ssl = SSL(source._ssl.value) SSL_set_accept_state(source._ssl.value) if self._user_config_ssl: self._user_config_ssl(self._intf_ssl) source._rbio = new_source_rbio source._wbio = new_source_wbio SSL_set_bio(source._ssl.value, new_source_rbio.value, new_source_wbio.value) new_source_rbio.disown() new_source_wbio.disown()
def _copy_server(self): source = self._sock self._udp_demux = source._udp_demux rsock = self._udp_demux.get_connection(source._pending_peer_address) self._ctx = source._ctx self._ssl = source._ssl new_source_wbio = _BIO(BIO_new_dgram(source._sock.fileno(), BIO_NOCLOSE)) if hasattr(source, "_rsock"): self._sock = source._sock self._rsock = rsock self._wbio = _BIO(BIO_new_dgram(self._sock.fileno(), BIO_NOCLOSE)) self._rbio = _BIO(BIO_new_dgram(rsock.fileno(), BIO_NOCLOSE)) new_source_rbio = _BIO(BIO_new_dgram(source._rsock.fileno(), BIO_NOCLOSE)) BIO_dgram_set_peer(self._wbio.value, source._pending_peer_address) else: self._sock = rsock self._wbio = _BIO(BIO_new_dgram(self._sock.fileno(), BIO_NOCLOSE)) self._rbio = self._wbio new_source_rbio = new_source_wbio BIO_dgram_set_connected(self._wbio.value, source._pending_peer_address) source._ssl = _SSL(SSL_new(self._ctx.value)) self._intf_ssl = SSL(source._ssl.value) SSL_set_accept_state(source._ssl.value) if self._user_config_ssl: self._user_config_ssl(self._intf_ssl) source._rbio = new_source_rbio source._wbio = new_source_wbio SSL_set_bio(source._ssl.value, new_source_rbio.value, new_source_wbio.value) new_source_rbio.disown() new_source_wbio.disown()
def _reconnect_unwrapped(self): source = self._sock self._sock = source._wsock self._udp_demux = source._demux self._rsock = source._rsock self._ctx = source._ctx self._wbio = _BIO(BIO_new_dgram(self._sock.fileno(), BIO_NOCLOSE)) self._rbio = _BIO(BIO_new_dgram(self._rsock.fileno(), BIO_NOCLOSE)) BIO_dgram_set_peer(self._wbio.value, source._peer_address) self._ssl = _SSL(SSL_new(self._ctx.value)) SSL_set_accept_state(self._ssl.value) if self._do_handshake_on_connect: return lambda: self.do_handshake()
def _init_server(self, peer_address): if self._sock.type != socket.SOCK_DGRAM: raise InvalidSocketError("sock must be of type SOCK_DGRAM") self._wbio = _BIO(BIO_new_dgram(self._sock.fileno(), BIO_NOCLOSE)) if peer_address: # Connect directly to this client peer, bypassing the demux rsock = self._sock BIO_dgram_set_connected(self._wbio.value, peer_address) else: from demux import UDPDemux self._udp_demux = UDPDemux(self._sock) rsock = self._udp_demux.get_connection(None) if rsock is self._sock: self._rbio = self._wbio else: self._rsock = rsock self._rbio = _BIO(BIO_new_dgram(self._rsock.fileno(), BIO_NOCLOSE)) server_method = DTLS_server_method if self._ssl_version == PROTOCOL_DTLSv1_2: server_method = DTLSv1_2_server_method elif self._ssl_version == PROTOCOL_DTLSv1: server_method = DTLSv1_server_method self._ctx = _CTX(SSL_CTX_new(server_method())) self._intf_ssl_ctx = SSLContext(self._ctx.value) SSL_CTX_set_session_cache_mode(self._ctx.value, SSL_SESS_CACHE_OFF) if self._cert_reqs == CERT_NONE: verify_mode = SSL_VERIFY_NONE elif self._cert_reqs == CERT_OPTIONAL: verify_mode = SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE else: verify_mode = SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE | \ SSL_VERIFY_FAIL_IF_NO_PEER_CERT self._config_ssl_ctx(verify_mode) if not peer_address: # Configure UDP listening socket self._listening = False self._listening_peer_address = None self._pending_peer_address = None self._cb_keepalive = SSL_CTX_set_cookie_cb( self._ctx.value, _CallbackProxy(self._generate_cookie_cb), _CallbackProxy(self._verify_cookie_cb)) self._ssl = _SSL(SSL_new(self._ctx.value)) self._intf_ssl = SSL(self._ssl.value) SSL_set_accept_state(self._ssl.value) if peer_address and self._do_handshake_on_connect: return lambda: self.do_handshake()
def _test_decode_cert(cert_filename): """format_cert testing Test the certificate conversion functionality with a PEM-encoded X509 certificate. """ cert_file = _BIO(BIO_new_file(cert_filename, "rb")) cert = _X509(PEM_read_bio_X509_AUX(cert_file.value)) return decode_cert(cert)
def _init_client(self, peer_address): if self._sock.type != socket.SOCK_DGRAM: raise InvalidSocketError("sock must be of type SOCK_DGRAM") self._wbio = _BIO(BIO_new_dgram(self._sock.fileno(), BIO_NOCLOSE)) self._rbio = self._wbio self._ctx = _CTX(SSL_CTX_new(DTLSv1_client_method())) if self._cert_reqs == CERT_NONE: verify_mode = SSL_VERIFY_NONE else: verify_mode = SSL_VERIFY_PEER self._config_ssl_ctx(verify_mode) self._ssl = _SSL(SSL_new(self._ctx.value)) SSL_set_connect_state(self._ssl.value) if peer_address: return lambda: self.connect(peer_address)
def _init_client(self, peer_address): if self._sock.type != socket.SOCK_DGRAM: raise InvalidSocketError("sock must be of type SOCK_DGRAM") self._wbio = _BIO(BIO_new_dgram(self._sock.fileno(), BIO_NOCLOSE)) self._rbio = self._wbio client_method = DTLSv1_2_client_method # no "any" exists, therefore use v1_2 (highest possible) if self._ssl_version == PROTOCOL_DTLSv1_2: client_method = DTLSv1_2_client_method elif self._ssl_version == PROTOCOL_DTLSv1: client_method = DTLSv1_client_method self._ctx = _CTX(SSL_CTX_new(client_method())) self._intf_ssl_ctx = SSLContext(self._ctx.value) if self._cert_reqs == CERT_NONE: verify_mode = SSL_VERIFY_NONE else: verify_mode = SSL_VERIFY_PEER self._config_ssl_ctx(verify_mode) self._ssl = _SSL(SSL_new(self._ctx.value)) self._intf_ssl = SSL(self._ssl.value) SSL_set_connect_state(self._ssl.value) if peer_address: return lambda: self.connect(peer_address)
def ASN1_TIME_print(asn1_time): bio = _BIO(BIO_new(BIO_s_mem())) _ASN1_TIME_print(bio.value, asn1_time) return BIO_gets(bio.value)
def GENERAL_NAME_print(general_name): bio = _BIO(BIO_new(BIO_s_mem())) _GENERAL_NAME_print(bio.value, general_name) return BIO_gets(bio.value)
def i2d_X509(x509): bio = _BIO(BIO_new(BIO_s_mem())) _i2d_X509_bio(bio.value, x509) return BIO_get_mem_data(bio.value)