Skip to content

Commit b76e74e

Browse files
committed
Brought in line with OpenKMIP#707
1 parent e629aed commit b76e74e

File tree

3 files changed

+25
-20
lines changed

3 files changed

+25
-20
lines changed

kmip/services/kmip_client.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -285,16 +285,17 @@ def open(self):
285285
six.reraise(*last_error)
286286

287287
def _create_socket(self, sock):
288-
context = ssl.create_default_context()
288+
context = ssl.SSLContext(self.ssl_version)
289289
context.verify_mode = self.cert_reqs
290-
context.check_hostname = False
291-
context.load_cert_chain(
292-
keyfile=self.keyfile,
293-
certfile=self.certfile
294-
)
295-
context.load_verify_locations(cafile=self.ca_certs)
290+
if self.ca_certs:
291+
context.load_verify_locations(self.ca_certs)
292+
if self.keyfile and not self.certfile:
293+
raise ValueError("certfile must be specified")
294+
if self.certfile:
295+
context.load_cert_chain(self.certfile, self.keyfile)
296296
self.socket = context.wrap_socket(
297297
sock,
298+
server_side=False,
298299
do_handshake_on_connect=self.do_handshake_on_connect,
299300
suppress_ragged_eofs=self.suppress_ragged_eofs)
300301
self.socket.settimeout(self.timeout)

kmip/services/server/server.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -287,20 +287,22 @@ def interrupt_handler(trigger, frame):
287287
for cipher in auth_suite_ciphers:
288288
self._logger.debug(cipher)
289289

290-
context = ssl.create_default_context(purpose=ssl.Purpose.CLIENT_AUTH)
290+
cafile = self.config.settings.get('ca_path')
291+
context = ssl.SSLContext(self.auth_suite.protocol)
291292
context.verify_mode = ssl.CERT_REQUIRED
292-
context.check_hostname = False
293-
context.load_cert_chain(
294-
certfile=self.config.settings.get('certificate_path'),
295-
keyfile=self.config.settings.get('key_path'),
296-
)
297-
context.load_verify_locations(cafile=self.config.settings.get('ca_path'))
298-
context.set_ciphers(self.auth_suite.ciphers)
293+
if self.auth_suite.ciphers:
294+
context.set_ciphers(self.auth_suite.ciphers)
295+
if cafile:
296+
context.load_verify_locations(cafile)
297+
certfile = self.config.settings.get('certificate_path')
298+
keyfile = self.config.settings.get('key_path')
299+
context.load_cert_chain(certfile, keyfile=keyfile)
300+
299301
self._socket = context.wrap_socket(
300302
self._socket,
301303
server_side=True,
302304
do_handshake_on_connect=False,
303-
suppress_ragged_eofs=True,
305+
suppress_ragged_eofs=True
304306
)
305307

306308
try:

kmip/tests/unit/services/server/test_server.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,10 @@ def test_start(self,
210210
# Test that in ideal cases no errors are generated and the right
211211
# log messages are.
212212
with mock.patch('socket.socket') as socket_mock:
213-
with mock.patch('ssl.wrap_socket') as ssl_mock:
213+
with mock.patch('ssl.SSLContext') as ssl_mock:
214214
socket_mock.return_value = a_mock
215-
ssl_mock.return_value = b_mock
215+
ssl_mock.return_value.wrap_socket.return_value = b_mock
216+
ssl_mock.return_value.load_cert_chain.return_value = None
216217

217218
manager_mock.assert_not_called()
218219
monitor_mock.assert_not_called()
@@ -271,9 +272,10 @@ def test_start(self,
271272

272273
# Test that a NetworkingError is generated if the socket bind fails.
273274
with mock.patch('socket.socket') as socket_mock:
274-
with mock.patch('ssl.wrap_socket') as ssl_mock:
275+
with mock.patch('ssl.SSLContext') as ssl_mock:
275276
socket_mock.return_value = a_mock
276-
ssl_mock.return_value = b_mock
277+
ssl_mock.return_value.wrap_socket.return_value = b_mock
278+
ssl_mock.return_value.load_cert_chain.return_value = None
277279

278280
test_exception = Exception()
279281
b_mock.bind.side_effect = test_exception

0 commit comments

Comments
 (0)