1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-12 13:20:55 +00:00

[mbedTLS] Keep reading/writing partial until "would block"

Them mbedTLS read and write functions will never read or write more than
the negotiated fragment length at every iteration (which usually depends
on MBEDTLS_SSL_IN_CONTENT_LEN and MBEDTLS_SSL_OUT_CONTENT_LEN).

For this reason, when reading or writing partial data, we must always
keep retrying until we receive a "would block" (no bytes read or wrote),
or we have fulfilled the read or write.
This commit is contained in:
Fabio Alessandrelli
2024-11-09 16:58:16 +01:00
parent 36e6207bb4
commit 35c223680a

View File

@@ -166,10 +166,11 @@ Error StreamPeerMbedTLS::put_partial_data(const uint8_t *p_data, int p_bytes, in
return OK; return OK;
} }
int ret = mbedtls_ssl_write(tls_ctx->get_context(), p_data, p_bytes); do {
int ret = mbedtls_ssl_write(tls_ctx->get_context(), &p_data[r_sent], p_bytes - r_sent);
if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) { if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
// Non blocking IO // Non blocking IO.
ret = 0; break;
} else if (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) { } else if (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) {
// Clean close // Clean close
disconnect_from_stream(); disconnect_from_stream();
@@ -179,8 +180,10 @@ Error StreamPeerMbedTLS::put_partial_data(const uint8_t *p_data, int p_bytes, in
disconnect_from_stream(); disconnect_from_stream();
return ERR_CONNECTION_ERROR; return ERR_CONNECTION_ERROR;
} }
r_sent += ret;
} while (r_sent < p_bytes);
r_sent = ret;
return OK; return OK;
} }
@@ -209,9 +212,11 @@ Error StreamPeerMbedTLS::get_partial_data(uint8_t *p_buffer, int p_bytes, int &r
r_received = 0; r_received = 0;
int ret = mbedtls_ssl_read(tls_ctx->get_context(), p_buffer, p_bytes); do {
int ret = mbedtls_ssl_read(tls_ctx->get_context(), &p_buffer[r_received], p_bytes - r_received);
if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) { if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
ret = 0; // non blocking io // Non blocking IO.
break;
} else if (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) { } else if (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) {
// Clean close // Clean close
disconnect_from_stream(); disconnect_from_stream();
@@ -222,7 +227,10 @@ Error StreamPeerMbedTLS::get_partial_data(uint8_t *p_buffer, int p_bytes, int &r
return ERR_CONNECTION_ERROR; return ERR_CONNECTION_ERROR;
} }
r_received = ret; r_received += ret;
} while (r_received < p_bytes);
return OK; return OK;
} }