diff --git a/kmip/services/kmip_client.py b/kmip/services/kmip_client.py index 7f72adf7..438391ab 100644 --- a/kmip/services/kmip_client.py +++ b/kmip/services/kmip_client.py @@ -285,13 +285,17 @@ def open(self): six.reraise(*last_error) def _create_socket(self, sock): - self.socket = ssl.wrap_socket( + context = ssl.SSLContext(self.ssl_version) + context.verify_mode = self.cert_reqs + if self.ca_certs: + context.load_verify_locations(self.ca_certs) + if self.keyfile and not self.certfile: + raise ValueError("certfile must be specified") + if self.certfile: + context.load_cert_chain(self.certfile, self.keyfile) + self.socket = context.wrap_socket( sock, - keyfile=self.keyfile, - certfile=self.certfile, - cert_reqs=self.cert_reqs, - ssl_version=self.ssl_version, - ca_certs=self.ca_certs, + server_side=False, do_handshake_on_connect=self.do_handshake_on_connect, suppress_ragged_eofs=self.suppress_ragged_eofs) self.socket.settimeout(self.timeout) diff --git a/kmip/services/server/server.py b/kmip/services/server/server.py index 534ab61d..cfbbb705 100644 --- a/kmip/services/server/server.py +++ b/kmip/services/server/server.py @@ -287,17 +287,26 @@ def interrupt_handler(trigger, frame): for cipher in auth_suite_ciphers: self._logger.debug(cipher) - self._socket = ssl.wrap_socket( + cafile = self.config.settings.get('ca_path') + context = ssl.SSLContext(self.auth_suite.protocol) + context.verify_mode = ssl.CERT_REQUIRED + if self.auth_suite.ciphers: + context.set_ciphers(self.auth_suite.ciphers) + if cafile: + context.load_verify_locations(cafile) + certfile = self.config.settings.get('certificate_path') + + if certfile: + keyfile = self.config.settings.get('key_path') + context.load_cert_chain(certfile, keyfile=keyfile) + else: + raise ValueError("certfile must be specified for server-side operations") + + self._socket = context.wrap_socket( self._socket, - keyfile=self.config.settings.get('key_path'), - certfile=self.config.settings.get('certificate_path'), server_side=True, - cert_reqs=ssl.CERT_REQUIRED, - ssl_version=self.auth_suite.protocol, - ca_certs=self.config.settings.get('ca_path'), do_handshake_on_connect=False, - suppress_ragged_eofs=True, - ciphers=self.auth_suite.ciphers + suppress_ragged_eofs=True ) try: diff --git a/kmip/tests/unit/services/server/test_server.py b/kmip/tests/unit/services/server/test_server.py index a9e9f194..0384e8fc 100644 --- a/kmip/tests/unit/services/server/test_server.py +++ b/kmip/tests/unit/services/server/test_server.py @@ -210,9 +210,9 @@ def test_start(self, # Test that in ideal cases no errors are generated and the right # log messages are. with mock.patch('socket.socket') as socket_mock: - with mock.patch('ssl.wrap_socket') as ssl_mock: + with mock.patch('ssl.SSLContext') as ssl_mock: socket_mock.return_value = a_mock - ssl_mock.return_value = b_mock + ssl_mock.return_value.wrap_socket.return_value = b_mock manager_mock.assert_not_called() monitor_mock.assert_not_called() @@ -271,7 +271,7 @@ def test_start(self, # Test that a NetworkingError is generated if the socket bind fails. with mock.patch('socket.socket') as socket_mock: - with mock.patch('ssl.wrap_socket') as ssl_mock: + with mock.patch('ssl.SSLContext.wrap_socket') as ssl_mock: socket_mock.return_value = a_mock ssl_mock.return_value = b_mock