Skip to content

Commit dd05ab5

Browse files
committed
Added OpenSSL.SSL.Connection.set_info_callback
1 parent 5619943 commit dd05ab5

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Changes:
1616
^^^^^^^^
1717

1818
- Added ``OpenSSL.SSL.Context.set_tls13_ciphersuites`` that allows the allowed TLS 1.3 ciphers.
19+
- Added ``OpenSSL.SSL.Connection.set_info_callback``
1920

2021
25.2.0 (UNRELEASED)
2122
-------------------

src/OpenSSL/SSL.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3233,3 +3233,27 @@ def request_ocsp(self) -> None:
32333233
self._ssl, _lib.TLSEXT_STATUSTYPE_ocsp
32343234
)
32353235
_openssl_assert(rc == 1)
3236+
3237+
def set_info_callback(
3238+
self, callback: Callable[[Connection, int, int], None]
3239+
) -> None:
3240+
"""
3241+
Set the information callback to *callback*. This function will be
3242+
called from time to time during SSL handshakes.
3243+
3244+
:param callback: The Python callback to use. This should take three
3245+
arguments: a Connection object and two integers. The first integer
3246+
specifies where in the SSL handshake the function was called, and
3247+
the other the return code from a (possibly failed) internal
3248+
function call.
3249+
:return: None
3250+
"""
3251+
3252+
@wraps(callback)
3253+
def wrapper(ssl, where, return_code): # type: ignore[no-untyped-def]
3254+
callback(Connection._reverse_mapping[ssl], where, return_code)
3255+
3256+
self._info_callback = _ffi.callback(
3257+
"void (*)(const SSL *, int, int)", wrapper
3258+
)
3259+
_lib.SSL_set_info_callback(self._ssl, self._info_callback)

tests/test_ssl.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3470,6 +3470,33 @@ def test_buffer_size(self) -> None:
34703470
data = conn.bio_read(2)
34713471
assert 2 == len(data)
34723472

3473+
def test_connection_set_info_callback(self) -> None:
3474+
(server, client) = socket_pair()
3475+
3476+
context = Context(SSLv23_METHOD)
3477+
context.use_certificate(load_certificate(FILETYPE_PEM, root_cert_pem))
3478+
context.use_privatekey(load_privatekey(FILETYPE_PEM, root_key_pem))
3479+
server = Connection(context, server)
3480+
server.set_accept_state()
3481+
3482+
client = Connection(Context(SSLv23_METHOD), client)
3483+
client.set_connect_state()
3484+
3485+
called = []
3486+
3487+
def info(conn: Connection, where: int, ret: int) -> None:
3488+
assert conn is client
3489+
called.append(where)
3490+
3491+
client.set_info_callback(info)
3492+
3493+
handshake(client, server)
3494+
3495+
# Verify that the callback was actually called during handshake
3496+
assert len(called) > 0
3497+
assert SSL_CB_HANDSHAKE_START in called
3498+
assert SSL_CB_HANDSHAKE_DONE in called
3499+
34733500

34743501
class TestConnectionGetCipherList:
34753502
"""

0 commit comments

Comments
 (0)