8000 ssl: Fix "timed out" exceptions · ladyada/circuitpython@3e029a9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3e029a9

Browse files
committed
ssl: Fix "timed out" exceptions
Incorrect error handling in send/recv would raise an OSError with an incorrect (negative) code. It's likely that this bug was always happening in the Pico W implementation, which became the basis of the current shared implementation. Push handling of WANT_{READ,WRITE} down into mbedtls_raise_error and use it in recv_into and send. Tested by connecting to google.com:443, sending nothing, and trying to read a byte: ```py import socketpool, ssl, time, wifi socket = socketpool.SocketPool(wifi.radio) ctx = ssl.SSLContext() with ctx.wrap_socket(socket.socket()) as ss: ss.connect(("google.com", 443)) ss.settimeout(1) b = bytearray(1) try: t0 = time.monotonic() ss.recv_into(b) except Exception as ee: t1 = time.monotonic() exc = ee print(t1-t0) raise exc ``` As desired, an exception `OSError: [Errno 116] ETIMEDOUT` occurred and the time delta value was 1.0 seconds. (tested on pycamera) Closes: adafruit#8988
1 parent 4a335af commit 3e029a9

File tree

1 file changed

+6
-18
lines changed

1 file changed

+6
-18
lines changed

shared-module/ssl/SSLSocket.c

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ STATIC NORETURN void mbedtls_raise_error(int err) {
7171
mp_raise_OSError(-err);
7272
}
7373

74+
if (err == MBEDTLS_ERR_SSL_WANT_WRITE || err == MBEDTLS_ERR_SSL_WANT_READ) {
75+
mp_raise_OSError(MP_EWOULDBLOCK);
76+
}
77+
7478
#if defined(MBEDTLS_ERROR_C)
7579
// Including mbedtls_strerror takes about 1.5KB due to the error strings.
7680
// MBEDTLS_ERROR_C is the define used by mbedtls to conditionally include mbedtls_strerror.
@@ -271,16 +275,8 @@ mp_uint_t common_hal_ssl_sslsocket_recv_into(ssl_sslsocket_obj_t *self, uint8_t
271275
DEBUG_PRINT("returning %d\n", ret);
272276
return ret;
273277
}
274-
if (ret == MBEDTLS_ERR_SSL_WANT_READ) {
275-
ret = MP_EWOULDBLOCK;
276-
} else if (ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
277-
// If handshake is not finished, read attempt may end up in protocol
278-
// wanting to write next handshake message. The same may happen with
279-
// renegotiation.
280-
ret = MP_EWOULDBLOCK;
281-
}
282278
DEBUG_PRINT("raising errno [error case] %d\n", ret);
283-
mp_raise_OSError(ret);
279+
mbedtls_raise_error(ret);
284280
}
285281

286282
mp_uint_t common_hal_ssl_sslsocket_send(ssl_sslsocket_obj_t *self, const uint8_t *buf, uint32_t len) {
@@ -290,16 +286,8 @@ mp_uint_t common_hal_ssl_sslsocket_send(ssl_sslsocket_obj_t *self, const uint8_t
290286
DEBUG_PRINT("returning %d\n", ret);
291287
return ret;
292288
}
293-
if (ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
294-
ret = MP_EWOULDBLOCK;
295-
} else if (ret == MBEDTLS_ERR_SSL_WANT_READ) {
296-
// If handshake is not finished, write attempt may end up in protocol
297-
// wanting to read next handshake message. The same may happen with
298-
// renegotiation.
299-
ret = MP_EWOULDBLOCK;
300-
}
301289
DEBUG_PRINT("raising errno [error case] %d\n", ret);
302-
mp_raise_OSError(ret);
290+
mbedtls_raise_error(ret);
303291
}
304292

305293
size_t common_hal_ssl_sslsocket_bind(ssl_sslsocket_obj_t *self, const char *host, size_t hostlen, uint32_t port) {

0 commit comments

Comments
 (0)
0